0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-25 05:02:57 +01:00

Run scripts inside teleported element in TeleportController

This commit is contained in:
Sage Abdullah 2024-01-23 18:48:35 +00:00 committed by Thibaud Colas
parent 981c1ac36a
commit a8bf1ab868

View File

@ -98,6 +98,22 @@ export class TeleportController extends Controller<HTMLTemplateElement> {
throw new Error('Invalid template content.');
}
// HACK:
// cloneNode doesn't run scripts, so we need to create new script elements
// and copy the attributes and innerHTML over. This is necessary when we're
// teleporting a template that contains legacy init code, e.g. initDateChooser.
// Only do this for inline scripts, as that's what we're expecting.
templateElement
.querySelectorAll('script:not([src], [type])')
.forEach((script) => {
const newScript = document.createElement('script');
Array.from(script.attributes).forEach((key) =>
newScript.setAttribute(key.nodeName, key.nodeValue || ''),
);
newScript.innerHTML = script.innerHTML;
script.replaceWith(newScript);
});
return templateElement;
}
}