!function(e,t){"object"==typeof module&&module.exports?module.exports=t():"function"==typeof define&&define.amd?define(t):e.findAndReplaceDOMText=t()}(this,function(){function e(){return function(n,r,i,o,d){if(r&&!r.nodeType&&arguments.length<=2)return!1;var a="function"==typeof i;a&&(i=function(e){return function(t,n){return e(t.text,n.startIndex)}}(i));var s=t(r,{find:n,wrap:a?null:i,replace:a?i:"$"+(o||"&"),prepMatch:function(e,t){if(!e[0])throw"findAndReplaceDOMText cannot handle zero-length matches";if(o>0){var n=e[o];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 e.revert=function(){return s.revert()},!0}.apply(null,arguments)||t.apply(null,arguments)}function t(e,t){return new n(e,t)}function n(t,n){var i=n.preset&&e.PRESETS[n.preset];if(n.portionMode=n.portionMode||r,i)for(var d in i)o.call(i,d)&&!o.call(n,d)&&(n[d]=i[d]);this.node=t,this.options=n,this.prepMatch=n.prepMatch||this.prepMatch,this.reverts=[],this.matches=this.search(),this.matches.length&&this.processMatches()}var r="retain",i=document,o={}.hasOwnProperty;return e.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},e.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},e.NON_INLINE_PROSE=function(t){return o.call(e.NON_CONTIGUOUS_PROSE_ELEMENTS,t.nodeName.toLowerCase())},e.PRESETS={prose:{forceContext:e.NON_INLINE_PROSE,filterElements:function(t){return!o.call(e.NON_PROSE_ELEMENTS,t.nodeName.toLowerCase())}}},e.Finder=n,n.prototype={search:function(){function e(o){for(var s=0,p=o.length;s
=s.endIndex?t={node:a,index:l++,text:a.data.substring(s.startIndex-p,s.endIndex-p),indexInMatch:0===p?0:p-s.startIndex,indexInNode:s.startIndex-p,endIndexInNode:s.endIndex-p,isEnd:!0}:e&&d.push({node:a,index:l++,text:a.data,indexInMatch:p-s.startIndex,indexInNode:0}),!e&&a.length+p>s.startIndex&&(e={node:a,index:l++,indexInMatch:0,indexInNode:s.startIndex-p,endIndexInNode:s.endIndex-p,text:a.data.substring(s.startIndex-p,s.endIndex-p)}),p+=a.data.length),n=a.nodeType===Node.ELEMENT_NODE&&o&&!o(a),e&&t){if(a=this.replaceMatch(s,e,d,t),p-=t.node.data.length-t.endIndexInNode,e=null,t=null,d=[],s=r.shift(),l=0,h++,!s)break}else if(!n&&(a.firstChild||a.nextSibling)){a.firstChild?(c.push(a),a=a.firstChild):a=a.nextSibling;continue}for(;;){if(a.nextSibling){a=a.nextSibling;break}if((a=c.pop())===i)break e}}},revert:function(){for(var e=this.reverts.length;e--;)this.reverts[e]();this.reverts=[]},prepareReplacementString:function(e,t,n){var r=this.options.portionMode;return"first"===r&&t.indexInMatch>0?"":(e=e.replace(/\$(\d+|&|`|')/g,function(e,t){var r;switch(t){case"&":r=n[0];break;case"`":r=n.input.substring(0,n.startIndex);break;case"'":r=n.input.substring(n.endIndex);break;default:r=n[+t]||""}return r}),"first"===r?e:t.isEnd?e.substring(t.indexInMatch):e.substring(t.indexInMatch,t.indexInMatch+t.text.length))},getPortionReplacementNode:function(e,t){var n=this.options.replace||"$&",r=this.options.wrap,o=this.options.wrapClass;if(r&&r.nodeType){var d=i.createElement("div");d.innerHTML=r.outerHTML||(new XMLSerializer).serializeToString(r),r=d.firstChild}if("function"==typeof n)return(n=n(e,t))&&n.nodeType?n:i.createTextNode(String(n));var a="string"==typeof r?i.createElement(r):r;return a&&o&&(a.className=o),(n=i.createTextNode(this.prepareReplacementString(n,e,t))).data&&a?(a.appendChild(n),a):n},replaceMatch:function(e,t,n,r){var o,d,a=t.node,s=r.node;if(a===s){var p=a;t.indexInNode>0&&(o=i.createTextNode(p.data.substring(0,t.indexInNode)),p.parentNode.insertBefore(o,p));var h=this.getPortionReplacementNode(r,e);return p.parentNode.insertBefore(h,p),r.endIndexInNode