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

Extend dl with div wrapped groups #89

Merged
merged 7 commits into from
Apr 7, 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
6 changes: 4 additions & 2 deletions Sources/Plot/API/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public extension HTML {
/// The context within an HTML document's `<head>` element.
enum HeadContext: HTMLContext, HTMLScriptableContext {}
/// The context within an HTML document's `<body>` element.
class BodyContext: HTMLStylableContext, HTMLScriptableContext, HTMLImageContainerContext {}
class BodyContext: HTMLStylableContext, HTMLScriptableContext, HTMLImageContainerContext, HTMLDividableContext {}
/// The context within an HTML `<a>` element.
final class AnchorContext: BodyContext, HTMLLinkableContext {}
/// The context within an HTML `<audio>` element.
Expand All @@ -99,7 +99,7 @@ public extension HTML {
/// The context within an HTML `<datalist>` element.
enum DataListContext: HTMLOptionListContext {}
/// The context within an HTML `<dl>` element.
enum DescriptionListContext: HTMLStylableContext {}
enum DescriptionListContext: HTMLStylableContext, HTMLDividableContext {}
/// The context within an HTML `<details>` element.
final class DetailsContext: BodyContext {}
/// The context within an HTML `<embed>` element.
Expand Down Expand Up @@ -153,6 +153,8 @@ public protocol HTMLContext {}
/// Context shared among all HTML elements that can have their dimensions
/// (width and height) specified through attributes, such as `<video>`.
public protocol HTMLDimensionContext: HTMLContext {}
/// Context shared among all HTML elements can be divided using `<div>` elements.
public protocol HTMLDividableContext: HTMLContext {}
/// Context shared among all HTML elements that can contain an `<img>` element.
public protocol HTMLImageContainerContext: HTMLContext {}
/// Context shared among all HTML elements that act as some form
Expand Down
2 changes: 1 addition & 1 deletion Sources/Plot/API/HTMLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public enum ElementDefinitions {
/// Definition for the `<button>` element.
public enum Button: ElementDefinition { public static var wrapper = Node.button }
/// Definition for the `<div>` element.
public enum Div: ElementDefinition { public static var wrapper = Node.div }
public enum Div: ElementDefinition { public static var wrapper = Node<HTML.BodyContext>.div }
/// Definition for the `<fieldset>` element.
public enum FieldSet: ElementDefinition { public static var wrapper = Node.fieldset }
/// Definition for the `<footer>` element.
Expand Down
14 changes: 8 additions & 6 deletions Sources/Plot/API/HTMLElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ public extension Node where Context: HTML.BodyContext {
.element(named: "details", nodes: nodes)
}

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

/// Add a `<dl>` HTML element within the current context.
/// - parameter nodes: The element's attributes and child elements.
static func dl(_ nodes: Node<HTML.DescriptionListContext>...) -> Node {
Expand Down Expand Up @@ -523,3 +517,11 @@ public extension Node where Context: HTMLScriptableContext {
.element(named: "script", nodes: nodes)
}
}

public extension Node where Context: HTMLDividableContext {
/// Add a `<div>` HTML element within the current context.
/// - parameter nodes: The element's attributes and child elements.
static func div(_ nodes: Node<Context>...) -> Node {
.element(named: "div", nodes: nodes)
}
}
4 changes: 2 additions & 2 deletions Tests/PlotTests/HTMLComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ final class HTMLComponentTests: XCTestCase {
}

func testAddingClassToNode() {
let html = Node.div().class("hello").render()
XCTAssertEqual(html, #"<div class="hello"></div>"#)
let html = Node.div(.p()).class("hello").render()
XCTAssertEqual(html, #"<div class="hello"><p></p></div>"#)
}

func testEnvironmentValuesDoNotApplyToSiblings() {
Expand Down
23 changes: 23 additions & 0 deletions Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ final class HTMLTests: XCTestCase {
<body><dl><dt>Term</dt><dd>Description</dd></dl></body>
""")
}

func testDescriptionListWithDiv() {
let html = HTML(.body(.dl(
.div(
.dt("Last modified time"),
.dd("2004-12-23T23:33Z")
),
.div(
.dt("Recommended update interval"),
.dd("60s")
),
.div(
.dt("Authors"),
.dt("Editors"),
.dd("Robert Rothman"),
.dd("Daniel Jackson")
)
)))

assertEqualHTMLContent(html, """
<body><dl><div><dt>Last modified time</dt><dd>2004-12-23T23:33Z</dd></div><div><dt>Recommended update interval</dt><dd>60s</dd></div><div><dt>Authors</dt><dt>Editors</dt><dd>Robert Rothman</dd><dd>Daniel Jackson</dd></div></dl></body>
""")
}

func testTextDirectionalityLeftToRight() {
let html = HTML(.body(
Expand Down