Skip to content

Commit

Permalink
Add support for colspan and rowspan on TableCell. (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey authored Sep 23, 2024
1 parent ac6e383 commit 9ce48ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/package-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: macos-latest
steps:
- name: Select Xcode
run: sudo xcode-select -s "/Applications/Xcode_16.1_beta.app"
run: sudo xcode-select -s "/Applications/Xcode_16.app"
- name: Get swift version
run: swift --version
- uses: actions/checkout@v4
Expand Down
30 changes: 22 additions & 8 deletions Sources/Slipstream/W3C/Elements/TabularData/TableCell.swift
Original file line number Diff line number Diff line change
@@ -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<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "td"

@_documentation(visibility: private)
@ViewBuilder public let content: () -> Content

public struct TableCell<Content>: 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?
}
6 changes: 6 additions & 0 deletions Tests/SlipstreamTests/W3C/TabularData/TableCellTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ struct TableCellTests {
try #expect(renderHTML(TableCell {}) == "<td></td>")
}

@Test func spans() throws {
try #expect(renderHTML(TableCell(rowSpan: 2) {}) == #"<td rowspan="2"></td>"#)
try #expect(renderHTML(TableCell(colSpan: 3) {}) == #"<td colspan="3"></td>"#)
try #expect(renderHTML(TableCell(rowSpan: 2, colSpan: 4) {}) == #"<td rowspan="2" colspan="4"></td>"#)
}

@Test func withText() throws {
try #expect(renderHTML(TableCell {
DOMString("Hello, world!")
Expand Down

0 comments on commit 9ce48ca

Please sign in to comment.