Skip to content

Commit

Permalink
feat: add directive for removing blank nodes (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-curtis authored Apr 12, 2024
1 parent 4828fdf commit c9d60c9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/HTMLPurifier/ConfigSchema/schema.ser

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions library/HTMLPurifier/ConfigSchema/schema/Core.RemoveBlanks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Core.RemoveBlanks
TYPE: bool
DEFAULT: false
VERSION: 4.18
--DESCRIPTION--
<p>
If set to true, blank nodes will be removed. This can be useful for maintaining
backwards compatibility when upgrading from previous versions of PHP.
</p>
--# vim: et sw=4 sts=4
3 changes: 3 additions & 0 deletions library/HTMLPurifier/Lexer/DOMLex.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function tokenizeHTML($html, $config, $context)
if ($config->get('Core.AllowParseManyTags') && defined('LIBXML_PARSEHUGE')) {
$options |= LIBXML_PARSEHUGE;
}
if ($config->get('Core.RemoveBlanks') && defined('LIBXML_NOBLANKS')) {
$options |= LIBXML_NOBLANKS;
}

set_error_handler(array($this, 'muteErrorHandler'));
// loadHTML() fails on PHP 5.3 when second parameter is given
Expand Down
1 change: 1 addition & 0 deletions plugins/phorum/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
$config->set('Core.EscapeNonASCIICharacters', true);
}
$config->set('Core.AllowParseManyTags', false);
$config->set('Core.RemoveBlanks', false);

// vim: et sw=4 sts=4
10 changes: 10 additions & 0 deletions tests/HTMLPurifier/FixtureData/RemoveBlankTestCaseInput.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<table>
<caption>
Cool Table
</caption>
<tr>
<td>
Element 1
</td>
</tr>
</table>
7 changes: 7 additions & 0 deletions tests/HTMLPurifier/FixtureData/RemoveBlankTestCaseOutput.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<table><caption>
Cool Table
</caption>
<tr><td>
Element 1
</td>
</tr></table>
13 changes: 13 additions & 0 deletions tests/HTMLPurifier/HTMLDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ public function test_manyNestedTags()
$this->assertIdentical($input, $output);
}

public function test_removeBlanks()
{
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.RemoveBlanks', true);

$input = file_get_contents(__DIR__ . '/FixtureData/RemoveBlankTestCaseInput.html');
$expected = file_get_contents(__DIR__ . '/FixtureData/RemoveBlankTestCaseOutput.html');

$purifier = new HTMLPurifier($config);
$actual = $purifier->purify($input);
$this->assertIdentical($expected, $actual);
}

}

// vim: et sw=4 sts=4

0 comments on commit c9d60c9

Please sign in to comment.