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 all commits
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
2 changes: 2 additions & 0 deletions Sources/Plot/API/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public extension HTML {
enum ListContext: HTMLStylableContext {}
/// The context within an HTML `<meta>` element.
enum MetaContext: HTMLNamableContext {}
/// The contect within an HTML `<object>` element.
enum ObjectContext: HTMLDimensionContext, HTMLTypeContext {}
/// The context within an HTML `<option>` element.
enum OptionContext: HTMLValueContext {}
/// The context within an HTML `<picture>` element.
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 @@ -239,6 +239,22 @@ public extension Node where Context: HTMLMediaContext {
}
}

public extension Attribute where Context == HTML.ObjectContext {
/// 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 == HTML.ObjectContext {
/// 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 Attribute where Context == HTML.AudioSourceContext {
/// Assign a type to this audio source. See `HTMLAudioFormat` for more info.
/// - parameter format: The audio format to assign.
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 @@ -310,6 +310,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
13 changes: 13 additions & 0 deletions Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,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>
""")
}

func testOnClick() {
let html = HTML(
Expand Down