5 ms·
Ex-browser dev here. Please don't ship invalid HTML like this. Web standards specify how to render valid HTML in a standard way, and browsers have become bette
by w0mbat 6y ago
Ex-browser dev here. Please don't ship invalid HTML like this.
Web standards specify how to render valid HTML in a standard way, and browsers have become better and better at that over the years.
Invalid HTML destroys all that. Each browser will have to guess how to repair and fill in the blanks on malformed incomplete tag soup, and there is no one right way to do that. Browser X will make different guesses from Browser Y, and the next version of each will be different too.
Please just write actual HTML that is valid and your website will render much more consistently and reliably across shifting platforms, browsers and versions. It is not the size of HTML that slows the web down anyway.
- Zarel 6y agoAs of HTML5, web standards specify how to render invalid HTML as well, precisely to avoid this problem: https://html.spec.whatwg.org/multipage/parsing.html#parse-errors https://html.spec.whatwg.org/multipage/parsing.html#parse-er... In addition, most of the suggestions in the link (leaving off <html> and <head>, leaving off quotes for attributes, not closing <p>) are valid HTML in the first place.
- lhorie 6y agoTo add to this, one of suggestions in the article was to drop the doctype. While that puts the browser in tag soup parsing mode rather than html5, in practice browsers don't actually start diverging much in behavior until you get to truly messed up markup, like malformed tags. So even the more egregious suggestions here are probably still fine, provided that you have equally egregious CSS hacks to get around ancient box model quirks
- oefrha 6y agoTrue, and the suggestions in the article are pretty tame. But let’s set aside the article and talk about invalid HTML for a minute. I write scrapers a lot (not the irresponsible kind and never for monetary gains) and invalid HTML, while technically parseable and to spec, are often a pain in the ass. You have to bring in a full blown HTML5 parser, and they could be way slower (e.g. lxml.html vs html5lib). Depending on your language of choice there might not even be a to-spec HTML5 parser available. So, just close your damn tags (except self-closing ones), and close them in order, it’s not hard, the size increase is minimal, it will help with your own sanity and people will thank you for it.
- thinkloop 6y agoFor many sites this is an endorsement not to close tags.
- oefrha 6y agoIt’s an annoyance, not a meaningful hindrance.
- goto11 6y agoThere are a lot more web sites than scrapers though. Why push the work to all the web sites in the world instead of fixing your scraper? But in any case, you are never getting all the web sites in the world to validate, so what does it matter if you can get some of them to validate? You still have to handle the rest.
- oefrha 6y ago"it will help with your own sanity" Writing unclosed/out-of-order tag soup is extra work, not less work. What a weird hill to die on.
- goto11 6y agoWhat people don't get about the web is that there is a significant (and growing) number of pages written by people who are now dead. So all the well-intentioned ideas about "cleaning up" html - it's not going to happen. It's not a question about being pro or con closing tags or which is "best". The question is about the billions of pages which already exist and is not going to change. If you write a web scraper, that is just the reality you work with.
- oefrha 6y agoWhere did I call for people to clean up existing HTML? Nowhere. Did I ask people to not follow bad advice (intentionally not closing non-self-closing tags just for the hell of it)? Absolutely.
- Izkata 6y ago> As of HTML5, web standards specify how to render invalid HTML as well Ah, but this post is (seemingly accidentally) recommending you use quirks mode instead of HTML5: > * <!DOCTYPE html> is required by the HTML spec (but not the XHTML spec). Most of browsers will not care if you omit it. It was created for backwards compatibility with prior HTML versions. You can omit it in minimal documents.
- kiwidrew 6y agoAll of the examples provided are valid HTML5, and all HTML5-compliant user agents are required to implement the HTML5 parsing algorithm, which constructs a DOM tree in a deterministic manner for any given input. HTML5 specifically permits tag omission in many cases [1], including but not limited to both the opening and closing <html>, <head>, and <body> tags. The parsing algorithm provided in the standard generates a DOM tree that includes the omitted tags in the correct place(s). For example, the following document is valid HTML5: <!DOCTYPE html> <title>Hello</title> <p>Welcome to this example. [1] https://www.w3.org/TR/html52/syntax.html#optional-tags https://www.w3.org/TR/html52/syntax.html#optional-tags
- deleted 6y ago[deleted]
- SahAssar 6y agoThe original post specifically mentions dropping the doctype which is not valid. It also implies that you can drop all (or at least many) closing tags without saying that some are optional and some are not.
- johncmouser 6y agowould you need the doctype if the user-agent said it was html5 compliant?
- SahAssar 6y agoThe doctype is required for it to be a valid html5 document.
- recursive 6y agoYes. Especially then. Without a doctype, it's not html5.
- pzmarzly 6y agoWithout doctype, browsers will render your page in quirks mode, leaving you scratching your head because of margins being set out of nowhere. https://developer.mozilla.org/en-US/docs/Mozilla/Mozilla_quirks_mode_behavior https://developer.mozilla.org/en-US/docs/Mozilla/Mozilla_qui...
- chrismorgan 6y agoI’m curious, when were you last a browser dev? Because as noted by other responders, what you’ve said is just wrong (quite apart from the fact that this is all valid HTML, except for removing the doctype), and has been for quite a few years now. Fifteen years ago, yeah, your advice was sound, but it’s been completely safe to depend on an HTML5 parser being in use for most of the last decade. (I’m not trying to be hostile, just seeking that misinformation be corrected, and you’ve made a claim that doesn’t make sense to me unless it’s from quite some years ago.)
- goto11 6y agoNothing of this is invalid HTML. It has always been part of the HTML DTD that certain tags are optional and certain tags does not need to be explicitly closed. This is a carry-over from SGML. The quoting rules for attributes are also well-defined, there is no guesswork or error-recovery involved. Now it this was about XHTML you would have a point. People often mix-up optional syntax and error recovery. You can leave out the <head>-tags, the parser can infer it - this is well-defined by the DTD. It is not an example of error recovery.