Fix infinite loop in Lexer.
[htmlpurifier.git] / tests / HTMLPurifier / Lexer / DirectLexTest.php
blobec5db62ee37561463083c70a74ee7f963f233fde
1 <?php
3 class HTMLPurifier_Lexer_DirectLexTest extends HTMLPurifier_Harness
6 protected $DirectLex;
8 public function setUp()
10 $this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
13 // internals testing
14 public function test_parseAttributeString()
16 $input[0] = 'href="about:blank" rel="nofollow"';
17 $expect[0] = array('href'=>'about:blank', 'rel'=>'nofollow');
19 $input[1] = "href='about:blank'";
20 $expect[1] = array('href'=>'about:blank');
22 // note that the single quotes aren't /really/ escaped
23 $input[2] = 'onclick="javascript:alert(\'asdf\');"';
24 $expect[2] = array('onclick' => "javascript:alert('asdf');");
26 $input[3] = 'selected';
27 $expect[3] = array('selected'=>'selected');
29 // [INVALID]
30 $input[4] = '="nokey"';
31 $expect[4] = array();
33 // [SIMPLE]
34 $input[5] = 'color=blue';
35 $expect[5] = array('color' => 'blue');
37 // [INVALID]
38 $input[6] = 'href="about:blank';
39 $expect[6] = array('href' => 'about:blank');
41 // [INVALID]
42 $input[7] = '"=';
43 $expect[7] = array('"' => '');
44 // we ought to get array()
46 $input[8] = 'href ="about:blank"rel ="nofollow"';
47 $expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
49 $input[9] = 'two bool';
50 $expect[9] = array('two' => 'two', 'bool' => 'bool');
52 $input[10] = 'name="input" selected';
53 $expect[10] = array('name' => 'input', 'selected' => 'selected');
55 $input[11] = '=""';
56 $expect[11] = array();
58 $input[12] = '="" =""';
59 $expect[12] = array(); // tough to say, just don't throw a loop
61 $input[13] = 'href="';
62 $expect[13] = array('href' => '');
64 $input[14] = 'href=" <';
65 $expect[14] = array('href' => ' <');
67 $config = HTMLPurifier_Config::createDefault();
68 $context = new HTMLPurifier_Context();
69 $size = count($input);
70 for($i = 0; $i < $size; $i++) {
71 $result = $this->DirectLex->parseAttributeString($input[$i], $config, $context);
72 $this->assertIdentical($expect[$i], $result, 'Test ' . $i . ': %s');
77 public function testLineNumbers()
79 // . . . . . . . . . .
80 // 01234567890123 01234567890123 0123456789012345 0123456789012 012345
81 $html = "<b>Line 1</b>\n<i>Line 2</i>\nStill Line 2<br\n/>Now Line 4\n\n<br />";
83 $expect = array(
84 // line 1
85 0 => new HTMLPurifier_Token_Start('b')
86 ,1 => new HTMLPurifier_Token_Text('Line 1')
87 ,2 => new HTMLPurifier_Token_End('b')
88 ,3 => new HTMLPurifier_Token_Text("\n")
89 // line 2
90 ,4 => new HTMLPurifier_Token_Start('i')
91 ,5 => new HTMLPurifier_Token_Text('Line 2')
92 ,6 => new HTMLPurifier_Token_End('i')
93 ,7 => new HTMLPurifier_Token_Text("\nStill Line 2")
94 // line 3
95 ,8 => new HTMLPurifier_Token_Empty('br')
96 // line 4
97 ,9 => new HTMLPurifier_Token_Text("Now Line 4\n\n")
98 // line SIX
99 ,10 => new HTMLPurifier_Token_Empty('br')
102 $context = new HTMLPurifier_Context();
103 $config = HTMLPurifier_Config::createDefault();
104 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
106 $this->assertIdentical($output, $expect);
108 $context = new HTMLPurifier_Context();
109 $config = HTMLPurifier_Config::create(array(
110 'Core.MaintainLineNumbers' => true
112 $expect[0]->position(1, 0);
113 $expect[1]->position(1, 3);
114 $expect[2]->position(1, 9);
115 $expect[3]->position(2, -1);
116 $expect[4]->position(2, 0);
117 $expect[5]->position(2, 3);
118 $expect[6]->position(2, 9);
119 $expect[7]->position(3, -1);
120 $expect[8]->position(3, 12);
121 $expect[9]->position(4, 2);
122 $expect[10]->position(6, 0);
124 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
125 $this->assertIdentical($output, $expect);
131 // vim: et sw=4 sts=4