GeneratorTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
  3. {
  4. /**
  5. * Entity lookup table to help for a few tests.
  6. */
  7. private $_entity_lookup;
  8. public function __construct() {
  9. parent::__construct();
  10. $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
  11. }
  12. public function setUp() {
  13. parent::setUp();
  14. $this->config->set('Output.Newline', "\n");
  15. }
  16. /**
  17. * Creates a generator based on config and context member variables.
  18. */
  19. protected function createGenerator() {
  20. return new HTMLPurifier_Generator($this->config, $this->context);
  21. }
  22. protected function assertGenerateFromToken($token, $html) {
  23. $generator = $this->createGenerator();
  24. $result = $generator->generateFromToken($token);
  25. $this->assertIdentical($result, $html);
  26. }
  27. function test_generateFromToken_text() {
  28. $this->assertGenerateFromToken(
  29. new HTMLPurifier_Token_Text('Foobar.<>'),
  30. 'Foobar.&lt;&gt;'
  31. );
  32. }
  33. function test_generateFromToken_startWithAttr() {
  34. $this->assertGenerateFromToken(
  35. new HTMLPurifier_Token_Start('a',
  36. array('href' => 'dyn?a=foo&b=bar')
  37. ),
  38. '<a href="dyn?a=foo&amp;b=bar">'
  39. );
  40. }
  41. function test_generateFromToken_end() {
  42. $this->assertGenerateFromToken(
  43. new HTMLPurifier_Token_End('b'),
  44. '</b>'
  45. );
  46. }
  47. function test_generateFromToken_emptyWithAttr() {
  48. $this->assertGenerateFromToken(
  49. new HTMLPurifier_Token_Empty('br',
  50. array('style' => 'font-family:"Courier New";')
  51. ),
  52. '<br style="font-family:&quot;Courier New&quot;;" />'
  53. );
  54. }
  55. function test_generateFromToken_startNoAttr() {
  56. $this->assertGenerateFromToken(
  57. new HTMLPurifier_Token_Start('asdf'),
  58. '<asdf>'
  59. );
  60. }
  61. function test_generateFromToken_emptyNoAttr() {
  62. $this->assertGenerateFromToken(
  63. new HTMLPurifier_Token_Empty('br'),
  64. '<br />'
  65. );
  66. }
  67. function test_generateFromToken_error() {
  68. $this->expectError('Cannot generate HTML from non-HTMLPurifier_Token object');
  69. $this->assertGenerateFromToken( null, '' );
  70. }
  71. function test_generateFromToken_unicode() {
  72. $theta_char = $this->_entity_lookup->table['theta'];
  73. $this->assertGenerateFromToken(
  74. new HTMLPurifier_Token_Text($theta_char),
  75. $theta_char
  76. );
  77. }
  78. function test_generateFromToken_backtick() {
  79. $this->assertGenerateFromToken(
  80. new HTMLPurifier_Token_Start('img', array('alt' => '`foo')),
  81. '<img alt="`foo ">'
  82. );
  83. }
  84. function test_generateFromToken_backtickDisabled() {
  85. $this->config->set('Output.FixInnerHTML', false);
  86. $this->assertGenerateFromToken(
  87. new HTMLPurifier_Token_Start('img', array('alt' => '`')),
  88. '<img alt="`">'
  89. );
  90. }
  91. function test_generateFromToken_backtickNoChange() {
  92. $this->assertGenerateFromToken(
  93. new HTMLPurifier_Token_Start('img', array('alt' => '`foo` bar')),
  94. '<img alt="`foo` bar">'
  95. );
  96. }
  97. function assertGenerateAttributes($attr, $expect, $element = false) {
  98. $generator = $this->createGenerator();
  99. $result = $generator->generateAttributes($attr, $element);
  100. $this->assertIdentical($result, $expect);
  101. }
  102. function test_generateAttributes_blank() {
  103. $this->assertGenerateAttributes(array(), '');
  104. }
  105. function test_generateAttributes_basic() {
  106. $this->assertGenerateAttributes(
  107. array('href' => 'dyn?a=foo&b=bar'),
  108. 'href="dyn?a=foo&amp;b=bar"'
  109. );
  110. }
  111. function test_generateAttributes_doubleQuote() {
  112. $this->assertGenerateAttributes(
  113. array('style' => 'font-family:"Courier New";'),
  114. 'style="font-family:&quot;Courier New&quot;;"'
  115. );
  116. }
  117. function test_generateAttributes_singleQuote() {
  118. $this->assertGenerateAttributes(
  119. array('style' => 'font-family:\'Courier New\';'),
  120. 'style="font-family:\'Courier New\';"'
  121. );
  122. }
  123. function test_generateAttributes_multiple() {
  124. $this->assertGenerateAttributes(
  125. array('src' => 'picture.jpg', 'alt' => 'Short & interesting'),
  126. 'src="picture.jpg" alt="Short &amp; interesting"'
  127. );
  128. }
  129. function test_generateAttributes_specialChar() {
  130. $theta_char = $this->_entity_lookup->table['theta'];
  131. $this->assertGenerateAttributes(
  132. array('title' => 'Theta is ' . $theta_char),
  133. 'title="Theta is ' . $theta_char . '"'
  134. );
  135. }
  136. function test_generateAttributes_minimized() {
  137. $this->config->set('HTML.Doctype', 'HTML 4.01 Transitional');
  138. $this->assertGenerateAttributes(
  139. array('compact' => 'compact'), 'compact', 'menu'
  140. );
  141. }
  142. function test_generateFromTokens() {
  143. $this->assertGeneration(
  144. array(
  145. new HTMLPurifier_Token_Start('b'),
  146. new HTMLPurifier_Token_Text('Foobar!'),
  147. new HTMLPurifier_Token_End('b')
  148. ),
  149. '<b>Foobar!</b>'
  150. );
  151. }
  152. protected function assertGeneration($tokens, $expect) {
  153. $generator = new HTMLPurifier_Generator($this->config, $this->context);
  154. $result = $generator->generateFromTokens($tokens);
  155. $this->assertIdentical($expect, $result);
  156. }
  157. function test_generateFromTokens_Scripting() {
  158. $this->assertGeneration(
  159. array(
  160. new HTMLPurifier_Token_Start('script'),
  161. new HTMLPurifier_Token_Text('alert(3 < 5);'),
  162. new HTMLPurifier_Token_End('script')
  163. ),
  164. "<script><!--//--><![CDATA[//><!--\nalert(3 < 5);\n//--><!]]></script>"
  165. );
  166. }
  167. function test_generateFromTokens_Scripting_missingCloseTag() {
  168. $this->assertGeneration(
  169. array(
  170. new HTMLPurifier_Token_Start('script'),
  171. new HTMLPurifier_Token_Text('alert(3 < 5);'),
  172. ),
  173. "<script>alert(3 &lt; 5);"
  174. );
  175. }
  176. function test_generateFromTokens_Scripting_doubleBlock() {
  177. $this->assertGeneration(
  178. array(
  179. new HTMLPurifier_Token_Start('script'),
  180. new HTMLPurifier_Token_Text('alert(3 < 5);'),
  181. new HTMLPurifier_Token_Text('foo();'),
  182. new HTMLPurifier_Token_End('script')
  183. ),
  184. "<script>alert(3 &lt; 5);foo();</script>"
  185. );
  186. }
  187. function test_generateFromTokens_Scripting_disableWrapper() {
  188. $this->config->set('Output.CommentScriptContents', false);
  189. $this->assertGeneration(
  190. array(
  191. new HTMLPurifier_Token_Start('script'),
  192. new HTMLPurifier_Token_Text('alert(3 < 5);'),
  193. new HTMLPurifier_Token_End('script')
  194. ),
  195. "<script>alert(3 &lt; 5);</script>"
  196. );
  197. }
  198. function test_generateFromTokens_XHTMLoff() {
  199. $this->config->set('HTML.XHTML', false);
  200. // omit trailing slash
  201. $this->assertGeneration(
  202. array( new HTMLPurifier_Token_Empty('br') ),
  203. '<br>'
  204. );
  205. // there should be a test for attribute minimization, but it is
  206. // impossible for something like that to happen due to our current
  207. // definitions! fix it later
  208. // namespaced attributes must be dropped
  209. $this->assertGeneration(
  210. array( new HTMLPurifier_Token_Start('p', array('xml:lang'=>'fr')) ),
  211. '<p>'
  212. );
  213. }
  214. function test_generateFromTokens_TidyFormat() {
  215. // abort test if tidy isn't loaded
  216. if (!extension_loaded('tidy')) return;
  217. // just don't test; Tidy is exploding on me.
  218. return;
  219. $this->config->set('Core.TidyFormat', true);
  220. $this->config->set('Output.Newline', "\n");
  221. // nice wrapping please
  222. $this->assertGeneration(
  223. array(
  224. new HTMLPurifier_Token_Start('div'),
  225. new HTMLPurifier_Token_Text('Text'),
  226. new HTMLPurifier_Token_End('div')
  227. ),
  228. "<div>\n Text\n</div>\n"
  229. );
  230. }
  231. function test_generateFromTokens_sortAttr() {
  232. $this->config->set('Output.SortAttr', true);
  233. $this->assertGeneration(
  234. array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
  235. '<p a="d" b="c">'
  236. );
  237. }
  238. }
  239. // vim: et sw=4 sts=4