Skip to content

Commit

Permalink
Replace AMP binding attributes with ones that can be parsed in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 24, 2018
1 parent b2ce0fe commit ba8c9cd
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions includes/utils/class-amp-dom-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public static function get_dom( $document ) {

$dom = new DOMDocument();

// Replace AMP binding attributes with something that libxml can parse (as HTML5 data-* attributes).
$document = preg_replace_callback(
// Match all start tags that contain a binding attribute.
'#<(?P<tag>\w\S+)(?P<attrs>\s+[^<]+\]=[^<]+)\s*>#',
function( $matches ) {
$attrs = preg_replace( '/\s\[([a-z0-9_\-]+)\]=/i', ' data-amp-binding---$1=', $matches['attrs'] );
// @todo What if someone does data-foo=" [text]='somthing bad' "?
return '<' . $matches['tag'] . $attrs . '>';
},
$document
);

/*
* Wrap in dummy tags, since XML needs one parent node.
* It also makes it easier to loop through nodes.
Expand Down

0 comments on commit ba8c9cd

Please sign in to comment.