Stop calling node() and deprecatedEditingOffset() in comparePositions
https://bugs.webkit.org/show_bug.cgi?id=54535

Reviewed by Enrica Casucci.

Source/WebCore: 

Replaced deprecatedNode() and deprecatedEditingOffset() by containerNode() and computeOffsetInContainerNode()
in comparePositions().
        
In addition, fixed a bunch of bugs in DeleteSelectionCommand::handleSpecialCaseBRDelete revealed by this change:
- Use node after position instead of deprecated node in determinig whether start and end positions at a br.
- Don't set m_startsAtEmptyLine true when BR is wrapped in a block element. The only reason this code had worked
was positions like (div, offset, 0) and (br, before) in <div><br> were treated differently, which we no longer do.

* editing/DeleteSelectionCommand.cpp:
(WebCore::DeleteSelectionCommand::handleSpecialCaseBRDelete):
* editing/htmlediting.cpp:
(WebCore::comparePositions):

LayoutTests: 

Rebaselined existing tests. There are no user-visible changes.

* editing/inseting/font-size-clears-from-typing-style-expected.txt: No longer keeps div's around
when merging paragraphs.
* platform/mac/editing/deleting/delete-br-002-expected.txt:
* platform/mac/editing/deleting/delete-br-004-expected.txt:
* platform/mac/editing/deleting/delete-br-005-expected.txt:
* platform/mac/editing/deleting/delete-br-006-expected.txt:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@121303 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 3421b6e..5d2c131 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,19 @@
+2012-06-26  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Stop calling node() and deprecatedEditingOffset() in comparePositions
+        https://bugs.webkit.org/show_bug.cgi?id=54535
+
+        Reviewed by Enrica Casucci.
+
+        Rebaselined existing tests. There are no user-visible changes.
+
+        * editing/inseting/font-size-clears-from-typing-style-expected.txt: No longer keeps div's around
+        when merging paragraphs.
+        * platform/mac/editing/deleting/delete-br-002-expected.txt:
+        * platform/mac/editing/deleting/delete-br-004-expected.txt:
+        * platform/mac/editing/deleting/delete-br-005-expected.txt:
+        * platform/mac/editing/deleting/delete-br-006-expected.txt:
+
 2012-06-26  Alice Cheng  <alice_cheng@apple.com>
 
         Crash at WebCore::TextIterator::handleTextBox
diff --git a/LayoutTests/editing/inserting/font-size-clears-from-typing-style-expected.txt b/LayoutTests/editing/inserting/font-size-clears-from-typing-style-expected.txt
index 11f42ed..4e4fe5c 100644
--- a/LayoutTests/editing/inserting/font-size-clears-from-typing-style-expected.txt
+++ b/LayoutTests/editing/inserting/font-size-clears-from-typing-style-expected.txt
@@ -1,4 +1,3 @@
 Tests that we don't serialize redundant font-size in typingStyle. There should be no span or style attribute around A or B below.See https://bugs.webkit.org/show_bug.cgi?id=26279.
