htmlfaq.rst
This file
Path
docs/htmlfaq.rst
207 lines · 9.5 KiB
Last changed by
Merge pull request #3175 from singingwolfboy/fix-rst-line-too-longDavid Baumgoldbefore adoption819368a
History of this fileTake it
1HTML/XHTML FAQ2==============34The Flask documentation and example applications are using HTML5. You5may notice that in many situations, when end tags are optional they are6not used, so that the HTML is cleaner and faster to load. Because there7is much confusion about HTML and XHTML among developers, this document tries8to answer some of the major questions.91011History of XHTML12----------------1314For a while, it appeared that HTML was about to be replaced by XHTML.15However, barely any websites on the Internet are actual XHTML (which is16HTML processed using XML rules). There are a couple of major reasons17why this is the case. One of them is Internet Explorer's lack of proper18XHTML support. The XHTML spec states that XHTML must be served with the MIME19type :mimetype:`application/xhtml+xml`, but Internet Explorer refuses20to read files with that MIME type.21While it is relatively easy to configure Web servers to serve XHTML properly,22few people do. This is likely because properly using XHTML can be quite23painful.2425One of the most important causes of pain is XML's draconian (strict and26ruthless) error handling. When an XML parsing error is encountered,27the browser is supposed to show the user an ugly error message, instead28of attempting to recover from the error and display what it can. Most of29the (X)HTML generation on the web is based on non-XML template engines30(such as Jinja, the one used in Flask) which do not protect you from31accidentally creating invalid XHTML. There are XML based template engines,32such as Kid and the popular Genshi, but they often come with a larger33runtime overhead and are not as straightforward to use because they have34to obey XML rules.3536The majority of users, however, assumed they were properly using XHTML.37They wrote an XHTML doctype at the top of the document and self-closed all38the necessary tags (``<br>`` becomes ``<br/>`` or ``<br></br>`` in XHTML).39However, even if the document properly validates as XHTML, what really40determines XHTML/HTML processing in browsers is the MIME type, which as41said before is often not set properly. So the valid XHTML was being treated42as invalid HTML.4344XHTML also changed the way JavaScript is used. To properly work with XHTML,45programmers have to use the namespaced DOM interface with the XHTML46namespace to query for HTML elements.4748History of HTML549----------------5051Development of the HTML5 specification was started in 2004 under the name52"Web Applications 1.0" by the Web Hypertext Application Technology Working53Group, or WHATWG (which was formed by the major browser vendors Apple,54Mozilla, and Opera) with the goal of writing a new and improved HTML55specification, based on existing browser behavior instead of unrealistic56and backwards-incompatible specifications.5758For example, in HTML4 ``<title/Hello/`` theoretically parses exactly the59same as ``<title>Hello</title>``. However, since people were using60XHTML-like tags along the lines of ``<link />``, browser vendors implemented61the XHTML syntax over the syntax defined by the specification.6263In 2007, the specification was adopted as the basis of a new HTML64specification under the umbrella of the W3C, known as HTML5. Currently,65it appears that XHTML is losing traction, as the XHTML 2 working group has66been disbanded and HTML5 is being implemented by all major browser vendors.6768HTML versus XHTML69-----------------7071The following table gives you a quick overview of features available in72HTML 4.01, XHTML 1.1 and HTML5. (XHTML 1.0 is not included, as it was73superseded by XHTML 1.1 and the barely-used XHTML5.)7475.. tabularcolumns:: |p{9cm}|p{2cm}|p{2cm}|p{2cm}|7677+-----------------------------------------+----------+----------+----------+78| | HTML4.01 | XHTML1.1 | HTML5 |79+=========================================+==========+==========+==========+80| ``<tag/value/`` == ``<tag>value</tag>`` | |Y| [1]_ | |N| | |N| |81+-----------------------------------------+----------+----------+----------+82| ``<br/>`` supported | |N| | |Y| | |Y| [2]_ |83+-----------------------------------------+----------+----------+----------+84| ``<script/>`` supported | |N| | |Y| | |N| |85+-----------------------------------------+----------+----------+----------+86| should be served as `text/html` | |Y| | |N| [3]_ | |Y| |87+-----------------------------------------+----------+----------+----------+88| should be served as | |N| | |Y| | |N| |89| `application/xhtml+xml` | | | |90+-----------------------------------------+----------+----------+----------+91| strict error handling | |N| | |Y| | |N| |92+-----------------------------------------+----------+----------+----------+93| inline SVG | |N| | |Y| | |Y| |94+-----------------------------------------+----------+----------+----------+95| inline MathML | |N| | |Y| | |Y| |96+-----------------------------------------+----------+----------+----------+97| ``<video>`` tag | |N| | |N| | |Y| |98+-----------------------------------------+----------+----------+----------+99| ``<audio>`` tag | |N| | |N| | |Y| |100+-----------------------------------------+----------+----------+----------+101| New semantic tags like ``<article>`` | |N| | |N| | |Y| |102+-----------------------------------------+----------+----------+----------+103104.. [1] This is an obscure feature inherited from SGML. It is usually not105 supported by browsers, for reasons detailed above.106.. [2] This is for compatibility with server code that generates XHTML for107 tags such as ``<br>``. It should not be used in new code.108.. [3] XHTML 1.0 is the last XHTML standard that allows to be served109 as `text/html` for backwards compatibility reasons.110111.. |Y| image:: _static/yes.png112 :alt: Yes113.. |N| image:: _static/no.png114 :alt: No115116What does "strict" mean?117------------------------118119HTML5 has strictly defined parsing rules, but it also specifies exactly120how a browser should react to parsing errors - unlike XHTML, which simply121states parsing should abort. Some people are confused by apparently122invalid syntax that still generates the expected results (for example,123missing end tags or unquoted attribute values).124125Some of these work because of the lenient error handling most browsers use126when they encounter a markup error, others are actually specified. The127following constructs are optional in HTML5 by standard, but have to be128supported by browsers:129130- Wrapping the document in an ``<html>`` tag131- Wrapping header elements in ``<head>`` or the body elements in132 ``<body>``133- Closing the ``<p>``, ``<li>``, ``<dt>``, ``<dd>``, ``<tr>``,134 ``<td>``, ``<th>``, ``<tbody>``, ``<thead>``, or ``<tfoot>`` tags.135- Quoting attributes, so long as they contain no whitespace or136 special characters (like ``<``, ``>``, ``'``, or ``"``).137- Requiring boolean attributes to have a value.138139This means the following page in HTML5 is perfectly valid:140141.. sourcecode:: html142143 <!doctype html>144 <title>Hello HTML5</title>145 <div class=header>146 <h1>Hello HTML5</h1>147 <p class=tagline>HTML5 is awesome148 </div>149 <ul class=nav>150 <li><a href=/index>Index</a>151 <li><a href=/downloads>Downloads</a>152 <li><a href=/about>About</a>153 </ul>154 <div class=body>155 <h2>HTML5 is probably the future</h2>156 <p>157 There might be some other things around but in terms of158 browser vendor support, HTML5 is hard to beat.159 <dl>160 <dt>Key 1161 <dd>Value 1162 <dt>Key 2163 <dd>Value 2164 </dl>165 </div>166167168New technologies in HTML5169-------------------------170171HTML5 adds many new features that make Web applications easier to write172and to use.173174- The ``<audio>`` and ``<video>`` tags provide a way to embed audio and175 video without complicated add-ons like QuickTime or Flash.176- Semantic elements like ``<article>``, ``<header>``, ``<nav>``, and177 ``<time>`` that make content easier to understand.178- The ``<canvas>`` tag, which supports a powerful drawing API, reducing179 the need for server-generated images to present data graphically.180- New form control types like ``<input type="date">`` that allow user181 agents to make entering and validating values easier.182- Advanced JavaScript APIs like Web Storage, Web Workers, Web Sockets,183 geolocation, and offline applications.184185Many other features have been added, as well. A good guide to new features186in HTML5 is Mark Pilgrim's soon-to-be-published book, `Dive Into HTML5`_.187Not all of them are supported in browsers yet, however, so use caution.188189.. _Dive Into HTML5: http://diveintohtml5.info/table-of-contents.html190191What should be used?192--------------------193194Currently, the answer is HTML5. There are very few reasons to use XHTML195considering the latest developments in Web browsers. To summarize the196reasons given above:197198- Internet Explorer (which, sadly, currently leads in market share)199 has poor support for XHTML.200- Many JavaScript libraries also do not support XHTML, due to the more201 complicated namespacing API it requires.202- HTML5 adds several new features, including semantic tags and the203 long-awaited ``<audio>`` and ``<video>`` tags.204- It has the support of most browser vendors behind it.205- It is much easier to write, and more compact.206207For most applications, it is undoubtedly better to use HTML5 than XHTML.