-
-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathclient-patch-browser.ts
More file actions
54 lines (46 loc) · 1.74 KB
/
client-patch-browser.ts
File metadata and controls
54 lines (46 loc) · 1.74 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
import { BUILD, NAMESPACE } from '@app-data';
import { consoleDevInfo, H, promiseResolve, win } from '@platform';
import type * as d from '../declarations';
export const patchBrowser = (): Promise<d.CustomElementsDefineOptions> => {
// NOTE!! This fn cannot use async/await!
if (BUILD.isDev && !BUILD.isTesting) {
consoleDevInfo('Running in development mode.');
}
if (BUILD.cloneNodeFix) {
// opted-in to polyfill cloneNode() for slot polyfilled components
patchCloneNodeFix((H as any).prototype);
}
const scriptElm = BUILD.scriptDataOpts
? win.document &&
Array.from(win.document.querySelectorAll('script')).find(
(s) =>
new RegExp(`\/${NAMESPACE}(\\.esm)?\\.js($|\\?|#)`).test(s.src) ||
s.getAttribute('data-stencil-namespace') === NAMESPACE,
)
: null;
const importMeta = import.meta.url;
const opts = BUILD.scriptDataOpts ? ((scriptElm as any) || {})['data-opts'] || {} : {};
if (importMeta !== '') {
opts.resourcesUrl = new URL('.', importMeta).href;
}
return promiseResolve(opts);
};
const patchCloneNodeFix = (HTMLElementPrototype: any) => {
const nativeCloneNodeFn = HTMLElementPrototype.cloneNode;
HTMLElementPrototype.cloneNode = function (this: Node, deep: boolean) {
if (this.nodeName === 'TEMPLATE') {
return nativeCloneNodeFn.call(this, deep);
}
const clonedNode = nativeCloneNodeFn.call(this, false);
const srcChildNodes = this.childNodes;
if (deep) {
for (let i = 0; i < srcChildNodes.length; i++) {
// Node.ATTRIBUTE_NODE === 2, and checking because IE11
if (srcChildNodes[i].nodeType !== 2) {
clonedNode.appendChild(srcChildNodes[i].cloneNode(true));
}
}
}
return clonedNode;
};
};