import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={node:"<node>"},str_=i18n.i18n.registerUIStrings("elements/DOMLinkifier.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export const decorateNodeLabel=function(e,n,t){const i=e,o=e.nodeType()===Node.ELEMENT_NODE&&e.pseudoType();o&&e.parentNode&&(e=e.parentNode);let r=e.nodeNameInCorrectCase();const s=n.createChild("span","node-label-name");s.textContent=r;const d=e.getAttribute("id");if(d){const e=n.createChild("span","node-label-id"),t="#"+d;r+=t,UI.UIUtils.createTextChild(e,t),s.classList.add("extra")}const a=e.getAttribute("class");if(a){const e=a.split(/\s+/);if(e.length){const t=new Set,i=n.createChild("span","extra node-label-class");for(let n=0;n<e.length;++n){const o=e[n];if(o&&!t.has(o)){const e="."+o;r+=e,UI.UIUtils.createTextChild(i,e),t.add(o)}}}}if(o){const e=n.createChild("span","extra node-label-pseudo"),t="::"+i.pseudoType();UI.UIUtils.createTextChild(e,t),r+=t}UI.Tooltip.Tooltip.install(n,t||r)};export const linkifyNodeReference=function(e,n={tooltip:void 0,preventKeyboardFocus:void 0}){if(!e)return document.createTextNode(i18nString(UIStrings.node));const t=document.createElement("span");t.classList.add("monospace");const i=UI.Utils.createShadowRootWithCoreStyles(t,{cssFile:"elements/domLinkifier.css",enableLegacyPatching:!1,delegatesFocus:void 0}).createChild("div","node-link");return decorateNodeLabel(e,i,n.tooltip),i.addEventListener("click",(()=>Common.Revealer.reveal(e,!1)&&!1),!1),i.addEventListener("mouseover",e.highlight.bind(e,void 0),!1),i.addEventListener("mouseleave",(()=>SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight()),!1),n.preventKeyboardFocus||(i.addEventListener("keydown",(n=>"Enter"===n.key&&Common.Revealer.reveal(e,!1)&&!1)),i.tabIndex=0,UI.ARIAUtils.markAsLink(i)),t};export const linkifyDeferredNodeReference=function(e,n={tooltip:void 0,preventKeyboardFocus:void 0}){const t=document.createElement("div"),i=UI.Utils.createShadowRootWithCoreStyles(t,{cssFile:"elements/domLinkifier.css",enableLegacyPatching:!1,delegatesFocus:void 0}).createChild("div","node-link");function o(e){Common.Revealer.reveal(e)}return i.createChild("slot"),i.addEventListener("click",e.resolve.bind(e,o),!1),i.addEventListener("mousedown",(e=>e.consume()),!1),n.preventKeyboardFocus||(i.addEventListener("keydown",(n=>"Enter"===n.key&&e.resolve(o))),i.tabIndex=0,UI.ARIAUtils.markAsLink(i)),t};let linkifierInstance;export class Linkifier{static instance(e={forceNew:null}){const{forceNew:n}=e;return linkifierInstance&&!n||(linkifierInstance=new Linkifier),linkifierInstance}linkify(e,n){if(e instanceof SDK.DOMModel.DOMNode)return linkifyNodeReference(e,n);if(e instanceof SDK.DOMModel.DeferredDOMNode)return linkifyDeferredNodeReference(e,n);throw new Error("Can't linkify non-node")}}