Skip to content

Commit 970bde0

Browse files
authored
fix(polyfill): complete window timer and EventTarget aliases (#151)
NativeScript may install global.addEventListener before window exists, so the old both-undefined guard left window.addEventListener missing. Browser libraries bind window.setTimeout/clearTimeout/matchMedia and documentElement.dataset / style.setProperty. Fill each alias independently.
1 parent 6f69749 commit 970bde0

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎packages/canvas-polyfill/DOM/HTMLElement.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,26 @@ export class Style {
9696
}
9797
}
9898
}
99+
100+
setProperty(property: string, value: string): void {
101+
this._values.set(property, value);
102+
}
103+
104+
getPropertyValue(property: string): string {
105+
const value = this._values.get(property);
106+
return value == null ? '' : String(value);
107+
}
108+
109+
removeProperty(property: string): string {
110+
const value = this._values.get(property);
111+
this._values.delete(property);
112+
return value == null ? '' : String(value);
113+
}
99114
}
100115

101116
export class HTMLElement extends Element {
102117
private _style: Style;
118+
private _dataset: Record<string, string> = {};
103119

104120
constructor(tagName: string = '') {
105121
super(tagName ?? '');
@@ -113,6 +129,10 @@ export class HTMLElement extends Element {
113129
return this._style;
114130
}
115131

132+
get dataset(): Record<string, string> {
133+
return this._dataset;
134+
}
135+
116136
set nativeElement(value) {
117137
this._nativeElement = value;
118138
if (value) {

‎packages/canvas-polyfill/window.ts‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { HTMLVideoElement } from './DOM/HTMLVideoElement';
66
import { HTMLAudioElement } from './DOM/HTMLAudioElement';
77
import { XMLDocument } from './DOM/XMLDocument';
88
import { DOMPointReadOnly, DOMPoint } from './DOM/DOMPointReadOnly';
9-
import { Device, fromObject, View } from '@nativescript/core';
9+
import { Device, fromObject, Screen, View } from '@nativescript/core';
1010
import { AbortController, AbortSignal } from '@nativescript/core/abortcontroller';
1111
import { CanvasRenderingContext2D, WebGLRenderingContext, WebGL2RenderingContext, ImageData, ImageBitmap } from '@nativescript/canvas';
1212
import { 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+
140148
import { DOMParser as Parser } from '@xmldom/xmldom';
141149

142150
type 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

179195
if ((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

Comments
 (0)