function n(n,t){e(n,"color",t,"data-rte-color")}function t(n,t){e(n,"backgroundColor",t,"data-rte-bg")}function o(n,t,o){for(;t&&t!==n;){if(t instanceof HTMLSpanElement&&t.hasAttribute(o))return t;t=t.parentNode}return null}function e(n,t,e,u){const i=window.getSelection();if(!i||0===i.rangeCount)return;const s=i.getRangeAt(0);if(!n.contains(s.commonAncestorContainer))return;const f=s.cloneRange();if(f.collapsed){const c=o(n,f.startContainer,u);if(c)return c.style[t]=e,void r(n,u,t);const s=document.createElement("span");s.setAttribute(u,"1"),s.style[t]=e,s.appendChild(document.createTextNode("​")),f.insertNode(s);const a=document.createRange();return a.setStart(s.firstChild,1),a.collapse(!0),i.removeAllRanges(),i.addRange(a),void r(n,u,t)}const a=o(n,f.startContainer,u),d=o(n,f.endContainer,u);if(a&&a===d){if(function(n,t){const o=document.createRange();return o.selectNodeContents(t),n.compareBoundaryPoints(Range.START_TO_START,o)<=0&&n.compareBoundaryPoints(Range.END_TO_END,o)>=0}(f,a))a.style[t]=e;else{const n=function(n,t,o,e,r){const u=n.style[e],i=document.createRange();i.setStart(n,0),i.setEnd(t.startContainer,t.startOffset);const s=document.createRange();s.setStart(t.endContainer,t.endOffset),s.setEnd(n,n.childNodes.length);const f=document.createDocumentFragment(),a=n=>{const t=document.createElement("span");return t.setAttribute(o,"1"),t.style[e]=n,t};if(c(i)){const n=a(u);n.appendChild(i.cloneContents()),f.appendChild(n)}const d=a(r);if(d.appendChild(t.cloneContents()),f.appendChild(d),c(s)){const n=a(u);n.appendChild(s.cloneContents()),f.appendChild(n)}return n.replaceWith(f),d}(a,f,u,t,e),o=window.getSelection(),r=document.createRange();r.selectNodeContents(n),o?.removeAllRanges(),o?.addRange(r)}return void r(n,u,t)}const l=(()=>{let n=f.commonAncestorContainer;for(;n&&!(n instanceof HTMLElement);)n=n.parentNode;return n})(),m=document.createTreeWalker(l||n,NodeFilter.SHOW_TEXT,{acceptNode:n=>{if(!n.nodeValue||!n.nodeValue.trim())return NodeFilter.FILTER_REJECT;const t=document.createRange();t.selectNodeContents(n);return f.compareBoundaryPoints(Range.END_TO_START,t)<0&&f.compareBoundaryPoints(Range.START_TO_END,t)>0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT}}),p=[];let g;for(;g=m.nextNode();)p.push(g);p.forEach((r=>{let c=0,i=r.length;r===f.startContainer&&(c=f.startOffset),r===f.endContainer&&(i=f.endOffset),c>0&&(r=r.splitText(c)),i<r.length&&r.splitText(i);const s=o(n,r,u);if(s)return void(s.style[t]=e);const a=document.createElement("span");a.setAttribute(u,"1"),a.style[t]=e;r.parentElement.replaceChild(a,r),a.appendChild(r)})),r(n,u,t),i.removeAllRanges(),i.addRange(f)}function r(n,t,o){const e=Array.from(n.querySelectorAll(`span[${t}]`)),r=n=>n.style[o];e.forEach((n=>{Array.from(n.querySelectorAll(`span[${t}]`)).forEach((t=>{if(r(t)===r(n)){for(;t.firstChild;)n.insertBefore(t.firstChild,t);t.remove()}}));const o=n.previousSibling;if(o instanceof HTMLSpanElement&&o.hasAttribute(t)&&r(o)===r(n)){for(;n.firstChild;)o.appendChild(n.firstChild);return void n.remove()}const e=n.nextSibling;if(e instanceof HTMLSpanElement&&e.hasAttribute(t)&&r(e)===r(n)){for(;e.firstChild;)n.appendChild(e.firstChild);e.remove()}}))}function c(n){if(n.collapsed)return!1;return(n.cloneContents().textContent||"").length>0}export{n as a,t as s};
