Skip to content

Commit 62f8a60

Browse files
committed
(fix) fix illegal handling at end of input code
1 parent 08cb242 commit 62f8a60

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/highlight.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,18 @@ const HLJS = function(hljs) {
521521
}
522522
}
523523

524-
// edge case for when illegal matches $ (end of line) which is technically
524+
// edge case for when illegal matches $ (end of line/text) which is technically
525525
// a 0 width match but not a begin/end match so it's not caught by the
526-
// first handler (when ignoreIllegals is true)
526+
// first handler (when `ignoreIllegals` is true)
527527
if (match.type === "illegal" && lexeme === "") {
528-
// advance so we aren't stuck in an infinite loop
529-
modeBuffer += "\n";
528+
if (match.index === codeToHighlight.length) {
529+
// we have matched the end of the text, so we can stop without
530+
// hacking modeBuffer
531+
} else {
532+
// matched literal `\n` (with `$`) so we must manually add the newline
533+
// itself to the modeBuffer so it is not lost when we advance the cursor
534+
modeBuffer += "\n";
535+
}
530536
return 1;
531537
}
532538

0 commit comments

Comments
 (0)