Track column numbers in addition to line numbers.
[htmlpurifier.git] / tests / HTMLPurifier / Lexer / DirectLexTest.php
bloba9fd49e5e51cf45708c3a217c4eef1c04529f33d
1 <?php
3 class HTMLPurifier_Lexer_DirectLexTest extends HTMLPurifier_Harness
6 protected $DirectLex;
8 function setUp() {
9 $this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
12 // internals testing
13 function test_parseAttributeString() {
15 $input[0] = 'href="about:blank" rel="nofollow"';
16 $expect[0] = array('href'=>'about:blank', 'rel'=>'nofollow');
18 $input[1] = "href='about:blank'";
19 $expect[1] = array('href'=>'about:blank');
21 // note that the single quotes aren't /really/ escaped
22 $input[2] = 'onclick="javascript:alert(\'asdf\');"';
23 $expect[2] = array('onclick' => "javascript:alert('asdf');");
25 $input[3] = 'selected';
26 $expect[3] = array('selected'=>'selected');
28 // [INVALID]
29 $input[4] = '="nokey"';
30 $expect[4] = array();
32 // [SIMPLE]
33 $input[5] = 'color=blue';
34 $expect[5] = array('color' => 'blue');
36 // [INVALID]
37 $input[6] = 'href="about:blank';
38 $expect[6] = array('href' => 'about:blank');
40 // [INVALID]
41 $input[7] = '"=';
42 $expect[7] = array('"' => '');
43 // we ought to get array()
45 $input[8] = 'href ="about:blank"rel ="nofollow"';
46 $expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
48 $input[9] = 'two bool';
49 $expect[9] = array('two' => 'two', 'bool' => 'bool');
51 $input[10] = 'name="input" selected';
52 $expect[10] = array('name' => 'input', 'selected' => 'selected');
54 $input[11] = '=""';
55 $expect[11] = array();
57 $input[12] = '="" =""';
58 $expect[12] = array('"' => ''); // tough to say, just don't throw a loop
60 $input[13] = 'href="';
61 $expect[13] = array('href' => '');
63 $input[14] = 'href=" <';
64 $expect[14] = array('href' => ' <');
66 $config = HTMLPurifier_Config::createDefault();
67 $context = new HTMLPurifier_Context();
68 $size = count($input);
69 for($i = 0; $i < $size; $i++) {
70 $result = $this->DirectLex->parseAttributeString($input[$i], $config, $context);
71 $this->assertIdentical($expect[$i], $result, 'Test ' . $i . ': %s');
76 function testLineNumbers() {
78 // . . . . . . . . . .
79 // 01234567890123 01234567890123 0123456789012345 0123456789012 012345
80 $html = "<b>Line 1</b>\n<i>Line 2</i>\nStill Line 2<br\n/>Now Line 4\n\n<br />";
82 $expect = array(
83 // line 1
84 0 => new HTMLPurifier_Token_Start('b')
85 ,1 => new HTMLPurifier_Token_Text('Line 1')
86 ,2 => new HTMLPurifier_Token_End('b')
87 ,3 => new HTMLPurifier_Token_Text("\n")
88 // line 2
89 ,4 => new HTMLPurifier_Token_Start('i')
90 ,5 => new HTMLPurifier_Token_Text('Line 2')
91 ,6 => new HTMLPurifier_Token_End('i')
92 ,7 => new HTMLPurifier_Token_Text("\nStill Line 2")
93 // line 3
94 ,8 => new HTMLPurifier_Token_Empty('br')
95 // line 4
96 ,9 => new HTMLPurifier_Token_Text("Now Line 4\n\n")
97 // line SIX
98 ,10 => new HTMLPurifier_Token_Empty('br')
101 $context = new HTMLPurifier_Context();
102 $config = HTMLPurifier_Config::createDefault();
103 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
105 $this->assertIdentical($output, $expect);
107 $context = new HTMLPurifier_Context();
108 $config = HTMLPurifier_Config::create(array(
109 'Core.MaintainLineNumbers' => true
111 $expect[0]->position(1, 0);
112 $expect[1]->position(1, 3);
113 $expect[2]->position(1, 9);
114 $expect[3]->position(2, -1);
115 $expect[4]->position(2, 0);
116 $expect[5]->position(2, 3);
117 $expect[6]->position(2, 9);
118 $expect[7]->position(3, -1);
119 $expect[8]->position(3, 12);
120 $expect[9]->position(4, 2);
121 $expect[10]->position(6, 0);
123 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
124 $this->assertIdentical($output, $expect);