From faaf8adec916168b2444743391b2adc9daac0af9 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 19 Oct 2018 11:06:52 -0400 Subject: [PATCH] Add support for a "pw-optional" or "data-pw-optional" attribute to Markup Regions. When a defined region of markup has this boolean attribute present, the wrapping region tags will be removed from the document if they will be blank (i.e. nothing was populated to them). --- wire/core/WireMarkupRegions.php | 40 ++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/wire/core/WireMarkupRegions.php b/wire/core/WireMarkupRegions.php index b82c6bdd..753906c3 100644 --- a/wire/core/WireMarkupRegions.php +++ b/wire/core/WireMarkupRegions.php @@ -177,6 +177,7 @@ class WireMarkupRegions extends Wire { // build the region (everything in $markupAfter that starts after the ">") $regionMarkup = empty($tagInfo['close']) ? '' : substr($markupAfter, $closeOpenTagPos + 1); $region = $this->getTagRegion($regionMarkup, $tagInfo, $options); + $region['startPos'] = $openTagPos; if($options['single']) { // single mode means we just return the markup @@ -863,6 +864,36 @@ class WireMarkupRegions extends Wire { return $markup; } + + /** + * Strip optional tags/comments from given markup + * + * @param string $markup + * @return string + * + */ + public function stripOptional($markup) { + $attrs = array('data-pw-optional', 'pw-optional'); + foreach($attrs as $attrName) { + if(strpos($markup, " $attrName") === false) continue; + $regions = $this->find($attrName, $markup, array('verbose' => true)); + foreach($regions as $region) { + $pos = strpos($markup, $region['html']); + if($pos === false) continue; + $content = trim($region['region']); + if(strlen($content)) { + // not empty, remove just the pw-optional attribute + $open = $region['open']; + $open = str_ireplace(" $attrName", '', $open); + $markup = substr($markup, 0, $pos) . $open . substr($markup, $pos + strlen($region['open'])); + } else { + // empty optional region, can be removed + $markup = substr($markup, 0, $pos) . substr($markup, $pos + strlen($region['html'])); + } + } + } + return $markup; + } /** * Merge attributes from one HTML tag to another @@ -1327,7 +1358,10 @@ class WireMarkupRegions extends Wire { if(!count($regions)) { if($debug) $htmlDocument = str_replace($debugLandmark, "
No regions
$debugLandmark", $htmlDocument); $recursionLevel--; - if(!$recursionLevel) $this->removeRegionTags($htmlDocument); + if(!$recursionLevel) { + $this->removeRegionTags($htmlDocument); + if(strpos($htmlDocument, 'pw-optional')) $htmlDocument = $this->stripOptional($htmlDocument); + } return 0; } @@ -1481,6 +1515,10 @@ class WireMarkupRegions extends Wire { } $recursionLevel--; + + if(!$recursionLevel && strpos($htmlDocument, 'pw-optional')) { + $htmlDocument = $this->stripOptional($htmlDocument); + } return $numUpdates; }