Skip to content

Commit 53aaa1e

Browse files
committed
fix(parser): fix interpolation parsing in v-pre
ref: vuejs/docs#2586
1 parent c3b704e commit 53aaa1e

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,35 @@ describe('compiler: parse', () => {
17331733
})
17341734
})
17351735

1736+
// https://github.com/vuejs/docs/issues/2586
1737+
test('v-pre with half-open interpolation', () => {
1738+
const ast = baseParse(
1739+
`<div v-pre>
1740+
<span>{{ number </span>
1741+
<span>}}</span>
1742+
</div>
1743+
`
1744+
)
1745+
expect((ast.children[0] as ElementNode).children).toMatchObject([
1746+
{
1747+
type: NodeTypes.ELEMENT,
1748+
children: [{ type: NodeTypes.TEXT, content: `{{ number ` }]
1749+
},
1750+
{
1751+
type: NodeTypes.ELEMENT,
1752+
children: [{ type: NodeTypes.TEXT, content: `}}` }]
1753+
}
1754+
])
1755+
1756+
const ast2 = baseParse(`<div v-pre><span>{{ number </span></div>`)
1757+
expect((ast2.children[0] as ElementNode).children).toMatchObject([
1758+
{
1759+
type: NodeTypes.ELEMENT,
1760+
children: [{ type: NodeTypes.TEXT, content: `{{ number ` }]
1761+
}
1762+
])
1763+
})
1764+
17361765
test('self-closing v-pre', () => {
17371766
const ast = baseParse(
17381767
`<div v-pre/>\n<div :id="foo"><Comp/>{{ bar }}</div>`

packages/compiler-core/src/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ const tokenizer = new Tokenizer(stack, {
236236
loc: getLoc(start)
237237
}
238238
if (name === 'pre') {
239-
inVPre = true
239+
inVPre = tokenizer.inVPre = true
240240
currentVPreBoundary = currentOpenTag
241241
// convert dirs before this one to attributes
242242
const props = currentOpenTag!.props
@@ -652,7 +652,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
652652
inPre--
653653
}
654654
if (currentVPreBoundary === el) {
655-
inVPre = false
655+
inVPre = tokenizer.inVPre = false
656656
currentVPreBoundary = null
657657
}
658658
if (

packages/compiler-core/src/tokenizer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export default class Tokenizer {
242242
public inRCDATA = false
243243
/** For disabling RCDATA tags handling */
244244
public inXML = false
245+
/** For disabling interpolation parsing in v-pre */
246+
public inVPre = false
245247
/** Record newline positions for fast line / column calculation */
246248
private newlines: number[] = []
247249

@@ -314,7 +316,7 @@ export default class Tokenizer {
314316
this.sectionStart = this.index
315317
} else if (!__BROWSER__ && c === CharCodes.Amp) {
316318
this.startEntity()
317-
} else if (c === this.delimiterOpen[0]) {
319+
} else if (!this.inVPre && c === this.delimiterOpen[0]) {
318320
this.state = State.InterpolationOpen
319321
this.delimiterIndex = 0
320322
this.stateInterpolationOpen(c)

0 commit comments

Comments
 (0)