Skip to content

Commit

Permalink
Merge pull request #39 from AlexLike/master
Browse files Browse the repository at this point in the history
Add support for the <object> tag
  • Loading branch information
JohnSundell authored Mar 31, 2023
2 parents d8fa91a + 9f93057 commit caaef5f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
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

0 comments on commit caaef5f

Please sign in to comment.