forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
358 lines (308 loc) · 11.4 KB
/
parse.js
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Parse a "fragment" of HTML, up to the end of the input or a particular
// template tag (using the "shouldStop" option).
HTMLTools.parseFragment = function (input, options) {
var scanner;
if (typeof input === 'string')
scanner = new Scanner(input);
else
// input can be a scanner. We'd better not have a different
// value for the "getTemplateTag" option as when the scanner
// was created, because we don't do anything special to reset
// the value (which is attached to the scanner).
scanner = input;
// ```
// { getTemplateTag: function (scanner, templateTagPosition) {
// if (templateTagPosition === HTMLTools.TEMPLATE_TAG_POSITION.ELEMENT) {
// ...
// ```
if (options && options.getTemplateTag)
scanner.getTemplateTag = options.getTemplateTag;
// function (scanner) -> boolean
var shouldStop = options && options.shouldStop;
var result;
if (options && options.textMode) {
if (options.textMode === HTML.TEXTMODE.STRING) {
result = getRawText(scanner, null, shouldStop);
} else if (options.textMode === HTML.TEXTMODE.RCDATA) {
result = getRCData(scanner, null, shouldStop);
} else {
throw new Error("Unsupported textMode: " + options.textMode);
}
} else {
result = getContent(scanner, shouldStop);
}
if (! scanner.isEOF()) {
// If we aren't at the end of the input, we either stopped at an unmatched
// HTML end tag or at a template tag (like `{{else}}` or `{{/if}}`).
// Detect the former case (stopped at an HTML end tag) and throw a good
// error.
var posBefore = scanner.pos;
try {
var endTag = getHTMLToken(scanner);
} catch (e) {
// ignore errors from getTemplateTag
}
// XXX we make some assumptions about shouldStop here, like that it
// won't tell us to stop at an HTML end tag. Should refactor
// `shouldStop` into something more suitable.
if (endTag && endTag.t === 'Tag' && endTag.isEnd) {
var closeTag = endTag.n;
var isVoidElement = HTML.isVoidElement(closeTag);
scanner.fatal("Unexpected HTML close tag" +
(isVoidElement ?
'. <' + endTag.n + '> should have no close tag.' : ''));
}
scanner.pos = posBefore; // rewind, we'll continue parsing as usual
// If no "shouldStop" option was provided, we should have consumed the whole
// input.
if (! shouldStop)
scanner.fatal("Expected EOF");
}
return result;
};
// Take a numeric Unicode code point, which may be larger than 16 bits,
// and encode it as a JavaScript UTF-16 string.
//
// Adapted from
// http://stackoverflow.com/questions/7126384/expressing-utf-16-unicode-characters-in-javascript/7126661.
codePointToString = HTMLTools.codePointToString = function(cp) {
if (cp >= 0 && cp <= 0xD7FF || cp >= 0xE000 && cp <= 0xFFFF) {
return String.fromCharCode(cp);
} else if (cp >= 0x10000 && cp <= 0x10FFFF) {
// we substract 0x10000 from cp to get a 20-bit number
// in the range 0..0xFFFF
cp -= 0x10000;
// we add 0xD800 to the number formed by the first 10 bits
// to give the first byte
var first = ((0xffc00 & cp) >> 10) + 0xD800;
// we add 0xDC00 to the number formed by the low 10 bits
// to give the second byte
var second = (0x3ff & cp) + 0xDC00;
return String.fromCharCode(first) + String.fromCharCode(second);
} else {
return '';
}
};
getContent = HTMLTools.Parse.getContent = function (scanner, shouldStopFunc) {
var items = [];
while (! scanner.isEOF()) {
if (shouldStopFunc && shouldStopFunc(scanner))
break;
var posBefore = scanner.pos;
var token = getHTMLToken(scanner);
if (! token)
// tokenizer reached EOF on its own, e.g. while scanning
// template comments like `{{! foo}}`.
continue;
if (token.t === 'Doctype') {
scanner.fatal("Unexpected Doctype");
} else if (token.t === 'Chars') {
pushOrAppendString(items, token.v);
} else if (token.t === 'CharRef') {
items.push(convertCharRef(token));
} else if (token.t === 'Comment') {
items.push(HTML.Comment(token.v));
} else if (token.t === 'TemplateTag') {
items.push(token.v);
} else if (token.t === 'Tag') {
if (token.isEnd) {
// Stop when we encounter an end tag at the top level.
// Rewind; we'll re-parse the end tag later.
scanner.pos = posBefore;
break;
}
var tagName = token.n;
// is this an element with no close tag (a BR, HR, IMG, etc.) based
// on its name?
var isVoid = HTML.isVoidElement(tagName);
if (token.isSelfClosing) {
if (! (isVoid || HTML.isKnownSVGElement(tagName) || tagName.indexOf(':') >= 0))
scanner.fatal('Only certain elements like BR, HR, IMG, etc. (and foreign elements like SVG) are allowed to self-close');
}
// result of parseAttrs may be null
var attrs = parseAttrs(token.attrs);
// arrays need to be wrapped in HTML.Attrs(...)
// when used to construct tags
if (HTML.isArray(attrs))
attrs = HTML.Attrs.apply(null, attrs);
var tagFunc = HTML.getTag(tagName);
if (isVoid || token.isSelfClosing) {
items.push(attrs ? tagFunc(attrs) : tagFunc());
} else {
// parse HTML tag contents.
// HTML treats a final `/` in a tag as part of an attribute, as in `<a href=/foo/>`, but the template author who writes `<circle r={{r}}/>`, say, may not be thinking about that, so generate a good error message in the "looks like self-close" case.
var looksLikeSelfClose = (scanner.input.substr(scanner.pos - 2, 2) === '/>');
var content = null;
if (token.n === 'textarea') {
if (scanner.peek() === '\n')
scanner.pos++;
var textareaValue = getRCData(scanner, token.n, shouldStopFunc);
if (textareaValue) {
if (attrs instanceof HTML.Attrs) {
attrs = HTML.Attrs.apply(
null, attrs.value.concat([{value: textareaValue}]));
} else {
attrs = (attrs || {});
attrs.value = textareaValue;
}
}
} else if (token.n === 'script' || token.n === 'style') {
content = getRawText(scanner, token.n, shouldStopFunc);
} else {
content = getContent(scanner, shouldStopFunc);
}
var endTag = getHTMLToken(scanner);
if (! (endTag && endTag.t === 'Tag' && endTag.isEnd && endTag.n === tagName))
scanner.fatal('Expected "' + tagName + '" end tag' + (looksLikeSelfClose ? ' -- if the "<' + token.n + ' />" tag was supposed to self-close, try adding a space before the "/"' : ''));
// XXX support implied end tags in cases allowed by the spec
// make `content` into an array suitable for applying tag constructor
// as in `FOO.apply(null, content)`.
if (content == null)
content = [];
else if (! (content instanceof Array))
content = [content];
items.push(HTML.getTag(tagName).apply(
null, (attrs ? [attrs] : []).concat(content)));
}
} else {
scanner.fatal("Unknown token type: " + token.t);
}
}
if (items.length === 0)
return null;
else if (items.length === 1)
return items[0];
else
return items;
};
var pushOrAppendString = function (items, string) {
if (items.length &&
typeof items[items.length - 1] === 'string')
items[items.length - 1] += string;
else
items.push(string);
};
// get RCDATA to go in the lowercase (or camel case) tagName (e.g. "textarea")
getRCData = HTMLTools.Parse.getRCData = function (scanner, tagName, shouldStopFunc) {
var items = [];
while (! scanner.isEOF()) {
// break at appropriate end tag
if (tagName && isLookingAtEndTag(scanner, tagName))
break;
if (shouldStopFunc && shouldStopFunc(scanner))
break;
var token = getHTMLToken(scanner, 'rcdata');
if (! token)
// tokenizer reached EOF on its own, e.g. while scanning
// template comments like `{{! foo}}`.
continue;
if (token.t === 'Chars') {
pushOrAppendString(items, token.v);
} else if (token.t === 'CharRef') {
items.push(convertCharRef(token));
} else if (token.t === 'TemplateTag') {
items.push(token.v);
} else {
// (can't happen)
scanner.fatal("Unknown or unexpected token type: " + token.t);
}
}
if (items.length === 0)
return null;
else if (items.length === 1)
return items[0];
else
return items;
};
var getRawText = function (scanner, tagName, shouldStopFunc) {
var items = [];
while (! scanner.isEOF()) {
// break at appropriate end tag
if (tagName && isLookingAtEndTag(scanner, tagName))
break;
if (shouldStopFunc && shouldStopFunc(scanner))
break;
var token = getHTMLToken(scanner, 'rawtext');
if (! token)
// tokenizer reached EOF on its own, e.g. while scanning
// template comments like `{{! foo}}`.
continue;
if (token.t === 'Chars') {
pushOrAppendString(items, token.v);
} else if (token.t === 'TemplateTag') {
items.push(token.v);
} else {
// (can't happen)
scanner.fatal("Unknown or unexpected token type: " + token.t);
}
}
if (items.length === 0)
return null;
else if (items.length === 1)
return items[0];
else
return items;
};
// Input: A token like `{ t: 'CharRef', v: '&', cp: [38] }`.
//
// Output: A tag like `HTML.CharRef({ html: '&', str: '&' })`.
var convertCharRef = function (token) {
var codePoints = token.cp;
var str = '';
for (var i = 0; i < codePoints.length; i++)
str += codePointToString(codePoints[i]);
return HTML.CharRef({ html: token.v, str: str });
};
// Input is always a dictionary (even if zero attributes) and each
// value in the dictionary is an array of `Chars`, `CharRef`,
// and maybe `TemplateTag` tokens.
//
// Output is null if there are zero attributes, and otherwise a
// dictionary, or an array of dictionaries and template tags.
// Each value in the dictionary is HTMLjs (e.g. a
// string or an array of `Chars`, `CharRef`, and `TemplateTag`
// nodes).
//
// An attribute value with no input tokens is represented as "",
// not an empty array, in order to prop open empty attributes
// with no template tags.
var parseAttrs = function (attrs) {
var result = null;
if (HTML.isArray(attrs)) {
// first element is nondynamic attrs, rest are template tags
var nondynamicAttrs = parseAttrs(attrs[0]);
if (nondynamicAttrs) {
result = (result || []);
result.push(nondynamicAttrs);
}
for (var i = 1; i < attrs.length; i++) {
var token = attrs[i];
if (token.t !== 'TemplateTag')
throw new Error("Expected TemplateTag token");
result = (result || []);
result.push(token.v);
}
return result;
}
for (var k in attrs) {
if (! result)
result = {};
var inValue = attrs[k];
var outParts = [];
for (var i = 0; i < inValue.length; i++) {
var token = inValue[i];
if (token.t === 'CharRef') {
outParts.push(convertCharRef(token));
} else if (token.t === 'TemplateTag') {
outParts.push(token.v);
} else if (token.t === 'Chars') {
pushOrAppendString(outParts, token.v);
}
}
var outValue = (inValue.length === 0 ? '' :
(outParts.length === 1 ? outParts[0] : outParts));
var properKey = HTMLTools.properCaseAttributeName(k);
result[properKey] = outValue;
}
return result;
};