diff --git a/Sources/SwiftHtml/Attributes/Global.swift b/Sources/SwiftHtml/Attributes/Global.swift index ba1227d..a002243 100644 --- a/Sources/SwiftHtml/Attributes/Global.swift +++ b/Sources/SwiftHtml/Attributes/Global.swift @@ -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) } diff --git a/Tests/SwiftHtmlTests/SwiftHtmlTests.swift b/Tests/SwiftHtmlTests/SwiftHtmlTests.swift index 2adda1a..0f69b54 100644 --- a/Tests/SwiftHtmlTests/SwiftHtmlTests.swift +++ b/Tests/SwiftHtmlTests/SwiftHtmlTests.swift @@ -82,6 +82,16 @@ final class SwiftHtmlTests: XCTestCase { XCTAssertEqual(#""#, html) } + func testRemoveLastClass() { + let doc = Document { + Span("") + .class("a") + .class(remove: "a") + } + let html = DocumentRenderer(minify: true).render(doc) + XCTAssertEqual(#""#, html) + } + func testToggleAddClass() { let doc = Document { Span("")