function FilterKernelTest::testHtmlCorrectorFilter

Same name in other branches
  1. 9 core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testHtmlCorrectorFilter()
  2. 8.9.x core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testHtmlCorrectorFilter()
  3. 10 core/modules/filter/tests/src/Kernel/FilterKernelTest.php \Drupal\Tests\filter\Kernel\FilterKernelTest::testHtmlCorrectorFilter()

Tests the HTML corrector filter.

@todo This test could really use some validity checking function.

File

core/modules/filter/tests/src/Kernel/FilterKernelTest.php, line 1071

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testHtmlCorrectorFilter() : void {
    // Tag closing.
    $f = Html::normalize('<p>text');
    $this->assertEquals('<p>text</p>', $f, 'HTML corrector -- tag closing at the end of input.');
    $f = Html::normalize('<p>text <img alt="ao');
    $this->assertEquals('<p>text <img alt="ao"></p>', $f, 'HTML corrector -- tag closing at the end of input + broken attribute.');
    $f = Html::normalize('<p>text<p><p>text');
    $this->assertEquals('<p>text</p><p></p><p>text</p>', $f, 'HTML corrector -- tag closing.');
    $f = Html::normalize("<ul><li>e1<li>e2");
    $this->assertEquals("<ul><li>e1</li><li>e2</li></ul>", $f, 'HTML corrector -- unclosed list tags.');
    $f = Html::normalize('<div id="d">content');
    $this->assertEquals('<div id="d">content</div>', $f, 'HTML corrector -- unclosed tag with attribute.');
    $f = Html::normalize('<hr><br>');
    $this->assertEquals('<hr><br>', $f, 'HTML corrector -- void element.');
    $f = Html::normalize('<P>test</P>');
    $this->assertEquals('<p>test</p>', $f, 'HTML corrector -- Convert upper cased tags to proper lowercased ones.');
    $f = Html::normalize('<P>test</p>');
    $this->assertEquals('<p>test</p>', $f, 'HTML corrector -- Convert upper cased tags to proper lowercased ones.');
    $f = Html::normalize('test<hr />');
    $this->assertEquals('test<hr>', $f, 'HTML corrector -- convert self-closing element to HTML5 void element.');
    $f = Html::normalize('test<hr/>');
    $this->assertEquals('test<hr>', $f, 'HTML corrector -- convert self-closing element to HTML5 void element.');
    $f = Html::normalize('test<hr    />');
    $this->assertEquals('test<hr>', $f, 'HTML corrector -- convert self-closing element with multiple spaces to HTML5 void element.');
    $f = Html::normalize('<span class="test" />');
    $this->assertEquals('<span class="test"></span>', $f, 'HTML corrector -- Convert XHTML that is properly formed but that would not be compatible with typical HTML user agents.');
    $f = Html::normalize('test1<br class="test">test2');
    $this->assertEquals('test1<br class="test">test2', $f, 'HTML corrector -- Keep self-closing tags.');
    $f = Html::normalize('line1<hr>line2');
    $this->assertEquals('line1<hr>line2', $f, 'HTML corrector -- Keep self-closing tags.');
    $f = Html::normalize('line1<HR>line2');
    $this->assertEquals('line1<hr>line2', $f, 'HTML corrector -- Keep self-closing tags.');
    $f = Html::normalize('<img src="http://example.com/test.jpg">test</img>');
    $this->assertEquals('<img src="http://example.com/test.jpg">test', $f, 'HTML corrector -- Fix self-closing single tags.');
    $f = Html::normalize('<br></br>');
    $this->assertEquals('<br><br>', $f, "HTML corrector -- Transform empty tags to a self-closed tag if the tag's content model is EMPTY.");
    $f = Html::normalize('<div></div>');
    $this->assertEquals('<div></div>', $f, "HTML corrector -- Do not transform empty tags to a single closed tag if the tag's content model is not EMPTY.");
    $f = Html::normalize('<p>line1<br/><hr/>line2</p>');
    $this->assertEquals('<p>line1<br></p><hr>line2', $f, 'HTML corrector -- Move non-inline elements outside of inline containers.');
    $f = Html::normalize('<p>line1<div>line2</div></p>');
    $this->assertEquals('<p>line1</p><div>line2</div>', $f, 'HTML corrector -- Move non-inline elements outside of inline containers.');
    $f = Html::normalize('<p>test<p>test</p>\\n');
    $this->assertEquals('<p>test</p><p>test</p>\\n', $f, 'HTML corrector -- Auto-close improperly nested tags.');
    $f = Html::normalize('<p>Line1<br><STRONG>bold stuff</b>');
    $this->assertEquals('<p>Line1<br><strong>bold stuff</strong></p>', $f, 'HTML corrector -- Properly close unclosed tags, and remove useless closing tags.');
    $f = Html::normalize('test <!-- this is a comment -->');
    $this->assertEquals('test <!-- this is a comment -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!--this is a comment-->');
    $this->assertEquals('test <!--this is a comment-->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->');
    $this->assertEquals('test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!-- comment <p>another comment</p> -->');
    $this->assertEquals('test <!-- comment <p>another comment</p> -->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('test <!--break-->');
    $this->assertEquals('test <!--break-->', $f, 'HTML corrector -- Do not touch HTML comments.');
    $f = Html::normalize('<p>test\\n</p>\\n');
    $this->assertEquals('<p>test\\n</p>\\n', $f, 'HTML corrector -- New-lines are accepted and kept as-is.');
    // cSpell:disable
    $f = Html::normalize('<p>دروبال');
    $this->assertEquals('<p>دروبال</p>', $f, 'HTML corrector -- Encoding is correctly kept.');
    // cSpell:enable
    $html = '<script>alert("test")</script>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- script element');
    $html = '<p><script>alert("test")</script></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- nested script element');
    $html = '<p><style> /* Styling */ body {color:red}</style></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- style element.');
    $html = '<p><style>
/*<![CDATA[*/
/* Styling */
body {color:red}
/*]]>*/
</style></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section /*<![CDATA[*/ properly escaped');
    $html = '<p><style>
/*<![CDATA[*/
  /* Styling */
  body {color:red}
/*]]>*/
</style></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section <!--/*--><![CDATA[/* ><!--*/ properly escaped');
    $html = '<p><script>
//<![CDATA[
  alert("test");
//]]>
</script></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section <!--//--><![CDATA[// ><!-- properly escaped');
    $html = '<p><script>
// <![CDATA[
  alert("test");
//]]>
</script></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section // <![CDATA[ properly escaped');
    $html = '<p><script>
// <![CDATA[![CDATA[![CDATA[
  alert("test");
//]]]]]]>
</script></p>';
    $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section // <![CDATA[![CDATA[![CDATA[ properly escaped');
    // Test calling Html::normalize() twice.
    $html = '<p><script>
// <![CDATA[![CDATA[![CDATA[
  alert("test");
//]]]]]]>
</script></p>';
    $this->assertEquals($html, Html::normalize(Html::normalize($html)), 'HTML corrector -- Existing cdata section // <![CDATA[![CDATA[![CDATA[ properly escaped');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.