Skip to content

Commit

Permalink
Core: Moved pattern matching + lookbehind logic into function (#2633)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Nov 25, 2020
1 parent 5cf9cfb commit 2457440
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 62 deletions.
48 changes: 29 additions & 19 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,25 @@ Token.stringify = function stringify(o, language) {
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
};

/**
* @param {RegExp} pattern
* @param {number} pos
* @param {string} text
* @param {boolean} lookbehind
* @returns {RegExpExecArray | null}
*/
function matchPattern(pattern, pos, text, lookbehind) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
if (match && lookbehind && match[1]) {
// change the match to remove the text matched by the Prism lookbehind group
var lookbehindLength = match[1].length;
match.index += lookbehindLength;
match[0] = match[0].slice(lookbehindLength);
}
return match;
}

/**
* @param {string} text
* @param {LinkedList<string | Token>} tokenList
Expand Down Expand Up @@ -855,7 +874,6 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
inside = patternObj.inside,
lookbehind = !!patternObj.lookbehind,
greedy = !!patternObj.greedy,
lookbehindLength = 0,
alias = patternObj.alias;

if (greedy && !patternObj.pattern.global) {
Expand Down Expand Up @@ -889,15 +907,15 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
}

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

if (greedy) {
pattern.lastIndex = pos;
var match = pattern.exec(text);
match = matchPattern(pattern, pos, text, lookbehind);
if (!match) {
break;
}

var from = match.index + (lookbehind && match[1] ? match[1].length : 0);
var from = match.index;
var to = match.index + match[0].length;
var p = pos;

Expand Down Expand Up @@ -931,24 +949,16 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
str = text.slice(pos, p);
match.index -= pos;
} else {
pattern.lastIndex = 0;

var match = pattern.exec(str);
}

if (!match) {
continue;
}

if (lookbehind) {
lookbehindLength = match[1] ? match[1].length : 0;
match = matchPattern(pattern, 0, str, lookbehind);
if (!match) {
continue;
}
}

var from = match.index + lookbehindLength,
matchStr = match[0].slice(lookbehindLength),
to = from + matchStr.length,
var from = match.index,
matchStr = match[0],
before = str.slice(0, from),
after = str.slice(to);
after = str.slice(from + matchStr.length);

var reach = pos + str.length;
if (rematch && reach > rematch.reach) {
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.

8 changes: 4 additions & 4 deletions docs/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ <h4 class="name" id="Grammar">Grammar</h4>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1161">line 1161</a>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1171">line 1171</a>
</li></ul></dd>


Expand Down Expand Up @@ -274,7 +274,7 @@ <h4 class="name" id="GrammarToken">GrammarToken</h4>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1140">line 1140</a>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1150">line 1150</a>
</li></ul></dd>


Expand Down Expand Up @@ -559,7 +559,7 @@ <h4 class="name" id="HighlightCallback"><span class="type-signature"></span>High

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1169">line 1169</a>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1179">line 1179</a>
</li></ul></dd>


Expand Down Expand Up @@ -713,7 +713,7 @@ <h4 class="name" id="HookCallback"><span class="type-signature"></span>HookCallb

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1179">line 1179</a>
<a href="prism-core.js.html">prism-core.js</a>, <a href="prism-core.js.html#line1189">line 1189</a>
</li></ul></dd>


Expand Down
Loading

0 comments on commit 2457440

Please sign in to comment.