Skip to content

Commit

Permalink
FIX Don't strip <header> tag from HTMLValue
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jul 9, 2024
1 parent 1943f9d commit e68b65c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
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|body)(\s[^>]*)?>#si', '', $content);
$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
30 changes: 29 additions & 1 deletion tests/php/View/Parsers/HTMLValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testInvalidHTMLParsing()
'<meta content="text/html"></meta>' => '<meta content="text/html">',
'<p><div class="example"></div><p>' => '<p></p><div class="example"></div><p></p>',
'<html><html><body><falsetag "attribute=""attribute""">' => '<falsetag></falsetag>',
'<body<body<body>/bodu>/body>' => '/bodu&gt;/body&gt;'
'<div<div<div>/dib>/div>' => '<div><div><div>/dib&gt;/div&gt;</div></div></div>'
];

foreach ($invalid as $input => $expected) {
Expand Down 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>',
],
[
'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');
}
}

0 comments on commit e68b65c

Please sign in to comment.