MCPcopy
hub / github.com/vuejs/core / transformText

Function transformText

packages/compiler-core/src/transforms/transformText.ts:18–119  ·  view source on GitHub ↗
(node, context)

Source from the content-addressed store, hash-verified

16// Merge adjacent text nodes and expressions into a single expression
17// e.g. <div>abc {{ d }} {{ e }}</div> should have a single expression node as child.
18export const transformText: NodeTransform = (node, context) => {
19 if (
20 node.type === NodeTypes.ROOT ||
21 node.type === NodeTypes.ELEMENT ||
22 node.type === NodeTypes.FOR ||
23 node.type === NodeTypes.IF_BRANCH
24 ) {
25 // perform the transform on node exit so that all expressions have already
26 // been processed.
27 return () => {
28 const children = node.children
29 let currentContainer: CompoundExpressionNode | undefined = undefined
30 let hasText = false
31
32 for (let i = 0; i < children.length; i++) {
33 const child = children[i]
34 if (isText(child)) {
35 hasText = true
36 for (let j = i + 1; j < children.length; j++) {
37 const next = children[j]
38 if (isText(next)) {
39 if (!currentContainer) {
40 currentContainer = children[i] = createCompoundExpression(
41 [child],
42 child.loc,
43 )
44 }
45 // merge adjacent text node into current
46 currentContainer.children.push(` + `, next)
47 children.splice(j, 1)
48 j--
49 } else {
50 currentContainer = undefined
51 break
52 }
53 }
54 }
55 }
56
57 if (
58 !hasText ||
59 // if this is a plain element with a single text child, leave it
60 // as-is since the runtime has dedicated fast path for this by directly
61 // setting textContent of the element.
62 // for component root it's always normalized anyway.
63 (children.length === 1 &&
64 (node.type === NodeTypes.ROOT ||
65 (node.type === NodeTypes.ELEMENT &&
66 node.tagType === ElementTypes.ELEMENT &&
67 // #3756
68 // custom directives can potentially add DOM elements arbitrarily,
69 // we need to avoid setting textContent of the element at runtime
70 // to avoid accidentally overwriting the DOM elements added
71 // by the user through custom directives.
72 !node.props.find(
73 p =>
74 p.type === NodeTypes.DIRECTIVE &&
75 !context.directiveTransforms[p.name],

Callers

nothing calls this directly

Calls 7

isTextFunction · 0.90
createCompoundExpressionFunction · 0.90
getConstantTypeFunction · 0.90
createCallExpressionFunction · 0.90
findMethod · 0.80
pushMethod · 0.65
helperMethod · 0.65

Tested by

no test coverage detected