From bb627ae7ac8116b855ffd45fe6ae2a8404f6017b Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Mon, 5 Aug 2024 22:09:20 -0400 Subject: [PATCH] Add Blockquote element. Part of https://github.com/jverkoey/slipstream/issues/25. --- .../Documentation.docc/W3C/W3CViews.md | 1 + .../Elements/GroupingContent/Blockquote.swift | 35 +++++++++++++++++++ .../Elements/GroupingContent/Paragraph.swift | 2 +- .../SlipstreamTests/W3C/BlockquoteTests.swift | 19 ++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 Sources/Slipstream/W3C/Elements/GroupingContent/Blockquote.swift create mode 100644 Tests/SlipstreamTests/W3C/BlockquoteTests.swift diff --git a/Sources/Slipstream/Documentation.docc/W3C/W3CViews.md b/Sources/Slipstream/Documentation.docc/W3C/W3CViews.md index 7868c16..3495e02 100644 --- a/Sources/Slipstream/Documentation.docc/W3C/W3CViews.md +++ b/Sources/Slipstream/Documentation.docc/W3C/W3CViews.md @@ -35,6 +35,7 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh - ``Paragraph`` - ``Divider`` +- ``Blockquote`` - ``Div`` ### Text-level semantics diff --git a/Sources/Slipstream/W3C/Elements/GroupingContent/Blockquote.swift b/Sources/Slipstream/W3C/Elements/GroupingContent/Blockquote.swift new file mode 100644 index 0000000..0f4a5f0 --- /dev/null +++ b/Sources/Slipstream/W3C/Elements/GroupingContent/Blockquote.swift @@ -0,0 +1,35 @@ +/// A view that represents a section that is quoted from another source. +/// +/// ```swift +/// struct MySiteContent: View { +/// var body: some View { +/// Body { +/// Blockquote { +/// Text("Hello, world!") +/// } +/// } +/// } +/// } +/// ``` +/// +/// - SeeAlso: W3C [blockquote](https://html.spec.whatwg.org/multipage/grouping-content.html#the-blockquote-element) specification. +@available(iOS 17.0, macOS 14.0, *) +public struct Blockquote: W3CElement where Content: View { + @_documentation(visibility: private) + public let tagName: String = "blockquote" + + @_documentation(visibility: private) + @ViewBuilder public let content: () -> Content + + /// Creates a blockquote. + public init(@ViewBuilder content: @escaping () -> Content) { + self.content = content + } + + /// Creates a blockquote with some static text. + public init(_ text: String) where Content == Text { + self.content = { + Text(text) + } + } +} diff --git a/Sources/Slipstream/W3C/Elements/GroupingContent/Paragraph.swift b/Sources/Slipstream/W3C/Elements/GroupingContent/Paragraph.swift index e56f7bd..1a4dd1f 100644 --- a/Sources/Slipstream/W3C/Elements/GroupingContent/Paragraph.swift +++ b/Sources/Slipstream/W3C/Elements/GroupingContent/Paragraph.swift @@ -24,7 +24,7 @@ public struct Paragraph: W3CElement where Content: View { self.content = content } - /// Creates an paragraph with some static text. + /// Creates a paragraph with some static text. public init(_ text: String) where Content == Text { self.content = { Text(text) diff --git a/Tests/SlipstreamTests/W3C/BlockquoteTests.swift b/Tests/SlipstreamTests/W3C/BlockquoteTests.swift new file mode 100644 index 0000000..8ad7a2f --- /dev/null +++ b/Tests/SlipstreamTests/W3C/BlockquoteTests.swift @@ -0,0 +1,19 @@ +import Testing + +import Slipstream + +struct BlockquoteTests { + @Test func emptyBlock() throws { + try #expect(renderHTML(Blockquote {}) == "
") + } + + @Test func withText() throws { + try #expect(renderHTML(Blockquote { + Text("Hello, world!") + }) == """ +
+ Hello, world! +
+""") + } +}