UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 5.27 kB
{ "version": 3, "sources": ["../../../src/components/iframe/get-compatibility-styles.js"], "sourcesContent": ["let compatibilityStyles = null;\n\n/**\n * Returns a list of stylesheets that target the editor canvas. A stylesheet is\n * considered targeting the editor a canvas if it contains the\n * `editor-styles-wrapper`, `wp-block`, or `wp-block-*` class selectors.\n *\n * Ideally, this hook should be removed in the future and styles should be added\n * explicitly as editor styles.\n */\nexport function getCompatibilityStyles() {\n\tif ( compatibilityStyles ) {\n\t\treturn compatibilityStyles;\n\t}\n\n\t// Only memoize the result once on load, since these stylesheets should not\n\t// change.\n\tcompatibilityStyles = Array.from( document.styleSheets ).reduce(\n\t\t( accumulator, styleSheet ) => {\n\t\t\ttry {\n\t\t\t\t// May fail for external styles.\n\t\t\t\t// eslint-disable-next-line no-unused-expressions\n\t\t\t\tstyleSheet.cssRules;\n\t\t\t} catch ( e ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\tconst { ownerNode, cssRules } = styleSheet;\n\n\t\t\t// Stylesheet is added by another stylesheet. See\n\t\t\t// https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet/ownerNode#notes.\n\t\t\tif ( ownerNode === null ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\tif ( ! cssRules ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\t// Don't try to add core WP styles. We are responsible for adding\n\t\t\t// them. This compatibility layer is only meant to add styles added\n\t\t\t// by plugins or themes.\n\t\t\tif ( ownerNode.id.startsWith( 'wp-' ) ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\t// Don't try to add styles without ID. Styles enqueued via the WP dependency system will always have IDs.\n\t\t\tif ( ! ownerNode.id ) {\n\t\t\t\treturn accumulator;\n\t\t\t}\n\n\t\t\tfunction matchFromRules( _cssRules ) {\n\t\t\t\treturn Array.from( _cssRules ).find(\n\t\t\t\t\t( {\n\t\t\t\t\t\tselectorText,\n\t\t\t\t\t\tconditionText,\n\t\t\t\t\t\tcssRules: __cssRules,\n\t\t\t\t\t} ) => {\n\t\t\t\t\t\t// If the rule is conditional then it will not have selector text.\n\t\t\t\t\t\t// Recurse into child CSS ruleset to determine selector eligibility.\n\t\t\t\t\t\tif ( conditionText ) {\n\t\t\t\t\t\t\treturn matchFromRules( __cssRules );\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\tselectorText &&\n\t\t\t\t\t\t\t( selectorText.includes(\n\t\t\t\t\t\t\t\t'.editor-styles-wrapper'\n\t\t\t\t\t\t\t) ||\n\t\t\t\t\t\t\t\tselectorText.includes( '.wp-block' ) )\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif ( matchFromRules( cssRules ) ) {\n\t\t\t\tconst isInline = ownerNode.tagName === 'STYLE';\n\n\t\t\t\tif ( isInline ) {\n\t\t\t\t\t// If the current target is inline,\n\t\t\t\t\t// it could be a dependency of an existing stylesheet.\n\t\t\t\t\t// Look for that dependency and add it BEFORE the current target.\n\t\t\t\t\tconst mainStylesCssId = ownerNode.id.replace(\n\t\t\t\t\t\t'-inline-css',\n\t\t\t\t\t\t'-css'\n\t\t\t\t\t);\n\t\t\t\t\tconst mainStylesElement =\n\t\t\t\t\t\tdocument.getElementById( mainStylesCssId );\n\t\t\t\t\tif ( mainStylesElement ) {\n\t\t\t\t\t\taccumulator.push( mainStylesElement.cloneNode( true ) );\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\taccumulator.push( ownerNode.cloneNode( true ) );\n\n\t\t\t\tif ( ! isInline ) {\n\t\t\t\t\t// If the current target is not inline,\n\t\t\t\t\t// we still look for inline styles that could be relevant for the current target.\n\t\t\t\t\t// If they exist, add them AFTER the current target.\n\t\t\t\t\tconst inlineStylesCssId = ownerNode.id.replace(\n\t\t\t\t\t\t'-css',\n\t\t\t\t\t\t'-inline-css'\n\t\t\t\t\t);\n\t\t\t\t\tconst inlineStylesElement =\n\t\t\t\t\t\tdocument.getElementById( inlineStylesCssId );\n\t\t\t\t\tif ( inlineStylesElement ) {\n\t\t\t\t\t\taccumulator.push(\n\t\t\t\t\t\t\tinlineStylesElement.cloneNode( true )\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn accumulator;\n\t\t},\n\t\t[]\n\t);\n\n\treturn compatibilityStyles;\n}\n"], "mappings": ";AAAA,IAAI,sBAAsB;AAUnB,SAAS,yBAAyB;AACxC,MAAK,qBAAsB;AAC1B,WAAO;AAAA,EACR;AAIA,wBAAsB,MAAM,KAAM,SAAS,WAAY,EAAE;AAAA,IACxD,CAAE,aAAa,eAAgB;AAC9B,UAAI;AAGH,mBAAW;AAAA,MACZ,SAAU,GAAI;AACb,eAAO;AAAA,MACR;AAEA,YAAM,EAAE,WAAW,SAAS,IAAI;AAIhC,UAAK,cAAc,MAAO;AACzB,eAAO;AAAA,MACR;AAEA,UAAK,CAAE,UAAW;AACjB,eAAO;AAAA,MACR;AAKA,UAAK,UAAU,GAAG,WAAY,KAAM,GAAI;AACvC,eAAO;AAAA,MACR;AAGA,UAAK,CAAE,UAAU,IAAK;AACrB,eAAO;AAAA,MACR;AAEA,eAAS,eAAgB,WAAY;AACpC,eAAO,MAAM,KAAM,SAAU,EAAE;AAAA,UAC9B,CAAE;AAAA,YACD;AAAA,YACA;AAAA,YACA,UAAU;AAAA,UACX,MAAO;AAGN,gBAAK,eAAgB;AACpB,qBAAO,eAAgB,UAAW;AAAA,YACnC;AAEA,mBACC,iBACE,aAAa;AAAA,cACd;AAAA,YACD,KACC,aAAa,SAAU,WAAY;AAAA,UAEtC;AAAA,QACD;AAAA,MACD;AAEA,UAAK,eAAgB,QAAS,GAAI;AACjC,cAAM,WAAW,UAAU,YAAY;AAEvC,YAAK,UAAW;AAIf,gBAAM,kBAAkB,UAAU,GAAG;AAAA,YACpC;AAAA,YACA;AAAA,UACD;AAC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