11import { parseGetMatchedStylesForNodeResponse } from "@devtoolcss/parser" ;
22import { CDPNodeType } from "./constants.js" ;
3- import { InspectorElement , InspectorNode } from "./InspectorDOM.js" ;
3+ import {
4+ InspectorNode ,
5+ InspectorElement ,
6+ InspectorDocument ,
7+ } from "./InspectorDOM.js" ;
48import EventEmitter from "./EventEmitter.js" ;
59import type { ParseOptions , ParsedCSS } from "@devtoolcss/parser" ;
610import {
@@ -40,24 +44,20 @@ export type ComputedStyleOptions = {
4044 raw ?: boolean ;
4145} ;
4246
43- function nsResolver ( prefix ) {
44- const ns = {
45- svg : "http://www.w3.org/2000/svg" ,
46- xhtml : "http://www.w3.org/1999/xhtml" ,
47- } ;
48- return ns [ prefix ] || null ;
49- }
50-
5147// we need EventEmitter for warning events, which can happen
5248// anytime event fired
5349export class Inspector extends EventEmitter {
5450 readonly documentImpl : DOMImplementation ;
5551 protected eventTimeout : number ;
5652
57- protected document : Document ;
53+ protected RootDocument : Document ;
5854
5955 protected selectedNode : InspectorNode | undefined ;
6056
57+ get document ( ) : InspectorDocument {
58+ return InspectorDocument . get ( this . RootDocument ) ;
59+ }
60+
6161 /**
6262 * @experimental
6363 */
@@ -68,48 +68,22 @@ export class Inspector extends EventEmitter {
6868 return undefined ;
6969 }
7070
71+ /* legacy forwardings */
72+
7173 querySelector ( selector : string ) : InspectorElement | null {
72- const el = this . document . querySelector ( selector ) ;
73- return el ? InspectorElement . get ( el ) : null ;
74+ return this . document . querySelector ( selector ) ;
7475 }
7576
7677 querySelectorAll ( selector : string ) : InspectorElement [ ] {
77- return Array . from ( this . document . querySelectorAll ( selector ) ) . map (
78- InspectorElement . get ,
79- ) ;
78+ return this . document . querySelectorAll ( selector ) ;
8079 }
8180
8281 queryXPath ( xpath : string ) : InspectorNode | null {
83- const result = this . document . evaluate (
84- xpath ,
85- this . document ,
86- nsResolver ,
87- 9 , //XPathResult.FIRST_ORDERED_NODE_TYPE
88- null ,
89- ) ;
90- const node = result . singleNodeValue ;
91- return node ? InspectorNode . get ( node ) : null ;
82+ return this . document . queryXPath ( xpath ) ;
9283 }
9384
9485 queryXPathAll ( xpath : string ) : InspectorNode [ ] {
95- const result = this . document . evaluate (
96- xpath ,
97- this . document ,
98- nsResolver ,
99- 7 , //XPathResult.ORDERED_NODE_SNAPSHOT_TYPE
100- null ,
101- ) ;
102- const nodes : InspectorNode [ ] = [ ] ;
103- for ( let i = 0 ; i < result . snapshotLength ; i ++ ) {
104- const node = result . snapshotItem ( i ) ;
105- if ( node ) {
106- const inspectorNode = InspectorNode . get ( node ) ;
107- if ( inspectorNode ) {
108- nodes . push ( inspectorNode ) ;
109- }
110- }
111- }
112- return nodes ;
86+ return this . document . queryXPathAll ( xpath ) ;
11387 }
11488
11589 protected idToNode = new Map < number , InspectorNode > ( ) ;
@@ -256,7 +230,7 @@ export class Inspector extends EventEmitter {
256230 switch ( cdpNode . nodeType ) {
257231 case CDPNodeType . ELEMENT_NODE :
258232 // iframe is safe because no children (not setting pierce)
259- docNode = this . document . createElement ( cdpNode . localName ) ;
233+ docNode = this . RootDocument . createElement ( cdpNode . localName ) ;
260234
261235 if ( Array . isArray ( cdpNode . attributes ) ) {
262236 for ( let i = 0 ; i < cdpNode . attributes . length ; i += 2 ) {
@@ -269,17 +243,17 @@ export class Inspector extends EventEmitter {
269243 break ;
270244
271245 case CDPNodeType . TEXT_NODE :
272- docNode = this . document . createTextNode ( cdpNode . nodeValue || "" ) ;
246+ docNode = this . RootDocument . createTextNode ( cdpNode . nodeValue || "" ) ;
273247 break ;
274248
275249 case CDPNodeType . COMMENT_NODE :
276- docNode = this . document . createComment ( cdpNode . nodeValue || "" ) ;
250+ docNode = this . RootDocument . createComment ( cdpNode . nodeValue || "" ) ;
277251 break ;
278252
279253 case CDPNodeType . DOCUMENT_NODE :
280- this . document = this . documentImpl . createHTMLDocument ( ) ;
281- this . document . removeChild ( this . document . documentElement ) ;
282- docNode = this . document ;
254+ this . RootDocument = this . documentImpl . createHTMLDocument ( ) ;
255+ this . RootDocument . removeChild ( this . RootDocument . documentElement ) ;
256+ docNode = this . RootDocument ;
283257 break ;
284258
285259 default :
@@ -294,12 +268,19 @@ export class Inspector extends EventEmitter {
294268 }
295269 }
296270
297- // The only place to new InspectorNode/Element
298- const node =
299- docNode . nodeType === CDPNodeType . ELEMENT_NODE ||
300- docNode . nodeType === CDPNodeType . DOCUMENT_NODE
301- ? new InspectorElement ( docNode as Element , cdpNode , this )
302- : new InspectorNode ( docNode , cdpNode , this ) ;
271+ // The only place to new InspectorNode/Element/Document
272+ let node : InspectorNode ;
273+ switch ( cdpNode . nodeType ) {
274+ case CDPNodeType . DOCUMENT_NODE :
275+ node = new InspectorDocument ( docNode as Document , cdpNode , this ) ;
276+ ( node as InspectorDocument ) . remove ;
277+ break ;
278+ case CDPNodeType . ELEMENT_NODE :
279+ node = new InspectorElement ( docNode as Element , cdpNode , this ) ;
280+ break ;
281+ default :
282+ node = new InspectorNode ( docNode , cdpNode , this ) ;
283+ }
303284 this . setMap ( cdpNode . nodeId , node ) ;
304285
305286 return docNode ;
0 commit comments