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 Redirect. #201

Merged
merged 1 commit into from
Sep 17, 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 @@ -55,7 +55,8 @@ provided below is an organized table of W3C HTML tags and their equivalent Slips
[`<link rel="stylesheet">`](https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet) | ``Stylesheet``
[`<link rel="tag">`](https://html.spec.whatwg.org/multipage/links.html#link-type-tag) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<link rel="terms-of-service">`](https://html.spec.whatwg.org/multipage/links.html#link-type-terms-of-service) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
[`<meta>`](https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element) | ``Meta``
[`<meta>`](https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element) | ``Meta``
[`<meta http-equiv="refresh">`](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh) | ``Redirect``
[`<meta charset="">`](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-charset) | ``Charset``
[`<style="">`](https://html.spec.whatwg.org/multipage/semantics.html#the-style-element) | ``Stylesheet``

Expand Down
28 changes: 28 additions & 0 deletions Sources/Slipstream/W3C/Elements/DocumentMetadata/Redirect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

import SwiftSoup

/// A view that causes a page to redirect to another URL after a delay.
///
/// - SeeAlso: W3C [http-equiv refresh](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh) specification.
@available(iOS 17.0, macOS 14.0, *)
public struct Redirect: View {
/// Creates a Redirect view.
public init(_ url: URL?, delay: TimeInterval = 0) {
self.url = url
self.delay = delay
}

@_documentation(visibility: private)
public func render(_ container: Element, environment: EnvironmentValues) throws {
guard let url else {
return
}
let element = try container.appendElement("meta")
try element.attr("http-equiv", "refresh")
try element.attr("content", "\(delay); url='\(url.absoluteString)'")
}

private let url: URL?
private let delay: TimeInterval
}
14 changes: 14 additions & 0 deletions Tests/SlipstreamTests/W3C/RedirectTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import Testing

import Slipstream

struct RedirectTests {
@Test func nilURL() throws {
try #expect(renderHTML(Redirect(nil)) == "")
}

@Test func withDelay() throws {
try #expect(renderHTML(Redirect(URL(string: "/home.html"), delay: 1)) == #"<meta http-equiv="refresh" content="1.0; url='/home.html'" />"#)
}
}