Skip to content

Commit

Permalink
Core: Fixed greedy partial lookbehinds not working (#2030)
Browse files Browse the repository at this point in the history
This fixes the bug that greedy patterns with a lookbehind group did not support other top-level alternatives (e.g. `/foo|(lookbehind)bar/`) causing an error when used.
  • Loading branch information
RunDevelopment authored Aug 29, 2019
1 parent e864d51 commit 174ed10
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ var _ = {
break;
}

var from = match.index + (lookbehind ? match[1].length : 0),
var from = match.index + (lookbehind && match[1] ? match[1].length : 0),
to = match.index + match[0].length,
k = i,
p = pos;
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 prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ var _ = {
break;
}

var from = match.index + (lookbehind ? match[1].length : 0),
var from = match.index + (lookbehind && match[1] ? match[1].length : 0),
to = match.index + match[0].length,
k = i,
p = pos;
Expand Down
21 changes: 20 additions & 1 deletion tests/core/greedy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe('Greedy matching', function () {
});
});

it('should support patterns with top-level alternatives that do not contain the lookbehind group', function () {
testTokens({
grammar: {
'a': /'[^']*'/,
'b': {
// This pattern has 2 top-level alternatives: foo and (^|[^\\])"[^"]*"
pattern: /foo|(^|[^\\])"[^"]*"/,
lookbehind: true,
greedy: true
}
},
code: 'foo "bar" \'baz\'',
expected: [
["b", "foo"],
["b", "\"bar\""],
["a", "'baz'"]
]
});
});

// https://github.com/PrismJS/prism/issues/1492
/*
it('should correctly rematch tokens', function () {
Expand Down Expand Up @@ -66,4 +86,3 @@ describe('Greedy matching', function () {
});
*/
});

0 comments on commit 174ed10

Please sign in to comment.