Skip to content

Commit

Permalink
Merge changes from #4193
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Mar 7, 2020
1 parent 455b094 commit e34c9fc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/common/src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface Attribute
const LOOP = 'loop';
const MEDIA = 'media';
const NAME = 'name';
const PROFILE = 'profile';
const REL = 'rel';
const ROLE = 'role';
const SIZES = 'sizes';
Expand Down
24 changes: 24 additions & 0 deletions lib/common/src/Dom/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public function loadHTML($source, $options = 0)
$this->deduplicateTag(Tag::HEAD);
$this->deduplicateTag(Tag::BODY);
$this->moveInvalidHeadNodesToBody();
$this->convertHeadProfileToLink();
}

return $success;
Expand Down Expand Up @@ -643,6 +644,29 @@ private function moveInvalidHeadNodesToBody()
}
}

/**
* 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 convertHeadProfileToLink()
{
if (! $this->head->hasAttribute(Attribute::PROFILE)) {
return;
}

$profile = $this->head->getAttribute(Attribute::PROFILE);
if ($profile) {
$link = $this->createElement(Tag::LINK);
$link->setAttribute(Attribute::REL, Attribute::PROFILE);
$link->setAttribute(Attribute::HREF, $profile);
$this->head->appendChild($link);
}

$this->head->removeAttribute(Attribute::PROFILE);
}

/**
* Force all self-closing tags to have closing tags.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/common/tests/Dom/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ public function dataDomDocument()
"<!DOCTYPE html> \n <!-- before \n <html> --> \n <html> \n <!-- before \n <head> ----> \n <head><meta charset=\"utf-8\"> \n <!-- within \n <head> ----> \n </head> \n <!-- before \n <body> ----> \n <body class=\"something\" data-something=\"something\"> \n <!-- within \n <body> ----> \n </body> \n <!-- after \n </body> ----> \n </html> \n <!-- after \n </html> --> \n",
"<!DOCTYPE html> \n <!-- before \n <html> --> \n <html> \n <!-- before \n <head> ----> \n <head><meta charset=\"utf-8\"> \n <!-- within \n <head> ----> \n </head> \n <!-- before \n <body> ----> \n <body class=\"something\" data-something=\"something\"> \n <!-- within \n <body> ----> \n </body> \n <!-- after \n </body> ----> \n </html> \n <!-- after \n </html> --> \n",
],
'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 e34c9fc

Please sign in to comment.