Replies: 1 comment
-
It's correct for Question 1, but incorrect for Question 2. VanJS supports you to explicitly specify the custom URI while constructing elements. For instance, the code below illustrate how to use VanJS to build SVG elements: const {circle, path, svg} = van.tags("http://www.w3.org/2000/svg")
const Smiley = () => svg({width: "16px", viewBox: "0 0 50 50"},
circle({cx: "25", cy: "25", "r": "20", stroke: "black", "stroke-width": "2", fill: "yellow"}),
circle({cx: "16", cy: "20", "r": "2", stroke: "black", "stroke-width": "2", fill: "black"}),
circle({cx: "34", cy: "20", "r": "2", stroke: "black", "stroke-width": "2", fill: "black"}),
path({"d": "M 15 30 Q 25 40, 35 30", stroke: "black", "stroke-width": "2", fill: "transparent"}),
)
van.add(document.body, Smiley()) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Today I had the following conversation with Claude and I am wondering if Claude is right. Can anybody confirm that the answers from Claude are right?
Question 1: Does VanJS use the top-down or bottom-up method when building a DOM tree?
VanJS uses a bottom-up approach when building the DOM tree. This method distinguishes VanJS from many other JavaScript frameworks and is one of the core concepts of its design.
With the bottom-up method, the innermost child elements are created first, and then work outward toward the parent nodes. This means:
Here's a simple example of how VanJS works:
In this example:
This bottom-up approach is a key difference from frameworks like React, which typically use a top-down approach where the tree is first declared as a whole and then rendered.
The bottom-up approach of VanJS contributes to its lightweight nature and simplicity, as it allows for a more natural composition of JavaScript function calls.
Question 2: If VanJS uses the bottom-up approach, it seems impossible for VanJS to correctly implement XML namespaces. A child node only has the namespace prefix (for example 'xlink'), but the corresponding namespace URL is defined on an ancestor (for example 'xmlns:xlink="http://www.w3.org/1999/xlink"'). With a bottom-up method, the parent node doesn't exist at the time the child node is created, so it's not possible to determine the correct namespace using the lookupNamespaceURI method. However, the namespace URL is required for creating the child node with the createElementNS method. What does VanJS do to solve this dilemma?
You've identified an interesting problem. With a bottom-up approach like in VanJS, it indeed seems challenging to correctly implement XML namespaces, since children are created before their parents.
VanJS solves this namespace dilemma through a pragmatic approach:
Predefined namespaces: VanJS directly supports some commonly used namespaces, especially for SVG and sometimes for other specific use cases.
Explicit namespace specification: When using namespaces in VanJS, developers must explicitly specify the namespace rather than relying on namespace inheritance.
Special helper tags: For SVG elements, for example, VanJS offers special helper functions that already use the correct SVG namespace.
Here's an example of handling SVG in VanJS:
For more specific namespaces like XLink, in VanJS you would need to set these attributes directly, e.g.:
In practice, it's important to know that modern browsers correctly interpret many attribute-namespace combinations even without explicit namespace declarations, especially with standardized namespaces like SVG and XLink.
VanJS ultimately circumvents the problem through a combination of predefined namespaces for common use cases and the expectation that developers will do the necessary follow-up work or use alternative methods for more specific namespaces.
Beta Was this translation helpful? Give feedback.
All reactions