From ba8c9cd847abd01c98fe3ac617a79d8b8724c9ed Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 24 Jan 2018 01:18:02 -0800 Subject: [PATCH] Replace AMP binding attributes with ones that can be parsed in PHP --- includes/utils/class-amp-dom-utils.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/includes/utils/class-amp-dom-utils.php b/includes/utils/class-amp-dom-utils.php index 9492dd4cee5..448d9c79c50 100644 --- a/includes/utils/class-amp-dom-utils.php +++ b/includes/utils/class-amp-dom-utils.php @@ -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\w\S+)(?P\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.