Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 7a19f92

Browse files
committed
🚧 [wip] attribute store
1 parent 115040e commit 7a19f92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+732
-649
lines changed

.mailmap

Lines changed: 0 additions & 5 deletions
This file was deleted.

CONTRIBUTORS.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SHELL=/bin/bash
44

55
baseUrl = https://raw.githubusercontent.com/BinaryBirds/github-workflows/refs/heads/main/scripts
66

7-
check: symlinks language deps lint headers
7+
check: symlinks language deps lint
88

99
symlinks:
1010
curl -s $(baseUrl)/check-broken-symlinks.sh | bash

README.md

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,60 @@
22

33
An awesome Swift HTML DSL library using result builders that closely follows the W3C standards.
44

5-
65
```swift
7-
import SwiftHtml
8-
9-
let doc = Document(.html) {
10-
Html {
11-
Head {
12-
Title("Hello Swift HTML DSL")
13-
14-
Meta().charset("utf-8")
15-
Meta().name(.viewport).content("width=device-width, initial-scale=1")
16-
17-
Link(rel: .stylesheet).href("./css/style.css")
6+
import SwiftHTML
7+
8+
let html = Html {
9+
Head {
10+
Title("Hello, SwiftHTML!")
11+
Meta().charset("utf-8")
12+
Meta().name(.viewport).content("width=device-width, initial-scale=1")
13+
Link(rel: .stylesheet).href("./css/style.css")
14+
}
15+
Body {
16+
H1("Hello, SwiftHTML!")
17+
Ul {
18+
Li("Type-safe HTML DSL for Swift 6+")
19+
Li("Concurrency-safety; sendable support")
20+
Li("Contains all the HTML tag definitions")
21+
Li("RSS, Sitemap, SVG support as well")
1822
}
19-
Body {
20-
Main {
21-
Div {
22-
Section {
23-
Img(src: "./images/swift.png", alt: "Swift Logo")
24-
.title("Picture of the Swift Logo")
25-
H1("Lorem ipsum")
26-
.class("red")
27-
P("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
28-
.class(["green", "blue"])
29-
.spellcheck(false)
30-
}
31-
32-
A("Download SwiftHtml now!")
33-
.href("https://github.com/binarybirds/swift-html/")
34-
.target(.blank)
35-
.download()
36-
37-
Abbr("WTFPL")
38-
.title("Do What The Fuck You Want To Public License")
39-
}
40-
}
41-
.class("container")
4223

43-
Script().src("./js/main.js").async()
44-
}
24+
Script(#"console.log("Hello, SwiftHTML!")"#)
25+
Script().src("./js/main.js").async()
4526
}
4627
}
4728

48-
let html = DocumentRenderer(minify: false, indent: 2).render(doc)
49-
print(html)
29+
let renderer = Renderer(indent: 4)
30+
let doc = Document(type: .html, root: html)
31+
let result = renderer.render(document: doc)
32+
print(result) // HTML output
5033
```
5134

35+
## Installation
5236

53-
## Install
54-
55-
You can simply use `SwiftHtml` as a dependency via the Swift Package Manager:
37+
You can use `SwiftHTML` as a dependency via Swift Package Manager:
5638

5739
```swift
58-
.package(url: "https://github.com/binarybirds/swift-html", from: "1.6.0"),
40+
.package(url: "https://github.com/binarybirds/swift-html", from: "2.0.0"),
5941
```
6042

61-
Add the `SwiftHtml` product from the `swift-html` package as a dependency to your target:
43+
Add the `SwiftHTML` product from the `swift-html` package as a dependency to your target:
6244

6345
```swift
64-
.product(name: "SwiftHtml", package: "swift-html"),
46+
.product(name: "SwiftHTML", package: "swift-html"),
6547
```
6648

6749
Import the framework:
6850

6951
```swift
70-
import SwiftHtml
52+
import SwiftHTML
7153
```
7254

7355
That's it.
7456

7557

76-
## Creating custom tags
58+
## Custom tags
7759

7860
You can define your own custom tags by subclassing the `Tag` or `EmptyTag` class.
7961

