Release 2.1.0, merged in 1313 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / Lexer / DirectLexTest.php
blobb79fd5c6662a0331518a29803b7cdec5a03326f5
1 <?php
3 require_once 'HTMLPurifier/Lexer/DirectLex.php';
5 class HTMLPurifier_Lexer_DirectLexTest extends HTMLPurifier_Harness
8 var $DirectLex;
10 function setUp() {
11 $this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
14 // internals testing
15 function test_parseAttributeString() {
17 $input[0] = 'href="about:blank" rel="nofollow"';
18 $expect[0] = array('href'=>'about:blank', 'rel'=>'nofollow');
20 $input[1] = "href='about:blank'";
21 $expect[1] = array('href'=>'about:blank');
23 // note that the single quotes aren't /really/ escaped
24 $input[2] = 'onclick="javascript:alert(\'asdf\');"';
25 $expect[2] = array('onclick' => "javascript:alert('asdf');");
27 $input[3] = 'selected';
28 $expect[3] = array('selected'=>'selected');
30 // [INVALID]
31 $input[4] = '="nokey"';
32 $expect[4] = array();
34 // [SIMPLE]
35 $input[5] = 'color=blue';
36 $expect[5] = array('color' => 'blue');
38 // [INVALID]
39 $input[6] = 'href="about:blank';
40 $expect[6] = array('href' => 'about:blank');
42 // [INVALID]
43 $input[7] = '"=';
44 $expect[7] = array('"' => '');
45 // we ought to get array()
47 $input[8] = 'href ="about:blank"rel ="nofollow"';
48 $expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
50 $input[9] = 'two bool';
51 $expect[9] = array('two' => 'two', 'bool' => 'bool');
53 $input[10] = 'name="input" selected';
54 $expect[10] = array('name' => 'input', 'selected' => 'selected');
56 $input[11] = '=""';
57 $expect[11] = array();
59 $input[12] = '="" =""';
60 $expect[12] = array('"' => ''); // tough to say, just don't throw a loop
62 $config = HTMLPurifier_Config::createDefault();
63 $context = new HTMLPurifier_Context();
64 $size = count($input);
65 for($i = 0; $i < $size; $i++) {
66 $result = $this->DirectLex->parseAttributeString($input[$i], $config, $context);
67 $this->assertIdentical($expect[$i], $result, 'Test ' . $i . ': %s');
68 paintIf($result, $expect[$i] != $result);
73 function testLineNumbers() {
75 $html = "<b>Line 1</b>\n<i>Line 2</i>\nStill Line 2<br\n/>Now Line 4\n\n<br />";
77 $expect = array(
78 // line 1
79 0 => new HTMLPurifier_Token_Start('b')
80 ,1 => new HTMLPurifier_Token_Text('Line 1')
81 ,2 => new HTMLPurifier_Token_End('b')
82 ,3 => new HTMLPurifier_Token_Text("\n")
83 // line 2
84 ,4 => new HTMLPurifier_Token_Start('i')
85 ,5 => new HTMLPurifier_Token_Text('Line 2')
86 ,6 => new HTMLPurifier_Token_End('i')
87 ,7 => new HTMLPurifier_Token_Text("\nStill Line 2")
88 // line 3
89 ,8 => new HTMLPurifier_Token_Empty('br')
90 // line 4
91 ,9 => new HTMLPurifier_Token_Text("Now Line 4\n\n")
92 // line SIX
93 ,10 => new HTMLPurifier_Token_Empty('br')
96 $context = new HTMLPurifier_Context();
97 $config = HTMLPurifier_Config::createDefault();
98 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
100 $this->assertIdentical($output, $expect);
102 $context = new HTMLPurifier_Context();
103 $config = HTMLPurifier_Config::create(array(
104 'Core.MaintainLineNumbers' => true
106 $expect[0]->line = 1;
107 $expect[1]->line = 1;
108 $expect[2]->line = 1;
109 $expect[3]->line = 1;
110 $expect[4]->line = 2;
111 $expect[5]->line = 2;
112 $expect[6]->line = 2;
113 $expect[7]->line = 2;
114 $expect[8]->line = 3;
115 $expect[9]->line = 4;
116 $expect[10]->line = 6;
118 $output = $this->DirectLex->tokenizeHTML($html, $config, $context);
119 $this->assertIdentical($output, $expect);