Skip to content

Commit

Permalink
added public API for controlling attribute merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sliemeobn committed Jun 25, 2024
1 parent 8884e3e commit b866aad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/Elementary/Core/AttributeStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private func flattenAttributes(_ attributes: consuming [StoredAttribute]) -> [St
guard attribute != blankedOut else { continue }

for j in attributes.indices[(i + 1)...] where attributes[j].name == attribute.name {
switch attribute.mergeMode {
switch attributes[j].mergeMode {
case let .appendValue(separator):
attribute.appending(value: attributes[j].value, separatedBy: separator)
case .replaceValue:
Expand Down
22 changes: 18 additions & 4 deletions Sources/Elementary/Core/Html+Attributes.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
public struct HTMLAttribute<Tag: HTMLTagDefinition> {
var htmlAttribute: StoredAttribute

init(name: String, value: String?, mergeMode: StoredAttribute.MergeMode = .replaceValue) {
htmlAttribute = .init(name: name, value: value, mergeMode: mergeMode)
}

public var name: String { htmlAttribute.name }
public var value: String? { htmlAttribute.value }
}

public struct HTMLAttributeMergeAction {
var mergeMode: StoredAttribute.MergeMode

public static var replacing: Self { .init(mergeMode: .replaceValue) }
public static var ignoring: Self { .init(mergeMode: .ignoreIfSet) }
public static func appending(seperatedBy: String) -> Self { .init(mergeMode: .appendValue(seperatedBy)) }
}

public extension HTMLAttribute {
init(name: String, value: String?, mergedBy action: HTMLAttributeMergeAction = .replacing) {
htmlAttribute = .init(name: name, value: value, mergeMode: action.mergeMode)
}

consuming func mergedBy(_ action: HTMLAttributeMergeAction) -> HTMLAttribute {
.init(name: name, value: value, mergedBy: action)
}
}

public struct _AttributedElement<Content: HTML>: HTML {
public var content: Content

Expand Down
4 changes: 2 additions & 2 deletions Sources/Elementary/HtmlAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
}

static func `class`(_ value: String) -> Self {
HTMLAttribute(name: "class", value: value, mergeMode: .appendValue(" "))
HTMLAttribute(name: "class", value: value, mergedBy: .appending(seperatedBy: " "))
}

static func data(_ key: String, value: String) -> Self {
HTMLAttribute(name: "data-\(key)", value: value)
}

static func style(_ value: String) -> Self {
HTMLAttribute(name: "style", value: value, mergeMode: .appendValue(";"))
HTMLAttribute(name: "style", value: value, mergedBy: .appending(seperatedBy: ";"))
}

static func title(_ value: String) -> Self {
Expand Down
10 changes: 10 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ final class AttributeRenderingTests: XCTestCase {
#"<br id="baz" hidden>"#
)
}

func testRespectsCustomMergeMode() {
HTMLAssertEqual(
br(.id("1"), .data("bar", value: "baz"))
.attributes(.id("2").mergedBy(.appending(seperatedBy: "-")))
.attributes(.id("3").mergedBy(.ignoring))
.attributes(.data("bar", value: "baq").mergedBy(.appending(seperatedBy: ""))),
#"<br id="1-2" data-bar="bazbaq">"#
)
}
}

0 comments on commit b866aad

Please sign in to comment.