diff --git a/dist/find-in-nw.js b/dist/find-in-nw.js index 4dbc450..c99c9e9 100644 --- a/dist/find-in-nw.js +++ b/dist/find-in-nw.js @@ -7,4 +7,4 @@ * and replaces each match (or node-separated portions of the match) * in the specified element. */ -!function(e,t){"object"==typeof module&&module.exports?module.exports=t():"function"==typeof define&&define.amd?define(t):e.findAndReplaceDOMText=t()}(this,(function(){var e=document,t={}.hasOwnProperty;function n(){return i.apply(null,arguments)||o.apply(null,arguments)}function i(e,t,i,r,d){if(t&&!t.nodeType&&arguments.length<=2)return!1;var s,a="function"==typeof i;a&&(s=i,i=function(e,t){return s(e.text,t.startIndex)});var c=o(t,{find:e,wrap:a?null:i,replace:a?i:"$"+(r||"&"),prepMatch:function(e,t){if(!e[0])throw"findAndReplaceDOMText cannot handle zero-length matches";if(r>0){var n=e[r];e.index+=e[0].indexOf(n),e[0]=n}return e.endIndex=e.index+e[0].length,e.startIndex=e.index,e.index=t,e},filterElements:d});return n.revert=function(){return c.revert()},!0}function o(e,t){return new r(e,t)}function r(e,i){var o=i.preset&&n.PRESETS[i.preset];if(i.portionMode=i.portionMode||"retain",o)for(var r in o)t.call(o,r)&&!t.call(i,r)&&(i[r]=o[r]);this.node=e,this.options=i,this.prepMatch=i.prepMatch||this.prepMatch,this.reverts=[],this.matches=this.search(),this.matches.length&&this.processMatches()}return n.NON_PROSE_ELEMENTS={br:1,hr:1,script:1,style:1,img:1,video:1,audio:1,canvas:1,svg:1,map:1,object:1,input:1,textarea:1,select:1,option:1,optgroup:1,button:1},n.NON_CONTIGUOUS_PROSE_ELEMENTS={address:1,article:1,aside:1,blockquote:1,dd:1,div:1,dl:1,fieldset:1,figcaption:1,figure:1,footer:1,form:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,header:1,hgroup:1,hr:1,main:1,nav:1,noscript:1,ol:1,output:1,p:1,pre:1,section:1,ul:1,br:1,li:1,summary:1,dt:1,details:1,rp:1,rt:1,rtc:1,script:1,style:1,img:1,video:1,audio:1,canvas:1,svg:1,map:1,object:1,input:1,textarea:1,select:1,option:1,optgroup:1,button:1,table:1,tbody:1,thead:1,th:1,tr:1,td:1,caption:1,col:1,tfoot:1,colgroup:1},n.NON_INLINE_PROSE=function(e){return t.call(n.NON_CONTIGUOUS_PROSE_ELEMENTS,e.nodeName.toLowerCase())},n.PRESETS={prose:{forceContext:n.NON_INLINE_PROSE,filterElements:function(e){return!t.call(n.NON_PROSE_ELEMENTS,e.nodeName.toLowerCase())}}},n.Finder=r,r.prototype={search:function(){var e,t=0,n=0,i=this.options.find,o=this.getAggregateText(),r=[],d=this;return i="string"==typeof i?RegExp(String(i).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1"),"g"):i,function o(s){for(var a=0,c=s.length;a=a.endIndex?t={node:s,index:h++,text:s.data.substring(a.startIndex-c,a.endIndex-c),indexInMatch:0===c?0:c-a.startIndex,indexInNode:a.startIndex-c,endIndexInNode:a.endIndex-c,isEnd:!0}:e&&d.push({node:s,index:h++,text:s.data,indexInMatch:c-a.startIndex,indexInNode:0}),!e&&s.length+c>a.startIndex&&(e={node:s,index:h++,indexInMatch:0,indexInNode:a.startIndex-c,endIndexInNode:a.endIndex-c,text:s.data.substring(a.startIndex-c,a.endIndex-c)}),c+=s.data.length),n=s.nodeType===Node.ELEMENT_NODE&&r&&!r(s),e&&t){if(s=this.replaceMatch(a,e,d,t),c-=t.node.data.length-t.endIndexInNode,e=null,t=null,d=[],h=0,!(a=i.shift()))break}else if(!n&&(s.firstChild||s.nextSibling)){s.firstChild?(l.push(s),s=s.firstChild):s=s.nextSibling;continue}for(;;){if(s.nextSibling){s=s.nextSibling;break}if((s=l.pop())===o)break e}}},revert:function(){for(var e=this.reverts.length;e--;)this.reverts[e]();this.reverts=[]},prepareReplacementString:function(e,t,n){var i=this.options.portionMode;return"first"===i&&t.indexInMatch>0?"":(e=e.replace(/\$(\d+|&|`|')/g,(function(e,t){var i;switch(t){case"&":i=n[0];break;case"`":i=n.input.substring(0,n.startIndex);break;case"'":i=n.input.substring(n.endIndex);break;default:i=n[+t]||""}return i})),"first"===i?e:t.isEnd?e.substring(t.indexInMatch):e.substring(t.indexInMatch,t.indexInMatch+t.text.length))},getPortionReplacementNode:function(t,n){var i=this.options.replace||"$&",o=this.options.wrap,r=this.options.wrapClass;if(o&&o.nodeType){var d=e.createElement("div");d.innerHTML=o.outerHTML||(new XMLSerializer).serializeToString(o),o=d.firstChild}if("function"==typeof i)return(i=i(t,n))&&i.nodeType?i:e.createTextNode(String(i));var s="string"==typeof o?e.createElement(o):o;return s&&r&&(s.className=r),(i=e.createTextNode(this.prepareReplacementString(i,t,n))).data&&s?(s.appendChild(i),s):i},replaceMatch:function(t,n,i,o){var r,d,s=n.node,a=o.node;if(s===a){var c=s;n.indexInNode>0&&(r=e.createTextNode(c.data.substring(0,n.indexInNode)),c.parentNode.insertBefore(r,c));var h=this.getPortionReplacementNode(o,t);return c.parentNode.insertBefore(h,c),o.endIndexInNodethis.total-1&&(this.currentToken=0),this.updateCount(),this.highlightCurrentToken()},showSearchBox:function(){const e=document.getElementById("find-in-nw-search-box"),t=document.getElementById("find-in-nw-input");e.classList.add("find-in-nw-search-box-visible");const n=window.getSelection().toString();n&&n.indexOf("\n")<0&&(t.value=n),t.value&&this.search(t.value),t.focus()},hideSearchBox:function(){document.getElementById("find-in-nw-search-box").classList.remove("find-in-nw-search-box-visible"),document.body.focus(),this.clearTokens()},getElementsToSearch:function(){const e=[];for(let t=0;t0){var n=e[r];e.index+=e[0].indexOf(n),e[0]=n}return e.endIndex=e.index+e[0].length,e.startIndex=e.index,e.index=t,e},filterElements:d});return n.revert=function(){return c.revert()},!0}function o(e,t){return new r(e,t)}function r(e,i){var o=i.preset&&n.PRESETS[i.preset];if(i.portionMode=i.portionMode||"retain",o)for(var r in o)t.call(o,r)&&!t.call(i,r)&&(i[r]=o[r]);this.node=e,this.options=i,this.prepMatch=i.prepMatch||this.prepMatch,this.reverts=[],this.matches=this.search(),this.matches.length&&this.processMatches()}return n.NON_PROSE_ELEMENTS={br:1,hr:1,script:1,style:1,img:1,video:1,audio:1,canvas:1,svg:1,map:1,object:1,input:1,textarea:1,select:1,option:1,optgroup:1,button:1},n.NON_CONTIGUOUS_PROSE_ELEMENTS={address:1,article:1,aside:1,blockquote:1,dd:1,div:1,dl:1,fieldset:1,figcaption:1,figure:1,footer:1,form:1,h1:1,h2:1,h3:1,h4:1,h5:1,h6:1,header:1,hgroup:1,hr:1,main:1,nav:1,noscript:1,ol:1,output:1,p:1,pre:1,section:1,ul:1,br:1,li:1,summary:1,dt:1,details:1,rp:1,rt:1,rtc:1,script:1,style:1,img:1,video:1,audio:1,canvas:1,svg:1,map:1,object:1,input:1,textarea:1,select:1,option:1,optgroup:1,button:1,table:1,tbody:1,thead:1,th:1,tr:1,td:1,caption:1,col:1,tfoot:1,colgroup:1},n.NON_INLINE_PROSE=function(e){return t.call(n.NON_CONTIGUOUS_PROSE_ELEMENTS,e.nodeName.toLowerCase())},n.PRESETS={prose:{forceContext:n.NON_INLINE_PROSE,filterElements:function(e){return!t.call(n.NON_PROSE_ELEMENTS,e.nodeName.toLowerCase())}}},n.Finder=r,r.prototype={search:function(){var e,t=0,n=0,i=this.options.find,o=this.getAggregateText(),r=[],d=this;return i="string"==typeof i?RegExp(String(i).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1"),"g"):i,function o(s){for(var a=0,c=s.length;a=a.endIndex?t={node:s,index:h++,text:s.data.substring(a.startIndex-c,a.endIndex-c),indexInMatch:0===c?0:c-a.startIndex,indexInNode:a.startIndex-c,endIndexInNode:a.endIndex-c,isEnd:!0}:e&&d.push({node:s,index:h++,text:s.data,indexInMatch:c-a.startIndex,indexInNode:0}),!e&&s.length+c>a.startIndex&&(e={node:s,index:h++,indexInMatch:0,indexInNode:a.startIndex-c,endIndexInNode:a.endIndex-c,text:s.data.substring(a.startIndex-c,a.endIndex-c)}),c+=s.data.length),n=s.nodeType===Node.ELEMENT_NODE&&r&&!r(s),e&&t){if(s=this.replaceMatch(a,e,d,t),c-=t.node.data.length-t.endIndexInNode,e=null,t=null,d=[],h=0,!(a=i.shift()))break}else if(!n&&(s.firstChild||s.nextSibling)){s.firstChild?(u.push(s),s=s.firstChild):s=s.nextSibling;continue}for(;;){if(s.nextSibling){s=s.nextSibling;break}if((s=u.pop())===o)break e}}},revert:function(){for(var e=this.reverts.length;e--;)this.reverts[e]();this.reverts=[]},prepareReplacementString:function(e,t,n){var i=this.options.portionMode;return"first"===i&&t.indexInMatch>0?"":(e=e.replace(/\$(\d+|&|`|')/g,(function(e,t){var i;switch(t){case"&":i=n[0];break;case"`":i=n.input.substring(0,n.startIndex);break;case"'":i=n.input.substring(n.endIndex);break;default:i=n[+t]||""}return i})),"first"===i?e:t.isEnd?e.substring(t.indexInMatch):e.substring(t.indexInMatch,t.indexInMatch+t.text.length))},getPortionReplacementNode:function(t,n){var i=this.options.replace||"$&",o=this.options.wrap,r=this.options.wrapClass;if(o&&o.nodeType){var d=e.createElement("div");d.innerHTML=o.outerHTML||(new XMLSerializer).serializeToString(o),o=d.firstChild}if("function"==typeof i)return(i=i(t,n))&&i.nodeType?i:e.createTextNode(String(i));var s="string"==typeof o?e.createElement(o):o;return s&&r&&(s.className=r),(i=e.createTextNode(this.prepareReplacementString(i,t,n))).data&&s?(s.appendChild(i),s):i},replaceMatch:function(t,n,i,o){var r,d,s=n.node,a=o.node;if(s===a){var c=s;n.indexInNode>0&&(r=e.createTextNode(c.data.substring(0,n.indexInNode)),c.parentNode.insertBefore(r,c));var h=this.getPortionReplacementNode(o,t);return c.parentNode.insertBefore(h,c),o.endIndexInNode=0&&t.left>=0&&t.bottom<=(window.innerHeight||document.documentElement.clientHeight)&&t.right<=(window.innerWidth||document.documentElement.clientWidth)},t=document.getElementsByClassName("find-in-nw-token");for(const n of t)if(e(n)){this.currentToken=parseInt(n.getAttribute(this.dataAttribute))||0;break}},highlightPrevious:function(){this.currentToken=this.currentToken-1,this.currentToken<0&&(this.currentToken=this.total-1),this.updateCount(),this.highlightCurrentToken()},highlightNext:function(){this.currentToken=this.currentToken+1,this.currentToken>this.total-1&&(this.currentToken=0),this.updateCount(),this.highlightCurrentToken()},showSearchBox:function(){const e=document.getElementById("find-in-nw-search-box"),t=document.getElementById("find-in-nw-input");e.classList.add("find-in-nw-search-box-visible");const n=window.getSelection().toString();n&&n.indexOf("\n")<0&&(t.value=n),t.value&&this.search(t.value),t.focus()},hideSearchBox:function(){document.getElementById("find-in-nw-search-box").classList.remove("find-in-nw-search-box-visible"),document.body.focus(),this.clearTokens()},getElementsToSearch:function(){const e=[];for(let t=0;t