Skip to content

Commit

Permalink
Fix remove class method (#13)
Browse files Browse the repository at this point in the history
If all the classes were removed also remove the attribute
  • Loading branch information
bgisme authored Feb 18, 2022
1 parent ecb4d01 commit 9e6389f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/SwiftHtml/Attributes/Global.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public extension Tag {
/// Removes an array of class values if the condition is true
func `class`(remove values: [String], _ condition: Bool = true) -> Self {
let newClasses = classArray.filter { !values.contains($0) }
if newClasses.isEmpty {
return deleteAttribute("class")
}
return `class`(newClasses, condition)
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftHtmlTests/SwiftHtmlTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ final class SwiftHtmlTests: XCTestCase {
XCTAssertEqual(#"<span class="a c"></span>"#, html)
}

func testRemoveLastClass() {
let doc = Document {
Span("")
.class("a")
.class(remove: "a")
}
let html = DocumentRenderer(minify: true).render(doc)
XCTAssertEqual(#"<span></span>"#, html)
}

func testToggleAddClass() {
let doc = Document {
Span("")
Expand Down

0 comments on commit 9e6389f

Please sign in to comment.