2019-10-01 02:51:55 +08:00
|
|
|
import { NodeTransform } from '../transform'
|
|
|
|
import {
|
|
|
|
NodeTypes,
|
2019-10-01 21:27:34 +08:00
|
|
|
TemplateChildNode,
|
2019-10-01 02:51:55 +08:00
|
|
|
TextNode,
|
|
|
|
InterpolationNode,
|
|
|
|
CompoundExpressionNode
|
|
|
|
} from '../ast'
|
|
|
|
|
2019-10-01 21:27:34 +08:00
|
|
|
const isText = (
|
|
|
|
node: TemplateChildNode
|
|
|
|
): node is TextNode | InterpolationNode =>
|
2019-10-01 02:51:55 +08:00
|
|
|
node.type === NodeTypes.INTERPOLATION || node.type === NodeTypes.TEXT
|
|
|
|
|
|
|
|
// Merge adjacent text nodes and expressions into a single expression
|
|
|
|
// e.g. <div>abc {{ d }} {{ e }}</div> should have a single expression node as child.
|
|
|
|
export const optimizeText: NodeTransform = node => {
|
|
|
|
if (node.type === NodeTypes.ROOT || node.type === NodeTypes.ELEMENT) {
|
|
|
|
// perform the transform on node exit so that all expressions have already
|
|
|
|
// been processed.
|
|
|
|
return () => {
|
|
|
|
const children = node.children
|
|
|
|
let currentContainer: CompoundExpressionNode | undefined = undefined
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const child = children[i]
|
|
|
|
if (isText(child)) {
|
|
|
|
for (let j = i + 1; j < children.length; j++) {
|
|
|
|
const next = children[j]
|
|
|
|
if (isText(next)) {
|
|
|
|
if (!currentContainer) {
|
|
|
|
currentContainer = children[i] = {
|
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION,
|
|
|
|
loc: child.loc,
|
|
|
|
children: [child]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// merge adjacent text node into current
|
|
|
|
currentContainer.children.push(` + `, next)
|
|
|
|
children.splice(j, 1)
|
|
|
|
j--
|
|
|
|
} else {
|
|
|
|
currentContainer = undefined
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|