Skip to content
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
22 changes: 19 additions & 3 deletions DOM/Sources/Parser.XML.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
// 3. This notice may not be removed or altered from any source distribution.
//

import Foundation

package extension XMLParser {

func parseSVG(_ e: XML.Element) throws -> DOM.SVG {
Expand Down Expand Up @@ -72,9 +74,9 @@ package extension XMLParser {
guard let data = data else { return nil }
var scanner = XMLParser.Scanner(text: data)

let x = try scanner.scanCoordinate()
let y = try scanner.scanCoordinate()
let width = try scanner.scanCoordinate()
let x = try scanner.scanViewBoxCoordinate()
let y = try scanner.scanViewBoxCoordinate()
let width = try scanner.scanViewBoxCoordinate()
let height = try scanner.scanCoordinate()

guard scanner.isEOF else {
Expand Down Expand Up @@ -247,3 +249,17 @@ package extension XMLParser {
return pattern
}
}

private extension XMLParser.Scanner {

mutating func scanViewBoxCoordinate() throws -> DOM.Coordinate {
let coordinate = try scanCoordinate()
_ = try? scanCharacter(matchingAny: .viewBoxSeparator)
return coordinate
}
}

private extension Foundation.CharacterSet {

static let viewBoxSeparator = Foundation.CharacterSet(charactersIn: ",")
}
19 changes: 19 additions & 0 deletions DOM/Tests/Parser.SVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ struct ParserSVGTests {
}
}

@Test
func viewBoxWithCommaDelimiters() throws {
let parsed = try #require(try XMLParser().parseViewBox("0,0,150,50"))
#expect(parsed.x == 0)
#expect(parsed.y == 0)
#expect(parsed.width == 150)
#expect(parsed.height == 50)
}

@Test
func invalidViewBoxWithCommaDelimiters() {
#expect(throws: (any Error).self) {
try XMLParser().parseViewBox("10,,,10,10,10")
}
#expect(throws: (any Error).self) {
try XMLParser().parseViewBox("10,10,10")
}
}

@Test
func clipPath() throws {
let node = XML.Element(name: "clipPath", attributes: ["id": "hello"])
Expand Down
2 changes: 1 addition & 1 deletion Samples.bundle/spider.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading