Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions Sources/CSS/Properties/FontWeight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,34 +29,71 @@ 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
public let value: String
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))
}
}
18 changes: 14 additions & 4 deletions Sources/CSS/Properties/ZIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}
74 changes: 74 additions & 0 deletions Tests/CSSTests/Properties/FontWeightTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// swift-web-standards
//
// Created by Binary Birds on 2026. 01. 28.
//

import Testing

Expand All @@ -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)
Expand All @@ -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"
)
}
}
39 changes: 34 additions & 5 deletions Tests/CSSTests/Properties/ZIndexTestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// swift-web-standards
//
// Created by Binary Birds on 2026. 01. 28.
//

import Testing

Expand All @@ -12,7 +13,7 @@ import Testing
struct ZIndexTests {

@Test
func initializers() {
func defaultInitializer() {
let property = ZIndex()

let renderer = StylesheetRenderer()
Expand All @@ -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")
}
}
Loading