import*as UI from"../ui/ui.js";import{TokenizerFactory}from"./CodeMirrorUtils.js";export class SyntaxHighlighter{constructor(e,t){this._mimeType=e,this._stripExtraWhitespace=t}createSpan(e,t){const i=document.createElement("span");return i.className=t.replace(/\S+/g,"cm-$&"),this._stripExtraWhitespace&&"whitespace"!==t&&(e=e.replace(/^[\n\r]*/,"").replace(/\s*$/,"")),UI.UIUtils.createTextChild(i,e),i}syntaxHighlightNode(e){const t=e.textContent?e.textContent.split("\n"):[];let i,r;e.removeChildren();const s=TokenizerFactory.instance().createTokenizer(this._mimeType);for(let a=0;a<t.length;++a){if(r=t[a],i=0,s(r,n.bind(this)),i<r.length){const t=r.substring(i,r.length);UI.UIUtils.createTextChild(e,t)}a<t.length-1&&UI.UIUtils.createTextChild(e,"\n")}return Promise.resolve();function n(t,s,n,a){if(s){if(n>i){const t=r.substring(i,n);UI.UIUtils.createTextChild(e,t)}e.appendChild(this.createSpan(t,s)),i=a}}}}