-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
lit-html - non quoted attribute values give ERROR #77
Comments
Yeah I'm gonna need actual HTML code pasted as plain text with parse output from Closing until it's evident it's an issue here. |
can you paste the code... |
Sure, i pasted the code with the cat command above, i'm sorry i didn't put it here
|
minimal repro <img src=${'x'}> imo this is out of scope for an html parser, and should be handled by an JSX parser related #57 |
Is there a way to tag attribute values that do not have quotes, like I'm using the html parser inside html`...` templates on lit. Your solution would be to inject the jsx parser inside those templates? The JSX parser already parses expressions as |
that would require to change the html parser but still, this is valid html, and a browser will show <div title=${expr}>text</div>
aah, so <div>${expr}</div>
<${tagName}>text</${tagName}> so you would have to extend the html parser to parse also you will have to handle escaped quotes: |
I haven't encountered that use case yet on lit-html.
Where i found that:
Apparently the problem comes when there's a single quotes inside the brackets, eg.
|
I started getting my whole Neovim to freeze whenever I opened some of the files in the project I am working on that use After finding this thread, I tried surrounding all attributes with quotes and noticed that the error was gone. Is there any workaround for this issue? I want to use the HTML parser but it's impossible since it completely freezes the application. |
hello @DiegoCardoso can you tell me if you have a function with a string parameter? e.g. |
javascript template string != html this will never work with an html parser you will have to create a javascript-and-html parser const tpl = html`<div class=${x ? "a" : 'b'}></div>`;
// ^^^^^^^^^^^^^^^^^ js ^ ^ ^
// ^^^^^^^^^^^ html
// ^^^^^^^^^^^^^^^^ js
// ^^^^^^^ html
// ^^ js |
This is html we're talking about tho. Here it is a minimal repro:
output:
the problem i'm encountering is not similar to @DiegoCardoso
ironically, i love qwik. it's my favorite lmao. |
no, that is "javascript with embedded html" |
i provided a html example with tree-sitter-cli output using this repo's latest commit as the parser regardless of where i found this, which is html templates from javascript, i think the fact that as long as the attribute value is unquoted, has a is there a way to ignore single quotes inside i'd love to implement and test it, could you maybe point me in the right direction? |
it does matter where this html is stored. context matters
exactly
at least this should not give a parse error unquoted attribute values end on space or
such hacks dont make sense
maybe look at JSX parsers see also |
It is still not clear to me why the Nevertheless, it feels wrong that the parser would cause a stack overflow error and freeze the whole application. Is there a simple way to disable the parser so that it won't try to parse JS strings? Now I need to uninstall the parser so I can continue working on my project. |
Thank you so much for your help @milahu !! Been testing it for these past few days and gave me an idea about creating a lit-html parser that would not take into account single quotes cases and parse ${...} into their own elements, since that's mostly what's been happening for html`` templates inside javascript. |
nice try, but that is still no javascript-in-javascript-string-interpolation parser --- a/grammar.js
+++ b/grammar.js
@@ -120,13 +120,16 @@ module.exports = grammar({
choice(
$.attribute_value,
$.quoted_attribute_value,
+ $.js_expression_value,
),
)),
),
- attribute_name: _ => /[^<>"'/=\s]+/,
+ attribute_name: _ => /[^<>"/=\s]+/,
- attribute_value: _ => /[^<>"'=\s]+/,
+ attribute_value: _ => /[^<>"=\s]+/,
+
+ js_expression_value : _ => /\${[^}]+}/,
// An entity can be named, numeric (decimal), or numeric (hexacecimal). The
// longest entity name is 29 characters long, and the HTML spec says that
@@ -134,7 +137,6 @@ module.exports = grammar({
entity: _ => /&(#([xX][0-9a-fA-F]{1,6}|[0-9]{1,5})|[A-Za-z]{1,30});?/,
quoted_attribute_value: $ => choice(
- seq('\'', optional(alias(/[^']+/, $.attribute_value)), '\''),
seq('"', optional(alias(/[^"]+/, $.attribute_value)), '"'),
),
this fails on const tpl = html`
<br id=${"}"}>
`; and const tpl = html`
<br id=${await (async () => {
// this can contain literally any javascript code, wrapped in an IIFE
(await import("...")).asdf();
return html`... including nested template strings`;
})()}>
`; nit: rename tree-sitter-html to tree-sitter-lit-html |
When working with lit-element webcomponents, i have a problem for binded attribute values.
When the attribute value is just ${...} without quotes, the next line gives an error because the parser is not recognizing the attribute value as a
quoted_attribute_value
As you can see, i get ERROR in the tree sitter playground.
But when the binded value is enclosed with quotes the problem goes away:
Note: for the framework, it is not necessary for attribute values to be enclosed with quotes, they work without them too.
The text was updated successfully, but these errors were encountered: