{
  "version": 3,
  "sources": ["../../../src/api/raw-handling/figure-content-reducer.ts"],
  "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   The node to check.\n * @param schema The schema to use.\n *\n * @return True if figure content, false if not.\n */\nfunction isFigureContent( node: Node, schema: Record< string, unknown > ) {\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 as any )?.children ?? {} );\n}\n\n/**\n * Whether or not the given node can have an anchor.\n *\n * @param node   The node to check.\n * @param schema The schema to use.\n *\n * @return True if it can, false if not.\n */\nfunction canHaveAnchor( node: Node, schema: Record< string, unknown > ) {\n\tconst tag = node.nodeName.toLowerCase();\n\n\treturn tag in ( ( schema?.figure as any )?.children?.a?.children ?? {} );\n}\n\n/**\n * Wraps the given element in a figure element.\n *\n * @param element       The element to wrap.\n * @param beforeElement The element before which to place the figure.\n */\nfunction wrapFigureContent( element: Node, beforeElement: Node = 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   The node to filter.\n * @param doc    The document of the node.\n * @param schema The schema to use.\n */\nexport default function figureContentReducer(\n\tnode: Node,\n\tdoc: Document,\n\tschema?: Record< string, unknown >\n) {\n\tif ( ! schema || ! 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 as HTMLElement ).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\tconst element = node as HTMLElement;\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 ( ! element.classList ) {\n\t\t\twrapFigureContent( nodeToInsert, wrapper );\n\t\t} else if (\n\t\t\telement.classList.contains( 'alignright' ) ||\n\t\t\telement.classList.contains( 'alignleft' ) ||\n\t\t\telement.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,MAAY,QAAoC;AACzE,QAAM,MAAM,KAAK,SAAS,YAAY;AAItC,MAAK,QAAQ,gBAAgB,cAAe,IAAK,GAAI;AACpD,WAAO;AAAA,EACR;AAEA,SAAO,QAAW,QAAQ,QAAiB,YAAY,CAAC;AACzD;AAUA,SAAS,cAAe,MAAY,QAAoC;AACvE,QAAM,MAAM,KAAK,SAAS,YAAY;AAEtC,SAAO,QAAW,QAAQ,QAAiB,UAAU,GAAG,YAAY,CAAC;AACtE;AAQA,SAAS,kBAAmB,SAAe,gBAAsB,SAAU;AAC1E,QAAM,SAAS,QAAQ,cAAe,cAAe,QAAS;AAC9D,gBAAc,WAAY,aAAc,QAAQ,aAAc;AAC9D,SAAO,YAAa,OAAQ;AAC7B;AAUe,SAAR,qBACN,MACA,KACA,QACC;AACD,MAAK,CAAE,UAAU,CAAE,gBAAiB,MAAM,MAAO,GAAI;AACpD;AAAA,EACD;AAE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