1616
1717import { ParsedSelector , parseSelector } from '../../server/common/selectorParser' ;
1818import type InjectedScript from '../../server/injected/injectedScript' ;
19- import { html } from './html' ;
2019
2120export class ConsoleAPI {
2221 private _injectedScript : InjectedScript ;
23- private _highlightContainer : Element ;
2422
2523 constructor ( injectedScript : InjectedScript ) {
2624 this . _injectedScript = injectedScript ;
27- this . _highlightContainer = html `< div style ="position: absolute; left: 0; top: 0; pointer-events: none; overflow: visible; z-index: 10000; "> </ div > ` ;
2825 ( window as any ) . playwright = {
2926 $ : ( selector : string ) => this . _querySelector ( selector ) ,
3027 $$ : ( selector : string ) => this . _querySelectorAll ( selector ) ,
3128 inspect : ( selector : string ) => this . _inspect ( selector ) ,
32- clear : ( ) => this . _clearHighlight ( )
3329 } ;
3430 }
3531
@@ -40,36 +36,12 @@ export class ConsoleAPI {
4036 }
4137 }
4238
43- private _highlightElements ( elements : Element [ ] = [ ] , target ?: Element ) {
44- const scrollLeft = document . scrollingElement ? document . scrollingElement . scrollLeft : 0 ;
45- const scrollTop = document . scrollingElement ? document . scrollingElement . scrollTop : 0 ;
46- this . _highlightContainer . textContent = '' ;
47- for ( const element of elements ) {
48- const rect = element . getBoundingClientRect ( ) ;
49- const highlight = html `< div style ="position: absolute; pointer-events: none; border-radius: 3px "> </ div > ` ;
50- highlight . style . left = ( rect . left + scrollLeft ) + 'px' ;
51- highlight . style . top = ( rect . top + scrollTop ) + 'px' ;
52- highlight . style . height = rect . height + 'px' ;
53- highlight . style . width = rect . width + 'px' ;
54- if ( element === target ) {
55- highlight . style . background = 'hsla(30, 97%, 37%, 0.3)' ;
56- highlight . style . border = '3px solid hsla(30, 97%, 37%, 0.6)' ;
57- } else {
58- highlight . style . background = 'hsla(120, 100%, 37%, 0.3)' ;
59- highlight . style . border = '3px solid hsla(120, 100%, 37%, 0.8)' ;
60- }
61- this . _highlightContainer . appendChild ( highlight ) ;
62- }
63- document . body . appendChild ( this . _highlightContainer ) ;
64- }
65-
6639 _querySelector ( selector : string ) : ( Element | undefined ) {
6740 if ( typeof selector !== 'string' )
6841 throw new Error ( `Usage: playwright.query('Playwright >> selector').` ) ;
6942 const parsed = parseSelector ( selector ) ;
7043 this . _checkSelector ( parsed ) ;
7144 const elements = this . _injectedScript . querySelectorAll ( parsed , document ) ;
72- this . _highlightElements ( elements , elements [ 0 ] ) ;
7345 return elements [ 0 ] ;
7446 }
7547
@@ -79,7 +51,6 @@ export class ConsoleAPI {
7951 const parsed = parseSelector ( selector ) ;
8052 this . _checkSelector ( parsed ) ;
8153 const elements = this . _injectedScript . querySelectorAll ( parsed , document ) ;
82- this . _highlightElements ( elements ) ;
8354 return elements ;
8455 }
8556
@@ -88,11 +59,6 @@ export class ConsoleAPI {
8859 return ;
8960 if ( typeof selector !== 'string' )
9061 throw new Error ( `Usage: playwright.inspect('Playwright >> selector').` ) ;
91- this . _highlightElements ( ) ;
9262 ( window as any ) . inspect ( this . _querySelector ( selector ) ) ;
9363 }
94-
95- _clearHighlight ( ) {
96- this . _highlightContainer . remove ( ) ;
97- }
9864}
0 commit comments