DirectLexTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class HTMLPurifier_Lexer_DirectLexTest extends HTMLPurifier_Harness
  3. {
  4. protected $DirectLex;
  5. function setUp() {
  6. $this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
  7. }
  8. // internals testing
  9. function test_parseAttributeString() {
  10. $input[0] = 'href="about:blank" rel="nofollow"';
  11. $expect[0] = array('href'=>'about:blank', 'rel'=>'nofollow');
  12. $input[1] = "href='about:blank'";
  13. $expect[1] = array('href'=>'about:blank');
  14. // note that the single quotes aren't /really/ escaped
  15. $input[2] = 'onclick="javascript:alert(\'asdf\');"';
  16. $expect[2] = array('onclick' => "javascript:alert('asdf');");
  17. $input[3] = 'selected';
  18. $expect[3] = array('selected'=>'selected');
  19. // [INVALID]
  20. $input[4] = '="nokey"';
  21. $expect[4] = array();
  22. // [SIMPLE]
  23. $input[5] = 'color=blue';
  24. $expect[5] = array('color' => 'blue');
  25. // [INVALID]
  26. $input[6] = 'href="about:blank';
  27. $expect[6] = array('href' => 'about:blank');
  28. // [INVALID]
  29. $input[7] = '"=';
  30. $expect[7] = array('"' => '');
  31. // we ought to get array()
  32. $input[8] = 'href ="about:blank"rel ="nofollow"';
  33. $expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
  34. $input[9] = 'two bool';
  35. $expect[9] = array('two' => 'two', 'bool' => 'bool');
  36. $input[10] = 'name="input" selected';
  37. $expect[10] = array('name' => 'input', 'selected' => 'selected');
  38. $input[11] = '=""';
  39. $expect[11] = array();
  40. $input[12] = '="" =""';
  41. $expect[12] = array('"' => ''); // tough to say, just don't throw a loop
  42. $input[13] = 'href="';
  43. $expect[13] = array('href' => '');
  44. $input[14] = 'href=" <';
  45. $expect[14] = array('href' => ' <');
  46. $config = HTMLPurifier_Config::createDefault();
  47. $context = new HTMLPurifier_Context();
  48. $size = count($input);
  49. for($i = 0; $i < $size; $i++) {
  50. $result = $this->DirectLex->parseAttributeString($input[$i], $config, $context);
  51. $this->assertIdentical($expect[$i], $result, 'Test ' . $i . ': %s');
  52. }
  53. }
  54. function testLineNumbers() {
  55. // . . . . . . . . . .
  56. // 01234567890123 01234567890123 0123456789012345 0123456789012 012345
  57. $html = "<b>Line 1</b>\n<i>Line 2</i>\nStill Line 2<br\n/>Now Line 4\n\n<br />";
  58. $expect = array(
  59. // line 1
  60. 0 => new HTMLPurifier_Token_Start('b')
  61. ,1 => new HTMLPurifier_Token_Text('Line 1')
  62. ,2 => new HTMLPurifier_Token_End('b')
  63. ,3 => new HTMLPurifier_Token_Text("\n")
  64. // line 2
  65. ,4 => new HTMLPurifier_Token_Start('i')
  66. ,5 => new HTMLPurifier_Token_Text('Line 2')
  67. ,6 => new HTMLPurifier_Token_End('i')
  68. ,7 => new HTMLPurifier_Token_Text("\nStill Line 2")
  69. // line 3
  70. ,8 => new HTMLPurifier_Token_Empty('br')
  71. // line 4
  72. ,9 => new HTMLPurifier_Token_Text("Now Line 4\n\n")
  73. // line SIX
  74. ,10 => new HTMLPurifier_Token_Empty('br')
  75. );
  76. $context = new HTMLPurifier_Context();
  77. $config = HTMLPurifier_Config::createDefault();
  78. $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
  79. $this->assertIdentical($output, $expect);
  80. $context = new HTMLPurifier_Context();
  81. $config = HTMLPurifier_Config::create(array(
  82. 'Core.MaintainLineNumbers' => true
  83. ));
  84. $expect[0]->position(1, 0);
  85. $expect[1]->position(1, 3);
  86. $expect[2]->position(1, 9);
  87. $expect[3]->position(2, -1);
  88. $expect[4]->position(2, 0);
  89. $expect[5]->position(2, 3);
  90. $expect[6]->position(2, 9);
  91. $expect[7]->position(3, -1);
  92. $expect[8]->position(3, 12);
  93. $expect[9]->position(4, 2);
  94. $expect[10]->position(6, 0);
  95. $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
  96. $this->assertIdentical($output, $expect);
  97. }
  98. }
  99. // vim: et sw=4 sts=4