patch to fix exception raised by KHTMLPart::selection() when a <br> was the end of selection

Darin Adler darin at apple.com
Thu Oct 9 23:29:24 CEST 2003


When the end of a selection is a <br> element, KHTMLPart::selection() 
ends up raising an exception because it passes an offset of 1 and the 
<br> node, and the <br> node is not a text node so the offset is 
treated as an offset within it's list of children.

This relatively straightforward change to RenderBR was the best fix I 
could think of.

-------------- next part --------------
Index: ChangeLog
===================================================================
RCS file: /local/home/cvs/Labyrinth/WebCore/ChangeLog,v
retrieving revision 1.2075
diff -p -u -u -p -r1.2075 ChangeLog
--- ChangeLog	2003/10/10 03:21:48	1.2075
+++ ChangeLog	2003/10/10 04:50:33
@@ -1,3 +1,15 @@
+2003-10-09  Darin Adler  <darin at apple.com>
+
+        Reviewed by Maciej.
+
+        - fixed 3449280 -- REGRESSION: crash copying a selection that ends in a <br>
+
+        * khtml/rendering/render_br.h: Add checkSelectionPointIgnoringContinuations.
+        * khtml/rendering/render_br.cpp: (RenderBR::checkSelectionPointIgnoringContinuations):
+        Override to never return an offset of 1. We can't use a character offset because the
+        DOM doesn't know that we're implemented as a text object. So an offset of 1 turns into
+        "after this element".
+
 2003-10-09  Maciej Stachowiak  <mjs at apple.com>
 
         - fixed crash I just added.
Index: khtml/rendering/render_br.cpp
===================================================================
RCS file: /local/home/cvs/Labyrinth/WebCore/khtml/rendering/render_br.cpp,v
retrieving revision 1.4
diff -p -u -u -p -r1.4 khtml/rendering/render_br.cpp
--- khtml/rendering/render_br.cpp	2002/11/12 21:44:52	1.4
+++ khtml/rendering/render_br.cpp	2003/10/10 04:50:33
@@ -47,8 +47,7 @@ void RenderBR::cursorPos(int /*offset*/,
     int absx, absy;
     absolutePosition(absx,absy);
     if (absx == -1) {
-        // we don't know out absoluate position, and there is not point returning
-        // just a relative one
+        // we don't know our absolute position, and there is no point returning just a relative one
         _x = _y = -1;
     }
     else {
@@ -57,5 +56,20 @@ void RenderBR::cursorPos(int /*offset*/,
     }
     height = RenderText::verticalPositionHint( false );
 
+}
+
+FindSelectionResult RenderBR::checkSelectionPointIgnoringContinuations(int _x, int _y, int _tx, int _ty, DOM::NodeImpl*& node, int &offset)
+{
+    FindSelectionResult result = RenderText::checkSelectionPointIgnoringContinuations(_x, _y, _tx, _ty, node, offset);
+
+    // Since the DOM does not consider this to be a text element, we can't return an offset of 1,
+    // because that means after the first child (and we have none) rather than after the
+    // first character. Instead we return a result of "after" and an offset of 0.
+    if (offset == 1 && node == element()) {
+        offset = 0;
+        result = SelectionPointAfter;
+    }
+
+    return result;
 }
 
Index: khtml/rendering/render_br.h
===================================================================
RCS file: /local/home/cvs/Labyrinth/WebCore/khtml/rendering/render_br.h,v
retrieving revision 1.4
diff -p -u -u -p -r1.4 khtml/rendering/render_br.h
--- khtml/rendering/render_br.h	2002/11/19 07:44:09	1.4
+++ khtml/rendering/render_br.h	2003/10/10 04:50:33
@@ -44,7 +44,6 @@ public:
     virtual void position(int, int, int, int, int, bool, bool, int) {}
     virtual unsigned int width(unsigned int, unsigned int, const Font *) const { return 0; }
     virtual unsigned int width( unsigned int, unsigned int, bool) const { return 0; }
-    virtual short width() const { return RenderText::width(); }
 
     virtual int height() const { return 0; }
 
@@ -55,7 +54,10 @@ public:
 
     virtual bool isBR() const { return true; }
     virtual void cursorPos(int offset, int &_x, int &_y, int &height);
+
+    virtual FindSelectionResult checkSelectionPointIgnoringContinuations(int _x, int _y, int _tx, int _ty, DOM::NodeImpl*& node, int &offset);
 };
 
-};
+}
+
 #endif
-------------- next part --------------


     -- Darin


More information about the Khtml-devel mailing list