UNPKG

zx-editor

Version:

ZxEditor is a HTML5 rich text editor

1 lines 12.8 kB
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n,r=arguments[t];for(n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},_createClass=function(){function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}}(),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(t,n,r){"function"==typeof define&&define.amd?define(["jquery"],function(e){return t(n,r,e)}):"object"===("undefined"==typeof module?"undefined":_typeof(module))&&module.exports?module.exports=t(n,r,require("jquery")):t(n,r,jQuery)}(function(t,o,e){var n=(_createClass(r,[{key:"log",value:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"debug",n=this.opt.log;this.opt.debug&&"object"===(void 0===n?"undefined":_typeof(n))&&"function"==typeof n[t]&&n[t]("mark.js: "+e)}},{key:"escapeStr",value:function(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}},{key:"createRegExp",value:function(e){return e=this.escapeStr(e),Object.keys(this.opt.synonyms).length&&(e=this.createSynonymsRegExp(e)),this.opt.ignoreJoiners&&(e=this.setupIgnoreJoinersRegExp(e)),this.opt.diacritics&&(e=this.createDiacriticsRegExp(e)),e=this.createMergedBlanksRegExp(e),this.opt.ignoreJoiners&&(e=this.createIgnoreJoinersRegExp(e)),this.createAccuracyRegExp(e)}},{key:"createSynonymsRegExp",value:function(e){var t,n,r,o=this.opt.synonyms,i=this.opt.caseSensitive?"":"i";for(t in o)o.hasOwnProperty(t)&&(r=o[t],n=this.escapeStr(t),r=this.escapeStr(r),e=e.replace(new RegExp("("+n+"|"+r+")","gm"+i),"("+n+"|"+r+")"));return e}},{key:"setupIgnoreJoinersRegExp",value:function(e){return e.replace(/[^(|)\\]/g,function(e,t,n){t=n.charAt(t+1);return/[(|)\\]/.test(t)||""===t?e:e+"\0"})}},{key:"createIgnoreJoinersRegExp",value:function(e){return e.split("\0").join("[\\u00ad|\\u200b|\\u200c|\\u200d]?")}},{key:"createDiacriticsRegExp",value:function(n){var r=this.opt.caseSensitive?"":"i",e=this.opt.caseSensitive?["aàáâãäåāąă","AÀÁÂÃÄÅĀĄĂ","cçćč","CÇĆČ","dđď","DĐĎ","eèéêëěēę","EÈÉÊËĚĒĘ","iìíîïī","IÌÍÎÏĪ","lł","LŁ","nñňń","NÑŇŃ","oòóôõöøō","OÒÓÔÕÖØŌ","rř","RŘ","sšśșş","SŠŚȘŞ","tťțţ","TŤȚŢ","uùúûüůū","UÙÚÛÜŮŪ","yÿý","YŸÝ","zžżź","ZŽŻŹ"]:["aÀÁÂÃÄÅàáâãäåĀāąĄăĂ","cÇçćĆčČ","dđĐďĎ","eÈÉÊËèéêëěĚĒēęĘ","iÌÍÎÏìíîïĪī","lłŁ","nÑñňŇńŃ","oÒÓÔÕÖØòóôõöøŌō","rřŘ","sŠšśŚșȘşŞ","tťŤțȚţŢ","uÙÚÛÜùúûüůŮŪū","yŸÿýÝ","zŽžżŻźŹ"],o=[];return n.split("").forEach(function(t){e.every(function(e){if(-1!==e.indexOf(t)){if(-1<o.indexOf(e))return!1;n=n.replace(new RegExp("["+e+"]","gm"+r),"["+e+"]"),o.push(e)}return!0})}),n}},{key:"createMergedBlanksRegExp",value:function(e){return e.replace(/[\s]+/gim,"[\\s]+")}},{key:"createAccuracyRegExp",value:function(e){var t=this,n=this.opt.accuracy,r="string"==typeof n?n:n.value,n="string"==typeof n?[]:n.limiters,o="";switch(n.forEach(function(e){o+="|"+t.escapeStr(e)}),r){case"partially":default:return"()("+e+")";case"complementary":return"()([^\\s"+o+"]*"+e+"[^\\s"+o+"]*)";case"exactly":return"(^|\\s"+o+")("+e+")(?=$|\\s"+o+")"}}},{key:"getSeparatedKeywords",value:function(e){var t=this,n=[];return e.forEach(function(e){t.opt.separateWordSearch?e.split(" ").forEach(function(e){e.trim()&&-1===n.indexOf(e)&&n.push(e)}):e.trim()&&-1===n.indexOf(e)&&n.push(e)}),{keywords:n.sort(function(e,t){return t.length-e.length}),length:n.length}}},{key:"getTextNodes",value:function(e){var t=this,n="",r=[];this.iterator.forEachNode(NodeFilter.SHOW_TEXT,function(e){r.push({start:n.length,end:(n+=e.textContent).length,node:e})},function(e){return t.matchesExclude(e.parentNode)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT},function(){e({value:n,nodes:r})})}},{key:"matchesExclude",value:function(e){return i.matches(e,this.opt.exclude.concat(["script","style","title","head","html"]))}},{key:"wrapRangeInTextNode",value:function(e,t,n){var r=this.opt.element||"mark",e=e.splitText(t),t=e.splitText(n-t),r=o.createElement(r);return r.setAttribute("data-markjs","true"),this.opt.className&&r.setAttribute("class",this.opt.className),r.textContent=e.textContent,e.parentNode.replaceChild(r,e),t}},{key:"wrapRangeInMappedTextNode",value:function(a,s,c,u,l){var f=this;a.nodes.every(function(o,i){var e=a.nodes[i+1];if(void 0===e||e.start>s){e=function(){if(!u(o.node))return{v:!1};var e=s-o.start,n=(c>o.end?o.end:c)-o.start,t=a.value.substr(0,o.start),r=a.value.substr(n+o.start);return o.node=f.wrapRangeInTextNode(o.node,e,n),a.value=t+r,a.nodes.forEach(function(e,t){i<=t&&(0<a.nodes[t].start&&t!==i&&(a.nodes[t].start-=n),a.nodes[t].end-=n)}),c-=n,l(o.node.previousSibling,o.start),c>o.end?void(s=o.end):{v:!1}}();if("object"===(void 0===e?"undefined":_typeof(e)))return e.v}return!0})}},{key:"wrapMatches",value:function(o,e,i,a,t){var s=this,c=0===e?0:e+1;this.getTextNodes(function(e){e.nodes.forEach(function(e){e=e.node;for(var t;null!==(t=o.exec(e.textContent))&&""!==t[c];)if(i(t[c],e)){var n=t.index;if(0!==c)for(var r=1;r<c;r++)n+=t[r].length;e=s.wrapRangeInTextNode(e,n,n+t[c].length),a(e.previousSibling),o.lastIndex=0}}),t()})}},{key:"wrapMatchesAcrossElements",value:function(i,e,a,s,c){var u=this,l=0===e?0:e+1;this.getTextNodes(function(e){for(var t;null!==(t=i.exec(e.value))&&""!==t[l];){var n=t.index;if(0!==l)for(var r=1;r<l;r++)n+=t[r].length;var o=n+t[l].length;u.wrapRangeInMappedTextNode(e,n,o,function(e){return a(t[l],e)},function(e,t){i.lastIndex=t,s(e)})}c()})}},{key:"unwrapMatches",value:function(e){for(var t=e.parentNode,n=o.createDocumentFragment();e.firstChild;)n.appendChild(e.removeChild(e.firstChild));t.replaceChild(n,e),this.ie?this.normalizeTextNode(t):t.normalize()}},{key:"normalizeTextNode",value:function(e){if(e){if(3===e.nodeType)for(;e.nextSibling&&3===e.nextSibling.nodeType;)e.nodeValue+=e.nextSibling.nodeValue,e.parentNode.removeChild(e.nextSibling);else this.normalizeTextNode(e.firstChild);this.normalizeTextNode(e.nextSibling)}}},{key:"markRegExp",value:function(e,t){var n=this;this.opt=t,this.log('Searching with expression "'+e+'"');var r=0,t="wrapMatches";this.opt.acrossElements&&(t="wrapMatchesAcrossElements"),this[t](e,this.opt.ignoreGroups,function(e,t){return n.opt.filter(t,e,r)},function(e){r++,n.opt.each(e)},function(){0===r&&n.opt.noMatch(e),n.opt.done(r)})}},{key:"mark",value:function(e,t){var o=this;this.opt=t;var i=0,a="wrapMatches",e=this.getSeparatedKeywords("string"==typeof e?[e]:e),s=e.keywords,c=e.length,u=this.opt.caseSensitive?"":"i",e=function e(n){var t=new RegExp(o.createRegExp(n),"gm"+u),r=0;o.log('Searching with expression "'+t+'"'),o[a](t,1,function(e,t){return o.opt.filter(t,n,i,r)},function(e){r++,i++,o.opt.each(e)},function(){0===r&&o.opt.noMatch(n),s[c-1]===n?o.opt.done(i):e(s[s.indexOf(n)+1])})};this.opt.acrossElements&&(a="wrapMatchesAcrossElements"),0===c?this.opt.done(i):e(s[0])}},{key:"unmark",value:function(e){var n=this;this.opt=e;var r=this.opt.element||"*";r+="[data-markjs]",this.opt.className&&(r+="."+this.opt.className),this.log('Removal selector "'+r+'"'),this.iterator.forEachNode(NodeFilter.SHOW_ELEMENT,function(e){n.unwrapMatches(e)},function(e){var t=i.matches(e,r),e=n.matchesExclude(e);return!t||e?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT},this.opt.done)}},{key:"opt",set:function(e){this._opt=_extends({},{element:"",className:"",exclude:[],iframes:!1,iframesTimeout:5e3,separateWordSearch:!0,diacritics:!0,synonyms:{},accuracy:"partially",acrossElements:!1,caseSensitive:!1,ignoreJoiners:!1,ignoreGroups:0,each:function(){},noMatch:function(){},filter:function(){return!0},done:function(){},debug:!1,log:t.console},e)},get:function(){return this._opt}},{key:"iterator",get:function(){return this._iterator||(this._iterator=new i(this.ctx,this.opt.iframes,this.opt.exclude,this.opt.iframesTimeout)),this._iterator}}]),r),i=(_createClass(c,[{key:"getContexts",value:function(){var n=[];return(void 0!==this.ctx&&this.ctx?NodeList.prototype.isPrototypeOf(this.ctx)?Array.prototype.slice.call(this.ctx):Array.isArray(this.ctx)?this.ctx:"string"==typeof this.ctx?Array.prototype.slice.call(o.querySelectorAll(this.ctx)):[this.ctx]:[]).forEach(function(t){var e=0<n.filter(function(e){return e.contains(t)}).length;-1!==n.indexOf(t)||e||n.push(t)}),n}},{key:"getIframeContents",value:function(e,t){var n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:function(){},r=void 0;try{var o=e.contentWindow,r=o.document;if(!o||!r)throw new Error("iframe inaccessible")}catch(e){n()}r&&t(r)}},{key:"isIframeBlank",value:function(e){var t="about:blank",n=e.getAttribute("src").trim();return e.contentWindow.location.href===t&&n!==t&&n}},{key:"observeIframeLoad",value:function(e,t,n){function r(){if(!i){i=!0,clearTimeout(a);try{o.isIframeBlank(e)||(e.removeEventListener("load",r),o.getIframeContents(e,t,n))}catch(e){n()}}}var o=this,i=!1,a=null;e.addEventListener("load",r),a=setTimeout(r,this.iframesTimeout)}},{key:"onIframeReady",value:function(e,t,n){try{"complete"!==e.contentWindow.document.readyState||this.isIframeBlank(e)?this.observeIframeLoad(e,t,n):this.getIframeContents(e,t,n)}catch(e){n()}}},{key:"waitForIframes",value:function(e,t){var n=this,r=0;this.forEachIframe(e,function(){return!0},function(e){r++,n.waitForIframes(e.querySelector("html"),function(){--r||t()})},function(e){e||t()})}},{key:"forEachIframe",value:function(e,n,r){function o(){--a<=0&&t(s)}var i=this,t=3<arguments.length&&void 0!==arguments[3]?arguments[3]:function(){},a=(e=e.querySelectorAll("iframe")).length,s=0,e=Array.prototype.slice.call(e);a||o(),e.forEach(function(t){c.matches(t,i.exclude)?o():i.onIframeReady(t,function(e){n(t)&&(s++,r(e)),o()},o)})}},{key:"createIterator",value:function(e,t,n){return o.createNodeIterator(e,t,n,!1)}},{key:"createInstanceOnIframe",value:function(e){return new c(e.querySelector("html"),this.iframes)}},{key:"compareNodeIframe",value:function(e,t,n){if(e.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_PRECEDING){if(null===t)return!0;if(t.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_FOLLOWING)return!0}return!1}},{key:"getIteratorNode",value:function(e){var t=e.previousNode();return{prevNode:t,node:(null===t||e.nextNode())&&e.nextNode()}}},{key:"checkIframeFilter",value:function(e,t,n,r){var o=!1,i=!1;return r.forEach(function(e,t){e.val===n&&(o=t,i=e.handled)}),this.compareNodeIframe(e,t,n)?(!1!==o||i?!1===o||i||(r[o].handled=!0):r.push({val:n,handled:!0}),!0):(!1===o&&r.push({val:n,handled:!1}),!1)}},{key:"handleOpenIframes",value:function(e,t,n,r){var o=this;e.forEach(function(e){e.handled||o.getIframeContents(e.val,function(e){o.createInstanceOnIframe(e).forEachNode(t,n,r)})})}},{key:"iterateThroughNodes",value:function(t,e,n,r,o){for(var i,a=this,s=this.createIterator(e,t,r),c=[],u=[],l=void 0,f=void 0;i=void 0,i=a.getIteratorNode(s),f=i.prevNode,l=i.node;)this.iframes&&this.forEachIframe(e,function(e){return a.checkIframeFilter(l,f,e,c)},function(e){a.createInstanceOnIframe(e).forEachNode(t,n,r)}),u.push(l);u.forEach(function(e){n(e)}),this.iframes&&this.handleOpenIframes(c,t,n,r),o()}},{key:"forEachNode",value:function(n,r,o){var i=this,a=3<arguments.length&&void 0!==arguments[3]?arguments[3]:function(){},e=this.getContexts(),s=e.length;s||a(),e.forEach(function(e){function t(){i.iterateThroughNodes(n,e,r,o,function(){--s<=0&&a()})}i.iframes?i.waitForIframes(e,t):t()})}}],[{key:"matches",value:function(t,e){var e="string"==typeof e?[e]:e,n=t.matches||t.matchesSelector||t.msMatchesSelector||t.mozMatchesSelector||t.oMatchesSelector||t.webkitMatchesSelector;if(n){var r=!1;return e.every(function(e){return!n.call(t,e)||!(r=!0)}),r}return!1}}]),c);function c(e){var t=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],n=2<arguments.length&&void 0!==arguments[2]?arguments[2]:[],r=3<arguments.length&&void 0!==arguments[3]?arguments[3]:5e3;_classCallCheck(this,c),this.ctx=e,this.iframes=t,this.exclude=n,this.iframesTimeout=r}function r(e){_classCallCheck(this,r),this.ctx=e,this.ie=!1;e=t.navigator.userAgent;(-1<e.indexOf("MSIE")||-1<e.indexOf("Trident"))&&(this.ie=!0)}return e.fn.mark=function(e,t){return new n(this.get()).mark(e,t),this},e.fn.markRegExp=function(e,t){return new n(this.get()).markRegExp(e,t),this},e.fn.unmark=function(e){return new n(this.get()).unmark(e),this},e},window,document);