Skip to content

Commit 0591f36

Browse files
committed
more tests & attributes
1 parent 5f61bed commit 0591f36

File tree

5 files changed

+122
-84
lines changed

5 files changed

+122
-84
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,13 @@ All standard tags support child elements by default.
227227

228228
## Future improvements
229229

230+
- [ ] Finish attributes (global, event)
231+
- [ ] Finish content models (use `assert` & types)
232+
- [ ] Get rid of public enums
230233
- [ ] Get rid of `@_exported SGML`
231234
- [ ] Add `OrderedDictionary` support?
232235

236+
233237
## Credits & references
234238

235239
- [HTML Standard](https://html.spec.whatwg.org/multipage/)

Sources/SwiftHTML/Attributes/ClassAttribute.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ public protocol ClassAttributeModifier {
1414

1515
extension ClassAttributeModifier where Self: Attributes & Mutable {
1616

17-
/// Sets a class attribute.
18-
public func `class`(
19-
_ value: String?
20-
) -> Self {
21-
setClass(value)
22-
}
23-
2417
/// Sets a class attribute.
2518
public func setClass(
2619
_ value: String?
@@ -53,4 +46,31 @@ extension ClassAttributeModifier where Self: Attributes & Mutable {
5346
addAttribute(ClassAttribute(value))
5447
}
5548
}
49+
50+
// MARK: -
51+
52+
/// Sets a class attribute.
53+
public func `class`(
54+
_ value: String?
55+
) -> Self {
56+
setClass(value)
57+
}
58+
59+
/// Sets a class attribute.
60+
public func `class`(
61+
_ values: [String]
62+
) -> Self {
63+
var mutatingSelf = self
64+
for item in values {
65+
mutatingSelf = mutatingSelf.addClass(item)
66+
}
67+
return mutatingSelf
68+
}
69+
70+
/// Sets a class attribute.
71+
public func `class`(
72+
_ values: String...
73+
) -> Self {
74+
`class`(values)
75+
}
5676
}

Sources/SwiftHTML/Attributes/DownloadAttribute.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ extension DownloadAttributeModifier where Self: Attributes & Mutable {
2020
) -> Self {
2121
setAttribute(DownloadAttribute(value))
2222
}
23+
24+
public func download() -> Self {
25+
setAttribute(DownloadAttribute(nil))
26+
}
2327
}

Sources/SwiftHTML/Attributes/_GlobalAttributes.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public protocol GlobalAttributeModifier:
1313

1414
}
1515

