Skip to content

Commit f4e14f9

Browse files
committed
refactor compileTextNode
1 parent 5d934bf commit f4e14f9

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

src/compile/compile.js

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -171,43 +171,52 @@ function compileTextNode (node, options) {
171171
return null
172172
}
173173
var frag = document.createDocumentFragment()
174-
var dirs = options.directives
175-
var el, token, value
174+
var el, token
176175
for (var i = 0, l = tokens.length; i < l; i++) {
177176
token = tokens[i]
178-
value = token.value
179-
if (token.tag) {
180-
if (token.oneTime) {
181-
el = document.createTextNode(value)
182-
} else {
183-
if (token.html) {
184-
el = document.createComment('v-html')
185-
token.type = 'html'
186-
token.def = dirs.html
187-
token.descriptor = dirParser.parse(value)[0]
188-
} else if (token.partial) {
189-
el = document.createComment('v-partial')
190-
token.type = 'partial'
191-
token.def = dirs.partial
192-
token.descriptor = dirParser.parse(value)[0]
193-
} else {
194-
// IE will clean up empty textNodes during
195-
// frag.cloneNode(true), so we have to give it
196-
// something here...
197-
el = document.createTextNode(' ')
198-
token.type = 'text'
199-
token.def = dirs.text
200-
token.descriptor = dirParser.parse(value)[0]
201-
}
202-
}
203-
} else {
204-
el = document.createTextNode(value)
205-
}
177+
el = token.tag
178+
? processTextToken(token, options)
179+
: document.createTextNode(token.value)
206180
frag.appendChild(el)
207181
}
208182
return makeTextNodeLinkFn(tokens, frag, options)
209183
}
210184

185+
/**
186+
* Process a single text token.
187+
*
188+
* @param {Object} token
189+
* @param {Object} options
190+
* @return {Node}
191+
*/
192+
193+
function processTextToken (token, options) {
194+
var el
195+
if (token.oneTime) {
196+
el = document.createTextNode(token.value)
197+
} else {
198+
if (token.html) {
199+
el = document.createComment('v-html')
200+
setTokenType('html')
201+
} else if (token.partial) {
202+
el = document.createComment('v-partial')
203+
setTokenType('partial')
204+
} else {
205+
// IE will clean up empty textNodes during
206+
// frag.cloneNode(true), so we have to give it
207+
// something here...
208+
el = document.createTextNode(' ')
209+
setTokenType('text')
210+
}
211+
}
212+
function setTokenType (type) {
213+
token.type = type
214+
token.def = options.directives[type]
215+
token.descriptor = dirParser.parse(token.value)[0]
216+
}
217+
return el
218+
}
219+
211220
/**
212221
* Build a function that processes a textNode.
213222
*

0 commit comments

Comments
 (0)