@wordpress/block-library
Version:
Block library for the WordPress editor.
59 lines (58 loc) • 2 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// packages/block-library/src/html/utils.js
/**
* Parses content string into separate HTML, CSS, and JS sections.
*
* Extracts CSS from <style data-wp-block-html="css"> tags and
* JavaScript from <script data-wp-block-html="js"> tags.
* Everything else is treated as HTML.
*
* @param {string} content - The combined content string
* @return {Object} Object with html, css, and js properties
*/
function parseContent(content = "") {
if (!content || !content.trim()) return {
html: "",
css: "",
js: ""
};
const doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = content;
const styleTag = doc.body.querySelector("style[data-wp-block-html=\"css\"]");
const css = styleTag ? styleTag.textContent.trim() : "";
if (styleTag) styleTag.remove();
const scriptTag = doc.body.querySelector("script[data-wp-block-html=\"js\"]");
const js = scriptTag ? scriptTag.textContent.trim() : "";
if (scriptTag) scriptTag.remove();
return {
html: doc.body.innerHTML.trim(),
css,
js
};
}
/**
* Serializes HTML, CSS, and JS into a single content string.
*
* Creates marked <style> and <script> tags for CSS and JS sections,
* then appends the HTML content.
*
* @param {Object} sections Object with html, css, and js properties
* @param {string} sections.html HTML content
* @param {string} sections.css CSS content
* @param {string} sections.js JavaScript content
* @return {string} Combined content string
*/
function serializeContent({ html = "", css = "", js = "" }) {
const parts = [];
if (css.trim()) parts.push(`<style data-wp-block-html="css">\n${css}\n</style>`);
if (js.trim()) parts.push(`<script data-wp-block-html="js">\n${js}\n<\/script>`);
if (html.trim()) parts.push(html);
return parts.join("\n\n");
}
exports.parseContent = parseContent;
exports.serializeContent = serializeContent;
//# sourceMappingURL=utils.cjs.map