Skip to content

Commit

Permalink
Adding API Poco::XML insertAfterNP() (#4061)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuduongquyet authored and aleks-f committed Nov 27, 2023
1 parent b962bfd commit a615877
Show file tree
Hide file tree
Showing 6 changed files with 112 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
1 change: 1 addition & 0 deletions XML/include/Poco/DOM/AbstractNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class XML_API AbstractNode: public Node
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);
Expand Down
8 changes: 8 additions & 0 deletions XML/include/Poco/DOM/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class XML_API Node: public EventTarget
/// 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,
Expand Down
74 changes: 74 additions & 0 deletions XML/src/AbstractContainerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions XML/src/AbstractNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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> 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());
}
Expand Down

0 comments on commit a615877

Please sign in to comment.