UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 27.4 kB
import*as Common from"../common/common.js";import*as Host from"../host/host.js";import*as Platform from"../platform/platform.js";import*as Root from"../root/root.js";import{CSSModel}from"./CSSModel.js";import{FrameManager}from"./FrameManager.js";import{OverlayModel}from"./OverlayModel.js";import{RuntimeModel}from"./RuntimeModel.js";import{Capability}from"./Target.js";import{SDKModel}from"./SDKModel.js";import{TargetManager}from"./TargetManager.js";import{ResourceTreeModel}from"./ResourceTreeModel.js";export class DOMNode{#e;#t;ownerDocument;#n;id;index;#o;#s;#r;#d;nodeValueInternal;#i;#a;#l;#h;#u;#c;#m;#I;#p;assignedSlot;shadowRootsInternal;#M;#N;#g;childNodeCountInternal;childrenInternal;nextSibling;previousSibling;firstChild;lastChild;parentNode;templateContentInternal;contentDocumentInternal;childDocumentPromiseForTesting;#v;publicId;systemId;internalSubset;name;value;constructor(e){this.#e=e,this.#t=this.#e.getAgent(),this.index=void 0,this.#m=null,this.#I=new Map,this.#p=[],this.assignedSlot=null,this.shadowRootsInternal=[],this.#M=new Map,this.#N=new Map,this.#g=0,this.childrenInternal=null,this.nextSibling=null,this.previousSibling=null,this.firstChild=null,this.lastChild=null,this.parentNode=null}static create(e,t,n,o){const s=new DOMNode(e);return s.init(t,n,o),s}init(e,t,n){if(this.#t=this.#e.getAgent(),this.ownerDocument=e,this.#n=t,this.id=n.nodeId,this.#o=n.backendNodeId,this.#e.registerNode(this),this.#s=n.nodeType,this.#r=n.nodeName,this.#d=n.localName,this.nodeValueInternal=n.nodeValue,this.#i=n.pseudoType,this.#a=n.pseudoIdentifier,this.#l=n.shadowRootType,this.#h=n.frameId||null,this.#u=n.xmlVersion,this.#c=Boolean(n.isSVG),n.attributes&&this.setAttributesPayload(n.attributes),this.childNodeCountInternal=n.childNodeCount||0,n.shadowRoots)for(let e=0;e<n.shadowRoots.length;++e){const t=n.shadowRoots[e],o=DOMNode.create(this.#e,this.ownerDocument,!0,t);this.shadowRootsInternal.push(o),o.parentNode=this}n.templateContent&&(this.templateContentInternal=DOMNode.create(this.#e,this.ownerDocument,!0,n.templateContent),this.templateContentInternal.parentNode=this,this.childrenInternal=[]);const o=new Set(["EMBED","IFRAME","OBJECT","PORTAL","FENCEDFRAME"]);n.contentDocument?(this.contentDocumentInternal=new DOMDocument(this.#e,n.contentDocument),this.contentDocumentInternal.parentNode=this,this.childrenInternal=[]):n.frameId&&o.has(n.nodeName)&&(this.childDocumentPromiseForTesting=this.requestChildDocument(n.frameId,this.#e.target()),this.childrenInternal=[]),n.importedDocument&&(this.#v=DOMNode.create(this.#e,this.ownerDocument,!0,n.importedDocument),this.#v.parentNode=this,this.childrenInternal=[]),n.distributedNodes&&this.setDistributedNodePayloads(n.distributedNodes),n.assignedSlot&&this.setAssignedSlot(n.assignedSlot),n.children&&this.setChildrenPayload(n.children),this.setPseudoElements(n.pseudoElements),this.#s===Node.ELEMENT_NODE?(this.ownerDocument&&!this.ownerDocument.documentElement&&"HTML"===this.#r&&(this.ownerDocument.documentElement=this),this.ownerDocument&&!this.ownerDocument.body&&"BODY"===this.#r&&(this.ownerDocument.body=this)):this.#s===Node.DOCUMENT_TYPE_NODE?(this.publicId=n.publicId,this.systemId=n.systemId,this.internalSubset=n.internalSubset):this.#s===Node.ATTRIBUTE_NODE&&(this.name=n.name,this.value=n.value)}async requestChildDocument(e,t){const n=await FrameManager.instance().getOrWaitForFrame(e,t),o=n.resourceTreeModel()?.target().model(DOMModel);return o?.requestDocument()||null}isAdFrameNode(){if(this.isIframe()&&this.#h){const e=FrameManager.instance().getFrame(this.#h);return!!e&&"none"!==e.adFrameType()}return!1}isSVGNode(){return this.#c}creationStackTrace(){if(this.#m)return this.#m;const e=this.#t.invoke_getNodeStackTraces({nodeId:this.id});return this.#m=e.then((e=>e.creation||null)),this.#m}get subtreeMarkerCount(){return this.#g}domModel(){return this.#e}backendNodeId(){return this.#o}children(){return this.childrenInternal?this.childrenInternal.slice():null}setChildren(e){this.childrenInternal=e}hasAttributes(){return this.#M.size>0}childNodeCount(){return this.childNodeCountInternal}setChildNodeCount(e){this.childNodeCountInternal=e}hasShadowRoots(){return Boolean(this.shadowRootsInternal.length)}shadowRoots(){return this.shadowRootsInternal.slice()}templateContent(){return this.templateContentInternal||null}contentDocument(){return this.contentDocumentInternal||null}setContentDocument(e){this.contentDocumentInternal=e}isIframe(){return"IFRAME"===this.#r}isPortal(){return"PORTAL"===this.#r}importedDocument(){return this.#v||null}nodeType(){return this.#s}nodeName(){return this.#r}pseudoType(){return this.#i}pseudoIdentifier(){return this.#a}hasPseudoElements(){return this.#I.size>0}pseudoElements(){return this.#I}beforePseudoElement(){return this.#I.get("before")?.at(-1)}afterPseudoElement(){return this.#I.get("after")?.at(-1)}markerPseudoElement(){return this.#I.get("marker")?.at(-1)}backdropPseudoElement(){return this.#I.get("backdrop")?.at(-1)}viewTransitionPseudoElements(){return[...this.#I.get("view-transition")||[],...this.#I.get("view-transition-group")||[],...this.#I.get("view-transition-image-pair")||[],...this.#I.get("view-transition-old")||[],...this.#I.get("view-transition-new")||[]]}hasAssignedSlot(){return null!==this.assignedSlot}isInsertionPoint(){return!this.isXMLNode()&&("SHADOW"===this.#r||"CONTENT"===this.#r||"SLOT"===this.#r)}distributedNodes(){return this.#p}isInShadowTree(){return this.#n}ancestorShadowHost(){const e=this.ancestorShadowRoot();return e?e.parentNode:null}ancestorShadowRoot(){if(!this.#n)return null;let e=this;for(;e&&!e.isShadowRoot();)e=e.parentNode;return e}ancestorUserAgentShadowRoot(){const e=this.ancestorShadowRoot();return e&&e.shadowRootType()===DOMNode.ShadowRootTypes.UserAgent?e:null}isShadowRoot(){return Boolean(this.#l)}shadowRootType(){return this.#l||null}nodeNameInCorrectCase(){const e=this.shadowRootType();return e?"#shadow-root ("+e+")":this.localName()?this.localName().length!==this.nodeName().length?this.nodeName():this.localName():this.nodeName()}setNodeName(e,t){this.#t.invoke_setNodeName({nodeId:this.id,name:e}).then((e=>{e.getError()||this.#e.markUndoableState(),t&&t(e.getError()||null,this.#e.nodeForId(e.nodeId))}))}localName(){return this.#d}nodeValue(){return this.nodeValueInternal}setNodeValueInternal(e){this.nodeValueInternal=e}setNodeValue(e,t){this.#t.invoke_setNodeValue({nodeId:this.id,value:e}).then((e=>{e.getError()||this.#e.markUndoableState(),t&&t(e.getError()||null)}))}getAttribute(e){const t=this.#M.get(e);return t?t.value:void 0}setAttribute(e,t,n){this.#t.invoke_setAttributesAsText({nodeId:this.id,text:t,name:e}).then((e=>{e.getError()||this.#e.markUndoableState(),n&&n(e.getError()||null)}))}setAttributeValue(e,t,n){this.#t.invoke_setAttributeValue({nodeId:this.id,name:e,value:t}).then((e=>{e.getError()||this.#e.markUndoableState(),n&&n(e.getError()||null)}))}setAttributeValuePromise(e,t){return new Promise((n=>this.setAttributeValue(e,t,n)))}attributes(){return[...this.#M.values()]}async removeAttribute(e){(await this.#t.invoke_removeAttribute({nodeId:this.id,name:e})).getError()||(this.#M.delete(e),this.#e.markUndoableState())}getChildNodes(e){this.childrenInternal?e(this.children()):this.#t.invoke_requestChildNodes({nodeId:this.id}).then((t=>{e(t.getError()?null:this.children())}))}async getSubtree(e,t){return(await this.#t.invoke_requestChildNodes({nodeId:this.id,depth:e,pierce:t})).getError()?null:this.childrenInternal}async getOuterHTML(){const{outerHTML:e}=await this.#t.invoke_getOuterHTML({nodeId:this.id});return e}setOuterHTML(e,t){this.#t.invoke_setOuterHTML({nodeId:this.id,outerHTML:e}).then((e=>{e.getError()||this.#e.markUndoableState(),t&&t(e.getError()||null)}))}removeNode(e){return this.#t.invoke_removeNode({nodeId:this.id}).then((t=>{t.getError()||this.#e.markUndoableState(),e&&e(t.getError()||null)}))}async copyNode(){const{outerHTML:e}=await this.#t.invoke_getOuterHTML({nodeId:this.id});return null!==e&&Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(e),e}path(){function e(e){return(void 0!==e.index||e.isShadowRoot()&&e.parentNode)&&e.#r.length}const t=[];let n=this;for(;n&&e(n);){const e="number"==typeof n.index?n.index:n.shadowRootType()===DOMNode.ShadowRootTypes.UserAgent?"u":"a";t.push([e,n.#r]),n=n.parentNode}return t.reverse(),t.join(",")}isAncestor(e){if(!e)return!1;let t=e.parentNode;for(;t;){if(this===t)return!0;t=t.parentNode}return!1}isDescendant(e){return null!==e&&e.isAncestor(this)}frameOwnerFrameId(){return this.#h}frameId(){let e=this.parentNode||this;for(;!e.#h&&e.parentNode;)e=e.parentNode;return e.#h}setAttributesPayload(e){let t=!this.#M||e.length!==2*this.#M.size;const n=this.#M||new Map;this.#M=new Map;for(let o=0;o<e.length;o+=2){const s=e[o],r=e[o+1];if(this.addAttribute(s,r),t)continue;const d=n.get(s);d&&d.value===r||(t=!0)}return t}insertChild(e,t){if(!this.childrenInternal)throw new Error("DOMNode._children is expected to not be null.");const n=DOMNode.create(this.#e,this.ownerDocument,this.#n,t);return this.childrenInternal.splice(e?this.childrenInternal.indexOf(e)+1:0,0,n),this.renumber(),n}removeChild(e){const t=e.pseudoType();if(t){const n=this.#I.get(t)?.filter((t=>t!==e));n&&n.length>0?this.#I.set(t,n):this.#I.delete(t)}else{const t=this.shadowRootsInternal.indexOf(e);if(-1!==t)this.shadowRootsInternal.splice(t,1);else{if(!this.childrenInternal)throw new Error("DOMNode._children is expected to not be null.");if(-1===this.childrenInternal.indexOf(e))throw new Error("DOMNode._children is expected to contain the node to be removed.");this.childrenInternal.splice(this.childrenInternal.indexOf(e),1)}}e.parentNode=null,this.#g-=e.#g,e.#g&&this.#e.dispatchEventToListeners(Events.MarkersChanged,this),this.renumber()}setChildrenPayload(e){this.childrenInternal=[];for(let t=0;t<e.length;++t){const n=e[t],o=DOMNode.create(this.#e,this.ownerDocument,this.#n,n);this.childrenInternal.push(o)}this.renumber()}setPseudoElements(e){if(e)for(let t=0;t<e.length;++t){const n=DOMNode.create(this.#e,this.ownerDocument,this.#n,e[t]);n.parentNode=this;const o=n.pseudoType();if(!o)throw new Error("DOMNode.pseudoType() is expected to be defined.");const s=this.#I.get(o);s?s.push(n):this.#I.set(o,[n])}}setDistributedNodePayloads(e){this.#p=[];for(const t of e)this.#p.push(new DOMNodeShortcut(this.#e.target(),t.backendNodeId,t.nodeType,t.nodeName))}setAssignedSlot(e){this.assignedSlot=new DOMNodeShortcut(this.#e.target(),e.backendNodeId,e.nodeType,e.nodeName)}renumber(){if(!this.childrenInternal)throw new Error("DOMNode._children is expected to not be null.");if(this.childNodeCountInternal=this.childrenInternal.length,0===this.childNodeCountInternal)return this.firstChild=null,void(this.lastChild=null);this.firstChild=this.childrenInternal[0],this.lastChild=this.childrenInternal[this.childNodeCountInternal-1];for(let e=0;e<this.childNodeCountInternal;++e){const t=this.childrenInternal[e];t.index=e,t.nextSibling=e+1<this.childNodeCountInternal?this.childrenInternal[e+1]:null,t.previousSibling=e-1>=0?this.childrenInternal[e-1]:null,t.parentNode=this}}addAttribute(e,t){const n={name:e,value:t,_node:this};this.#M.set(e,n)}setAttributeInternal(e,t){const n=this.#M.get(e);n?n.value=t:this.addAttribute(e,t)}removeAttributeInternal(e){this.#M.delete(e)}copyTo(e,t,n){this.#t.invoke_copyTo({nodeId:this.id,targetNodeId:e.id,insertBeforeNodeId:t?t.id:void 0}).then((e=>{e.getError()||this.#e.markUndoableState(),n&&n(e.getError()||null,this.#e.nodeForId(e.nodeId))}))}moveTo(e,t,n){this.#t.invoke_moveTo({nodeId:this.id,targetNodeId:e.id,insertBeforeNodeId:t?t.id:void 0}).then((e=>{e.getError()||this.#e.markUndoableState(),n&&n(e.getError()||null,this.#e.nodeForId(e.nodeId))}))}isXMLNode(){return Boolean(this.#u)}setMarker(e,t){if(null!==t){if(this.parentNode&&!this.#N.has(e))for(let e=this;e;e=e.parentNode)++e.#g;this.#N.set(e,t);for(let e=this;e;e=e.parentNode)this.#e.dispatchEventToListeners(Events.MarkersChanged,e)}else{if(!this.#N.has(e))return;this.#N.delete(e);for(let e=this;e;e=e.parentNode)--e.#g;for(let e=this;e;e=e.parentNode)this.#e.dispatchEventToListeners(Events.MarkersChanged,e)}}marker(e){return this.#N.get(e)||null}getMarkerKeysForTest(){return[...this.#N.keys()]}traverseMarkers(e){!function t(n){if(n.#g){for(const t of n.#N.keys())e(n,t);if(n.childrenInternal)for(const e of n.childrenInternal)t(e)}}(this)}resolveURL(e){if(!e)return e;for(let t=this;t;t=t.parentNode)if(t instanceof DOMDocument&&t.baseURL)return Common.ParsedURL.ParsedURL.completeURL(t.baseURL,e);return null}highlight(e){this.#e.overlayModel().highlightInOverlay({node:this,selectorList:void 0},e)}highlightForTwoSeconds(){this.#e.overlayModel().highlightInOverlayForTwoSeconds({node:this,selectorList:void 0})}async resolveToObject(e){const{object:t}=await this.#t.invoke_resolveNode({nodeId:this.id,backendNodeId:void 0,objectGroup:e});return t&&this.#e.runtimeModelInternal.createRemoteObject(t)||null}async boxModel(){const{model:e}=await this.#t.invoke_getBoxModel({nodeId:this.id});return e}async setAsInspectedNode(){let e=this;for(e&&e.pseudoType()&&(e=e.parentNode);e;){let t=e.ancestorUserAgentShadowRoot();if(!t)break;if(t=e.ancestorShadowHost(),!t)break;e=t}if(!e)throw new Error("In DOMNode.setAsInspectedNode: node is expected to not be null.");await this.#t.invoke_setInspectedNode({nodeId:e.id})}enclosingElementOrSelf(){let e=this;return e&&e.nodeType()===Node.TEXT_NODE&&e.parentNode&&(e=e.parentNode),e&&e.nodeType()!==Node.ELEMENT_NODE&&(e=null),e}async scrollIntoView(){const e=this.enclosingElementOrSelf();if(!e)return;const t=await e.resolveToObject();t&&(t.callFunction((function(){this.scrollIntoViewIfNeeded(!0)})),t.release(),e.highlightForTwoSeconds())}async focus(){const e=this.enclosingElementOrSelf();if(!e)throw new Error("DOMNode.focus expects node to not be null.");const t=await e.resolveToObject();t&&(await t.callFunction((function(){this.focus()})),t.release(),e.highlightForTwoSeconds(),await this.#e.target().pageAgent().invoke_bringToFront())}simpleSelector(){const e=this.localName()||this.nodeName().toLowerCase();if(this.nodeType()!==Node.ELEMENT_NODE)return e;const t=this.getAttribute("type"),n=this.getAttribute("id"),o=this.getAttribute("class");if("input"===e&&t&&!n&&!o)return e+'[type="'+CSS.escape(t)+'"]';if(n)return e+"#"+CSS.escape(n);if(o){return("div"===e?"":e)+"."+o.trim().split(/\s+/g).map((e=>CSS.escape(e))).join(".")}return e}}!function(e){let t;!function(e){e.UserAgent="user-agent",e.Open="open",e.Closed="closed"}(t=e.ShadowRootTypes||(e.ShadowRootTypes={}))}(DOMNode||(DOMNode={}));export class DeferredDOMNode{#e;#o;constructor(e,t){this.#e=e.model(DOMModel),this.#o=t}resolve(e){this.resolvePromise().then(e)}async resolvePromise(){const e=await this.#e.pushNodesByBackendIdsToFrontend(new Set([this.#o]));return e&&e.get(this.#o)||null}backendNodeId(){return this.#o}domModel(){return this.#e}highlight(){this.#e.overlayModel().highlightInOverlay({deferredNode:this,selectorList:void 0})}}export class DOMNodeShortcut{nodeType;nodeName;deferredNode;constructor(e,t,n,o){this.nodeType=n,this.nodeName=o,this.deferredNode=new DeferredDOMNode(e,t)}}export class DOMDocument extends DOMNode{body;documentElement;documentURL;baseURL;constructor(e,t){super(e),this.body=null,this.documentElement=null,this.init(this,!1,t),this.documentURL=t.documentURL||"",this.baseURL=t.baseURL||""}}export class DOMModel extends SDKModel{agent;idToDOMNode=new Map;#D;#b;runtimeModelInternal;#f;#E;#w;#T;#O;constructor(e){super(e),this.agent=e.domAgent(),this.#D=null,this.#b=new Set,e.registerDOMDispatcher(new DOMDispatcher(this)),this.runtimeModelInternal=e.model(RuntimeModel),this.#E=null,e.suspended()||this.agent.invoke_enable({}),Root.Runtime.experiments.isEnabled("captureNodeCreationStacks")&&this.agent.invoke_setNodeStackTracesEnabled({enable:!0})}runtimeModel(){return this.runtimeModelInternal}cssModel(){return this.target().model(CSSModel)}overlayModel(){return this.target().model(OverlayModel)}static cancelSearch(){for(const e of TargetManager.instance().models(DOMModel))e.cancelSearch()}scheduleMutationEvent(e){this.hasEventListeners(Events.DOMMutated)&&(this.#f=(this.#f||0)+1,Promise.resolve().then(function(e,t){if(!this.hasEventListeners(Events.DOMMutated)||this.#f!==t)return;this.dispatchEventToListeners(Events.DOMMutated,e)}.bind(this,e,this.#f)))}requestDocument(){return this.#D?Promise.resolve(this.#D):(this.#E||(this.#E=this.requestDocumentInternal()),this.#E)}async getOwnerNodeForFrame(e){const t=await this.agent.invoke_getFrameOwner({frameId:e});return t.getError()?null:new DeferredDOMNode(this.target(),t.backendNodeId)}async requestDocumentInternal(){const e=await this.agent.invoke_getDocument({});if(e.getError())return null;const{root:t}=e;if(this.#E=null,t&&this.setDocument(t),!this.#D)return console.error("No document"),null;const n=this.parentModel();if(n&&!this.#w){await n.requestDocument();const e=this.target().model(ResourceTreeModel)?.mainFrame;if(e){const t=await n.agent.invoke_getFrameOwner({frameId:e.id});!t.getError()&&t.nodeId&&(this.#w=n.nodeForId(t.nodeId))}}if(this.#w){const e=this.#w.contentDocument();this.#w.setContentDocument(this.#D),this.#w.setChildren([]),this.#D?(this.#D.parentNode=this.#w,this.dispatchEventToListeners(Events.NodeInserted,this.#D)):e&&this.dispatchEventToListeners(Events.NodeRemoved,{node:e,parent:this.#w})}return this.#D}existingDocument(){return this.#D}async pushNodeToFrontend(e){await this.requestDocument();const{nodeId:t}=await this.agent.invoke_requestNode({objectId:e});return t?this.nodeForId(t):null}pushNodeByPathToFrontend(e){return this.requestDocument().then((()=>this.agent.invoke_pushNodeByPathToFrontend({path:e}))).then((({nodeId:e})=>e))}async pushNodesByBackendIdsToFrontend(e){await this.requestDocument();const t=[...e],{nodeIds:n}=await this.agent.invoke_pushNodesByBackendIdsToFrontend({backendNodeIds:t});if(!n)return null;const o=new Map;for(let e=0;e<n.length;++e)n[e]&&o.set(t[e],this.nodeForId(n[e]));return o}attributeModified(e,t,n){const o=this.idToDOMNode.get(e);o&&(o.setAttributeInternal(t,n),this.dispatchEventToListeners(Events.AttrModified,{node:o,name:t}),this.scheduleMutationEvent(o))}attributeRemoved(e,t){const n=this.idToDOMNode.get(e);n&&(n.removeAttributeInternal(t),this.dispatchEventToListeners(Events.AttrRemoved,{node:n,name:t}),this.scheduleMutationEvent(n))}inlineStyleInvalidated(e){Platform.SetUtilities.addAll(this.#b,e),this.#T||(this.#T=window.setTimeout(this.loadNodeAttributes.bind(this),20))}loadNodeAttributes(){this.#T=void 0;for(const e of this.#b)this.agent.invoke_getAttributes({nodeId:e}).then((({attributes:t})=>{if(!t)return;const n=this.idToDOMNode.get(e);n&&n.setAttributesPayload(t)&&(this.dispatchEventToListeners(Events.AttrModified,{node:n,name:"style"}),this.scheduleMutationEvent(n))}));this.#b.clear()}characterDataModified(e,t){const n=this.idToDOMNode.get(e);n?(n.setNodeValueInternal(t),this.dispatchEventToListeners(Events.CharacterDataModified,n),this.scheduleMutationEvent(n)):console.error("nodeId could not be resolved to a node")}nodeForId(e){return e&&this.idToDOMNode.get(e)||null}documentUpdated(){const e=this.#E;this.setDocument(null),this.parentModel()&&!e&&this.requestDocument()}setDocument(e){this.idToDOMNode=new Map,this.#D=e&&"nodeId"in e?new DOMDocument(this,e):null,DOMModelUndoStack.instance().dispose(this),this.parentModel()||this.dispatchEventToListeners(Events.DocumentUpdated,this)}setDetachedRoot(e){"#document"===e.nodeName?new DOMDocument(this,e):DOMNode.create(this,null,!1,e)}setChildNodes(e,t){if(!e&&t.length)return void this.setDetachedRoot(t[0]);const n=this.idToDOMNode.get(e);n?.setChildrenPayload(t)}childNodeCountUpdated(e,t){const n=this.idToDOMNode.get(e);n?(n.setChildNodeCount(t),this.dispatchEventToListeners(Events.ChildNodeCountUpdated,n),this.scheduleMutationEvent(n)):console.error("nodeId could not be resolved to a node")}childNodeInserted(e,t,n){const o=this.idToDOMNode.get(e),s=this.idToDOMNode.get(t);if(!o)return void console.error("parentId could not be resolved to a node");const r=o.insertChild(s,n);this.idToDOMNode.set(r.id,r),this.dispatchEventToListeners(Events.NodeInserted,r),this.scheduleMutationEvent(r)}childNodeRemoved(e,t){const n=this.idToDOMNode.get(e),o=this.idToDOMNode.get(t);n&&o?(n.removeChild(o),this.unbind(o),this.dispatchEventToListeners(Events.NodeRemoved,{node:o,parent:n}),this.scheduleMutationEvent(o)):console.error("parentId or nodeId could not be resolved to a node")}shadowRootPushed(e,t){const n=this.idToDOMNode.get(e);if(!n)return;const o=DOMNode.create(this,n.ownerDocument,!0,t);o.parentNode=n,this.idToDOMNode.set(o.id,o),n.shadowRootsInternal.unshift(o),this.dispatchEventToListeners(Events.NodeInserted,o),this.scheduleMutationEvent(o)}shadowRootPopped(e,t){const n=this.idToDOMNode.get(e);if(!n)return;const o=this.idToDOMNode.get(t);o&&(n.removeChild(o),this.unbind(o),this.dispatchEventToListeners(Events.NodeRemoved,{node:o,parent:n}),this.scheduleMutationEvent(o))}pseudoElementAdded(e,t){const n=this.idToDOMNode.get(e);if(!n)return;const o=DOMNode.create(this,n.ownerDocument,!1,t);o.parentNode=n,this.idToDOMNode.set(o.id,o);const s=o.pseudoType();if(!s)throw new Error("DOMModel._pseudoElementAdded expects pseudoType to be defined.");const r=n.pseudoElements().get(s);r?(Platform.DCHECK((()=>s.startsWith("view-transition")),"DOMModel.pseudoElementAdded expects parent to not already have this pseudo type added; only view-transition* pseudo elements can coexist under the same parent."),r.push(o)):n.pseudoElements().set(s,[o]),this.dispatchEventToListeners(Events.NodeInserted,o),this.scheduleMutationEvent(o)}topLayerElementsUpdated(){this.dispatchEventToListeners(Events.TopLayerElementsChanged)}pseudoElementRemoved(e,t){const n=this.idToDOMNode.get(e);if(!n)return;const o=this.idToDOMNode.get(t);o&&(n.removeChild(o),this.unbind(o),this.dispatchEventToListeners(Events.NodeRemoved,{node:o,parent:n}),this.scheduleMutationEvent(o))}distributedNodesUpdated(e,t){const n=this.idToDOMNode.get(e);n&&(n.setDistributedNodePayloads(t),this.dispatchEventToListeners(Events.DistributedNodesChanged,n),this.scheduleMutationEvent(n))}unbind(e){this.idToDOMNode.delete(e.id);const t=e.children();for(let e=0;t&&e<t.length;++e)this.unbind(t[e]);for(let t=0;t<e.shadowRootsInternal.length;++t)this.unbind(e.shadowRootsInternal[t]);const n=e.pseudoElements();for(const e of n.values())for(const t of e)this.unbind(t);const o=e.templateContent();o&&this.unbind(o)}async getNodesByStyle(e,t=!1){if(await this.requestDocument(),!this.#D)throw new Error("DOMModel.getNodesByStyle expects to have a document.");const n=await this.agent.invoke_getNodesForSubtreeByStyle({nodeId:this.#D.id,computedStyles:e,pierce:t});if(n.getError())throw n.getError();return n.nodeIds}async performSearch(e,t){const n=await this.agent.invoke_performSearch({query:e,includeUserAgentShadowDOM:t});return n.getError()||(this.#O=n.searchId),n.getError()?0:n.resultCount}async searchResult(e){if(!this.#O)return null;const{nodeIds:t}=await this.agent.invoke_getSearchResults({searchId:this.#O,fromIndex:e,toIndex:e+1});return t&&1===t.length?this.nodeForId(t[0]):null}cancelSearch(){this.#O&&(this.agent.invoke_discardSearchResults({searchId:this.#O}),this.#O=void 0)}classNamesPromise(e){return this.agent.invoke_collectClassNamesFromSubtree({nodeId:e}).then((({classNames:e})=>e||[]))}querySelector(e,t){return this.agent.invoke_querySelector({nodeId:e,selector:t}).then((({nodeId:e})=>e))}querySelectorAll(e,t){return this.agent.invoke_querySelectorAll({nodeId:e,selector:t}).then((({nodeIds:e})=>e))}getTopLayerElements(){return this.agent.invoke_getTopLayerElements().then((({nodeIds:e})=>e))}markUndoableState(e){DOMModelUndoStack.instance().markUndoableState(this,e||!1)}async nodeForLocation(e,t,n){const o=await this.agent.invoke_getNodeForLocation({x:e,y:t,includeUserAgentShadowDOM:n});return o.getError()||!o.nodeId?null:this.nodeForId(o.nodeId)}async getContainerForNode(e,t,n,o){const{nodeId:s}=await this.agent.invoke_getContainerForNode({nodeId:e,containerName:t,physicalAxes:n,logicalAxes:o});return s?this.nodeForId(s):null}pushObjectAsNodeToFrontend(e){return e.isNode()&&e.objectId?this.pushNodeToFrontend(e.objectId):Promise.resolve(null)}suspendModel(){return this.agent.invoke_disable().then((()=>this.setDocument(null)))}async resumeModel(){await this.agent.invoke_enable({})}dispose(){DOMModelUndoStack.instance().dispose(this)}parentModel(){const e=this.target().parentTarget();return e?e.model(DOMModel):null}getAgent(){return this.agent}registerNode(e){this.idToDOMNode.set(e.id,e)}}export var Events;!function(e){e.AttrModified="AttrModified",e.AttrRemoved="AttrRemoved",e.CharacterDataModified="CharacterDataModified",e.DOMMutated="DOMMutated",e.NodeInserted="NodeInserted",e.NodeRemoved="NodeRemoved",e.DocumentUpdated="DocumentUpdated",e.ChildNodeCountUpdated="ChildNodeCountUpdated",e.DistributedNodesChanged="DistributedNodesChanged",e.MarkersChanged="MarkersChanged",e.TopLayerElementsChanged="TopLayerElementsChanged"}(Events||(Events={}));class DOMDispatcher{#k;constructor(e){this.#k=e}documentUpdated(){this.#k.documentUpdated()}attributeModified({nodeId:e,name:t,value:n}){this.#k.attributeModified(e,t,n)}attributeRemoved({nodeId:e,name:t}){this.#k.attributeRemoved(e,t)}inlineStyleInvalidated({nodeIds:e}){this.#k.inlineStyleInvalidated(e)}characterDataModified({nodeId:e,characterData:t}){this.#k.characterDataModified(e,t)}setChildNodes({parentId:e,nodes:t}){this.#k.setChildNodes(e,t)}childNodeCountUpdated({nodeId:e,childNodeCount:t}){this.#k.childNodeCountUpdated(e,t)}childNodeInserted({parentNodeId:e,previousNodeId:t,node:n}){this.#k.childNodeInserted(e,t,n)}childNodeRemoved({parentNodeId:e,nodeId:t}){this.#k.childNodeRemoved(e,t)}shadowRootPushed({hostId:e,root:t}){this.#k.shadowRootPushed(e,t)}shadowRootPopped({hostId:e,rootId:t}){this.#k.shadowRootPopped(e,t)}pseudoElementAdded({parentId:e,pseudoElement:t}){this.#k.pseudoElementAdded(e,t)}pseudoElementRemoved({parentId:e,pseudoElementId:t}){this.#k.pseudoElementRemoved(e,t)}distributedNodesUpdated({insertionPointId:e,distributedNodes:t}){this.#k.distributedNodesUpdated(e,t)}topLayerElementsUpdated(){this.#k.topLayerElementsUpdated()}}let DOMModelUndoStackInstance;export class DOMModelUndoStack{#y;#S;#C;constructor(){this.#y=[],this.#S=0,this.#C=null}static instance(e={forceNew:null}){const{forceNew:t}=e;return DOMModelUndoStackInstance&&!t||(DOMModelUndoStackInstance=new DOMModelUndoStack),DOMModelUndoStackInstance}async markUndoableState(e,t){this.#C&&e!==this.#C&&(this.#C.markUndoableState(),this.#C=null),t&&this.#C===e||(this.#y=this.#y.slice(0,this.#S),this.#y.push(e),this.#S=this.#y.length,t?this.#C=e:(await e.getAgent().invoke_markUndoableState(),this.#C=null))}async undo(){if(0===this.#S)return Promise.resolve();--this.#S,this.#C=null,await this.#y[this.#S].getAgent().invoke_undo()}async redo(){if(this.#S>=this.#y.length)return Promise.resolve();++this.#S,this.#C=null,await this.#y[this.#S-1].getAgent().invoke_redo()}dispose(e){let t=0;for(let n=0;n<this.#S;++n)this.#y[n]===e&&++t;Platform.ArrayUtilities.removeElement(this.#y,e),this.#S-=t,this.#C===e&&(this.#C=null)}}SDKModel.register(DOMModel,{capabilities:Capability.DOM,autostart:!0});