6 ms·
The First HTML LSP That Reports Syntax Errors
- paranoidxprod 2y agoThe LSP space is super interesting right now. Looking forward to diving deeper into/trying this!
- meiraleal 2y agoThe specification is so huge and complex. I was interested for a while but that faded fast when I started to dig into it. Also no browser-only support made it a no-go for me. Although I have seem a few cool hacks to run it in a webworker.
- ivanjermakov 2y agoLSP server does not have to implement all of the spec to be useful. Just using document change and diagnostics allows to implement basic linter or spell checker. I can recommend a nice video series on implementing LSP from scratch: https://youtube.com/playlist?list=PLq5tGLDKHlW9XKRj5-plHdvbkT5AOdM7-&si=6r1WkhasHJJ3FkCg https://youtube.com/playlist?list=PLq5tGLDKHlW9XKRj5-plHdvbk...
- kristoff_it 2y agoYep, the LSP mentioned in the blog post (I'm the author) only implements diagnostics and autoformatting for now. With just that you can already provide a lot of utility. This is my starting point for implementing language servers in Zig: https://github.com/kristoff-it/zig-lsp-kit https://github.com/kristoff-it/zig-lsp-kit
- duped 2y agoWhat does 'browser only' support mean?
- meiraleal 2y agoVSCode needs a server, LSP runs in that server.
- duped 2y agoIt doesn't actually, the "server" can (and in many cases does) run in the JS event loop. Do you want the protocol to specify that language servers are able to run in a browser? Because that's very outside the scope of the protocol, which doesn't constrain the client or server implementations. LSP doesn't define the transport layer between them, just that they should use JSON RPC.
- meiraleal 2y ago> It doesn't actually, the "server" can (and in many cases does) run in the JS event loop. Yeah I know it is possible, just not trivial. So for me it was more complex to implement it in the browser than just create a simpler tree-sitter.
- duped 2y agoLSP and tree-sitter solve different problems and aren't interchangeable, it sounds like you were trying to pound a square peg into a round hole. LSP doesn't (nor can it) specify anything that would make your life easier to use a language server in the browser. There are editors that provide clients and language servers written in JS, though.
- kristoff_it 2y agoI just tested SuperHTML with GitHub Codespaces and it works https://twitter.com/croloris/status/1835701531200418122 https://twitter.com/croloris/status/1835701531200418122
- catlifeonmars 2y agoThis is totally pedantic, but LSP stands for “Language Server Protocol”. I think they mean LS (or Language Server)
- aaronbrethorst 2y agoThat's a clarification that you can take all the way to the bank—or at least to the AT machine.
- kreetx 2y agoYup, much like pull request vs merge request - the "wrong" one is here to stay.
- imjonse 2y agoOn gitlab at least it is merge request, but the platform using the "wrong" term is more popular.
- hyperbrainer 2y agoIs it really "wrong" if everybody understands what you mean using the term?
- ffsm8 2y agoAnother instance of this are DNS - which is pretty much always called domain name server server (DNS server) Though you can at least have an argument there, as the first server could theoretically be the process that's running on the hardware/virtual server (which would be the second server).
- throw10920 2y ago> The best feature of SuperHTML is that it is based on the official HTML living specification, but it deviates from it when it makes sense to do so. I think this is actually a decent design decision. Unlike in networking, where application of Postel's Law (specifically, the "be liberal in what you accept" part) can lead to implementation details becoming de-facto part of the spec, SuperHTML isn't outputting data to another system for further processing - it's giving information (validation errors) to a human for their own use, and if this information leads to humans writing HTML that's a more-robust strict subset of the formal HTML spec, all the better.
- notpushkin 2y ago> a more-robust strict subset of the formal HTML spec I still think we’d be better off just using XHTML. There are some practical problems with XHTML5 (e.g. there’s no named entities support for some reason), but the syntax makes sense at least.
- zarzavat 2y agoThat was tried 20 years ago and it turns out that humans are not good at writing XML. XML makes sense if you are authoring HTML in an editor. However, this is not how most HTML is actually produced. It's mostly produced by templating engines. This means that you can't validate the XHTML during development because it's being generated on the fly. You only find out that it's valid in testing or production, perhaps only for a subset of users in certain situations. With HTML this is OK because there is error recovery. For XHTML you get literal downtime because the entire page shows a WSOD in the worst case. Yes XHTML is okay as an internal tool, if for some reason your pipeline depends on parsing your own HTML then switching to XHTML internally could be a win. Just don't ship XHTML to browsers.
- bazoom42 2y agoSurely a template engine would be able to produce valid (X)HTML? Strict XHTML failed on the web because older browsers could not show it at all (since it used a different mime type) so nobody sane would use it. The problem wasn’t the strictness per se, the problem was how it was introduced without concern for backwards compatibility. JavaScript is strict in the sense any syntax error will terminate execution. This seems to work fine because there is an incentive to make the syntax valid. If XHTML was introduced in a backwards compatible way but new features (like canvas) only worked in strict mode, I’m sure it would have caught on. The incentives just have to be there.
- chrismorgan 2y ago> My favorite example is <li>item<li>, which is unfortunately both valid HTML and also an obvious typo. If it’s <li>item<li> and then the next line is <li>item</li>, it’s an obvious error, but you certainly need to discriminate in that sort of way. Because if your thing goes complaining when I write <li>item<li>item, I’ll be upset. (In practice, it would almost always be <li>item and then a next line <li>item; when doing things like horizontal menus, you used to use `display: inline-block` and need to avoid whitespace between list items, so you would find <li>item<li>item in serious code, but now you’d use flex and so whitespace between items gets vanished, so you can write it on multiple lines without it affecting the actual layout.)
- kristoff_it 2y ago> Because if your thing goes complaining when I write <li>item<li>item, I’ll be upset. It does, sorry. No optional tags period. Giving whitespace more semantic value than what it already has is a dangerous game. I already use whitespace to let the user "talk" with the autoformater and from my perspective that already spends all my whitespace (and weirdness) budget.
- alexpetros 2y agoFirst of all, this is awesome work and it's wonderful to see people taking writing plain HTML seriously, because, as you identified, the existing tooling is completely insufficient. That having been said, I think the reason people largely don't write plain HTML is because the affordances that HTML makes for writeability (like omitting closing tags on <li>, <p>, etc) are frowned upon. If you're going to make all this effort to understand the HTML in the document, it's kind of a bummer to not make it actually understand some core aspects of HTML, aspects that are especially useful when writing it directly instead of treating HTML as a compile target.
- knallfrosch 2y ago> Because if your thing goes complaining when I write <li>item<li>item, I’ll be upset. And I'll be upset in the code review if you write HTML like that.
- tstusr20190823 2y agoMy html is: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> </html> Why it warns html_elements_cant_self_close on meta tag?
- someothherguyy 2y agoIt is a void element https://html.spec.whatwg.org/multipage/syntax.html#void-elements https://html.spec.whatwg.org/multipage/syntax.html#void-elem...
- thedumbname 2y ago> Void elements only have a start tag; end tags must not be specified for void elements So?
- someothherguyy 2y agoIts valid HTML, but > On void elements, it does not mark the start tag as self-closing but instead is unnecessary and has no effect of any kind. For such void elements, it should be used only with caution — especially since, if directly preceded by an unquoted attribute value, it becomes part of the attribute value rather than being discarded by the parser.
- kristoff_it 2y agoThe "<div/>" syntax is short for "<div></div>" in XHTML lingo. In HTML void tags only have a start tag and don't close at all. <meta foo bar> <img bar baz> And so forth. There's a hardcoded list of void tags in HTML. Horribly dumb design idea if you ask me, but it is what it is.
- tstusr20190823 2y agoOK, so it should not warn with XHTML doctype? But it does too
- 2y ago
- deleted 2y ago[deleted]
- tuyiown 2y ago<li>item<li> Is not valid HTML, it's merely valid grammar syntax for a loose parser. From the MDN: > Permitted parents: An <ul>, <ol>, or <menu> element. Though not a conforming usage, the obsolete <dir> can also be a parent. I do like adding restrictions on confusing patterns with no known legitimate use cases or better alternatives.
- kristoff_it 2y agoThe example omits the context around those <li>s but you can assume they're inside a <ul>. That's semantically valid html because </li> can be omitted. But despite being valid and unambiguous for the parser, it can still lead to confusing problems for unsuspecting developers: <ul> <li>item1</li> <li>item2<li> <script id="this-script"> let ul = document.getElementById("this-script").parentElement; console.log(ul.tagName); // prints "LI" // *confused screams by the developer* </script> </ul>
- tuyiown 2y agoOk, that's a specific rule for <li>, among others for closing tags, that was ignorance on my part, I overlooked the "sometimes" in the "It's valid HTML because the spec allows you to omit closing tags sometimes" comment.
- dmsnell 2y ago> Is not valid HTML, it's merely valid grammar syntax for a loose parser. It's an incredible journey writing a spec-compliant HTML parser. One of the things that stands out from the very first steps are that the "loose parser" is kind of a myth. Parsing HTML is fully-specified. The syntax is full of surprises with their own legacy, but every spec-compliant parser will produce the same result from the same input. HTML is, in a sense, a shorthand notation for a DOM tree - it is not the tree itself. The term "invalid HTML" also winds up fairly meaningless, as HTML error are mainly there as warnings for HTML validators, but are unnecessary for general parsing and rendering. And these are things we can't easily say about XML parsers. There are certain errors from which XML processors are allowed to recovery, but which ones those are depends on which parser is run. --- > I do like adding restrictions on confusing patterns with no known legitimate use cases or better alternatives. HTML was based loosely on SGML, a language designed to encode structure in a way that humans could easily type. Particular care was made in SGML to allow syntax "minimizations" (omitted tags, for example), so that humans would overcome the effort to encode the required structure. It was noted in the spec that if people had to type every single tag they would likely give up. They did. But SGML also had well-specified content models in the DTD, formalizing features like optional tags, short tags, tags derived from content templates, default attribute values. Any compliant SGML parser could reconstruct the missing syntax by parsing against that DTD. HTML missed out on this and effectively the DTD was externalized in the browsers. The effort was made to produce a proper SGML DTD for HTML, but it was too late. Perhaps if there had been widely-available SGML spec and parsers at the time HTML was created the story would be different. Needless to say, these patterns are the result of formal systems taking human factors into their designs. XML came later as a way to make software parsers easier, largely abandoning the human factors and use-case of people writing SGML/HTML/XML in a text editor. SGML is still rather fun to write and many of these minimization features are far more ergonomic than they might seem at first. If you have a parser that properly understands them, they are basically just convenient macros for writing XML.
- Nelkins 2y agoDo you think this can be applied to HTML fragments? I think it's relatively common (or at least not uncommon) to embed HTML in another language as a string. Would be cool to get LSP functionality in those strings. I'm thinking specifically with something like this: https://github.com/alfonsogarciacaro/vscode-template-fsharp-highlight https://github.com/alfonsogarciacaro/vscode-template-fsharp-...
- Animats 2y agoDid they just re-invent Dreamweaver's editor?
- wonger_ 2y agoI'm excited for the autoformatting! Other HTML prettifiers are not good enough for me.