Skip to content

Commit 0351cf7

Browse files
committed
prevent string literal error when entityRef in xpath string literal
1 parent a4b4138 commit 0351cf7

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/xpLexer.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,16 @@ export class XPathLexer {
503503
if (result.length > 0) {
504504
let lastToken = result[result.length - 1];
505505
if (lastToken.tokenType === TokenLevelState.string) {
506-
XPathLexer.checkStringLiteralEnd(lastToken);
506+
XPathLexer.checkExitStringLiteralEnd(lastToken, result);
507+
} else if (lastToken.tokenType === TokenLevelState.entityRef) {
508+
if (result.length > 1) {
509+
const nextLastToken = result[result.length - 2];
510+
if (nextLastToken.tokenType === TokenLevelState.string) {
511+
if (!lastToken.value.endsWith('"') && !lastToken.value.startsWith(''')) {
512+
lastToken['error'] = ErrorType.XPathStringLiteral;
513+
}
514+
}
515+
}
507516
}
508517
}
509518
position.line = this.lineNumber;
@@ -666,13 +675,30 @@ export class XPathLexer {
666675
return result;
667676
}
668677

678+
public static checkExitStringLiteralEnd(lastToken: BaseToken, result: BaseToken[]) {
679+
let followsEntityRef = false;
680+
if (result.length > 1) {
681+
const nextLastToken = result[result.length - 2];
682+
followsEntityRef = nextLastToken.tokenType === TokenLevelState.entityRef;
683+
}
684+
if (followsEntityRef) {
685+
let lastChar = lastToken.value.charAt(lastToken.value.length - 1);
686+
const lastCharIsSingleQuote = lastChar === "'";
687+
if (lastChar !== '"' && !lastCharIsSingleQuote && !lastToken.value.endsWith('"') && !lastToken.value.startsWith(''')) {
688+
lastToken['error'] = ErrorType.XPathStringLiteral;
689+
}
690+
} else {
691+
this.checkStringLiteralEnd(lastToken);
692+
}
693+
}
694+
669695
public static checkStringLiteralEnd(lastToken: BaseToken) {
670-
let firstChar = lastToken.value.charAt(0);
671696
let lastChar = lastToken.value.charAt(lastToken.value.length - 1);
697+
let firstChar = lastToken.value.charAt(0);
672698
if (!((lastChar === firstChar && lastToken.value.length > 1) || (lastToken.value.length > 6 &&
673699
(lastToken.value.startsWith('"') && lastToken.value.endsWith('"')) || (lastToken.value.startsWith(''') && lastToken.value.endsWith('''))))) {
674700
lastToken['error'] = ErrorType.XPathStringLiteral;
675-
}
701+
}
676702
}
677703

678704
private static closeMatchesOpen(close: CharLevelState, stack: Token[]): boolean {

0 commit comments

Comments
 (0)