Skip to content

Commit

Permalink
A tag with builder & new tag builder expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Nov 29, 2021
1 parent b22ee02 commit a3ce262
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Sources/SwiftHtml/Components/TagBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public enum TagBuilder {
Tag(.init(type: .group), children: components.flatMap { $0 })
}

public static func buildExpression(_ expression: [Tag]) -> [Tag] {
expression
}

public static func buildExpression(_ expression: Tag) -> [Tag] {
[expression]
}

public static func buildEither(first component: [Tag]) -> [Tag] {
component
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftHtml/Html/Tags/A.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
/// - An active link is underlined and red
public final class A: Tag {

public init(_ value: String) {
super.init(Node(type: .standard, name: "a", contents: value))
public init(_ contents: String? = nil, @TagBuilder _ builder: () -> [Tag]) {
super.init(Node(type: .standard, name: "a", contents: contents), children: builder())
}

public convenience init(_ contents: String) {
self.init(contents) {}
}
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/SwiftHtmlTests/Tags/ATagTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 2021. 11. 29..
//

import XCTest
@testable import SwiftHtml

final class ATagTests: XCTestCase {

func testA() {

let doc = Document(.unspecified) {
A {
P("Hello")
}
.href("world")
}
XCTAssertEqual(DocumentRenderer(minify: true).render(doc), #"<a href="world"><p>Hello</p></a>"#)
}

}
10 changes: 10 additions & 0 deletions Tests/SwiftHtmlTests/Tags/FormTagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import XCTest

final class FormTagTests: XCTestCase {

func testGroup() {
let items = ["a", "b", "c"]
let doc = Document(.unspecified) {
Form {
items.map { P($0) }
}
}
XCTAssertEqual(DocumentRenderer(minify: true).render(doc), #"<form><p>a</p><p>b</p><p>c</p></form>"#)
}

func testForm() {
let doc = Document(.html) {
Form {
Expand Down

0 comments on commit a3ce262

Please sign in to comment.