77
88import HTMLKitMacros
99
10- // TODO: fix some `height` & `width` value types (they're CSS pixels, not only integers) | change some to `String` or allow overloads
11-
1210// MARK: Elements
1311@freestanding ( expression)
1412public macro html( xmlns: String ? = nil , _ innerHTML: [ String ] ) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
@@ -142,8 +140,8 @@ public macro button(
142140@freestanding ( expression)
143141public macro canvas(
144142 attributes: Set < HTMLElementAttribute > = [ ] ,
145- height: Int ? = nil ,
146- width: Int ? = nil ,
143+ height: HTMLElementAttribute . CSSUnit ? = nil ,
144+ width: HTMLElementAttribute . CSSUnit ? = nil ,
147145 _ innerHTML: [ String ] = [ ]
148146) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
149147
@@ -226,10 +224,10 @@ public macro em(attributes: Set<HTMLElementAttribute> = [], _ innerHTML: [String
226224@freestanding ( expression)
227225public macro embed(
228226 attributes: Set < HTMLElementAttribute > = [ ] ,
229- height: Int ? = nil ,
227+ height: HTMLElementAttribute . CSSUnit ? = nil ,
230228 src: String ? = nil ,
231229 type: String ? = nil ,
232- width: Int ? = nil ,
230+ width: HTMLElementAttribute . CSSUnit ? = nil ,
233231 _ innerHTML: [ String ] = [ ]
234232) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
235233
@@ -314,14 +312,14 @@ public macro iframe(
314312 browsingtopics: Bool = false ,
315313 credentialless: Bool = false ,
316314 csp: String ? = nil ,
317- height: Int ? = nil ,
315+ height: HTMLElementAttribute . CSSUnit ? = nil ,
318316 loading: HTMLElementAttribute . Loading ? = nil ,
319317 name: String ? = nil ,
320318 referrerpolicy: HTMLElementAttribute . ReferrerPolicy ? = nil ,
321319 sandbox: [ HTMLElementAttribute . Iframe . Sandbox ] = [ ] ,
322320 src: String ? = nil ,
323321 srcdoc: String ? = nil ,
324- width: Int ? = nil ,
322+ width: HTMLElementAttribute . CSSUnit ? = nil ,
325323 _ innerHTML: [ String ] = [ ]
326324) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
327325
@@ -334,14 +332,14 @@ public macro img(
334332 decoding: HTMLElementAttribute . Decoding ? = nil ,
335333 elementtiming: String ? = nil ,
336334 fetchpriority: HTMLElementAttribute . FetchPriority ? = nil ,
337- height: Int ? = nil ,
335+ height: HTMLElementAttribute . CSSUnit ? = nil ,
338336 ismap: Bool = false ,
339337 loading: HTMLElementAttribute . Loading ? = nil ,
340338 referrerpolicy: HTMLElementAttribute . ReferrerPolicy ? = nil ,
341339 sizes: [ String ] = [ ] ,
342340 src: String ? = nil ,
343341 srcset: [ String ] = [ ] ,
344- width: Int ? = nil ,
342+ width: HTMLElementAttribute . CSSUnit ? = nil ,
345343 usemap: String ? = nil ,
346344 _ innerHTML: [ String ] = [ ]
347345) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
@@ -362,7 +360,7 @@ public macro input(
362360 formmethod: HTMLElementAttribute . FormMethod ? = nil ,
363361 formnovalidate: Bool = false ,
364362 formtarget: HTMLElementAttribute . FormTarget ? = nil ,
365- height: Int ? = nil ,
363+ height: HTMLElementAttribute . CSSUnit ? = nil ,
366364 list: String ? = nil ,
367365 max: Int ? = nil ,
368366 maxlength: Int ? = nil ,
@@ -381,7 +379,7 @@ public macro input(
381379 step: Float ? = nil ,
382380 type: HTMLElementAttribute . InputMode ? = nil ,
383381 value: String ? = nil ,
384- width: Int ? = nil ,
382+ width: HTMLElementAttribute . CSSUnit ? = nil ,
385383 _ innerHTML: [ String ] = [ ]
386384) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
387385
@@ -487,10 +485,10 @@ public macro noscript(attributes: Set<HTMLElementAttribute> = [], _ innerHTML: [
487485public macro object(
488486 attributes: Set < HTMLElementAttribute > = [ ] ,
489487 form: String ? = nil ,
490- height: Int ? = nil ,
488+ height: HTMLElementAttribute . CSSUnit ? = nil ,
491489 name: String ? = nil ,
492490 type: String ? = nil ,
493- width: Int ? = nil ,
491+ width: HTMLElementAttribute . CSSUnit ? = nil ,
494492 _ innerHTML: [ String ] = [ ]
495493) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
496494
@@ -770,22 +768,21 @@ public macro video(
770768 crossorigin: HTMLElementAttribute . CrossOrigin ? = nil ,
771769 disablepictureinpicture: Bool = false ,
772770 disableremoteplayback: Bool = false ,
773- height: Int ? = nil ,
771+ height: HTMLElementAttribute . CSSUnit ? = nil ,
774772 loop: Bool = false ,
775773 muted: Bool = false ,
776774 playsinline: Bool = true ,
777775 poster: String ? = nil ,
778776 preload: HTMLElementAttribute . Preload ? = nil ,
779777 src: String ? = nil ,
780- width: Int ? = nil ,
778+ width: HTMLElementAttribute . CSSUnit ? = nil ,
781779 _ innerHTML: [ String ] = [ ]
782780) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
783781
784782// MARK: W
785783@freestanding ( expression)
786784public macro wbr( attributes: Set < HTMLElementAttribute > = [ ] , _ innerHTML: [ String ] = [ ] ) -> String = #externalMacro( module: " HTMLKitMacros " , type: " HTMLElement " )
787785
788-
789786// MARK: HTMLElementAttribute
790787public struct HTMLElementAttribute : Hashable {
791788 public let id : String
@@ -795,6 +792,50 @@ public struct HTMLElementAttribute : Hashable {
795792 }
796793}
797794
795+ // MARK: CSSUnit
796+ public extension HTMLElementAttribute {
797+ struct CSSUnit : CustomStringConvertible {
798+ let string : String
799+ init ( _ string: String ) {
800+ self . string = string
801+ }
802+ public var description : String { string }
803+ }
804+ }
805+ public extension HTMLElementAttribute . CSSUnit { // https://www.w3schools.com/cssref/css_units.php
806+ // absolute
807+ static func centimeters( _ value: Float ) -> Self { Self ( " \( value) cm " ) }
808+ static func millimeters( _ value: Float ) -> Self { Self ( " \( value) mm " ) }
809+ /// 1 inch = 96px = 2.54cm
810+ static func inches( _ value: Float ) -> Self { Self ( " \( value) in " ) }
811+ /// 1 pixel = 1/96th of 1inch
812+ static func pixels( _ value: Float ) -> Self { Self ( " \( value) px " ) }
813+ /// 1 point = 1/72 of 1inch
814+ static func points( _ value: Float ) -> Self { Self ( " \( value) pt " ) }
815+ /// 1 pica = 12 points
816+ static func picas( _ value: Float ) -> Self { Self ( " \( value) pc " ) }
817+
818+ // relative
819+ /// Relative to the font-size of the element (2em means 2 times the size of the current font)
820+ static func em( _ value: Float ) -> Self { Self ( " \( value) em " ) }
821+ /// Relative to the x-height of the current font (rarely used)
822+ static func ex( _ value: Float ) -> Self { Self ( " \( value) ex " ) }
823+ /// Relative to the width of the "0" (zero)
824+ static func ch( _ value: Float ) -> Self { Self ( " \( value) ch " ) }
825+ /// Relative to font-size of the root element
826+ static func rem( _ value: Float ) -> Self { Self ( " \( value) rem " ) }
827+ /// Relative to 1% of the width of the viewport
828+ static func viewportWidth( _ value: Float ) -> Self { Self ( " \( value) vw " ) }
829+ /// Relative to 1% of the height of the viewport
830+ static func viewportHeight( _ value: Float ) -> Self { Self ( " \( value) vh " ) }
831+ /// Relative to 1% of viewport's smaller dimension
832+ static func viewportMin( _ value: Float ) -> Self { Self ( " \( value) vmin " ) }
833+ /// Relative to 1% of viewport's larger dimension
834+ static func viewportMax( _ value: Float ) -> Self { Self ( " \( value) vmax " ) }
835+ /// Relative to the parent element
836+ static func percent( _ value: Float ) -> Self { Self ( " \( value) % " ) }
837+ }
838+
798839// MARK: Global Attributes
799840public extension HTMLElementAttribute {
800841 static func accesskey( _ value: String ) -> Self { Self ( " accesskey " ) }
0 commit comments