summaryrefslogtreecommitdiff
path: root/test/SimpleTest/TokenizerPositionTest.php
blob: 534456a2819e37fef780bd0d8b0e4f2debfc5496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

require_once dirname(__FILE__) . '/../autorun.php';

class HTML5_PositionTestableTokenizer extends HTML5_TestableTokenizer
{
    public $outputLines = array();
    public $outputCols  = array();
    private $characterTokens = array();
    protected function emitToken($token, $checkStream = true, $dry = false) {
        parent::emitToken($token, $checkStream, $dry);
        // XXX: The tests should really include the parse errors, but I'm lazy.
        switch ($token['type']) {
            case self::PARSEERROR:
                return;
            
            case self::CHARACTER:
                if ($this->characterTokens) {
                    array_pop($this->outputLines);
                    array_pop($this->outputCols);
                }
                $this->characterTokens[] = $token;
            
            default:
                $this->outputLines[] = $this->stream()->getCurrentLine();
                $this->outputCols[]  = $this->stream()->getColumnOffset();
        }
        if ($token['type'] !== self::CHARACTER) {
            $this->characterTokens = array();
        }
    }
}

class HTML5_TokenizerTestOfPosition extends UnitTestCase
{
    function testBasic() {
        $this->assertPositions(
            "<b><i>f<p>\n<b>a</b>",
            array(1,1,1,1, 2,2,2,2),
            array(3,6,7,10,0,3,4,8)
        );
    }
    
    function testUnicode() {
        $this->assertPositions(
            "\xC2\xA2<b>\xE2\x82\xACa<b>\xf4\x8a\xaf\x8d",
            array(1,1,1,1,1),
            array(1,4,6,9,10)
        );
    }
    
    function testData() {
        $this->assertPositions(
            "a\na\n\xC2\xA2<b>",
            array(3,3),
            array(1,4)
        );
    }
    
    function testMarkupDeclarationDoubleDash() {
        $this->assertPositions(
            '<!-- foo -->',
            array(1),
            array(12)
        );
    }
    
    function testMarkupDeclarationDoctype() {
        $this->assertPositions(
            '<!DOCTYPE>',
            array(1),
            array(10)
        );
    }
    
    function testAfterDoctypeNamePublic() {
        $this->assertPositions(
            '<!DOCTYPE PUBLIC "foo">',
            array(1),
            array(23)
        );
    }
    
    function testAfterDoctypeNameSystem() {
        $this->assertPositions(
            '<!DOCTYPE SYSTEM "foo">',
            array(1),
            array(23)
        );
    }
    
    function testDecEntitySansSemicolon() {
        $this->assertPositions(
            '&#300',
            array(1),
            array(5)
        );
    }
    
    function testDecEntityWithSemicolon() {
        $this->assertPositions(
            '&#300;',
            array(1),
            array(6)
        );
    }
    
    function testHexEntity() {
        $this->assertPositions(
            '&#x300;',
            array(1),
            array(7)
        );
    }
    
    function testEmptyEntity() {
        $this->assertPositions(
            '&#;<b>',
            array(1,1),
            array(3,6)
        );
    }
    
    function testNamedEntity() {
        $this->assertPositions(
            '&quot;foo<b>',
            array(1,1),
            array(9,12)
        );
    }
    
    function testBadNamedEntity() {
        $this->assertPositions(
            '&zzz;b',
            array(1),
            array(6)
        );
    }
    
    function testAttributeEntity() {
        $this->assertPositions(
            '<b foo="&amper">a',
            array( 1, 1),
            array(16,17)
        );
    }
    
    function testBogusComment() {
        $this->assertPositions(
            "<!as asdfe \nasdf>d",
            array(2,2),
            array(5,6)
        );
    }
    
    protected function assertPositions($input, $lines, $cols, $flag = HTML5_Tokenizer::PCDATA, $lastStartTag = null) {
        $tokenizer = new HTML5_PositionTestableTokenizer($input, $flag, $lastStartTag);
        $GLOBALS['TIME'] -= get_microtime();
        $tokenizer->parse($input);
        $GLOBALS['TIME'] += get_microtime();
        $this->assertIdentical($tokenizer->outputLines, $lines, 'Lines: %s');
        $this->assertIdentical($tokenizer->outputCols, $cols,   'Cols: %s');
    }
}