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

Fragile HTML parser -> create Debug bundle #75

Open
mindplay-dk opened this issue Mar 10, 2020 · 6 comments
Open

Fragile HTML parser -> create Debug bundle #75

mindplay-dk opened this issue Mar 10, 2020 · 6 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@mindplay-dk
Copy link
Contributor

In this example, I didn't explicitly close a couple of <input> elements:

https://codesandbox.io/s/vigilant-goodall-hqh8u

That is, <input> and other HTML void elements seem to require a trailing slash, e.g. <input /> - but there is no error message when void elements aren't explicitly terminated, and you get some strange results in the DOM.

Is the parser XHTML by design?

Handling HTML 5 void elements shouldn't require much work, and shouldn't add much to the footprint, I think - I wrote a toy HTML 5 parser myself in 0.5 KB, and the magic is less than 100 bytes.

@luwes luwes added the question Further information is requested label Mar 11, 2020
@luwes
Copy link
Owner

luwes commented Mar 11, 2020

Good question @mindplay-dk. To be honest I've stumbled upon this issue myself where I forgot the closing slash. Sinuous's HTML parser is 95% https://github.com/developit/htm. I'm not fully sure why they didn't add self closing tags; performance or size.

@mindplay-dk
Copy link
Contributor Author

Hmm, yeah...

htm is JSX-like syntax in plain JavaScript

So I guess the name is a little misleading - it's not trying to be a substitute for HTML, but for JSX, so maybe that's why the parsing rules are XML-like, which JSX is too...

@luwes
Copy link
Owner

luwes commented Mar 18, 2020

closing this one as I think it's a fairly small inconvenience in devex.

I personally rather add a closing slash that gets compiled away than to add an extra 100 bytes to my bundle (OCD 😄)

@luwes luwes closed this as completed Mar 18, 2020
@mindplay-dk
Copy link
Contributor Author

closing this one as I think it's a fairly small inconvenience in devex.

Honestly, it would be, if it would at least error.

Is there some way to make it throw for mismatching opening/closing tags?

I haven't looked at the source, but you must have a stack of some sort that keeps track of open/closed elements? Probably should check if a closing element matches the one on the stack and throw if it doesn't. Maybe check the stack length at the end of an operation, and throw if it's non-empty.

Just getting broken HTML is not good DX imo - besides being difficult to debug, this can can lead to silent bugs that end up in production.

These are syntax errors, which really shouldn't just be ignored. Silent bugs are the worst.

I personally rather add a closing slash that gets compiled away than to add an extra 100 bytes to my bundle

I'm down with that - and as said, if the intent is to work like JSX, that's based on XML and not HTML, so you'd expect to have to close your tags.

But JSX parsers will error if you forget to self-close (or close) any tags.

@luwes luwes reopened this Mar 21, 2020
@luwes
Copy link
Owner

luwes commented Mar 21, 2020

good points! we could possibly add a debug bundle with more error messaging that would make this visible in the the development environment.

@luwes luwes added the enhancement New feature or request label Mar 21, 2020
@luwes luwes changed the title Fragile HTML parser Fragile HTML parser -> create Debug bundle Oct 17, 2020
@Freak613
Copy link

Freak613 commented Mar 26, 2021

Definitely shouldn't be done on runtime. Same as JSX parsers do not work in runtime.
ESLint seems good place to do such checks.
There is one plugin for lit-html, probably worth to try:
https://github.com/43081j/eslint-plugin-lit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants