You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
13 KiB

4 years ago
  1. html5lib
  2. ========
  3. .. image:: https://travis-ci.org/html5lib/html5lib-python.png?branch=master
  4. :target: https://travis-ci.org/html5lib/html5lib-python
  5. html5lib is a pure-python library for parsing HTML. It is designed to
  6. conform to the WHATWG HTML specification, as is implemented by all major
  7. web browsers.
  8. Usage
  9. -----
  10. Simple usage follows this pattern:
  11. .. code-block:: python
  12. import html5lib
  13. with open("mydocument.html", "rb") as f:
  14. document = html5lib.parse(f)
  15. or:
  16. .. code-block:: python
  17. import html5lib
  18. document = html5lib.parse("<p>Hello World!")
  19. By default, the ``document`` will be an ``xml.etree`` element instance.
  20. Whenever possible, html5lib chooses the accelerated ``ElementTree``
  21. implementation (i.e. ``xml.etree.cElementTree`` on Python 2.x).
  22. Two other tree types are supported: ``xml.dom.minidom`` and
  23. ``lxml.etree``. To use an alternative format, specify the name of
  24. a treebuilder:
  25. .. code-block:: python
  26. import html5lib
  27. with open("mydocument.html", "rb") as f:
  28. lxml_etree_document = html5lib.parse(f, treebuilder="lxml")
  29. When using with ``urllib2`` (Python 2), the charset from HTTP should be
  30. pass into html5lib as follows:
  31. .. code-block:: python
  32. from contextlib import closing
  33. from urllib2 import urlopen
  34. import html5lib
  35. with closing(urlopen("http://example.com/")) as f:
  36. document = html5lib.parse(f, transport_encoding=f.info().getparam("charset"))
  37. When using with ``urllib.request`` (Python 3), the charset from HTTP
  38. should be pass into html5lib as follows:
  39. .. code-block:: python
  40. from urllib.request import urlopen
  41. import html5lib
  42. with urlopen("http://example.com/") as f:
  43. document = html5lib.parse(f, transport_encoding=f.info().get_content_charset())
  44. To have more control over the parser, create a parser object explicitly.
  45. For instance, to make the parser raise exceptions on parse errors, use:
  46. .. code-block:: python
  47. import html5lib
  48. with open("mydocument.html", "rb") as f:
  49. parser = html5lib.HTMLParser(strict=True)
  50. document = parser.parse(f)
  51. When you're instantiating parser objects explicitly, pass a treebuilder
  52. class as the ``tree`` keyword argument to use an alternative document
  53. format:
  54. .. code-block:: python
  55. import html5lib
  56. parser = html5lib.HTMLParser(tree=html5lib.getTreeBuilder("dom"))
  57. minidom_document = parser.parse("<p>Hello World!")
  58. More documentation is available at https://html5lib.readthedocs.io/.
  59. Installation
  60. ------------
  61. html5lib works on CPython 2.7+, CPython 3.3+ and PyPy. To install it,
  62. use:
  63. .. code-block:: bash
  64. $ pip install html5lib
  65. Optional Dependencies
  66. ---------------------
  67. The following third-party libraries may be used for additional
  68. functionality:
  69. - ``datrie`` can be used under CPython to improve parsing performance
  70. (though in almost all cases the improvement is marginal);
  71. - ``lxml`` is supported as a tree format (for both building and
  72. walking) under CPython (but *not* PyPy where it is known to cause
  73. segfaults);
  74. - ``genshi`` has a treewalker (but not builder); and
  75. - ``chardet`` can be used as a fallback when character encoding cannot
  76. be determined.
  77. Bugs
  78. ----
  79. Please report any bugs on the `issue tracker
  80. <https://github.com/html5lib/html5lib-python/issues>`_.
  81. Tests
  82. -----
  83. Unit tests require the ``pytest`` and ``mock`` libraries and can be
  84. run using the ``py.test`` command in the root directory.
  85. Test data are contained in a separate `html5lib-tests
  86. <https://github.com/html5lib/html5lib-tests>`_ repository and included
  87. as a submodule, thus for git checkouts they must be initialized::
  88. $ git submodule init
  89. $ git submodule update
  90. If you have all compatible Python implementations available on your
  91. system, you can run tests on all of them using the ``tox`` utility,
  92. which can be found on PyPI.
  93. Questions?
  94. ----------
  95. There's a mailing list available for support on Google Groups,
  96. `html5lib-discuss <http://groups.google.com/group/html5lib-discuss>`_,
  97. though you may get a quicker response asking on IRC in `#whatwg on
  98. irc.freenode.net <http://wiki.whatwg.org/wiki/IRC>`_.
  99. Change Log
  100. ----------
  101. 1.0.1
  102. ~~~~~
  103. Released on December 7, 2017
  104. Breaking changes:
  105. * Drop support for Python 2.6. (#330) (Thank you, Hugo, Will Kahn-Greene!)
  106. * Remove ``utils/spider.py`` (#353) (Thank you, Jon Dufresne!)
  107. Features:
  108. * Improve documentation. (#300, #307) (Thank you, Jon Dufresne, Tom Most,
  109. Will Kahn-Greene!)
  110. * Add iframe seamless boolean attribute. (Thank you, Ritwik Gupta!)
  111. * Add itemscope as a boolean attribute. (#194) (Thank you, Jonathan Vanasco!)
  112. * Support Python 3.6. (#333) (Thank you, Jon Dufresne!)
  113. * Add CI support for Windows using AppVeyor. (Thank you, John Vandenberg!)
  114. * Improve testing and CI and add code coverage (#323, #334), (Thank you, Jon
  115. Dufresne, John Vandenberg, Geoffrey Sneddon, Will Kahn-Greene!)
  116. * Semver-compliant version number.
  117. Bug fixes:
  118. * Add support for setuptools < 18.5 to support environment markers. (Thank you,
  119. John Vandenberg!)
  120. * Add explicit dependency for six >= 1.9. (Thank you, Eric Amorde!)
  121. * Fix regexes to work with Python 3.7 regex adjustments. (#318, #379) (Thank
  122. you, Benedikt Morbach, Ville Skyttä, Mark Vasilkov!)
  123. * Fix alphabeticalattributes filter namespace bug. (#324) (Thank you, Will
  124. Kahn-Greene!)
  125. * Include license file in generated wheel package. (#350) (Thank you, Jon
  126. Dufresne!)
  127. * Fix annotation-xml typo. (#339) (Thank you, Will Kahn-Greene!)
  128. * Allow uppercase hex chararcters in CSS colour check. (#377) (Thank you,
  129. Komal Dembla, Hugo!)
  130. 1.0
  131. ~~~
  132. Released and unreleased on December 7, 2017. Badly packaged release.
  133. 0.999999999/1.0b10
  134. ~~~~~~~~~~~~~~~~~~
  135. Released on July 15, 2016
  136. * Fix attribute order going to the tree builder to be document order
  137. instead of reverse document order(!).
  138. 0.99999999/1.0b9
  139. ~~~~~~~~~~~~~~~~
  140. Released on July 14, 2016
  141. * **Added ordereddict as a mandatory dependency on Python 2.6.**
  142. * Added ``lxml``, ``genshi``, ``datrie``, ``charade``, and ``all``
  143. extras that will do the right thing based on the specific
  144. interpreter implementation.
  145. * Now requires the ``mock`` package for the testsuite.
  146. * Cease supporting DATrie under PyPy.
  147. * **Remove PullDOM support, as this hasn't ever been properly
  148. tested, doesn't entirely work, and as far as I can tell is
  149. completely unused by anyone.**
  150. * Move testsuite to ``py.test``.
  151. * **Fix #124: move to webencodings for decoding the input byte stream;
  152. this makes html5lib compliant with the Encoding Standard, and
  153. introduces a required dependency on webencodings.**
  154. * **Cease supporting Python 3.2 (in both CPython and PyPy forms).**
  155. * **Fix comments containing double-dash with lxml 3.5 and above.**
  156. * **Use scripting disabled by default (as we don't implement
  157. scripting).**
  158. * **Fix #11, avoiding the XSS bug potentially caused by serializer
  159. allowing attribute values to be escaped out of in old browser versions,
  160. changing the quote_attr_values option on serializer to take one of
  161. three values, "always" (the old True value), "legacy" (the new option,
  162. and the new default), and "spec" (the old False value, and the old
  163. default).**
  164. * **Fix #72 by rewriting the sanitizer to apply only to treewalkers
  165. (instead of the tokenizer); as such, this will require amending all
  166. callers of it to use it via the treewalker API.**
  167. * **Drop support of charade, now that chardet is supported once more.**
  168. * **Replace the charset keyword argument on parse and related methods
  169. with a set of keyword arguments: override_encoding, transport_encoding,
  170. same_origin_parent_encoding, likely_encoding, and default_encoding.**
  171. * **Move filters._base, treebuilder._base, and treewalkers._base to .base
  172. to clarify their status as public.**
  173. * **Get rid of the sanitizer package. Merge sanitizer.sanitize into the
  174. sanitizer.htmlsanitizer module and move that to sanitizer. This means
  175. anyone who used sanitizer.sanitize or sanitizer.HTMLSanitizer needs no
  176. code changes.**
  177. * **Rename treewalkers.lxmletree to .etree_lxml and
  178. treewalkers.genshistream to .genshi to have a consistent API.**
  179. * Move a whole load of stuff (inputstream, ihatexml, trie, tokenizer,
  180. utils) to be underscore prefixed to clarify their status as private.
  181. 0.9999999/1.0b8
  182. ~~~~~~~~~~~~~~~
  183. Released on September 10, 2015
  184. * Fix #195: fix the sanitizer to drop broken URLs (it threw an
  185. exception between 0.9999 and 0.999999).
  186. 0.999999/1.0b7
  187. ~~~~~~~~~~~~~~
  188. Released on July 7, 2015
  189. * Fix #189: fix the sanitizer to allow relative URLs again (as it did
  190. prior to 0.9999/1.0b5).
  191. 0.99999/1.0b6
  192. ~~~~~~~~~~~~~
  193. Released on April 30, 2015
  194. * Fix #188: fix the sanitizer to not throw an exception when sanitizing
  195. bogus data URLs.
  196. 0.9999/1.0b5
  197. ~~~~~~~~~~~~
  198. Released on April 29, 2015
  199. * Fix #153: Sanitizer fails to treat some attributes as URLs. Despite how
  200. this sounds, this has no known security implications. No known version
  201. of IE (5.5 to current), Firefox (3 to current), Safari (6 to current),
  202. Chrome (1 to current), or Opera (12 to current) will run any script
  203. provided in these attributes.
  204. * Pass error message to the ParseError exception in strict parsing mode.
  205. * Allow data URIs in the sanitizer, with a whitelist of content-types.
  206. * Add support for Python implementations that don't support lone
  207. surrogates (read: Jython). Fixes #2.
  208. * Remove localization of error messages. This functionality was totally
  209. unused (and untested that everything was localizable), so we may as
  210. well follow numerous browsers in not supporting translating technical
  211. strings.
  212. * Expose treewalkers.pprint as a public API.
  213. * Add a documentEncoding property to HTML5Parser, fix #121.
  214. 0.999
  215. ~~~~~
  216. Released on December 23, 2013
  217. * Fix #127: add work-around for CPython issue #20007: .read(0) on
  218. http.client.HTTPResponse drops the rest of the content.
  219. * Fix #115: lxml treewalker can now deal with fragments containing, at
  220. their root level, text nodes with non-ASCII characters on Python 2.
  221. 0.99
  222. ~~~~
  223. Released on September 10, 2013
  224. * No library changes from 1.0b3; released as 0.99 as pip has changed
  225. behaviour from 1.4 to avoid installing pre-release versions per
  226. PEP 440.
  227. 1.0b3
  228. ~~~~~
  229. Released on July 24, 2013
  230. * Removed ``RecursiveTreeWalker`` from ``treewalkers._base``. Any
  231. implementation using it should be moved to
  232. ``NonRecursiveTreeWalker``, as everything bundled with html5lib has
  233. for years.
  234. * Fix #67 so that ``BufferedStream`` to correctly returns a bytes
  235. object, thereby fixing any case where html5lib is passed a
  236. non-seekable RawIOBase-like object.
  237. 1.0b2
  238. ~~~~~
  239. Released on June 27, 2013
  240. * Removed reordering of attributes within the serializer. There is now
  241. an ``alphabetical_attributes`` option which preserves the previous
  242. behaviour through a new filter. This allows attribute order to be
  243. preserved through html5lib if the tree builder preserves order.
  244. * Removed ``dom2sax`` from DOM treebuilders. It has been replaced by
  245. ``treeadapters.sax.to_sax`` which is generic and supports any
  246. treewalker; it also resolves all known bugs with ``dom2sax``.
  247. * Fix treewalker assertions on hitting bytes strings on
  248. Python 2. Previous to 1.0b1, treewalkers coped with mixed
  249. bytes/unicode data on Python 2; this reintroduces this prior
  250. behaviour on Python 2. Behaviour is unchanged on Python 3.
  251. 1.0b1
  252. ~~~~~
  253. Released on May 17, 2013
  254. * Implementation updated to implement the `HTML specification
  255. <http://www.whatwg.org/specs/web-apps/current-work/>`_ as of 5th May
  256. 2013 (`SVN <http://svn.whatwg.org/webapps/>`_ revision r7867).
  257. * Python 3.2+ supported in a single codebase using the ``six`` library.
  258. * Removed support for Python 2.5 and older.
  259. * Removed the deprecated Beautiful Soup 3 treebuilder.
  260. ``beautifulsoup4`` can use ``html5lib`` as a parser instead. Note that
  261. since it doesn't support namespaces, foreign content like SVG and
  262. MathML is parsed incorrectly.
  263. * Removed ``simpletree`` from the package. The default tree builder is
  264. now ``etree`` (using the ``xml.etree.cElementTree`` implementation if
  265. available, and ``xml.etree.ElementTree`` otherwise).
  266. * Removed the ``XHTMLSerializer`` as it never actually guaranteed its
  267. output was well-formed XML, and hence provided little of use.
  268. * Removed default DOM treebuilder, so ``html5lib.treebuilders.dom`` is no
  269. longer supported. ``html5lib.treebuilders.getTreeBuilder("dom")`` will
  270. return the default DOM treebuilder, which uses ``xml.dom.minidom``.
  271. * Optional heuristic character encoding detection now based on
  272. ``charade`` for Python 2.6 - 3.3 compatibility.
  273. * Optional ``Genshi`` treewalker support fixed.
  274. * Many bugfixes, including:
  275. * #33: null in attribute value breaks XML AttValue;
  276. * #4: nested, indirect descendant, <button> causes infinite loop;
  277. * `Google Code 215
  278. <http://code.google.com/p/html5lib/issues/detail?id=215>`_: Properly
  279. detect seekable streams;
  280. * `Google Code 206
  281. <http://code.google.com/p/html5lib/issues/detail?id=206>`_: add
  282. support for <video preload=...>, <audio preload=...>;
  283. * `Google Code 205
  284. <http://code.google.com/p/html5lib/issues/detail?id=205>`_: add
  285. support for <video poster=...>;
  286. * `Google Code 202
  287. <http://code.google.com/p/html5lib/issues/detail?id=202>`_: Unicode
  288. file breaks InputStream.
  289. * Source code is now mostly PEP 8 compliant.
  290. * Test harness has been improved and now depends on ``nose``.
  291. * Documentation updated and moved to https://html5lib.readthedocs.io/.
  292. 0.95
  293. ~~~~
  294. Released on February 11, 2012
  295. 0.90
  296. ~~~~
  297. Released on January 17, 2010
  298. 0.11.1
  299. ~~~~~~
  300. Released on June 12, 2008
  301. 0.11
  302. ~~~~
  303. Released on June 10, 2008
  304. 0.10
  305. ~~~~
  306. Released on October 7, 2007
  307. 0.9
  308. ~~~
  309. Released on March 11, 2007
  310. 0.2
  311. ~~~
  312. Released on January 8, 2007