Skip to content

Commit

Permalink
Core: Greedy patterns should always be matched against the full string.
Browse files Browse the repository at this point in the history
Fix #1355
  • Loading branch information
Golmote committed Mar 26, 2018
1 parent 172d351 commit 294efaa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
22 changes: 9 additions & 13 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,9 @@ var _ = _self.Prism = {
continue;
}

pattern.lastIndex = 0;

var match = pattern.exec(str),
delNum = 1;

// Greedy patterns can override/remove up to two previously matched tokens
if (!match && greedy && i != strarr.length - 1) {
if (greedy && i != strarr.length - 1) {
pattern.lastIndex = pos;
match = pattern.exec(text);
var match = pattern.exec(text);
if (!match) {
break;
}
Expand All @@ -355,18 +349,20 @@ var _ = _self.Prism = {
}
}

/*
* If strarr[i] is a Token, then the match starts inside another Token, which is invalid
* If strarr[k - 1] is greedy we are in conflict with another greedy pattern
*/
if (strarr[i] instanceof Token || strarr[k - 1].greedy) {
// If strarr[i] is a Token, then the match starts inside another Token, which is invalid
if (strarr[i] instanceof Token) {
continue;
}

// Number of tokens to delete and replace with the new match
delNum = k - i;
str = text.slice(pos, p);
match.index -= pos;
} else {
pattern.lastIndex = 0;

var match = pattern.exec(str),
delNum = 1;
}

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.

22 changes: 9 additions & 13 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,9 @@ var _ = _self.Prism = {
continue;
}

pattern.lastIndex = 0;

var match = pattern.exec(str),
delNum = 1;

// Greedy patterns can override/remove up to two previously matched tokens
if (!match && greedy && i != strarr.length - 1) {
if (greedy && i != strarr.length - 1) {
pattern.lastIndex = pos;
match = pattern.exec(text);
var match = pattern.exec(text);
if (!match) {
break;
}
Expand All @@ -360,18 +354,20 @@ var _ = _self.Prism = {
}
}

/*
* If strarr[i] is a Token, then the match starts inside another Token, which is invalid
* If strarr[k - 1] is greedy we are in conflict with another greedy pattern
*/
if (strarr[i] instanceof Token || strarr[k - 1].greedy) {
// If strarr[i] is a Token, then the match starts inside another Token, which is invalid
if (strarr[i] instanceof Token) {
continue;
}

// Number of tokens to delete and replace with the new match
delNum = k - i;
str = text.slice(pos, p);
match.index -= pos;
} else {
pattern.lastIndex = 0;

var match = pattern.exec(str),
delNum = 1;
}

if (!match) {
Expand Down
24 changes: 24 additions & 0 deletions tests/languages/python/issue1355.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
print('""#')
print('"trigger="#T'+str(3))

----------------------------------------------------

[
["keyword", "print"],
["punctuation", "("],
["string", "'\"\"#'"],
["punctuation", ")"],
["keyword", "print"],
["punctuation", "("],
["string", "'\"trigger=\"#T'"],
["operator", "+"],
["builtin", "str"],
["punctuation", "("],
["number", "3"],
["punctuation", ")"],
["punctuation", ")"]
]

----------------------------------------------------

Checks for comment-like substrings. See #1355

0 comments on commit 294efaa

Please sign in to comment.