|
| 1 | +// dom_polyfill.js |
| 2 | +// |
| 3 | +// Minimal browser/DOM globals for the Babylon Native Playground JS host. |
| 4 | +// Provides TextEncoder and PointerEvent on runtimes (older Chakra) that do |
| 5 | +// not ship them natively. AbortController is provided by a native polyfill. |
| 6 | +// Each shim is self-detecting and no-ops if the symbol already exists. |
| 7 | +// |
| 8 | +// `document` is shimmed by validation_native.js once it has loaded the |
| 9 | +// config, since the runner needs to inject test-specific behaviour into |
| 10 | +// createElement. |
| 11 | + |
| 12 | +(function () { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + // Chakra has no `globalThis`; use the Function-constructor trick. |
| 16 | + var g = (new Function('return this'))(); |
| 17 | + |
| 18 | + if (typeof g.TextEncoder === 'undefined') { |
| 19 | + function TextEncoder() {} |
| 20 | + Object.defineProperty(TextEncoder.prototype, 'encoding', { |
| 21 | + get: function () { return 'utf-8'; }, |
| 22 | + configurable: true, |
| 23 | + }); |
| 24 | + TextEncoder.prototype.encode = function (input) { |
| 25 | + var str = input === undefined ? '' : String(input); |
| 26 | + var bytes = []; |
| 27 | + for (var i = 0; i < str.length; i++) { |
| 28 | + var code = str.charCodeAt(i); |
| 29 | + if (code >= 0xD800 && code <= 0xDBFF && i + 1 < str.length) { |
| 30 | + var c2 = str.charCodeAt(i + 1); |
| 31 | + if (c2 >= 0xDC00 && c2 <= 0xDFFF) { |
| 32 | + code = 0x10000 + (((code & 0x3FF) << 10) | (c2 & 0x3FF)); |
| 33 | + i++; |
| 34 | + } |
| 35 | + } |
| 36 | + if (code < 0x80) { |
| 37 | + bytes.push(code); |
| 38 | + } else if (code < 0x800) { |
| 39 | + bytes.push(0xC0 | (code >> 6)); |
| 40 | + bytes.push(0x80 | (code & 0x3F)); |
| 41 | + } else if (code < 0x10000) { |
| 42 | + bytes.push(0xE0 | (code >> 12)); |
| 43 | + bytes.push(0x80 | ((code >> 6) & 0x3F)); |
| 44 | + bytes.push(0x80 | (code & 0x3F)); |
| 45 | + } else { |
| 46 | + bytes.push(0xF0 | (code >> 18)); |
| 47 | + bytes.push(0x80 | ((code >> 12) & 0x3F)); |
| 48 | + bytes.push(0x80 | ((code >> 6) & 0x3F)); |
| 49 | + bytes.push(0x80 | (code & 0x3F)); |
| 50 | + } |
| 51 | + } |
| 52 | + return new Uint8Array(bytes); |
| 53 | + }; |
| 54 | + TextEncoder.prototype.encodeInto = function (input, dest) { |
| 55 | + var arr = this.encode(input); |
| 56 | + var n = Math.min(arr.length, dest.length); |
| 57 | + for (var i = 0; i < n; i++) dest[i] = arr[i]; |
| 58 | + return { read: String(input).length, written: n }; |
| 59 | + }; |
| 60 | + g.TextEncoder = TextEncoder; |
| 61 | + } |
| 62 | + |
| 63 | + if (typeof g.PointerEvent === 'undefined') { |
| 64 | + function PointerEvent(type, init) { |
| 65 | + init = init || {}; |
| 66 | + this.type = String(type || ''); |
| 67 | + this.bubbles = !!init.bubbles; |
| 68 | + this.cancelable = init.cancelable !== false; |
| 69 | + this.composed = !!init.composed; |
| 70 | + this.defaultPrevented = false; |
| 71 | + this.target = init.target || null; |
| 72 | + this.currentTarget = null; |
| 73 | + this.timeStamp = Date.now(); |
| 74 | + this.pointerId = init.pointerId !== undefined ? init.pointerId : 0; |
| 75 | + this.width = init.width !== undefined ? init.width : 1; |
| 76 | + this.height = init.height !== undefined ? init.height : 1; |
| 77 | + this.pressure = init.pressure !== undefined ? init.pressure : 0.5; |
| 78 | + this.tangentialPressure = init.tangentialPressure || 0; |
| 79 | + this.tiltX = init.tiltX || 0; |
| 80 | + this.tiltY = init.tiltY || 0; |
| 81 | + this.twist = init.twist || 0; |
| 82 | + this.altitudeAngle = init.altitudeAngle || 0; |
| 83 | + this.azimuthAngle = init.azimuthAngle || 0; |
| 84 | + this.pointerType = init.pointerType || 'mouse'; |
| 85 | + this.isPrimary = init.isPrimary !== false; |
| 86 | + this.clientX = init.clientX || 0; |
| 87 | + this.clientY = init.clientY || 0; |
| 88 | + this.offsetX = init.offsetX || 0; |
| 89 | + this.offsetY = init.offsetY || 0; |
| 90 | + this.pageX = init.pageX || 0; |
| 91 | + this.pageY = init.pageY || 0; |
| 92 | + this.screenX = init.screenX || 0; |
| 93 | + this.screenY = init.screenY || 0; |
| 94 | + this.movementX = init.movementX || 0; |
| 95 | + this.movementY = init.movementY || 0; |
| 96 | + this.button = init.button || 0; |
| 97 | + this.buttons = init.buttons || 0; |
| 98 | + this.relatedTarget = init.relatedTarget || null; |
| 99 | + this.ctrlKey = !!init.ctrlKey; |
| 100 | + this.shiftKey = !!init.shiftKey; |
| 101 | + this.altKey = !!init.altKey; |
| 102 | + this.metaKey = !!init.metaKey; |
| 103 | + this.detail = init.detail || 0; |
| 104 | + this.view = init.view || null; |
| 105 | + } |
| 106 | + PointerEvent.prototype.preventDefault = function () { this.defaultPrevented = true; }; |
| 107 | + PointerEvent.prototype.stopPropagation = function () {}; |
| 108 | + PointerEvent.prototype.stopImmediatePropagation = function () {}; |
| 109 | + PointerEvent.prototype.getModifierState = function () { return false; }; |
| 110 | + PointerEvent.prototype.getCoalescedEvents = function () { return []; }; |
| 111 | + PointerEvent.prototype.getPredictedEvents = function () { return []; }; |
| 112 | + g.PointerEvent = PointerEvent; |
| 113 | + } |
| 114 | +})(); |
0 commit comments