mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 08:31:43 +02:00
Adding API Poco::XML insertAfterNP() (#4061)
This commit is contained in:
@@ -37,6 +37,7 @@ public:
|
|||||||
Node* firstChild() const;
|
Node* firstChild() const;
|
||||||
Node* lastChild() const;
|
Node* lastChild() const;
|
||||||
Node* insertBefore(Node* newChild, Node* refChild);
|
Node* insertBefore(Node* newChild, Node* refChild);
|
||||||
|
Node* insertAfterNP(Node* newChild, Node* refChild);
|
||||||
Node* replaceChild(Node* newChild, Node* oldChild);
|
Node* replaceChild(Node* newChild, Node* oldChild);
|
||||||
Node* removeChild(Node* oldChild);
|
Node* removeChild(Node* oldChild);
|
||||||
Node* appendChild(Node* newChild);
|
Node* appendChild(Node* newChild);
|
||||||
|
@@ -52,6 +52,7 @@ public:
|
|||||||
NamedNodeMap* attributes() const;
|
NamedNodeMap* attributes() const;
|
||||||
Document* ownerDocument() const;
|
Document* ownerDocument() const;
|
||||||
Node* insertBefore(Node* newChild, Node* refChild);
|
Node* insertBefore(Node* newChild, Node* refChild);
|
||||||
|
Node* insertAfterNP(Node* newChild, Node* refChild);
|
||||||
Node* replaceChild(Node* newChild, Node* oldChild);
|
Node* replaceChild(Node* newChild, Node* oldChild);
|
||||||
Node* removeChild(Node* oldChild);
|
Node* removeChild(Node* oldChild);
|
||||||
Node* appendChild(Node* newChild);
|
Node* appendChild(Node* newChild);
|
||||||
|
@@ -133,6 +133,14 @@ public:
|
|||||||
/// If newChild is a DocumentFragment object, all of its children are
|
/// If newChild is a DocumentFragment object, all of its children are
|
||||||
/// inserted in the same order, before refChild. If the newChild is already
|
/// inserted in the same order, before refChild. If the newChild is already
|
||||||
/// in the tree, it is first removed.
|
/// 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;
|
virtual Node* replaceChild(Node* newChild, Node* oldChild) = 0;
|
||||||
/// Replaces the child node oldChild with newChild in the list of children,
|
/// Replaces the child node oldChild with newChild in the list of children,
|
||||||
|
@@ -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)
|
Node* AbstractContainerNode::replaceChild(Node* newChild, Node* oldChild)
|
||||||
{
|
{
|
||||||
poco_check_ptr (newChild);
|
poco_check_ptr (newChild);
|
||||||
|
@@ -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)
|
Node* AbstractNode::replaceChild(Node* newChild, Node* oldChild)
|
||||||
{
|
{
|
||||||
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
|
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
|
||||||
|
@@ -65,18 +65,33 @@ void ChildNodesTest::testChildNodes()
|
|||||||
assertTrue (pNL->item(1) == pChild1);
|
assertTrue (pNL->item(1) == pChild1);
|
||||||
assertTrue (pNL->item(2) == pChild2);
|
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);
|
pRoot->removeChild(pChild1);
|
||||||
assertTrue (pNL->length() == 2);
|
assertTrue(pNL->length() == 3);
|
||||||
assertTrue (pNL->item(0) == pChild0);
|
assertTrue(pNL->item(0) == pChild0);
|
||||||
assertTrue (pNL->item(1) == pChild2);
|
assertTrue(pNL->item(1) == pChild3);
|
||||||
|
assertTrue(pNL->item(2) == pChild2);
|
||||||
|
|
||||||
pRoot->removeChild(pChild0);
|
pRoot->removeChild(pChild0);
|
||||||
assertTrue (pNL->length() == 1);
|
assertTrue(pNL->length() == 2);
|
||||||
assertTrue (pNL->item(0) == pChild2);
|
assertTrue(pNL->item(0) == pChild3);
|
||||||
|
assertTrue(pNL->item(1) == pChild2);
|
||||||
|
|
||||||
pRoot->removeChild(pChild2);
|
pRoot->removeChild(pChild2);
|
||||||
assertTrue (pNL->length() == 0);
|
assertTrue(pNL->length() == 1);
|
||||||
assertTrue (pNL->item(0) == 0);
|
assertTrue(pNL->item(0) == pChild3);
|
||||||
|
|
||||||
|
pRoot->removeChild(pChild3);
|
||||||
|
assertTrue(pNL->length() == 0);
|
||||||
|
assertTrue(pNL->item(0) == nullptr);
|
||||||
|
|
||||||
assertTrue (!pRoot->hasChildNodes());
|
assertTrue (!pRoot->hasChildNodes());
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user