提交 50bbebd7 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

wip(uts): transformText

上级 4201aa5f
......@@ -30,6 +30,26 @@ describe('compiler: transform text', () => {
]),
createElementVNode("text", null, toDisplayString(_ctx.eee) + "fff", 1 /* TEXT */),
createElementVNode("text", null, toDisplayString(_ctx.ggg), 1 /* TEXT */)
])`
)
})
test('\n', () => {
assert(
`<view>
<text>\\\\\n 换行</text>
<text>\\\\n 换行</text>
<text>\\\n 换行</text>
<text>\\n 换行</text>
<text>\n 换行</text>
<text>\n 换行 \\n 换行 \\\n 换行 \\\\n 换行 \\\\\n 换行</text>
</view>`,
`createElementVNode(\"view\", null, [
createElementVNode(\"text\", null, \"\\\\\\\\ 换行\"),
createElementVNode(\"text\", null, \"\\\\n 换行\"),
createElementVNode(\"text\", null, \"\\\\ 换行\"),
createElementVNode(\"text\", null, \"\\n 换行\"),
createElementVNode(\"text\", null, \" 换行\"),
createElementVNode(\"text\", null, \" 换行 \\n 换行 \\\\ 换行 \\\\\\n 换行 \\\\\\\\ 换行\")
])`
)
})
......
......@@ -84,16 +84,13 @@ function parseText(node: ElementNode) {
let firstTextChild
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i]
if (isText(child) && typeof (child as TextNode).content === 'string') {
const content = (child as TextNode).content
if (isText(child) && typeof content === 'string') {
if (!firstTextChild) {
firstTextChild = child
;(firstTextChild as TextNode).content = (
firstTextChild as TextNode
).content.replace(/\\n/g, '\n')
;(firstTextChild as TextNode).content = translateObliqueLine(content)
} else {
;(firstTextChild as TextNode).content += (
child as TextNode
).content.replace(/\\n/g, '\n')
;(firstTextChild as TextNode).content += translateObliqueLine(content)
node.children.splice(i, 1)
i--
}
......@@ -123,3 +120,17 @@ function createText(
loc: node.loc,
}
}
function translateObliqueLine(content: string): string {
const strFragments = content.split('\\n')
return strFragments
.map((str, index) => {
if (index === strFragments.length - 1) return str
str += '\\n'
if (!(str.split('\\').length % 2)) {
str = str.replaceAll(/\\n/g, '\n')
}
return str.replaceAll(/\\\\/g, '\\')
})
.join('')
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册