Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for colspan and rowspan on TableCell. #203

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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