You are here

public function FilterKernelTest::testHtmlCorrectorFilter in Drupal 8

Same name and namespace in other branches
  1. 9 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 938

Class

FilterKernelTest
Tests Filter module filters individually.

Namespace

Drupal\Tests\filter\Kernel

Code

public function testHtmlCorrectorFilter() {

  // Tag closing.
  $f = Html::normalize('<p>text');
  $this
    ->assertEqual($f, '<p>text</p>', 'HTML corrector -- tag closing at the end of input.');
  $f = Html::normalize('<p>text<p><p>text');
  $this
    ->assertEqual($f, '<p>text</p><p></p><p>text</p>', 'HTML corrector -- tag closing.');
  $f = Html::normalize("<ul><li>e1<li>e2");
  $this
    ->assertEqual($f, "<ul><li>e1</li><li>e2</li></ul>", 'HTML corrector -- unclosed list tags.');
  $f = Html::normalize('<div id="d">content');
  $this
    ->assertEqual($f, '<div id="d">content</div>', 'HTML corrector -- unclosed tag with attribute.');

  // XHTML slash for empty elements.
  $f = Html::normalize('<hr><br>');
  $this
    ->assertEqual($f, '<hr /><br />', 'HTML corrector -- XHTML closing slash.');
  $f = Html::normalize('<P>test</P>');
  $this
    ->assertEqual($f, '<p>test</p>', 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
  $f = Html::normalize('<P>test</p>');
  $this
    ->assertEqual($f, '<p>test</p>', 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
  $f = Html::normalize('test<hr />');
  $this
    ->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through.');
  $f = Html::normalize('test<hr/>');
  $this
    ->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through, but ensure there is a single space before the closing slash.');
  $f = Html::normalize('test<hr    />');
  $this
    ->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through, but ensure there are not too many spaces before the closing slash.');
  $f = Html::normalize('<span class="test" />');
  $this
    ->assertEqual($f, '<span class="test"></span>', '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
    ->assertEqual($f, 'test1<br class="test" />test2', 'HTML corrector -- Automatically close single tags.');
  $f = Html::normalize('line1<hr>line2');
  $this
    ->assertEqual($f, 'line1<hr />line2', 'HTML corrector -- Automatically close single tags.');
  $f = Html::normalize('line1<HR>line2');
  $this
    ->assertEqual($f, 'line1<hr />line2', 'HTML corrector -- Automatically close single tags.');
  $f = Html::normalize('<img src="http://example.com/test.jpg">test</img>');
  $this
    ->assertEqual($f, '<img src="http://example.com/test.jpg" />test', 'HTML corrector -- Automatically close single tags.');
  $f = Html::normalize('<br></br>');
  $this
    ->assertEqual($f, '<br />', "HTML corrector -- Transform empty tags to a single closed tag if the tag's content model is EMPTY.");
  $f = Html::normalize('<div></div>');
  $this
    ->assertEqual($f, '<div></div>', "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
    ->assertEqual($f, '<p>line1<br /></p><hr />line2', 'HTML corrector -- Move non-inline elements outside of inline containers.');
  $f = Html::normalize('<p>line1<div>line2</div></p>');
  $this
    ->assertEqual($f, '<p>line1</p><div>line2</div>', 'HTML corrector -- Move non-inline elements outside of inline containers.');
  $f = Html::normalize('<p>test<p>test</p>\\n');
  $this
    ->assertEqual($f, '<p>test</p><p>test</p>\\n', 'HTML corrector -- Auto-close improperly nested tags.');
  $f = Html::normalize('<p>Line1<br><STRONG>bold stuff</b>');
  $this
    ->assertEqual($f, '<p>Line1<br /><strong>bold stuff</strong></p>', 'HTML corrector -- Properly close unclosed tags, and remove useless closing tags.');
  $f = Html::normalize('test <!-- this is a comment -->');
  $this
    ->assertEqual($f, 'test <!-- this is a comment -->', 'HTML corrector -- Do not touch HTML comments.');
  $f = Html::normalize('test <!--this is a comment-->');
  $this
    ->assertEqual($f, 'test <!--this is a comment-->', 'HTML corrector -- Do not touch HTML comments.');
  $f = Html::normalize('test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->');
  $this
    ->assertEqual($f, 'test <!-- comment <p>another
    <strong>multiple</strong> line
    comment</p> -->', 'HTML corrector -- Do not touch HTML comments.');
  $f = Html::normalize('test <!-- comment <p>another comment</p> -->');
  $this
    ->assertEqual($f, 'test <!-- comment <p>another comment</p> -->', 'HTML corrector -- Do not touch HTML comments.');
  $f = Html::normalize('test <!--break-->');
  $this
    ->assertEqual($f, 'test <!--break-->', 'HTML corrector -- Do not touch HTML comments.');
  $f = Html::normalize('<p>test\\n</p>\\n');
  $this
    ->assertEqual($f, '<p>test\\n</p>\\n', 'HTML corrector -- New-lines are accepted and kept as-is.');
  $f = Html::normalize('<p>دروبال');
  $this
    ->assertEqual($f, '<p>دروبال</p>', 'HTML corrector -- Encoding is correctly kept.');
  $f = Html::normalize('<script>alert("test")</script>');
  $this
    ->assertEqual($f, '<script>
<!--//--><![CDATA[// ><!--
alert("test")
//--><!]]>
</script>', 'HTML corrector -- CDATA added to script element');
  $f = Html::normalize('<p><script>alert("test")</script></p>');
  $this
    ->assertEqual($f, '<p><script>
<!--//--><![CDATA[// ><!--
alert("test")
//--><!]]>
</script></p>', 'HTML corrector -- CDATA added to a nested script element');
  $f = Html::normalize('<p><style> /* Styling */ body {color:red}</style></p>');
  $this
    ->assertEqual($f, '<p><style>
<!--/*--><![CDATA[/* ><!--*/
 /* Styling */ body {color:red}
/*--><!]]>*/
</style></p>', 'HTML corrector -- CDATA added to a style element.');
  $filtered_data = Html::normalize('<p><style>
/*<![CDATA[*/
/* Styling */
body {color:red}
/*]]>*/
</style></p>');
  $this
    ->assertEqual($filtered_data, '<p><style>
<!--/*--><![CDATA[/* ><!--*/

/*<![CDATA[*/
/* Styling */
body {color:red}
/*]]]]><![CDATA[>*/

/*--><!]]>*/
</style></p>', new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
    '@pattern_name' => '/*<![CDATA[*/',
  ]));
  $filtered_data = Html::normalize('<p><style>
  <!--/*--><![CDATA[/* ><!--*/
  /* Styling */
  body {color:red}
  /*--><!]]>*/
</style></p>');
  $this
    ->assertEqual($filtered_data, '<p><style>
<!--/*--><![CDATA[/* ><!--*/

  <!--/*--><![CDATA[/* ><!--*/
  /* Styling */
  body {color:red}
  /*--><!]]]]><![CDATA[>*/

/*--><!]]>*/
</style></p>', new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
    '@pattern_name' => '<!--/*--><![CDATA[/* ><!--*/',
  ]));
  $filtered_data = Html::normalize('<p><script>
<!--//--><![CDATA[// ><!--
  alert("test");
//--><!]]>
</script></p>');
  $this
    ->assertEqual($filtered_data, '<p><script>
<!--//--><![CDATA[// ><!--

<!--//--><![CDATA[// ><!--
  alert("test");
//--><!]]]]><![CDATA[>

//--><!]]>
</script></p>', new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
    '@pattern_name' => '<!--//--><![CDATA[// ><!--',
  ]));
  $filtered_data = Html::normalize('<p><script>
// <![CDATA[
  alert("test");
// ]]>
</script></p>');
  $this
    ->assertEqual($filtered_data, '<p><script>
<!--//--><![CDATA[// ><!--

// <![CDATA[
  alert("test");
// ]]]]><![CDATA[>

//--><!]]>
</script></p>', new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', [
    '@pattern_name' => '// <![CDATA[',
  ]));
}