Skip to content

fix(compiler-core): prevent incorrect entity parsing with logical AND #13363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,17 @@ describe('compiler: parse', () => {
})
})

// #13361
test('directive with multiline value', () => {
const ast = baseParse(
`<div v-if="
foo &&
bar
"></div>`,
)
expect(ast.loc.end.line).toBe(4)
})

test('directive with argument', () => {
const ast = baseParse('<div v-on:click/>')
const directive = (ast.children[0] as ElementNode).props[0]
Expand Down
15 changes: 11 additions & 4 deletions packages/compiler-core/src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default class Tokenizer {
}
this.state = State.BeforeTagName
this.sectionStart = this.index
} else if (!__BROWSER__ && c === CharCodes.Amp) {
} else if (!__BROWSER__ && c === CharCodes.Amp && !this.isLogicalAnd()) {
this.startEntity()
} else if (!this.inVPre && c === this.delimiterOpen[0]) {
this.state = State.InterpolationOpen
Expand Down Expand Up @@ -436,7 +436,7 @@ export default class Tokenizer {
(this.currentSequence === Sequences.TextareaEnd && !this.inSFCRoot)
) {
// We have to parse entities in <title> and <textarea> tags.
if (!__BROWSER__ && c === CharCodes.Amp) {
if (!__BROWSER__ && c === CharCodes.Amp && !this.isLogicalAnd()) {
this.startEntity()
} else if (!this.inVPre && c === this.delimiterOpen[0]) {
// We also need to handle interpolation
Expand Down Expand Up @@ -795,7 +795,7 @@ export default class Tokenizer {
this.index + 1,
)
this.state = State.BeforeAttrName
} else if (!__BROWSER__ && c === CharCodes.Amp) {
} else if (!__BROWSER__ && c === CharCodes.Amp && !this.isLogicalAnd()) {
this.startEntity()
}
}
Expand Down Expand Up @@ -823,7 +823,7 @@ export default class Tokenizer {
ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
this.index,
)
} else if (!__BROWSER__ && c === CharCodes.Amp) {
} else if (!__BROWSER__ && c === CharCodes.Amp && !this.isLogicalAnd()) {
this.startEntity()
}
}
Expand Down Expand Up @@ -1178,4 +1178,11 @@ export default class Tokenizer {
}
}
}

private isLogicalAnd(): boolean {
return (
this.buffer.charCodeAt(this.index - 1) === CharCodes.Amp ||
this.buffer.charCodeAt(this.index + 1) === CharCodes.Amp
)
}
}