Skip to content

Commit

Permalink
fix(parser): fix interpolation parsing in v-pre
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 4, 2023
1 parent c3b704e commit 53aaa1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
29 changes: 29 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,35 @@ describe('compiler: parse', () => {
})
})

// https://github.com/vuejs/docs/issues/2586
test('v-pre with half-open interpolation', () => {
const ast = baseParse(
`<div v-pre>
<span>{{ number </span>
<span>}}</span>
</div>
`
)
expect((ast.children[0] as ElementNode).children).toMatchObject([
{
type: NodeTypes.ELEMENT,
children: [{ type: NodeTypes.TEXT, content: `{{ number ` }]
},
{
type: NodeTypes.ELEMENT,
children: [{ type: NodeTypes.TEXT, content: `}}` }]
}
])

const ast2 = baseParse(`<div v-pre><span>{{ number </span></div>`)
expect((ast2.children[0] as ElementNode).children).toMatchObject([
{
type: NodeTypes.ELEMENT,
children: [{ type: NodeTypes.TEXT, content: `{{ number ` }]
}
])
})

test('self-closing v-pre', () => {
const ast = baseParse(
`<div v-pre/>\n<div :id="foo"><Comp/>{{ bar }}</div>`
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const tokenizer = new Tokenizer(stack, {
loc: getLoc(start)
}
if (name === 'pre') {
inVPre = true
inVPre = tokenizer.inVPre = true
currentVPreBoundary = currentOpenTag
// convert dirs before this one to attributes
const props = currentOpenTag!.props
Expand Down Expand Up @@ -652,7 +652,7 @@ function onCloseTag(el: ElementNode, end: number, isImplied = false) {
inPre--
}
if (currentVPreBoundary === el) {
inVPre = false
inVPre = tokenizer.inVPre = false
currentVPreBoundary = null
}
if (
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-core/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ export default class Tokenizer {
public inRCDATA = false
/** For disabling RCDATA tags handling */
public inXML = false
/** For disabling interpolation parsing in v-pre */
public inVPre = false
/** Record newline positions for fast line / column calculation */
private newlines: number[] = []

Expand Down Expand Up @@ -314,7 +316,7 @@ export default class Tokenizer {
this.sectionStart = this.index
} else if (!__BROWSER__ && c === CharCodes.Amp) {
this.startEntity()
} else if (c === this.delimiterOpen[0]) {
} else if (!this.inVPre && c === this.delimiterOpen[0]) {
this.state = State.InterpolationOpen
this.delimiterIndex = 0
this.stateInterpolationOpen(c)
Expand Down

0 comments on commit 53aaa1e

Please sign in to comment.