############### Edit the tree ############### Change a parsed tree in place: build nodes by hand, insert and move them, set an element's text and attributes, and remove what you do not need. ********************** Build a tree by hand ********************** Construct nodes with :class:`~turbohtml.Element`, :class:`~turbohtml.Text`, and :class:`~turbohtml.Comment`, then assemble them. A list value for a token-list attribute (``class``, ``rel``, ...) joins on a space, and the ``text`` setter fills an element with a single text child: .. testcode:: from turbohtml import Element card = Element("article", {"class": ["card", "lg"]}) heading = Element("h2") heading.text = "Title" card.append(heading) print(card.html) .. testoutput::

Title

***************************** Edit a parsed tree in place ***************************** The structural edits move nodes within a tree and adopt nodes from another. ``unwrap`` replaces an element with its children and ``decompose`` drops a subtree: .. testcode:: doc = turbohtml.parse("

keep bold drop me

") p = doc.find("p") print(doc.find("b").unwrap()) doc.find("span").decompose() print(p.html) .. testoutput:: Element('b')

keep bold

******************************** Strip tags matching a selector ******************************** Where :meth:`~turbohtml.Node.prune` keeps the matches, its two inverses drop them in bulk. :meth:`~turbohtml.Node.remove` deletes every matching element and its whole subtree, and :meth:`~turbohtml.Node.strip_tags` unwraps every match, dropping the tag but lifting its children into its place. Both take one CSS selector, edit in place, and return the node, so they chain like ``prune``: .. testcode:: markup = "

keep this text

" doc = turbohtml.parse(markup) doc.remove("script") # drop the