ValidateAttributesTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class HTMLPurifier_Strategy_ValidateAttributesTest extends
  3. HTMLPurifier_StrategyHarness
  4. {
  5. function setUp() {
  6. parent::setUp();
  7. $this->obj = new HTMLPurifier_Strategy_ValidateAttributes();
  8. }
  9. function testEmptyInput() {
  10. $this->assertResult('');
  11. }
  12. function testRemoveIDByDefault() {
  13. $this->assertResult(
  14. '<div id="valid">Kill the ID.</div>',
  15. '<div>Kill the ID.</div>'
  16. );
  17. }
  18. function testRemoveInvalidDir() {
  19. $this->assertResult(
  20. '<span dir="up-to-down">Bad dir.</span>',
  21. '<span>Bad dir.</span>'
  22. );
  23. }
  24. function testPreserveValidClass() {
  25. $this->assertResult('<div class="valid">Valid</div>');
  26. }
  27. function testSelectivelyRemoveInvalidClasses() {
  28. $this->config->set('HTML.Doctype', 'XHTML 1.1');
  29. $this->assertResult(
  30. '<div class="valid 0invalid">Keep valid.</div>',
  31. '<div class="valid">Keep valid.</div>'
  32. );
  33. }
  34. function testPreserveTitle() {
  35. $this->assertResult(
  36. '<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>'
  37. );
  38. }
  39. function testAddXMLLang() {
  40. $this->assertResult(
  41. '<span lang="fr">La soupe.</span>',
  42. '<span lang="fr" xml:lang="fr">La soupe.</span>'
  43. );
  44. }
  45. function testOnlyXMLLangInXHTML11() {
  46. $this->config->set('HTML.Doctype', 'XHTML 1.1');
  47. $this->assertResult(
  48. '<b lang="en">asdf</b>',
  49. '<b xml:lang="en">asdf</b>'
  50. );
  51. }
  52. function testBasicURI() {
  53. $this->assertResult('<a href="http://www.google.com/">Google</a>');
  54. }
  55. function testInvalidURI() {
  56. $this->assertResult(
  57. '<a href="javascript:badstuff();">Google</a>',
  58. '<a>Google</a>'
  59. );
  60. }
  61. function testBdoAddMissingDir() {
  62. $this->assertResult(
  63. '<bdo>Go left.</bdo>',
  64. '<bdo dir="ltr">Go left.</bdo>'
  65. );
  66. }
  67. function testBdoReplaceInvalidDirWithDefault() {
  68. $this->assertResult(
  69. '<bdo dir="blahblah">Invalid value!</bdo>',
  70. '<bdo dir="ltr">Invalid value!</bdo>'
  71. );
  72. }
  73. function testBdoAlternateDefaultDir() {
  74. $this->config->set('Attr.DefaultTextDir', 'rtl');
  75. $this->assertResult(
  76. '<bdo>Go right.</bdo>',
  77. '<bdo dir="rtl">Go right.</bdo>'
  78. );
  79. }
  80. function testRemoveDirWhenNotRequired() {
  81. $this->assertResult(
  82. '<span dir="blahblah">Invalid value!</span>',
  83. '<span>Invalid value!</span>'
  84. );
  85. }
  86. function testTableAttributes() {
  87. $this->assertResult(
  88. '<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
  89. <col align="right" width="4*" />
  90. <col charoff="5" align="char" width="*" />
  91. <tr valign="top">
  92. <th abbr="name">Fiddly name</th>
  93. <th abbr="price">Super-duper-price</th>
  94. </tr>
  95. <tr>
  96. <td abbr="carrot">Carrot Humungous</td>
  97. <td>$500.23</td>
  98. </tr>
  99. <tr>
  100. <td colspan="2">Taken off the market</td>
  101. </tr>
  102. </table>'
  103. );
  104. }
  105. function testColSpanIsNonZero() {
  106. $this->assertResult(
  107. '<col span="0" />',
  108. '<col />'
  109. );
  110. }
  111. function testImgAddDefaults() {
  112. $this->config->set('Core.RemoveInvalidImg', false);
  113. $this->assertResult(
  114. '<img />',
  115. '<img src="" alt="Invalid image" />'
  116. );
  117. }
  118. function testImgGenerateAlt() {
  119. $this->assertResult(
  120. '<img src="foobar.jpg" />',
  121. '<img src="foobar.jpg" alt="foobar.jpg" />'
  122. );
  123. }
  124. function testImgAddDefaultSrc() {
  125. $this->config->set('Core.RemoveInvalidImg', false);
  126. $this->assertResult(
  127. '<img alt="pretty picture" />',
  128. '<img alt="pretty picture" src="" />'
  129. );
  130. }
  131. function testImgRemoveNonRetrievableProtocol() {
  132. $this->config->set('Core.RemoveInvalidImg', false);
  133. $this->assertResult(
  134. '<img src="mailto:foo@example.com" />',
  135. '<img alt="mailto:foo@example.com" src="" />'
  136. );
  137. }
  138. function testPreserveRel() {
  139. $this->config->set('Attr.AllowedRel', 'nofollow');
  140. $this->assertResult('<a href="foo" rel="nofollow" />');
  141. }
  142. function testPreserveTarget() {
  143. $this->config->set('Attr.AllowedFrameTargets', '_top');
  144. $this->config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
  145. $this->assertResult('<a href="foo" target="_top" />');
  146. }
  147. function testRemoveTargetWhenNotSupported() {
  148. $this->config->set('HTML.Doctype', 'XHTML 1.0 Strict');
  149. $this->config->set('Attr.AllowedFrameTargets', '_top');
  150. $this->assertResult(
  151. '<a href="foo" target="_top" />',
  152. '<a href="foo" />'
  153. );
  154. }
  155. function testKeepAbsoluteCSSWidthAndHeightOnImg() {
  156. $this->assertResult(
  157. '<img src="" alt="" style="width:10px;height:10px;border:1px solid #000;" />'
  158. );
  159. }
  160. function testRemoveLargeCSSWidthAndHeightOnImg() {
  161. $this->assertResult(
  162. '<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />',
  163. '<img src="" alt="" style="border:1px solid #000;" />'
  164. );
  165. }
  166. function testRemoveLargeCSSWidthAndHeightOnImgWithUserConf() {
  167. $this->config->set('CSS.MaxImgLength', '1px');
  168. $this->assertResult(
  169. '<img src="" alt="" style="width:1mm;height:1mm;border:1px solid #000;" />',
  170. '<img src="" alt="" style="border:1px solid #000;" />'
  171. );
  172. }
  173. function testKeepLargeCSSWidthAndHeightOnImgWhenToldTo() {
  174. $this->config->set('CSS.MaxImgLength', null);
  175. $this->assertResult(
  176. '<img src="" alt="" style="width:10000000px;height:10000000px;border:1px solid #000;" />'
  177. );
  178. }
  179. function testKeepPercentCSSWidthAndHeightOnImgWhenToldTo() {
  180. $this->config->set('CSS.MaxImgLength', null);
  181. $this->assertResult(
  182. '<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />'
  183. );
  184. }
  185. function testRemoveRelativeCSSWidthAndHeightOnImg() {
  186. $this->assertResult(
  187. '<img src="" alt="" style="width:10em;height:10em;border:1px solid #000;" />',
  188. '<img src="" alt="" style="border:1px solid #000;" />'
  189. );
  190. }
  191. function testRemovePercentCSSWidthAndHeightOnImg() {
  192. $this->assertResult(
  193. '<img src="" alt="" style="width:100%;height:100%;border:1px solid #000;" />',
  194. '<img src="" alt="" style="border:1px solid #000;" />'
  195. );
  196. }
  197. }
  198. // vim: et sw=4 sts=4