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 Canvas view. #172

Merged
merged 1 commit into from
Sep 5, 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
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@ provided below is an organized table of W3C HTML tags and their equivalent Slips
[`<noscript>`](https://html.spec.whatwg.org/multipage/sections.html#the-noscript-element) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<template>`](https://html.spec.whatwg.org/multipage/sections.html#the-template-element) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<slot>`](https://html.spec.whatwg.org/multipage/sections.html#the-slot-element) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<canvas>`](https://html.spec.whatwg.org/multipage/sections.html#the-canvas-element) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<canvas>`](https://html.spec.whatwg.org/multipage/sections.html#the-canvas-element) | ``Canvas``
1 change: 1 addition & 0 deletions Sources/Slipstream/Documentation.docc/W3C/W3CViews.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ The complete W3C HTML elements standard can be found [here](https://html.spec.wh
### Scripting

- <doc:Script>
- ``Canvas``
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct ArrayView: View {
self.array = array
}

private var array: [any View]
private let array: [any View]

public func render(_ container: Element, environment: EnvironmentValues) throws {
for view in array {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct ConditionalView<T: View, F: View>: View {
self.condition = condition
}

private var condition: Condition
private let condition: Condition

public func render(_ container: Element, environment: EnvironmentValues) throws {
switch condition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct TupleView<T>: View {
self.value = value
}

private var value: T
private let value: T

public func render(_ container: Element, environment: EnvironmentValues) throws {
/// Our tuple may be composed of any number of View types, so we use Mirror to
Expand Down
22 changes: 22 additions & 0 deletions Sources/Slipstream/W3C/Elements/Scripting/Canvas.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

import SwiftSoup

/// A view that provides scripts with a resolution-dependent bitmap canvas,
/// which can be used for rendering graphs, game graphics, art, or other
/// visual images on the fly.
///
/// - SeeAlso: W3C [canvas](https://html.spec.whatwg.org/multipage/canvas.html#the-canvas-element) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct Canvas<Content>: W3CElement where Content: View {
@_documentation(visibility: private)
public let tagName: String = "canvas"

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

/// Creates a Canvas view.
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
}
19 changes: 19 additions & 0 deletions Tests/SlipstreamTests/W3C/CanvasTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Testing

import Slipstream

struct CanvasTests {
@Test func emptyBlock() throws {
try #expect(renderHTML(Canvas {}) == "<canvas></canvas>")
}

@Test func withText() throws {
try #expect(renderHTML(Canvas {
DOMString("Hello, world!")
}) == """
<canvas>
Hello, world!
</canvas>
""")
}
}