Can HTMLRewriter extract specific HTML elements, or move them within the document?

I know how to edit specific HTML elements using HTMLRewriter. But what I would like to do is e.g.

  • Move an element to the bottom of its containing element (container.appendChild(node) in Javascript)

  • Add HTML above a specific element, on the condition that a different element is found on the page

Are such operations possible using HTMLRewriter? So far I’ve only found code that explain how to modify individual elements. I have not found any documentation about extracting or moving elements or checking for the existence of particular classes/elements in the entire document.