@wordpress/block-library
Version:
Block library for the WordPress editor.
8 lines (7 loc) • 3.19 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/html/utils.js"],
"sourcesContent": ["/**\n * Parses content string into separate HTML, CSS, and JS sections.\n *\n * Extracts CSS from <style data-wp-block-html=\"css\"> tags and\n * JavaScript from <script data-wp-block-html=\"js\"> tags.\n * Everything else is treated as HTML.\n *\n * @param {string} content - The combined content string\n * @return {Object} Object with html, css, and js properties\n */\nexport function parseContent( content = '' ) {\n\tif ( ! content || ! content.trim() ) {\n\t\treturn { html: '', css: '', js: '' };\n\t}\n\n\t// Create a temporary document to parse HTML safely\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = content;\n\n\t// Extract CSS from marked style tag\n\tconst styleTag = doc.body.querySelector(\n\t\t'style[data-wp-block-html=\"css\"]'\n\t);\n\tconst css = styleTag ? styleTag.textContent.trim() : '';\n\tif ( styleTag ) {\n\t\tstyleTag.remove();\n\t}\n\n\t// Extract JS from marked script tag\n\tconst scriptTag = doc.body.querySelector(\n\t\t'script[data-wp-block-html=\"js\"]'\n\t);\n\tconst js = scriptTag ? scriptTag.textContent.trim() : '';\n\tif ( scriptTag ) {\n\t\tscriptTag.remove();\n\t}\n\n\t// Everything else is HTML\n\tconst html = doc.body.innerHTML.trim();\n\n\treturn { html, css, js };\n}\n\n/**\n * Serializes HTML, CSS, and JS into a single content string.\n *\n * Creates marked <style> and <script> tags for CSS and JS sections,\n * then appends the HTML content.\n *\n * @param {Object} sections Object with html, css, and js properties\n * @param {string} sections.html HTML content\n * @param {string} sections.css CSS content\n * @param {string} sections.js JavaScript content\n * @return {string} Combined content string\n */\nexport function serializeContent( { html = '', css = '', js = '' } ) {\n\tconst parts = [];\n\n\t// Add CSS if present\n\tif ( css.trim() ) {\n\t\tparts.push( `<style data-wp-block-html=\"css\">\\n${ css }\\n</style>` );\n\t}\n\n\t// Add JS if present\n\tif ( js.trim() ) {\n\t\tparts.push( `<script data-wp-block-html=\"js\">\\n${ js }\\n</script>` );\n\t}\n\n\t// Add HTML\n\tif ( html.trim() ) {\n\t\tparts.push( html );\n\t}\n\n\treturn parts.join( '\\n\\n' );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,SAAS,aAAc,UAAU,IAAK;AAC5C,MAAK,CAAE,WAAW,CAAE,QAAQ,KAAK,GAAI;AACpC,WAAO,EAAE,MAAM,IAAI,KAAK,IAAI,IAAI,GAAG;AAAA,EACpC;AAGA,QAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;AAC3D,MAAI,KAAK,YAAY;AAGrB,QAAM,WAAW,IAAI,KAAK;AAAA,IACzB;AAAA,EACD;AACA,QAAM,MAAM,WAAW,SAAS,YAAY,KAAK,IAAI;AACrD,MAAK,UAAW;AACf,aAAS,OAAO;AAAA,EACjB;AAGA,QAAM,YAAY,IAAI,KAAK;AAAA,IAC1B;AAAA,EACD;AACA,QAAM,KAAK,YAAY,UAAU,YAAY,KAAK,IAAI;AACtD,MAAK,WAAY;AAChB,cAAU,OAAO;AAAA,EAClB;AAGA,QAAM,OAAO,IAAI,KAAK,UAAU,KAAK;AAErC,SAAO,EAAE,MAAM,KAAK,GAAG;AACxB;AAcO,SAAS,iBAAkB,EAAE,OAAO,IAAI,MAAM,IAAI,KAAK,GAAG,GAAI;AACpE,QAAM,QAAQ,CAAC;AAGf,MAAK,IAAI,KAAK,GAAI;AACjB,UAAM,KAAM;AAAA,EAAsC,GAAI;AAAA,SAAa;AAAA,EACpE;AAGA,MAAK,GAAG,KAAK,GAAI;AAChB,UAAM,KAAM;AAAA,EAAsC,EAAG;AAAA,UAAc;AAAA,EACpE;AAGA,MAAK,KAAK,KAAK,GAAI;AAClB,UAAM,KAAM,IAAK;AAAA,EAClB;AAEA,SAAO,MAAM,KAAM,MAAO;AAC3B;",
"names": []
}