-
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
4 changed files
with
75 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
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
51 changes: 51 additions & 0 deletions
51
Sources/Slipstream/W3C/Elements/DocumentMetadata/Icon.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,51 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A view that is an icon representing the page or site/. | ||
/// | ||
/// ```swift | ||
/// struct MySiteMetadata: View { | ||
/// var body: some View { | ||
/// Head { | ||
/// Icon(URL(string: "/favicon.ico")) | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - SeeAlso: W3C [rel="icon"](https://html.spec.whatwg.org/multipage/links.html#rel-icon) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Icon: View { | ||
/// Creates an Icon view. | ||
/// | ||
/// - Parameters: | ||
/// - url: The icon will be loaded from this URL. | ||
/// - sizes: Gives the sizes of icons for visual media. If present, is only advisory. | ||
/// - type: The MIME type of the linked resource. If present, is only advisory and must be a valid MIME type. | ||
public init(_ url: URL?, sizes: String? = nil, type: String? = nil) { | ||
self.url = url | ||
self.sizes = sizes | ||
self.type = type | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
guard let url else { | ||
return | ||
} | ||
let element = try container.appendElement("link") | ||
try element.attr("rel", "icon") | ||
try element.attr("href", url.absoluteString) | ||
if let sizes { | ||
try element.attr("sizes", sizes) | ||
} | ||
if let type { | ||
try element.attr("type", type) | ||
} | ||
} | ||
|
||
private let url: URL? | ||
private let sizes: String? | ||
private let type: String? | ||
} |
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,22 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct IconTests { | ||
@Test func nilURL() throws { | ||
try #expect(renderHTML(Icon(nil)) == "") | ||
} | ||
|
||
@Test func validURL() throws { | ||
try #expect(renderHTML(Icon(URL(string: "/favicon.ico"))) == #"<link rel="icon" href="/favicon.ico" />"#) | ||
} | ||
|
||
@Test func sizes() throws { | ||
try #expect(renderHTML(Icon(URL(string: "/favicon.ico"), sizes: "16x16")) == #"<link rel="icon" href="/favicon.ico" sizes="16x16" />"#) | ||
} | ||
|
||
@Test func type() throws { | ||
try #expect(renderHTML(Icon(URL(string: "/favicon.ico"), type: "image/png")) == #"<link rel="icon" href="/favicon.ico" type="image/png" />"#) | ||
} | ||
} |