diff --git a/src/Dom/Document.php b/src/Dom/Document.php index 48878088b21..4ce2dd5093b 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -541,17 +541,29 @@ 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. + * So if it exists and there's no link[rel=profile], add it to the as a link[rel=profile]. */ 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' ); + if ( ! $profile ) { + return; } + + // If there's already a head > link[rel=profile], don't create another. + $links = $this->head->getElementsByTagName( 'link' ); + foreach( $links as $link ) { + if ( 'profile' === $link->getAttribute( 'rel' ) ) { + $this->head->removeAttribute( 'profile' ); + return; + } + } + + // Create a and append it to . + $new_link = $this->createElement( 'link' ); + $new_link->setAttribute( 'rel', 'profile' ); + $new_link->setAttribute( 'href', $profile ); + $this->head->appendChild( $new_link ); + $this->head->removeAttribute( 'profile' ); } /** diff --git a/tests/php/test-class-amp-dom-document.php b/tests/php/test-class-amp-dom-document.php index f2fa7a9ba03..cb6f3383f37 100644 --- a/tests/php/test-class-amp-dom-document.php +++ b/tests/php/test-class-amp-dom-document.php @@ -169,6 +169,11 @@ public function data_dom_document() { '', '', ], + 'link_rel_profile_already_exists' => [ + 'utf-8', + '', + '', + ], ]; }