What is the internal mechanism that browsers use to process/understand HTML? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
开发者_运维知识库 Improve this questionHow do browsers understand HTML?
What is the actual processing that takes place internally so that the browser renders HTML in the proper viewable way?
It depends on the particular browser, but the general procedure goes something like this:
- Read the HTML and parse it into a DOM tree.
- Load linked resources (stylesheets, scripts, images, media)
- Calculate the page layout (positions, sizes, colors, fonts, etc.)
- Render the page
In modern browsers, these operations run partly in parallel, making things much more complicated than they seem.
If you want to know more details, you could look at the source code - at least Firefox (and other Gecko-based browsers) and WebKit (the basis of Google Chrome and Safari) are Open Source.
This is a larger question than it appears when first asked.
A lot goes on behind the scenes. The HTML is parsed, scripts are located, resources are loaded, some of those need to be parsed. Style sheets add to the fun. Scripts can create more work by rewriting the document as it is loaded. Somewhere along the way, the obvious security concerns must be addressed. And with every step, you have to assume that every page is a potential attempt to subvert the whole computer and defend against every attack you can think of as well as every attack you can't think of today.
And that is nowhere near a comprehensive list.
A good example with full source code available is Gecko, the rendering engine behind Mozilla Firefox. It is well maintained, fast, quite standards compliant, and about as secure as 1000s of code reviewers and attackers can make it.
It’s really not my field, but with any language, the computer has to parse it, and build an internal representation of it.
In versions of HTML before 5, each browser decided how to parse HTML itself. Starting with HTML5, the HTML spec actually defines how to parse HTML.
Not all browsers implement this yet (possibly none do), but the WebKit team at least is working on it, and looking at their work, or the HTML5 spec, might be a good place to start if you’re learning about parsing HTML.
- http://webkit.org/blog/1273/the-html5-parsing-algorithm/
精彩评论