Merge branch '5.2' into 5

This commit is contained in:
Guy Sartorelli 2024-07-10 10:04:52 +12:00
commit 3829160151
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
2 changed files with 29 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class HTMLValue extends ViewableData
*/ */
public function setContent($content) public function setContent($content)
{ {
$content = preg_replace('#</?(html|head|body)[^>]*>#si', '', $content); $content = preg_replace('#</?(html|head(?!er)|body)[^>]*>#si', '', $content);
$html5 = new HTML5(['disable_html_ns' => true]); $html5 = new HTML5(['disable_html_ns' => true]);
$document = $html5->loadHTML( $document = $html5->loadHTML(
'<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' . '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>' .

View File

@ -160,4 +160,32 @@ class HTMLValueTest extends SapphireTest
$this->assertEquals($noscript, $value->getContent(), 'Child tags are left untouched in noscript tags.'); $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');
}
} }