55// Created by Evan Anderson on 9/19/24.
66//
77
8+ import SwiftSyntax
9+
810// MARK: HTMLKitUtilities
911public enum HTMLKitUtilities {
1012 public struct ElementData {
@@ -68,40 +70,87 @@ public extension String {
6870
6971// MARK: CSSUnit
7072public extension HTMLElementAttribute {
71- enum CSSUnit { // https://www.w3schools.com/cssref/css_units.php
73+ enum CSSUnit : HTMLInitializable { // https://www.w3schools.com/cssref/css_units.php
7274 // absolute
73- case centimeters( _ value: Float )
74- case millimeters( _ value: Float )
75+ case centimeters( _ value: Float ? )
76+ case millimeters( _ value: Float ? )
7577 /// 1 inch = 96px = 2.54cm
76- case inches( _ value: Float )
78+ case inches( _ value: Float ? )
7779 /// 1 pixel = 1/96th of 1inch
78- case pixels( _ value: Float )
80+ case pixels( _ value: Float ? )
7981 /// 1 point = 1/72 of 1inch
80- case points( _ value: Float )
82+ case points( _ value: Float ? )
8183 /// 1 pica = 12 points
82- case picas( _ value: Float )
84+ case picas( _ value: Float ? )
8385
8486 // relative
8587 /// Relative to the font-size of the element (2em means 2 times the size of the current font)
86- case em( _ value: Float )
88+ case em( _ value: Float ? )
8789 /// Relative to the x-height of the current font (rarely used)
88- case ex( _ value: Float )
90+ case ex( _ value: Float ? )
8991 /// Relative to the width of the "0" (zero)
90- case ch( _ value: Float )
92+ case ch( _ value: Float ? )
9193 /// Relative to font-size of the root element
92- case rem( _ value: Float )
94+ case rem( _ value: Float ? )
9395 /// Relative to 1% of the width of the viewport
94- case viewportWidth( _ value: Float )
96+ case viewportWidth( _ value: Float ? )
9597 /// Relative to 1% of the height of the viewport
96- case viewportHeight( _ value: Float )
98+ case viewportHeight( _ value: Float ? )
9799 /// Relative to 1% of viewport's smaller dimension
98- case viewportMin( _ value: Float )
100+ case viewportMin( _ value: Float ? )
99101 /// Relative to 1% of viewport's larger dimension
100- case viewportMax( _ value: Float )
102+ case viewportMax( _ value: Float ? )
101103 /// Relative to the parent element
102- case percent( _ value: Float )
104+ case percent( _ value: Float ? )
105+
106+ public init ? ( key: String , arguments: LabeledExprListSyntax ) {
107+ func float( ) -> Float ? {
108+ guard let s: String = arguments. first!. expression. floatLiteral? . literal. text else { return nil }
109+ return Float ( s)
110+ }
111+ switch key {
112+ case " centimeters " : self = . centimeters( float ( ) )
113+ case " millimeters " : self = . millimeters( float ( ) )
114+ case " inches " : self = . inches( float ( ) )
115+ case " pixels " : self = . pixels( float ( ) )
116+ case " points " : self = . points( float ( ) )
117+ case " picas " : self = . picas( float ( ) )
118+
119+ case " em " : self = . em( float ( ) )
120+ case " ex " : self = . ex( float ( ) )
121+ case " ch " : self = . ch( float ( ) )
122+ case " rem " : self = . rem( float ( ) )
123+ case " viewportWidth " : self = . viewportWidth( float ( ) )
124+ case " viewportHeight " : self = . viewportHeight( float ( ) )
125+ case " viewportMin " : self = . viewportMin( float ( ) )
126+ case " viewportMax " : self = . viewportMax( float ( ) )
127+ case " percent " : self = . percent( float ( ) )
128+ default : return nil
129+ }
130+ }
103131
104- public var htmlValue : String {
132+ public var key : String {
133+ switch self {
134+ case . centimeters( _) : return " centimeters "
135+ case . millimeters( _) : return " millimeters "
136+ case . inches( _) : return " inches "
137+ case . pixels( _) : return " pixels "
138+ case . points( _) : return " points "
139+ case . picas( _) : return " picas "
140+
141+ case . em( _) : return " em "
142+ case . ex( _) : return " ex "
143+ case . ch( _) : return " ch "
144+ case . rem( _) : return " rem "
145+ case . viewportWidth( _) : return " viewportWidth "
146+ case . viewportHeight( _) : return " viewportHeight "
147+ case . viewportMin( _) : return " viewportMin "
148+ case . viewportMax( _) : return " viewportMax "
149+ case . percent( _) : return " percent "
150+ }
151+ }
152+
153+ public var htmlValue : String ? {
105154 switch self {
106155 case . centimeters( let v) ,
107156 . millimeters( let v) ,
@@ -147,12 +196,13 @@ public extension HTMLElementAttribute {
147196}
148197
149198// MARK: LiteralReturnType
150- public indirect enum LiteralReturnType {
199+ public enum LiteralReturnType {
151200 case boolean( Bool )
152201 case string( String )
153- case enumCase( String )
202+ case int( Int )
203+ case float( Float )
154204 case interpolation( String )
155- case array( of : LiteralReturnType )
205+ case array( [ Any ] )
156206
157207 public var isInterpolation : Bool {
158208 switch self {
@@ -166,26 +216,24 @@ public indirect enum LiteralReturnType {
166216 default : return false
167217 }
168218 }
169- public var isEnumCase : Bool {
170- switch self {
171- case . enumCase( _) : return true
172- default : return false
173- }
174- }
175219
176220 public func value( key: String ) -> String ? {
177221 switch self {
178222 case . boolean( let b) : return b ? key : nil
179- case . string( var string) , . enumCase ( var string ) :
180- if string. isEmpty && ( isEnumCase || isString && key == " attributionsrc " ) {
223+ case . string( var string) :
224+ if string. isEmpty && key == " attributionsrc " {
181225 return " "
182226 }
183227 string. escapeHTML ( escapeAttributes: true )
184228 return string
229+ case . int( let int) :
230+ return String ( describing: int)
231+ case . float( let float) :
232+ return String ( describing: float)
185233 case . interpolation( let string) :
186234 return string
187- case . array( let value ) :
188- return " "
235+ case . array( _ ) :
236+ return nil
189237 }
190238 }
191239}
0 commit comments