Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
304 lines (278 loc) · 10.2 KB
/
Copy pathutils.js
File metadata and controls
304 lines (278 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @fileOverview Common utiltiies used by objects.
* @license ORB Software. Copyright (c) 2022 Ocean Blue Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
hbbtv.utils = (function() {
const HAS_WEAKREF_SUPPORT = (function() {
try {
const test = new WeakRef({});
return true;
} catch (e) {
console.log(
'Warning: For development on non-production browsers only (WeakRef unsupported)'
);
}
return false;
})();
function weakRef(ref) {
return ref ? (HAS_WEAKREF_SUPPORT ? new WeakRef(ref) : ref) : ref;
}
function weakDeref(ref) {
return ref ? (HAS_WEAKREF_SUPPORT ? ref.deref() : ref) : ref;
}
function createFinalizationRegistry(callback) {
return HAS_WEAKREF_SUPPORT ?
new FinalizationRegistry(callback) : {
register: () => {},
unregister: () => {},
};
}
function runOnMainLooper(thiz, runnable) {
setTimeout(runnable.bind(thiz), 0);
}
function defineConstantProperties(prototype, constants) {
for (const [key, value] of Object.entries(constants)) {
Object.defineProperty(prototype, key, {
value: value,
});
}
}
function defineGetterProperties(prototype, getters) {
for (const [key, getter] of Object.entries(getters)) {
Object.defineProperty(prototype, key, {
get: getter,
});
}
}
function defineGetterSetterProperties(prototype, props) {
for (const [key, prop] of Object.entries(props)) {
Object.defineProperty(prototype, key, {
set: prop.set,
get: prop.get,
});
}
}
function preventDefaultMediaHandling(target) {
const parent = target.parentNode;
target.setAttribute('classid', 'hbbtv');
if (parent) {
// Chrome ignores classid changes once object is already in the tree, so remove and add it back
const sibling = target.nextSibling;
parent.removeChild(target);
if (sibling) {
parent.insertBefore(target, sibling);
} else {
parent.appendChild(target);
}
}
}
function base64Encode(str) {
if (str === undefined) {
str = '';
}
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let result = '';
let length = str.length;
let i = 0;
while (i < length) {
let output0 = (str.charCodeAt(i) & 0b11111100) >> 2;
let output1 = (str.charCodeAt(i) & 0b00000011) << 4;
let output2 = 64;
let output3 = 64;
if (++i < length) {
output1 |= (str.charCodeAt(i) & 0b11110000) >> 4;
output2 = (str.charCodeAt(i) & 0b00001111) << 2;
if (++i < length) {
output2 |= (str.charCodeAt(i) & 0b11000000) >> 6;
output3 = str.charCodeAt(i) & 0b00111111;
++i;
}
}
result += table[output0];
result += table[output1];
result += table[output2];
result += table[output3];
}
return result;
}
function base64Decode(str) {
return atob(str);
}
function insertAfter(thiz, child, sibling) {
if (thiz) {
const nextSibling = sibling.nextSibling;
if (nextSibling) {
thiz.insertBefore(child, nextSibling);
} else {
thiz.appendChild(child);
}
}
}
function matchElementStyle(thiz, element) {
if (thiz) {
const style = window.getComputedStyle(element);
Array.from(style).forEach((key) =>
thiz.style.setProperty(
key,
style.getPropertyValue(key),
style.getPropertyPriority(key)
)
);
if (thiz.style.position !== 'fixed') {
thiz.style.left = element.offsetLeft + 'px';
thiz.style.top = element.offsetTop + 'px';
thiz.style.margin = 0;
thiz.style.position = 'absolute';
}
/**
* WPE-2.28 Uses object-fit: contain by default, which results into the video
* presenting with an offset on the y-axis. We need to override this default style
* in order for the video to present in the correct y-position.
*/
if (hbbtv.native.name === "rdk") {
thiz.style.objectFit = "fill";
}
}
}
const gListenerFinalization = createFinalizationRegistry(({
target,
type,
wrapper
}) => {
target.removeEventListener(type, wrapper);
});
class EventDispatcher {
constructor(target) {
this.delegate = document.createDocumentFragment();
this.wrappers = {};
this.count = 0;
this.shadow = {};
this.target = target;
}
/**
* Add a listener that will be called for this event type, unless it was collected.
*
* This method does not create a strong reference from the EventDispatcher to the listener,
* so the listener (and its references) can be collected once they become otherwise
* unreachable. Useful for events where no explicit removal method is guaranteed to be called
* later.
*
* Note: Passing in an anonymous function (for example) will probably result in listener being
* collected immediately! A reference to listener needs to be held by the caller, for example
* in an object with an associated lifetime.
*/
addWeakEventListener(type, listener) {
if (!HAS_WEAKREF_SUPPORT) {
return this.addEventListener(type, listener);
}
if (!(type in this.wrappers)) {
this.wrappers[type] = new WeakMap();
}
if (!this.wrappers[type].has(listener)) {
const weak = new WeakRef(listener);
const wrapper = (event) => {
const listener = weak.deref();
if (listener) {
listener(event);
}
};
this.wrappers[type].set(listener, wrapper);
gListenerFinalization.register(listener, {
target: this,
type,
wrapper,
});
this.addEventListener(type, wrapper);
}
}
/**
* Remove a previously added weak event listener.
*/
removeWeakEventListener(type, listener) {
if (!HAS_WEAKREF_SUPPORT) {
return this.removeEventListener(type, listener);
}
if (type in this.wrappers) {
const wrapper = this.wrappers[type].get(listener);
if (wrapper) {
this.wrappers[type].delete(listener);
gListenerFinalization.unregister(listener);
this.removeEventListener(type, wrapper);
}
}
}
/**
* Add a counted listener that will be called for this event type.
*
* Returns the total number of counted listeners currently added to this EventDispatcher.
*/
addCountedEventListener(type, listener) {
this.addEventListener(type, listener);
if (!(type in this.shadow)) {
this.shadow[type] = new WeakSet([listener]);
this.count++;
} else if (!this.shadow[type].has(listener)) {
this.shadow[type].add(listener);
this.count++;
}
return this.count;
}
/**
* Remove a previously added counted listener.
*
* Returns the total number of counted listeners currently added to this EventDispatcher.
*/
removeCountedEventListener(type, listener) {
this.removeEventListener(type, listener);
if (type in this.shadow) {
if (this.shadow[type].has(listener)) {
this.shadow[type].delete(listener);
this.count--;
}
}
return this.count;
}
addEventListener(type, listener) {
this.delegate.addEventListener(type, listener);
}
removeEventListener(type, listener) {
this.delegate.removeEventListener(type, listener);
}
dispatchEvent(event) {
if (this.target) {
Object.defineProperty(event, 'target', {
writable: false,
value: this.target,
});
}
this.delegate.dispatchEvent(event);
}
}
return {
HAS_WEAKREF_SUPPORT: HAS_WEAKREF_SUPPORT,
weakRef: weakRef,
weakDeref: weakDeref,
createFinalizationRegistry: createFinalizationRegistry,
runOnMainLooper: runOnMainLooper,
defineConstantProperties: defineConstantProperties,
defineGetterProperties: defineGetterProperties,
defineGetterSetterProperties: defineGetterSetterProperties,
preventDefaultMediaHandling: preventDefaultMediaHandling,
base64Encode: base64Encode,
base64Decode: base64Decode,
insertAfter: insertAfter,
matchElementStyle: matchElementStyle,
EventDispatcher: EventDispatcher,
};
})();