-
Notifications
You must be signed in to change notification settings - Fork 365
/
walk.ts
187 lines (168 loc) · 4.78 KB
/
walk.ts
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
import { builtInDirectives, Directive } from './directives'
import { _if } from './directives/if'
import { _for } from './directives/for'
import { bind } from './directives/bind'
import { on } from './directives/on'
import { text } from './directives/text'
import { evaluate } from './eval'
import { checkAttr } from './utils'
import { ref } from './directives/ref'
import { Context, createScopedContext } from './context'
const dirRE = /^(?:v-|:|@)/
const modifierRE = /\.([\w-]+)/g
export let inOnce = false
export const walk = (node: Node, ctx: Context): ChildNode | null | void => {
const type = node.nodeType
if (type === 1) {
// Element
const el = node as Element
if (el.hasAttribute('v-pre')) {
return
}
checkAttr(el, 'v-cloak')
let exp: string | null
// v-if
if ((exp = checkAttr(el, 'v-if'))) {
return _if(el, exp, ctx)
}
// v-for
if ((exp = checkAttr(el, 'v-for'))) {
return _for(el, exp, ctx)
}
// v-scope
if ((exp = checkAttr(el, 'v-scope')) || exp === '') {
const scope = exp ? evaluate(ctx.scope, exp) : {}
ctx = createScopedContext(ctx, scope)
if (scope.$template) {
resolveTemplate(el, scope.$template)
}
}
// v-once
const hasVOnce = checkAttr(el, 'v-once') != null
if (hasVOnce) {
inOnce = true
}
// ref
if ((exp = checkAttr(el, 'ref'))) {
applyDirective(el, ref, `"${exp}"`, ctx)
}
// process children first before self attrs
walkChildren(el, ctx)
// other directives
const deferred: [string, string][] = []
for (const { name, value } of [...el.attributes]) {
if (dirRE.test(name) && name !== 'v-cloak') {
if (name === 'v-model') {
// defer v-model since it relies on :value bindings to be processed
// first, but also before v-on listeners (#73)
deferred.unshift([name, value])
} else if (name[0] === '@' || /^v-on\b/.test(name)) {
deferred.push([name, value])
} else {
processDirective(el, name, value, ctx)
}
}
}
for (const [name, value] of deferred) {
processDirective(el, name, value, ctx)
}
if (hasVOnce) {
inOnce = false
}
} else if (type === 3) {
// Text
const data = (node as Text).data
if (data.includes(ctx.delimiters[0])) {
let segments: string[] = []
let lastIndex = 0
let match
while ((match = ctx.delimitersRE.exec(data))) {
const leading = data.slice(lastIndex, match.index)
if (leading) segments.push(JSON.stringify(leading))
segments.push(`$s(${match[1]})`)
lastIndex = match.index + match[0].length
}
if (lastIndex < data.length) {
segments.push(JSON.stringify(data.slice(lastIndex)))
}
applyDirective(node, text, segments.join('+'), ctx)
}
} else if (type === 11) {
walkChildren(node as DocumentFragment, ctx)
}
}
const walkChildren = (node: Element | DocumentFragment, ctx: Context) => {
let child = node.firstChild
while (child) {
child = walk(child, ctx) || child.nextSibling
}
}
const processDirective = (
el: Element,
raw: string,
exp: string,
ctx: Context
) => {
let dir: Directive
let arg: string | undefined
let modifiers: Record<string, true> | undefined
// modifiers
raw = raw.replace(modifierRE, (_, m) => {
;(modifiers || (modifiers = {}))[m] = true
return ''
})
if (raw[0] === ':') {
dir = bind
arg = raw.slice(1)
} else if (raw[0] === '@') {
dir = on
arg = raw.slice(1)
} else {
const argIndex = raw.indexOf(':')
const dirName = argIndex > 0 ? raw.slice(2, argIndex) : raw.slice(2)
dir = builtInDirectives[dirName] || ctx.dirs[dirName]
arg = argIndex > 0 ? raw.slice(argIndex + 1) : undefined
}
if (dir) {
if (dir === bind && arg === 'ref') dir = ref
applyDirective(el, dir, exp, ctx, arg, modifiers)
el.removeAttribute(raw)
} else if (import.meta.env.DEV) {
console.error(`unknown custom directive ${raw}.`)
}
}
const applyDirective = (
el: Node,
dir: Directive<any>,
exp: string,
ctx: Context,
arg?: string,
modifiers?: Record<string, true>
) => {
const get = (e = exp) => evaluate(ctx.scope, e, el)
const cleanup = dir({
el,
get,
effect: ctx.effect,
ctx,
exp,
arg,
modifiers
})
if (cleanup) {
ctx.cleanups.push(cleanup)
}
}
const resolveTemplate = (el: Element, template: string) => {
if (template[0] === '#') {
const templateEl = document.querySelector(template)
if (import.meta.env.DEV && !templateEl) {
console.error(
`template selector ${template} has no matching <template> element.`
)
}
el.appendChild((templateEl as HTMLTemplateElement).content.cloneNode(true))
return
}
el.innerHTML = template
}