From 2200adbdfba1b476d25a7234b68d824c0ebf8454 Mon Sep 17 00:00:00 2001
From: Alexander Zank
Date: Fri, 20 Mar 2020 11:40:14 +0100
Subject: [PATCH 1/2] Add support for the `` tag and its `data: URL`
attribute with a new `HTMLLinkableDataContext`. Include `testObject()` and
pass successfully.
---
Sources/Plot/API/HTML.swift | 5 +++++
Sources/Plot/API/HTMLAttributes.swift | 16 ++++++++++++++++
Sources/Plot/API/HTMLElements.swift | 6 ++++++
Tests/PlotTests/HTMLTests.swift | 16 +++++++++++++++-
4 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/Sources/Plot/API/HTML.swift b/Sources/Plot/API/HTML.swift
index 86561d0..1813713 100644
--- a/Sources/Plot/API/HTML.swift
+++ b/Sources/Plot/API/HTML.swift
@@ -83,6 +83,8 @@ public extension HTML {
enum ListContext: HTMLStylableContext {}
/// The context within an HTML ` ` element.
enum MetaContext: HTMLNamableContext {}
+ /// The contect within an HTM `` element.
+ enum ObjectContext: HTMLDimensionContext, HTMLLinkableDataContext, HTMLTypeContext {}
/// The context within an HTML `` element.
enum OptionContext: HTMLValueContext {}
/// The context within an HTML `` element.
@@ -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 ` ` or ``.
public protocol HTMLLinkableContext: HTMLContext {}
+/// Context shared among all HTML elements that support the `data` (URL)
+/// attribute, such as `.`
+public protocol HTMLLinkableDataContext: HTMLContext {}
/// Context shared among all HTML elements that enable media playback,
/// such as `` and ``.
public protocol HTMLMediaContext: HTMLSourceListContext {}
diff --git a/Sources/Plot/API/HTMLAttributes.swift b/Sources/Plot/API/HTMLAttributes.swift
index 17b058a..d0e2b88 100644
--- a/Sources/Plot/API/HTMLAttributes.swift
+++ b/Sources/Plot/API/HTMLAttributes.swift
@@ -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.
diff --git a/Sources/Plot/API/HTMLElements.swift b/Sources/Plot/API/HTMLElements.swift
index f4e600e..725f2cb 100644
--- a/Sources/Plot/API/HTMLElements.swift
+++ b/Sources/Plot/API/HTMLElements.swift
@@ -282,6 +282,12 @@ public extension Node where Context: HTML.BodyContext {
static func noscript(_ nodes: Node...) -> Node {
.element(named: "noscript", nodes: nodes)
}
+
+ /// Add an `` HTML element within the current context.
+ /// - parameter nodes: The element's attributes and child elements.
+ static func object(_ nodes: Node...) -> Node {
+ .element(named: "object", nodes: nodes)
+ }
/// Add an `` HTML element within the current context.
/// - parameter nodes: The element's attributes and child elements.
diff --git a/Tests/PlotTests/HTMLTests.swift b/Tests/PlotTests/HTMLTests.swift
index 51f56d7..abebd9f 100644
--- a/Tests/PlotTests/HTMLTests.swift
+++ b/Tests/PlotTests/HTMLTests.swift
@@ -666,6 +666,19 @@ final class HTMLTests: XCTestCase {