The code paths in DOMHandler::getTrailingSpace() and DOMHandler::getLeadingSpace() taken when useWhitespaceHeuristics is true seem like they're better placed in Separators::buildSep() where the other logic to restore original separators already exists. This duplication has led to a few bugs (T260960 / T245206).
From the discussion in the review,
https://gerrit.wikimedia.org/r/c/mediawiki/services/parsoid/+/628955/3/src/Html2Wt/DOMHandlers/DOMHandler.php
- buildSep was assumed to restore separators only between adjacent elements, however, it has an asymmetric path of walking back from non-elements nodes to find an element to calculate dsr off of. That should maybe be extended to both directions.
- Some helpers can be created to help clarify predicates, like,
- DOMUtils::haveDSR($node) for $node instanceof DOMElement and !empty( .. dsr ... )
- !isAnOldEltNode( $lc )) for !$lc || !DOMUtils::isElt( $lc ) || WTUtils::isNewElt( $lc )