-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
tag.js
314 lines (245 loc) · 6.97 KB
/
tag.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
import readExpression from '../read/expression.js';
import readScript from '../read/script.js';
import readStyle from '../read/style.js';
import { readEventHandlerDirective, readBindingDirective } from '../read/directives.js';
import { trimStart, trimEnd } from '../utils/trim.js';
import { decodeCharacterReferences } from '../utils/html.js';
import voidElementNames from '../../utils/voidElementNames.js';
const validTagName = /^\!?[a-zA-Z]{1,}:?[a-zA-Z0-9\-]*/;
const invalidUnquotedAttributeCharacters = /[\s"'=<>\/`]/;
const SELF = ':Self';
const specials = {
script: {
read: readScript,
property: 'js'
},
style: {
read: readStyle,
property: 'css'
}
};
// based on http://developers.whatwg.org/syntax.html#syntax-tag-omission
const disallowedContents = {
li: [ 'li' ],
dt: [ 'dt', 'dd' ],
dd: [ 'dt', 'dd' ],
p: 'address article aside blockquote div dl fieldset footer form h1 h2 h3 h4 h5 h6 header hgroup hr main menu nav ol p pre section table ul'.split( ' ' ),
rt: [ 'rt', 'rp' ],
rp: [ 'rt', 'rp' ],
optgroup: [ 'optgroup' ],
option: [ 'option', 'optgroup' ],
thead: [ 'tbody', 'tfoot' ],
tbody: [ 'tbody', 'tfoot' ],
tfoot: [ 'tbody' ],
tr: [ 'tr', 'tbody' ],
td: [ 'td', 'th', 'tr' ],
th: [ 'td', 'th', 'tr' ]
};
function stripWhitespace ( element ) {
if ( element.children.length ) {
const firstChild = element.children[0];
const lastChild = element.children[ element.children.length - 1 ];
if ( firstChild.type === 'Text' ) {
firstChild.data = trimStart( firstChild.data );
if ( !firstChild.data ) element.children.shift();
}
if ( lastChild.type === 'Text' ) {
lastChild.data = trimEnd( lastChild.data );
if ( !lastChild.data ) element.children.pop();
}
}
}
export default function tag ( parser ) {
const start = parser.index++;
let parent = parser.current();
if ( parser.eat( '!--' ) ) {
const data = parser.readUntil( /-->/ );
parser.eat( '-->' );
parser.current().children.push({
start,
end: parser.index,
type: 'Comment',
data
});
return null;
}
const isClosingTag = parser.eat( '/' );
const name = readTagName( parser );
parser.allowWhitespace();
if ( isClosingTag ) {
if ( voidElementNames.test( name ) ) {
parser.error( `<${name}> is a void element and cannot have children, or a closing tag`, start );
}
if ( !parser.eat( '>' ) ) parser.error( `Expected '>'` );
// close any elements that don't have their own closing tags, e.g. <div><p></div>
while ( parent.name !== name ) {
if ( parent.type !== 'Element' ) parser.error( `</${name}> attempted to close an element that was not open`, start );
parent.end = start;
parser.stack.pop();
parent = parser.current();
}
// strip leading/trailing whitespace as necessary
stripWhitespace( parent );
parent.end = parser.index;
parser.stack.pop();
return null;
} else if ( parent.name in disallowedContents ) {
// can this be a child of the parent element, or does it implicitly
// close it, like `<li>one<li>two`?
const disallowed = disallowedContents[ parent.name ];
if ( ~disallowed.indexOf( name ) ) {
stripWhitespace( parent );
parent.end = start;
parser.stack.pop();
}
}
const attributes = [];
const uniqueNames = new Set();
let attribute;
while ( attribute = readAttribute( parser, uniqueNames ) ) {
attributes.push( attribute );
parser.allowWhitespace();
}
parser.allowWhitespace();
// special cases – <script> and <style>
if ( name in specials ) {
const special = specials[ name ];
if ( parser[ special.property ] ) {
parser.index = start;
parser.error( `You can only have one <${name}> tag per component` );
}
parser.eat( '>', true );
parser[ special.property ] = special.read( parser, start, attributes );
return;
}
const element = {
start,
end: null, // filled in later
type: 'Element',
name,
attributes,
children: []
};
parser.current().children.push( element );
const selfClosing = parser.eat( '/' ) || voidElementNames.test( name );
parser.eat( '>', true );
if ( selfClosing ) {
element.end = parser.index;
} else {
// don't push self-closing elements onto the stack
parser.stack.push( element );
}
return null;
}
function readTagName ( parser ) {
const start = parser.index;
if ( parser.eat( SELF ) ) {
// check we're inside a block, otherwise this
// will cause infinite recursion
let i = parser.stack.length;
let legal = false;
while ( i-- ) {
const fragment = parser.stack[i];
if ( fragment.type === 'IfBlock' || fragment.type === 'EachBlock' ) {
legal = true;
break;
}
}
if ( !legal ) {
parser.error( `<${SELF}> components can only exist inside if-blocks or each-blocks`, start );
}
return SELF;
}
const name = parser.readUntil( /(\s|\/|>)/ );
if ( !validTagName.test( name ) ) {
parser.error( `Expected valid tag name`, start );
}
return name;
}
function readAttribute ( parser, uniqueNames ) {
const start = parser.index;
const name = parser.readUntil( /(\s|=|\/|>)/ );
if ( !name ) return null;
if ( uniqueNames.has( name ) ) {
parser.error( 'Attributes need to be unique', start );
}
uniqueNames.add( name );
parser.allowWhitespace();
if ( /^on:/.test( name ) ) {
parser.eat( '=', true );
return readEventHandlerDirective( parser, start, name.slice( 3 ) );
}
if ( /^bind:/.test( name ) ) {
return readBindingDirective( parser, start, name.slice( 5 ) );
}
if ( /^ref:/.test( name ) ) {
return {
start,
end: parser.index,
type: 'Ref',
name: name.slice( 4 )
};
}
const value = parser.eat( '=' ) ? readAttributeValue( parser ) : true;
return {
start,
end: parser.index,
type: 'Attribute',
name,
value
};
}
function readAttributeValue ( parser ) {
let quoteMark;
if ( parser.eat( `'` ) ) quoteMark = `'`;
if ( parser.eat( `"` ) ) quoteMark = `"`;
let currentChunk = {
start: parser.index,
end: null,
type: 'Text',
data: ''
};
const done = quoteMark ?
char => char === quoteMark :
char => invalidUnquotedAttributeCharacters.test( char );
const chunks = [];
while ( parser.index < parser.template.length ) {
const index = parser.index;
if ( parser.eat( '{{' ) ) {
if ( currentChunk.data ) {
currentChunk.end = index;
chunks.push( currentChunk );
}
const expression = readExpression( parser );
parser.allowWhitespace();
if ( !parser.eat( '}}' ) ) {
parser.error( `Expected }}` );
}
chunks.push({
start: index,
end: parser.index,
type: 'MustacheTag',
expression
});
currentChunk = {
start: parser.index,
end: null,
type: 'Text',
data: ''
};
}
else if ( done( parser.template[ parser.index ] ) ) {
currentChunk.end = parser.index;
if ( quoteMark ) parser.index += 1;
if ( currentChunk.data ) chunks.push( currentChunk );
chunks.forEach( chunk => {
if ( chunk.type === 'Text' ) chunk.data = decodeCharacterReferences( chunk.data );
});
return chunks;
}
else {
currentChunk.data += parser.template[ parser.index++ ];
}
}
parser.error( `Unexpected end of input` );
}