diff --git a/Sources/Elementary/Core/AttributeStorage.swift b/Sources/Elementary/Core/AttributeStorage.swift index c638a6c..0f26c34 100644 --- a/Sources/Elementary/Core/AttributeStorage.swift +++ b/Sources/Elementary/Core/AttributeStorage.swift @@ -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: diff --git a/Sources/Elementary/Core/Html+Attributes.swift b/Sources/Elementary/Core/Html+Attributes.swift index 4390703..9e2043f 100644 --- a/Sources/Elementary/Core/Html+Attributes.swift +++ b/Sources/Elementary/Core/Html+Attributes.swift @@ -1,14 +1,28 @@ public struct HTMLAttribute { 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: HTML { public var content: Content diff --git a/Sources/Elementary/HtmlAttributes.swift b/Sources/Elementary/HtmlAttributes.swift index d907e07..716e6ce 100644 --- a/Sources/Elementary/HtmlAttributes.swift +++ b/Sources/Elementary/HtmlAttributes.swift @@ -11,7 +11,7 @@ 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 { @@ -19,7 +19,7 @@ public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global { } 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 { diff --git a/Tests/ElementaryTests/AttributeRenderingTests.swift b/Tests/ElementaryTests/AttributeRenderingTests.swift index c315101..5cccd5d 100644 --- a/Tests/ElementaryTests/AttributeRenderingTests.swift +++ b/Tests/ElementaryTests/AttributeRenderingTests.swift @@ -57,4 +57,14 @@ final class AttributeRenderingTests: XCTestCase { #""# ) } + + 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: ""))), + #"
"# + ) + } }