Refactor DOM filter to return modified document

This allows e.g. chunk processing return a new document instead of
modifying the existing document.
Clean chunking code