From fa1ff60b97b0f6ea8f976b95bb53f0247b5522f9 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Wed, 2 Aug 2017 10:56:26 -0400 Subject: [PATCH] Add 2nd pass support to Markup Regions, so that a region added in 1st pass can be manipulated by commands in the 2nd pass. Also, some optimizations to region hints, and debug mode improvements. --- wire/core/WireMarkupRegions.php | 120 ++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 35 deletions(-) diff --git a/wire/core/WireMarkupRegions.php b/wire/core/WireMarkupRegions.php index 36ac85fd..62ae1991 100644 --- a/wire/core/WireMarkupRegions.php +++ b/wire/core/WireMarkupRegions.php @@ -84,7 +84,7 @@ class WireMarkupRegions extends Wire { * - `leftover` (bool): Specify true if you want to return a "leftover" key in return value with leftover markup. * @return array Returns one of the following: * - Associative array of [ 'id' => 'markup' ] when finding specific attributes or #id attributes. - * - Regular array of markup regions when finding region shaving a specific class attribute. + * - Regular array of markup regions when finding regions having a specific class attribute. * - Associative array of verbose information when the verbose option is used. * @throws WireException if given invalid $find string * @@ -498,7 +498,14 @@ class WireMarkupRegions extends Wire { 'html' => '', // region with wrapping tags ); + if($tagInfo['pwid']) $tagID = $tagInfo['pwid']; + else if(!empty($tagInfo['id'])) $tagID = $tagInfo['id']; + else if(!empty($tagInfo['attrs']['id'])) $tagID = $tagInfo['attrs']['id']; + else if(!empty($tagInfo['actionTarget'])) $tagID = $tagInfo['actionTarget']; + else $tagID = ''; + $selfClose = empty($tagInfo['close']); + $closeHint = "$tagInfo[close]"; $closeQty = $selfClose ? 1 : substr_count($region, $tagInfo['close']); if(!$closeQty) { @@ -508,19 +515,17 @@ class WireMarkupRegions extends Wire { } else if($selfClose) { $region = ''; if($verbose) $verboseRegion['details'] = 'Self closing tag (empty region)'; - + + } else if($tagID && false !== ($pos = strpos($region, $closeHint))) { + // close tag indicates what it closes, i.e. “” + $region = substr($region, 0, $pos); + $tagInfo['close'] = $closeHint; + if($verbose) $verboseRegion['details'] = "Fast match with HTML comment hint"; + } else if($closeQty === 1) { // just one close tag present, making our job easy $region = substr($region, 0, strrpos($region, $tagInfo['close'])); - if($verbose) { - $verboseRegion['details'] = 'Only 1 possible closing tag: ' . $tagInfo['close']; - } - - } else if($tagInfo['pwid'] && false !== ($pos = strpos($region, "$tagInfo[close]"))) { - // close tag indicates what it closes, i.e. “” - $region = substr($region, 0, $pos); - $tagInfo['close'] = "$tagInfo[close]"; - if($verbose) $verboseRegion['details'] = "Fast match with HTML comment hint"; + if($verbose) $verboseRegion['details'] = "Only 1 possible closing tag: $tagInfo[close]"; } else { // multiple close tags present, must figure out which is the right one @@ -1293,6 +1298,7 @@ class WireMarkupRegions extends Wire { $leftoverMarkup = ''; } else if($this->hasRegions($htmlRegions)) { + $htmlRegions = $this->stripRegions('