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 the dir HTML attribute #72

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 6 additions & 0 deletions Sources/Plot/API/ComponentAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public extension Component {
attribute(named: "id", value: id)
}

/// Assign a directionality to this component's element.
/// - parameter directionality: The directionality to assign.
func directionality(_ directionality: Directionality) -> Component {
attribute(named: "dir", value: directionality.rawValue)
}

/// Assign whether this component hierarchy's `Input` components should have
/// autocomplete turned on or off. This value is placed in the environment, and
/// is thus inherited by all child components. Note that this modifier only
Expand Down
12 changes: 12 additions & 0 deletions Sources/Plot/API/Directionality.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Plot
* Copyright (c) John Sundell 2021
* MIT license, see LICENSE file for details
*/

/// Enum defining an element's text directionality.
public enum Directionality: String {
case leftToRight = "ltr"
case rightToLeft = "rtl"
case auto = "auto"
}
12 changes: 12 additions & 0 deletions Sources/Plot/API/HTMLAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public extension Attribute where Context: HTMLContext {
static func title(_ title: String) -> Attribute {
Attribute(name: "title", value: title)
}

/// Specify a directionality for the element.
/// - parameter directionality: The directionality to assign to the element.
static func dir(_ directionality: Directionality) -> Attribute {
alobaili marked this conversation as resolved.
Show resolved Hide resolved
Attribute(name: "dir", value: directionality.rawValue)
}
}

public extension Node where Context: HTMLContext {
Expand Down Expand Up @@ -80,6 +86,12 @@ public extension Node where Context: HTMLContext {
static func hidden(_ isHidden: Bool) -> Node {
isHidden ? .attribute(named: "hidden") : .empty
}

/// Specify a directionality for the element.
/// - parameter directionality: The directionality to assign to the element.
static func dir(_ directionality: Directionality) -> Node {
alobaili marked this conversation as resolved.
Show resolved Hide resolved
.attribute(named: "dir", value: directionality.rawValue)
}
}

public extension Attribute where Context: HTMLNamableContext {
Expand Down
8 changes: 8 additions & 0 deletions Tests/PlotTests/HTMLComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ final class HTMLComponentTests: XCTestCase {
""")
}

func testAssigningDirectionalityToElement() {
let html = Paragraph("Hello")
.dir(.leftToRight)
.render()

XCTAssertEqual(html, #"<p dir="ltr">Hello</p>"#)
}

func testAppendingClasses() {
let html = Paragraph("Hello")
.class("one")
Expand Down
55 changes: 55 additions & 0 deletions Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ final class HTMLTests: XCTestCase {
XCTAssertEqual(html.render(), #"<!DOCTYPE html><html lang="en"></html>"#)
}

func testPageDirectionalityLeftToRight() {
let html = HTML(.dir(.leftToRight))
XCTAssertEqual(html.render(), #"<!DOCTYPE html><html dir="ltr"></html>"#)
}

func testPageDirectionalityRightToLeft() {
let html = HTML(.dir(.rightToLeft))
XCTAssertEqual(html.render(), #"<!DOCTYPE html><html dir="rtl"></html>"#)
}

func testPageDirectionalityAuto() {
let html = HTML(.dir(.auto))
XCTAssertEqual(html.render(), #"<!DOCTYPE html><html dir="auto"></html>"#)
}

func testHeadAndBody() {
let html = HTML(.head(), .body())
assertEqualHTMLContent(html, "<head></head><body></body>")
Expand Down Expand Up @@ -249,6 +264,46 @@ final class HTMLTests: XCTestCase {
""")
}

func testTextDirectionalityLeftToRight() {
let html = HTML(.body(
.h1(.dir(.leftToRight), "Text")
))

assertEqualHTMLContent(html, #"<body><h1 dir="ltr">Text</h1></body>"#)
}

func testTextDirectionalityRightToLeft() {
let html = HTML(.body(
.h1(.dir(.rightToLeft), "Text")
))

assertEqualHTMLContent(html, #"<body><h1 dir="rtl">Text</h1></body>"#)
}

func testTextDirectionalityAuto() {
let html = HTML(.body(
.h1(.dir(.auto), "Text")
))

assertEqualHTMLContent(html, #"<body><h1 dir="auto">Text</h1></body>"#)
}

func testInputDirectionalityAuto() {
let html = HTML(.body(
.input(.dir(.auto))
))

assertEqualHTMLContent(html, #"<body><input dir="auto"/></body>"#)
}

func testTextAreaDirectionalityLeftToRight() {
let html = HTML(.body(
.textarea(.dir(.auto))
))

assertEqualHTMLContent(html, #"<body><textarea dir="auto"></textarea></body>"#)
}

func testAnchors() throws {
let html = try HTML(.body(
.a(.href("a.html"), .target(.blank), .text("A")),
Expand Down