Skip to content

Commit 1ba427c

Browse files
committed
handle CSS declarations with multiple selectors
1 parent 1355dce commit 1ba427c

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

DOM/Sources/Parser.XML.Scanner.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ package extension XMLParser {
5555
package mutating func scanStringIfPossible(_ token: String) -> Bool {
5656
return (try? self.scanString(token)) == true
5757
}
58-
58+
59+
@discardableResult
60+
package mutating func nextScanString(_ token: String) -> Bool {
61+
scanner.currentIndex = currentIndex
62+
defer { scanner.currentIndex = currentIndex }
63+
return scanStringIfPossible(token)
64+
}
65+
5966
package mutating func scanString(matchingAny tokens: Set<String>) throws -> String {
6067
scanner.currentIndex = currentIndex
6168
guard let match = tokens.first(where: { scanner.scanString($0) != nil }) else {

DOM/Tests/Parser.XML.StyleSheetTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,43 @@ final class ParserXMLStyleSheetTests: XCTestCase {
110110
XCTAssertEqual(sheet[.class("b")]?.fill, .color(.keyword(.blue)))
111111
XCTAssertEqual(sheet[.element("rect")]?.fill, .color(.keyword(.pink)))
112112
}
113+
114+
func testMergesSelectors() throws {
115+
let entries = try XMLParser.parseEntries(
116+
"""
117+
.a {
118+
fill: red;
119+
}
120+
.a {
121+
stroke: blue;
122+
}
123+
.a {
124+
fill: purple;
125+
}
126+
"""
127+
)
128+
129+
XCTAssertEqual(
130+
entries,
131+
[.class("a"): ["fill": "purple", "stroke": "blue"]]
132+
)
133+
}
134+
135+
func testMutlipleSelectors() throws {
136+
let entries = try XMLParser.parseEntries(
137+
"""
138+
.a, .b {
139+
fill: red;
140+
}
141+
"""
142+
)
143+
144+
XCTAssertEqual(
145+
entries,
146+
[
147+
.class("a"): ["fill": "red"],
148+
.class("b"): ["fill": "red"]
149+
]
150+
)
151+
}
113152
}

Examples/Sources/GalleryView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ struct GalleryView: View {
5252
"yawning.svg",
5353
"thats-no-moon.svg",
5454
"alert.svg",
55-
"effigy.svg"
55+
"effigy.svg",
56+
"stylesheet-multiple.svg"
5657
].compactMap {
5758
SVG(named: $0, in: .samples)
5859
}
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)