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

Attributes as ExpressibleByDictionaryLiteral #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions Sources/HTML/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

@_exported import Swim

extension Node {
static func element(_ name: String, _ attributes: [String:String], _ node: Node?) -> Node {
Node.element(name, Attributes(attributes: attributes.sorted(by: { $0.key < $1.key }).map(Attribute.init)), node)
}
}



public struct ATag: Tag {
public let elementName: String = "a"
Expand Down
31 changes: 26 additions & 5 deletions Sources/Swim/Node.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import Foundation

public struct Attribute: Hashable {
public var key: String
public var value: String

public init(_ pair: (String, String)) {
(self.key, self.value) = pair
}
}

public struct Attributes: ExpressibleByDictionaryLiteral, Hashable {
public var attributes: [Attribute]

public init(attributes: [Attribute]) {
self.attributes = attributes
}

public init(dictionaryLiteral elements: (String, String)...) {
self.attributes = elements.map(Attribute.init)
}
}

public enum Node: Hashable {
// The `Node`'s name, attribute and children.
indirect case element(String, [String: String], Node?)
indirect case element(String, Attributes, Node?)

// The `Node`'s text contents.
case text(String)
Expand Down Expand Up @@ -49,14 +70,14 @@ extension Node: TextOutputStreamable {
target.write("<")
target.write(name)

for (key, value) in attributes.sorted(by: { $0 < $1 }) {
for attribute in attributes.attributes {
target.write(" ")
target.write(key)
target.write(attribute.key)

guard value != "" else { continue }
guard attribute.value != "" else { continue }

target.write("=\"")
target.write(value.replacingOccurrences(of: "\"", with: "&quot;"))
target.write(attribute.value.replacingOccurrences(of: "\"", with: "&quot;"))
target.write("\"")
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Swim/Visitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public protocol Visitor {
associatedtype Result

func visitElement(name: String, attributes: [String: String], child: Node?) -> Result
func visitElement(name: String, attributes: Attributes, child: Node?) -> Result

func visitText(text: String) -> Result

Expand Down Expand Up @@ -42,7 +42,7 @@ extension Visitor {
}

public extension Visitor where Result == Node {
func visitElement(name: String, attributes: [String: String], child: Node?) -> Result {
func visitElement(name: String, attributes: Attributes, child: Node?) -> Result {
.element(name, attributes, child.map(visitNode))
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/HTMLTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ final class HTMLTests: XCTestCase {
struct TextExtractionVisitor: Visitor {
typealias Result = [String]

func visitElement(name: String, attributes: [String : String], child: Node?) -> [String] {
func visitElement(name: String, attributes: Attributes, child: Node?) -> [String] {
child.map(visitNode) ?? []
}

Expand Down Expand Up @@ -264,7 +264,7 @@ final class HTMLTests: XCTestCase {
struct Sanitizer: Visitor {
var denyList: [Tag]

func visitElement(name: String, attributes: [String : String], child: Node?) -> Node {
func visitElement(name: String, attributes: Attributes, child: Node?) -> Node {
if denyList.contains(where: { $0.elementName == name }) {
let original = Node.element(name, attributes, child)

Expand Down