Skip to content

Commit

Permalink
Add Redirect.
Browse files Browse the repository at this point in the history
Part of #25.
  • Loading branch information
jverkoey committed Sep 17, 2024
1 parent 1837def commit 5216736
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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'" />"#)
}
}

0 comments on commit 5216736

Please sign in to comment.