Root of an HTML tree.
Since the early days of the World Wide Web, there have been many versions: [source: https://www.w3schools.com/html/html_intro.asp]
- 1989: Tim Berners-Lee invented www
- 1991: Tim Berners-Lee invented HTML
- 1993: Dave Raggett drafted HTML+
- 1995: HTML Working Group defined HTML 2.0
- 1997: W3C Recommendation: HTML 3.2
- 1999: W3C Recommendation: HTML 4.01
- 2000: W3C Recommendation: XHTML 1.0
- 2008: WHATWG HTML5 First Public Draft
- 2012: WHATWG HTML5 Living Standard
- 2014: W3C Recommendation: HTML5
- 2016: W3C Candidate Recommendation: HTML 5.1
- 2017: W3C Recommendation: HTML5.1 2nd Edition
- 2017: W3C Recommendation: HTML5.2
HTML elements are generally made of a start tag, an optional element content, and an end tag. However, several elements have only a start tag, like "br" or "img", and a few elements don't have tag at all, like comments.
An HTMLTree has two children: a doctype node, and a "html" node. The "html" tag is the container for all HTML elements of the page. The following properties allow to access to "html" children nodes:
- head
- body_header
- body_nav
- body_main
- body_footer
- body_script
Example
>>> # Create the tree
>>> htree = HTMLTree("index")
>>> htree.add_html_attribute("lang", "en")
>>> # Fill in the <head> element node with:
>>> # a title, a meta and a link
>>> htree.head.title("Purpose")
>>> htree.head.meta({"charset": "utf-8"})
>>> htree.head.link(rel="icon", href="/static/favicon.ico")
>>> # Fill in the <body> element node with:
>>> # A <nav> tag in the header, a <h1> tag in the body and a <p> tag in the footer
>>> htree.set_body_attribute("class", "colors_scheme_dark")
>>> nav = HTMLNode(htree.body_header.identifier, "navmenu", "nav")
>>> htree.body_header.append_child(nav)
>>> node = HTMLNode(htree.body_main.identifier, None, "h1", value="this is a title")
>>> htree.body_main.append_child(node)
>>> node = HTMLNode(htree.body_footer.identifier, None, "p", value="© me now")
>>> htree.body_footer.append_child(node)
>>> # Save into a file
>>> htree.serialize_to_file("/path/to/file.html")
This class does not support yet the global attributes -- i.e. attributes that can be used with all HTML elements. See https://www.w3schools.com/TAgs/ref_standardattributes.asp