Skip to content

Commit 9e6389f

Browse files
authored
Fix remove class method (#13)
If all the classes were removed also remove the attribute
1 parent ecb4d01 commit 9e6389f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Sources/SwiftHtml/Attributes/Global.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public extension Tag {
107107
/// Removes an array of class values if the condition is true
108108
func `class`(remove values: [String], _ condition: Bool = true) -> Self {
109109
let newClasses = classArray.filter { !values.contains($0) }
110+
if newClasses.isEmpty {
111+
return deleteAttribute("class")
112+
}
110113
return `class`(newClasses, condition)
111114
}
112115

Tests/SwiftHtmlTests/SwiftHtmlTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ final class SwiftHtmlTests: XCTestCase {
8282
XCTAssertEqual(#"<span class="a c"></span>"#, html)
8383
}
8484

85+
func testRemoveLastClass() {
86+
let doc = Document {
87+
Span("")
88+
.class("a")
89+
.class(remove: "a")
90+
}
91+
let html = DocumentRenderer(minify: true).render(doc)
92+
XCTAssertEqual(#"<span></span>"#, html)
93+
}
94+
8595
func testToggleAddClass() {
8696
let doc = Document {
8797
Span("")

0 commit comments

Comments
 (0)