UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 3.47 kB
{ "version": 3, "sources": ["../../../src/api/parser/fix-custom-classname.js"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { hasBlockSupport } from '../registration';\nimport { getSaveContent } from '../serializer';\nimport { parseWithAttributeSchema } from './get-block-attributes';\n\nconst CLASS_ATTR_SCHEMA = {\n\ttype: 'string',\n\tsource: 'attribute',\n\tselector: '[data-custom-class-name] > *',\n\tattribute: 'class',\n};\n\n/**\n * Given an HTML string, returns an array of class names assigned to the root\n * element in the markup.\n *\n * @param {string} innerHTML Markup string from which to extract classes.\n *\n * @return {string[]} Array of class names assigned to the root element.\n */\nexport function getHTMLRootElementClasses( innerHTML ) {\n\tconst parsed = parseWithAttributeSchema(\n\t\t`<div data-custom-class-name>${ innerHTML }</div>`,\n\t\tCLASS_ATTR_SCHEMA\n\t);\n\n\treturn parsed ? parsed.trim().split( /\\s+/ ) : [];\n}\n\n/**\n * Given a parsed set of block attributes, if the block supports custom class\n * names and an unknown class (per the block's serialization behavior) is\n * found, the unknown classes are treated as custom classes. This prevents the\n * block from being considered as invalid.\n *\n * @param {Object} blockAttributes Original block attributes.\n * @param {Object} blockType Block type settings.\n * @param {string} innerHTML Original block markup.\n *\n * @return {Object} Filtered block attributes.\n */\nexport function fixCustomClassname( blockAttributes, blockType, innerHTML ) {\n\tif ( ! hasBlockSupport( blockType, 'customClassName', true ) ) {\n\t\treturn blockAttributes;\n\t}\n\n\tconst modifiedBlockAttributes = { ...blockAttributes };\n\t// To determine difference, serialize block given the known set of\n\t// attributes, with the exception of `className`. This will determine\n\t// the default set of classes. From there, any difference in innerHTML\n\t// can be considered as custom classes.\n\tconst { className: omittedClassName, ...attributesSansClassName } =\n\t\tmodifiedBlockAttributes;\n\tconst serialized = getSaveContent( blockType, attributesSansClassName );\n\tconst defaultClasses = getHTMLRootElementClasses( serialized );\n\tconst actualClasses = getHTMLRootElementClasses( innerHTML );\n\n\tconst customClasses = actualClasses.filter(\n\t\t( className ) => ! defaultClasses.includes( className )\n\t);\n\n\tif ( customClasses.length ) {\n\t\tmodifiedBlockAttributes.className = customClasses.join( ' ' );\n\t} else if ( serialized ) {\n\t\tdelete modifiedBlockAttributes.className;\n\t}\n\n\treturn modifiedBlockAttributes;\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,0BAAgC;AAChC,wBAA+B;AAC/B,kCAAyC;AAEzC,IAAM,oBAAoB;AAAA,EACzB,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,WAAW;AACZ;AAUO,SAAS,0BAA2B,WAAY;AACtD,QAAM,aAAS;AAAA,IACd,+BAAgC,SAAU;AAAA,IAC1C;AAAA,EACD;AAEA,SAAO,SAAS,OAAO,KAAK,EAAE,MAAO,KAAM,IAAI,CAAC;AACjD;AAcO,SAAS,mBAAoB,iBAAiB,WAAW,WAAY;AAC3E,MAAK,KAAE,qCAAiB,WAAW,mBAAmB,IAAK,GAAI;AAC9D,WAAO;AAAA,EACR;AAEA,QAAM,0BAA0B,EAAE,GAAG,gBAAgB;AAKrD,QAAM,EAAE,WAAW,kBAAkB,GAAG,wBAAwB,IAC/D;AACD,QAAM,iBAAa,kCAAgB,WAAW,uBAAwB;AACtE,QAAM,iBAAiB,0BAA2B,UAAW;AAC7D,QAAM,gBAAgB,0BAA2B,SAAU;AAE3D,QAAM,gBAAgB,cAAc;AAAA,IACnC,CAAE,cAAe,CAAE,eAAe,SAAU,SAAU;AAAA,EACvD;AAEA,MAAK,cAAc,QAAS;AAC3B,4BAAwB,YAAY,cAAc,KAAM,GAAI;AAAA,EAC7D,WAAY,YAAa;AACxB,WAAO,wBAAwB;AAAA,EAChC;AAEA,SAAO;AACR;", "names": [] }