Adding API Poco::XML insertAfterNP() (#4061)

This commit is contained in:
tuduongquyet 2023-11-27 01:49:50 +07:00 committed by GitHub
parent 388a3b4010
commit af69d61ff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 7 deletions

View File

@ -37,6 +37,7 @@ public:
Node* firstChild() const;
Node* lastChild() const;
Node* insertBefore(Node* newChild, Node* refChild);
Node* insertAfterNP(Node* newChild, Node* refChild);
Node* replaceChild(Node* newChild, Node* oldChild);
Node* removeChild(Node* oldChild);
Node* appendChild(Node* newChild);

View File

@ -52,6 +52,7 @@ public:
NamedNodeMap* attributes() const;
Document* ownerDocument() const;
Node* insertBefore(Node* newChild, Node* refChild);
Node* insertAfterNP(Node* newChild, Node* refChild);
Node* replaceChild(Node* newChild, Node* oldChild);
Node* removeChild(Node* oldChild);
Node* appendChild(Node* newChild);

View File

@ -133,6 +133,14 @@ public:
/// If newChild is a DocumentFragment object, all of its children are
/// inserted in the same order, before refChild. If the newChild is already
/// in the tree, it is first removed.
virtual Node* insertAfterNP(Node* newChild, Node* refChild) = 0;
/// Inserts the node newChild after the existing child node refChild.
///
/// If refChild is null, insert newChild at the beginning of the list of children.
/// If newChild is a DocumentFragment object, all of its children are
/// inserted in the same order, after refChild. If the newChild is already
/// in the tree, it is first removed.
virtual Node* replaceChild(Node* newChild, Node* oldChild) = 0;
/// Replaces the child node oldChild with newChild in the list of children,

View File

@ -151,6 +151,80 @@ Node* AbstractContainerNode::insertBefore(Node* newChild, Node* refChild)
}
Node* AbstractContainerNode::insertAfterNP(Node* newChild, Node* refChild)
{
poco_check_ptr (newChild);
if (static_cast<AbstractNode*>(newChild)->_pOwner != _pOwner && static_cast<AbstractNode*>(newChild)->_pOwner != this)
throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
if (refChild && static_cast<AbstractNode*>(refChild)->_pParent != this)
throw DOMException(DOMException::NOT_FOUND_ERR);
if (newChild == refChild)
return nullptr;
if (this == newChild)
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
AbstractNode* pFirst = 0;
AbstractNode* pLast = 0;
if (newChild->nodeType() == Node::DOCUMENT_FRAGMENT_NODE)
{
AbstractContainerNode* pFrag = static_cast<AbstractContainerNode*>(newChild);
pFirst = pFrag->_pFirstChild;
pLast = pFirst;
if (pFirst)
{
while (pLast->_pNext)
{
pLast->_pParent = this;
pLast = pLast->_pNext;
}
pLast->_pParent = this;
}
pFrag->_pFirstChild = 0;
}
else
{
newChild->duplicate();
AbstractContainerNode* pParent = static_cast<AbstractNode*>(newChild)->_pParent;
if (pParent) pParent->removeChild(newChild);
pFirst = static_cast<AbstractNode*>(newChild);
pLast = pFirst;
pFirst->_pParent = this;
}
if (_pFirstChild && pFirst)
{
AbstractNode* pCur = _pFirstChild;
while (pCur && pCur != refChild)
{
pCur = pCur->_pNext;
}
if (pCur)
{
pLast->_pNext = pCur->_pNext;
pCur->_pNext = pFirst;
}
else throw DOMException(DOMException::NOT_FOUND_ERR);
}
else
{
_pFirstChild = pFirst;
}
if (events())
{
while (pFirst && pFirst != pLast->_pNext)
{
pFirst->dispatchNodeInserted();
pFirst->dispatchNodeInsertedIntoDocument();
pFirst = pFirst->_pNext;
}
dispatchSubtreeModified();
}
return newChild;
}
Node* AbstractContainerNode::replaceChild(Node* newChild, Node* oldChild)
{
poco_check_ptr (newChild);

View File

@ -144,6 +144,12 @@ Node* AbstractNode::insertBefore(Node* newChild, Node* refChild)
}
Node* AbstractNode::insertAfterNP(Node* newChild, Node* refChild)
{
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
}
Node* AbstractNode::replaceChild(Node* newChild, Node* oldChild)
{
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);

View File

@ -65,18 +65,33 @@ void ChildNodesTest::testChildNodes()
assertTrue (pNL->item(1) == pChild1);
assertTrue (pNL->item(2) == pChild2);
AutoPtr<Element> pChild3 = pDoc->createElement("child3");
pRoot->insertAfterNP(pChild3, pChild1);
assertTrue(pNL->length() == 4);
assertTrue(pNL->item(0) == pChild0);
assertTrue(pNL->item(1) == pChild1);
assertTrue(pNL->item(2) == pChild3);
assertTrue(pNL->item(3) == pChild2);
pRoot->removeChild(pChild1);
assertTrue (pNL->length() == 2);
assertTrue (pNL->item(0) == pChild0);
assertTrue (pNL->item(1) == pChild2);
assertTrue(pNL->length() == 3);
assertTrue(pNL->item(0) == pChild0);
assertTrue(pNL->item(1) == pChild3);
assertTrue(pNL->item(2) == pChild2);
pRoot->removeChild(pChild0);
assertTrue (pNL->length() == 1);
assertTrue (pNL->item(0) == pChild2);
assertTrue(pNL->length() == 2);
assertTrue(pNL->item(0) == pChild3);
assertTrue(pNL->item(1) == pChild2);
pRoot->removeChild(pChild2);
assertTrue (pNL->length() == 0);
assertTrue (pNL->item(0) == 0);
assertTrue(pNL->length() == 1);
assertTrue(pNL->item(0) == pChild3);
pRoot->removeChild(pChild3);
assertTrue(pNL->length() == 0);
assertTrue(pNL->item(0) == nullptr);
assertTrue (!pRoot->hasChildNodes());
}