Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 0729daf

Browse files
author
mattpass
committed
Improving regex fail handling
1 parent 623ee10 commit 0729daf

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

assets/js/icecoder.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,10 +2820,16 @@ var ICEcoder = {
28202820

28212821
// Test an input for balanced regex brackets
28222822
regexIsBalanced: function(input) {
2823-
let brackets = "[]{}()";
2823+
const brackets = "[]{}()";
2824+
const bracketsSlash = brackets + "\\";
28242825
let stack = [];
28252826
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+
28272833
// Go thru each char in input
28282834
for (let char of input) {
28292835
// Find index of this char in brackets string
@@ -2845,6 +2851,12 @@ var ICEcoder = {
28452851
}
28462852
}
28472853
}
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+
28482860
// If we have items left on our stack, we have an unbalanced input
28492861
return {
28502862
"balanced" : stack.length === 0,
@@ -2881,17 +2893,24 @@ var ICEcoder = {
28812893

28822894
// Find & replace text according to user selections
28832895
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;
28852897

28862898
// Get our replace value and results display
28872899
replace = get('replace').value;
28882900
results = get('results');
28892901

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
28922903
if (true === parent.ICEcoder.findRegex) {
28932904
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+
)) {
28952914
results.innerHTML = "No results";
28962915
this.content.contentWindow.document.getElementById('resultsBar').innerHTML = "";
28972916
this.content.contentWindow.document.getElementById('resultsBar').style.display = "none";
@@ -2902,17 +2921,21 @@ var ICEcoder = {
29022921
}
29032922
}
29042923

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+
}
29072930

29082931
// Get CM pane
29092932
thisCM = this.getThisCM();
29102933

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-
29142934
// Finding in this document only
29152935
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+
29162939
// Replacing?
29172940
if (t['and'] === document.findAndReplace.connector.value && true === canActionChanges) {
29182941
// Find & replace the next instance, or all?

0 commit comments

Comments
 (0)