From 8d2942b2d10d3b9e1ce6e5399f61c10cc54bc364 Mon Sep 17 00:00:00 2001 From: nkalvi Date: Thu, 23 Feb 2023 10:30:46 -0800 Subject: [PATCH] Try to solve https://github.com/pointfreeco/swift-html/issues/94 --- Html.playground/Contents.swift | 22 ++++++++++++++++++++++ Html.playground/contents.xcplayground | 2 +- Sources/Html/ChildOf.swift | 12 ++++++++++++ Tests/HtmlTests/ElementsTests.swift | 26 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Html.playground/Contents.swift b/Html.playground/Contents.swift index d414f09..380c086 100644 --- a/Html.playground/Contents.swift +++ b/Html.playground/Contents.swift @@ -115,6 +115,28 @@ render(unexclaim(document)) ) ) +// Based on https://html.spec.whatwg.org/multipage/grouping-content.html#the-dl-element +let dl_exmaple_with_div: Node = .dl( + .div( + .dt("Last modified time"), + .dd("2004-12-23T23:33Z") + ), + .div( + .dt("Recommended update interval"), + .dd("60s") + ), + .div( + .dt("Authors"), + .dt("Editors"), + .dd("Robert Rothman"), + .dd("Daniel Jackson") + ) +) + +render(dl_exmaple_with_div) + + + /// A function that "redacts" an HTML document by transforming all text nodes /// into █-sequences of characters. func redacted(node: Node) -> Node { diff --git a/Html.playground/contents.xcplayground b/Html.playground/contents.xcplayground index a93d484..63b6dd8 100644 --- a/Html.playground/contents.xcplayground +++ b/Html.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Sources/Html/ChildOf.swift b/Sources/Html/ChildOf.swift index 862e06d..4b953a0 100644 --- a/Sources/Html/ChildOf.swift +++ b/Sources/Html/ChildOf.swift @@ -89,6 +89,18 @@ extension ChildOf where Element == Tag.Dl { } } +// https://html.spec.whatwg.org/multipage/grouping-content.html#the-dl-element +extension ChildOf where Element == Tag.Dl { + /// The `
` element wraps a group, that is part of a term-description group in a description list (`
` element). + /// + /// - Parameters: + /// - attributes: Attributes. + /// - content: Child nodes (`
` or `dd` elements). + public static func div(attributes: [Attribute] = [], _ content: ChildOf...) -> ChildOf { + return .init(.element("div", attributes: attributes, ChildOf.fragment(content).rawValue)) + } +} + extension ChildOf where Element == Tag.Fieldset { /// The `` element represents a caption for the rest of the contents of the `` element's parent `
` element, if any. /// diff --git a/Tests/HtmlTests/ElementsTests.swift b/Tests/HtmlTests/ElementsTests.swift index 7dfbde5..32c292f 100644 --- a/Tests/HtmlTests/ElementsTests.swift +++ b/Tests/HtmlTests/ElementsTests.swift @@ -21,4 +21,30 @@ final class ElementsTests: XCTestCase { ) ) } + + func testDlwithDiv() { + let dl_exmaple_with_div: Node = .dl( + .div( + .dt("Last modified time"), + .dd("2004-12-23T23:33Z") + ), + .div( + .dt("Recommended update interval"), + .dd("60s") + ), + .div( + .dt("Authors"), + .dt("Editors"), + .dd("Robert Rothman"), + .dd("Daniel Jackson") + ) + ) + + XCTAssertEqual( + """ +
Last modified time
2004-12-23T23:33Z
Recommended update interval
60s
Authors
Editors
Robert Rothman
Daniel Jackson
+ """, + render(dl_exmaple_with_div) + ) + } }