#############################
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
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("