@@ -44,40 +44,45 @@ public extension String {
4444// MARK: CSSUnit
4545public extension HTMLElementAttribute {
4646 struct CSSUnit {
47+ public let htmlValue : String ?
48+
49+ private init ( _ value: Float ) {
50+ htmlValue = value. description
51+ }
4752 }
4853}
4954public extension HTMLElementAttribute . CSSUnit { // https://www.w3schools.com/cssref/css_units.php
5055 // absolute
51- static func centimeters( _ value: Float ) -> Self { Self ( ) }
52- static func millimeters( _ value: Float ) -> Self { Self ( ) }
56+ static func centimeters( _ value: Float ) -> Self { Self ( value ) }
57+ static func millimeters( _ value: Float ) -> Self { Self ( value ) }
5358 /// 1 inch = 96px = 2.54cm
54- static func inches( _ value: Float ) -> Self { Self ( ) }
59+ static func inches( _ value: Float ) -> Self { Self ( value ) }
5560 /// 1 pixel = 1/96th of 1inch
56- static func pixels( _ value: Float ) -> Self { Self ( ) }
61+ static func pixels( _ value: Float ) -> Self { Self ( value ) }
5762 /// 1 point = 1/72 of 1inch
58- static func points( _ value: Float ) -> Self { Self ( ) }
63+ static func points( _ value: Float ) -> Self { Self ( value ) }
5964 /// 1 pica = 12 points
60- static func picas( _ value: Float ) -> Self { Self ( ) }
65+ static func picas( _ value: Float ) -> Self { Self ( value ) }
6166
6267 // relative
6368 /// Relative to the font-size of the element (2em means 2 times the size of the current font)
64- static func em( _ value: Float ) -> Self { Self ( ) }
69+ static func em( _ value: Float ) -> Self { Self ( value ) }
6570 /// Relative to the x-height of the current font (rarely used)
66- static func ex( _ value: Float ) -> Self { Self ( ) }
71+ static func ex( _ value: Float ) -> Self { Self ( value ) }
6772 /// Relative to the width of the "0" (zero)
68- static func ch( _ value: Float ) -> Self { Self ( ) }
73+ static func ch( _ value: Float ) -> Self { Self ( value ) }
6974 /// Relative to font-size of the root element
70- static func rem( _ value: Float ) -> Self { Self ( ) }
75+ static func rem( _ value: Float ) -> Self { Self ( value ) }
7176 /// Relative to 1% of the width of the viewport
72- static func viewportWidth( _ value: Float ) -> Self { Self ( ) }
77+ static func viewportWidth( _ value: Float ) -> Self { Self ( value ) }
7378 /// Relative to 1% of the height of the viewport
74- static func viewportHeight( _ value: Float ) -> Self { Self ( ) }
79+ static func viewportHeight( _ value: Float ) -> Self { Self ( value ) }
7580 /// Relative to 1% of viewport's smaller dimension
76- static func viewportMin( _ value: Float ) -> Self { Self ( ) }
81+ static func viewportMin( _ value: Float ) -> Self { Self ( value ) }
7782 /// Relative to 1% of viewport's larger dimension
78- static func viewportMax( _ value: Float ) -> Self { Self ( ) }
83+ static func viewportMax( _ value: Float ) -> Self { Self ( value ) }
7984 /// Relative to the parent element
80- static func percent( _ value: Float ) -> Self { Self ( ) }
85+ static func percent( _ value: Float ) -> Self { Self ( value ) }
8186}
8287
8388
@@ -244,16 +249,23 @@ indirect enum HTMLElementValueType {
244249 case cssUnit
245250 case array( of: HTMLElementValueType )
246251
247- static func consume( _ range: inout Substring , length: Int ) -> String {
248- let slice : Substring = range [ range. startIndex..< range. index ( range. endIndex, offsetBy: length) ]
249- range = range [ range. index ( range. startIndex, offsetBy: length) ... ]
252+ private static func cleanup( _ range: inout Substring ) {
250253 while ( range. first? . isWhitespace ?? false ) || range. first == " , " {
251254 range. removeFirst ( )
252255 }
256+ }
257+ static func consume( _ range: inout Substring , length: Int ) -> String {
258+ let slice : Substring = range [ range. startIndex..< range. index ( range. endIndex, offsetBy: length) ]
259+ range = range [ range. index ( range. startIndex, offsetBy: length) ... ]
260+ cleanup ( & range)
253261 return String ( slice)
254262 }
255- static func cString( _ range: inout Substring ) -> String ? {
256- guard range. first == " \" " else { return nil }
263+ static func cString( key: String , _ range: inout Substring ) -> String ? {
264+ guard range. hasPrefix ( key + " : " ) else { return nil }
265+ range. removeFirst ( key. count + 1 )
266+ while range. first != " \" " {
267+ range. removeFirst ( )
268+ }
257269 range. removeFirst ( )
258270 guard let index: Substring . Index = range. firstIndex ( of: " \" " ) else { return nil }
259271 return consume ( & range, length: range. distance ( from: range. startIndex, to: index) )
@@ -275,11 +287,15 @@ indirect enum HTMLElementValueType {
275287 while ( range. first? . isNumber ?? false ) || range. first == " . " || range. first == " _ " {
276288 string. append ( range. removeFirst ( ) )
277289 }
290+ cleanup ( & range)
278291 return Float ( string) !
279292 }
280- static func cAttribute< T: HTMLInitializable > ( _ range: inout Substring ) -> T ? {
281- guard range. first == " . " else { return nil }
282- range. removeFirst ( )
293+ static func cAttribute< T: HTMLInitializable > ( key: String , _ range: inout Substring ) -> T ? {
294+ guard range. hasPrefix ( key + " : " ) else { return nil }
295+ range. removeFirst ( key. count + 1 )
296+ while ( range. first? . isWhitespace ?? false ) || range. first == " . " {
297+ range. removeFirst ( )
298+ }
283299 var string : String = " " , depth : Int = 1
284300 while let char: Character = range. first {
285301 if char == " ( " {
@@ -289,11 +305,14 @@ indirect enum HTMLElementValueType {
289305 }
290306 if depth == 0 {
291307 break
308+ } else if depth == 1 && char == " , " {
309+ break
292310 }
293311 string. append ( range. removeFirst ( ) )
294312 }
295- string += " ) "
296- return T ( rawValue: string)
313+ guard let value: T = T ( rawValue: string) else { return nil }
314+ cleanup ( & range)
315+ return value
297316 }
298317}
299318
0 commit comments