Skip to content

Commit

Permalink
Fix minor bug with new greedy-feature
Browse files Browse the repository at this point in the history
This fixes a minor bug that causes invalid highlighting in some
edge cases, where two greedy patterns overlap each other.
  • Loading branch information
zeitgeist87 committed Mar 8, 2016
1 parent 5978d4a commit 49cf899
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
8 changes: 6 additions & 2 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ var _ = _self.Prism = {
delNum = 3;

if (to <= len) {
if (strarr[i + 1].greedy) {
continue;
}
delNum = 2;
combStr = combStr.slice(0, len);
}
Expand All @@ -353,7 +356,7 @@ var _ = _self.Prism = {
args.push(before);
}

var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match);
var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match, greedy);

args.push(wrapped);

Expand Down Expand Up @@ -394,12 +397,13 @@ var _ = _self.Prism = {
}
};

var Token = _.Token = function(type, content, alias, matchedStr) {
var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
this.type = type;
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.matchedStr = matchedStr || null;
this.greedy = !!greedy;
};

Token.stringify = function(o, language, parent) {
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.

1 change: 1 addition & 0 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
Prism.languages.insertBefore('javascript', 'class-name', {
'template-string': {
pattern: /`(?:\\\\|\\?[^\\])*?`/,
greedy: true,
inside: {
'interpolation': {
pattern: /\$\{[^}]+\}/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

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

9 changes: 7 additions & 2 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ var _ = _self.Prism = {
delNum = 3;

if (to <= len) {
if (strarr[i + 1].greedy) {
continue;
}
delNum = 2;
combStr = combStr.slice(0, len);
}
Expand All @@ -358,7 +361,7 @@ var _ = _self.Prism = {
args.push(before);
}

var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match);
var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match, greedy);

args.push(wrapped);

Expand Down Expand Up @@ -399,12 +402,13 @@ var _ = _self.Prism = {
}
};

var Token = _.Token = function(type, content, alias, matchedStr) {
var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
this.type = type;
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.matchedStr = matchedStr || null;
this.greedy = !!greedy;
};

Token.stringify = function(o, language, parent) {
Expand Down Expand Up @@ -657,6 +661,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
Prism.languages.insertBefore('javascript', 'class-name', {
'template-string': {
pattern: /`(?:\\\\|\\?[^\\])*?`/,
greedy: true,
inside: {
'interpolation': {
pattern: /\$\{[^}]+\}/,
Expand Down
4 changes: 3 additions & 1 deletion tests/languages/javascript/regex_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/[\[\]]{2,4}(?:foo)*/;
/foo"test"bar/
/foo\//
1 / 4 + "/, not a regex";

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

Expand All @@ -11,7 +12,8 @@
["regex", "/foo/gimyu"], ["punctuation", ","],
["regex", "/[\\[\\]]{2,4}(?:foo)*/"], ["punctuation", ";"],
["regex", "/foo\"test\"bar/"],
["regex", "/foo\\//"]
["regex", "/foo\\//"],
["number", "1"], ["operator", "/"], ["number", "4"], ["operator", "+"], ["string", "\"/, not a regex\""], ["punctuation", ";"]
]

----------------------------------------------------
Expand Down

0 comments on commit 49cf899

Please sign in to comment.