Skip to content

Commit

Permalink
Core: Fixed bug with greedy matching (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Nov 25, 2020
1 parent 3f4ae00 commit 8fa8dd2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/prism-core.js
Expand Up @@ -890,7 +890,7 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {

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

if (greedy && currentNode != tokenList.tail.prev) {
if (greedy) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
if (!match) {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/prism-core.js.html
Expand Up @@ -943,7 +943,7 @@ <h1 class="page-title">prism-core.js</h1>

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

if (greedy &amp;&amp; currentNode != tokenList.tail.prev) {
if (greedy) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
if (!match) {
Expand Down
2 changes: 1 addition & 1 deletion prism.js
Expand Up @@ -895,7 +895,7 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {

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

if (greedy && currentNode != tokenList.tail.prev) {
if (greedy) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
if (!match) {
Expand Down
21 changes: 21 additions & 0 deletions tests/core/greedy.js
Expand Up @@ -84,4 +84,25 @@ describe('Greedy matching', function () {
]
});
});

it('should always match tokens against the whole text', function () {
// this is to test for a bug where greedy tokens where matched like non-greedy ones if the token stream ended on
// a string
testTokens({
grammar: {
'a': /a/,
'b': {
pattern: /^b/,
greedy: true
}
},
code: 'bab',
expected: [
["b", "b"],
["a", "a"],
"b"
]
});
});

});

0 comments on commit 8fa8dd2

Please sign in to comment.