Type-safe domain model of HTML elements and attributes in Swift.
swift-html-types provides Swift representation of HTML elements and their attributes with compile-time safety. The package is designed as a foundational building block for Swift-based HTML generation and manipulation tools.
- HTML element coverage: Standard HTML elements represented as Swift types
- Type-safe attributes: Each element exposes only its valid attributes with appropriate types
- IDE discoverability: Autocomplete reveals available attributes per element
- Input type specialization: Different input types (search, color, email, etc.) have distinct signatures
- Foundation integration: Optional
HTMLTypesFoundationmodule for URL safety - Modular design: Focused package that composes with other HTML ecosystem packages
Add the dependency in your Package.swift:
dependencies: [
.package(url: "https://github.com/coenttb/swift-html-types", from: "0.1.0")
]- Select File > Add Packages...
- Enter package URL:
https://github.com/coenttb/swift-html-types - Select version 0.1.0 or "Up to Next Major Version"
import HTMLTypes
// Create elements with appropriate attributes
let anchor = Anchor(href: "https://example.com")
let input = Input.search(name: "query", placeholder: "Search...")import HTMLTypesFoundation
let url: Foundation.URL = .init(string: "https://example.com")!
let anchor = Anchor(href: Href(url))The package improves discoverability of available attributes for each HTML element. For example, invoking the search static func on Input reveals the available attributes:
let input = Input.search(
name: Name?,
value: Value<String>?,
list: List?,
maxlength: Maxlength?,
minlength: Minlength?,
pattern: Pattern?,
placeholder: Placeholder?,
readonly: Readonly?,
size: Size?,
spellcheck: Spellcheck?,
required: Required?,
disabled: Disabled?,
form: Form.ID?
)Contrast this with other input types, like color, that permit a different set of attributes:
let input = Input.color(
name: Name?,
value: Value<String>?,
disabled: Disabled?,
form: Form.ID?
)Each input type exposes only the attributes that are semantically valid for that type, providing compile-time safety and better developer experience.
- Swift 5.9+
- macOS 14.0+ / iOS 17.0+ / Linux
- swift-html: The Swift library for domain-accurate and type-safe HTML & CSS.
- swift-html-css-pointfree: A Swift package integrating swift-html-types and swift-css-types with pointfree-html.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache 2.0 License. See LICENSE for details.