Skip to content

Commit 2457440

Browse files
Core: Moved pattern matching + lookbehind logic into function (#2633)
1 parent 5cf9cfb commit 2457440

File tree

5 files changed

+92
-62
lines changed

5 files changed

+92
-62
lines changed

Diff for: components/prism-core.js

+29-19
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,25 @@ Token.stringify = function stringify(o, language) {
823823
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
824824
};
825825

826+
/**
827+
* @param {RegExp} pattern
828+
* @param {number} pos
829+
* @param {string} text
830+
* @param {boolean} lookbehind
831+
* @returns {RegExpExecArray | null}
832+
*/
833+
function matchPattern(pattern, pos, text, lookbehind) {
834+
pattern.lastIndex = pos;
835+
var match = pattern.exec(text);
836+
if (match && lookbehind && match[1]) {
837+
// change the match to remove the text matched by the Prism lookbehind group
838+
var lookbehindLength = match[1].length;
839+
match.index += lookbehindLength;
840+
match[0] = match[0].slice(lookbehindLength);
841+
}
842+
return match;
843+
}
844+
826845
/**
827846
* @param {string} text
828847
* @param {LinkedList<string | Token>} tokenList
@@ -855,7 +874,6 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
855874
inside = patternObj.inside,
856875
lookbehind = !!patternObj.lookbehind,
857876
greedy = !!patternObj.greedy,
858-
lookbehindLength = 0,
859877
alias = patternObj.alias;
860878

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

891909
var removeCount = 1; // this is the to parameter of removeBetween
910+
var match;
892911

893912
if (greedy) {
894-
pattern.lastIndex = pos;
895-
var match = pattern.exec(text);
913+
match = matchPattern(pattern, pos, text, lookbehind);
896914
if (!match) {
897915
break;
898916
}
899917

900-
var from = match.index + (lookbehind && match[1] ? match[1].length : 0);
918+
var from = match.index;
901919
var to = match.index + match[0].length;
902920
var p = pos;
903921

@@ -931,24 +949,16 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
931949
str = text.slice(pos, p);
932950
match.index -= pos;
933951
} else {
934-
pattern.lastIndex = 0;
935-
936-
var match = pattern.exec(str);
937-
}
938-
939-
if (!match) {
940-
continue;
941-
}
942-
943-
if (lookbehind) {
944-
lookbehindLength = match[1] ? match[1].length : 0;
952+
match = matchPattern(pattern, 0, str, lookbehind);
953+
if (!match) {
954+
continue;
955+
}
945956
}
946957

947-
var from = match.index + lookbehindLength,
948-
matchStr = match[0].slice(lookbehindLength),
949-
to = from + matchStr.length,
958+
var from = match.index,
959+
matchStr = match[0],
950960
before = str.slice(0, from),
951-
after = str.slice(to);
961+
after = str.slice(from + matchStr.length);
952962

953963
var reach = pos + str.length;
954964
if (rematch && reach > rematch.reach) {

Diff for: components/prism-core.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/global.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ <h4 class="name" id="Grammar">Grammar</h4>
143143

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

149149

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

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

280280

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

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

565565

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

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

719719

0 commit comments

Comments
 (0)