feat(html): SVG attribute-name case adjustment in foreign content (#83)
This commit is contained in:
parent
2af445dd09
commit
038774cfc2
@ -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 (`<b><i></b></i>` -> `<b><i></i></b>`), content after an out-of-order close is re-wrapped by reconstructing the active formatting elements (`<b><i></b>X</i>` -> `<b><i></i></b><i>X</i>`), and a block inside a formatting element reparents through the furthest-block path, including the canonical `<p>1<b>2<i>3</b>4</i>5</p>` -> `<p>1<b>2<i>3</i></b><i>4</i>5</p>`.
|
||||
- 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 `<foreignObject>`, `<linearGradient>`, `<clipPath>`, 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 `<svg>` or `<math>` 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 `<svg>`) stays in no namespace. (SVG/MathML element- and attribute-name case adjustment, and HTML integration points, are a later refinement.)
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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)],
|
||||
|
||||
@ -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",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,4 +65,18 @@ struct HTMLForeignContentTests {
|
||||
#expect(find("clippath", in: "<clippath></clippath>")?.name.namespaceURI == nil)
|
||||
#expect(find("clipPath", in: "<clippath></clippath>") == nil)
|
||||
}
|
||||
|
||||
@Test("SVG attribute names are restored to their canonical camel case")
|
||||
func test_svgAttributeCase() {
|
||||
let rect = find("rect", in: "<svg><rect viewbox=\"0 0 1 1\" width=\"1\"></rect></svg>")
|
||||
#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: "<p viewbox=\"x\"></p>")
|
||||
#expect(element?.attributes.contains { $0.name.description == "viewbox" } == true)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user