-| <div>
-|   id="wrapper"
-|   "B<#selection-caret>A"
+| "B<#selection-caret>A"
+| <br>
diff --git a/LayoutTests/platform/mac/editing/deleting/delete-br-002-expected.txt b/LayoutTests/platform/mac/editing/deleting/delete-br-002-expected.txt
index 5191b31..82e27c0d 100644
--- a/LayoutTests/platform/mac/editing/deleting/delete-br-002-expected.txt
+++ b/LayoutTests/platform/mac/editing/deleting/delete-br-002-expected.txt
@@ -24,6 +24,7 @@
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document toDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 2 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 layer at (0,0) size 800x600
diff --git a/LayoutTests/platform/mac/editing/deleting/delete-br-004-expected.txt b/LayoutTests/platform/mac/editing/deleting/delete-br-004-expected.txt
index d3fb5ef..7e60bc4 100644
--- a/LayoutTests/platform/mac/editing/deleting/delete-br-004-expected.txt
+++ b/LayoutTests/platform/mac/editing/deleting/delete-br-004-expected.txt
@@ -25,6 +25,7 @@
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 4 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 9 of #text > SPAN > DIV > BODY > HTML > #document to 9 of #text > SPAN > DIV > BODY > HTML > #document toDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 layer at (0,0) size 800x600
@@ -42,5 +43,4 @@
           RenderBR {BR} at (14,42) size 0x28
           RenderText {#text} at (14,70) size 92x28
             text run at (14,70) width 92: "years ago"
-        RenderText {#text} at (0,0) size 0x0
 caret: position 0 of child 3 {#text} of child 1 {SPAN} of child 1 {DIV} of body
diff --git a/LayoutTests/platform/mac/editing/deleting/delete-br-005-expected.txt b/LayoutTests/platform/mac/editing/deleting/delete-br-005-expected.txt
index 32dffec..63c0400 100644
--- a/LayoutTests/platform/mac/editing/deleting/delete-br-005-expected.txt
+++ b/LayoutTests/platform/mac/editing/deleting/delete-br-005-expected.txt
@@ -25,9 +25,11 @@
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 4 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 9 of #text > SPAN > DIV > BODY > HTML > #document to 9 of #text > SPAN > DIV > BODY > HTML > #document toDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 9 of #text > SPAN > DIV > BODY > HTML > #document to 9 of #text > SPAN > DIV > BODY > HTML > #document toDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 2 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 layer at (0,0) size 800x600
@@ -44,5 +46,4 @@
           RenderBR {BR} at (218,36) size 0x0
           RenderText {#text} at (14,42) size 92x28
             text run at (14,42) width 92: "years ago"
-        RenderText {#text} at (0,0) size 0x0
 caret: position 0 of child 2 {#text} of child 1 {SPAN} of child 1 {DIV} of body
diff --git a/LayoutTests/platform/mac/editing/deleting/delete-br-006-expected.txt b/LayoutTests/platform/mac/editing/deleting/delete-br-006-expected.txt
index 028ba06..f59c452 100644
--- a/LayoutTests/platform/mac/editing/deleting/delete-br-006-expected.txt
+++ b/LayoutTests/platform/mac/editing/deleting/delete-br-006-expected.txt
@@ -25,9 +25,11 @@
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 4 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 9 of #text > SPAN > DIV > BODY > HTML > #document to 9 of #text > SPAN > DIV > BODY > HTML > #document toDOMRange:range from 3 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 3 of SPAN > DIV > BODY > HTML > #document
+EDITING DELEGATE: shouldChangeSelectedDOMRange:range from 9 of #text > SPAN > DIV > BODY > HTML > #document to 9 of #text > SPAN > DIV > BODY > HTML > #document toDOMRange:range from 2 of SPAN > DIV > BODY > HTML > #document to 2 of SPAN > DIV > BODY > HTML > #document affinity:NSSelectionAffinityDownstream stillSelecting:FALSE
 EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification
 EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification
 EDITING DELEGATE: shouldDeleteDOMRange:range from 1 of SPAN > DIV > BODY > HTML > #document to 2 of SPAN > DIV > BODY > HTML > #document
@@ -47,5 +49,4 @@
             text run at (157,14) width 61: "seven "
           RenderText {#text} at (218,14) size 92x28
             text run at (218,14) width 92: "years ago"
-        RenderText {#text} at (0,0) size 0x0
 caret: position 23 of child 0 {#text} of child 1 {SPAN} of child 1 {DIV} of body
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 2ef2d39..5e6a003 100755
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,23 @@
+2012-06-26  Ryosuke Niwa  <rniwa@webkit.org>
+
+        Stop calling node() and deprecatedEditingOffset() in comparePositions
+        https://bugs.webkit.org/show_bug.cgi?id=54535
+
+        Reviewed by Enrica Casucci.
+
+        Replaced deprecatedNode() and deprecatedEditingOffset() by containerNode() and computeOffsetInContainerNode()
+        in comparePositions().
+        
+        In addition, fixed a bunch of bugs in DeleteSelectionCommand::handleSpecialCaseBRDelete revealed by this change:
+        - Use node after position instead of deprecated node in determinig whether start and end positions at a br.
+        - Don't set m_startsAtEmptyLine true when BR is wrapped in a block element. The only reason this code had worked
+        was positions like (div, offset, 0) and (br, before) in <div><br> were treated differently, which we no longer do.
+
+        * editing/DeleteSelectionCommand.cpp:
+        (WebCore::DeleteSelectionCommand::handleSpecialCaseBRDelete):
+        * editing/htmlediting.cpp:
+        (WebCore::comparePositions):
+
 2012-06-26  Christophe Dumez  <christophe.dumez@intel.com>
 
         [EFL] Use eina_file_ls() in EFL implementation of FileSystem listDirectory()
diff --git a/Source/WebCore/editing/DeleteSelectionCommand.cpp b/Source/WebCore/editing/DeleteSelectionCommand.cpp
index a59ed61..2e059c8 100644
--- a/Source/WebCore/editing/DeleteSelectionCommand.cpp
+++ b/Source/WebCore/editing/DeleteSelectionCommand.cpp
@@ -297,18 +297,26 @@
 
 bool DeleteSelectionCommand::handleSpecialCaseBRDelete()
 {
+    Node* nodeAfterUpstreamStart = m_upstreamStart.computeNodeAfterPosition();
+    Node* nodeAfterDownstreamStart = m_downstreamStart.computeNodeAfterPosition();
+    // Upstream end will appear before BR due to canonicalization
+    Node* nodeAfterUpstreamEnd = m_upstreamEnd.computeNodeAfterPosition();
+
+    if (!nodeAfterUpstreamStart || !nodeAfterDownstreamStart)
+        return false;
+
     // Check for special-case where the selection contains only a BR on a line by itself after another BR.
-    bool upstreamStartIsBR = m_upstreamStart.deprecatedNode()->hasTagName(brTag);
-    bool downstreamStartIsBR = m_downstreamStart.deprecatedNode()->hasTagName(brTag);
-    bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && m_downstreamStart.deprecatedNode() == m_upstreamEnd.deprecatedNode();
+    bool upstreamStartIsBR = nodeAfterUpstreamStart->hasTagName(brTag);
+    bool downstreamStartIsBR = nodeAfterDownstreamStart->hasTagName(brTag);
+    bool isBROnLineByItself = upstreamStartIsBR && downstreamStartIsBR && nodeAfterDownstreamStart == nodeAfterUpstreamEnd;
     if (isBROnLineByItself) {
-        removeNode(m_downstreamStart.deprecatedNode());
+        removeNode(nodeAfterDownstreamStart);
         return true;
     }
 
-    // Not a special-case delete per se, but we can detect that the merging of content between blocks
-    // should not be done.
-    if (upstreamStartIsBR && downstreamStartIsBR) {
+    // FIXME: This code doesn't belong in here.
+    // We detect the case where the start is an empty line consisting of BR not wrapped in a block element.
+    if (upstreamStartIsBR && downstreamStartIsBR && !(isStartOfBlock(positionBeforeNode(nodeAfterUpstreamStart)) && isEndOfBlock(positionAfterNode(nodeAfterUpstreamStart)))) {
         m_startsAtEmptyLine = true;
         m_endingPosition = m_downstreamEnd;
     }
diff --git a/Source/WebCore/editing/htmlediting.cpp b/Source/WebCore/editing/htmlediting.cpp
index 7d8fc39..d782d17 100644
--- a/Source/WebCore/editing/htmlediting.cpp
+++ b/Source/WebCore/editing/htmlediting.cpp
@@ -78,15 +78,15 @@
     if (!commonScope)
         return 0;
 
-    Node* nodeA = commonScope->ancestorInThisScope(a.deprecatedNode());
+    Node* nodeA = commonScope->ancestorInThisScope(a.containerNode());
     ASSERT(nodeA);
-    bool hasDescendentA = nodeA != a.deprecatedNode();
-    int offsetA = hasDescendentA ? 0 : a.deprecatedEditingOffset();
+    bool hasDescendentA = nodeA != a.containerNode();
+    int offsetA = hasDescendentA ? 0 : a.computeOffsetInContainerNode();
 
-    Node* nodeB = commonScope->ancestorInThisScope(b.deprecatedNode());
+    Node* nodeB = commonScope->ancestorInThisScope(b.containerNode());
     ASSERT(nodeB);
-    bool hasDescendentB = nodeB != b.deprecatedNode();
-    int offsetB = hasDescendentB ? 0 : b.deprecatedEditingOffset();
+    bool hasDescendentB = nodeB != b.containerNode();
+    int offsetB = hasDescendentB ? 0 : b.computeOffsetInContainerNode();
 
     int bias = 0;
     if (nodeA == nodeB) {