/** * FIXME: remove this comments and import when below issue is fixed. * This import is necessary for type generation due to a bug in the TypeScript compiler. * See: https://github.com/microsoft/TypeScript/issues/42873 */ // eslint-disable-next-line @typescript-eslint/no-unused-vars import type { TSESLint } from "@typescript-eslint/utils"; import type { TSESTree as T } from "@typescript-eslint/utils"; import { parse, parseFragment } from "parse5"; import type { DefaultTreeAdapterMap } from "parse5"; import { isValidHTMLNesting } from "validate-html-nesting"; import { createRule } from "../compat"; import { isDOMElementName, jsxPropName } from "../utils"; type MessageIds = "invalidNesting" | "invalidNestingInserted"; type Options = []; /** * `` from a parsed document, used as the parsing context so fragments are * interpreted the way `innerHTML` would interpret them. */ type ParentNode = DefaultTreeAdapterMap["parentNode"]; type ChildNode = DefaultTreeAdapterMap["childNode"]; const htmlElement = parse("") .childNodes[1] as ParentNode; const bodyContext = htmlElement.childNodes[1] as ParentNode; type Namespace = "html" | "svg" | "math"; const rewriteCache = new Map(); const tableWrappers: Record = { caption: ["table"], colgroup: ["table"], tbody: ["table"], thead: ["table"], tfoot: ["table"], tr: ["table", "tbody"], td: ["table", "tbody", "tr"], th: ["table", "tbody", "tr"], }; /** Void elements take no end tag, and the parser would read `
` as another `
`. */ const voidElements = new Set([ "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr", ]); /** parse5 lowercases tag names outside SVG, so `` comes back as `foreignobject`. */ const isTag = (node: ChildNode, tag: string): boolean => node.nodeName.toLowerCase() === tag.toLowerCase(); /** * Does the HTML parser refuse to keep `child` as a direct child of `parent`? * * `validate-html-nesting` alone is not enough to answer this. Its tables encode * the spec's *content model* — `
` belongs in `
`, `` * in `
`, `` takes only `` — and conformance is a different * question from what the parser does. A browser keeps `
` * exactly as written; it only rearranges a narrower set of cases. Reporting the * conformance answer under a message that talks about the browser rewriting the * DOM would be a false positive at error level. * * So the parser has the final say: the pair is only invalid when parse5, which * implements the HTML parsing algorithm and is what Solid's own compiler uses to * validate templates, fails to give back the same parent/child relationship. * The library stays as the cheap pre-filter that decides when to ask. */ const parserRewrites = (parent: string, child: string, namespace: Namespace): boolean => { const key = `${namespace}:${parent}>${child}`; const cached = rewriteCache.get(key); if (cached !== undefined) return cached; // A body fragment discards table-section tags without their table ancestors. Foreign // content is parsed inside its `` or ``, where those are plain elements. const foreign = namespace !== "html"; const wrappers = foreign ? [namespace] : tableWrappers[parent] ?? []; const documentParent = !foreign && (parent === "head" || parent === "body"); const childMarkup = voidElements.has(child) ? `<${child}>` : `<${child}>`; const markup = wrappers.reduceRight( (content, tag) => `<${tag}>${content}`, `<${parent}>${childMarkup}` ); let fragment: ParentNode | undefined = documentParent ? (parse(markup).childNodes.find((node) => node.nodeName === "html") as ParentNode | undefined) : parseFragment(bodyContext, markup, {}); for (const tag of wrappers) { fragment = fragment?.childNodes.find((node) => node.nodeName === tag) as ParentNode | undefined; } const roots = (fragment?.childNodes ?? []).filter((n) => n.nodeName !== "#text"); const root = documentParent ? roots.find((node) => node.nodeName === parent) : roots.length === 1 && isTag(roots[0], parent) ? roots[0] : undefined; // `
` renders the right side in place; `
&& cond` doesn't render it. if (parent.right !== current) return null; current = parent; parent = parent.parent; break; case "ConditionalExpression": if (parent.test === current) return null; current = parent; parent = parent.parent; break; default: if (!isTransparent(parent)) return null; current = parent; parent = parent.parent; } } return null; }; /** * Does `node` reach `parent` through an expression container or a `children` * prop? Those compile to an `insert()`, which the client performs as written; * only server-rendered HTML goes back through the parser. */ const isInserted = (node: T.JSXElement, parent: T.JSXElement): boolean => { let current: T.Node | undefined = node.parent; while (current && current !== parent) { if (current.type === "JSXExpressionContainer" || current.type === "JSXAttribute") return true; current = current.parent; } return false; }; /** SVG elements whose children the parser reads as HTML again, by lowercase name. */ const svgIntegrationPoints = new Set(["foreignobject", "desc", "title"]); /** MathML elements whose children the parser reads as HTML again. */ const mathIntegrationPoints = new Set(["mi", "mo", "mn", "ms", "mtext"]); /** MathML tags that stay MathML right inside those, children included. */ const mathTextLeaves = new Set(["mglyph", "malignmark"]); /** * HTML tags that make the parser leave SVG or MathML wherever they appear in it. * `` only does with some attributes, so it's checked on its own below. */ const foreignContentExits = new Set([ "b", "big", "blockquote", "body", "br", "center", "code", "dd", "div", "dl", "dt", "em", "embed", "h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "i", "img", "li", "listing", "menu", "meta", "nobr", "ol", "p", "pre", "ruby", "s", "small", "span", "strike", "strong", "sub", "sup", "table", "tt", "u", "ul", "var", ]); /** `` leaves foreign content when it has one of these attributes. */ const fontExitAttributes = new Set(["color", "face", "size"]); /** `` hands its children back to HTML with one of these encodings. */ const htmlAnnotationEncodings = new Set(["text/html", "application/xhtml+xml"]); /** * Does the parser read this element's children as HTML, although the element sits in * SVG or MathML? Integration points only count in their own namespace: `` is * one in SVG and a plain element in MathML, and `<mi>` the other way around. */ const switchesToHTML = ( element: T.JSXElement, tag: string, namespace: Exclude<Namespace, "html"> ): boolean => { if (foreignContentExits.has(tag)) return true; const { attributes } = element.openingElement; if (tag === "font") { return attributes.some( (attr) => attr.type === "JSXAttribute" && fontExitAttributes.has(jsxPropName(attr).toLowerCase()) ); } if (namespace === "svg") return svgIntegrationPoints.has(tag); if (tag === "annotation-xml") { // The parser lowercases attribute names and keeps the first of a repeated one. const encoding = attributes.find( (attr): attr is T.JSXAttribute => attr.type === "JSXAttribute" && jsxPropName(attr).toLowerCase() === "encoding" )?.value; return ( encoding?.type === "Literal" && typeof encoding.value === "string" && htmlAnnotationEncodings.has(encoding.value.toLowerCase()) ); } return mathIntegrationPoints.has(tag); }; /** * Does the parser start SVG or MathML at this `<svg>` or `<math>`, written right inside * `parentTag`? It does coming from HTML, and for `<svg>` in MathML's `<annotation-xml>`. * Anywhere else in foreign content the tag is a plain element of the namespace around * it, so the `<mi>` in `<svg><math><mi>` is an SVG element and keeps its children. */ const startsNamespace = (tag: string, namespace: Namespace, parentTag: string): boolean => namespace === "html" || (tag === "svg" && namespace === "math" && parentTag === "annotation-xml"); /** * The namespace the parser is in when it reaches this element's start tag: SVG or * MathML below an `<svg>` or `<math>` that starts it, HTML again below an integration * point like `<foreignObject>` or a tag that leaves foreign content like `<p>`. * Inside `<svg>`, `<a><a /></a>` and `<title><b />` are kept as written, so * judging them as HTML would report markup that is fine. * * The walk crosses expressions and components alike, although the compiler starts * a new template there. A wrong guess here can only hide a report, never add one. * An `` or `` right behind one is the root of that template, which starts * its own namespace. */ const getParsingNamespace = (node: T.JSXElement): Namespace => { // The element itself comes along, since `` can set its own namespace. const elements: Array<[T.JSXElement, string]> = []; for (let current: T.Node | undefined = node; current; current = current.parent) { if (current.type !== "JSXElement") continue; const tag = getDOMTagName(current); // The tokenizer lowercases tag names before any of the checks below. if (tag) elements.push([current, tag.toLowerCase()]); } // Outermost first, since whether an element switches depends on the namespace it is in. let namespace: Namespace = "html"; let parent: [T.JSXElement, string, Namespace] | undefined; for (const [element, tag] of elements.reverse()) { // Anything between two elements, like an expression or a component, is a boundary. const outer = parent && element.parent === parent[0] ? parent : undefined; // The namespace this element's own start tag is parsed in. const own: Namespace = outer && outer[2] === "math" && mathIntegrationPoints.has(outer[1]) && mathTextLeaves.has(tag) ? "math" : namespace; if (element === node) return own; if (tag === "svg" || tag === "math") { namespace = !outer || startsNamespace(tag, own, outer[1]) ? tag : own; } else { namespace = own !== "html" && switchesToHTML(element, tag, own) ? "html" : own; } parent = [element, tag, own]; } return namespace; }; export default createRule({ meta: { type: "problem", docs: { description: "Disallow HTML nesting the browser refuses to keep, like `

`, which silently renders a different DOM than the JSX describes.", url: "https://github.com/solidjs/eslint-plugin-solid/blob/main/packages/eslint-plugin-solid/docs/validate-jsx-nesting.md", }, schema: [], messages: { invalidNesting: "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The HTML parser restructures this, so the rendered DOM won't match this markup.", invalidNestingInserted: "Invalid HTML nesting: `<{{child}}>` cannot be a child of `<{{parent}}>`. The client inserts it as written, but the HTML parser restructures it in server-rendered HTML, so hydration won't match this markup.", }, }, defaultOptions: [], create(context) { const check = (node: T.JSXElement, child: string, parentNode: T.JSXElement) => { const parent = getDOMTagName(parentNode); if (!parent) return; if (isValidHTMLNesting(parent, child)) return; if (!parserRewrites(parent, child, getParsingNamespace(parentNode))) return; context.report({ node: node.openingElement.name, messageId: isInserted(node, parentNode) ? "invalidNestingInserted" : "invalidNesting", data: { parent, child }, }); }; return { JSXElement(node) { const child = getDOMTagName(node); if (!child) return; const parentNode = getDOMParent(node); if (parentNode) check(node, child, parentNode); }, }; }, });