import { joinAdjacentText } from '#dom-css/import/adjacent-text' import { INLINE_TAGS } from '#dom-css/import/flow' import { parseStyleAttribute } from '#dom-css/import/style-attribute' import type { DesignDocument, DesignElement, DesignNode } from '#dom-css/types' import { html, parseFragment, type DefaultTreeAdapterTypes } from 'parse5' function attrsToRecord(attrs: DefaultTreeAdapterTypes.Element['attrs']): Record { const result: Record = {} for (const attr of attrs) result[attr.name] = attr.value return result } function isTextNode( node: DefaultTreeAdapterTypes.ChildNode ): node is DefaultTreeAdapterTypes.TextNode { return node.nodeName === '#text' } const NON_RENDERED_TAGS = new Set(['head', 'link', 'meta', 'script', 'style', 'template', 'title']) function isInlineElement(node: DefaultTreeAdapterTypes.ChildNode | undefined): boolean { return node !== undefined && 'tagName' in node && INLINE_TAGS.has(node.tagName.toLowerCase()) } /** * Text, and the spaces in or beside inline elements, which CSS draws among their words; spaces * between blocks lay nothing out, so they are left out. */ function keepsText( node: DefaultTreeAdapterTypes.TextNode, siblings: readonly DefaultTreeAdapterTypes.ChildNode[], index: number, inInline: boolean ): boolean { if (node.value.trim().length > 0) return true return ( node.value.length > 0 && (inInline || isInlineElement(siblings[index - 1]) || isInlineElement(siblings[index + 1])) ) } function childrenToDesignNodes( nodes: readonly DefaultTreeAdapterTypes.ChildNode[], inInline: boolean ): DesignNode[] { return joinAdjacentText( nodes .map((node, index) => childToDesignNode(node, index, nodes, inInline)) .filter((child): child is DesignNode => child !== null) ) } function childToDesignNode( node: DefaultTreeAdapterTypes.ChildNode, index: number, siblings: readonly DefaultTreeAdapterTypes.ChildNode[], inInline: boolean ): DesignNode | null { if (isTextNode(node)) { return keepsText(node, siblings, index, inInline) ? { type: 'text', text: node.value } : null } if (!('tagName' in node)) return null if (NON_RENDERED_TAGS.has(node.tagName.toLowerCase())) return null const attrs = attrsToRecord(node.attrs) const element: DesignElement = { type: 'element', // SVG names such as `clipPath` keep the case the parser gives them, which SVG needs. tagName: node.namespaceURI === html.NS.SVG ? node.tagName : node.tagName.toLowerCase(), attrs, children: childrenToDesignNodes(node.childNodes, isInlineElement(node)) } const inlineStyle = parseStyleAttribute(attrs.style) if (inlineStyle) element.inlineStyle = inlineStyle return element } /** * The elements and text of an HTML fragment as a design document, without a browser: what a CSS * runtime that styles documents elsewhere, such as the HTML engine, parses them into. Leaves out * what draws nothing (``, `