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 all 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
23 changes: 23 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,28 @@ 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 isn't empty, add it to the <head> as a link[rel=profile] and strip the attribute.
*/
private function convert_head_profile_to_link() {
if ( ! $this->head->hasAttribute( 'profile' ) ) {
return;
}

$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.
*
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>',
],
'profile_attribute_in_head_moved_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>',
],
'profile_attribute_in_head_empty_string' => [
'utf-8',
'<!DOCTYPE html><html><head profile=""></head><body></body></html>',
'<!DOCTYPE html><html><head><meta charset="utf-8"></head><body></body></html>',
],
];
}

Expand Down