mirror of
https://github.com/processwire/processwire.git
synced 2025-08-14 18:55:56 +02:00
Add support for closing tag hints in WireMarkupRegions. These increase speed/efficiency for large documents.
This commit is contained in:
@@ -516,6 +516,12 @@ class WireMarkupRegions extends Wire {
|
|||||||
$verboseRegion['details'] = 'Only 1 possible closing tag: ' . $tagInfo['close'];
|
$verboseRegion['details'] = 'Only 1 possible closing tag: ' . $tagInfo['close'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if($tagInfo['pwid'] && false !== ($pos = strpos($region, "$tagInfo[close]<!--#$tagInfo[pwid]-->"))) {
|
||||||
|
// close tag indicates what it closes, i.e. “</div><!--#content-->”
|
||||||
|
$region = substr($region, 0, $pos);
|
||||||
|
$tagInfo['close'] = "$tagInfo[close]<!--#$tagInfo[pwid]-->";
|
||||||
|
if($verbose) $verboseRegion['details'] = "Fast match with HTML comment hint";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// multiple close tags present, must figure out which is the right one
|
// multiple close tags present, must figure out which is the right one
|
||||||
$testStart = 0;
|
$testStart = 0;
|
||||||
@@ -523,9 +529,15 @@ class WireMarkupRegions extends Wire {
|
|||||||
$maxDoCnt = 100000;
|
$maxDoCnt = 100000;
|
||||||
$openTag1 = "<$tagInfo[name]>";
|
$openTag1 = "<$tagInfo[name]>";
|
||||||
$openTag2 = "<$tagInfo[name] ";
|
$openTag2 = "<$tagInfo[name] ";
|
||||||
|
$fail = false;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$doCnt++;
|
$doCnt++;
|
||||||
$testPos = stripos($region, $tagInfo['close'], $testStart);
|
$testPos = stripos($region, $tagInfo['close'], $testStart);
|
||||||
|
if($testPos === false) {
|
||||||
|
$fail = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
$test = substr($region, 0, $testPos);
|
$test = substr($region, 0, $testPos);
|
||||||
$openCnt = substr_count($test, $openTag1) + substr_count($test, $openTag2);
|
$openCnt = substr_count($test, $openTag1) + substr_count($test, $openTag2);
|
||||||
$closeCnt = substr_count($test, $tagInfo['close']);
|
$closeCnt = substr_count($test, $tagInfo['close']);
|
||||||
@@ -539,7 +551,14 @@ class WireMarkupRegions extends Wire {
|
|||||||
}
|
}
|
||||||
} while($doCnt < $maxDoCnt && $testStart < strlen($region));
|
} while($doCnt < $maxDoCnt && $testStart < strlen($region));
|
||||||
|
|
||||||
if($doCnt >= $maxDoCnt) {
|
if($fail) {
|
||||||
|
if($verbose) {
|
||||||
|
$verboseRegion['error'] = true;
|
||||||
|
$verboseRegion['details'] = "Failed to find closing tag $tagInfo[close] after $doCnt iterations";
|
||||||
|
} else {
|
||||||
|
$region = 'error';
|
||||||
|
}
|
||||||
|
} else if($doCnt >= $maxDoCnt) {
|
||||||
if($verbose) {
|
if($verbose) {
|
||||||
$verboseRegion['error'] = true;
|
$verboseRegion['error'] = true;
|
||||||
$verboseRegion['details'] = "Failed region match after $doCnt tests for <$tagInfo[name]> tag(s)";
|
$verboseRegion['details'] = "Failed region match after $doCnt tests for <$tagInfo[name]> tag(s)";
|
||||||
@@ -794,6 +813,15 @@ class WireMarkupRegions extends Wire {
|
|||||||
|
|
||||||
$open = strpos($tag, '<') === 0 ? $tag : "<$tag";
|
$open = strpos($tag, '<') === 0 ? $tag : "<$tag";
|
||||||
$close = $tag == '<!--' ? '-->' : '</' . trim($tag, '<>') . '>';
|
$close = $tag == '<!--' ? '-->' : '</' . trim($tag, '<>') . '>';
|
||||||
|
|
||||||
|
// keep comments that start with <!--#
|
||||||
|
if($tag == "<!--" && strpos($markup, "<!--#") !== false) {
|
||||||
|
$hasHints = true;
|
||||||
|
$markup = str_replace("<!--#", "<!~~#", $markup);
|
||||||
|
$markup = preg_replace('/<!--#([-_.a-zA-Z0-9]+)-->/', '<!~~$1~~>', $markup);
|
||||||
|
} else {
|
||||||
|
$hasHints = false;
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$pos = stripos($markup, $open, $startPos);
|
$pos = stripos($markup, $open, $startPos);
|
||||||
@@ -811,6 +839,11 @@ class WireMarkupRegions extends Wire {
|
|||||||
if($getRegions) return $regions;
|
if($getRegions) return $regions;
|
||||||
if(count($regions)) $markup = str_replace($regions, '', $markup);
|
if(count($regions)) $markup = str_replace($regions, '', $markup);
|
||||||
|
|
||||||
|
if($hasHints) {
|
||||||
|
// keep comments that start with <!--#
|
||||||
|
$markup = str_replace(array("<!~~#", "~~>"), array("<!--#", "-->"), $markup);
|
||||||
|
}
|
||||||
|
|
||||||
return $markup;
|
return $markup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user