No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

DirectLexTest.php 4.1KB

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