-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-tag.mjs
149 lines (128 loc) · 3.21 KB
/
parse-tag.mjs
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
import {createNode} from './node.mjs'
import {parseComment} from './parse-comment.mjs'
import {parseTextNode} from './parse-textnode.mjs'
import {parseAttribute} from './parse-attribute.mjs'
const syntaxError = (state, node) => {
throw new SyntaxError(
`Unclosed <${node.nodeName.toLowerCase()}> tag at: `+
`${state.src.slice(node.onset, node.onset + 45).replace(/\n/g, '\\n')}...`
)
}
export const emptyTags = [
'AREA',
'BASE',
'BR',
'COL',
'!DOCTYPE',
'EMBED',
'HR',
'IMG',
'INPUT',
'LINK',
'META',
'PARAM',
'SOURCE',
'TRACK',
'WBR',
'EMBEDDED-FRAGMENT'
]
const tagOnset = /^<[^!\\/]/
const nameOffset = /[\s\n>]/
const parseOpeningTag = state => {
/// Parses the opening tag of an element.
/// => undefined
const onset = state.pos
state.pos++
while (!state.end() && !nameOffset.test(state.peek())) {
state.pos++
}
const nodeName = state.src.slice(onset + 1, state.pos)
state.hostNode.nodeName = nodeName.toUpperCase()
while (!state.end() && state.peek() !== '>') {
if (state.peek(2) === '/>') {
state.hostNode._closed = true
state.pos += 2
return
}
parseAttribute(state)
if (state.peek() !== '>') {
state.pos++
}
}
state.pos++ // step past final ">"
}
const parseClosingTag = (node, state) => {
/// Consumes the closing tag if it exists and returns true if so.
/// => boolean
if (state.end() || state.peek(2) !== '</') {
return false
}
// The closing tag must match the opening tag.
const close = '</' + node.nodeName.toLowerCase() + '>'
if (state.end() || state.peek(close.length) !== close) {
syntaxError(state, node)
}
state.pos += (node.nodeName.length + 3)
node._closed = true
return true
}
const consume = (state, nodeName) => {
/// Consume the content until encountering an unescaped closing
/// tag (script or style).
nodeName = nodeName.toLowerCase()
const closingTag = `</${nodeName}>`
const onset = state.pos
const node = createNode(state)
node.nodeName = 'TEXTNODE'
state.hostNode.childNodes.push(node)
const closePos = state.src.indexOf(closingTag, state.pos)
state.pos = closePos
node.nodeValue = state.src.slice(onset, state.pos)
if (state.end()) {
syntaxError(state, node)
}
}
export const parseTag = state => {
/// Parses an element into a new AST node.
/// => undefined
const next2 = state.peek(2)
if (!tagOnset.test(next2)) {
return
}
const node = createNode(state)
node.parent = state.hostNode
state.hostNode.childNodes.push(node)
state.hostNode = node
parseOpeningTag(state)
if (emptyTags.includes(node.nodeName)) {
if (node.nodeName == 'EMBEDDED-FRAGMENT') {
const fragment = state.embeddedFragments[node.uid]
fragment.parent = node
node.parent.childNodes.pop()
node.parent.childNodes.push(fragment)
}
state.hostNode = node.parent
return
} else if (node._closed) { // <foo/>
state.hostNode = node.parent
return
}
// Parse the contents of the tag.
let lastPos = state.pos
while (!parseClosingTag(node, state)) {
if (['SCRIPT','STYLE'].includes(node.nodeName)) {
consume(state, node.nodeName)
}
parseComment(state)
parseTextNode(state)
parseTag(state)
if (state.pos === lastPos) {
break
}
lastPos = state.pos
}
if (!node._closed) {
syntaxError(state, node)
}
state.hostNode = node.parent
}