DisallowedRawHtmlExtensionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional\Extension\DisallowedRawHtml;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  14. use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
  15. use League\CommonMark\MarkdownConverter;
  16. use PHPUnit\Framework\TestCase;
  17. final class DisallowedRawHtmlExtensionTest extends TestCase
  18. {
  19. public function testDisallowedRawHtmlExtensionWithSpecExample(): void
  20. {
  21. $input = <<<'MD'
  22. <strong> <title> <style> <em>
  23. <blockquote>
  24. <xmp> is disallowed. <XMP> is also disallowed.
  25. </blockquote>
  26. MD;
  27. $expected = <<<'HTML'
  28. <p><strong> &lt;title> &lt;style> <em></p>
  29. <blockquote>
  30. &lt;xmp> is disallowed. &lt;XMP> is also disallowed.
  31. </blockquote>
  32. HTML;
  33. $environment = new Environment();
  34. $environment->addExtension(new CommonMarkCoreExtension());
  35. $environment->addExtension(new DisallowedRawHtmlExtension());
  36. $converter = new MarkdownConverter($environment);
  37. $this->assertSame($expected, (string) $converter->convert($input));
  38. }
  39. public function testIndividualHtmlTagsAsBlocks(): void
  40. {
  41. $input = <<<'MD'
  42. <title>My Cool Website</title>
  43. <textarea>
  44. foo=bar
  45. </textarea>
  46. <style>* { display: none; </style>
  47. <xmp>Itallic font should be marked up using the <i> and </i> tags.</xmp>
  48. <iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  49. <noembed><h1>Alternative content</h1></noembed>
  50. <noframes><h1>Alternative content</h1></noframes>
  51. <hr>
  52. <script type="application/javascript">alert('XSS is fun!')</script>
  53. <plaintext>foo</plaintext>
  54. MD;
  55. $expected = <<<'HTML'
  56. &lt;title>My Cool Website&lt;/title>
  57. &lt;textarea>
  58. foo=bar
  59. &lt;/textarea>
  60. &lt;style>* { display: none; &lt;/style>
  61. <p>&lt;xmp>Itallic font should be marked up using the <i> and </i> tags.&lt;/xmp></p>
  62. &lt;iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>&lt;/iframe>
  63. <p>&lt;noembed><h1>Alternative content</h1>&lt;/noembed></p>
  64. &lt;noframes><h1>Alternative content</h1>&lt;/noframes>
  65. <hr>
  66. &lt;script type="application/javascript">alert('XSS is fun!')&lt;/script>
  67. &lt;plaintext>foo&lt;/plaintext>
  68. HTML;
  69. $environment = new Environment();
  70. $environment->addExtension(new CommonMarkCoreExtension());
  71. $environment->addExtension(new DisallowedRawHtmlExtension());
  72. $converter = new MarkdownConverter($environment);
  73. $this->assertSame($expected, (string) $converter->convert($input));
  74. }
  75. }