Skip to content

Commit 00ec241

Browse files
authored
Merge pull request #74 from swhitty/accept-empty-namespace
accept missing namespace
2 parents d9797bc + 0f1bcc3 commit 00ec241

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

SwiftDraw/XML.SAXParser.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension XML {
4545
#endif
4646

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

5050
private var rootNode: Element?
5151
private var elements: [Element]
@@ -84,10 +84,13 @@ extension XML {
8484
return try parse(data: data)
8585
}
8686

87+
func isValidNamespaceURI(_ uri: String?) -> Bool {
88+
validNamespaces.contains(uri ?? "")
89+
}
90+
8791
func parser(_ parser: FoundationXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName _: String?, attributes attributeDict: [String: String] = [:]) {
8892
guard
89-
self.parser === parser,
90-
namespaceURI == self.namespaceURI else {
93+
self.parser === parser, isValidNamespaceURI(namespaceURI) else {
9194
return
9295
}
9396

@@ -103,9 +106,7 @@ extension XML {
103106
}
104107

105108
func parser(_ parser: FoundationXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName _: String?) {
106-
guard
107-
namespaceURI == self.namespaceURI,
108-
currentElement.name == elementName else {
109+
guard isValidNamespaceURI(namespaceURI), currentElement.name == elementName else {
109110
return
110111
}
111112

SwiftDrawTests/Parser.SVGTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,22 @@ final class ParserSVGTests: XCTestCase {
133133
let circle = try XCTUnwrap(svg.firstGraphicsElement(with: "b") as? DOM.Circle)
134134
XCTAssertEqual(circle.id, "b")
135135
}
136+
137+
func testMissingNamespce() throws {
138+
let svg = try DOM.SVG.parse(xml: #"""
139+
<?xml version="1.0" encoding="UTF-8"?>
140+
<svg width="64" height="64" version="1.1">
141+
<defs>
142+
<rect id="a" x="10" y="20" width="30" height="40" />
143+
</defs>
144+
<circle id="b" cx="50" cy="60" r="70" />
145+
</svg>
146+
"""#)
147+
148+
let rect = try XCTUnwrap(svg.firstGraphicsElement(with: "a") as? DOM.Rect)
149+
XCTAssertEqual(rect.id, "a")
150+
151+
let circle = try XCTUnwrap(svg.firstGraphicsElement(with: "b") as? DOM.Circle)
152+
XCTAssertEqual(circle.id, "b")
153+
}
136154
}

0 commit comments

Comments
 (0)