code: private void changeDocumentToThis(NodeImpl node) {
          Map<String, UserData> userData = node.document.getUserDataMapForRead(node);
          if (!userData.isEmpty()) {
              getUserDataMap(node).putAll(userData);
          }
          node.document = this;
  
          // change the document on all child nodes
          NodeList list = node.getChildNodes();
          for (int i = 0; i < list.getLength(); i++) {
              changeDocumentToThis((NodeImpl) list.item(i));
          }
  
          // change the document on all attribute nodes
          if (node.getNodeType() == Node.ELEMENT_NODE) {
              NamedNodeMap attributes = node.getAttributes();
              for (int i = 0; i < attributes.getLength(); i++) {
                  changeDocumentToThis((AttrImpl) attributes.item(i));
              }
          }
      }
docs: Recursively change the document of {@code node} without also changing its
  parent node. Only adoptNode() should invoke this method, otherwise nodes
  will be left in an inconsistent state.