MCPcopy
hub / github.com/QwikDev/partytown / insertBefore

Method insertBefore

src/lib/web-worker/worker-node.ts:49–101  ·  view source on GitHub ↗
(newNode: WorkerNode, referenceNode: Node | null)

Source from the content-addressed store, hash-verified

47 set href(_: any) {}
48
49 insertBefore(newNode: WorkerNode, referenceNode: Node | null) {
50 // ensure the node being added to the window's document
51 // is given the same winId as the window it's being added to
52 const winId = (newNode[WinIdKey] = this[WinIdKey]);
53 const instanceId = newNode[InstanceIdKey];
54 const nodeName = newNode[InstanceDataKey];
55 const isScript = nodeName === NodeName.Script;
56 const isIFrame = nodeName === NodeName.IFrame;
57
58 if (isScript) {
59 const scriptContent = getInstanceStateValue<string>(newNode, StateProp.innerHTML);
60 const scriptType = getInstanceStateValue<string>(newNode, StateProp.type);
61
62 if (scriptContent) {
63 if (isScriptJsType(scriptType)) {
64 // @ts-ignore
65 const scriptId = newNode.id;
66 const loadOnMainThread =
67 scriptId && testIfMustLoadScriptOnMainThread(config, scriptId);
68
69 if (loadOnMainThread) {
70 setter(newNode, ['type'], 'text/javascript');
71 } else {
72 const errorMsg = runScriptContent(env, instanceId, scriptContent, winId, '');
73 const datasetType = errorMsg ? 'pterror' : 'ptid';
74 const datasetValue = errorMsg || instanceId;
75 setter(newNode, ['type'], SCRIPT_TYPE + SCRIPT_TYPE_EXEC);
76 setter(newNode, ['dataset', datasetType], datasetValue);
77 }
78 }
79 setter(newNode, ['innerHTML'], scriptContent);
80 }
81 }
82
83 callMethod(this, ['insertBefore'], [newNode, referenceNode], CallType.NonBlocking);
84
85 if (isIFrame) {
86 // an iframe element's instanceId is also
87 // the winId of its contentWindow
88 const src = getInstanceStateValue<string>(newNode, StateProp.src);
89 if (src && src.startsWith('javascript:')) {
90 const scriptContent = src.split('javascript:')[1];
91 runScriptContent(env, instanceId, scriptContent, winId, '');
92 }
93 insertIframe(instanceId, newNode);
94 }
95 if (isScript) {
96 sendToMain(true);
97 webWorkerCtx.$postMessage$([WorkerMessageType.InitializeNextScript, winId]);
98 }
99
100 return newNode;
101 }
102
103 get nodeName() {
104 return this[InstanceDataKey] === '#s' ? '#document-fragment' : this[InstanceDataKey];

Callers 15

appendChildMethod · 0.80
drawFunction · 0.80
renderFunction · 0.80
selection_cloneShallowFunction · 0.80
selection_cloneDeepFunction · 0.80
mermaid.jsFile · 0.80
lowerFunction · 0.80
_initDocumentFunction · 0.80
_sanitizeElementsFunction · 0.80
CFunction · 0.80
TFunction · 0.80

Calls 8

getInstanceStateValueFunction · 0.90
isScriptJsTypeFunction · 0.90
setterFunction · 0.90
runScriptContentFunction · 0.90
callMethodFunction · 0.90
insertIframeFunction · 0.90
sendToMainFunction · 0.90

Tested by

no test coverage detected