UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 6.15 kB
{ "version": 3, "sources": ["../../src/hooks/cross-origin-isolation.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { createHigherOrderComponent } from '@wordpress/compose';\n\n/**\n * Adds crossorigin and credentialless attributes to elements as needed.\n *\n * @param {Element} el The element to modify.\n */\nfunction addCrossOriginAttributes( el ) {\n\t// Add the crossorigin attribute if missing.\n\tif ( ! el.hasAttribute( 'crossorigin' ) ) {\n\t\tel.setAttribute( 'crossorigin', 'anonymous' );\n\t}\n\n\t// For iframes, add the credentialless attribute.\n\tif ( el.nodeName === 'IFRAME' && ! el.hasAttribute( 'credentialless' ) ) {\n\t\t// Do not modify the iframed editor canvas.\n\t\tif ( el.getAttribute( 'src' )?.startsWith( 'blob:' ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tel.setAttribute( 'credentialless', '' );\n\n\t\t// Reload the iframe to ensure the new attribute is taken into account.\n\t\tconst origSrc = el.getAttribute( 'src' ) || '';\n\t\tel.setAttribute( 'src', '' );\n\t\tel.setAttribute( 'src', origSrc );\n\t}\n}\n\n// Only add the mutation observer if the site is cross-origin isolated.\nif ( window.crossOriginIsolated ) {\n\t/*\n\t * Detects dynamically added DOM nodes that are missing the `crossorigin` attribute.\n\t */\n\tconst observer = new window.MutationObserver( ( mutations ) => {\n\t\tmutations.forEach( ( mutation ) => {\n\t\t\t[ mutation.addedNodes, mutation.target ].forEach( ( value ) => {\n\t\t\t\tconst nodes =\n\t\t\t\t\tvalue instanceof window.NodeList ? value : [ value ];\n\t\t\t\tnodes.forEach( ( node ) => {\n\t\t\t\t\tconst el = node;\n\n\t\t\t\t\tif ( ! el.querySelectorAll ) {\n\t\t\t\t\t\t// Most likely a text node.\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tel.querySelectorAll(\n\t\t\t\t\t\t'img,source,script,video,link,iframe'\n\t\t\t\t\t).forEach( ( v ) => {\n\t\t\t\t\t\taddCrossOriginAttributes( v );\n\t\t\t\t\t} );\n\n\t\t\t\t\tif ( el.nodeName === 'IFRAME' ) {\n\t\t\t\t\t\tconst iframeNode = el;\n\n\t\t\t\t\t\t/*\n\t\t\t\t\t\t * Sandboxed iframes should not get modified. For example embedding a tweet served in a sandboxed\n\t\t\t\t\t\t * iframe, the tweet itself would not be modified.\n\t\t\t\t\t\t */\n\t\t\t\t\t\tconst isEmbedSandboxIframe =\n\t\t\t\t\t\t\tiframeNode.classList.contains(\n\t\t\t\t\t\t\t\t'components-sandbox'\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif ( ! isEmbedSandboxIframe ) {\n\t\t\t\t\t\t\tiframeNode.addEventListener( 'load', () => {\n\t\t\t\t\t\t\t\tif ( iframeNode.contentDocument ) {\n\t\t\t\t\t\t\t\t\tobserver.observe(\n\t\t\t\t\t\t\t\t\t\tiframeNode.contentDocument,\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tchildList: true,\n\t\t\t\t\t\t\t\t\t\t\tattributes: true,\n\t\t\t\t\t\t\t\t\t\t\tsubtree: true,\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\t[\n\t\t\t\t\t\t\t'IMG',\n\t\t\t\t\t\t\t'SOURCE',\n\t\t\t\t\t\t\t'SCRIPT',\n\t\t\t\t\t\t\t'VIDEO',\n\t\t\t\t\t\t\t'LINK',\n\t\t\t\t\t\t\t'IFRAME',\n\t\t\t\t\t\t].includes( el.nodeName )\n\t\t\t\t\t) {\n\t\t\t\t\t\taddCrossOriginAttributes( el );\n\t\t\t\t\t}\n\t\t\t\t} );\n\t\t\t} );\n\t\t} );\n\t} );\n\n\tobserver.observe( document.body, {\n\t\tchildList: true,\n\t\tattributes: true,\n\t\tsubtree: true,\n\t} );\n}\n\n// Only apply the embed preview filter when cross-origin isolated.\nif ( window.crossOriginIsolated ) {\n\tconst supportsCredentialless =\n\t\t'credentialless' in window.HTMLIFrameElement.prototype;\n\n\tconst disableEmbedPreviews = createHigherOrderComponent(\n\t\t( BlockEdit ) => ( props ) => {\n\t\t\tif ( 'core/embed' !== props.name ) {\n\t\t\t\treturn <BlockEdit { ...props } />;\n\t\t\t}\n\n\t\t\t// List of embeds that do not support a preview is from packages/block-library/src/embed/variations.js.\n\t\t\tconst previewable =\n\t\t\t\tsupportsCredentialless &&\n\t\t\t\t! [ 'facebook', 'smugmug' ].includes(\n\t\t\t\t\tprops.attributes.providerNameSlug\n\t\t\t\t);\n\n\t\t\treturn (\n\t\t\t\t<BlockEdit\n\t\t\t\t\t{ ...props }\n\t\t\t\t\tattributes={ { ...props.attributes, previewable } }\n\t\t\t\t/>\n\t\t\t);\n\t\t},\n\t\t'withDisabledEmbedPreview'\n\t);\n\n\taddFilter(\n\t\t'editor.BlockEdit',\n\t\t'media-experiments/disable-embed-previews',\n\t\tdisableEmbedPreviews\n\t);\n}\n"], "mappings": ";;;AAGA,mBAA0B;AAC1B,qBAA2C;AAiHhC;AA1GX,SAAS,yBAA0B,IAAK;AAEvC,MAAK,CAAE,GAAG,aAAc,aAAc,GAAI;AACzC,OAAG,aAAc,eAAe,WAAY;AAAA,EAC7C;AAGA,MAAK,GAAG,aAAa,YAAY,CAAE,GAAG,aAAc,gBAAiB,GAAI;AAExE,QAAK,GAAG,aAAc,KAAM,GAAG,WAAY,OAAQ,GAAI;AACtD;AAAA,IACD;AAEA,OAAG,aAAc,kBAAkB,EAAG;AAGtC,UAAM,UAAU,GAAG,aAAc,KAAM,KAAK;AAC5C,OAAG,aAAc,OAAO,EAAG;AAC3B,OAAG,aAAc,OAAO,OAAQ;AAAA,EACjC;AACD;AAGA,IAAK,OAAO,qBAAsB;AAIjC,QAAM,WAAW,IAAI,OAAO,iBAAkB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