Skip to content

Commit f4fa5bc

Browse files
committed
fix tag builder for Swift 5.8
1 parent 1fd007c commit f4fa5bc

File tree

22 files changed

+642
-568
lines changed

22 files changed

+642
-568
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ open class Description: Tag {
119119

120120
open class Rss: Tag {
121121

122-
public init(@TagBuilder _ builder: () -> [Tag]) {
122+
public init(@TagBuilder _ builder: () -> Tag) {
123123
super.init(builder())
124124
setAttributes([
125125
.init(key: "version", value: "2.0"),

Sources/SwiftHtml/Tags/Map.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// The `<map>` element contains a number of `<area>` elements, that defines the clickable areas in the image map.
1313
open class Map: Tag {
1414

15-
public init(name: String, @TagBuilder _ builder: () -> [Tag]) {
16-
super.init(builder())
15+
public init(name: String, @TagBuilder _ builder: () -> Tag) {
16+
super.init([builder()])
1717
setAttributes([
1818
.init(key: "name", value: name)
1919
])

Sources/SwiftRss/Rss.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// https://validator.w3.org/feed/docs/rss2.html#ltttlgtSubelementOfLtchannelgt
99
open class Rss: Tag {
1010

11-
public init(@TagBuilder _ builder: () -> [Tag]) {
12-
super.init(builder())
11+
public init(@TagBuilder _ builder: () -> Tag) {
12+
super.init([builder()])
1313
setAttributes([
1414
.init(key: "version", value: "2.0"),
1515
// .init(key: "xmlns:atom", value: "http://www.w3.org/2005/Atom"),

Sources/SwiftSgml/Tag.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ open class Tag {
2828
}
2929

3030
/// initialize a new Tag with children using a builder
31-
public convenience init(@TagBuilder _ builder: () -> [Tag]) {
32-
self.init(builder())
31+
public convenience init(@TagBuilder _ builder: () -> Tag) {
32+
self.init([builder()])
3333
}
3434

3535
// /// initialize a new Tag with children using an async throwing builder

Sources/SwiftSgml/TagBuilder.swift

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,28 @@
88
@resultBuilder
99
public enum TagBuilder {
1010

11-
public static func buildBlock(_ components: [Tag]...) -> [Tag] {
12-
components.flatMap { $0 }
13-
}
14-
1511
public static func buildBlock(_ components: [Tag]...) -> Tag {
16-
let flat = components.flatMap { $0 }
17-
if flat.count < 2, let first = flat.first {
18-
return first
19-
}
20-
return GroupTag(flat)
12+
GroupTag(components.flatMap { $0 })
2113
}
22-
23-
public static func buildExpression(_ expression: [Tag]) -> [Tag] {
24-
expression
14+
15+
public static func buildBlock(_ components: Tag...) -> Tag {
16+
GroupTag(components)
2517
}
2618

27-
public static func buildExpression(_ expression: Tag) -> [Tag] {
28-
[expression]
19+
public static func buildOptional(_ component: Tag?) -> Tag {
20+
component ?? GroupTag()
2921
}
30-
31-
public static func buildEither(first component: [Tag]) -> [Tag] {
22+
23+
public static func buildEither(first component: Tag) -> Tag {
3224
component
3325
}
3426

35-
public static func buildEither(second component: [Tag]) -> [Tag] {
27+
public static func buildEither(second component: Tag) -> Tag {
3628
component
3729
}
3830

39-
public static func buildOptional(_ component: [Tag]?) -> [Tag] {
40-
component ?? []
41-
}
42-
43-
public static func buildArray(_ components: [[Tag]]) -> [Tag] {
44-
components.flatMap { $0 }
31+
public static func buildArray(_ components: [Tag]) -> Tag {
32+
GroupTag(components)
4533
}
4634
}
4735

Sources/SwiftSitemap/SitemapIndex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
open class SitemapIndex: Tag {
99

10-
public init(@TagBuilder _ builder: () -> [Tag]) {
11-
super.init(builder())
10+
public init(@TagBuilder _ builder: () -> Tag) {
11+
super.init([builder()])
1212
setAttributes([
1313
.init(key: "xmlns", value: "http://www.sitemaps.org/schemas/sitemap/0.9"),
1414
])

Sources/SwiftSitemap/UrlSet.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
open class UrlSet: Tag {
99

10-
public init(@TagBuilder _ builder: () -> [Tag]) {
11-
super.init(builder())
10+
public init(@TagBuilder _ builder: () -> Tag) {
11+
super.init([builder()])
1212
setAttributes([
1313
.init(key: "xmlns", value: "http://www.sitemaps.org/schemas/sitemap/0.9"),
1414
])

Tests/SwiftHtmlTests/SwiftHtmlTests.swift

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import XCTest
1010

1111
extension Div {
1212

13-
convenience init(_ value: String, @TagBuilder _ builder: () -> [Tag]) {
13+
convenience init(_ value: String, @TagBuilder _ builder: () -> Tag) {
1414
self.init(builder())
1515
self.setAttributes([
1616
.init(key: "some-key", value: value)
@@ -19,7 +19,7 @@ extension Div {
1919
}
2020

2121
final class SwiftHtmlTests: XCTestCase {
22-
22+
2323
func testCustomInitWithAttribute() {
2424
let doc = Document {
2525
Div("some-value") {
@@ -30,15 +30,15 @@ final class SwiftHtmlTests: XCTestCase {
3030
let html = DocumentRenderer(minify: true).render(doc)
3131
XCTAssertEqual(#"<div some-key="some-value"><span>a</span><span>b</span></div>"#, html)
3232
}
33-
33+
3434
func testClassAttribute() {
3535
let doc = Document {
3636
Span("").class("a", "b", "", "b", "c")
3737
}
3838
let html = DocumentRenderer(minify: true).render(doc)
3939
XCTAssertEqual(#"<span class="a b b c"></span>"#, html)
4040
}
41-
41+
4242
func testMultipleClasses() {
4343
let doc = Document {
4444
Span("")
@@ -48,7 +48,7 @@ final class SwiftHtmlTests: XCTestCase {
4848
let html = DocumentRenderer(minify: true).render(doc)
4949
XCTAssertEqual(#"<span class="d e f"></span>"#, html)
5050
}
51-
51+
5252
func testClassManipulation() {
5353
let doc = Document {
5454
Span("")
@@ -61,7 +61,7 @@ final class SwiftHtmlTests: XCTestCase {
6161
let html = DocumentRenderer(minify: true).render(doc)
6262
XCTAssertEqual(#"<span class="a f"></span>"#, html)
6363
}
64-
64+
6565
func testAddClass() {
6666
let doc = Document {
6767
Span("")
@@ -71,7 +71,7 @@ final class SwiftHtmlTests: XCTestCase {
7171
let html = DocumentRenderer(minify: true).render(doc)
7272
XCTAssertEqual(#"<span class="a b c d"></span>"#, html)
7373
}
74-
74+
7575
func testRemoveClass() {
7676
let doc = Document {
7777
Span("")
@@ -81,7 +81,7 @@ final class SwiftHtmlTests: XCTestCase {
8181
let html = DocumentRenderer(minify: true).render(doc)
8282
XCTAssertEqual(#"<span class="a c"></span>"#, html)
8383
}
84-
84+
8585
func testRemoveLastClass() {
8686
let doc = Document {
8787
Span("")
@@ -91,7 +91,7 @@ final class SwiftHtmlTests: XCTestCase {
9191
let html = DocumentRenderer(minify: true).render(doc)
9292
XCTAssertEqual(#"<span></span>"#, html)
9393
}
94-
94+
9595
func testToggleAddClass() {
9696
let doc = Document {
9797
Span("")
@@ -101,7 +101,7 @@ final class SwiftHtmlTests: XCTestCase {
101101
let html = DocumentRenderer(minify: true).render(doc)
102102
XCTAssertEqual(#"<span class="a b c d"></span>"#, html)
103103
}
104-
104+
105105
func testToggleRemoveClass() {
106106
let doc = Document {
107107
Span("")
@@ -111,7 +111,7 @@ final class SwiftHtmlTests: XCTestCase {
111111
let html = DocumentRenderer(minify: true).render(doc)
112112
XCTAssertEqual(#"<span class="a c"></span>"#, html)
113113
}
114-
114+
115115
func testSetEmptyStyle() {
116116
let doc = Document {
117117
Span("")
@@ -120,7 +120,7 @@ final class SwiftHtmlTests: XCTestCase {
120120
let html = DocumentRenderer(minify: true).render(doc)
121121
XCTAssertEqual(#"<span></span>"#, html)
122122
}
123-
123+
124124
func testTextTag() {
125125
let doc = Document() {
126126
P {
@@ -130,18 +130,20 @@ final class SwiftHtmlTests: XCTestCase {
130130
}
131131
}
132132

133-
XCTAssertEqual(DocumentRenderer().render(doc), """
134-
<p>
135-
<span>foo</span>
136-
bar
137-
<span>baz</span>
138-
</p>
139-
""")
133+
let html = """
134+
<p>
135+
<span>foo</span>
136+
bar
137+
<span>baz</span>
138+
</p>
139+
"""
140+
141+
assert(doc: doc, html: html)
140142
}
141-
143+
142144
func testMultiGroupTagBuilderAndRenderer() {
143145
let values: [String] = ["a", "b", "c"]
144-
146+
145147
let doc = Document {
146148
Div {
147149
values.map { item -> Tag in
@@ -153,24 +155,25 @@ final class SwiftHtmlTests: XCTestCase {
153155
}
154156
}
155157

156-
XCTAssertEqual(DocumentRenderer().render(doc), """
157-
<div>
158-
<h1>a</h1>
159-
<p>a</p>
160-
<h1>b</h1>
161-
<p>b</p>
162-
<h1>c</h1>
163-
<p>c</p>
164-
</div>
165-
""")
158+
let html = """
159+
<div>
160+
<h1>a</h1>
161+
<p>a</p>
162+
<h1>b</h1>
163+
<p>b</p>
164+
<h1>c</h1>
165+
<p>c</p>
166+
</div>
167+
"""
168+
assert(doc: doc, html: html)
166169
}
167-
170+
168171
func testHtmlDocument() {
169172
let doc = Document(.html) {
170173
Html {
171174
Head {
172175
Title("Hello Swift DSL")
173-
176+
174177
Meta().charset("utf-8")
175178
Meta().name(.viewport).content("width=device-width, initial-scale=1")
176179

@@ -204,30 +207,31 @@ final class SwiftHtmlTests: XCTestCase {
204207
}
205208
}
206209

207-
XCTAssertEqual(DocumentRenderer().render(doc), """
208-
<!DOCTYPE html>
209-
<html>
210-
<head>
211-
<title>Hello Swift DSL</title>
212-
<meta charset="utf-8">
213-
<meta name="viewport" content="width=device-width, initial-scale=1">
214-
<link rel="stylesheet" href="./css/style.css">
215-
</head>
216-
<body>
217-
<main class="container">
218-
<div>
219-
<section>
220-
<img src="./images/swift.png" alt="Swift Logo" title="Picture of the Swift Logo">
221-
<h1 class="red">Lorem ipsum</h1>
222-
<p class="green blue" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta.</p>
223-
</section>
224-
<a href="https://swift.org" target="_blank" download>Hello Swift HTML DSL!</a>
225-
<abbr title="World Health Organization">WHO</abbr>
226-
</div>
227-
</main>
228-
<script src="./javascript/main.js"></script>
229-
</body>
230-
</html>
231-
""")
210+
let html = """
211+
<!DOCTYPE html>
212+
<html>
213+
<head>
214+
<title>Hello Swift DSL</title>
215+
<meta charset="utf-8">
216+
<meta name="viewport" content="width=device-width, initial-scale=1">
217+
<link rel="stylesheet" href="./css/style.css">
218+
</head>
219+
<body>
220+
<main class="container">
221+
<div>
222+
<section>
223+
<img src="./images/swift.png" alt="Swift Logo" title="Picture of the Swift Logo">
224+
<h1 class="red">Lorem ipsum</h1>
225+
<p class="green blue" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pretium leo eu euismod porta.</p>
226+
</section>
227+
<a href="https://swift.org" target="_blank" download>Hello Swift HTML DSL!</a>
228+
<abbr title="World Health Organization">WHO</abbr>
229+
</div>
230+
</main>
231+
<script src="./javascript/main.js"></script>
232+
</body>
233+
</html>
234+
"""
235+
assert(doc: doc, html: html)
232236
}
233237
}

0 commit comments

Comments
 (0)