@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 3.02 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":[],"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":";;;;;;;;;;;;;;;;AAUA,SAAgB,aAAc,UAAU,IAAK;CAC5C,IAAK,CAAE,WAAW,CAAE,QAAQ,KAAK,GAChC,OAAO;EAAE,MAAM;EAAI,KAAK;EAAI,IAAI;CAAG;CAIpC,MAAM,MAAM,SAAS,eAAe,mBAAoB,EAAG;CAC3D,IAAI,KAAK,YAAY;CAGrB,MAAM,WAAW,IAAI,KAAK,cACzB,mCACD;CACA,MAAM,MAAM,WAAW,SAAS,YAAY,KAAK,IAAI;CACrD,IAAK,UACJ,SAAS,OAAO;CAIjB,MAAM,YAAY,IAAI,KAAK,cAC1B,mCACD;CACA,MAAM,KAAK,YAAY,UAAU,YAAY,KAAK,IAAI;CACtD,IAAK,WACJ,UAAU,OAAO;CAMlB,OAAO;EAAE,MAFI,IAAI,KAAK,UAAU,KAEpB;EAAG;EAAK;CAAG;AACxB;;;;;;;;;;;;;AAcA,SAAgB,iBAAkB,EAAE,OAAO,IAAI,MAAM,IAAI,KAAK,MAAO;CACpE,MAAM,QAAQ,CAAC;CAGf,IAAK,IAAI,KAAK,GACb,MAAM,KAAM,qCAAsC,IAAK,WAAY;CAIpE,IAAK,GAAG,KAAK,GACZ,MAAM,KAAM,qCAAsC,GAAI,aAAa;CAIpE,IAAK,KAAK,KAAK,GACd,MAAM,KAAM,IAAK;CAGlB,OAAO,MAAM,KAAM,MAAO;AAC3B"}