From 0484342735dcb32c315f7ebadfb9f1ab9d6714d5 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Wed, 29 Jan 2020 13:57:32 -0600 Subject: [PATCH] Convert head[profile] attribute to a link[rel=profile] Like Weston mentioned, that was invalid HTML5, so convert it to a link[rel=profile]. --- src/Dom/Document.php | 18 ++++++++++++++++++ tests/php/test-class-amp-dom-document.php | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/src/Dom/Document.php b/src/Dom/Document.php index 5bad55dddb0..48878088b21 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -359,6 +359,7 @@ public function loadHTML( $source, $options = 0 ) { $this->deduplicate_tag( self::TAG_HEAD ); $this->deduplicate_tag( self::TAG_BODY ); $this->move_invalid_head_nodes_to_body(); + $this->convert_head_profile_to_link(); } return $success; @@ -536,6 +537,23 @@ private function move_invalid_head_nodes_to_body() { } } + /** + * Converts a possible head[profile] attribute to link[rel=profile]. + * + * The head[profile] attribute is only valid in HTML4, not HTML5. + * So if it exists, add it to the as a link[rel=profile] and strip the attribute. + */ + private function convert_head_profile_to_link() { + $profile = $this->head->getAttribute( 'profile' ); + if ( $profile ) { + $link = $this->createElement( 'link' ); + $link->setAttribute( 'rel', 'profile' ); + $link->setAttribute( 'href', $profile ); + $this->head->appendChild( $link ); + $this->head->removeAttribute( 'profile' ); + } + } + /** * Force all self-closing tags to have closing tags. * diff --git a/tests/php/test-class-amp-dom-document.php b/tests/php/test-class-amp-dom-document.php index 199dce78f84..f2fa7a9ba03 100644 --- a/tests/php/test-class-amp-dom-document.php +++ b/tests/php/test-class-amp-dom-document.php @@ -164,6 +164,11 @@ public function data_dom_document() { ' ', '' . $head . '', ], + 'link_attribute_in_head_converted_to_link' => [ + 'utf-8', + '', + '', + ], ]; }