6 ms·
Idiosyncrasies of the HTML Parser
- xupybd 5y agoI didn't think there was just one HTML Parser?
- chrismorgan 5y agoHTML parsing is exhaustively defined in terms of state machines (with the two main ones being a tokeniser and a tree builder), so that all implementations of it will produce identical results for each given input. It is therefore reasonable to speak of “the HTML parser”. (Actually, this isn’t true in the presence of scripting because of document.write(), which allows you to feed arbitrary data to the parser during parsing. But that’s mostly a technicality.)
- SavantIdiot 5y agoI know this isn't about the content, but that is a damn fine layout. +1. Would steal.
- james-bcn 5y agoAgreed. It's worth spending a little time studying the source, it's extremely neat. There are definitely a few techniques I'm going to steal from this.
- tannhaeuser 5y agoJust skimming through the intro chapter I already spotted inaccuracies regarding SGML. For example: > The following are equivalent: <link rel="stylesheet" href="style.css" /> [and] <link rel="stylesheet" href="style.css">> No they're not. The author should study the 1998 WebSGML adaptations to ISO 8879 (the SGML spec), and in particular the NETENABL IMMEDNET feature. I mean, I welcome discussion about HTML parsing; just not the kind of narrative painting HTML5 as some kind of academic or community consensus when it's very clear that HTML5 as it is is a dead end, and a regression due to its monolithic nature tying a parsing spec to a concrete markup language, unlike SGML and XML.
- edoceo 5y agoLoads of changes have happened since 1998. And both of those elements render and do the exact same thing - or what am I (a non-SGML expert) missing? Edit: I read this but it didn't add much clarity -- https://medium.com/hackernoon/to-close-or-not-to-close-4365d24853ff https://medium.com/hackernoon/to-close-or-not-to-close-4365d...
- innocenat 5y agoThe GP quote was in SGML History section, not HTML section. So yes, he was talking about 1998.
- tannhaeuser 5y agoThe context I cited is about parsing the XMLish "/>" empty-element tag which is not treated in the way the author thinks. WebSGML (aka ISO 8879 Corr 2/Annex K) has the option to parse this XML-like rather than under traditional SGML rules; which is no surprise since W3C XML and ISO (Web)SGML were published in a coordinated way and authored by the same people. Loads of changes may have happened to CSS and JS, but HTML has remained largely the same, save for the addition of a couple elements (section, article, main, header/footer added by Ian Hickson early on without any input by other parties) and import of SVG (the major contribution of XML) directly into HTML. Which is exactly the problem: HTML hasn't advanced for eg mobile, but everything else around it has turned into absurd complexity so we can continue to pretend we're writing 1990's casual academic content (the original use case for HTML the markup language). When, of all the languages in the web stack, markup is the one thing that has typing, schemas, and a schema evolution story.
- sanxiyn 5y agoI agree HTML5 is very unsatisfactory in tying parsing to markup, but it has an extremely important and redeeming virtue of parsing existing HTML documents in the wild. I agree no one should base a new markup on HTML5, but eh, no one is? You seem to be attacking a strawman there.
- masswerk 5y ago> The author should study the 1998 WebSGML adaptations to ISO 8879 (the SGML spec) Mind that the author is literally one of the designers and maintainers of the HTML5 parsing specs. From the section "About the author": > He contributed to the design of some aspects of the HTML parser specification […] and is currently an editor of the WHATWG HTML standard and the WHATWG Quirks Mode standard.
- _the_inflator 5y ago“Parsers are easy!” - sorry, couldn’t resist. ;) Have a nice day folks!
- dmitriid 5y agoIt is easy. Unless you have an ambiguous system which: - has to produce valid output in the presence of invalid input - apparently has to take external scripting such as `document.write` and `document.innerHtml` into account - has decades of legacy and conflicting standards thrown in And, undoubtedly, it's still the easiest part of a browser.
- sanxiyn 5y agoIt was NOT the easiest part before HTML5. Thanks to HTML5 effort, it now is. This is the great accomplishment of HTML5.
- dmitriid 5y agoCompared to layout, paint, keeping track of live objects in the DOM etc. it was probably still the easiest part :) HTML5 did bring more order to the chaos, but I doubt it reduced it that much.
- sanxiyn 5y agoI think HTML used to be harder than HTTP. I think now it is easier.
- benibela 5y agoIt became harder In the past you could write a straightforward parser for the pages you had. When it did not work on some other pages, you could say, that page has invalid HTML, it is not expected to be parseable. Now you have to spend a lot of time to implement everything in HTML5
- goto11 5y ago
- jokoon 5y agoExplanation why HTML is not that great from a CS professor https://www.youtube.com/watch?v=RH0o-QjnwDg https://www.youtube.com/watch?v=RH0o-QjnwDg https://www.youtube.com/watch?v=Q4dYwEyjZcY https://www.youtube.com/watch?v=Q4dYwEyjZcY
- trhway 5y agohtml parser isn't that bad, and the interesting part is only adaptive "repair" of the incorrect html as throwing a error and refusing to parse further like say a programming language compiler would do isn't an option for html. Where things get idiosyncratic is the html renderer :)
- captainmuon 5y agoThe time where I had to worry about the HTML parser is mostly gone, though I don't know if it is because the situation got better with HTML5 or if I am just used to all the warts. The thing I dislike most is the treatment of self-closing tags like <br />. In HTML5, the correct form is <br> but you can add the slash and it is ignored as syntactic sugar. But you cannot write <div id="mydiv" />. In XHTML, this was a shorthand for <div id="mydiv"></div>. It's really ugly that you have to write the closing tag in this case, and for seemingly no reason. https://stackoverflow.com/questions/3558119/are-non-void-self-closing-tags-valid-in-html5 https://stackoverflow.com/questions/3558119/are-non-void-sel...
- masklinn 5y ago> But you cannot write <div id="mydiv" />. Sure you can. And exactly as with <br/> the / is ignored. Ignored means exactly that. HTML does not have xml-style self-closing tags at all. > for seemingly no reason. The reason is pretty simple: there were no self/closing tags in SGML, so there were no self-closing tags in the mangled mess browsers made of it. The goal of the HTML5 parser effort was to create an interoperable and backwards compatible parsing algorithm. Adding self-closing tags to the langage would have tin counter to that goal and would definitely have broken existing code unnecessarily, even though it might have been convenient in some cases.
- deleted 5y ago[deleted]
- saurik 5y ago>> But you cannot write <div id="mydiv" />. > Sure you can. And exactly as with <br/> the / is ignored. Obviously, the phrase "you cannot write" has the context "as a shorthand for self-closing the tag", and doesn't deserve some pedantic "of course you can do that, it just doesn't do what you hoped it would" response :/. To really be pedantic about it: SGML actually had a feature similar to this feature and HTML specified it; however, the syntax "<br/" was a valid way to write that tag (technically an open tag, but due to the DTD it would auto close) and the ">" after would get rendered... not ignored (and in fact might even cause the document to fail if that > was in a disallowed context of a strict document). https://jkorpela.fi/html/empty.html https://jkorpela.fi/html/empty.html Ironically, the W3's comment on this is to discourage its usage "until widely deployed", as they have essentially always specified stuff no one implemented correctly (lol). https://www.w3.org/MarkUp/html3/HTMLandSGML.html https://www.w3.org/MarkUp/html3/HTMLandSGML.html
- chrismorgan 5y agoIn the Introduction: > IE6 had an interesting HTML parser. It did not necessarily produce a tree; rather it would produce a graph, to more faithfully preserve author intent. Ill-formed markup, e.g., <em><p></em></p>, would result in an ill-formed DOM. This could cause scripts to go into infinite loops by just trying to iterate over the DOM. Huh, didn’t know that. It’s the sort of thing that makes me wonder, “hmm, was that what was happening that time all those years ago?” > The problem is I now have to determine which of these four options to make the other three browsers implement (that is, which do I put in the spec). What do you think is the most likely to be accepted by the others? As a reminder, the options are incestual elements that can be their own uncles, elements who have secret lives in the rendering engine, elements that change their mind about who their parents are half-way through their childhood, and quantum elements whose parents change depending on whether you observe their birth or not. That whole section is great reading, showing just a little bit of the madness that preceded the specification of HTML parsing, and how monumental a job it was.
- itsbits 5y ago> Huh, didn’t know that. It’s the sort of thing that makes me wonder, “hmm, was that what was happening that time all those years ago?” I think it was always a graph. I remember reading DOM implementation years ago, where a node will have siblings references as well making it graph DS.
- chrismorgan 5y agoSure, you’ve always been able to access siblings directly from nodes, rather than having to go up and down again, but that’s just a shortcut rather than being an inherent part of the logical data structure, since nodes don’t own their siblings, but rather their siblings are computed from their parent’s children. This here is talking about the DOM actually producing a non-tree structure, where one text node is a child of multiple elements.
- itsbits 5y agocheck this implementation https://blogs.windows.com/msedgedev/2017/04/19/modernizing-dom-tree-microsoft-edge/ https://blogs.windows.com/msedgedev/2017/04/19/modernizing-d... graph doesn't mean multiple parents all the time. if you have references of other nodes in current node data(not just as children) as long as you can travel across Data structure, enough to say it as graph structure.
- deleted 5y ago[deleted]
- pwdisswordfish8 5y agohttps://htmlparser.info/security/ https://htmlparser.info/security/ Just what I was looking for.