@@ -6,7 +6,7 @@ import { HTMLVideoElement } from './DOM/HTMLVideoElement';
66import { HTMLAudioElement } from './DOM/HTMLAudioElement' ;
77import { XMLDocument } from './DOM/XMLDocument' ;
88import { DOMPointReadOnly , DOMPoint } from './DOM/DOMPointReadOnly' ;
9- import { Device , fromObject , View } from '@nativescript/core' ;
9+ import { Device , fromObject , Screen , View } from '@nativescript/core' ;
1010import { AbortController , AbortSignal } from '@nativescript/core/abortcontroller' ;
1111import { CanvasRenderingContext2D , WebGLRenderingContext , WebGL2RenderingContext , ImageData , ImageBitmap } from '@nativescript/canvas' ;
1212import { HTMLCollection } from './DOM/HTMLCollection' ;
@@ -112,7 +112,7 @@ function _emitOff(eventName: string, listener: Function) {
112112
113113( global as any ) . window . scrollTo = ( global as any ) . scrollTo = ( global as any ) . scrollTo || ( ( ) => ( { } ) ) ;
114114
115- if ( typeof ( global as any ) . addEventListener === 'undefined ' && typeof ( global as any ) . window ?. addEventListener === 'undefined ') {
115+ if ( typeof ( global as any ) . addEventListener !== 'function ' && typeof ( global as any ) . window ?. addEventListener !== 'function ') {
116116 ( global as any ) . window . addEventListener = ( global as any ) . addEventListener = ( eventName : string , listener ) => {
117117 if ( typeof listener !== 'function' ) {
118118 return ;
@@ -131,12 +131,20 @@ if (typeof (global as any).addEventListener === 'undefined' && typeof (global as
131131 } ;
132132}
133133
134- if ( typeof ( global as any ) . removeEventListener === 'undefined ' && typeof ( global as any ) . window ?. removeEventListener === 'undefined ') {
134+ if ( typeof ( global as any ) . removeEventListener !== 'function ' && typeof ( global as any ) . window ?. removeEventListener !== 'function ') {
135135 ( global as any ) . window . removeEventListener = ( global as any ) . removeEventListener = ( eventName : string , listener ) => {
136136 _emitOff ( eventName , listener ) ;
137137 } ;
138138}
139139
140+ // NativeScript can install global listeners before a separate window exists.
141+ // Fill each missing alias independently; preserve existing implementations.
142+ for ( const method of [ 'addEventListener' , 'removeEventListener' ] ) {
143+ const host = global as any ;
144+ if ( typeof host . window [ method ] !== 'function' ) host . window [ method ] = host [ method ] . bind ( host ) ;
145+ if ( typeof host [ method ] !== 'function' ) host [ method ] = host . window [ method ] . bind ( host . window ) ;
146+ }
147+
140148import { DOMParser as Parser } from '@xmldom/xmldom' ;
141149
142150type DOMParserSupportedType = 'application/xhtml+xml' | 'application/xml' | 'image/svg+xml' | 'text/html' | 'text/xml' ;
@@ -173,8 +181,16 @@ export class DOMParser {
173181 hostname : '' , // window.location.hostname returns the domain name of the web host
174182 pathname : '' , // window.location.pathname returns the path and filename of the current page
175183 protocol : 'https' , // window.location.protocol returns the web protocol used (http: or https:)
184+ search : '' ,
176185 assign : null , // window.location.assign loads a new document
177186} ;
187+ if ( typeof ( global as any ) . window . location . search !== 'string' ) {
188+ try {
189+ ( global as any ) . window . location . search = '' ;
190+ } catch {
191+ /* native location may be sealed */
192+ }
193+ }
178194
179195if ( ( global as any ) . document ) {
180196 ( global as any ) . document . readyState = 'complete' ;
@@ -184,9 +200,43 @@ if ((global as any).document) {
184200( global as any ) . window . onpointerdown = global . onpointerdown = ( ) => { } ;
185201
186202( global as any ) . window . setTimeout = setTimeout ;
203+ ( global as any ) . window . clearTimeout = clearTimeout ;
187204( global as any ) . window . setInterval = setInterval ;
205+ ( global as any ) . window . clearInterval = clearInterval ;
188206( global as any ) . window . requestAnimationFrame = requestAnimationFrame ;
189207( global as any ) . window . cancelAnimationFrame = cancelAnimationFrame ;
208+ const matchMediaStub = ( query ?: string ) => ( {
209+ matches : false ,
210+ media : query ?? '' ,
211+ onchange : null ,
212+ addListener ( ) { } ,
213+ removeListener ( ) { } ,
214+ addEventListener ( ) { } ,
215+ removeEventListener ( ) { } ,
216+ dispatchEvent ( ) {
217+ return false ;
218+ } ,
219+ } ) ;
220+ if ( typeof ( global as any ) . window . matchMedia !== 'function' ) ( global as any ) . window . matchMedia = matchMediaStub ;
221+ if ( typeof ( global as any ) . matchMedia !== 'function' ) ( global as any ) . matchMedia = matchMediaStub ;
222+ try {
223+ const dpr = Screen . mainScreen . scale || 1 ;
224+ ( global as any ) . window . devicePixelRatio = dpr ;
225+ if ( typeof ( global as any ) . devicePixelRatio === 'undefined' ) ( global as any ) . devicePixelRatio = dpr ;
226+ } catch {
227+ ( global as any ) . window . devicePixelRatio = ( global as any ) . window . devicePixelRatio || 1 ;
228+ }
229+ if ( ( global as any ) . window . parent == null ) ( global as any ) . window . parent = ( global as any ) . window ;
230+ if ( ( global as any ) . document ) {
231+ const docEl = ( global as any ) . document . documentElement ;
232+ if ( docEl && ! docEl . dataset ) docEl . dataset = { } ;
233+ if ( typeof ( global as any ) . document . hidden === 'undefined' ) {
234+ Object . defineProperty ( ( global as any ) . document , 'hidden' , { get : ( ) => false , configurable : true } ) ;
235+ }
236+ if ( typeof ( global as any ) . document . visibilityState === 'undefined' ) {
237+ Object . defineProperty ( ( global as any ) . document , 'visibilityState' , { get : ( ) => 'visible' , configurable : true } ) ;
238+ }
239+ }
190240( global as any ) . window . getComputedStyle = function ( element , pseudoEltOptional ) {
191241 const obj : any = { } ;
192242 obj . getPropertyValue = function ( prop ) {
0 commit comments