UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 3.48 kB
{ "version": 3, "sources": ["../../../src/api/raw-handling/html-formatting-remover.js"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { getSibling } from './utils';\n\nfunction isFormattingSpace( character ) {\n\treturn (\n\t\tcharacter === ' ' ||\n\t\tcharacter === '\\r' ||\n\t\tcharacter === '\\n' ||\n\t\tcharacter === '\\t'\n\t);\n}\n\n/**\n * Removes spacing that formats HTML.\n *\n * @see https://www.w3.org/TR/css-text-3/#white-space-processing\n *\n * @param {Node} node The node to be processed.\n * @return {void}\n */\nexport default function htmlFormattingRemover( node ) {\n\tif ( node.nodeType !== node.TEXT_NODE ) {\n\t\treturn;\n\t}\n\n\t// Ignore pre content. Note that this does not use Element#closest due to\n\t// a combination of (a) node may not be Element and (b) node.parentElement\n\t// does not have full support in all browsers (Internet Exporer).\n\t//\n\t// See: https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement#Browser_compatibility\n\n\t/** @type {Node?} */\n\tlet parent = node;\n\twhile ( ( parent = parent.parentNode ) ) {\n\t\tif (\n\t\t\tparent.nodeType === parent.ELEMENT_NODE &&\n\t\t\tparent.nodeName === 'PRE'\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\t// First, replace any sequence of HTML formatting space with a single space.\n\tlet newData = node.data.replace( /[ \\r\\n\\t]+/g, ' ' );\n\n\t// Remove the leading space if the text element is at the start of a block,\n\t// is preceded by a line break element, or has a space in the previous\n\t// node.\n\tif ( newData[ 0 ] === ' ' ) {\n\t\tconst previousSibling = getSibling( node, 'previous' );\n\n\t\tif (\n\t\t\t! previousSibling ||\n\t\t\tpreviousSibling.nodeName === 'BR' ||\n\t\t\tpreviousSibling.textContent.slice( -1 ) === ' '\n\t\t) {\n\t\t\tnewData = newData.slice( 1 );\n\t\t}\n\t}\n\n\t// Remove the trailing space if the text element is at the end of a block,\n\t// is succeeded by a line break element, or has a space in the next text\n\t// node.\n\tif ( newData[ newData.length - 1 ] === ' ' ) {\n\t\tconst nextSibling = getSibling( node, 'next' );\n\n\t\tif (\n\t\t\t! nextSibling ||\n\t\t\tnextSibling.nodeName === 'BR' ||\n\t\t\t( nextSibling.nodeType === nextSibling.TEXT_NODE &&\n\t\t\t\tisFormattingSpace( nextSibling.textContent[ 0 ] ) )\n\t\t) {\n\t\t\tnewData = newData.slice( 0, -1 );\n\t\t}\n\t}\n\n\t// If there's no data left, remove the node, so `previousSibling` stays\n\t// accurate. Otherwise, update the node data.\n\tif ( ! newData ) {\n\t\tnode.parentNode.removeChild( node );\n\t} else {\n\t\tnode.data = newData;\n\t}\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAA2B;AAE3B,SAAS,kBAAmB,WAAY;AACvC,SACC,cAAc,OACd,cAAc,QACd,cAAc,QACd,cAAc;AAEhB;AAUe,SAAR,sBAAwC,MAAO;AACrD,MAAK,KAAK,aAAa,KAAK,WAAY;AACvC;AAAA,EACD;AASA,MAAI,SAAS;AACb,SAAU,SAAS,OAAO,YAAe;AACxC,QACC,OAAO,aAAa,OAAO,gBAC3B,OAAO,aAAa,OACnB;AACD;AAAA,IACD;AAAA,EACD;AAGA,MAAI,UAAU,KAAK,KAAK,QAAS,eAAe,GAAI;AAKpD,MAAK,QAAS,CAAE,MAAM,KAAM;AAC3B,UAAM,sBAAkB,yBAAY,MAAM,UAAW;AAErD,QACC,CAAE,mBACF,gBAAgB,aAAa,QAC7B,gBAAgB,YAAY,MAAO,EAAG,MAAM,KAC3C;AACD,gBAAU,QAAQ,MAAO,CAAE;AAAA,IAC5B;AAAA,EACD;AAKA,MAAK,QAAS,QAAQ,SAAS,CAAE,MAAM,KAAM;AAC5C,UAAM,kBAAc,yBAAY,MAAM,MAAO;AAE7C,QACC,CAAE,eACF,YAAY,aAAa,QACvB,YAAY,aAAa,YAAY,aACtC,kBAAmB,YAAY,YAAa,CAAE,CAAE,GAChD;AACD,gBAAU,QAAQ,MAAO,GAAG,EAAG;AAAA,IAChC;AAAA,EACD;AAIA,MAAK,CAAE,SAAU;AAChB,SAAK,WAAW,YAAa,IAAK;AAAA,EACnC,OAAO;AACN,SAAK,OAAO;AAAA,EACb;AACD;", "names": [] }