summaryrefslogtreecommitdiff
path: root/src/HTML5/Parser/Tokenizer.php
blob: 1501ab1bb162eadacb86615e45095a14e8998167 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace HTML5\Parser;

class Tokenizer {
  protected $scanner;
  protected $events;
  protected $tok;

  /**
   * Buffer for text.
   */
  protected $text = '';

  public function __construct($scanner, $eventHandler) {
    $this->scanner = $scanner;
    $this->events = $eventHandler;
  }

  /**
   * 8.2.4.1
   */
  public function consumeData() {
    // Scan a token
    $this->scanner->next();
    // Character Ref
    $this->characterReference();

    // TagOpen
    // Null
    // EOF
    // Character
  }

  /**
   * 8.2.4.2
   */
  protected function characterReference($inAttr = FALSE) {
    if ($this->tok == '&') {
      $this->tok = $this->scanner->next();
      $$this->text .= $this->consumeCharacterReference($inAttr);
    }
  }

  protected function consumeCharacterReference($inAttribute = FALSE) {
    $entity = '';
    $start = $this->scanner->position();

    // Whitespace: Ignore
    switch ($this->tok) {
    case NULL:
    case "\t":
    case "\n":
    case "\f":
    case ' ':
    case '&':
    case '<':
      // Don't consume; just return. Spec says return nothing, but I 
      // think we have to append '&' to the string.
      return '&';
    case '#':
      // Consume and read a number
      $this->tok = $this->scanner->next();
      // X[0-9a-fA-F]+;
      // x[0-9a-fA-F]+;
      if ($this->tok == 'x' || $this->tok == 'X') {
        $hex = $this->scanner->getHex();
        $this->tok = $this->scanner->current();
        if (empty($hex)) {
          throw ParseError("Expected &#xHEX;, got &#x" . $this->tok);
        }
        $entity = hexdec($hex);
      }
      // [0-9]+;
      else {
        $entity = $this->scanner->getNumeric();
        $this->tok = $this->scanner->current();
        if (empty($numeric)) {
          throw ParseError("Expected &#DIGITS;, got $#" . $this->tok);
        }
      }
      break;
    default:
      // Attempt to consume a string up to a ';'.
      // [a-zA-Z0-9]+;
      $entity = $this->scanner->getAsciiAlpha();
      $this->tok = $this->scanner->current();

    }

    // We have an entity. We're done here.
    if ($this->tok == ';') {
      return $entity;
    }

    // If in an attribute, then failing to match ; means unconsume the 
    // entire string. Otherwise, failure to match is an error.
    if ($inAttribute) {
      $this->scanner->unconsume($this->scanner->position() - $start);
      return '&';
    }

    throw new ParseError("Expected &ENTITY;, got &ENTITY (no trailing ;)");

  }

  protected function rcdata() {
    // Ampersand
    // <
    // Null
    // EOF
    // Character
  }

  protected function rawtext() {
    // < is a literal
    // NULL is an error
    // EOF
    // Character data
  }

  protected function scriptData() {
    // < is a literal
    // NULL is an error
    // EOF
    // Character data
  }

  /**
   * 8.2.4.7
   */
  protected function plaintext() {
    // NULL -> parse error
    // EOF -> eof
    // -> Character data
  }

  /**
   * 8.2.4.8
   */
  protected function tagOpen() {
    // ! -> markup declaration
    // / -> end tagopen
    // a-zA-Z -> tagname
    // ? -> parse error
    // -> Anything else is a parse error
  }

  /**
   * 8.2.4.9
   */
  protected function endTagOpen() {
    // a-zA-Z -> tagname
    // > -> parse error
    // EOF -> parse error
    // -> parse error
  }

  /**
   * 8.2.4.10
   */
  protected function tagName() {
    // tab, lf, ff, space -> before attr name
    // / -> self-closing tag
    // > -> current tag is done, data-state
    // NULL parse error
    // EOF -> parse error
    // -> append to tagname
  }

  /**
   * 8.2.4.11
   */
  protected function rcdataLessThan() {
    // / -> empty the tmp buffer and go to end-tag
    // ->rcdata
  }

  /**
   * 8.2.4.12
   */
  protected function rcdataEndTag() {
    // A-Za-z: append to tagname
    // -> rcdata state
  }

  /**
   * 8.2.4.13
   */
  protected function rcdataEndTagName() {
    // tab, lf, ff, space -> before attribute or treat as anything
    // / -> self-closing tag
    // > -> end tag, back to data
    // A-Za-z -> append to tagname
    // -> rcdata state
  }

  /**
   * 8.2.4.14
   */
  protected function rawtextLessThan() {
    // / -> rawtext endtag state
    // -> rawtext
  }

  /**
   * 8.2.4.15
   */
  protected function rawtextEndTagOpen() {
    // A-Za-z -> rawtext
    // ->rawtext
  }

  protected function rawtextEndTagName() {
    // tab, lf, ff, space -> before attr name
    //
  }

  protected function scriptLessThan(){
  }
  protected function scriptEndTagOpen() {
  }
  protected function scriptEndTagName() {
  }
  protected function scriptEscapeStart() {
  }
  protected function scriptEscapeStartDash() {
  }
  protected function scriptEscaped() {
  }
  protected function scriptEscapedDash() {
  }
  protected function scriptEscapedDashDash() {
  }
  protected function scriptEscapedLessThan() {
  }
  protected function scriptEscapedEndTagOpen() {
  }
  protected function scriptEscapedEndTagName() {
  }
  protected function scriptDoubleEscapeStart() {
  }
  protected function scriptDoubleEscaped() {
  }
  protected function scriptDoubleEscapedDash() {
  }
  protected function scriptDoubleEscapedDashDash() {
  }
  protected function scriptDoubleEscapedLessThan() {
  }
  protected function scriptDoubleEscapeEnd() {
  }
  protected function beforeAttributeName() {
  }
  protected function attributeName() {
  }
  protected function afterAttributeName() {
  }
  protected function beforeAttributeValue() {
  }
  protected function attributeValueDoubleQuote() {
  }
  protected function attributeValueSingleQuote() {
  }
  protected function attributeValueUnquoted() {
  }
  protected function characterReferenceInAttributeValue() {
  }
  protected function afterAttributeValueQuoted() {
  }
  protected function selfCloseingStartTag() {
  }
  protected function bogusComment() {
  }
  protected function markupDeclarationOpen() {
  }
  protected function commentStart() {
  }
  protected function commentStartDash() {
  }
  protected function comment() {
  }
  protected function commentEndDash() {
  }
  protected function commentEnd() {
  }
  protected function commentEndBangState() {
  }
  protected function doctype() {
  }
  protected function beforeDoctype() {
  }
  protected function doctypeName() {
  }
  protected function afterDoctypeName() {
  }
  protected function doctypePublicKeyword() {
  }
  protected function beforeDoctypePublicId() {
  }
  protected function doctypePublicIdDoubleQuoted() {
  }
  protected function doctypePublicIdSingleQuoted() {
  }
  protected function afterDoctypePublicId() {
  }
  protected function betweenDoctypePublicAndSystem() {
  }
  protected function afterDoctypeSystemKeyword() {
  }
  protected function beforeDoctypeSystemIdentifier() {
  }
  protected function doctypeSystemIdDoubleQuoted() {
  }
  protected function doctypeSystemIdSingleQuoted() {
  }
  protected function afterDoctypeSystemId() {
  }
  protected function bogusDoctype() {
  }
  protected function cdataSection() {
  }





}