-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.colorfy.min.js
1 lines (1 loc) · 7.01 KB
/
jquery.colorfy.min.js
1
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _slicedToArray=function(){function e(e,t){var n=[],r=!0,o=!1,i=void 0;try{for(var a,s=e[Symbol.iterator]();!(r=(a=s.next()).done)&&(n.push(a.value),!t||n.length!==t);r=!0);}catch(c){o=!0,i=c}finally{try{!r&&s["return"]&&s["return"]()}finally{if(o)throw i}}return n}return function(t,n){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),_createClass=function(){function e(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(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),thisBrowser=function(){var e=window.navigator.userAgent.indexOf("MSIE ")>0,t=window.navigator.userAgent.indexOf("Chrome")>0,n=window.navigator.userAgent.indexOf("Firefox")>0,r=window.navigator.userAgent.indexOf("Safari")>0;return e?"IE":t?"Chrome":n?"Firefox":r?"Safari":void 0}(),assocArray=function(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];if(Array.isArray(e))return e;var t=[];return Object.keys(e).forEach(function(n){t.unshift([n,e[n]])}),t},htmlfy=function(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/\//g,"/").replace(/\n/g,"<br>").replace(/ /g," ")},datafy=function(e){return e.replace(/<(?!br|\/br).+?>/gm,"").replace(/<br>/g,"\n").replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&").replace(/"/g,'"').replace(/'/g,"'").replace(///g,"/").replace(/ /g," ")},ColorNode=function(){function e(t,n,r,o){_classCallCheck(this,e),this.content=t,this.htmlfier=n,this.descriptor=assocArray(r),this.klass=o,this.subnodes=[],this.supernode=null,this.processed=!1,this.terminate=!1}return _createClass(e,[{key:"toHTML",value:function(){this.processed||this.process();var e=this.klass?"<span class='"+this.klass+"'>":"",t=this.klass?"</span>":"",n=[];if(this.terminate)n.push(this.htmlfier(this.content));else{var r=!0,o=!1,i=void 0;try{for(var a,s=this.subnodes[Symbol.iterator]();!(r=(a=s.next()).done);r=!0){var c=a.value;n.push(c.toHTML())}}catch(l){o=!0,i=l}finally{try{!r&&s["return"]&&s["return"]()}finally{if(o)throw i}}}return e+n.join("")+t}},{key:"process",value:function(){if(0==this.descriptor.length)this.terminate=!0;else{for(var t=this.descriptor.pop(),n=_slicedToArray(t,2),r=n[0],o=n[1],i=0,a=this.content,s=void 0;s=o.exec(a);){var c=a.substr(i,s.index),l=s[0];if(i=l.length+s.index,a=a.substr(i),i=0,c.length>0){var u=new e(c,this.htmlfier,this.descriptor.slice(0),null);this.subnodes.push(u)}var d=new e(l,this.htmlfier,this.descriptor.slice(0),r);this.subnodes.push(d)}var f=new e(a,this.htmlfier,this.descriptor.slice(0),null);this.subnodes.push(f)}this.processed=!0}}]),e}(),syntaxDescriptors={},_colorfy=function(e,t,n,r){n||(n=htmlfy);var o=new ColorNode(e,n,t,r);return o.toHTML()},_colorfy2=function(e,t){var n=void 0;return n=jQuery?jQuery.fn.colorfy[t]:syntaxDescriptors[t],_colorfy(e,n,htmlfy,t)},parentsOfNode=function(e){for(var t=[e];e=e.parentNode;)t.unshift(e);return t},commonAncestor=function(e,t){var n=parentsOfNode(e),r=parentsOfNode(t);if(n[0]!=r[0])return null;for(var o=0,i=n.length;i>o;o++)if(n[o]!=r[o])return n[o-1]},lengthOfNode=function e(t){if(t.nodeType==Node.TEXT_NODE)return t.nodeValue.length;if("BR"==t.tagName)return 1;if("SPAN"==t.tagName||"DIV"==t.tagName){for(var n=0,r=0,o=t.childNodes.length;o>r;r++)n+=e(t.childNodes[r]);return n}return 0},lengthOfNodeToOffset=function(e,t){if(e.nodeType==Node.TEXT_NODE)return t;if("BR"==e.tagName)return t;if("SPAN"==e.tagName||"DIV"==e.tagName){for(var n=0,r=0;t>r;r++)n+=lengthOfNode(e.childNodes[r]);return n}return t},cursorLocation=function t(e,n,r,o){if(o||(o=e),o==n)return lengthOfNodeToOffset(n,r);if(!o.contains(n)&&e.contains(commonAncestor(o,n))&&o.compareDocumentPosition(n)==Node.DOCUMENT_POSITION_FOLLOWING)return lengthOfNode(o);if(!o.contains(n)&&e.contains(commonAncestor(o,n))&&o.compareDocumentPosition(n)==Node.DOCUMENT_POSITION_PRECEDING)return 0;if(o.contains(n)){for(var i=0,a=0,s=o.childNodes.length;s>a;a++)i+=t(e,n,r,o.childNodes[a]);return i}return 0},nodeAndOffset=function(e,t){var n=!0;e:for(;n;){var r=e,o=t;if(i=a=s=void 0,n=!1,lengthOfNode(o)<r)return[];if(o.nodeType==Node.TEXT_NODE)return[o,r];if("BR"==o.tagName)switch(thisBrowser){case"Chrome":return[o.nextSibling,0];case"Firefox":return"BR"==o.nextSibling.tagname?[o.nextSibling,0]:[o,0];case"IE":case"Safari":default:return[o,r]}else for(var i=0,a=o.childNodes.length;a>i;i++){var s=o.childNodes[i];if(!(lengthOfNode(s)<r)){e=r,t=s,n=!0;continue e}r-=lengthOfNode(s)}}},restoreCursor=function(e){if(document.activeElement==e){var t=window.getSelection();if(t.isCollapsed){var n=nodeAndOffset(e.getAttribute("data-cursor"),e),r=_slicedToArray(n,2),o=r[0],i=r[1];o&&i>=0&&t.collapse(o,i)}}},saveCursor=function(e){if(document.activeElement==e){var t=window.getSelection();if(t.isCollapsed){var n=t.anchorNode,r=t.anchorOffset;if(e.contains(n)){var o=cursorLocation(e,n,r);e.setAttribute("data-cursor",o)}}}},Colorfy=function(){function e(t){_classCallCheck(this,e),this.node=t}return _createClass(e,[{key:"colorfy",value:function(e){var t=this,n=document.createElement("div");n.setAttribute("contenteditable",!0),n.setAttribute("class",this.node.getAttribute("class")),n.style.maxHeight=this.node.clientHeight,n.style.height=this.node.clientHeight,"INPUT"==this.node.tagName?n.style.overflow="hidden":n.style.overflow="scroll",this.node.parentNode.insertBefore(n,this.node.nextSibling),this.node.style.display="none",this.colorfyTriggeredChange=!1;var r=function(){if(!t.colorfyTriggeredChange){n.dataText=t.node.value;var e=document.createEvent("CustomEvent");e.initEvent("receive-content",!0,!0),n.dispatchEvent(e)}};jQuery?jQuery(this.node).on("keyup paste change input",r):(this.node.addEventListener("keyup",r),this.node.addEventListener("paste",r),this.node.addEventListener("change",r),this.node.addEventListener("input",r));var o=function(){saveCursor(n),n.dataText=datafy(n.innerHTML);var e=document.createEvent("CustomEvent");e.initEvent("send-content",!0,!0),n.dispatchEvent(e),e=document.createEvent("CustomEvent"),e.initEvent("receive-content",!0,!0),n.dispatchEvent(e)};jQuery?jQuery(n).on("input paste",o):(n.addEventListener("input",o),n.addEventListener("paste",o)),n.addEventListener("receive-content",function(t){var r="";r="Firefox"==thisBrowser?n.textContent:n.innerText,0==r.length?n.style.display="block":n.style.display="inline-block",n.innerHTML=_colorfy2(n.dataText,e),restoreCursor(n)}),n.addEventListener("send-content",function(e){t.colorfyTriggeredChange=!0,t.node.value=n.dataText,t.colorfyTriggeredChange=!1;var r=new Event("change");t.node.dispatchEvent(r)}),n.dataText=this.node.value;var i=document.createEvent("CustomEvent");i.initEvent("receive-content",!0,!0),n.dispatchEvent(i)}}]),e}();jQuery.fn.colorfy=function(e){this.each(function(){var t=new Colorfy(this);t.colorfy(e)})};