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

FIX Don't strip <header> tag from HTMLValue #11302

Merged
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
2 changes: 1 addition & 1 deletion src/View/Parsers/HTMLValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct($fragment = null)
*/
public function setContent($content)
{
$content = preg_replace('#</?(html|head|body)[^>]*>#si', '', $content);
$content = preg_replace('#</?(html|head(?!er)|body)[^>]*>#si', '', $content);
Copy link
Contributor

@michalkleiner michalkleiner Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment here — I know it's cooler to use more regex foo, but imho here it would be much easier for anyone reading the list of tags if it was explicitly listed as html|head|body|header.

$html5 = new HTML5(['disable_html_ns' => true]);
$document = $html5->loadHTML(
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .
Expand Down
28 changes: 28 additions & 0 deletions tests/php/View/Parsers/HTMLValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,32 @@ public function testValidHTMLInNoscriptTags()
$this->assertEquals($noscript, $value->getContent(), 'Child tags are left untouched in noscript tags.');
}
}

public function provideOnlyStripIntendedTags(): array
{
return [
[
'input' => '<html><head></head><body><div><p>blahblah</p></div></body></html>',
'expected' => '<div><p>blahblah</p></div>',
],
[
'input' => '<html><head></head><body><header></header><div><p>blahblah</p></div></body></html>',
'expected' => '<header></header><div><p>blahblah</p></div>',
],
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
[
'input' => '<html some-attribute another-attribute="something"><head></head><body><div><p>blahblah</p></div></body></html>',
'expected' => '<div><p>blahblah</p></div>',
],
];
}

/**
* @dataProvider provideOnlyStripIntendedTags
*/
public function testOnlyStripIntendedTags(string $input, string $expected): void
{
$value = new HTMLValue();
$value->setContent($input);
$this->assertEquals($expected, $value->getContent(), 'Invalid HTML can be parsed');
}
}
Loading