@@ -2820,10 +2820,16 @@ var ICEcoder = {
2820
2820
2821
2821
// Test an input for balanced regex brackets
2822
2822
regexIsBalanced : function ( input ) {
2823
- let brackets = "[]{}()" ;
2823
+ const brackets = "[]{}()" ;
2824
+ const bracketsSlash = brackets + "\\" ;
2824
2825
let stack = [ ] ;
2825
2826
let remainder = "" ;
2826
-
2827
+
2828
+ // Remove backslash escaped brackets & slashes before testing
2829
+ for ( let i = 0 ; i < bracketsSlash . length ; i ++ ) {
2830
+ input = input . replace ( new RegExp ( '\\\\\\' + bracketsSlash [ i ] , 'g' ) , 'ICECODERC' + i )
2831
+ }
2832
+
2827
2833
// Go thru each char in input
2828
2834
for ( let char of input ) {
2829
2835
// Find index of this char in brackets string
@@ -2845,6 +2851,12 @@ var ICEcoder = {
2845
2851
}
2846
2852
}
2847
2853
}
2854
+
2855
+ // Replace backslash escaped brackets & slashes
2856
+ for ( let i = 0 ; i < bracketsSlash . length ; i ++ ) {
2857
+ remainder = remainder . replace ( new RegExp ( 'ICECODERC' + i , 'g' ) , '\\' + bracketsSlash [ i ] )
2858
+ }
2859
+
2848
2860
// If we have items left on our stack, we have an unbalanced input
2849
2861
return {
2850
2862
"balanced" : stack . length === 0 ,
@@ -2881,17 +2893,24 @@ var ICEcoder = {
2881
2893
2882
2894
// Find & replace text according to user selections
2883
2895
findReplace : function ( find , selectNext , canActionChanges , findPrevious ) {
2884
- let replace , results , thisCM , thisSelection , rBlocks , rExpMatch0String , replaceQS , targetQS , filesQS ;
2896
+ let replace , results , rExp , thisCM , thisSelection , rBlocks , rExpMatch0String , replaceQS , targetQS , filesQS ;
2885
2897
2886
2898
// Get our replace value and results display
2887
2899
replace = get ( 'replace' ) . value ;
2888
2900
results = get ( 'results' ) ;
2889
2901
2890
- // If we're finding with regex and have unbalanced bracket pairs, or nothing but brackets, or only have ^ or $ or .*
2891
- // highlight find box red and return, avoids CPU crash
2902
+ // If we're finding with regex and have a problem with it, highlight find box red and return, avoids CPU crash
2892
2903
if ( true === parent . ICEcoder . findRegex ) {
2893
2904
const balancedInfo = this . regexIsBalanced ( find ) ;
2894
- if ( false === balancedInfo [ 'balanced' ] || "" === balancedInfo [ 'remainder' ] . replace ( / \[ \] \{ \} \( \) / g, "" ) || "" === find . replace ( / \^ | \$ | \. \* / g, "" ) ) {
2905
+
2906
+ // Turn input box red if empty, or no balancedInfo data, or we have unbalanced bracket pairs, or the remainder
2907
+ // is empty aside from brackets, or find or only has ^ or $ or .*
2908
+ if ( "" !== find && (
2909
+ false === balancedInfo ||
2910
+ false === balancedInfo [ 'balanced' ] ||
2911
+ ( "" === balancedInfo [ 'remainder' ] . replace ( / \[ \] \{ \} \( \) / g, "" ) ) ||
2912
+ "" === find . replace ( / \^ | \$ | \. \* / g, "" )
2913
+ ) ) {
2895
2914
results . innerHTML = "No results" ;
2896
2915
this . content . contentWindow . document . getElementById ( 'resultsBar' ) . innerHTML = "" ;
2897
2916
this . content . contentWindow . document . getElementById ( 'resultsBar' ) . style . display = "none" ;
@@ -2902,17 +2921,21 @@ var ICEcoder = {
2902
2921
}
2903
2922
}
2904
2923
2905
- // Determine our find rExp
2906
- const rExp = new RegExp ( true === parent . ICEcoder . findRegex ? find : ICEcoder . escapeRegex ( find ) , "gi" ) ;
2924
+ // Determine our find rExp, inside a try/catch incase there's an uncaught issue with format
2925
+ try {
2926
+ rExp = new RegExp ( true === parent . ICEcoder . findRegex ? find : ICEcoder . escapeRegex ( find ) , "gi" ) ;
2927
+ } catch ( e ) {
2928
+ return false ;
2929
+ }
2907
2930
2908
2931
// Get CM pane
2909
2932
thisCM = this . getThisCM ( ) ;
2910
2933
2911
- // Get this selection, either as regex escaped version or regular, for comparisons
2912
- thisSelection = true === parent . ICEcoder . findRegex ? ICEcoder . escapeRegex ( thisCM . getSelection ( ) ) : thisCM . getSelection ( ) ;
2913
-
2914
2934
// Finding in this document only
2915
2935
if ( thisCM && 0 < find . length && t [ 'this document' ] === document . findAndReplace . target . value ) {
2936
+ // Get this selection, either as regex escaped version or regular, for comparisons
2937
+ thisSelection = true === parent . ICEcoder . findRegex ? ICEcoder . escapeRegex ( thisCM . getSelection ( ) ) : thisCM . getSelection ( ) ;
2938
+
2916
2939
// Replacing?
2917
2940
if ( t [ 'and' ] === document . findAndReplace . connector . value && true === canActionChanges ) {
2918
2941
// Find & replace the next instance, or all?
0 commit comments