@@ -243,8 +225,6 @@ let tag = WebIndexTemplate(ctx) {
243225
.render(req)
244226
```
245227

246-
If you want to create a lightweight template engine for the [Vapor](https://vapor.codes/) web framework using SwiftHtml, you can see a working example inside the [Feather CMS core](https://github.com/FeatherCMS/feather-core) repository.
247-
248228

249229
## Credits & references
250230

Sources/DOM/Nodes/ShortNode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
public struct ShortNode: Node {
22

33
public var name: String
4-
public var attributes: [Attribute]
4+
public var properties: [Property]
55

66
public init(
77
name: String,
8-
attributes: [Attribute] = []
8+
properties: [Property] = []
99
) {
1010
self.name = name
11-
self.attributes = attributes
11+
self.properties = properties
1212
}
1313
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
public struct StandardNode: Node {
22

33
public var name: String
4-
public var attributes: [Attribute]
4+
public var properties: [Property]
55
public var children: [Node] { list.items }
66

77
private var list: ListNode
88

99
public init(
1010
name: String,
11-
attributes: [Attribute] = [],
11+
properties: [Property] = [],
1212
children: [Node] = []
1313
) {
1414
self.name = name
15-
self.attributes = attributes
15+
self.properties = properties
1616
self.list = .init(items: children)
1717
}
1818
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct Attribute: Sendable {
1+
public struct Property: Sendable {
22

33
public var name: String
44
public var value: String?

Sources/DOM/Renderer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public struct Renderer {
2626
}
2727

2828
func renderAttributeList(
29-
_ attributes: [Attribute]
29+
_ attributes: [Property]
3030
) -> String {
3131
let attributesList =
3232
attributes
@@ -39,7 +39,7 @@ public struct Renderer {
3939
}
4040

4141
func renderAttribute(
42-
_ attribute: Attribute
42+
_ attribute: Property
4343
) -> String {
4444
if let value = attribute.value {
4545
return #"\#(attribute.name)="\#(value)""#
@@ -50,7 +50,7 @@ public struct Renderer {
5050
func renderStandardOpening(
5151
_ node: StandardNode
5252
) -> String {
53-
let attributesList = renderAttributeList(node.attributes)
53+
let attributesList = renderAttributeList(node.properties)
5454
return "<\(node.name)\(attributesList)>"
5555
}
5656

@@ -63,7 +63,7 @@ public struct Renderer {
6363
func renderShort(
6464
_ node: ShortNode
6565
) -> String {
66-
let attributesList = renderAttributeList(node.attributes)
66+
let attributesList = renderAttributeList(node.properties)
6767
return "<\(node.name)\(attributesList)>"
6868
}
6969

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import DOM
2+
3+
public protocol Attribute: Sendable {
4+
var name: String { get }
5+
var value: String? { get }
6+
}
7+
8+
public struct RawAttribute: Attribute {
9+
public var name: String
10+
public var value: String?
11+
12+
public init(
13+
name: String,
14+
value: String? = nil
15+
) {
16+
self.name = name
17+
self.value = value
18+
}
19+
}

Sources/SGML/Attribute.swift renamed to Sources/SGML/Attribute/AttributeStore.swift

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
import DOM
22

3-
public protocol Attribute: Sendable {
4-
var name: String { get }
5-
var value: String? { get }
6-
}
7-
8-
public struct RawAttribute: Attribute {
9-
public var name: String
10-
public var value: String?
11-
12-
public init(
13-
name: String,
14-
value: String? = nil
15-
) {
16-
self.name = name
17-
self.value = value
18-
}
19-
}
20-
21-
public struct Attributes: Sendable {
3+
public struct AttributeStore: Sendable {
224

235
var storage: [String: [String?]]
246

@@ -70,7 +52,7 @@ public struct Attributes: Sendable {
7052
// }
7153
}
7254

73-
public var domAttributes: [DOM.Attribute] {
55+
public var properties: [Property] {
7456
storage.map { name, value in
7557
let values = value.compactMap { $0 }.sorted()
7658
return .init(

0 commit comments

Comments
 (0)