Skip to content

accept missing namespace #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions SwiftDraw/XML.SAXParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension XML {
#endif

private let parser: FoundationXMLParser
private let namespaceURI = "http://www.w3.org/2000/svg"
private let validNamespaces = Set(["http://www.w3.org/2000/svg", ""])

private var rootNode: Element?
private var elements: [Element]
Expand Down Expand Up @@ -84,10 +84,13 @@ extension XML {
return try parse(data: data)
}

func isValidNamespaceURI(_ uri: String?) -> Bool {
validNamespaces.contains(uri ?? "")
}

func parser(_ parser: FoundationXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName _: String?, attributes attributeDict: [String: String] = [:]) {
guard
self.parser === parser,
namespaceURI == self.namespaceURI else {
self.parser === parser, isValidNamespaceURI(namespaceURI) else {
return
}

Expand All @@ -103,9 +106,7 @@ extension XML {
}

func parser(_ parser: FoundationXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName _: String?) {
guard
namespaceURI == self.namespaceURI,
currentElement.name == elementName else {
guard isValidNamespaceURI(namespaceURI), currentElement.name == elementName else {
return
}

Expand Down
18 changes: 18 additions & 0 deletions SwiftDrawTests/Parser.SVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,22 @@ final class ParserSVGTests: XCTestCase {
let circle = try XCTUnwrap(svg.firstGraphicsElement(with: "b") as? DOM.Circle)
XCTAssertEqual(circle.id, "b")
}

func testMissingNamespce() throws {
let svg = try DOM.SVG.parse(xml: #"""
<?xml version="1.0" encoding="UTF-8"?>
<svg width="64" height="64" version="1.1">
<defs>
<rect id="a" x="10" y="20" width="30" height="40" />
</defs>
<circle id="b" cx="50" cy="60" r="70" />
</svg>
"""#)

let rect = try XCTUnwrap(svg.firstGraphicsElement(with: "a") as? DOM.Rect)
XCTAssertEqual(rect.id, "a")

let circle = try XCTUnwrap(svg.firstGraphicsElement(with: "b") as? DOM.Circle)
XCTAssertEqual(circle.id, "b")
}
}