summaryrefslogtreecommitdiff
path: root/streamhtmlparser/htmlparser_fsm.config
blob: e80d055e8688d7eabe385022f2134e815defeea8 (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
328
329
330
331
332
333
334
335
336
# Copyright 2008 Google Inc. All Rights Reserved.
# Author: falmeida@google.com (Filipe Almeida)

# TODO(falmeida): Add more descriptive names to the states and drop the
# abbreviations.
# TODO(falmeida): Reorder the states so that it's easier to read.
# TODO(falmeida): Support CDATA blocks in the form: <![CDATA[.

name = 'htmlparser'

comment = 'Definition of a finite state machine for a subset of HTTP 4.1'

condition('<', '<')
condition('>', '>')
condition('=', '=')

# TODO(falmeida): This is not the correct expression. tag and attribute names
# can only consist of alpha character.
condition('id', 'A-Za-z0-9_:-')
condition('idtag', 'A-Za-z0-9/_:-')

# Whitespace according to: http://www.w3.org/TR/html401/struct/text.html#h-9.1
condition('space', ' \t\n\r')
condition('!', '!')
condition('q', '\'')
condition('dq', '\"')
condition('/', '/')
condition('*', '*')
condition('-', '-')
condition('?', '?')
condition('lf', '\n')
condition('quote', '\\')

# TODO(falmeida): This default rule is a hack and shouldn't be here.
condition('default', '[:default:]')

state(name = 'text',
      external = 'text',
      transitions = [
        ['<', 'tag_start'],
        ['default', 'text']
      ])

# When we found the < character in text.
# Tag opening is defined in the HTML5 draft here:
# http://www.whatwg.org/specs/web-apps/current-work/#tag-open-state
# We don't exactly follow this and are much more loose in order to mimic the way
# the major browsers behave.
state(name = 'tag_start',
      external = 'tag',
      transitions = [
        ['idtag', 'tag_name'],
        ['?', 'pi'],
        ['!', 'declaration_start'],
        ['<', 'tag_start'],
        ['default', 'text']
      ])

# Name of the tag. Includes the closing tag character '/'.
state(name = 'tag_name',
      external = 'tag',
      transitions = [
        ['idtag', 'tag_name'],
        ['space', 'tag_space'],
        ['>', 'tag_close']
      ])

# HTML declaration and comment parsing
#
# We don't expose declaration state because at this point we only want to
# ensure that we are parsing them correctly so we don't get out of sync.
# This is specifically made for DOCTYPE declarations and won't work if DTD's
# are defined inside the declaration.
# The HTML5 spec says we should specificly look for the string '<!DOCTYPE HTML'
# but that will add a lot of unecessary states, and unless we build a simple
# declarative way to unfold a string match into multiple states, I don't
# think it's worth worrying about for now.

# Got '<!'. The next character will decide if we open a declaration or a
# comment.
state(name = 'declaration_start',
      external = 'text',
      transitions = [
        ['-', 'comment_open'],
        ['>', 'text'],
        ['default', 'declaration_body']
      ])

# Inside a declaration. Ie: <!DOCTYPE. We close when we see a '>'
state(name = 'declaration_body',
      external = 'text',
      transitions = [
        ['>', 'text'],
        ['default', 'declaration_body']
      ])

# Got '<!-'.
state(name = 'comment_open',
      external = 'text',
      transitions = [
        ['-', 'comment_body'],
        ['default', 'text']
      ])

# Inside a comment. We only close when we see '-->'
state(name = 'comment_body',
      external = 'comment',
      transitions = [
        ['-', 'comment_dash'],
        ['default', 'comment_body']
      ])

# Got '-' inside a comment.
state(name = 'comment_dash',
      external = 'comment',
      transitions = [
        ['-', 'comment_dash_dash'],
        ['default', 'comment_body']
      ])

# Got '--' inside a comment.
state(name = 'comment_dash_dash',
      external = 'comment',
      transitions = [
        ['-', 'comment_dash_dash'],
        ['>', 'text'],
        ['default', 'comment_body']
      ])

# XML Processing instruction parsing according to:
# http://www.w3.org/TR/REC-xml/#sec-pi
#
# Everything between the characters <? and ?> is considered to be part of the
# processing instruction.
state(name = 'pi',
      external = 'text',
      transitions = [
        ['?', 'pi_may_end'],
        ['default', 'pi']
      ])

state(name = 'pi_may_end',
      external = 'text',
      transitions = [
        ['>', 'text'],
        ['default', 'pi']
      ])

# Whitespace between tag name, attributes.
state(name = 'tag_space',
      external = 'tag',
      transitions = [
        ['>', 'tag_close'],
        ['space', 'tag_space'],
        ['id', 'attr'],
        ['/', 'tag_space']
      ])

state(name = 'tag_close',
      external = 'text',
      transitions = [
        ['<', 'tag_start'],
        ['default', 'text']
      ])

# Name of the attribute.
state(name = 'attr',
      external = 'attr',
      transitions = [
        ['id', 'attr'],
        ['>', 'tag_close'],
        ['/', 'tag_space'],
        ['=', 'value'],
        ['space', 'attr_space']
      ])

# After the attribute name.
state(name = 'attr_space',
      external = 'attr',
      transitions = [
        ['>', 'tag_close'],
        ['space', 'attr_space'],
        ['id', 'attr'],
        ['/', 'tag_space'],
        ['=', 'value']
      ])

# Expecting a value, after attribute=
state(name = 'value',
      external = 'value',
      transitions = [
        ['q', 'value_q_start'],
        ['dq', 'value_dq_start'],
        ['space', 'value'],
        ['>', 'tag_close'],
        ['default', 'value_text']
      ])

# Unquoted attribute value.
state(name = 'value_text',
      external = 'value',
      transitions = [
        ['>', 'tag_close'],
        ['space', 'tag_space'],
        ['default', 'value_text']
      ])

# First character of a single quoted attribute value.
state(name = 'value_q_start',
      external = 'value',
      transitions = [
        ['q', 'tag_space'],
        ['default', 'value_q']
      ])

# In the middle of a single quoted attribute value.
state(name = 'value_q',
      external = 'value',
      transitions = [
        ['q', 'tag_space'],
        ['default', 'value_q']
      ])

# First character of a double quoted attribute value.
state(name = 'value_dq_start',
      external = 'value',
      transitions = [
        ['dq', 'tag_space'],
        ['default', 'value_dq']
      ])

# In the middle of a double quoted attribute value.
state(name = 'value_dq',
      external = 'value',
      transitions = [
        ['dq', 'tag_space'],
        ['default', 'value_dq']
      ])

# CDATA escaping text spans.
# TODO(falmeida): These states should go after cdata_text.

# Got '<!'
state(name = 'cdata_comment_start',
      external = 'text',
      transitions = [
        ['-', 'cdata_comment_start_dash'],
        ['default', 'cdata_text'],
      ])

# Got '<!-'.
state(name = 'cdata_comment_start_dash',
      external = 'text',
      transitions = [
        ['-', 'cdata_comment_body'],
        ['default', 'cdata_text']
      ])

# Inside a comment
state(name = 'cdata_comment_body',
      external = 'text',
      transitions = [
        ['-', 'cdata_comment_dash'],
        ['default', 'cdata_comment_body']
      ])

# Got '-' inside a comment.
state(name = 'cdata_comment_dash',
      external = 'text',
      transitions = [
        ['-', 'cdata_comment_dash_dash'],
        ['default', 'cdata_comment_body']
      ])

# Got '--' inside a comment.
state(name = 'cdata_comment_dash_dash',
      external = 'text',
      transitions = [
        ['-', 'cdata_comment_dash_dash'],
        ['>', 'cdata_text'],
        ['default', 'cdata_comment_body']
      ])

# CDATA processing
#
# To simplify the code, we treat RCDATA and CDATA sections the same since the
# differences between them don't affect the context we are in.
state(name = 'cdata_text',
      external = 'text',
      transitions = [
        ['<', 'cdata_lt'],
        ['default', 'cdata_text']
      ])

# Possible beginning of the closing tag.
state(name = 'cdata_lt',
      external = 'text',
      transitions = [
        ['/', 'cdata_may_close'],
        ['!', 'cdata_comment_start'],
        ['default', 'cdata_text']
      ])

# If we encounter </tag where tag matches the last opened tag, we exit the
# CDATA section. Part of this logic is handled in the code.
state(name = 'cdata_may_close',
      external = 'text',
      transitions = [
        ['idtag', 'cdata_may_close'],
        ['>', 'text'],
        ['space', 'tag_space'],
        ['default', 'cdata_text']
      ])

# The next states are used for specialized parser modes.
state(name = 'js_file',
      external = 'js_file',
      transitions = [
        ['default', 'js_file']
      ])

# TODO(falmeida): Having css_file and js_file as the external name doesn't make
#                 sense. This should instead be text and the js/css state be
#                 returned by # in_js() and in_css().
state(name = 'css_file',
      external = 'css_file',
      transitions = [
        ['default', 'css_file']
      ])

state(name = 'null',
      external = 'text',
      transitions = [
        ['default', 'null']
      ])