From 9830c5e1b6740d367b28d416797ff4e6b007e541 Mon Sep 17 00:00:00 2001 From: brian Date: Mon, 16 Dec 2024 00:52:07 -0800 Subject: [PATCH] Adds new alt attribute to produce alt text for images (#62) * Adds new alt attribute to produce alt text for images * removed non-test and formatting --------- Co-authored-by: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> --- Sources/Elementary/HtmlAttributes.swift | 6 ++++++ Tests/ElementaryTests/AttributeRenderingTests.swift | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/Sources/Elementary/HtmlAttributes.swift b/Sources/Elementary/HtmlAttributes.swift index 18a2e00..32ff5b7 100644 --- a/Sources/Elementary/HtmlAttributes.swift +++ b/Sources/Elementary/HtmlAttributes.swift @@ -357,6 +357,12 @@ public extension HTMLAttributeValue { } } +public extension HTMLAttribute where Tag == HTMLTag.img { + static func alt(_ value: String) -> Self { + HTMLAttribute(name: "alt", value: value) + } +} + public extension HTMLAttribute where Tag: HTMLTrait.Attributes.autocomplete { static func autocomplete(_ value: HTMLAttributeValue.AutoComplete) -> Self { HTMLAttribute(name: "autocomplete", value: value.rawValue) diff --git a/Tests/ElementaryTests/AttributeRenderingTests.swift b/Tests/ElementaryTests/AttributeRenderingTests.swift index af4946d..ac36d08 100644 --- a/Tests/ElementaryTests/AttributeRenderingTests.swift +++ b/Tests/ElementaryTests/AttributeRenderingTests.swift @@ -130,4 +130,11 @@ final class AttributeRenderingTests: XCTestCase { #""# ) } + + func testRendersAltForImg() async throws { + try await HTMLAssertEqual( + img(.src("/path/to/dog.jpeg"), .alt("A happy dog"), .width(200), .height(200)), + #"A happy dog"# + ) + } }