Skip to content

Commit

Permalink
Merge pull request #4193 from ampproject/add/head-profile-conversion
Browse files Browse the repository at this point in the history
Convert `head[profile]` attribute to `link[rel=profile]`
  • Loading branch information
westonruter authored Jan 29, 2020
2 parents 6021df7 + 6ebae07 commit b04f801
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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

0 comments on commit b04f801

Please sign in to comment.