From e92923b74201289fbed5a4bca273887faa36e39a Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:11:38 +0200 Subject: [PATCH] added charset attribute --- Sources/Elementary/HtmlAttributes.swift | 31 ++++++++++++++++++- .../AttributeRenderingTests.swift | 7 +++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Sources/Elementary/HtmlAttributes.swift b/Sources/Elementary/HtmlAttributes.swift index 716e6ce..7ec1da6 100644 --- a/Sources/Elementary/HtmlAttributes.swift +++ b/Sources/Elementary/HtmlAttributes.swift @@ -142,8 +142,37 @@ public extension HTMLAttribute where Tag: HTMLTrait.Attributes.autofocus { } } -// form tag attributes +// charset attribute +public extension HTMLTrait.Attributes { + protocol charset {} +} + +extension HTMLTag.meta: HTMLTrait.Attributes.charset {} +extension HTMLTag.script: HTMLTrait.Attributes.charset {} + +public extension HTMLAttributeValue { + struct CharacterSet: ExpressibleByStringLiteral, RawRepresentable, Sendable, Equatable { + public var rawValue: String + + public init(rawValue: String) { + self.rawValue = rawValue + } + + public init(stringLiteral value: String) { + rawValue = value + } + + public static var utf8: Self { "UTF-8" } + } +} +public extension HTMLAttribute where Tag: HTMLTrait.Attributes.charset { + static func charset(_ value: HTMLAttributeValue.CharacterSet) -> Self { + HTMLAttribute(name: "charset", value: value.rawValue) + } +} + +// form tag attributes public extension HTMLAttribute where Tag == HTMLTag.form { struct Method: Sendable, Equatable { var value: String diff --git a/Tests/ElementaryTests/AttributeRenderingTests.swift b/Tests/ElementaryTests/AttributeRenderingTests.swift index 24648da..94aff4b 100644 --- a/Tests/ElementaryTests/AttributeRenderingTests.swift +++ b/Tests/ElementaryTests/AttributeRenderingTests.swift @@ -88,4 +88,11 @@ final class AttributeRenderingTests: XCTestCase { #"

"# ) } + + func testRendersMetaCharset() { + HTMLAssertEqual( + meta(.charset(.utf8)), + #""# + ) + } }