Skip to content

Commit

Permalink
If there's already a link[rel=profile], don't create another
Browse files Browse the repository at this point in the history
For example, Twenty Twenty has a link[rel=profile],
though it doesn't have a head[profile].
  • Loading branch information
kienstra committed Jan 29, 2020
1 parent 0484342 commit 7f60c5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head> as a link[rel=profile] and strip the attribute.
* So if it exists and there's no link[rel=profile], add it to the <head> 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 <link> and append it to <head>.
$new_link = $this->createElement( 'link' );
$new_link->setAttribute( 'rel', 'profile' );
$new_link->setAttribute( 'href', $profile );
$this->head->appendChild( $new_link );
$this->head->removeAttribute( 'profile' );
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/php/test-class-amp-dom-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public function data_dom_document() {
'<!DOCTYPE html><html><head profile="https://example.com"></head><body></body></html>',
'<!DOCTYPE html><html><head><meta charset="utf-8"><link rel="profile" href="https://example.com"></head><body></body></html>',
],
'link_rel_profile_already_exists' => [
'utf-8',
'<!DOCTYPE html><html><head profile="https://example.com"><link rel="profile" href="https://foo.com"></head><body></body></html>',
'<!DOCTYPE html><html><head><meta charset="utf-8"><link rel="profile" href="https://foo.com"></head><body></body></html>',
],
];
}

Expand Down

0 comments on commit 7f60c5c

Please sign in to comment.