Няма описание

DOM.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace MailPoet\Util;
  3. if (!defined('ABSPATH')) exit;
  4. use pQuery\DomNode;
  5. class DOM {
  6. /**
  7. * Splits a DOM tree around the cut element, bringing it up to bound
  8. * ancestor and splitting left and right siblings into subtrees along
  9. * the way, retaining order and nesting level.
  10. */
  11. public static function splitOn(DomNode $bound, DomNode $cutElement) {
  12. $ignoreTextAndCommentNodes = false;
  13. $grandparent = $cutElement->parent;
  14. for ($parent = $cutElement->parent; $bound != $parent; $parent = $grandparent) {
  15. // Clone parent node without children, but with attributes
  16. $parent->after($parent->toString());
  17. $right = $parent->getNextSibling($ignoreTextAndCommentNodes);
  18. $right->clear();
  19. while ($sibling = $cutElement->getNextSibling($ignoreTextAndCommentNodes)) {
  20. $sibling->move($right);
  21. }
  22. // Reattach cut_element and right siblings to grandparent
  23. /* @phpstan-ignore-next-line Because there is a wrong annotation in the library tburry/pquery */
  24. $grandparent = $parent->parent;
  25. $indexAfterParent = $parent->index() + 1;
  26. $right->move($grandparent, $indexAfterParent);
  27. $indexAfterParent = $parent->index() + 1;
  28. $cutElement->move($grandparent, $indexAfterParent);
  29. }
  30. }
  31. public static function findTopAncestor(DomNode $item) {
  32. while ($item->parent->parent !== null) {
  33. $item = $item->parent;
  34. }
  35. return $item;
  36. }
  37. }