Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert head[profile] attribute to link[rel=profile] #4193

Merged
merged 5 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -536,6 +537,35 @@ 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 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 ) {
westonruter marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
westonruter marked this conversation as resolved.
Show resolved Hide resolved

// 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' );
}

/**
* Force all self-closing tags to have closing tags.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/php/test-class-amp-dom-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ public function data_dom_document() {
'<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]--><!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]--><!--[if IE 8]> <html class="lt-ie9"> <![endif]--><!--[if gt IE 8]><!--> <html class=""> <!--<![endif]--></html>',
'<!DOCTYPE html><html class="">' . $head . '<body></body></html>',
],
'link_attribute_in_head_converted_to_link' => [
'utf-8',
'<!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