diff --git a/Sources/Slipstream/W3C/Elements/TabularData/TableCell.swift b/Sources/Slipstream/W3C/Elements/TabularData/TableCell.swift index 7880c06..5469e5e 100644 --- a/Sources/Slipstream/W3C/Elements/TabularData/TableCell.swift +++ b/Sources/Slipstream/W3C/Elements/TabularData/TableCell.swift @@ -1,16 +1,30 @@ +import SwiftSoup + /// A view that represents a data cell in a table. /// /// - SeeAlso: W3C [td](https://html.spec.whatwg.org/multipage/tables.html#the-td-element) specification. @available(iOS 17.0, macOS 14.0, *) -public struct TableCell: W3CElement where Content: View { - @_documentation(visibility: private) - public let tagName: String = "td" - - @_documentation(visibility: private) - @ViewBuilder public let content: () -> Content - +public struct TableCell: View where Content: View { /// Creates a table cell. - public init(@ViewBuilder content: @escaping () -> Content) { + public init(rowSpan: Int? = nil, colSpan: Int? = nil, @ViewBuilder content: @escaping () -> Content) { self.content = content + self.rowSpan = rowSpan + self.colSpan = colSpan } + + @_documentation(visibility: private) + public func render(_ container: Element, environment: EnvironmentValues) throws { + let element = try container.appendElement("td") + if let rowSpan { + try element.attr("rowspan", "\(rowSpan)") + } + if let colSpan { + try element.attr("colspan", "\(colSpan)") + } + try self.content().render(element, environment: environment) + } + + @ViewBuilder private let content: () -> Content + private let rowSpan: Int? + private let colSpan: Int? } diff --git a/Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift b/Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift index 2f31069..bf931a5 100644 --- a/Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift +++ b/Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift @@ -7,6 +7,12 @@ struct TableCellTests { try #expect(renderHTML(TableCell {}) == "") } + @Test func spans() throws { + try #expect(renderHTML(TableCell(rowSpan: 2) {}) == #""#) + try #expect(renderHTML(TableCell(colSpan: 3) {}) == #""#) + try #expect(renderHTML(TableCell(rowSpan: 2, colSpan: 4) {}) == #""#) + } + @Test func withText() throws { try #expect(renderHTML(TableCell { DOMString("Hello, world!")