Skip to content

Commit

Permalink
Support arrays of attributes in initializers and .attributes() modi…
Browse files Browse the repository at this point in the history
…fier (#22)

* initialize html element with array of attributes

* adds tests and adjust parameter names
  • Loading branch information
lovetodream authored Aug 28, 2024
1 parent 7215231 commit 04bd2fc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Sources/Elementary/Core/Html+Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public extension HTMLElement {
self.attributes = .init(attributes)
self.content = content()
}

/// Creates a new HTML element with the specified attributes and content.
/// - Parameters:
/// - attributes: The attributes to apply to the element as an array.
/// - content: The content of the element.
init(attributes: [HTMLAttribute<Tag>], @HTMLBuilder content: () -> Content) {
self.attributes = .init(attributes)
self.content = content()
}
}

public extension HTMLVoidElement {
Expand All @@ -93,6 +102,12 @@ public extension HTMLVoidElement {
init(_ attributes: HTMLAttribute<Tag>...) {
self.attributes = .init(attributes)
}

/// Creates a new HTML void element with the specified attributes.
/// - Parameter attributes: The attributes to apply to the element as an array.
init(attributes: [HTMLAttribute<Tag>]) {
self.attributes = .init(attributes)
}
}

public extension HTML where Tag: HTMLTrait.Attributes.Global {
Expand All @@ -117,6 +132,15 @@ public extension HTML where Tag: HTMLTrait.Attributes.Global {
func attributes(_ attributes: HTMLAttribute<Tag>..., when condition: Bool = true) -> _AttributedElement<Self> {
_AttributedElement(content: self, attributes: .init(condition ? attributes : []))
}

/// Adds the specified attributes to the element.
/// - Parameters:
/// - attributes: The attributes to add to the element as an array.
/// - condition: If set to false, the attributes will not be added.
/// - Returns: A new element with the specified attributes added.
func attributes(contentsOf attributes: [HTMLAttribute<Tag>], when condition: Bool = true) -> _AttributedElement<Self> {
_AttributedElement(content: self, attributes: .init(condition ? attributes : []))
}
}

private extension _RenderingContext {
Expand Down
21 changes: 21 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,25 @@ final class AttributeRenderingTests: XCTestCase {
#"<input type="text" required>"#
)
}

func testRendersAttributesArray() async throws {
try await HTMLAssertEqual(
p(attributes: [.id("foo"), .class("foo"), .hidden]) {},
#"<p id="foo" class="foo" hidden></p>"#
)
}

func testRendersAttributesArrayOnVoidElement() async throws {
try await HTMLAssertEqual(
input(attributes: [.type(.text), .required]),
#"<input type="text" required>"#
)
}

func testRendersAppliedConditionalAttributesArray() async throws {
try await HTMLAssertEqual(
img(.id("1")).attributes(contentsOf: [.class("2"), .id("no")], when: false).attributes(contentsOf: [.style("2")], when: true),
#"<img id="1" style="2">"#
)
}
}

0 comments on commit 04bd2fc

Please sign in to comment.