From 038774cfc21690f182b37330fd8644b212d585a4 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Tue, 9 Jun 2026 05:52:26 +0200 Subject: [PATCH] feat(html): SVG attribute-name case adjustment in foreign content (#83) --- CHANGELOG.md | 1 + README.md | 2 +- Sources/HTML/HTMLDocumentBuilder.swift | 19 ++++++++++++++++--- Sources/HTML/HTMLForeignNames.swift | 26 ++++++++++++++++++++++++++ Tests/HTMLForeignContentTests.swift | 14 ++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cf9054..71f93b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- HTML5 SVG attribute-name case adjustment (#83, stage 5): an SVG attribute written in mixed case is restored to its canonical camel case in the tree (`viewbox` -> `viewBox`, `gradienttransform` -> `gradientTransform`, and the rest), applying only to elements in the SVG namespace. HTML attribute names are left as the tokenizer lowercased them. With this, SVG/MathML foreign content carries correct element and attribute names. - HTML5 adoption agency algorithm and active formatting elements (#83, stage 4): misnested formatting tags are now recovered exactly as the HTML standard prescribes. Overlapping tags nest (`` -> ``), content after an out-of-order close is re-wrapped by reconstructing the active formatting elements (`X` -> `X`), and a block inside a formatting element reparents through the furthest-block path, including the canonical `

12345

` -> `

12345

`. - HTML5 SVG element-name case adjustment (#83, stage 3): an SVG element written in mixed case (which the tokenizer lowercases) is restored to its canonical camel case in the tree, so ``, ``, ``, and the rest carry their correct SVG names. Applies only in the SVG namespace; same-named HTML elements keep their lowercased name. - HTML5 foreign content (#83, stage 2): an `` or `` element and all of its descendants now carry the SVG (`http://www.w3.org/2000/svg`) or MathML (`http://www.w3.org/1998/Math/MathML`) namespace on their `QualifiedName`, so foreign elements are distinguishable from same-named HTML; ordinary HTML content (including content after a closed ``) stays in no namespace. (SVG/MathML element- and attribute-name case adjustment, and HTML integration points, are a later refinement.) diff --git a/README.md b/README.md index f97218c..dddbc86 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ flowchart TB PureXML is a working, dependency-free XML library today: parse, emit, validate, query, and stream documents on macOS, Linux, Windows, and WASM. The test suite -currently runs **733 tests in 115 suites** (`swift test`). +currently runs **735 tests in 115 suites** (`swift test`). ### Shipped (libxml2-aligned surface) diff --git a/Sources/HTML/HTMLDocumentBuilder.swift b/Sources/HTML/HTMLDocumentBuilder.swift index 5aff056..48bb561 100644 --- a/Sources/HTML/HTMLDocumentBuilder.swift +++ b/Sources/HTML/HTMLDocumentBuilder.swift @@ -203,7 +203,7 @@ final class HTMLDocument { } bodyEnsureTableContext(for: name) let namespace = bodyForeignNamespace(for: name) - let element = PureXML.Model.TreeNode.element(qualifiedName(name, namespace), attributes: modelAttributes(attributes)) + let element = PureXML.Model.TreeNode.element(qualifiedName(name, namespace), attributes: adjustedAttributes(modelAttributes(attributes), namespace: namespace)) openBody.last?.append(element) if !(PureXML.HTML.Elements.void.contains(name) || selfClosing) { openBody.append(element) @@ -240,6 +240,16 @@ final class HTMLDocument { openBody.append(element) } + /// Restores SVG attribute names to their canonical camel case (`viewBox`, not + /// the tokenizer's `viewbox`) for elements in the SVG namespace. + private func adjustedAttributes(_ attributes: [Attribute], namespace: String?) -> [Attribute] { + guard namespace == ForeignNamespace.svg else { return attributes } + return attributes.map { attribute in + guard let adjusted = PureXML.HTML.ForeignNames.svgAttributes[attribute.name.localName.lowercased()] else { return attribute } + return Attribute(adjusted, attribute.value) + } + } + /// The foreign-content namespace for a body element: SVG/MathML on entry, or /// the nearest open foreign ancestor's namespace inside one. private func bodyForeignNamespace(for name: String) -> String? { @@ -247,9 +257,12 @@ final class HTMLDocument { if name == "math" { return ForeignNamespace.mathml } return openBody.reversed().compactMap { $0.name?.namespaceURI }.first } +} - // MARK: Insertion primitives - +/// The head's open-element stack still uses the pop-on-close `DocFrame` model (it +/// needs no adoption agency); these primitives drive it, kept in an extension so +/// the class body stays within its size budget. +extension HTMLDocument { private func open( _ name: String, _ attributes: [(String, String)], diff --git a/Sources/HTML/HTMLForeignNames.swift b/Sources/HTML/HTMLForeignNames.swift index a1ae9f6..8203c6e 100644 --- a/Sources/HTML/HTMLForeignNames.swift +++ b/Sources/HTML/HTMLForeignNames.swift @@ -19,5 +19,31 @@ extension PureXML.HTML { "glyphref": "glyphRef", "lineargradient": "linearGradient", "radialgradient": "radialGradient", "textpath": "textPath", ] + + /// The HTML5 "adjust SVG attributes" table: SVG attributes whose correct + /// names are camel-cased, keyed by the lowercased form the tokenizer + /// produces. Applied to the attributes of an element in the SVG namespace. + static let svgAttributes: [String: String] = [ + "attributename": "attributeName", "attributetype": "attributeType", "basefrequency": "baseFrequency", + "baseprofile": "baseProfile", "calcmode": "calcMode", "clippathunits": "clipPathUnits", + "diffuseconstant": "diffuseConstant", "edgemode": "edgeMode", "filterunits": "filterUnits", + "glyphref": "glyphRef", "gradienttransform": "gradientTransform", "gradientunits": "gradientUnits", + "kernelmatrix": "kernelMatrix", "kernelunitlength": "kernelUnitLength", "keypoints": "keyPoints", + "keysplines": "keySplines", "keytimes": "keyTimes", "lengthadjust": "lengthAdjust", + "limitingconeangle": "limitingConeAngle", "markerheight": "markerHeight", "markerunits": "markerUnits", + "markerwidth": "markerWidth", "maskcontentunits": "maskContentUnits", "maskunits": "maskUnits", + "numoctaves": "numOctaves", "pathlength": "pathLength", "patterncontentunits": "patternContentUnits", + "patterntransform": "patternTransform", "patternunits": "patternUnits", "pointsatx": "pointsAtX", + "pointsaty": "pointsAtY", "pointsatz": "pointsAtZ", "preservealpha": "preserveAlpha", + "preserveaspectratio": "preserveAspectRatio", "primitiveunits": "primitiveUnits", "refx": "refX", + "refy": "refY", "repeatcount": "repeatCount", "repeatdur": "repeatDur", + "requiredextensions": "requiredExtensions", "requiredfeatures": "requiredFeatures", "specularconstant": "specularConstant", + "specularexponent": "specularExponent", "spreadmethod": "spreadMethod", "startoffset": "startOffset", + "stddeviation": "stdDeviation", "stitchtiles": "stitchTiles", "surfacescale": "surfaceScale", + "systemlanguage": "systemLanguage", "tablevalues": "tableValues", "targetx": "targetX", + "targety": "targetY", "textlength": "textLength", "viewbox": "viewBox", + "viewtarget": "viewTarget", "xchannelselector": "xChannelSelector", "ychannelselector": "yChannelSelector", + "zoomandpan": "zoomAndPan", + ] } } diff --git a/Tests/HTMLForeignContentTests.swift b/Tests/HTMLForeignContentTests.swift index d07d50d..f443ade 100644 --- a/Tests/HTMLForeignContentTests.swift +++ b/Tests/HTMLForeignContentTests.swift @@ -65,4 +65,18 @@ struct HTMLForeignContentTests { #expect(find("clippath", in: "")?.name.namespaceURI == nil) #expect(find("clipPath", in: "") == nil) } + + @Test("SVG attribute names are restored to their canonical camel case") + func test_svgAttributeCase() { + let rect = find("rect", in: "") + #expect(rect?.attributes.contains { $0.name.description == "viewBox" } == true) + #expect(rect?.attributes.contains { $0.name.description == "width" } == true) + #expect(rect?.attributes.contains { $0.name.description == "viewbox" } == false) + } + + @Test("HTML attribute names are not case-adjusted") + func test_htmlAttributeNotAdjusted() { + let element = find("p", in: "

") + #expect(element?.attributes.contains { $0.name.description == "viewbox" } == true) + } }