From 4d9d13794bb399d7d94e2bb94278d521bcf599a5 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Sat, 21 Sep 2024 20:30:29 +0200 Subject: [PATCH] added width and height attributes (#42) --- Sources/Elementary/HtmlAttributes.swift | 27 +++++++++++++++++-- .../AttributeRenderingTests.swift | 7 +++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Sources/Elementary/HtmlAttributes.swift b/Sources/Elementary/HtmlAttributes.swift index 51ebae1..08e2551 100644 --- a/Sources/Elementary/HtmlAttributes.swift +++ b/Sources/Elementary/HtmlAttributes.swift @@ -92,7 +92,7 @@ public extension HTMLAttribute where Tag == HTMLTag.meta { // link tag attributes // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link public extension HTMLAttribute where Tag == HTMLTag.link { - public struct As: Sendable, ExpressibleByStringLiteral { + struct As: Sendable, ExpressibleByStringLiteral { var value: String init(value: String) { @@ -118,7 +118,7 @@ public extension HTMLAttribute where Tag == HTMLTag.link { public static let author = As(value: "author") } - public static func `as`(_ value: As) -> Self { + static func `as`(_ value: As) -> Self { HTMLAttribute(name: "as", value: value.value) } } @@ -461,6 +461,29 @@ extension HTMLAttribute where Tag: HTMLTrait.Attributes.referrerpolicy { } } +// width and height attributes +public extension HTMLTrait.Attributes { + protocol dimensions {} +} + +extension HTMLTag.canvas: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.embed: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.iframe: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.img: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.input: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.object: HTMLTrait.Attributes.dimensions {} +extension HTMLTag.video: HTMLTrait.Attributes.dimensions {} + +public extension HTMLAttribute where Tag: HTMLTrait.Attributes.dimensions { + static func width(_ value: Int) -> Self { + HTMLAttribute(name: "width", value: "\(value)") + } + + static func height(_ value: Int) -> Self { + HTMLAttribute(name: "height", value: "\(value)") + } +} + // form tag attributes public extension HTMLAttribute where Tag == HTMLTag.form { struct Method: Sendable, Equatable { diff --git a/Tests/ElementaryTests/AttributeRenderingTests.swift b/Tests/ElementaryTests/AttributeRenderingTests.swift index 81ebb9a..b490980 100644 --- a/Tests/ElementaryTests/AttributeRenderingTests.swift +++ b/Tests/ElementaryTests/AttributeRenderingTests.swift @@ -123,4 +123,11 @@ final class AttributeRenderingTests: XCTestCase { #""# ) } + + func testRendersWidthAndHeightAttributes() async throws { + try await HTMLAssertEqual( + img(.width(100), .height(200)), + #""# + ) + } }