Skip to content

Commit ac6e383

Browse files
authored
Add Redirect. (#201)
Part of #25.
1 parent 1837def commit ac6e383

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Sources/Slipstream/Documentation.docc/Guides/SlipstreamForWebDevelopers.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ provided below is an organized table of W3C HTML tags and their equivalent Slips
5555
[`<link rel="stylesheet">`](https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet) | ``Stylesheet``
5656
[`<link rel="tag">`](https://html.spec.whatwg.org/multipage/links.html#link-type-tag) | [Not implemented yet](https://github.com/jverkoey/slipstream/issues/25)
5757
[`<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)
58-
[`<meta>`](https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element) | ``Meta``
58+
[`<meta>`](https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element) | ``Meta``
59+
[`<meta http-equiv="refresh">`](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh) | ``Redirect``
5960
[`<meta charset="">`](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-charset) | ``Charset``
6061
[`<style="">`](https://html.spec.whatwg.org/multipage/semantics.html#the-style-element) | ``Stylesheet``
6162

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
import SwiftSoup
4+
5+
/// A view that causes a page to redirect to another URL after a delay.
6+
///
7+
/// - SeeAlso: W3C [http-equiv refresh](https://html.spec.whatwg.org/multipage/semantics.html#attr-meta-http-equiv-refresh) specification.
8+
@available(iOS 17.0, macOS 14.0, *)
9+
public struct Redirect: View {
10+
/// Creates a Redirect view.
11+
public init(_ url: URL?, delay: TimeInterval = 0) {
12+
self.url = url
13+
self.delay = delay
14+
}
15+
16+
@_documentation(visibility: private)
17+
public func render(_ container: Element, environment: EnvironmentValues) throws {
18+
guard let url else {
19+
return
20+
}
21+
let element = try container.appendElement("meta")
22+
try element.attr("http-equiv", "refresh")
23+
try element.attr("content", "\(delay); url='\(url.absoluteString)'")
24+
}
25+
26+
private let url: URL?
27+
private let delay: TimeInterval
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
import Testing
3+
4+
import Slipstream
5+
6+
struct RedirectTests {
7+
@Test func nilURL() throws {
8+
try #expect(renderHTML(Redirect(nil)) == "")
9+
}
10+
11+
@Test func withDelay() throws {
12+
try #expect(renderHTML(Redirect(URL(string: "/home.html"), delay: 1)) == #"<meta http-equiv="refresh" content="1.0; url='/home.html'" />"#)
13+
}
14+
}

0 commit comments

Comments
 (0)