@wordpress/block-editor
Version:
103 lines (101 loc) • 3.22 kB
JavaScript
;
// packages/block-editor/src/hooks/cross-origin-isolation.js
var import_hooks = require("@wordpress/hooks");
var import_compose = require("@wordpress/compose");
var import_jsx_runtime = require("react/jsx-runtime");
function addCrossOriginAttributes(el) {
if (!el.hasAttribute("crossorigin")) {
el.setAttribute("crossorigin", "anonymous");
}
if (el.nodeName === "IFRAME" && !el.hasAttribute("credentialless")) {
if (el.getAttribute("src")?.startsWith("blob:")) {
return;
}
el.setAttribute("credentialless", "");
const origSrc = el.getAttribute("src") || "";
el.setAttribute("src", "");
el.setAttribute("src", origSrc);
}
}
if (window.crossOriginIsolated) {
const observer = new window.MutationObserver((mutations) => {
mutations.forEach((mutation) => {
[mutation.addedNodes, mutation.target].forEach((value) => {
const nodes = value instanceof window.NodeList ? value : [value];
nodes.forEach((node) => {
const el = node;
if (!el.querySelectorAll) {
return;
}
el.querySelectorAll(
"img,source,script,video,link,iframe"
).forEach((v) => {
addCrossOriginAttributes(v);
});
if (el.nodeName === "IFRAME") {
const iframeNode = el;
const isEmbedSandboxIframe = iframeNode.classList.contains(
"components-sandbox"
);
if (!isEmbedSandboxIframe) {
iframeNode.addEventListener("load", () => {
if (iframeNode.contentDocument) {
observer.observe(
iframeNode.contentDocument,
{
childList: true,
attributes: true,
subtree: true
}
);
}
});
}
}
if ([
"IMG",
"SOURCE",
"SCRIPT",
"VIDEO",
"LINK",
"IFRAME"
].includes(el.nodeName)) {
addCrossOriginAttributes(el);
}
});
});
});
});
observer.observe(document.body, {
childList: true,
attributes: true,
subtree: true
});
}
if (window.crossOriginIsolated) {
const supportsCredentialless = "credentialless" in window.HTMLIFrameElement.prototype;
const disableEmbedPreviews = (0, import_compose.createHigherOrderComponent)(
(BlockEdit) => (props) => {
if ("core/embed" !== props.name) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockEdit, { ...props });
}
const previewable = supportsCredentialless && !["facebook", "smugmug"].includes(
props.attributes.providerNameSlug
);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
BlockEdit,
{
...props,
attributes: { ...props.attributes, previewable }
}
);
},
"withDisabledEmbedPreview"
);
(0, import_hooks.addFilter)(
"editor.BlockEdit",
"media-experiments/disable-embed-previews",
disableEmbedPreviews
);
}
//# sourceMappingURL=cross-origin-isolation.cjs.map