Pyjo.DOM.HTML - HTML/XML engine

from Pyjo.DOM.HTML import HTML

# Turn HTML into DOM tree
html = HTML()
html.parse('<div><p id="a">Test</p><p id="b">123</p></div>')
tree = html.tree

# Or import the module, as in Google Python Style Guide

import Pyjo.DOM.HTML

# Turn HTML into DOM tree
html = Pyjo.DOM.HTML.new()
html.parse('<div><p id="a">Test</p><p id="b">123</p></div>')
tree = html.tree

Pyjo.DOM.HTML is the HTML/XML engine used by Pyjo.DOM, based on the HTML Living Standard and the Extensible Markup Language (XML) 1.0.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::DOM::HTML of Mojolicious 9.53.

Functions

class Pyjo.DOM.HTML.Pyjo_DOM_HTML(tree: Tree | None = None, **kwargs: bool | None)[source]

Pyjo.DOM.HTML inherits all methods from Pyjo.Base and implements the following new ones.

parse(html: Anything) → Self[source]
html = html.parse('<foo bar="baz">I ♥ Pyjoyment!</foo>')

Parse HTML/XML fragment. Bytes are a whole document, whose encoding is found as the HTML Living Standard sniffs it: a byte order mark, the encoding of an XML declaration, a <meta charset> or <meta http-equiv="Content-Type"> in the first 1024 bytes, and UTF-8 by default.

render() → str[source]
string = html.render()

Render DOM to HTML/XML.

tag(name: str, content: str | Callable[[], str] | None = None, /, **attrs: Any) → Self[source]
html = html.tag('div', 'safe content', id='foo')

Generate HTML/XML tag.

tree: Tree
tree = html.tree
html.tree = ['root']

Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.

property xml: bool | None[source]
boolean = html.xml
html.xml = boolean

Disable HTML semantics in parser and activate case-sensitivity, defaults to auto-detection based on a leading XML declaration.

Pyjo.DOM.HTML.tag_to_html(name: str, content: str | Callable[[], str] | None = None, /, **attrs: Any) → str[source]
string = tag_to_html('div', 'safe content', id='foo')

Generate HTML/XML tag and render it right away. This is a significantly faster alternative to tag() for template systems that have to generate a lot of tags.