Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the <object> tag #39

Merged
merged 4 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/Plot/API/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public extension HTML {
enum ListContext: HTMLStylableContext {}
/// The context within an HTML `<meta>` element.
enum MetaContext: HTMLNamableContext {}
/// The contect within an HTM `<object>` element.
enum ObjectContext: HTMLDimensionContext, HTMLLinkableDataContext, HTMLTypeContext {}
/// The context within an HTML `<option>` element.
enum OptionContext: HTMLValueContext {}
/// The context within an HTML `<picture>` element.
Expand Down Expand Up @@ -117,6 +119,9 @@ public protocol HTMLImageContainerContext: HTMLContext {}
/// Context shared among all HTML elements that act as some form
/// of link to an external resource, such as `<link>` or `<a>`.
public protocol HTMLLinkableContext: HTMLContext {}
/// Context shared among all HTML elements that support the `data` (URL)
/// attribute, such as `<object>.`
public protocol HTMLLinkableDataContext: HTMLContext {}
AlexLike marked this conversation as resolved.
Show resolved Hide resolved
/// Context shared among all HTML elements that enable media playback,
/// such as `<audio>` and `<video>`.
public protocol HTMLMediaContext: HTMLSourceListContext {}
Expand Down
16 changes: 16 additions & 0 deletions Sources/Plot/API/HTMLAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ public extension Node where Context: HTMLSourceContext {
}
}

public extension Attribute where Context: HTMLLinkableDataContext {
/// Assign an external resource to the element, using its `data` attribute.
/// - parameter url: The data URL to assign.
static func data(_ url: URLRepresentable) -> Attribute {
Attribute(name: "data", value: url.string)
}
}

public extension Node where Context: HTMLLinkableDataContext {
/// Assign an external resource to the element, using its `data` attribute.
/// - parameter url: The data URL to assign.
static func data(_ url: URLRepresentable) -> Node {
.attribute(named: "data", value: url.string)
}
}

public extension Node where Context: HTMLMediaContext {
/// Assign whether the element's media controls should be enabled.
/// - parameter enableControls: Whether controls should be shown.
Expand Down
6 changes: 6 additions & 0 deletions Sources/Plot/API/HTMLElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ public extension Node where Context: HTML.BodyContext {
static func noscript(_ nodes: Node<HTML.BodyContext>...) -> Node {
.element(named: "noscript", nodes: nodes)
}

/// Add an `<object>` HTML element within the current context.
/// - parameter nodes: The element's attributes and child elements.
static func object(_ nodes: Node<HTML.ObjectContext>...) -> Node {
.element(named: "object", nodes: nodes)
}

/// Add an `<ol>` HTML element within the current context.
/// - parameter nodes: The element's attributes and child elements.
Expand Down
16 changes: 15 additions & 1 deletion Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,19 @@ final class HTMLTests: XCTestCase {
</picture></body>
""")
}

func testObject() {
let html = HTML(.body(.object(
.data("vector.svg"),
.attribute(.type("image/svg+xml")),
.attribute(.width(200)),
.attribute(.height(100))
)))

assertEqualHTMLContent(html, """
<body><object data="vector.svg" type="image/svg+xml" width="200" height="100"></object></body>
""")
}
}

extension HTMLTests {
Expand Down Expand Up @@ -732,7 +745,8 @@ extension HTMLTests {
("testDataAttributes", testDataAttributes),
("testSubresourceIntegrity", testSubresourceIntegrity),
("testComments", testComments),
("testPicture", testPicture)
("testPicture", testPicture),
("testObject", testObject)
]
}
}