UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 2.18 kB
{ "version": 3, "sources": ["../../../src/api/raw-handling/heading-transformer.js"], "sourcesContent": ["/**\n * Transforms paragraphs with heading semantics into proper heading tags.\n *\n * This handles cases where content (like from Microsoft Word Online) comes in as\n * <p> tags with role=\"heading\" and aria-level attributes, and converts them to\n * proper <h1>, <h2>, etc. tags.\n *\n * @param {Node} node The node to check and potentially transform.\n */\nexport default function headingTransformer( node ) {\n\tif ( node.nodeType !== node.ELEMENT_NODE ) {\n\t\treturn;\n\t}\n\n\t// Check if this is a paragraph with heading semantics.\n\tif (\n\t\tnode.tagName === 'P' &&\n\t\tnode.getAttribute( 'role' ) === 'heading' &&\n\t\tnode.hasAttribute( 'aria-level' )\n\t) {\n\t\tconst level = parseInt( node.getAttribute( 'aria-level' ), 10 );\n\n\t\t// To ensure valid heading level (1-6).\n\t\tif ( level >= 1 && level <= 6 ) {\n\t\t\tconst headingTag = `H${ level }`;\n\t\t\tconst newHeading = node.ownerDocument.createElement( headingTag );\n\n\t\t\t// Copying all attributes except role and aria-level.\n\t\t\tArray.from( node.attributes ).forEach( ( attr ) => {\n\t\t\t\tif ( attr.name !== 'role' && attr.name !== 'aria-level' ) {\n\t\t\t\t\tnewHeading.setAttribute( attr.name, attr.value );\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\twhile ( node.firstChild ) {\n\t\t\t\tnewHeading.appendChild( node.firstChild );\n\t\t\t}\n\n\t\t\t// Replacing the paragraph with the heading.\n\t\t\tnode.parentNode.replaceChild( newHeading, node );\n\t\t}\n\t}\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASe,SAAR,mBAAqC,MAAO;AAClD,MAAK,KAAK,aAAa,KAAK,cAAe;AAC1C;AAAA,EACD;AAGA,MACC,KAAK,YAAY,OACjB,KAAK,aAAc,MAAO,MAAM,aAChC,KAAK,aAAc,YAAa,GAC/B;AACD,UAAM,QAAQ,SAAU,KAAK,aAAc,YAAa,GAAG,EAAG;AAG9D,QAAK,SAAS,KAAK,SAAS,GAAI;AAC/B,YAAM,aAAa,IAAK,KAAM;AAC9B,YAAM,aAAa,KAAK,cAAc,cAAe,UAAW;AAGhE,YAAM,KAAM,KAAK,UAAW,EAAE,QAAS,CAAE,SAAU;AAClD,YAAK,KAAK,SAAS,UAAU,KAAK,SAAS,cAAe;AACzD,qBAAW,aAAc,KAAK,MAAM,KAAK,KAAM;AAAA,QAChD;AAAA,MACD,CAAE;AAEF,aAAQ,KAAK,YAAa;AACzB,mBAAW,YAAa,KAAK,UAAW;AAAA,MACzC;AAGA,WAAK,WAAW,aAAc,YAAY,IAAK;AAAA,IAChD;AAAA,EACD;AACD;", "names": [] }