You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.0KB

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