-
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
8 changed files
with
117 additions
and
18 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
Sources/Slipstream/Documentation.docc/Views/W3C/EmbeddedContent/Image.md
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,7 @@ | ||
# ``Image`` | ||
|
||
## Topics | ||
|
||
### Required accessibility modifiers | ||
|
||
- ``View/accessibilityLabel(_:)`` |
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 |
---|---|---|
|
@@ -8,3 +8,7 @@ | |
- ``View/classNames(_:)`` | ||
- ``View/id(_:)`` | ||
- ``View/language(_:)`` | ||
|
||
### Images | ||
|
||
- ``View/accessibilityLabel(_:)`` |
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
13 changes: 13 additions & 0 deletions
13
Sources/Slipstream/W3C/Attributes/View+accessibilityLabel.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,13 @@ | ||
extension View { | ||
/// Provides equivalent content for those who cannot process images or | ||
/// who have image loading disabled. | ||
/// | ||
/// - SeeAlso: W3C [alt](https://html.spec.whatwg.org/multipage/images.html#alt) guidance. | ||
/// | ||
/// ## See Also | ||
/// | ||
/// - ``Image`` | ||
public func accessibilityLabel(_ string: String) -> some View { | ||
modifier(AttributeModifier("alt", value: string)) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Sources/Slipstream/W3C/Elements/EmbeddedContent/Image.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,41 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A view that displays an image. | ||
/// | ||
/// ```swift | ||
/// struct MySiteContent: View { | ||
/// var body: some View { | ||
/// Body { | ||
/// Image(URL(string: "/logo.png")) | ||
/// .accessibilityLabel("My awesome site logo") | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - SeeAlso: W3C [`img`](https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element) specification. | ||
/// - SeeAlso: W3C [requirements for providing text to act as an alternative for images](https://html.spec.whatwg.org/multipage/images.html#alt). | ||
/// | ||
/// ## See Also | ||
/// | ||
/// - ``View/accessibilityLabel(_:)`` | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Image: View { | ||
/// Creates a Stylesheet view. | ||
public init(_ url: URL?) { | ||
self.url = url | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
guard let url else { | ||
return | ||
} | ||
let element = try container.appendElement("img") | ||
try element.attr("src", url.absoluteString) | ||
} | ||
|
||
private let url: URL? | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct ImageTests { | ||
@Test func nilURL() throws { | ||
try #expect(renderHTML(Image(nil)) == "") | ||
} | ||
|
||
@Test func url() throws { | ||
try #expect(renderHTML(Image(URL(string: "/logo.png"))) == #"<img src="/logo.png" />"#) | ||
} | ||
|
||
@Test func accessibilityLabel() throws { | ||
try #expect(renderHTML(Image(URL(string: "/logo.png")).accessibilityLabel("My logo")) == #"<img src="/logo.png" alt="My logo" />"#) | ||
} | ||
} |