Skip to content

Commit

Permalink
Adding API XML::AbstractContainerNode::insertAfterNP()
Browse files Browse the repository at this point in the history
  • Loading branch information
tuduongquyet committed Jul 12, 2023
1 parent bce5f96 commit cb790a4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
1 change: 1 addition & 0 deletions XML/include/Poco/DOM/AbstractContainerNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class XML_API AbstractContainerNode: public AbstractNode
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);
Expand Down
73 changes: 73 additions & 0 deletions XML/src/AbstractContainerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,79 @@ Node* AbstractContainerNode::replaceChild(Node* newChild, Node* oldChild)
}


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 newChild;
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::removeChild(Node* oldChild)
{
poco_check_ptr (oldChild);
Expand Down
29 changes: 22 additions & 7 deletions XML/testsuite/src/ChildNodesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,33 @@ void ChildNodesTest::testChildNodes()
assertTrue (pNL->item(1) == pChild1);
assertTrue (pNL->item(2) == pChild2);

AutoPtr<Element> pNewElement = pDoc->createElement("newElement");
pRoot->insertAfterNP(pNewElement, pChild1);

assertTrue(pNL->length() == 4);
assertTrue(pNL->item(0) == pChild0);
assertTrue(pNL->item(1) == pChild1);
assertTrue(pNL->item(2) == pNewElement);
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) == pNewElement);
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) == pNewElement);
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) == pNewElement);

pRoot->removeChild(pNewElement);
assertTrue(pNL->length() == 0);
assertTrue(pNL->item(0) == nullptr);

assertTrue (!pRoot->hasChildNodes());
}
Expand Down

0 comments on commit cb790a4

Please sign in to comment.