Reviewed by Harrison
authorkocienda <kocienda@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Nov 2004 22:17:02 +0000 (22:17 +0000)
committerkocienda <kocienda@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 29 Nov 2004 22:17:02 +0000 (22:17 +0000)
        Added some new helpers to the VisiblePosition class. I will begin to use these when I check in
        my improved paste code.

        * khtml/editing/visible_position.cpp:
        (khtml::blockRelationship)
        (khtml::visiblePositionsInDifferentBlocks)
        (khtml::isFirstVisiblePositionInBlock)
        (khtml::isFirstVisiblePositionInNode)
        (khtml::isLastVisiblePositionInBlock)
        * khtml/editing/visible_position.h

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@8084 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog-2005-08-23
WebCore/khtml/editing/visible_position.cpp
WebCore/khtml/editing/visible_position.h

index 490f237d7b8cb7d6dde2da59a0c516bbb1c966d0..2a63cbf78ba7e8e20d9d5c0f4559bbe6ba2ec9e4 100644 (file)
@@ -1,3 +1,18 @@
+2004-11-29  Ken Kocienda  <kocienda@apple.com>
+
+        Reviewed by Harrison
+
+        Added some new helpers to the VisiblePosition class. I will begin to use these when I check in
+        my improved paste code.
+
+        * khtml/editing/visible_position.cpp:
+        (khtml::blockRelationship)
+        (khtml::visiblePositionsInDifferentBlocks)
+        (khtml::isFirstVisiblePositionInBlock)
+        (khtml::isFirstVisiblePositionInNode)
+        (khtml::isLastVisiblePositionInBlock)
+        * khtml/editing/visible_position.h
+
 2004-11-29  Ken Kocienda  <kocienda@apple.com>
 
         Reviewed by Harrison
index 02dbe93596859c633fc6f3d9efcf310b410881ac..015484a10123959dced11d25a2f09f2fa0f8175c 100644 (file)
@@ -471,18 +471,53 @@ bool visiblePositionsOnDifferentLines(const VisiblePosition &pos1, const Visible
     return (b1 && b2 && b1->root() != b2->root());
 }
 
-bool visiblePositionsInDifferentBlocks(const VisiblePosition &pos1, const VisiblePosition &pos2)
+enum EBlockRelationship { 
+    NoBlockRelationship, 
+    SameBlockRelationship, 
+    PeerBlockRelationship, 
+    AncestorBlockRelationship, 
+    DescendantBlockRelationship, 
+    OtherBlockRelationship 
+};
+
+static EBlockRelationship blockRelationship(const VisiblePosition &pos1, const VisiblePosition &pos2)
 {
     if (pos1.isNull() || pos2.isNull())
-        return false;
+        return NoBlockRelationship;
     if (pos1 == pos2)
-        return false;
+        return SameBlockRelationship;
 
     Position p1 = pos1.deepEquivalent();
     Position p2 = pos2.deepEquivalent();
     NodeImpl *b1 = p1.node()->enclosingBlockFlowElement();
     NodeImpl *b2 = p2.node()->enclosingBlockFlowElement();
-    return (b1 != b2);
+    if (!b1 || !b2)
+        return NoBlockRelationship;
+    if (b1 == b2) 
+        return SameBlockRelationship;
+    if (b1->parentNode() == b2->parentNode())
+        return PeerBlockRelationship;
+    if (b2->isAncestor(b1))
+        return AncestorBlockRelationship;
+    if (b1->isAncestor(b2))
+        return DescendantBlockRelationship;
+    return OtherBlockRelationship;
+}
+
+bool visiblePositionsInDifferentBlocks(const VisiblePosition &pos1, const VisiblePosition &pos2)
+{
+    switch (blockRelationship(pos1, pos2)) {
+        case NoBlockRelationship:
+        case SameBlockRelationship:
+            return false;
+        case PeerBlockRelationship:
+        case AncestorBlockRelationship:
+        case DescendantBlockRelationship:
+        case OtherBlockRelationship:
+            return true;
+    }
+    ASSERT_NOT_REACHED();
+    return false;
 }
 
 bool isFirstVisiblePositionOnLine(const VisiblePosition &pos)
@@ -494,6 +529,38 @@ bool isFirstVisiblePositionOnLine(const VisiblePosition &pos)
     return previous.isNull() || visiblePositionsOnDifferentLines(pos, previous);
 }
 
+bool isFirstVisiblePositionInBlock(const VisiblePosition &pos)
+{
+    if (pos.isNull())
+        return false;
+        
+    VisiblePosition previous = pos.previous();
+    if (previous.isNull())
+        return true;
+    
+    switch (blockRelationship(pos, previous)) {
+        case NoBlockRelationship:
+        case SameBlockRelationship:
+        case AncestorBlockRelationship:
+            return false;
+        case PeerBlockRelationship:
+        case DescendantBlockRelationship:
+        case OtherBlockRelationship:
+            return true;
+    }
+    ASSERT_NOT_REACHED();
+    return false;
+}
+
+bool isFirstVisiblePositionInNode(const VisiblePosition &pos, const NodeImpl *node)
+{
+    if (pos.isNull())
+        return false;
+        
+    VisiblePosition previous = pos.previous();
+    return previous.isNull() || !previous.deepEquivalent().node()->isAncestor(node);
+}
+
 bool isLastVisiblePositionOnLine(const VisiblePosition &pos)
 {
     if (pos.isNull())
@@ -509,7 +576,21 @@ bool isLastVisiblePositionInBlock(const VisiblePosition &pos)
         return false;
         
     VisiblePosition next = pos.next();
-    return next.isNull() || visiblePositionsInDifferentBlocks(pos, next);
+    if (next.isNull())
+        return true;
+    
+    switch (blockRelationship(pos, next)) {
+        case NoBlockRelationship:
+        case SameBlockRelationship:
+        case DescendantBlockRelationship:
+            return false;
+        case PeerBlockRelationship:
+        case AncestorBlockRelationship:
+        case OtherBlockRelationship:
+            return true;
+    }
+    ASSERT_NOT_REACHED();
+    return false;
 }
 
 bool isLastVisiblePositionInNode(const VisiblePosition &pos, const NodeImpl *node)
index 435a46f56a260814240a5d274fdba462d4ff577f..d9ea7c3df69ab69b6e7ad075c096292136405a7e 100644 (file)
@@ -121,6 +121,8 @@ VisiblePosition endVisiblePosition(const DOM::RangeImpl *);
 bool visiblePositionsOnDifferentLines(const VisiblePosition &, const VisiblePosition &);
 bool visiblePositionsInDifferentBlocks(const VisiblePosition &, const VisiblePosition &);
 bool isFirstVisiblePositionOnLine(const VisiblePosition &);
+bool isFirstVisiblePositionInBlock(const VisiblePosition &);
+bool isFirstVisiblePositionInNode(const VisiblePosition &, const DOM::NodeImpl *);
 bool isLastVisiblePositionOnLine(const VisiblePosition &);
 bool isLastVisiblePositionInBlock(const VisiblePosition &);
 bool isLastVisiblePositionInNode(const VisiblePosition &, const DOM::NodeImpl *);