Skip to content

Commit 8fa8dd2

Browse files
Core: Fixed bug with greedy matching (#2632)
1 parent 3f4ae00 commit 8fa8dd2

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

components/prism-core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
890890

891891
var removeCount = 1; // this is the to parameter of removeBetween
892892

893-
if (greedy && currentNode != tokenList.tail.prev) {
893+
if (greedy) {
894894
pattern.lastIndex = pos;
895895
var match = pattern.exec(text);
896896
if (!match) {

components/prism-core.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/prism-core.js.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ <h1 class="page-title">prism-core.js</h1>
943943

944944
var removeCount = 1; // this is the to parameter of removeBetween
945945

946-
if (greedy &amp;&amp; currentNode != tokenList.tail.prev) {
946+
if (greedy) {
947947
pattern.lastIndex = pos;
948948
var match = pattern.exec(text);
949949
if (!match) {

prism.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
895895

896896
var removeCount = 1; // this is the to parameter of removeBetween
897897

898-
if (greedy && currentNode != tokenList.tail.prev) {
898+
if (greedy) {
899899
pattern.lastIndex = pos;
900900
var match = pattern.exec(text);
901901
if (!match) {

tests/core/greedy.js

+21
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,25 @@ describe('Greedy matching', function () {
8484
]
8585
});
8686
});
87+
88+
it('should always match tokens against the whole text', function () {
89+
// this is to test for a bug where greedy tokens where matched like non-greedy ones if the token stream ended on
90+
// a string
91+
testTokens({
92+
grammar: {
93+
'a': /a/,
94+
'b': {
95+
pattern: /^b/,
96+
greedy: true
97+
}
98+
},
99+
code: 'bab',
100+
expected: [
101+
["b", "b"],
102+
["a", "a"],
103+
"b"
104+
]
105+
});
106+
});
107+
87108
});

0 commit comments

Comments
 (0)