-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #25.
- Loading branch information
Showing
3 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Sources/Slipstream/W3C/Elements/DocumentMetadata/Redirect.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'" />"#) | ||
} | ||
} |