@@ -823,6 +823,25 @@ Token.stringify = function stringify(o, language) {
823
823
return '<' + env . tag + ' class="' + env . classes . join ( ' ' ) + '"' + attributes + '>' + env . content + '</' + env . tag + '>' ;
824
824
} ;
825
825
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
+
826
845
/**
827
846
* @param {string } text
828
847
* @param {LinkedList<string | Token> } tokenList
@@ -855,7 +874,6 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
855
874
inside = patternObj . inside ,
856
875
lookbehind = ! ! patternObj . lookbehind ,
857
876
greedy = ! ! patternObj . greedy ,
858
- lookbehindLength = 0 ,
859
877
alias = patternObj . alias ;
860
878
861
879
if ( greedy && ! patternObj . pattern . global ) {
@@ -889,15 +907,15 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
889
907
}
890
908
891
909
var removeCount = 1 ; // this is the to parameter of removeBetween
910
+ var match ;
892
911
893
912
if ( greedy ) {
894
- pattern . lastIndex = pos ;
895
- var match = pattern . exec ( text ) ;
913
+ match = matchPattern ( pattern , pos , text , lookbehind ) ;
896
914
if ( ! match ) {
897
915
break ;
898
916
}
899
917
900
- var from = match . index + ( lookbehind && match [ 1 ] ? match [ 1 ] . length : 0 ) ;
918
+ var from = match . index ;
901
919
var to = match . index + match [ 0 ] . length ;
902
920
var p = pos ;
903
921
@@ -931,24 +949,16 @@ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
931
949
str = text . slice ( pos , p ) ;
932
950
match . index -= pos ;
933
951
} 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
+ }
945
956
}
946
957
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 ] ,
950
960
before = str . slice ( 0 , from ) ,
951
- after = str . slice ( to ) ;
961
+ after = str . slice ( from + matchStr . length ) ;
952
962
953
963
var reach = pos + str . length ;
954
964
if ( rematch && reach > rematch . reach ) {
0 commit comments