RemoveForeignElementsTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. class HTMLPurifier_Strategy_RemoveForeignElementsTest extends HTMLPurifier_StrategyHarness
  3. {
  4. function setUp() {
  5. parent::setUp();
  6. $this->obj = new HTMLPurifier_Strategy_RemoveForeignElements();
  7. }
  8. function testBlankInput() {
  9. $this->assertResult('');
  10. }
  11. function testPreserveRecognizedElements() {
  12. $this->assertResult('This is <b>bold text</b>.');
  13. }
  14. function testRemoveForeignElements() {
  15. $this->assertResult(
  16. '<asdf>Bling</asdf><d href="bang">Bong</d><foobar />',
  17. 'BlingBong'
  18. );
  19. }
  20. function testRemoveScriptAndContents() {
  21. $this->assertResult(
  22. '<script>alert();</script>',
  23. ''
  24. );
  25. }
  26. function testRemoveStyleAndContents() {
  27. $this->assertResult(
  28. '<style>.foo {blink;}</style>',
  29. ''
  30. );
  31. }
  32. function testRemoveOnlyScriptTagsLegacy() {
  33. $this->config->set('Core.RemoveScriptContents', false);
  34. $this->assertResult(
  35. '<script>alert();</script>',
  36. 'alert();'
  37. );
  38. }
  39. function testRemoveOnlyScriptTags() {
  40. $this->config->set('Core.HiddenElements', array());
  41. $this->assertResult(
  42. '<script>alert();</script>',
  43. 'alert();'
  44. );
  45. }
  46. function testRemoveInvalidImg() {
  47. $this->assertResult('<img />', '');
  48. }
  49. function testPreserveValidImg() {
  50. $this->assertResult('<img src="foobar.gif" alt="foobar.gif" />');
  51. }
  52. function testPreserveInvalidImgWhenRemovalIsDisabled() {
  53. $this->config->set('Core.RemoveInvalidImg', false);
  54. $this->assertResult('<img />');
  55. }
  56. function testTextifyCommentedScriptContents() {
  57. $this->config->set('HTML.Trusted', true);
  58. $this->config->set('Output.CommentScriptContents', false); // simplify output
  59. $this->assertResult(
  60. '<script type="text/javascript"><!--
  61. alert(<b>bold</b>);
  62. // --></script>',
  63. '<script type="text/javascript">
  64. alert(&lt;b&gt;bold&lt;/b&gt;);
  65. // </script>'
  66. );
  67. }
  68. function testRequiredAttributesTestNotPerformedOnEndTag() {
  69. $def = $this->config->getHTMLDefinition(true);
  70. $def->addElement('f', 'Block', 'Optional: #PCDATA', false, array('req*' => 'Text'));
  71. $this->assertResult('<f req="text">Foo</f> Bar');
  72. }
  73. function testPreserveCommentsWithHTMLTrusted() {
  74. $this->config->set('HTML.Trusted', true);
  75. $this->assertResult('<!-- foo -->');
  76. }
  77. function testRemoveTrailingHyphensInComment() {
  78. $this->config->set('HTML.Trusted', true);
  79. $this->assertResult('<!-- foo ----->', '<!-- foo -->');
  80. }
  81. function testCollapseDoubleHyphensInComment() {
  82. $this->config->set('HTML.Trusted', true);
  83. $this->assertResult('<!-- bo --- asdf--as -->', '<!-- bo - asdf-as -->');
  84. }
  85. function testPreserveCommentsWithLookup() {
  86. $this->config->set('HTML.AllowedComments', array('allowed'));
  87. $this->assertResult('<!-- allowed --><!-- not allowed -->', '<!-- allowed -->');
  88. }
  89. function testPreserveCommentsWithRegexp() {
  90. $this->config->set('HTML.AllowedCommentsRegexp', '/^allowed[1-9]$/');
  91. $this->assertResult('<!-- allowed1 --><!-- not allowed -->', '<!-- allowed1 -->');
  92. }
  93. }
  94. // vim: et sw=4 sts=4