-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-attribute.mjs
42 lines (36 loc) · 1021 Bytes
/
parse-attribute.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
const nameOffset = '= \t\n\r>'
export const parseAttribute = state => {
/// Parses an element's attributes into an Object.
/// => undefined
if (/\s/.test(state.peek())) {
return
}
const onset = state.pos
while (!nameOffset.includes(state.peek())) {
state.pos++
}
const attrName = state.src.slice(onset, state.pos)
state.hostNode[attrName] = 'true'
if (state.peek() === '=') {
state.pos++ // step past: =
if (state.peek() !== '"') {
throw new Error(
`Invalid ${attrName} attribute at ${state.pos} (double quotes are required).`
)
}
state.pos++ // step post opening double quote.
const valueOnset = state.pos
state.pos = state.src.indexOf('"', state.pos)
if (state.peek() !== '"') {
throw new Error(
`Invalid ${attrName} attribute at ${onset} (double quotes are required).`
)
}
const attrValue = state.src.slice(valueOnset, state.pos)
if (attrName === 'class') {
state.hostNode.className = attrValue
} else {
state.hostNode[attrName] = attrValue
}
}
}