Skip to content

Commit 9ce48ca

Browse files
authored
Add support for colspan and rowspan on TableCell. (#203)
1 parent ac6e383 commit 9ce48ca

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

.github/workflows/package-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: macos-latest
1212
steps:
1313
- name: Select Xcode
14-
run: sudo xcode-select -s "/Applications/Xcode_16.1_beta.app"
14+
run: sudo xcode-select -s "/Applications/Xcode_16.app"
1515
- name: Get swift version
1616
run: swift --version
1717
- uses: actions/checkout@v4
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
import SwiftSoup
2+
13
/// A view that represents a data cell in a table.
24
///
35
/// - SeeAlso: W3C [td](https://html.spec.whatwg.org/multipage/tables.html#the-td-element) specification.
46
@available(iOS 17.0, macOS 14.0, *)
5-
public struct TableCell<Content>: W3CElement where Content: View {
6-
@_documentation(visibility: private)
7-
public let tagName: String = "td"
8-
9-
@_documentation(visibility: private)
10-
@ViewBuilder public let content: () -> Content
11-
7+
public struct TableCell<Content>: View where Content: View {
128
/// Creates a table cell.
13-
public init(@ViewBuilder content: @escaping () -> Content) {
9+
public init(rowSpan: Int? = nil, colSpan: Int? = nil, @ViewBuilder content: @escaping () -> Content) {
1410
self.content = content
11+
self.rowSpan = rowSpan
12+
self.colSpan = colSpan
1513
}
14+
15+
@_documentation(visibility: private)
16+
public func render(_ container: Element, environment: EnvironmentValues) throws {
17+
let element = try container.appendElement("td")
18+
if let rowSpan {
19+
try element.attr("rowspan", "\(rowSpan)")
20+
}
21+
if let colSpan {
22+
try element.attr("colspan", "\(colSpan)")
23+
}
24+
try self.content().render(element, environment: environment)
25+
}
26+
27+
@ViewBuilder private let content: () -> Content
28+
private let rowSpan: Int?
29+
private let colSpan: Int?
1630
}

Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ struct TableCellTests {
77
try #expect(renderHTML(TableCell {}) == "<td></td>")
88
}
99

10+
@Test func spans() throws {
11+
try #expect(renderHTML(TableCell(rowSpan: 2) {}) == #"<td rowspan="2"></td>"#)
12+
try #expect(renderHTML(TableCell(colSpan: 3) {}) == #"<td colspan="3"></td>"#)
13+
try #expect(renderHTML(TableCell(rowSpan: 2, colSpan: 4) {}) == #"<td rowspan="2" colspan="4"></td>"#)
14+
}
15+
1016
@Test func withText() throws {
1117
try #expect(renderHTML(TableCell {
1218
DOMString("Hello, world!")

0 commit comments

Comments
 (0)