16+
extension GlobalAttributeModifier where Self: Attributes & Mutable {
17+
18+
public func spellcheck(
19+
_ value: Bool
20+
) -> Self {
21+
setAttribute(name: "spellcheck", value: String(value))
22+
}
23+
}
24+
1625
public protocol HTMLAttribute: Attribute {
1726

1827
}
@@ -49,7 +58,7 @@ extension HTMLAttribute {
4958
// lang
5059
// nonce
5160
// popover
52-
// spellcheck
61+
// spellcheck
5362
// ✅ style
5463
// tabindex
5564
// ✅ title
@@ -101,9 +110,7 @@ extension HTMLAttribute {
101110
// }
102111
//
103112
// /// Specifies whether the element is to have its spelling and grammar checked or not
104-
// public func spellcheck(_ value: Bool) -> Self {
105-
// attribute("spellcheck", String(value))
106-
// }
113+
107114
//
108115
// /// Specifies the tabbing order of an element
109116
// public func tabindex(_ value: Int) -> Self {

Tests/SwiftHTMLTests/SwiftHTMLTestSuite.swift

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -63,77 +63,80 @@ struct SwiftHTMLTestSuite {
6363
#expect(result == expectation)
6464
}
6565

66-
//
67-
// func testHtmlDocument() {
68-
// let doc = Document(.html) {
69-
// Html {
70-
// Head {
71-
// Title("Hello Swift DSL")
72-
//
73-
// Meta().charset("utf-8")
74-
// Meta().name(.viewport)
75-
// .content("width=device-width, initial-scale=1")
76-
//
77-
// Link(rel: .stylesheet).href("./css/style.css")
78-
// }
79-
// Body {
80-
// Main {
81-
// Div {
82-
// Section {
83-
// Img(
84-
// src: "./images/swift.png",
85-
// alt: "Swift Logo"
86-
// )
87-
// .title("Picture of the Swift Logo")
88-
// H1("Lorem ipsum")
89-
// .class("red")
90-
// P(
91-
// "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta."
92-
// )
93-
// .class(["green", "blue"])
94-
// .spellcheck(false)
95-
// }
96-
// A("Hello Swift HTML DSL!")
97-
// .href("https://swift.org")
98-
// .target(.blank)
99-
// .download()
100-
// Abbr("WHO")
101-
// .title("World Health Organization")
102-
// }
103-
// }
104-
// .class("container")
105-
//
106-
// Script()
107-
// .src("./javascript/main.js")
108-
// }
109-
// }
110-
// }
111-
//
112-
// let html = """
113-
// <!DOCTYPE html>
114-
// <html>
115-
// <head>
116-
// <title>Hello Swift DSL</title>
117-
// <meta charset="utf-8">
118-
// <meta name="viewport" content="width=device-width, initial-scale=1">
119-
// <link rel="stylesheet" href="./css/style.css">
120-
// </head>
121-
// <body>
122-
// <main class="container">
123-
// <div>
124-
// <section>
125-
// <img src="./images/swift.png" alt="Swift Logo" title="Picture of the Swift Logo">
126-
// <h1 class="red">Lorem ipsum</h1>
127-
// <p class="green blue" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta.</p>
128-
// </section>
129-
// <a href="https://swift.org" target="_blank" download>Hello Swift HTML DSL!</a>
130-
// <abbr title="World Health Organization">WHO</abbr>
131-
// </div>
132-
// </main>
133-
// <script src="./javascript/main.js"></script>
134-
// </body>
135-
// </html>
136-
// """
137-
// assert(doc: doc, html: html)
138-
// }
66+
@Test
67+
func compatiblityWithVersion1Syntax() async throws {
68+
let html = Html {
69+
Head {
70+
Title("Hello Swift DSL")
71+
72+
Meta().charset("utf-8")
73+
Meta().name(.viewport)
74+
.content("width=device-width, initial-scale=1")
75+
76+
Link(rel: .stylesheet).href("./css/style.css")
77+
}
78+
Body {
79+
Main {
80+
Div {
81+
Section {
82+
Img(
83+
src: "./images/swift.png",
84+
alt: "Swift Logo"
85+
)
86+
.title("Picture of the Swift Logo")
87+
H1("Lorem ipsum")
88+
.class("red")
89+
P(
90+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta."
91+
)
92+
.class(["green", "blue"])
93+
.spellcheck(false)
94+
}
95+
A("Hello Swift HTML DSL!")
96+
.href("https://swift.org")
97+
.target(.blank)
98+
.download()
99+
Abbr("WHO")
100+
.title("World Health Organization")
101+
}
102+
}
103+
.class("container")
104+
105+
Script()
106+
.src("./javascript/main.js")
107+
}
108+
}
109+
110+
let renderer = Renderer(indent: 4)
111+
let doc = Document(type: .html, root: html)
112+
113+
let expectation = #"""
114+
<!doctype html>
115+
<html>
116+
<head>
117+
<title>Hello Swift DSL</title>
118+
<meta charset="utf-8">
119+
<meta content="width=device-width, initial-scale=1" name="viewport">
120+
<link href="./css/style.css" rel="stylesheet">
121+
</head>
122+
<body>
123+
<main class="container">
124+
<div>
125+
<section>
126+
<img alt="Swift Logo" src="./images/swift.png" title="Picture of the Swift Logo">
127+
<h1 class="red">Lorem ipsum</h1>
128+
<p class="blue green" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta.</p>
129+
</section>
130+
<a href="https://swift.org" target="_blank" download>Hello Swift HTML DSL!</a>
131+
<abbr title="World Health Organization">WHO</abbr>
132+
</div>
133+
</main>
134+
<script src="./javascript/main.js">
135+
</body>
136+
</html>
137+
"""#
138+
139+
let result = renderer.render(document: doc)
140+
#expect(result == expectation)
141+
}
139142
}

0 commit comments

Comments
 (0)