diff --git a/Html.playground/Contents.swift b/Html.playground/Contents.swift
index cd025f4..03efaa6 100644
--- a/Html.playground/Contents.swift
+++ b/Html.playground/Contents.swift
@@ -32,30 +32,31 @@ code {
"""
/// A document built in the HTML DSL.
-let doc = html([
- head([
- style(unsafe: stylesheet)
- ]),
- body([
- h1(["đș HTML"]),
- p(["""
+let doc = document(
+ doctype,
+ html(
+ head(
+ style(unsafe: stylesheet)
+ ),
+ body(
+ h1("đș HTML"),
+ p("""
A Swift DSL for type-safe, extensible, and transformable HTML documents.
"""
- ]),
-
- h2(["Motivation"]),
- p(["""
+ ),
+ h2("Motivation"),
+ p("""
When building server-side application in Swift it is important to be able to render HTML documents. The current best practice in the community is to use templating languages like Stencil, Mustache, Handlebars, Leaf and others. However, templating languages are inherently unsafe due to the API being stringly typed. The vast majority of errors that can arise in creating a template happen only at runtime, including typos and type mismatches.
-"""]),
- p(["""
+"""),
+ p("""
Thatâs unfortunate because we are used to working in Swift, which is strongly typed, and many potential bugs are discovered at compile time rather than runtime. Our approach is to instead embed HTML documents directly into Swift types so that we immediately get all of the features and safety Swift has to offer.
-"""]),
-
- h2(["Examples"]),
- p(["""
+"""
+ ),
+ h2("Examples"),
+ p("""
HTML documents can be created with this library in a tree-like fashion, much like how you might create a nested JSON document:
-"""]),
- pre(["""
+"""),
+ pre("""
import Html
let document = html([
@@ -64,21 +65,20 @@ let document = html([
p(["Youâve found our site!"])
])
])
-"""]),
-
- p([
- "Underneath the hood these tag functions ",
- code(["html"]),
- ", ", code(["body"]),
- ", ", code(["h1"]),
- "etc. are just creating and nesting instances of a ",
- code(["Node"]),
- " type, which is a simple Swift enum. The cool part is that because ",
- code(["Node"]),
- " is just a simple Swift type, we can transform it in all types of intersting ways. For a silly example, what if we wanted to remove all instances of exclamation marks from our document?"
- ]),
-
- pre(["""
+"""),
+
+ p(
+ "Underneath the hood these tag functions ",
+ code("html"),
+ ", ", code("body"),
+ ", ", code("h1"),
+ "etc. are just creating and nesting instances of a ",
+ code("Node"),
+ " type, which is a simple Swift enum. The cool part is that because ",
+ code("Node"),
+ " is just a simple Swift type, we can transform it in all types of intersting ways. For a silly example, what if we wanted to remove all instances of exclamation marks from our document?"
+ ),
+ pre("""
func unexclaim(_ node: Node) -> Node {
switch node {
case .comment:
@@ -101,25 +101,25 @@ func unexclaim(_ node: Node) -> Node {
unexclaim(document) // Node
"""
- ]),
+ ),
- p([
- "And of course you can first run the document through the ", code(["unexlaim"]), " transformation, and then render it:"
- ]),
+ p(
+ "And of course you can first run the document through the ", code("unexclaim"), " transformation, and then render it:"
+ ),
- pre(["""
+ pre("""
render(unexclaim(document))
//
Welcome. Youâve found our site.
"""
- ]),
-
- ])
- ])
+ )
+ )
+ )
+)
/// A function that "redacts" an HTML document by transforming all text nodes
/// into â-sequences of characters.
func redacted(node: Node) -> Node {
- func redacted(string: String) -> String {
+ func redact(string: String) -> String {
return string
.split(separator: " ")
.map { String(repeating: "â", count: $0.count )}
@@ -135,7 +135,7 @@ func redacted(node: Node) -> Node {
return .comment("")
// Raw strings will be redacted
case let .raw(string):
- return .raw(redacted(string: string))
+ return .raw(redact(string: string))
// Style tags will not be redacted
case .element("style", _, _):
return node
@@ -148,11 +148,14 @@ func redacted(node: Node) -> Node {
children
)
// All other elements will have their children redacted.
- case let .element(tag, attrs, children):
- return .element(tag, attrs, children.map(redacted(node:)))
+ case let .element(tag, attrs, child):
+ return .element(tag, attrs, redacted(node: child))
+ // All fragments will have their children redacted.
+ case let .fragment(children):
+ return .fragment(children.map(redacted(node:)))
// Text nodes will be redacted
case let .text(string):
- return .text(redacted(string: string))
+ return .text(redact(string: string))
}
}
diff --git a/README.md b/README.md
index cfe6c43..c739b1c 100644
--- a/README.md
+++ b/README.md
@@ -29,12 +29,12 @@ HTML documents can be created in a tree-like fashion, much like you might create
```swift
import Html
-let document = html([
- body([
- h1(["Welcome!"]),
- p(["Youâve found our site!"])
- ])
- ])
+let document = html(
+ body(
+ h1("Welcome!"),
+ p("Youâve found our site!")
+ )
+)
```
Underneath the hood these tag functions `html`, `body`, `h1`, etc., are just creating and nesting instances of a `Node` type, which is a simple Swift enum. Because `Node` is just a simple Swift type, we can transform it in all kinds of interesting ways. For a silly example, what if we wanted to remove all instances of exclamation marks from our document?
@@ -54,6 +54,10 @@ func unexclaim(_ node: Node) -> Node {
// Recursively transform all of the children of an element
return .element(tag, attrs, children.map(unexclaim))
+ case let .fragment(children):
+ // Recursively transform all of the children of a fragment
+ return .fragment(children.map(unexclaim))
+
case let .raw(string), .text(string):
// Transform text nodes by replacing exclamation marks with periods.
return string.replacingOccurrences(of: "!", with: ".")
@@ -95,31 +99,32 @@ Here the `src` attribute takes a string, but `width` and `height` take integers,
For a more advanced example, `` tags can only be placed inside `` and `` tags, and we can represent this fact so that itâs impossible to construct an invalid document:
```swift
-let listTag = ul([
- li(["Cat"]),
- li(["Dog"]),
- li(["Rabbit"])
- ]) // â
Compiles!
+let listTag = ul(
+ li("Cat"),
+ li("Dog"),
+ li("Rabbit")
+) // â
Compiles!
render(listTag)
//
-div([
- li(["Cat"]),
- li(["Dog"]),
- li(["Rabbit"])
- ]) // đ Compile error
+div(
+ li("Cat"),
+ li("Dog"),
+ li("Rabbit")
+) // đ Compile error
```
## Design
-The core of the library is a single enum with 5 cases:
+The core of the library is a single enum with 6 cases:
```swift
public enum Node {
case comment(String)
case doctype(String)
- indirect case element(String, [(key: String, value: String?)], [Node])
+ indirect case element(String, [(key: String, value: String?)], Node)
+ indirect case fragment([Node])
case raw(String)
case text(String)
}
@@ -138,27 +143,24 @@ Node.element("html", [], [
// versus
// Using helper functions
-html([
- body([
- h1(["Welcome!"]),
- p(["Youâve found our site!"])
- ])
+html(
+ body(
+ h1("Welcome!"),
+ p("Youâve found our site!")
+ )
+)
```
This makes the âSwiftificationâ of an HTML document looks very similar to the original document.
## FAQ
-
### Why would I use this over a templating language?
diff --git a/Sources/Html/DebugRender.swift b/Sources/Html/DebugRender.swift
index 66aa528..4f74e7f 100644
--- a/Sources/Html/DebugRender.swift
+++ b/Sources/Html/DebugRender.swift
@@ -86,14 +86,16 @@ public func debugRender(_ node: Node, config: Config = .pretty) -> String {
output.append(">")
output.append(config.newline)
guard !children.isEmpty || !voidElements.contains(tag) else { return }
- for node in children {
- debugRenderHelp(node, into: &output, config: config, indentation: indentation + config.indentation)
- }
+ debugRenderHelp(children, into: &output, config: config, indentation: indentation + config.indentation)
output.append(indentation)
output.append("")
output.append(tag)
output.append(">")
output.append(config.newline)
+ case let .fragment(children):
+ for node in children {
+ debugRenderHelp(node, into: &output, config: config, indentation: indentation)
+ }
case let .raw(string), let .text(string):
guard !string.isEmpty else { return }
output.append(indentation)
diff --git a/Sources/Html/Elements.swift b/Sources/Html/Elements.swift
index 834c104..7d26b2a 100644
--- a/Sources/Html/Elements.swift
+++ b/Sources/Html/Elements.swift
@@ -1,9 +1,9 @@
-public func element(_ name: StaticString, _ attribs: [Attribute], _ children: [Node]) -> Node {
- return .element(String(describing: name), attribs.map { ($0.key, $0.value) }, children)
+public func element(_ name: StaticString, _ attribs: [Attribute] = [], _ children: Node...) -> Node {
+ return .element(String(describing: name), attribs.map { ($0.key, $0.value) }, .fragment(children))
}
-public func element(_ name: StaticString, _ children: [Node]) -> Node {
- return .element(String(describing: name), [], children)
+public func element(_ name: StaticString, _ children: Node...) -> Node {
+ return .element(String(describing: name), [], .fragment(children))
}
public struct ChildOf {
@@ -13,6 +13,18 @@ public struct ChildOf {
}
}
+extension ChildOf {
+ public static func fragment(_ children: [ChildOf]) -> ChildOf {
+ return .init(.fragment(children.map { $0.rawValue }))
+ }
+}
+
+extension ChildOf: ExpressibleByArrayLiteral {
+ public init(arrayLiteral elements: ChildOf...) {
+ self = .fragment(elements)
+ }
+}
+
public enum Tag {
public enum A {}
public enum Abbr {}
@@ -123,15 +135,15 @@ public enum Tag {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func a(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("a", attribs, content)
+public func a(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("a", attribs, .fragment(content))
}
/// The `` element represents either a hyperlink (a hypertext anchor) labeled by its contents, or a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.
///
/// - Parameter content: Child nodes.
-public func a(_ content: [Node]) -> Node {
- return a([], content)
+public func a(_ content: Node...) -> Node {
+ return a([], .fragment(content))
}
/// The `` element represents an abbreviation or acronym, optionally with its expansion.
@@ -139,15 +151,15 @@ public func a(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func abbr(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("abbr", attribs, content)
+public func abbr(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("abbr", attribs, .fragment(content))
}
/// The `` element represents an abbreviation or acronym, optionally with its expansion.
///
/// - Parameter content: Child nodes.
-public func abbr(_ content: [Node]) -> Node {
- return abbr([], content)
+public func abbr(_ content: Node...) -> Node {
+ return abbr([], .fragment(content))
}
/// The `` element represents contact information for a person, people or organization. It should include physical and/or digital location/contact information and a means of identifying a person(s) or organization the information pertains to.
@@ -155,15 +167,15 @@ public func abbr(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func address(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("address", attribs, content)
+public func address(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("address", attribs, .fragment(content))
}
/// The `` element represents contact information for a person, people or organization. It should include physical and/or digital location/contact information and a means of identifying a person(s) or organization the information pertains to.
///
/// - Parameter content: Child nodes.
-public func address(_ content: [Node]) -> Node {
- return address([], content)
+public func address(_ content: Node...) -> Node {
+ return address([], .fragment(content))
}
/// The ` ` element represents either a hyperlink with some text and a corresponding area on an image
@@ -177,8 +189,8 @@ public func area(_ attribs: [Attribute]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func article(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("article", attribs, content)
+public func article(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("article", attribs, .fragment(content))
}
/// The `` element represents a complete, or self-contained, composition in a document, page,
@@ -186,8 +198,8 @@ public func article(_ attribs: [Attribute], _ content: [Node]) -> N
/// report, a blog or other social media post.
///
/// - Parameter content: Child nodes.
-public func article(_ content: [Node]) -> Node {
- return article([], content)
+public func article(_ content: Node...) -> Node {
+ return article([], .fragment(content))
}
/// The `` element represents a section of a page that consists of content that is tangentially related to the content of the parenting sectioning content, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography.
@@ -195,15 +207,15 @@ public func article(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func aside(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("aside", attribs, content)
+public func aside(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("aside", attribs, .fragment(content))
}
/// The `` element represents a section of a page that consists of content that is tangentially related to the content of the parenting sectioning content, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography.
///
/// - Parameter content: Child nodes.
-public func aside(_ content: [Node]) -> Node {
- return aside([], content)
+public func aside(_ content: Node...) -> Node {
+ return aside([], .fragment(content))
}
/// An `` element represents a sound or audio stream.
@@ -215,17 +227,17 @@ public func aside(_ content: [Node]) -> Node {
public func audio(
_ attribs: [Attribute],
_ content: [ChildOf],
- _ transparent: [Node] = [])
+ _ transparent: Node = [])
-> Node {
- return element("audio", attribs, content.map { $0.rawValue } + transparent)
+ return element("audio", attribs, .fragment(content.map { $0.rawValue } + [transparent]))
}
/// An `` element represents a sound or audio stream.
///
/// - Parameter content: Child nodes.
/// - transparent: Additional child nodes that render as content for older Web browsers which do not support ``
-public func audio(_ content: [ChildOf], _ transparent: [Node] = []) -> Node {
+public func audio(_ content: [ChildOf], _ transparent: Node = []) -> Node {
return audio([], content, transparent)
}
@@ -234,15 +246,15 @@ public func audio(_ content: [ChildOf], _ transparent: [Node] = []) -
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func b(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("b", attribs, content)
+public func b(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("b", attribs, .fragment(content))
}
/// The `` element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood, such as key words in a document abstract, product names in a review, actionable words in interactive text-driven software, or an article lede.
///
/// - Parameter content: Child nodes.
-public func b(_ content: [Node]) -> Node {
- return b([], content)
+public func b(_ content: Node...) -> Node {
+ return b([], .fragment(content))
}
/// The ` ` element allows authors to specify the document base URL for the purposes of parsing URLs, and the name of the default browsing context for the purposes of following hyperlinks. The element does not represent any content beyond this information.
@@ -258,15 +270,15 @@ public func base(_ attribs: [Attribute]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func bdi(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("bdi", attribs, content)
+public func bdi(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("bdi", attribs, .fragment(content))
}
/// The `` element represents a span of text that is to be isolated from its surroundings for the purposes of bidirectional text formatting.
///
/// - Parameter content: Child nodes.
-public func bdi(_ content: [Node]) -> Node {
- return bdi([], content)
+public func bdi(_ content: Node...) -> Node {
+ return bdi([], .fragment(content))
}
/// A sub-set of directions, valid for `` elements.
@@ -283,8 +295,8 @@ public enum BdoDirection: String {
/// - dir: The element's text directionality.
/// - attribs: Attributes.
/// - content: Child nodes.
-public func bdo(dir: BdoDirection, _ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("bdo", [.init("dir", dir.rawValue)] + attribs, content)
+public func bdo(dir: BdoDirection, _ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("bdo", [.init("dir", dir.rawValue)] + attribs, .fragment(content))
}
/// The `` element represents explicit text directionality formatting control for its children. It allows authors to override the Unicode bidirectional algorithm by explicitly specifying a direction override.
@@ -292,8 +304,8 @@ public func bdo(dir: BdoDirection, _ attribs: [Attribute], _ content: [
/// - Parameters:
/// - dir: The element's text directionality.
/// - content: Child nodes.
-public func bdo(dir: BdoDirection, _ content: [Node]) -> Node {
- return bdo(dir: dir, [], content)
+public func bdo(dir: BdoDirection, _ content: Node...) -> Node {
+ return bdo(dir: dir, [], .fragment(content))
}
/// The `` element represents content that is quoted from another source, optionally with a citation which must be within a `` or `` element, and optionally with in-line changes such as annotations and abbreviations.
@@ -301,15 +313,15 @@ public func bdo(dir: BdoDirection, _ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func blockquote(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("blockquote", attribs, content)
+public func blockquote(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("blockquote", attribs, .fragment(content))
}
/// The `` element represents content that is quoted from another source, optionally with a citation which must be within a `` or `` element, and optionally with in-line changes such as annotations and abbreviations.
///
/// - Parameter content: Child nodes.
-public func blockquote(_ content: [Node]) -> Node {
- return blockquote([], content)
+public func blockquote(_ content: Node...) -> Node {
+ return blockquote([], .fragment(content))
}
/// The `` element represents the content of the document.
@@ -317,15 +329,15 @@ public func blockquote(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func body(_ attribs: [Attribute], _ content: [Node]) -> ChildOf {
- return .init(element("body", attribs, content))
+public func body(_ attribs: [Attribute], _ content: Node...) -> ChildOf {
+ return .init(element("body", attribs, .fragment(content)))
}
/// The `` element represents the content of the document.
///
/// - Parameter content: Child nodes.
-public func body(_ content: [Node]) -> ChildOf {
- return body([], content)
+public func body(_ content: Node...) -> ChildOf {
+ return body([], .fragment(content))
}
/// The ` ` element represents a line break.
@@ -336,15 +348,15 @@ public let br: Node = element("br", [])
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func button(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("button", attribs, content)
+public func button(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("button", attribs, .fragment(content))
}
/// The `` element represents a control allowing a user to trigger actions, when enabled. It is labeled by its content.
///
/// - Parameter content: Child nodes.
-public func button(_ content: [Node]) -> Node {
- return button([], content)
+public func button(_ content: Node...) -> Node {
+ return button([], .fragment(content))
}
/// The `` element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.
@@ -352,8 +364,8 @@ public func button(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func canvas(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("canvas", attribs, content)
+public func canvas(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("canvas", attribs, .fragment(content))
}
/// The `` element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.
@@ -361,8 +373,8 @@ public func canvas(_ attribs: [Attribute], _ content: [Node]) -> Nod
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func canvas(_ content: [Node]) -> Node {
- return canvas([], content)
+public func canvas(_ content: Node...) -> Node {
+ return canvas([], .fragment(content))
}
// TODO: "caption" can only be the first element of a "table"
@@ -371,16 +383,16 @@ public func canvas(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func caption(_ attribs: [Attribute], _ content: [Node]) -> ChildOf {
- return .init(element("caption", attribs, content))
+public func caption(_ attribs: [Attribute], _ content: Node...) -> ChildOf {
+ return .init(element("caption", attribs, .fragment(content)))
}
// TODO: "caption" can only be the first element of a "table"
/// The `` element represents the title of the table that is its parent, if it has a parent and that is a table element.
///
/// - Parameter content: Child nodes.
-public func caption(_ content: [Node]) -> ChildOf {
- return caption([], content)
+public func caption(_ content: Node...) -> ChildOf {
+ return caption([], .fragment(content))
}
/// The `` element represents a reference to a creative work. It must include the title of the work or the name of the author (person, people or organization) or an URL reference, or a reference in abbreviated form as per the conventions used for the addition of citation metadata.
@@ -388,15 +400,15 @@ public func caption(_ content: [Node]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func cite(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("cite", attribs, content)
+public func cite(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("cite", attribs, .fragment(content))
}
/// The `` element represents a reference to a creative work. It must include the title of the work or the name of the author (person, people or organization) or an URL reference, or a reference in abbreviated form as per the conventions used for the addition of citation metadata.
///
/// - Parameter content: Child nodes.
-public func cite(_ content: [Node]) -> Node {
- return cite([], content)
+public func cite(_ content: Node...) -> Node {
+ return cite([], .fragment(content))
}
/// The `` element represents a fragment of computer code. This could be an XML element name, a file name, a computer program, or any other string that a computer would recognize.
@@ -404,8 +416,8 @@ public func cite(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func code(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("code", attribs, content)
+public func code(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("code", attribs, .fragment(content))
}
/// The `` element represents a fragment of computer code. This could be an XML element name, a file name, a computer program, or any other string that a computer would recognize.
@@ -413,8 +425,8 @@ public func code(_ attribs: [Attribute], _ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func code(_ content: [Node]) -> Node {
- return code([], content)
+public func code(_ content: Node...) -> Node {
+ return code([], .fragment(content))
}
/// If a ` ` element has a parent and that is a `` element that itself has a parent that is a `` element, then the ` ` element represents one or more columns in the column group represented by that ``.
@@ -430,17 +442,17 @@ public func col(_ attribs: [Attribute]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func colgroup(_ attribs: [Attribute], _ content: [ChildOf])
+public func colgroup(_ attribs: [Attribute], _ content: ChildOf...)
-> ChildOf {
- return .init(element("colgroup", attribs, content.map { $0.rawValue }))
+ return .init(element("colgroup", attribs, ChildOf.fragment(content).rawValue))
}
/// The `` element represents a group of one or more columns in the `table` that is its parent, if it has a parent and that is a `` element.
///
/// - Parameter content: Child nodes.
-public func colgroup(_ content: [ChildOf]) -> ChildOf {
- return colgroup([], content)
+public func colgroup(_ content: ChildOf...) -> ChildOf {
+ return colgroup([], .fragment(content))
}
/// The `` element represents a description, part of a term-description group in a description list (`` element).
@@ -448,15 +460,15 @@ public func colgroup(_ content: [ChildOf]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func dd(_ attribs: [Attribute], _ content: [Node]) -> ChildOf {
- return .init(element("dd", attribs, content))
+public func dd(_ attribs: [Attribute], _ content: Node...) -> ChildOf {
+ return .init(element("dd", attribs, .fragment(content)))
}
/// The `` element represents a description, part of a term-description group in a description list (`` element).
///
/// - Parameter content: Child nodes.
-public func dd(_ content: [Node]) -> ChildOf {
- return dd([], content)
+public func dd(_ content: Node...) -> ChildOf {
+ return dd([], .fragment(content))
}
/// The `` element represents a removal from the document.
@@ -464,15 +476,15 @@ public func dd(_ content: [Node]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func del(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("del", attribs, content)
+public func del(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("del", attribs, .fragment(content))
}
/// The `` element represents a removal from the document.
///
/// - Parameter content: Child nodes.
-public func del(_ content: [Node]) -> Node {
- return del([], content)
+public func del(_ content: Node...) -> Node {
+ return del([], .fragment(content))
}
// TODO: required first child element "summary"
@@ -481,16 +493,16 @@ public func del(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func details(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("details", attribs, content)
+public func details(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("details", attribs, .fragment(content))
}
// TODO: required first child element "summary"
/// The `` element represents a disclosure widget from which the user can obtain additional information or controls.
///
/// - Parameter content: Child nodes.
-public func details(_ content: [Node]) -> Node {
- return details([], content)
+public func details(_ content: Node...) -> Node {
+ return details([], .fragment(content))
}
/// The `` element represents the defining instance of a term. The term-description group, ``, `
` or `` element that is the nearest ancestor of the `` element must also contain the definition(s) for the term given by the `` element.
@@ -498,15 +510,15 @@ public func details(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func dfn(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("dfn", attribs, content)
+public func dfn(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("dfn", attribs, .fragment(content))
}
/// The `` element represents the defining instance of a term. The term-description group, ``, `
` or `` element that is the nearest ancestor of the `` element must also contain the definition(s) for the term given by the `` element.
///
/// - Parameter content: Child nodes.
-public func dfn(_ content: [Node]) -> Node {
- return dfn([], content)
+public func dfn(_ content: Node...) -> Node {
+ return dfn([], .fragment(content))
}
/// The `` element has no special meaning at all. It represents its children. It can be used with the `class`, `lang`, and `title` attributes to mark up semantics common to a group of consecutive elements.
@@ -514,15 +526,15 @@ public func dfn(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func div(_ attribs: [Attribute
], _ content: [Node]) -> Node {
- return element("div", attribs, content)
+public func div(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("div", attribs, .fragment(content))
}
/// The `` element has no special meaning at all. It represents its children.
///
/// - Parameter content: Child nodes.
-public func div(_ content: [Node]) -> Node {
- return div([], content)
+public func div(_ content: Node...) -> Node {
+ return div([], .fragment(content))
}
/// The `
` element represents a description list of zero or more term-description groups. Each term-description group consists of one or more terms (represented by `` elements) possibly as children of a `` element child, and one or more descriptions (represented by `
` elements possibly as children of a `` element child), ignoring any nodes other than `
` and ` ` element children, and ` ` and ` ` elements that are children of `` element children within a single `
` element.
@@ -530,15 +542,15 @@ public func div(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func dl(_ attribs: [Attribute], _ content: [ChildOf]) -> Node {
- return element("dl", attribs, content.map { $0.rawValue })
+public func dl(_ attribs: [Attribute], _ content: ChildOf...) -> Node {
+ return element("dl", attribs, ChildOf.fragment(content).rawValue)
}
/// The `` element represents a description list of zero or more term-description groups. Each term-description group consists of one or more terms (represented by `` elements) possibly as children of a `` element child, and one or more descriptions (represented by `
` elements possibly as children of a `` element child), ignoring any nodes other than `
` and ` ` element children, and ` ` and ` ` elements that are children of `` element children within a single `
` element.
///
/// - Parameter content: Child nodes.
-public func dl(_ content: [ChildOf]) -> Node {
- return dl([], content)
+public func dl(_ content: ChildOf...) -> Node {
+ return dl([], .fragment(content))
}
/// The `` element represents a term, part of a term-description group in a description list (` ` element).
@@ -546,15 +558,15 @@ public func dl(_ content: [ChildOf]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func dt(_ attribs: [Attribute], _ content: [Node]) -> ChildOf {
- return .init(element("dt", attribs, content))
+public func dt(_ attribs: [Attribute], _ content: Node...) -> ChildOf {
+ return .init(element("dt", attribs, .fragment(content)))
}
/// The `` element represents a term, part of a term-description group in a description list (` ` element).
///
/// - Parameter content: Child nodes.
-public func dt(_ content: [Node]) -> ChildOf {
- return dt([], content)
+public func dt(_ content: Node...) -> ChildOf {
+ return dt([], .fragment(content))
}
/// The `` element represents stress emphasis of its contents.
@@ -562,15 +574,15 @@ public func dt(_ content: [Node]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func em(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("em", attribs, content)
+public func em(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("em", attribs, .fragment(content))
}
/// The `` element represents stress emphasis of its contents.
///
/// - Parameter content: Child nodes.
-public func em(_ content: [Node]) -> Node {
- return em([], content)
+public func em(_ content: Node...) -> Node {
+ return em([], .fragment(content))
}
/// The `` element provides an integration point for an external (typically non-HTML) application or interactive content.
@@ -585,15 +597,15 @@ public func embed(_ attribs: [Attribute]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func fieldset(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("fieldset", attribs, content)
+public func fieldset(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("fieldset", attribs, .fragment(content))
}
/// The `` element represents a set of form controls optionally grouped under a common name.
///
/// - Parameter content: Child nodes.
-public func fieldset(_ content: [Node]) -> Node {
- return fieldset([], content)
+public func fieldset(_ content: Node...) -> Node {
+ return fieldset([], .fragment(content))
}
/// The `` element represents a caption or legend for the rest of the contents of the `` element's parent `` element, if any.
@@ -601,17 +613,17 @@ public func fieldset(_ content: [Node]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func figcaption(_ attribs: [Attribute], _ content: [Node])
+public func figcaption(_ attribs: [Attribute], _ content: Node...)
-> ChildOf {
- return .init(element("figcaption", attribs, content))
+ return .init(element("figcaption", attribs, .fragment(content)))
}
/// The `` element represents a caption or legend for the rest of the contents of the `` element's parent `` element, if any.
///
/// - Parameter content: Child nodes.
-public func figcaption(_ content: [Node]) -> ChildOf {
- return figcaption([], content)
+public func figcaption(_ content: Node...) -> ChildOf {
+ return figcaption([], .fragment(content))
}
/// The `` element represents some flow content, optionally with a caption, that is self-contained (like a complete sentence) and is typically referenced as a single unit from the main flow of the document.
@@ -619,15 +631,15 @@ public func figcaption(_ content: [Node]) -> ChildOf {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func figure(_ attribs: [Attribute], _ content: [ChildOf]) -> Node {
- return element("figure", attribs, content.map { $0.rawValue })
+public func figure(_ attribs: [Attribute], _ content: ChildOf...) -> Node {
+ return element("figure", attribs, ChildOf.fragment(content).rawValue)
}
/// The `` element represents some flow content, optionally with a caption, that is self-contained (like a complete sentence) and is typically referenced as a single unit from the main flow of the document.
///
/// - Parameter content: Child nodes.
-public func figure(_ content: [ChildOf]) -> Node {
- return figure([], content)
+public func figure(_ content: ChildOf...) -> Node {
+ return figure([], .fragment(content))
}
/// The `` element represents a footer for its nearest ancestor `` element or sectioning content or sectioning root element. A footer typically contains information about its section, such as who wrote it, links to related documents, copyright data, and the like.
@@ -635,15 +647,15 @@ public func figure(_ content: [ChildOf]) -> Node {
/// - Parameters:
/// - attribs: Attributes.
/// - content: Child nodes.
-public func footer(_ attribs: [Attribute], _ content: [Node]) -> Node {
- return element("footer", attribs, content)
+public func footer(_ attribs: [Attribute], _ content: Node...) -> Node {
+ return element("footer", attribs, .fragment(content))
}
/// The `` element represents a footer for its nearest ancestor `` element or sectioning content or sectioning root element. A footer typically contains information about its section, such as who wrote it, links to related documents, copyright data, and the like.
///
/// - Parameter content: Child nodes.
-public func footer(_ content: [Node]) -> Node {
- return footer([], content)
+public func footer(_ content: Node...) -> Node {
+ return footer([], .fragment(content))
}
/// The `