Skip to content

Commit ea86ece

Browse files
authored
Merge pull request #87 from swhitty/reduce-strictness-parseColor
reduces strictness when parsing rbg() colors
2 parents 1d02849 + af35a5b commit ea86ece

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

DOM/Sources/Parser.XML.Color.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,34 @@ extension XMLParser {
120120
return url
121121
}
122122

123-
private func parseIntColor(data: String, withAlpha: Bool) throws -> DOM.Color {
123+
private func parseIntColor(data: String, requireAlpha: Bool) throws -> DOM.Color {
124124
var scanner = XMLParser.Scanner(text: data)
125-
try scanner.scanString(withAlpha ? "rgba(" : "rgb(")
126-
125+
try scanner.scanString(requireAlpha ? "rgba(" : "rgb(")
126+
127127
let r = try scanner.scanUInt8()
128128
scanner.scanStringIfPossible(",")
129129
let g = try scanner.scanUInt8()
130130
scanner.scanStringIfPossible(",")
131131
let b = try scanner.scanUInt8()
132132
var a: Float = 1.0
133133

134-
if withAlpha {
134+
if requireAlpha {
135135
scanner.scanStringIfPossible(",")
136-
a = try scanner.scanFloat() // Opacity
136+
a = try scanner.scanAlpha()
137+
} else if scanner.scanStringIfPossible(",") {
138+
a = try scanner.scanAlpha()
137139
}
138-
140+
139141
try scanner.scanString(")")
140-
return .rgbi(r, g, b, a)
142+
return .rgbi(r, g, b, min(1, a))
141143
}
142144

143145
private func parseColorRGBi(data: String) throws -> DOM.Color {
144-
return try parseIntColor(data: data, withAlpha: false)
146+
return try parseIntColor(data: data, requireAlpha: false)
145147
}
146148

147149
private func parseColorRGBAi(data: String) throws -> DOM.Color {
148-
return try parseIntColor(data: data, withAlpha: true)
150+
return try parseIntColor(data: data, requireAlpha: true)
149151
}
150152

151153
private func parsePercentageColor(data: String, withAlpha: Bool) throws -> DOM.Color {

DOM/Sources/Parser.XML.Scanner.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,15 @@ package extension XMLParser {
214214
currentIndex = scanner.currentIndex
215215
return val
216216
}
217-
217+
218+
package mutating func scanAlpha() throws -> Float {
219+
if let pc = try? scanPercentage() {
220+
return pc
221+
} else {
222+
return try scanFloat()
223+
}
224+
}
225+
218226
package mutating func scanPercentage() throws -> Float {
219227
let initialLocation = currentIndex
220228
scanner.currentIndex = currentIndex

DOM/Tests/Parser.XML.ColorTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ final class ParserColorTests: XCTestCase {
6666
XCTAssertEqual(try XMLParser().parseColor("rgb(0,1,2)"), .rgbi(0, 1, 2, 1.0))
6767
XCTAssertEqual(try XMLParser().parseColor(" rgb( 0 , 1 , 2) "), .rgbi(0, 1, 2, 1.0))
6868
XCTAssertEqual(try XMLParser().parseColor("rgb(255,100,78)"), .rgbi(255, 100, 78, 1.0))
69+
70+
XCTAssertEqual(try XMLParser().parseColor("rgb(0,1,2,255)"), .rgbi(0, 1, 2, 1.0))
71+
XCTAssertEqual(try XMLParser().parseColor("rgb(0,1,2,25%)"), .rgbi(0, 1, 2, 0.25))
72+
XCTAssertEqual(try XMLParser().parseColor(" rgb( 0 , 1 , 2, 0.5) "), .rgbi(0, 1, 2, 0.5))
73+
XCTAssertEqual(try XMLParser().parseColor("rgb(255,100, 78, 0)"), .rgbi(255, 100, 78, 0))
6974
}
7075

7176
func testColorRGBf() {

Examples/Sources/GalleryView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct GalleryView: View {
5151
"worried.svg",
5252
"yawning.svg",
5353
"thats-no-moon.svg",
54-
"alert.svg"
54+
"alert.svg",
55+
"effigy.svg"
5556
].compactMap {
5657
SVG(named: $0, in: .samples)
5758
}

Samples.bundle/effigy.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)