tng 2003/01/27 10:16:52
Modified: c/src/xercesc/dom/impl DOMRangeImpl.cpp
Log:
More fixes to DOMRange.
Revision Changes Path
1.9 +102 -71 xml-xerces/c/src/xercesc/dom/impl/DOMRangeImpl.cpp
Index: DOMRangeImpl.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/dom/impl/DOMRangeImpl.cpp,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DOMRangeImpl.cpp 16 Jan 2003 20:12:19 -0000 1.8
+++ DOMRangeImpl.cpp 27 Jan 2003 18:16:52 -0000 1.9
@@ -228,16 +228,19 @@
validateNode(refNode);
checkIndex(refNode, offset);
- fStartContainer = (DOMNode*) refNode;
- fStartOffset = offset;
-
// error if not the same owner document
if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
+ if ( refNode != fDocument ) {
+ collapse(true); //collapse the range positions to start
+ fCollapsed = true;
throw DOMException(
DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
}
+ fStartContainer = (DOMNode*) refNode;
+ fStartOffset = offset;
+
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fEndContainer))
@@ -256,16 +259,19 @@
validateNode(refNode);
checkIndex(refNode, offset);
- fEndContainer = (DOMNode*) refNode;
- fEndOffset = offset;
-
// error if not the same owner document
if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
+ if ( refNode != fDocument ) {
+ collapse(false); //collapse the range positions to end
+ fCollapsed = true;
throw DOMException(
DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
}
+ fEndContainer = (DOMNode*) refNode;
+ fEndOffset = offset;
+
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fStartContainer))
@@ -290,6 +296,16 @@
DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
}
+ // error if not the same owner document
+ if (fDocument != refNode->getOwnerDocument()) {
+ if ( refNode != fDocument ) {
+ collapse(true); //collapse the range positions to start
+ fCollapsed = true;
+ throw DOMException(
+ DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
+ }
+
fStartContainer = refNode->getParentNode();
XMLSize_t i = 0;
for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling()) {
@@ -300,13 +316,6 @@
else
fStartOffset = i-1;
- // error if not the same owner document
- if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
- }
-
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fEndContainer))
@@ -331,6 +340,16 @@
DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
}
+ // error if not the same owner document
+ if (fDocument != refNode->getOwnerDocument()) {
+ if ( refNode != fDocument ) {
+ collapse(true); //collapse the range positions to start
+ fCollapsed = true;
+ throw DOMException(
+ DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
+ }
+
fStartContainer = refNode->getParentNode();
XMLSize_t i = 0;
for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling()) {
@@ -339,13 +358,6 @@
fStartOffset = i;
- // error if not the same owner document
- if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
- }
-
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fEndContainer))
@@ -370,6 +382,16 @@
DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
}
+ // error if not the same owner document
+ if (fDocument != refNode->getOwnerDocument()) {
+ if ( refNode != fDocument ) {
+ collapse(false); //collapse the range positions to end
+ fCollapsed = true;
+ throw DOMException(
+ DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
+ }
+
fEndContainer = refNode->getParentNode();
XMLSize_t i = 0;
for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling(), i++) ;
@@ -379,13 +401,6 @@
else
fEndOffset = i-1;
- // error if not the same owner document
- if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
- }
-
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fStartContainer))
@@ -410,6 +425,16 @@
DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
}
+ // error if not the same owner document
+ if (fDocument != refNode->getOwnerDocument()) {
+ if ( refNode != fDocument ) {
+ collapse(false); //collapse the range positions to end
+ fCollapsed = true;
+ throw DOMException(
+ DOMException::WRONG_DOCUMENT_ERR, 0);
+ }
+ }
+
fEndContainer = refNode->getParentNode();
XMLSize_t i = 0;
for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling(), i++) ;
@@ -419,13 +444,6 @@
else
fEndOffset = i;
- // error if not the same owner document
- if (fDocument != refNode->getOwnerDocument()) {
- if ( refNode != fDocument )
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
- }
-
// they may be of same document, but not same root container
// collapse if not the same root container
if (!commonAncestorOf(refNode, fStartContainer))
@@ -677,27 +695,40 @@
}
}
- DOMNode* ancestor = (DOMNode*) commonAncestorOf(pointA, pointB);
-
// case 4: preorder traversal of context tree.
- if (ancestor) {
- DOMNode* current = ancestor;
+ // Instead of literally walking the context tree in pre-order,
+ // we use relative node depth walking which is usually faster
- do {
- if (current == pointA) return -1;
- if (current == pointB) return 1;
- current = nextNode(current, true);
- }
- while (current!=0 && current!=ancestor);
+ int depthDiff = 0;
+ for ( DOMNode* n = pointB; n != 0; n = n->getParentNode() )
+ depthDiff++;
+ for ( DOMNode* n = pointA; n != 0; n = n->getParentNode() )
+ depthDiff--;
+ while (depthDiff > 0) {
+ pointB = pointB->getParentNode();
+ depthDiff--;
+ }
+ while (depthDiff < 0) {
+ pointA = pointA->getParentNode();
+ depthDiff++;
+ }
+ for (DOMNode* pB = pointB->getParentNode(),
+ *pA = pointA->getParentNode();
+ pB != pA;
+ pB = pB->getParentNode(), pA = pA->getParentNode() )
+ {
+ pointB = pB;
+ pointA = pA;
}
- else {
- // case 5: there is no common ancestor of these two points
- // it means the two Ranges are not in the same "root container"
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
+ for ( DOMNode* n = pointB->getNextSibling();
+ n != 0;
+ n = n->getNextSibling() )
+ {
+ if (n == pointA) {
+ return 1;
+ }
}
-
- return -2; // this should never happen
+ return -1;
}
@@ -723,16 +754,19 @@
{
if (newNode == 0) return; //don't have to do anything
- for (DOMNode* aNode = fStartContainer; aNode!=0; aNode =
aNode->getParentNode()) {
- if (castToNodeImpl(newNode)->isReadOnly()) {
+ if( fDetached) {
throw DOMException(
- DOMException::NO_MODIFICATION_ALLOWED_ERR, 0);
- }
+ DOMException::INVALID_STATE_ERR, 0);
}
- if (fDocument != newNode->getOwnerDocument()) {
- throw DOMException(
- DOMException::WRONG_DOCUMENT_ERR, 0);
+ int type = newNode->getNodeType();
+ if (type == DOMNode::ATTRIBUTE_NODE
+ || type == DOMNode::ENTITY_NODE
+ || type == DOMNode::NOTATION_NODE
+ || type == DOMNode::DOCUMENT_NODE)
+ {
+ throw DOMRangeException(
+ DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
}
// Prevent cycles in the tree.
@@ -742,19 +776,16 @@
DOMException::HIERARCHY_REQUEST_ERR, 0);
}
- if( fDetached) {
+ for (DOMNode* aNode = fStartContainer; aNode!=0; aNode =
aNode->getParentNode()) {
+ if (castToNodeImpl(newNode)->isReadOnly()) {
throw DOMException(
- DOMException::INVALID_STATE_ERR, 0);
+ DOMException::NO_MODIFICATION_ALLOWED_ERR, 0);
+ }
}
- int type = newNode->getNodeType();
- if (type == DOMNode::ATTRIBUTE_NODE
- || type == DOMNode::ENTITY_NODE
- || type == DOMNode::NOTATION_NODE
- || type == DOMNode::DOCUMENT_NODE)
- {
- throw DOMRangeException(
- DOMRangeException::INVALID_NODE_TYPE_ERR, 0);
+ if (fDocument != newNode->getOwnerDocument()) {
+ throw DOMException(
+ DOMException::WRONG_DOCUMENT_ERR, 0);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]