-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
custom element attributes are returned as children rather than properties #21
Comments
well, it seems like a rather big issue... this function here seems to be used to differentiate between estree nodes and properties and since both estree nodes and properties may have a Is branding a possibility? |
Exception for dash in tag name might be viable? Can custom elements be used in SVG? |
Hmmm, what do you mean by "can be used in SVG"? There are no custom svg elements (yet?) (see here point 6.4). But I would expect them to work as a foreign object, embedded in a svg structure. Then again there's a way to check for registered custom elements but all in all I think all this just makes the issue more and more complex. If I understood the code correctly (and mind you, I spent all of 5 minutes skimming over it) changing this line to something like |
This project can be used to generate SVG, with HTML has exceptions for custom element names that overlap with SVG: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name.
I think you meant to link to a spec, but linked to the code here? What is point 6.4 in this code? This code can run on servers. Or ahead of time. So it should work regardless of what is registered.
hast is a plain vanilla json format; so no symbols. The definition of “node” here is basically any object with a |
🙈 I'm so sorry, fixed it. Meant to link to the DOM spec.
Yeah, hit me 5 min after I wrote it as well...
I see, that complicates things...
Well, they're not able to define custom nodes right now, are they? I get your point, but HTML also disallows certain element names etc... maybe an attribute with spaces in it? Would be json compatible but not feasible as an attribute on a custom element I think? |
One popular example in the wild is MDX, which embeds ESM, JSX, and expressions inside HTML.
Can you clarify what you mean? |
Seems to be possible to put arbitrary dash-cased named in the SVG namespace:
|
I'm not sure I follow completely, but that example you gave also leads to a node with children instead of properties?
I was thinking that since Though if you want JSON serializability you'd probably want the tree to be as small as possible, so... also not ideal. |
true, but those would never be valid html elements? Is the problem here that, when parsing a node like Hmmm, this kinda reminds me of all those discussions around JS types, variadic arguments and "aot" knowledge about arguments^^ Isn't the problem originally caused by wanting to offer an "overload" signature of |
Which is inverse from what you are asking about. You want props instead of a node.
Ahh. Yes, we can allow another field.
The concern of hast is more with what DOMs could be generated by an HTML parser; not with what will eventually work at runtime. Folks could have such HTML files and hast needs to represent it.
Yep!
In regular HTML, |
Ah, I see. I didn't realize that hast nodes could also be used like that (even though I did just that a few years ago^^)
true, I think I'm going to use that in my lib. For my case that solves the issue, thanks :) Now, for this "doing the inverse"... I'm thinking that if |
What are you doing? The props handling other than the Without the props handling and the overload, this project is useless, as you can make elements directly:
No, you can pass children: |
Hey, sorry for the late answer.
Writing a lib to convert jsx to an html string. You can see more here. Fair warning, this project is very much in flux.
I agree as far as the props handling is concerned, but I don't think the overload is necessary for this project to have a purpose. Whether I call
Yeah... you're right again. Well, I don't see many other options then. |
No worries! Fun! The trouble with JSX is that it could be used for anything. Not for HTML per se. If for HTML, then frameworks differ in what they expect too. Some support Then, JSX is more than just HTML. You have components. And JavaScript code that needs to be evaluated somewhere. How do you want to handle that? Although, https://github.com/syntax-tree/hastscript/blob/2a7451dc1eb2adc6b07af6efed8a8bdcc8f13758/lib/create-automatic-runtime.js could be used but doesn’t do all you want I believe, and it has the same problem. I would recommend looking at the existing code across this organization. https://github.com/syntax-tree/hast-util-to-jsx-runtime, https://github.com/syntax-tree/hast-util-from-dom, and also https://github.com/wooorm/property-information and https://github.com/mdx-js/mdx/tree/main/packages/mdx. All depends on whether you’re building something at compile-time or runtime, and what the goals are of the tool. |
Thanks for the suggestions - before I started writing this lib I did some digging in the unified ecosystem, but found no match for what I wanted :) I am quite specific in my implementation what kind of JSX I want to allow and am trying to stay as close to HTML as I can, apart from things like allowing And yes, come to think of it, I don't think I need this library for that, simply creating the objects should be enough 🤔 |
well, after playing around a bit I think I'm going to stick with this library. While I am currently skipping property handling (which should only be temporary anyway) the children handling is simply convenient to have, and it works fine for my purposes. |
Note that this issue doesn't require custom elements and occurs with |
Yeah, but... when would you want that? That's not valid html, is it?
…On Thu, Jan 18, 2024, 18:29 Matthew Phillips ***@***.***> wrote:
Note that this issue doesn't require custom elements and occurs with <div
type="number" value="1"> as well.
—
Reply to this email directly, view it on GitHub
<#21 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACOHZTVJAWHZBRQJC4ANHNLYPFLZJAVCNFSM6AAAAABB2E462OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJYHEYTOMRWGQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
@matthewp why are you trying to create that? |
@hesxenon Is it valid HTML, open up your browser and do this: new DOMParser().parseFromString('<div type="number" value="1"></div>', 'text/html') And you'll get a DOM node with those attributes. HTML actually doesn't care if you add arbitrary attributes to elements. @wooorm I don't actually need |
"Valid" relating to html is different things. Accepted by the parser and exposed in the DOM are one thing but... This tool is a small tool that makes it easier for humans to make ASTs. Why are you using this project, and in way? |
@wooorm In Astro we are using
We are directly using |
One thing I can think of to solve it is to treat the 2nd argument as parameters when it is ambiguous, and the 3rd argument is an array, and there are exactly 3 arguments. That's the typical case if you'd do programmatic prop and child building and then call the h function. As is done in both your use cases? As an aside, why is raw used here in Astro? I thought the mdx jsx stuff was used to parse these things already? |
We don't use the mdx stuff with regular |
Or maybe a toProperties function here does basically everything for all the tools too... @remcohaszing maybe you have a preference? |
I think this is the key. There is no way to reliably detect the meaning of the second argument. No matter what you do Personally I would remove this overload entirely, which would of course be semver major. Lines 74 to 77 in 2a7451d
|
I personally quite love doing |
I looks quite nice in examples. I wonder if it’s actually useful in production code too, where parameters are likely some variable, not a hardcoded value. Personally I like to use |
JSX is one use case but so is calling |
Is the idea of special casing dashed names (custom elements) still ok as a stopgap? This comes up because a popular library Shoelace: https://shoelace.style/ has its own input element and thus mirrors the |
I can’t deny the |
I made this project for such use cases. Humans to call a DSL that looks like what it does so they can omit superfluous things. There are several other niceties too, such as Any reason you prefer the breaking change (which is a useful and IMO nice-to-have overload) instead of the other viable alternatives?
Yep, I think that’s fine too! |
I just want to point out that there are many popular and very convenient
ast libraries out there that _do_ force explicit prop passing and are still
very convenient and human readable.
Your project is yours to do with as you please ofc, and it's a very useful
library either way. But I also think removing the overload would be the
cleaner option, especially in the long run.
…On Sat, Jan 20, 2024, 10:11 Titus ***@***.***> wrote:
I made this project for such use cases. Humans to call a DSL that looks
like what it does so they can omit superfluous things. There are several
other niceties too, such as h('div#id.class').
Why do you want to cause a breaking change when there are also like 3
viable alternatives? Anything you don’t like about those alternatives?
Is the idea of special casing dashed names (custom elements) still ok as a
stopgap?
Yep, I think that’s fine too!
—
Reply to this email directly, view it on GitHub
<#21 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACOHZTQVYCQW5UXWYMVYVQTYPOC5FAVCNFSM6AAAAABB2E462OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBSGAZDAMBSGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This issue practically only appeared when props with In most practical cases, it is possible to detect the difference between props and nodes in this overload. |
Thank you @wooorm! |
Initial checklist
Affected packages and versions
8
Link to runnable example
https://jsbin.com/mivorid/2/edit?html,console
Steps to reproduce
simply calling
h("my-custom-element", {type: "date", value: ""}, [])
yields unexpected results.Expected behavior
It should yield
Actual behavior
It does yield
Affected runtime and version
[email protected]/[email protected]
Affected package manager and version
No response
Affected OS and version
No response
Build and bundle tools
No response
The text was updated successfully, but these errors were encountered: