Skip to content

Commit

Permalink
added width and height attributes (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
sliemeobn authored Sep 21, 2024
1 parent d82525e commit 4d9d137
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Sources/Elementary/HtmlAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,11 @@ final class AttributeRenderingTests: XCTestCase {
#"<img id="1" style="2">"#
)
}

func testRendersWidthAndHeightAttributes() async throws {
try await HTMLAssertEqual(
img(.width(100), .height(200)),
#"<img width="100" height="200">"#
)
}
}

0 comments on commit 4d9d137

Please sign in to comment.