PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/sahana/3rd/htmlpurifier/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php

#
PHP | 442 lines | 375 code | 43 blank | 24 comment | 0 complexity | 2e5934dead16c6b931db97bc5e7c044d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. require_once('HTMLPurifier/Config.php');
  3. require_once('HTMLPurifier/StrategyHarness.php');
  4. require_once('HTMLPurifier/Strategy/ValidateAttributes.php');
  5. class HTMLPurifier_Strategy_ValidateAttributesTest extends
  6. HTMLPurifier_StrategyHarness
  7. {
  8. function setUp() {
  9. parent::setUp();
  10. $this->obj = new HTMLPurifier_Strategy_ValidateAttributes();
  11. $this->config = array('HTML.Doctype' => 'XHTML 1.0 Strict');
  12. }
  13. function testEmpty() {
  14. $this->assertResult('');
  15. }
  16. function testIDs() {
  17. $this->assertResult(
  18. '<div id="valid">Kill the ID.</div>',
  19. '<div>Kill the ID.</div>'
  20. );
  21. $this->assertResult('<div id="valid">Preserve the ID.</div>', true,
  22. array('HTML.EnableAttrID' => true));
  23. $this->assertResult(
  24. '<div id="0invalid">Kill the ID.</div>',
  25. '<div>Kill the ID.</div>',
  26. array('HTML.EnableAttrID' => true)
  27. );
  28. // test id accumulator
  29. $this->assertResult(
  30. '<div id="valid">Valid</div><div id="valid">Invalid</div>',
  31. '<div id="valid">Valid</div><div>Invalid</div>',
  32. array('HTML.EnableAttrID' => true)
  33. );
  34. $this->assertResult(
  35. '<span dir="up-to-down">Bad dir.</span>',
  36. '<span>Bad dir.</span>'
  37. );
  38. // test attribute key case sensitivity
  39. $this->assertResult(
  40. '<div ID="valid">Convert ID to lowercase.</div>',
  41. '<div id="valid">Convert ID to lowercase.</div>',
  42. array('HTML.EnableAttrID' => true)
  43. );
  44. // test simple attribute substitution
  45. $this->assertResult(
  46. '<div id=" valid ">Trim whitespace.</div>',
  47. '<div id="valid">Trim whitespace.</div>',
  48. array('HTML.EnableAttrID' => true)
  49. );
  50. // test configuration id blacklist
  51. $this->assertResult(
  52. '<div id="invalid">Invalid</div>',
  53. '<div>Invalid</div>',
  54. array(
  55. 'Attr.IDBlacklist' => array('invalid'),
  56. 'HTML.EnableAttrID' => true
  57. )
  58. );
  59. // name rewritten as id
  60. $this->assertResult(
  61. '<a name="foobar" />',
  62. '<a id="foobar" />',
  63. array('HTML.EnableAttrID' => true)
  64. );
  65. }
  66. function testClasses() {
  67. $this->assertResult('<div class="valid">Valid</div>');
  68. $this->assertResult(
  69. '<div class="valid 0invalid">Keep valid.</div>',
  70. '<div class="valid">Keep valid.</div>'
  71. );
  72. }
  73. function testTitle() {
  74. $this->assertResult(
  75. '<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>'
  76. );
  77. }
  78. function testLang() {
  79. $this->assertResult(
  80. '<span lang="fr">La soupe.</span>',
  81. '<span lang="fr" xml:lang="fr">La soupe.</span>'
  82. );
  83. // test only xml:lang for XHTML 1.1
  84. $this->assertResult(
  85. '<b lang="en">asdf</b>',
  86. '<b xml:lang="en">asdf</b>', array('HTML.Doctype' => 'XHTML 1.1')
  87. );
  88. }
  89. function testAlign() {
  90. $this->assertResult(
  91. '<h1 align="center">Centered Headline</h1>',
  92. '<h1 style="text-align:center;">Centered Headline</h1>'
  93. );
  94. $this->assertResult(
  95. '<h1 align="right">Right-aligned Headline</h1>',
  96. '<h1 style="text-align:right;">Right-aligned Headline</h1>'
  97. );
  98. $this->assertResult(
  99. '<h1 align="left">Left-aligned Headline</h1>',
  100. '<h1 style="text-align:left;">Left-aligned Headline</h1>'
  101. );
  102. $this->assertResult(
  103. '<p align="justify">Justified Paragraph</p>',
  104. '<p style="text-align:justify;">Justified Paragraph</p>'
  105. );
  106. $this->assertResult(
  107. '<h1 align="invalid">Invalid Headline</h1>',
  108. '<h1>Invalid Headline</h1>'
  109. );
  110. }
  111. function testTable() {
  112. $this->assertResult(
  113. '<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
  114. <col align="right" width="4*" />
  115. <col charoff="5" align="char" width="*" />
  116. <tr valign="top">
  117. <th abbr="name">Fiddly name</th>
  118. <th abbr="price">Super-duper-price</th>
  119. </tr>
  120. <tr>
  121. <td abbr="carrot">Carrot Humungous</td>
  122. <td>$500.23</td>
  123. </tr>
  124. <tr>
  125. <td colspan="2">Taken off the market</td>
  126. </tr>
  127. </table>'
  128. );
  129. // test col.span is non-zero
  130. $this->assertResult(
  131. '<col span="0" />',
  132. '<col />'
  133. );
  134. // lengths
  135. $this->assertResult(
  136. '<td width="5%" height="10" /><th width="10" height="5%" /><hr width="10" height="10" />',
  137. '<td style="width:5%;height:10px;" /><th style="width:10px;height:5%;" /><hr style="width:10px;" />'
  138. );
  139. // td boolean transformation
  140. $this->assertResult(
  141. '<td nowrap />',
  142. '<td style="white-space:nowrap;" />'
  143. );
  144. // caption align transformation
  145. $this->assertResult(
  146. '<caption align="left" />',
  147. '<caption style="text-align:left;" />'
  148. );
  149. $this->assertResult(
  150. '<caption align="right" />',
  151. '<caption style="text-align:right;" />'
  152. );
  153. $this->assertResult(
  154. '<caption align="top" />',
  155. '<caption style="caption-side:top;" />'
  156. );
  157. $this->assertResult(
  158. '<caption align="bottom" />',
  159. '<caption style="caption-side:bottom;" />'
  160. );
  161. $this->assertResult(
  162. '<caption align="nonsense" />',
  163. '<caption />'
  164. );
  165. // align transformation
  166. $this->assertResult(
  167. '<table align="left" />',
  168. '<table style="float:left;" />'
  169. );
  170. $this->assertResult(
  171. '<table align="center" />',
  172. '<table style="margin-left:auto;margin-right:auto;" />'
  173. );
  174. $this->assertResult(
  175. '<table align="right" />',
  176. '<table style="float:right;" />'
  177. );
  178. $this->assertResult(
  179. '<table align="top" />',
  180. '<table />'
  181. );
  182. }
  183. function testURI() {
  184. $this->assertResult('<a href="http://www.google.com/">Google</a>');
  185. // test invalid URI
  186. $this->assertResult(
  187. '<a href="javascript:badstuff();">Google</a>',
  188. '<a>Google</a>'
  189. );
  190. }
  191. function testImg() {
  192. $this->assertResult(
  193. '<img />',
  194. '<img src="" alt="Invalid image" />',
  195. array('Core.RemoveInvalidImg' => false)
  196. );
  197. $this->assertResult(
  198. '<img src="foobar.jpg" />',
  199. '<img src="foobar.jpg" alt="foobar.jpg" />'
  200. );
  201. $this->assertResult(
  202. '<img alt="pretty picture" />',
  203. '<img alt="pretty picture" src="" />',
  204. array('Core.RemoveInvalidImg' => false)
  205. );
  206. // mailto in image is not allowed
  207. $this->assertResult(
  208. '<img src="mailto:foo@example.com" />',
  209. '<img alt="mailto:foo@example.com" src="" />',
  210. array('Core.RemoveInvalidImg' => false)
  211. );
  212. // align transformation
  213. $this->assertResult(
  214. '<img src="foobar.jpg" alt="foobar" align="left" />',
  215. '<img src="foobar.jpg" alt="foobar" style="float:left;" />'
  216. );
  217. $this->assertResult(
  218. '<img src="foobar.jpg" alt="foobar" align="right" />',
  219. '<img src="foobar.jpg" alt="foobar" style="float:right;" />'
  220. );
  221. $this->assertResult(
  222. '<img src="foobar.jpg" alt="foobar" align="bottom" />',
  223. '<img src="foobar.jpg" alt="foobar" style="vertical-align:baseline;" />'
  224. );
  225. $this->assertResult(
  226. '<img src="foobar.jpg" alt="foobar" align="middle" />',
  227. '<img src="foobar.jpg" alt="foobar" style="vertical-align:middle;" />'
  228. );
  229. $this->assertResult(
  230. '<img src="foobar.jpg" alt="foobar" align="top" />',
  231. '<img src="foobar.jpg" alt="foobar" style="vertical-align:top;" />'
  232. );
  233. $this->assertResult(
  234. '<img src="foobar.jpg" alt="foobar" align="outerspace" />',
  235. '<img src="foobar.jpg" alt="foobar" />'
  236. );
  237. }
  238. function testBdo() {
  239. // test required attributes for bdo
  240. $this->assertResult(
  241. '<bdo>Go left.</bdo>',
  242. '<bdo dir="ltr">Go left.</bdo>'
  243. );
  244. $this->assertResult(
  245. '<bdo dir="blahblah">Invalid value!</bdo>',
  246. '<bdo dir="ltr">Invalid value!</bdo>'
  247. );
  248. }
  249. function testDir() {
  250. // see testBdo, behavior is subtly different
  251. $this->assertResult(
  252. '<span dir="blahblah">Invalid value!</span>',
  253. '<span>Invalid value!</span>'
  254. );
  255. }
  256. function testLinks() {
  257. // link types
  258. $this->assertResult(
  259. '<a href="foo" rel="nofollow" />',
  260. true,
  261. array('Attr.AllowedRel' => 'nofollow')
  262. );
  263. // link targets
  264. $this->assertResult(
  265. '<a href="foo" target="_top" />',
  266. true,
  267. array('Attr.AllowedFrameTargets' => '_top',
  268. 'HTML.Doctype' => 'XHTML 1.0 Transitional')
  269. );
  270. $this->assertResult(
  271. '<a href="foo" target="_top" />',
  272. '<a href="foo" />'
  273. );
  274. $this->assertResult(
  275. '<a href="foo" target="_top" />',
  276. '<a href="foo" />',
  277. array('Attr.AllowedFrameTargets' => '_top', 'HTML.Strict' => true)
  278. );
  279. }
  280. function testBorder() {
  281. // border
  282. $this->assertResult(
  283. '<img src="foo" alt="foo" hspace="1" vspace="3" />',
  284. '<img src="foo" alt="foo" style="margin-top:3px;margin-bottom:3px;margin-left:1px;margin-right:1px;" />',
  285. array('Attr.AllowedRel' => 'nofollow')
  286. );
  287. }
  288. function testHr() {
  289. $this->assertResult(
  290. '<hr size="3" />',
  291. '<hr style="height:3px;" />'
  292. );
  293. $this->assertResult(
  294. '<hr noshade />',
  295. '<hr style="color:#808080;background-color:#808080;border:0;" />'
  296. );
  297. // align transformation
  298. $this->assertResult(
  299. '<hr align="left" />',
  300. '<hr style="margin-left:0;margin-right:auto;text-align:left;" />'
  301. );
  302. $this->assertResult(
  303. '<hr align="center" />',
  304. '<hr style="margin-left:auto;margin-right:auto;text-align:center;" />'
  305. );
  306. $this->assertResult(
  307. '<hr align="right" />',
  308. '<hr style="margin-left:auto;margin-right:0;text-align:right;" />'
  309. );
  310. $this->assertResult(
  311. '<hr align="bottom" />',
  312. '<hr />'
  313. );
  314. }
  315. function testBr() {
  316. // br clear transformation
  317. $this->assertResult(
  318. '<br clear="left" />',
  319. '<br style="clear:left;" />'
  320. );
  321. $this->assertResult(
  322. '<br clear="right" />',
  323. '<br style="clear:right;" />'
  324. );
  325. $this->assertResult( // test both?
  326. '<br clear="all" />',
  327. '<br style="clear:both;" />'
  328. );
  329. $this->assertResult(
  330. '<br clear="none" />',
  331. '<br style="clear:none;" />'
  332. );
  333. $this->assertResult(
  334. '<br clear="foo" />',
  335. '<br />'
  336. );
  337. }
  338. function testListTypeTransform() {
  339. // ul
  340. $this->assertResult(
  341. '<ul type="disc" />',
  342. '<ul style="list-style-type:disc;" />'
  343. );
  344. $this->assertResult(
  345. '<ul type="square" />',
  346. '<ul style="list-style-type:square;" />'
  347. );
  348. $this->assertResult(
  349. '<ul type="circle" />',
  350. '<ul style="list-style-type:circle;" />'
  351. );
  352. $this->assertResult( // case insensitive
  353. '<ul type="CIRCLE" />',
  354. '<ul style="list-style-type:circle;" />'
  355. );
  356. $this->assertResult(
  357. '<ul type="a" />',
  358. '<ul />'
  359. );
  360. // ol
  361. $this->assertResult(
  362. '<ol type="1" />',
  363. '<ol style="list-style-type:decimal;" />'
  364. );
  365. $this->assertResult(
  366. '<ol type="i" />',
  367. '<ol style="list-style-type:lower-roman;" />'
  368. );
  369. $this->assertResult(
  370. '<ol type="I" />',
  371. '<ol style="list-style-type:upper-roman;" />'
  372. );
  373. $this->assertResult(
  374. '<ol type="a" />',
  375. '<ol style="list-style-type:lower-alpha;" />'
  376. );
  377. $this->assertResult(
  378. '<ol type="A" />',
  379. '<ol style="list-style-type:upper-alpha;" />'
  380. );
  381. $this->assertResult(
  382. '<ol type="disc" />',
  383. '<ol />'
  384. );
  385. // li
  386. $this->assertResult(
  387. '<li type="circle" />',
  388. '<li style="list-style-type:circle;" />'
  389. );
  390. $this->assertResult(
  391. '<li type="A" />',
  392. '<li style="list-style-type:upper-alpha;" />'
  393. );
  394. $this->assertResult( // case sensitive
  395. '<li type="CIRCLE" />',
  396. '<li />'
  397. );
  398. }
  399. }