1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Update WireMarkupRegions to remove class attribute-based action support, since our final spec uses action attributes instead. It only does this for PW installations installed today or later, just in case anyone is using some older examples on an existing site. If you want to force use of the newer version (which is more efficient) set $config->useMarkupRegions=2; The updates made in this commit might also fix processwire/processwire-issues#294 but have not yet confirmed.

This commit is contained in:
Ryan Cramer
2017-06-22 08:45:16 -04:00
parent 3348f3f13a
commit 763963ec39
2 changed files with 104 additions and 41 deletions

View File

@@ -131,11 +131,22 @@ class WireMarkupRegions extends Wire {
$positions = array(); $positions = array();
foreach($tests as $str) { foreach($tests as $str) {
$pos = stripos($markup, $str, $startPos); $pos = stripos($markup, $str, $startPos);
if($pos !== false) { if($pos === false) continue;
if($selector === '.pw-*') {
// extra validation for .pw-* selectors to confirm they match a pw-[action] (legacy support)
$testAction = '';
$testPW = substr($markup, $pos, 20);
foreach($this->actions as $testAction) {
if(strpos($testPW, $testAction) !== false) {
$testAction = true;
break;
}
}
if($testAction !== true) continue; // if not a pw-[action] then skip
}
if($str[0] == '<') $pos++; // HTML tag match, bump+1 to enable match if($str[0] == '<') $pos++; // HTML tag match, bump+1 to enable match
$positions[$pos] = $pos; $positions[$pos] = $pos;
} }
}
// if no tests matched, we can abort now // if no tests matched, we can abort now
if(empty($positions)) break; if(empty($positions)) break;
@@ -318,6 +329,7 @@ class WireMarkupRegions extends Wire {
} }
$c = substr($find, 0, 1); $c = substr($find, 0, 1);
$z = substr($find, -1);
if($c === '#') { if($c === '#') {
// match an id, pw-id or data-pw-id attribute // match an id, pw-id or data-pw-id attribute
@@ -329,7 +341,42 @@ class WireMarkupRegions extends Wire {
$finds[] = " $attr=$find>"; $finds[] = " $attr=$find>";
} }
} else if($c === '.' && substr($find, -1) == '*') { } else if($find === '[pw-action]') {
// find pw-[action] boolean attributes
foreach($this->actions as $action) {
$finds[] = " data-pw-$action ";
$finds[] = " data-pw-$action>";
$finds[] = " data-pw-$action=";
$finds[] = " pw-$action ";
$finds[] = " pw-$action>";
$finds[] = " pw-$action=";
}
/*
} else if($c === '[' && $z === ']') {
// find any attribute (not currently used by markup regions)
if(strpos($find, '=') === false) {
// match an attribute only
$attr = trim($find, '[]*+');
$tail = substr($find, -2);
if($tail === '*]') {
// match an attribute prefix
$finds = array(" $attr");
} else {
// match a whole attribute name
$finds = array(
" $attr ",
" $attr>",
" $attr=",
);
}
} else {
// match attribute and value (not yet implemented)
}
*/
} else if($c === '.' && $z === '*') {
// match a class name prefix or action prefix // match a class name prefix or action prefix
$find = trim($find, '.*'); $find = trim($find, '.*');
$finds = array( $finds = array(
@@ -369,7 +416,7 @@ class WireMarkupRegions extends Wire {
} else if(strpos($find, '=') !== false) { } else if(strpos($find, '=') !== false) {
// some other specified attribute in attr=value format // some other specified attribute in attr=value format
if(strpos($find, '[') !== false && substr($find, -1) === ']') { if(strpos($find, '[') !== false && $z === ']') {
// i.e. div[attr=value] // i.e. div[attr=value]
list($findTag, $find) = explode('[', $find, 2); list($findTag, $find) = explode('[', $find, 2);
$find = rtrim($find, ']'); $find = rtrim($find, ']');
@@ -395,7 +442,7 @@ class WireMarkupRegions extends Wire {
} }
} }
} else if(substr($find, -1) == '*') { } else if($z === '*') {
// data-pw-* matches non-value attribute starting with a prefix // data-pw-* matches non-value attribute starting with a prefix
$finds = array( $finds = array(
" $find", " $find",
@@ -465,7 +512,9 @@ class WireMarkupRegions extends Wire {
} else if($closeQty === 1) { } else if($closeQty === 1) {
// just one close tag present, making our job easy // just one close tag present, making our job easy
$region = substr($region, 0, strrpos($region, $tagInfo['close'])); $region = substr($region, 0, strrpos($region, $tagInfo['close']));
if($verbose) $verboseRegion['details'] = 'Only 1 possible closing tag'; if($verbose) {
$verboseRegion['details'] = 'Only 1 possible closing tag: ' . $tagInfo['close'];
}
} 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
@@ -1132,7 +1181,7 @@ class WireMarkupRegions extends Wire {
* definitions, though can be used with or without it. * definitions, though can be used with or without it.
* *
* Beyond replacement of elements, append, prepend, insert before, insert after, and remove are also * Beyond replacement of elements, append, prepend, insert before, insert after, and remove are also
* supported via “pw-” prefix classes that you can add. The classes do not appear in the final output * supported via “pw-” prefix attributes that you can add. The attributes do not appear in the final output
* markup. When performing replacements or modifications to elements, PW will merge the attributes * markup. When performing replacements or modifications to elements, PW will merge the attributes
* so that attributes present in the final output are present, plus any that were added by the markup * so that attributes present in the final output are present, plus any that were added by the markup
* regions. See the examples for more details. * regions. See the examples for more details.
@@ -1147,50 +1196,43 @@ class WireMarkupRegions extends Wire {
* *
* In the examples, a “pw-id” or “data-pw-id” attribute may be used instead of an “id” attribute, when * In the examples, a “pw-id” or “data-pw-id” attribute may be used instead of an “id” attribute, when
* or if preferred. In addition, any “pw-” attribute may be specified as a “data-pw-” attribute if you * or if preferred. In addition, any “pw-” attribute may be specified as a “data-pw-” attribute if you
* prefer it. If you don't like using “pw-” class names, then use “pw-” attributes instead. * prefer it.
* ~~~~~~ * ~~~~~~
* Replacing and removing elements * Replacing and removing elements
* *
* <div id='main'>This replaces the #main div and merges any attributes</div> * <div id='main'>This replaces the #main div and merges any attributes</div>
* <div pw-replace='main'>This does the same as above</div> * <div pw-replace='main'>This does the same as above</div>
* * <div id='main' pw-replace>This does the same as above</div>
* <div id='main' class='pw-remove'>This removes #main completely</div> * <div pw-remove='main'>This removes the #main div</div>
* <div pw-remove='main'>This does the same as above</div> * <div id='main' pw-remove>This removes the #main div (same as above)</div>
* *
* Prepending and appending elements * Prepending and appending elements
* *
* <div id='main' class='pw-prepend'><p>This prepends #main with this p tag</p></div> * <div id='main' class='pw-prepend'><p>This prepends #main with this p tag</p></div>
* <p pw-prepend='main'>This does the same as above</p> * <p pw-prepend='main'>This does the same as above</p>
* * <div id='main' pw-append><p>This appends #main with this p tag</p></div>
* <div id='main' class='pw-append'><p>This appends #main with this p tag</p></div> * <p pw-append='main'>Removes the #main div</p>
* <p pw-append='main'>This does the same as above</p>
* *
* Modifying attributes on an existing element * Modifying attributes on an existing element
* *
* <div id='main' class='pw-prepend bar'><p>This prepends #main and adds "bar" class to main</p></div> * <div id='main' class='bar' pw-prepend><p>This prepends #main and adds "bar" class to main</p></div>
* <div id='main' class='pw-append foo'><p>This appends #main and adds a "foo" class to #main</p></div> * <div id='main' class='foo' pw-append><p>This appends #main and adds a "foo" class to #main</p></div>
* * <div id='main' title='hello' pw-append>Appends #main with this text + adds title attribute to #main</div>
* <div id='main' class='pw-append' title='hello'>Appends #main with this text + adds title attribute to #main</div> * <div id='main' class='-baz' pw-append>Appends #main with this text + removes class “baz” from #main</div>
* <div id='main' class='pw-append -baz'>Appends #main with this text + removes class “baz” from #main</div>
* *
* Inserting new elements * Inserting new elements
* *
* <h2 class='pw-before-main'>This adds an h2 headline with this text before #main</h2> * <h2 pw-before='main'>This adds an h2 headline with this text before #main</h2>
* <h2 pw-before='main'>This does the same as above</h2> * <footer pw-after='main'><p>This adds a footer element with this text after #main</p></footer>
* <div pw-append='main' class='foo'>This appends a div.foo to #main with this text</div>
* <div pw-prepend='main' class='bar'>This prepends a div.bar to #main with this text</div>
* *
* <footer class='pw-after-main'><p>This adds a footer element with this text after #main</p></footer>
* <footer pw-after='main'><p>This does the same as above</p></footer>
*
* <div class='pw-append-main foo'>This appends a div.foo to #main with this text</div>
* <div pw-append='main' class='foo'>This does the same as above</div>
*
* <div class='pw-prepend-main bar'>This prepends a div.bar to #main with this text</div>
* <div pw-prepend='main' class='bar'>This does the same as above</div>
* ~~~~~~ * ~~~~~~
* *
* @param string $htmlDocument Document to populate regions to * @param string $htmlDocument Document to populate regions to
* @param string|array $htmlRegions Markup containing regions (or regions array from a find call) * @param string|array $htmlRegions Markup containing regions (or regions array from a find call)
* @param array $options * @param array $options Options to modify behavior:
* - `useClassActions` (bool): Allow "pw-*" actions to be specified in class names? Per original/legacy spec. (default=false)
* @return int Number of updates made to $htmlDocument * @return int Number of updates made to $htmlDocument
* *
*/ */
@@ -1202,8 +1244,12 @@ class WireMarkupRegions extends Wire {
$recursionLevel++; $recursionLevel++;
$callQty++; $callQty++;
$leftoverMarkup = ''; $defaults = array(
'useClassActions' => false // allow use of "pw-*" class actions? (legacy)
);
$options = array_merge($defaults, $options);
$leftoverMarkup = '';
$debugLandmark = "<!--PW-REGION-DEBUG-->"; $debugLandmark = "<!--PW-REGION-DEBUG-->";
$hasDebugLandmark = strpos($htmlDocument, $debugLandmark) !== false; $hasDebugLandmark = strpos($htmlDocument, $debugLandmark) !== false;
$debug = $hasDebugLandmark && $this->wire('config')->debug; $debug = $hasDebugLandmark && $this->wire('config')->debug;
@@ -1214,7 +1260,8 @@ class WireMarkupRegions extends Wire {
$leftoverMarkup = ''; $leftoverMarkup = '';
} else if($this->hasRegions($htmlRegions)) { } else if($this->hasRegions($htmlRegions)) {
$regions = $this->find(".pw-*, id=", $htmlRegions, array( $selector = $options['useClassActions'] ? ".pw-*, id=" : "[pw-action], id=";
$regions = $this->find($selector, $htmlRegions, array(
'verbose' => true, 'verbose' => true,
'leftover' => true, 'leftover' => true,
'debugNote' => (isset($options['debugNote']) ? "$options[debugNote] => " : "") . "populate($callQty)", 'debugNote' => (isset($options['debugNote']) ? "$options[debugNote] => " : "") . "populate($callQty)",

View File

@@ -540,19 +540,35 @@ class PageRender extends WireData implements Module, ConfigurableModule {
* *
*/ */
protected function populateMarkupRegions(&$html) { protected function populateMarkupRegions(&$html) {
$pos = stripos($html, '<!DOCTYPE'); $pos = stripos($html, '<!DOCTYPE');
if($pos === false) { if($pos === false) {
// if no doctype match, attempt an html tag match // if no doctype match, attempt an html tag match
$pos = stripos($html, '<html'); $pos = stripos($html, '<html');
} }
if($pos) {
// if no document start, or document starts at pos==0, then nothing to populate
if(!$pos) return;
// split document at doctype/html boundary // split document at doctype/html boundary
$htmlBefore = substr($html, 0, $pos); $htmlBefore = substr($html, 0, $pos);
$html = substr($html, $pos); $html = substr($html, $pos);
$markupRegions = new WireMarkupRegions(); $markupRegions = new WireMarkupRegions();
$this->wire($markupRegions); $this->wire($markupRegions);
$markupRegions->populate($html, $htmlBefore);
$options = array('useClassActions' => true);
$config = $this->wire('config');
$version = (int) $config->useMarkupRegions;
if($config->installed >= 1498132609 || $version >= 2) {
// If PW installed after June 21, 2017 do not use legacy class actions
// as they are no longer part of the current WireMarkupRegions spec.
// Can also force this behavior by setting $config->useMarkupRegions = 2;
$options['useClassActions'] = false;
} }
$markupRegions->populate($html, $htmlBefore, $options);
} }
/** /**