UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 4.23 kB
{ "version": 3, "sources": ["../../../src/api/raw-handling/figure-content-reducer.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { isTextContent } from '@wordpress/dom';\n\n/**\n * Whether or not the given node is figure content.\n *\n * @param {Node} node The node to check.\n * @param {Object} schema The schema to use.\n *\n * @return {boolean} True if figure content, false if not.\n */\nfunction isFigureContent( node, schema ) {\n\tconst tag = node.nodeName.toLowerCase();\n\n\t// We are looking for tags that can be a child of the figure tag, excluding\n\t// `figcaption` and any phrasing content.\n\tif ( tag === 'figcaption' || isTextContent( node ) ) {\n\t\treturn false;\n\t}\n\n\treturn tag in ( schema?.figure?.children ?? {} );\n}\n\n/**\n * Whether or not the given node can have an anchor.\n *\n * @param {Node} node The node to check.\n * @param {Object} schema The schema to use.\n *\n * @return {boolean} True if it can, false if not.\n */\nfunction canHaveAnchor( node, schema ) {\n\tconst tag = node.nodeName.toLowerCase();\n\n\treturn tag in ( schema?.figure?.children?.a?.children ?? {} );\n}\n\n/**\n * Wraps the given element in a figure element.\n *\n * @param {Element} element The element to wrap.\n * @param {Element} beforeElement The element before which to place the figure.\n */\nfunction wrapFigureContent( element, beforeElement = element ) {\n\tconst figure = element.ownerDocument.createElement( 'figure' );\n\tbeforeElement.parentNode.insertBefore( figure, beforeElement );\n\tfigure.appendChild( element );\n}\n\n/**\n * This filter takes figure content out of paragraphs, wraps it in a figure\n * element, and moves any anchors with it if needed.\n *\n * @param {Node} node The node to filter.\n * @param {Document} doc The document of the node.\n * @param {Object} schema The schema to use.\n *\n * @return {void}\n */\nexport default function figureContentReducer( node, doc, schema ) {\n\tif ( ! isFigureContent( node, schema ) ) {\n\t\treturn;\n\t}\n\n\tlet nodeToInsert = node;\n\tconst parentNode = node.parentNode;\n\n\t// If the figure content can have an anchor and its parent is an anchor with\n\t// only the figure content, take the anchor out instead of just the content.\n\tif (\n\t\tcanHaveAnchor( node, schema ) &&\n\t\tparentNode.nodeName === 'A' &&\n\t\tparentNode.childNodes.length === 1\n\t) {\n\t\tnodeToInsert = node.parentNode;\n\t}\n\n\tconst wrapper = nodeToInsert.closest( 'p,div' );\n\n\t// If wrapped in a paragraph or div, only extract if it's aligned or if\n\t// there is no text content.\n\t// Otherwise, if directly at the root, wrap in a figure element.\n\tif ( wrapper ) {\n\t\t// In jsdom-jscore, 'node.classList' can be undefined.\n\t\t// In this case, default to extract as it offers a better UI experience on mobile.\n\t\tif ( ! node.classList ) {\n\t\t\twrapFigureContent( nodeToInsert, wrapper );\n\t\t} else if (\n\t\t\tnode.classList.contains( 'alignright' ) ||\n\t\t\tnode.classList.contains( 'alignleft' ) ||\n\t\t\tnode.classList.contains( 'aligncenter' ) ||\n\t\t\t! wrapper.textContent.trim()\n\t\t) {\n\t\t\twrapFigureContent( nodeToInsert, wrapper );\n\t\t}\n\t} else {\n\t\twrapFigureContent( nodeToInsert );\n\t}\n}\n"], "mappings": ";AAGA,SAAS,qBAAqB;AAU9B,SAAS,gBAAiB,MAAM,QAAS;AACxC,QAAM,MAAM,KAAK,SAAS,YAAY;AAItC,MAAK,QAAQ,gBAAgB,cAAe,IAAK,GAAI;AACpD,WAAO;AAAA,EACR;AAEA,SAAO,QAAS,QAAQ,QAAQ,YAAY,CAAC;AAC9C;AAUA,SAAS,cAAe,MAAM,QAAS;AACtC,QAAM,MAAM,KAAK,SAAS,YAAY;AAEtC,SAAO,QAAS,QAAQ,QAAQ,UAAU,GAAG,YAAY,CAAC;AAC3D;AAQA,SAAS,kBAAmB,SAAS,gBAAgB,SAAU;AAC9D,QAAM,SAAS,QAAQ,cAAc,cAAe,QAAS;AAC7D,gBAAc,WAAW,aAAc,QAAQ,aAAc;AAC7D,SAAO,YAAa,OAAQ;AAC7B;AAYe,SAAR,qBAAuC,MAAM,KAAK,QAAS;AACjE,MAAK,CAAE,gBAAiB,MAAM,MAAO,GAAI;AACxC;AAAA,EACD;AAEA,MAAI,eAAe;AACnB,QAAM,aAAa,KAAK;A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