-
Notifications
You must be signed in to change notification settings - Fork 56
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 Fragment Node #23
Conversation
One other consideration: if we start composing HTML from a bunch of |
* Change element children from [Node] to Node * Update * Variadics * Fix * Update README.md * Add HTML example
Sources/Html/Node.swift
Outdated
} | ||
|
||
public prefix func ... (nodes: [Node]) -> Node { | ||
return .fragment(nodes.filter { !$0.isEmpty }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand why this filter is needed. What's up with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was just trying to get rid of the nesting and slipped it in here. Probably not necessary or probably better as an explicit traversal?
Sources/Html/Node.swift
Outdated
case .element: | ||
return false | ||
case let .fragment(children): | ||
return children.isEmpty || children.allSatisfy { $0.isEmpty } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can drop the children.isEmpty
since allSatisfy
returns true for empty collections.
``` | ||
|
||
This makes the “Swiftification” of an HTML document looks very similar to the original document. | ||
|
||
## FAQ | ||
|
||
<!-- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha oops! wonder how that slipped in
Co-Authored-By: stephencelis <stephen.celis@gmail.com>
This tweet points out a problem with the current API:
https://twitter.com/hoagy_ytfc/status/1079550683253219328
Without being able to represent a list of nodes as a node itself, we can't currently use Vapor's protocol-oriented
ResponseEncodable
API to return a doctype alongside more root nodes:One solution is to add a
fragment
node, which represents a document or fragment of HTML. The DOM spec has included aDocumentFragment
node type for a very long time and React recently added its ownFragment
API, so the precedent is there. We've also struggled with ourView
construct in the past as to whether we should be composing(A) -> Node
or(A) -> [Node]
, so this could help solve that problem.The change in the PR results in:
A few unanswered questions:
Node.fragment
is pretty verbose. Even when there's inference,.fragment([…])
might not read well. Do we wanna think of alternative names? Do we want to add adocument
function as an alias?Do we remove the
render([Node])
overload or keep it around as convenience?Do we add
ExpressibleByArrayLiteral
or are we worried about Swift diagnostics?