diff --git a/Sources/CSS/Properties/FontWeight.swift b/Sources/CSS/Properties/FontWeight.swift index d5dcdd7..5b4e23e 100644 --- a/Sources/CSS/Properties/FontWeight.swift +++ b/Sources/CSS/Properties/FontWeight.swift @@ -3,12 +3,24 @@ // swift-web-standards // // Created by Binary Birds on 2026. 02. 02. +// /// CSS `font-weight` property. -/// Provides typed values for this declaration. +/// +/// Provides typed values and numeric support for font weight declarations. +/// +/// ### Examples +/// ```swift +/// FontWeight(.normal) +/// FontWeight(.bold) +/// FontWeight(.w400) // -> "400" +/// FontWeight(.number(550)) +/// FontWeight(700) // -> "700" +/// ``` public struct FontWeight: Property { + /// Value options for the `font-weight` property. - public enum Value: String, Sendable { + public enum Value: Sendable { /// Defines normal characters. This is default. case normal /// Defines thick characters. @@ -17,21 +29,52 @@ public struct FontWeight: Property { case bolder /// Defines lighter characters. case lighter - /// Defines from thin to thick characters. 400 is the same as normal, and 700 is the same as bold. - // @TODO: better numeric value support - case w100 - case w200 - case w300 - case w400 - case w500 - case w600 - case w700 - case w800 - case w900 + + /// A numeric font weight. + /// + /// Typical values are `100...900` in increments of `100`, + /// but variable fonts may support intermediate values. + /// + /// - Note: Negative values are not valid in CSS. + case number(Int) + + /// Convenience numeric values. + /// + /// `w400` is equivalent to `normal`, and `w700` is equivalent to `bold`. + case w100, w200, w300, w400, w500, w600, w700, w800, w900 + /// Sets this property to its default value. case initial /// Inherits this property from its parent element. case inherit + + /// Rendered CSS value. + var cssValue: String { + switch self { + case .normal: return "normal" + case .bold: return "bold" + case .bolder: return "bolder" + case .lighter: return "lighter" + + case .number(let value): + return String(value) + + case .w100: return "100" + case .w200: return "200" + case .w300: return "300" + case .w400: return "400" + case .w500: return "500" + case .w600: return "600" + case .w700: return "700" + case .w800: return "800" + case .w900: return "900" + + case .initial: return "initial" + case .inherit: return "inherit" + } + } + + public var rawValue: String { cssValue } } public let name: String @@ -39,12 +82,18 @@ public struct FontWeight: Property { public var isImportant: Bool /// Specifies the weight of a font. + /// /// - Parameter value: The property value. - public init( - _ value: Value - ) { + public init(_ value: Value = .normal) { self.name = "font-weight" - self.value = value.rawValue + self.value = value.cssValue self.isImportant = false } + + /// Specifies the weight of a font using a numeric value. + /// + /// - Parameter value: A numeric font weight value. + public init(_ value: Int) { + self.init(.number(value)) + } } diff --git a/Sources/CSS/Properties/ZIndex.swift b/Sources/CSS/Properties/ZIndex.swift index b8f4782..56ce89c 100644 --- a/Sources/CSS/Properties/ZIndex.swift +++ b/Sources/CSS/Properties/ZIndex.swift @@ -8,15 +8,15 @@ /// Provides typed values for this declaration. public struct ZIndex: Property { /// Value options for the `z-index` property. - public enum Value: String, Sendable { + public enum Value: Sendable { /// Sets the stack order equal to its parents. This is default. case auto - /// Sets the stack order of the element. Negative numbers are allowed. - case number /// Sets this property to its default value. case initial /// Inherits this property from its parent element. case inherit + /// Sets the stack order of the element. Negative numbers are allowed. + case number(Int) } public let name: String @@ -29,7 +29,17 @@ public struct ZIndex: Property { _ value: Value = .auto ) { self.name = "z-index" - self.value = value.rawValue self.isImportant = false + + switch value { + case .auto: + self.value = "auto" + case .initial: + self.value = "initial" + case .inherit: + self.value = "inherit" + case .number(let number): + self.value = String(number) + } } } diff --git a/Tests/CSSTests/Properties/FontWeightTestSuite.swift b/Tests/CSSTests/Properties/FontWeightTestSuite.swift index 315c9d2..e69330a 100644 --- a/Tests/CSSTests/Properties/FontWeightTestSuite.swift +++ b/Tests/CSSTests/Properties/FontWeightTestSuite.swift @@ -3,6 +3,7 @@ // swift-web-standards // // Created by Binary Birds on 2026. 01. 28. +// import Testing @@ -23,6 +24,67 @@ struct FontWeightTests { #expect(result == expectation) } + @Test + func keywordValues() { + let renderer = StylesheetRenderer() + + #expect( + renderer.renderProperty(FontWeight(.normal)) + == "font-weight: normal" + ) + #expect( + renderer.renderProperty(FontWeight(.bold)) == "font-weight: bold" + ) + #expect( + renderer.renderProperty(FontWeight(.bolder)) + == "font-weight: bolder" + ) + #expect( + renderer.renderProperty(FontWeight(.lighter)) + == "font-weight: lighter" + ) + } + + @Test + func numericPresets() { + let renderer = StylesheetRenderer() + + #expect( + renderer.renderProperty(FontWeight(.w100)) == "font-weight: 100" + ) + #expect( + renderer.renderProperty(FontWeight(.w400)) == "font-weight: 400" + ) + #expect( + renderer.renderProperty(FontWeight(.w700)) == "font-weight: 700" + ) + #expect( + renderer.renderProperty(FontWeight(.w900)) == "font-weight: 900" + ) + } + + @Test + func numericValue() { + let renderer = StylesheetRenderer() + + #expect( + renderer.renderProperty(FontWeight(.number(550))) + == "font-weight: 550" + ) + #expect( + renderer.renderProperty(FontWeight(.number(400))) + == "font-weight: 400" + ) + } + + @Test + func numericInitializer() { + let renderer = StylesheetRenderer() + + // If you added `public init(_ value: Int)` this should compile and pass. + #expect(renderer.renderProperty(FontWeight(700)) == "font-weight: 700") + } + @Test func important() { let property = FontWeight(.initial) @@ -35,4 +97,16 @@ struct FontWeightTests { #expect(result == expectation) } + + @Test + func importantWithNumericValue() { + let renderer = StylesheetRenderer() + + let property = FontWeight(.w400) + .important() + + #expect( + renderer.renderProperty(property) == "font-weight: 400 !important" + ) + } } diff --git a/Tests/CSSTests/Properties/ZIndexTestSuite.swift b/Tests/CSSTests/Properties/ZIndexTestSuite.swift index afa076c..b5b383d 100644 --- a/Tests/CSSTests/Properties/ZIndexTestSuite.swift +++ b/Tests/CSSTests/Properties/ZIndexTestSuite.swift @@ -3,6 +3,7 @@ // swift-web-standards // // Created by Binary Birds on 2026. 01. 28. +// import Testing @@ -12,7 +13,7 @@ import Testing struct ZIndexTests { @Test - func initializers() { + func defaultInitializer() { let property = ZIndex() let renderer = StylesheetRenderer() @@ -23,16 +24,44 @@ struct ZIndexTests { #expect(result == expectation) } + @Test + func integerValue() { + let property = ZIndex(.number(10)) + + let renderer = StylesheetRenderer() + let result = renderer.renderProperty(property) + + #expect(result == "z-index: 10") + } + + @Test + func zeroValue() { + let property = ZIndex(.number(0)) + + let renderer = StylesheetRenderer() + let result = renderer.renderProperty(property) + + #expect(result == "z-index: 0") + } + + @Test + func negativeValue() { + let property = ZIndex(.number(-5)) + + let renderer = StylesheetRenderer() + let result = renderer.renderProperty(property) + + #expect(result == "z-index: -5") + } + @Test func important() { - let property = ZIndex() + let property = ZIndex(.number(100)) .important() let renderer = StylesheetRenderer() let result = renderer.renderProperty(property) - let expectation = "\(property.name): \(property.value) !important" - - #expect(result == expectation) + #expect(result == "z-index: 100 !important") } }