Skip to content

Commit

Permalink
added charset attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sliemeobn committed Jun 25, 2024
1 parent fe837e0 commit e92923b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Sources/Elementary/HtmlAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ final class AttributeRenderingTests: XCTestCase {
#"<p onblur="doIt()"></p>"#
)
}

func testRendersMetaCharset() {
HTMLAssertEqual(
meta(.charset(.utf8)),
#"<meta charset="UTF-8">"#
)
}
}

0 comments on commit e92923b

Please sign in to comment.