diff --git a/components/prism-core.js b/components/prism-core.js index 8b8343b485..29a7952d7b 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -823,6 +823,25 @@ Token.stringify = function stringify(o, language) { return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + ''; }; +/** + * @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} tokenList @@ -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) { @@ -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; @@ -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) { diff --git a/components/prism-core.min.js b/components/prism-core.min.js index 1589f00289..89173b8a76 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(u){var c=/\blang(?:uage)?-([\w-]+)\b/i,n=0,M={manual:u.Prism&&u.Prism.manual,disableWorkerMessageHandler:u.Prism&&u.Prism.disableWorkerMessageHandler,util:{encode:function e(n){return n instanceof W?new W(n.type,e(n.content),n.alias):Array.isArray(n)?n.map(e):n.replace(/&/g,"&").replace(/=l.reach);k+=y.value.length,y=y.next){var b=y.value;if(t.length>n.length)return;if(!(b instanceof W)){var x=1;if(h){m.lastIndex=k;var w=m.exec(n);if(!w)break;var A=w.index+(f&&w[1]?w[1].length:0),P=w.index+w[0].length,S=k;for(S+=y.value.length;S<=A;)y=y.next,S+=y.value.length;if(S-=y.value.length,k=S,y.value instanceof W)continue;for(var E=y;E!==t.tail&&(Sl.reach&&(l.reach=j);var C=y.prev;L&&(C=I(t,C,L),k+=L.length),z(t,C,x);var _=new W(o,g?M.tokenize(O,g):O,v,O);y=I(t,C,_),N&&I(t,y,N),1"+a.content+""},!u.document)return u.addEventListener&&(M.disableWorkerMessageHandler||u.addEventListener("message",function(e){var n=JSON.parse(e.data),t=n.language,r=n.code,a=n.immediateClose;u.postMessage(M.highlight(r,M.languages[t],t)),a&&u.close()},!1)),M;var e=M.util.currentScript();function t(){M.manual||M.highlightAll()}if(e&&(M.filename=e.src,e.hasAttribute("data-manual")&&(M.manual=!0)),!M.manual){var r=document.readyState;"loading"===r||"interactive"===r&&e&&e.defer?document.addEventListener("DOMContentLoaded",t):window.requestAnimationFrame?window.requestAnimationFrame(t):window.setTimeout(t,16)}return M}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(u){var c=/\blang(?:uage)?-([\w-]+)\b/i,n=0,_={manual:u.Prism&&u.Prism.manual,disableWorkerMessageHandler:u.Prism&&u.Prism.disableWorkerMessageHandler,util:{encode:function e(n){return n instanceof M?new M(n.type,e(n.content),n.alias):Array.isArray(n)?n.map(e):n.replace(/&/g,"&").replace(/=l.reach);y+=m.value.length,m=m.next){var k=m.value;if(t.length>n.length)return;if(!(k instanceof M)){var b,x=1;if(h){if(!(b=W(p,y,n,f)))break;var w=b.index,A=b.index+b[0].length,P=y;for(P+=m.value.length;P<=w;)m=m.next,P+=m.value.length;if(P-=m.value.length,y=P,m.value instanceof M)continue;for(var S=m;S!==t.tail&&(Pl.reach&&(l.reach=N);var j=m.prev;O&&(j=z(t,j,O),y+=O.length),I(t,j,x);var C=new M(o,g?_.tokenize(E,g):E,d,E);m=z(t,j,C),L&&z(t,m,L),1"+a.content+""},!u.document)return u.addEventListener&&(_.disableWorkerMessageHandler||u.addEventListener("message",function(e){var n=JSON.parse(e.data),t=n.language,r=n.code,a=n.immediateClose;u.postMessage(_.highlight(r,_.languages[t],t)),a&&u.close()},!1)),_;var e=_.util.currentScript();function t(){_.manual||_.highlightAll()}if(e&&(_.filename=e.src,e.hasAttribute("data-manual")&&(_.manual=!0)),!_.manual){var r=document.readyState;"loading"===r||"interactive"===r&&e&&e.defer?document.addEventListener("DOMContentLoaded",t):window.requestAnimationFrame?window.requestAnimationFrame(t):window.setTimeout(t,16)}return _}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file diff --git a/docs/global.html b/docs/global.html index 380162886a..4838a92ced 100644 --- a/docs/global.html +++ b/docs/global.html @@ -143,7 +143,7 @@

Grammar

Source:
@@ -274,7 +274,7 @@

GrammarToken

Source:
@@ -559,7 +559,7 @@

High
Source:
@@ -713,7 +713,7 @@

HookCallb
Source:
diff --git a/docs/prism-core.js.html b/docs/prism-core.js.html index f8a5edcd1e..ed5112a7fd 100644 --- a/docs/prism-core.js.html +++ b/docs/prism-core.js.html @@ -876,6 +876,25 @@

prism-core.js

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 @@ -908,7 +927,6 @@

prism-core.js

inside = patternObj.inside, lookbehind = !!patternObj.lookbehind, greedy = !!patternObj.greedy, - lookbehindLength = 0, alias = patternObj.alias; if (greedy && !patternObj.pattern.global) { @@ -942,15 +960,15 @@

prism-core.js

} 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; @@ -984,24 +1002,16 @@

prism-core.js

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) { diff --git a/prism.js b/prism.js index 02af74b4c8..cab8797b12 100644 --- a/prism.js +++ b/prism.js @@ -828,6 +828,25 @@ Token.stringify = function stringify(o, language) { return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + ''; }; +/** + * @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} tokenList @@ -860,7 +879,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) { @@ -894,15 +912,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; @@ -936,24 +954,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) {