############################# Building and editing a tree ############################# Everything so far read a document that already existed. You can also build one. Construct nodes from their classes and assemble them with :meth:`~turbohtml.Element.append`; the ``text`` setter fills an element with a single text child: .. testcode:: import turbohtml from turbohtml import Element, Comment article = turbohtml.Element("article", {"class": "post"}) title = turbohtml.Element("h1") title.text = "Tea" article.append(title) article.append(Comment("draft")) print(article.html) .. testoutput::

Tea

A list value for a token-list attribute (``class``, ``rel``, ...) joins on a space, and ``None`` (or ``""``) sets an empty attribute, which reads back as the empty string: .. testcode:: print(turbohtml.Element("input", {"class": ["a", "b"], "disabled": None}).html) .. testoutput:: Editing a parsed tree uses the BeautifulSoup vocabulary (``insert_before``, ``replace_with``, ``wrap``, ``unwrap``, ``decompose``), and ``element.attrs`` is a live mapping you assign to. A node already in a tree moves; a node from another tree is adopted by copy: .. testcode:: doc = turbohtml.parse("

keep bold drop

") print(doc.find("b").unwrap()) doc.find("span").decompose() doc.find("p").attrs["class"] = "lead" print(doc.find("p").html) .. testoutput:: Element('b')

keep bold

Duplicate a subtree with :func:`python:copy.deepcopy` (or :mod:`python:pickle`); the clone is a standalone tree you can edit without touching the original: .. testcode:: import copy clone = copy.deepcopy(article) clone.append(turbohtml.Element("footer")) print(clone.html == article.html) .. testoutput:: False When you serialize, set an ``Html`` config's ``layout`` to a :class:`~turbohtml.Minify` to shrink the output without changing what it means: it folds insignificant whitespace, omits optional tags, unquotes safe attributes, and strips comments, and the result reparses to the same tree. Here the ```` tags stay because real whitespace separates the items: .. testcode:: from turbohtml import Html, Minify page = turbohtml.parse("") print(page.find("ul").serialize(Html(layout=Minify()))) .. testoutput:: With a tree you can build and reshape, continue to :doc:`exporting` to turn it back into text.