heti/dist/heti-addon.min.js
2020-03-01 22:24:17 +08:00

1 line
8.6 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Heti=t()}(this,(function(){"use strict";var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var t=function(e,t){return e(t={exports:{}},t.exports),t.exports}((function(t){var n,r;n=e,r=function(){var e=document,t={}.hasOwnProperty;function n(){return r.apply(null,arguments)||i.apply(null,arguments)}function r(e,t,r,o,a){if(t&&!t.nodeType&&arguments.length<=2)return!1;var s,d="function"==typeof r;d&&(s=r,r=function(e,t){return s(e.text,t.startIndex)});var p=i(t,{find:e,wrap:d?null:r,replace:d?r:"$"+(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:a});return n.revert=function(){return p.revert()},!0}function i(e,t){return new o(e,t)}function o(e,r){var i=r.preset&&n.PRESETS[r.preset];if(r.portionMode=r.portionMode||"retain",i)for(var o in i)t.call(i,o)&&!t.call(r,o)&&(r[o]=i[o]);this.node=e,this.options=r,this.prepMatch=r.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=o,o.prototype={search:function(){var e,t=0,n=0,r=this.options.find,i=this.getAggregateText(),o=[],a=this;return r="string"==typeof r?RegExp(String(r).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1"),"g"):r,function i(s){for(var d=0,p=s.length;d<p;++d){var c=s[d];if("string"==typeof c){if(r.global)for(;e=r.exec(c);)o.push(a.prepMatch(e,t++,n));else(e=c.match(r))&&o.push(a.prepMatch(e,0,n));n+=c.length}else i(c)}}(i),o},prepMatch:function(e,t,n){if(!e[0])throw new Error("findAndReplaceDOMText cannot handle zero-length matches");return e.endIndex=n+e.index+e[0].length,e.startIndex=n+e.index,e.index=t,e},getAggregateText:function(){var e=this.options.filterElements,t=this.options.forceContext;return function n(r){if(r.nodeType===Node.TEXT_NODE)return[r.data];if(e&&!e(r))return[];var i=[""],o=0;if(r=r.firstChild)do{if(r.nodeType!==Node.TEXT_NODE){var a=n(r);t&&r.nodeType===Node.ELEMENT_NODE&&(!0===t||t(r))?(i[++o]=a,i[++o]=""):("string"==typeof a[0]&&(i[o]+=a.shift()),a.length&&(i[++o]=a,i[++o]=""))}else i[o]+=r.data}while(r=r.nextSibling);return i}(this.node)},processMatches:function(){var e,t,n,r=this.matches,i=this.node,o=this.options.filterElements,a=[],s=i,d=r.shift(),p=0,c=0,l=[i];e:for(;;){if(s.nodeType===Node.TEXT_NODE&&(!t&&s.length+p>=d.endIndex?t={node:s,index:c++,text:s.data.substring(d.startIndex-p,d.endIndex-p),indexInMatch:0===p?0:p-d.startIndex,indexInNode:d.startIndex-p,endIndexInNode:d.endIndex-p,isEnd:!0}:e&&a.push({node:s,index:c++,text:s.data,indexInMatch:p-d.startIndex,indexInNode:0}),!e&&s.length+p>d.startIndex&&(e={node:s,index:c++,indexInMatch:0,indexInNode:d.startIndex-p,endIndexInNode:d.endIndex-p,text:s.data.substring(d.startIndex-p,d.endIndex-p)}),p+=s.data.length),n=s.nodeType===Node.ELEMENT_NODE&&o&&!o(s),e&&t){if(s=this.replaceMatch(d,e,a,t),p-=t.node.data.length-t.endIndexInNode,e=null,t=null,a=[],c=0,!(d=r.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())===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(t,n){var r=this.options.replace||"$&",i=this.options.wrap,o=this.options.wrapClass;if(i&&i.nodeType){var a=e.createElement("div");a.innerHTML=i.outerHTML||(new XMLSerializer).serializeToString(i),i=a.firstChild}if("function"==typeof r)return(r=r(t,n))&&r.nodeType?r:e.createTextNode(String(r));var s="string"==typeof i?e.createElement(i):i;return s&&o&&(s.className=o),(r=e.createTextNode(this.prepareReplacementString(r,t,n))).data&&s?(s.appendChild(r),s):r},replaceMatch:function(t,n,r,i){var o,a,s=n.node,d=i.node;if(s===d){var p=s;n.indexInNode>0&&(o=e.createTextNode(p.data.substring(0,n.indexInNode)),p.parentNode.insertBefore(o,p));var c=this.getPortionReplacementNode(i,t);return p.parentNode.insertBefore(c,p),i.endIndexInNode<p.length&&(a=e.createTextNode(p.data.substring(i.endIndexInNode)),p.parentNode.insertBefore(a,p)),p.parentNode.removeChild(p),this.reverts.push((function(){o===c.previousSibling&&o.parentNode.removeChild(o),a===c.nextSibling&&a.parentNode.removeChild(a),c.parentNode.replaceChild(p,c)})),c}o=e.createTextNode(s.data.substring(0,n.indexInNode)),a=e.createTextNode(d.data.substring(i.endIndexInNode));for(var l=this.getPortionReplacementNode(n,t),h=[],u=0,f=r.length;u<f;++u){var g=r[u],x=this.getPortionReplacementNode(g,t);g.node.parentNode.replaceChild(x,g.node),this.reverts.push(function(e,t){return function(){t.parentNode.replaceChild(e.node,t)}}(g,x)),h.push(x)}var N=this.getPortionReplacementNode(i,t);return s.parentNode.insertBefore(o,s),s.parentNode.insertBefore(l,s),s.parentNode.removeChild(s),d.parentNode.insertBefore(N,d),d.parentNode.insertBefore(a,d),d.parentNode.removeChild(d),this.reverts.push((function(){o.parentNode.removeChild(o),l.parentNode.replaceChild(s,l),a.parentNode.removeChild(a),N.parentNode.replaceChild(d,N)})),N}},n},t.exports?t.exports=r():n.findAndReplaceDOMText=r()}));const n="⺀-⻿⼀-⿟぀-ゟ゠-ヺー-ヿ㄀-ㄯ㈀-㋿㐀-䶿一-鿿豈-﫿",r="A-Za-zͰ-Ͽ0-9`~!@#\\$%\\^&\\*\\(\\)-_=\\+\\[\\]{}\\\\\\|;:'\",<.>\\/\\?",i={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,ins:1,del:1,s:1},o={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,pre:1,code:1,sup:1,sub:1,"heti-spacing":1},a={}.hasOwnProperty;return class{constructor(e){this.rootSelector=e||".heti",this.REG_FULL=new RegExp(`(?<=[${n}])( *[${r}]+(?: +[${r}]+)* *)(?=[${n}])`,"g"),this.REG_START=new RegExp(`([${r}]+(?: +[${r}]+)* *)(?=[${n}])`,"g"),this.REG_END=new RegExp(`(?<=[${n}])( *[${r}]+(?: +[${r}]+)*)`,"g"),this.funcForceContext=function(e){return a.call(i,e.nodeName.toLowerCase())},this.funcFilterElements=function(e){return!(e.classList&&e.classList.contains("heti-skip")||a.call(o,e.nodeName.toLowerCase()))}}spacingElements(e){for(let t of e)this.spacingElement(t)}spacingElement(e){const n={forceContext:this.funcForceContext,filterElements:this.funcFilterElements},r=function(e,t){const n=document.createElement("heti-spacing");return n.className=e,n.textContent=t.trim(),n};t(e,Object.assign(n,{find:this.REG_FULL,replace:e=>r("heti-spacing-start heti-spacing-end",e.text)})),t(e,Object.assign(n,{find:this.REG_START,replace:e=>r("heti-spacing-start",e.text)})),t(e,Object.assign(n,{find:this.REG_END,replace:e=>r("heti-spacing-end",e.text)}))}autoSpacing(){document.addEventListener("DOMContentLoaded",()=>{const e=document.querySelectorAll(this.rootSelector);for(let t of e)this.spacingElement(t)})}}}));