@@ -71,13 +71,15 @@ extension XMLParser {
7171 var scanner = XMLParser . Scanner ( text: removeCSSComments ( from: text) )
7272 var entries = [ DOM . StyleSheet. Selector: [ String: String] ] ( )
7373
74- var last : ( DOM . StyleSheet . Selector , [ String : String ] ) ?
75- repeat {
76- last = try scanner. scanNextSelector ( )
77- if let last = last {
78- entries [ last. 0 ] = last. 1
74+ while let ( selectors, attributes) = try scanner. scanNextSelectorDecl ( ) {
75+ for selector in selectors {
76+ var copy = entries [ selector] ?? [ : ]
77+ for (key, value) in attributes {
78+ copy [ key] = value
79+ }
80+ entries [ selector] = copy
7981 }
80- } while last != nil
82+ }
8183
8284 return entries
8385 }
@@ -91,15 +93,10 @@ extension XMLParser {
9193
9294extension XMLParser . Scanner {
9395
94- mutating func scanNextSelector( ) throws -> ( DOM . StyleSheet . Selector , [ String : String ] ) ? {
95- if let c = try scanNextClass ( ) {
96- return ( . class( c) , try scanAtttributes ( ) )
97- } else if let id = try scanNextID ( ) {
98- return ( . id( id) , try scanAtttributes ( ) )
99- } else if let e = try scanNextElement ( ) {
100- return ( . element( e) , try scanAtttributes ( ) )
101- }
102- return nil
96+ mutating func scanNextSelectorDecl( ) throws -> ( [ DOM . StyleSheet . Selector ] , [ String : String ] ) ? {
97+ let selectorTypes = try scanSelectorTypes ( )
98+ guard !selectorTypes. isEmpty else { return nil }
99+ return ( selectorTypes, try scanAtttributes ( ) )
103100 }
104101
105102 private mutating func scanNextClass( ) throws -> String ? {
@@ -124,7 +121,30 @@ extension XMLParser.Scanner {
124121 }
125122
126123 private mutating func scanSelectorName( ) throws -> String ? {
127- try scanString ( upTo: " { " ) . trimmingCharacters ( in: . whitespacesAndNewlines)
124+ guard !nextScanString( " { " ) else { return nil }
125+ let name = try scanString ( upTo: . init( charactersIn: " {, " ) ) . trimmingCharacters ( in: . whitespacesAndNewlines)
126+ scanStringIfPossible ( " , " )
127+ return name
128+ }
129+
130+ mutating func scanSelectorTypes( ) throws -> [ DOM . StyleSheet . Selector ] {
131+ var selectors : [ DOM . StyleSheet . Selector ] = [ ]
132+ while let next = try scanNextSelectorType ( ) {
133+ selectors. append ( next)
134+ }
135+ return selectors
136+ }
137+
138+ private mutating func scanNextSelectorType( ) throws -> DOM . StyleSheet . Selector ? {
139+ if let name = try scanNextClass ( ) {
140+ return . class( name)
141+ } else if let name = try scanNextID ( ) {
142+ return . id( name)
143+ } else if let name = try scanNextElement ( ) {
144+ return . element( name)
145+ } else {
146+ return nil
147+ }
128148 }
129149
130150 private mutating func scanAtttributes( ) throws -> [ String : String ] {
0 commit comments