From 2d97f55b16561e93a0b07ff78f571970d0c385aa Mon Sep 17 00:00:00 2001 From: craftxbox Date: Mon, 4 Nov 2019 00:30:09 -0330 Subject: [PATCH] my hatred for ie is immesurable --- goon/browserassets/js/browserOutput.js | 64 +++++++++++++++++++++----- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/goon/browserassets/js/browserOutput.js b/goon/browserassets/js/browserOutput.js index 094e8258562..b94734040bb 100644 --- a/goon/browserassets/js/browserOutput.js +++ b/goon/browserassets/js/browserOutput.js @@ -171,25 +171,67 @@ function setHighlightColor(match) { match.style.background = opts.highlightColor } +function escapeRegex(input){ // put this in a function incase it ever needs to be used elsewhere, makes code cleaner + return input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + //Highlights words based on user settings function highlightTerms(el) { if(regexHasError) return; //just stop right there ig the regex is gonna except + function highlightRecursor(element,term){ //recursor function to do the highlighting proper + var regex = new RegExp("("+term+")","gi") + function replace(str) { + return str.replace(regex,'$&') + } + var s='' + var work=element.innerHTML; + var work_lc=work.toLowerCase() + var ind=0; + while(ind < work_lc.length) { + console.log(s); + var next_term=work_lc.substring(ind).search(regex); + if(next_term!=-1)next_term+=ind; + var next_tag =work_lc.indexOf('<',ind) + if(next_tag == -1) { + s+=replace(work.substring(ind)); + break; + } + else if(next_term==-1) { + s+=work.substring(ind) + break; + } + else if(next_tag < next_term) { + var temp=work_lc.indexOf('>',next_tag) + s+=work.substring(ind,temp+1); + ind=temp+1; + } + else { + s+=replace(work.substring(ind,next_tag)); + ind=next_tag + } + } + + element.innerHTML=s; + } + for (var i = 0; i < opts.highlightTerms.length; i++) { //Each highlight term if(opts.highlightTerms[i]) { if(!opts.highlightRegexEnable){ - if(el.innerText.toString().toLowerCase().includes(opts.highlightTerms[i].toLowerCase())) //match normally - el.innerHTML = ''+el.innerHTML+'' //encloseincludes - continue; + var innerTerms = opts.highlightTerms[i].split(" ") + for(var a in innerTerms){ + highlightRecursor(el,escapeRegex(innerTerms[a])) + } } - var rexp; - try{ - rexp = new RegExp(opts.highlightTerms[i],"gmi") - } catch(e){ - el.innerHTML+='
Your highlight regex - '+opts.highlightTerms[i]+' - is malformed. Thrown exception: '+e+'' - regexHasError = true; - return; + else { + try{ + new RegExp(opts.highlightTerms[i],"gmi") // check to make sure the pattern wont cause issues + } catch(e){ + el.innerHTML+='
Your highlight regex pattern - '+opts.highlightTerms[i]+' - is malformed.
Your highlights have been disabled until they are next edited
Thrown exception: '+e+'
' + regexHasError = true; + return; + } + highlightRecursor(el,opts.highlightTerms[i]) } - el.innerHTML = el.innerHTML.replace(rexp,"$0") //i cant figure out a proper, non snowflakey way to let people select the group that gets highlighted } } }