Skip to content

Commit

Permalink
Adds new alt attribute to produce alt text for images (#62)
Browse files Browse the repository at this point in the history
* Adds new alt attribute to produce alt text for images

* removed non-test and formatting

---------

Co-authored-by: Simon Leeb <[email protected]>
  • Loading branch information
ocapmycap and sliemeobn authored Dec 16, 2024
1 parent e884367 commit 9830c5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/Elementary/HtmlAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ final class AttributeRenderingTests: XCTestCase {
#"<img width="100" height="200">"#
)
}

func testRendersAltForImg() async throws {
try await HTMLAssertEqual(
img(.src("/path/to/dog.jpeg"), .alt("A happy dog"), .width(200), .height(200)),
#"<img src="/path/to/dog.jpeg" alt="A happy dog" width="200" height="200">"#
)
}
}

0 comments on commit 9830c5e

Please sign in to comment.