UNPKG

@wordpress/blocks

Version:
73 lines (69 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fixCustomClassname = fixCustomClassname; exports.getHTMLRootElementClasses = getHTMLRootElementClasses; var _registration = require("../registration"); var _serializer = require("../serializer"); var _getBlockAttributes = require("./get-block-attributes"); /** * Internal dependencies */ const CLASS_ATTR_SCHEMA = { type: 'string', source: 'attribute', selector: '[data-custom-class-name] > *', attribute: 'class' }; /** * Given an HTML string, returns an array of class names assigned to the root * element in the markup. * * @param {string} innerHTML Markup string from which to extract classes. * * @return {string[]} Array of class names assigned to the root element. */ function getHTMLRootElementClasses(innerHTML) { const parsed = (0, _getBlockAttributes.parseWithAttributeSchema)(`<div data-custom-class-name>${innerHTML}</div>`, CLASS_ATTR_SCHEMA); return parsed ? parsed.trim().split(/\s+/) : []; } /** * Given a parsed set of block attributes, if the block supports custom class * names and an unknown class (per the block's serialization behavior) is * found, the unknown classes are treated as custom classes. This prevents the * block from being considered as invalid. * * @param {Object} blockAttributes Original block attributes. * @param {Object} blockType Block type settings. * @param {string} innerHTML Original block markup. * * @return {Object} Filtered block attributes. */ function fixCustomClassname(blockAttributes, blockType, innerHTML) { if (!(0, _registration.hasBlockSupport)(blockType, 'customClassName', true)) { return blockAttributes; } const modifiedBlockAttributes = { ...blockAttributes }; // To determine difference, serialize block given the known set of // attributes, with the exception of `className`. This will determine // the default set of classes. From there, any difference in innerHTML // can be considered as custom classes. const { className: omittedClassName, ...attributesSansClassName } = modifiedBlockAttributes; const serialized = (0, _serializer.getSaveContent)(blockType, attributesSansClassName); const defaultClasses = getHTMLRootElementClasses(serialized); const actualClasses = getHTMLRootElementClasses(innerHTML); const customClasses = actualClasses.filter(className => !defaultClasses.includes(className)); if (customClasses.length) { modifiedBlockAttributes.className = customClasses.join(' '); } else if (serialized) { delete modifiedBlockAttributes.className; } return modifiedBlockAttributes; } //# sourceMappingURL=fix-custom-classname.js.map