@wordpress/blocks
Version:
Block API for WordPress.
8 lines (7 loc) • 3.55 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/api/raw-handling/normalise-blocks.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { isEmpty, isPhrasingContent } from '@wordpress/dom';\n\nexport default function normaliseBlocks( HTML, options = {} ) {\n\tconst decuDoc = document.implementation.createHTMLDocument( '' );\n\tconst accuDoc = document.implementation.createHTMLDocument( '' );\n\n\tconst decu = decuDoc.body;\n\tconst accu = accuDoc.body;\n\n\tdecu.innerHTML = HTML;\n\n\twhile ( decu.firstChild ) {\n\t\tconst node = decu.firstChild;\n\n\t\t// Text nodes: wrap in a paragraph, or append to previous.\n\t\tif ( node.nodeType === node.TEXT_NODE ) {\n\t\t\tif ( isEmpty( node ) ) {\n\t\t\t\tdecu.removeChild( node );\n\t\t\t} else {\n\t\t\t\tif ( ! accu.lastChild || accu.lastChild.nodeName !== 'P' ) {\n\t\t\t\t\taccu.appendChild( accuDoc.createElement( 'P' ) );\n\t\t\t\t}\n\n\t\t\t\taccu.lastChild.appendChild( node );\n\t\t\t}\n\t\t\t// Element nodes.\n\t\t} else if ( node.nodeType === node.ELEMENT_NODE ) {\n\t\t\t// BR nodes: create a new paragraph on double, or append to previous.\n\t\t\tif ( node.nodeName === 'BR' ) {\n\t\t\t\tif ( node.nextSibling && node.nextSibling.nodeName === 'BR' ) {\n\t\t\t\t\taccu.appendChild( accuDoc.createElement( 'P' ) );\n\t\t\t\t\tdecu.removeChild( node.nextSibling );\n\t\t\t\t}\n\n\t\t\t\t// Don't append to an empty paragraph.\n\t\t\t\tif (\n\t\t\t\t\taccu.lastChild &&\n\t\t\t\t\taccu.lastChild.nodeName === 'P' &&\n\t\t\t\t\taccu.lastChild.hasChildNodes()\n\t\t\t\t) {\n\t\t\t\t\taccu.lastChild.appendChild( node );\n\t\t\t\t} else {\n\t\t\t\t\tdecu.removeChild( node );\n\t\t\t\t}\n\t\t\t} else if ( node.nodeName === 'P' ) {\n\t\t\t\t// Only append non-empty paragraph nodes.\n\t\t\t\tif ( isEmpty( node ) && ! options.raw ) {\n\t\t\t\t\tdecu.removeChild( node );\n\t\t\t\t} else {\n\t\t\t\t\taccu.appendChild( node );\n\t\t\t\t}\n\t\t\t} else if ( isPhrasingContent( node ) ) {\n\t\t\t\tif ( ! accu.lastChild || accu.lastChild.nodeName !== 'P' ) {\n\t\t\t\t\taccu.appendChild( accuDoc.createElement( 'P' ) );\n\t\t\t\t}\n\t\t\t\taccu.lastChild.appendChild( node );\n\t\t\t} else {\n\t\t\t\taccu.appendChild( node );\n\t\t\t}\n\t\t} else {\n\t\t\tdecu.removeChild( node );\n\t\t}\n\t}\n\n\treturn accu.innerHTML;\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA2C;AAE5B,SAAR,gBAAkC,MAAM,UAAU,CAAC,GAAI;AAC7D,QAAM,UAAU,SAAS,eAAe,mBAAoB,EAAG;AAC/D,QAAM,UAAU,SAAS,eAAe,mBAAoB,EAAG;AAE/D,QAAM,OAAO,QAAQ;AACrB,QAAM,OAAO,QAAQ;AAErB,OAAK,YAAY;AAEjB,SAAQ,KAAK,YAAa;AACzB,UAAM,OAAO,KAAK;AAGlB,QAAK,KAAK,aAAa,KAAK,WAAY;AACvC,cAAK,oBAAS,IAAK,GAAI;AACtB,aAAK,YAAa,IAAK;AAAA,MACxB,OAAO;AACN,YAAK,CAAE,KAAK,aAAa,KAAK,UAAU,aAAa,KAAM;AAC1D,eAAK,YAAa,QAAQ,cAAe,GAAI,CAAE;AAAA,QAChD;AAEA,aAAK,UAAU,YAAa,IAAK;AAAA,MAClC;AAAA,IAED,WAAY,KAAK,aAAa,KAAK,cAAe;AAEjD,UAAK,KAAK,aAAa,MAAO;AAC7B,YAAK,KAAK,eAAe,KAAK,YAAY,aAAa,MAAO;AAC7D,eAAK,YAAa,QAAQ,cAAe,GAAI,CAAE;AAC/C,eAA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