Skip to content

Commit

Permalink
Fix greedy-flag bug
Browse files Browse the repository at this point in the history
This bug occurs in the relatively rare case of a pattern matching the
empty string. It was reported in issue #1039. If for example a HTML
page contains an empty script tag `<script></script>` then the script
pattern will match anything inside, which is the empty string.

This empty string is then passed to the constructor of the Token class.
Since `""` is falsy in Javascript the property `matchedStr` is set to
`null`.

But the property `matchedStr` is needed to calculate the current
position for the greedy feature. A `null` value in `matchedStr` results
in a `pos` that is `NaN`. This causes the bug described in issue #1039.

Since the property `matchedStr` is only ever needed to calculate the
length of the Token, it is more efficient to store the length directly
instead of the string. As a side effect this also fixes issue #1039.
  • Loading branch information
zeitgeist87 committed Oct 24, 2016
1 parent 03ecf74 commit 32cd99f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ var _ = _self.Prism = {
pattern = pattern.pattern || pattern;

// Don’t cache length as it changes during the loop
for (var i=0, pos = 0; i<strarr.length; pos += (strarr[i].matchedStr || strarr[i]).length, ++i) {
for (var i=0, pos = 0; i<strarr.length; pos += strarr[i].length, ++i) {

var str = strarr[i];

Expand Down Expand Up @@ -321,7 +321,7 @@ var _ = _self.Prism = {
p = pos;

for (var len = strarr.length; k < len && p < to; ++k) {
p += (strarr[k].matchedStr || strarr[k]).length;
p += strarr[k].length;
// Move the index i to the element in strarr that is closest to from
if (from >= p) {
++i;
Expand Down Expand Up @@ -409,7 +409,7 @@ var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.matchedStr = matchedStr || null;
this.length = (matchedStr || "").length|0;
this.greedy = !!greedy;
};

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.

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

// Don’t cache length as it changes during the loop
for (var i=0, pos = 0; i<strarr.length; pos += (strarr[i].matchedStr || strarr[i]).length, ++i) {
for (var i=0, pos = 0; i<strarr.length; pos += strarr[i].length, ++i) {

var str = strarr[i];

Expand Down Expand Up @@ -326,7 +326,7 @@ var _ = _self.Prism = {
p = pos;

for (var len = strarr.length; k < len && p < to; ++k) {
p += (strarr[k].matchedStr || strarr[k]).length;
p += strarr[k].length;
// Move the index i to the element in strarr that is closest to from
if (from >= p) {
++i;
Expand Down Expand Up @@ -414,7 +414,7 @@ var Token = _.Token = function(type, content, alias, matchedStr, greedy) {
this.content = content;
this.alias = alias;
// Copy of the full string this token was created from
this.matchedStr = matchedStr || null;
this.length = (matchedStr || "").length|0;
this.greedy = !!greedy;
};

Expand Down

0 comments on commit 32cd99f

Please sign in to comment.