Bug 1068979 - Handle surrogate pair if necessary when backspacing into a text node. r=ehsan
authorJonathan Kew <jkew@mozilla.com>
Mon, 22 Sep 2014 22:15:02 +0100
changeset 206595 8e12b1e8a2f51f06f11a6c2cff24fcf7493a1ed6
parent 206594 0c50a30b5be7d1ddfd06fc530a1daf0533bc62f0
child 206596 034dc794140bf5c43b909fd7749648cb4424ccf6
push idunknown
push userunknown
push dateunknown
reviewersehsan
bugs1068979
milestone35.0a1
Bug 1068979 - Handle surrogate pair if necessary when backspacing into a text node. r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -2012,23 +2012,32 @@ nsHTMLEditRules::WillDeleteSelection(Sel
       else
         res = wsObj.DeleteWSBackward();
       *aHandled = true;
       NS_ENSURE_SUCCESS(res, res);
       res = InsertBRIfNeeded(aSelection);
       return res;
     } else if (wsType == WSType::text) {
       // found normal text to delete.  
+      nsRefPtr<Text> nodeAsText = visNode_->GetAsText();
       int32_t so = visOffset;
       int32_t eo = visOffset+1;
       if (aAction == nsIEditor::ePrevious) 
       { 
         if (so == 0) return NS_ERROR_UNEXPECTED;
-        so--; 
-        eo--; 
+        so--;
+        eo--;
+        // bug 1068979: delete both codepoints if surrogate pair
+        if (so > 0) {
+          const nsTextFragment *text = nodeAsText->GetText();
+          if (NS_IS_LOW_SURROGATE(text->CharAt(so)) &&
+              NS_IS_HIGH_SURROGATE(text->CharAt(so - 1))) {
+            so--;
+          }
+        }
       }
       else
       {
         nsCOMPtr<nsIDOMRange> range;
         res = aSelection->GetRangeAt(0, getter_AddRefs(range));
         NS_ENSURE_SUCCESS(res, res);
 
 #ifdef DEBUG
@@ -2048,17 +2057,16 @@ nsHTMLEditRules::WillDeleteSelection(Sel
         res = range->GetEndOffset(&eo);
         NS_ENSURE_SUCCESS(res, res);
       }
       NS_ENSURE_STATE(mHTMLEditor);
       res = nsWSRunObject::PrepareToDeleteRange(mHTMLEditor,
           address_of(visNode_), &so, address_of(visNode_), &eo);
       NS_ENSURE_SUCCESS(res, res);
       visNode = GetAsDOMNode(visNode_);
-      nsRefPtr<Text> nodeAsText = visNode_->GetAsText();
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->DeleteText(*nodeAsText, std::min(so, eo),
                                     DeprecatedAbs(eo - so));
       *aHandled = true;
       NS_ENSURE_SUCCESS(res, res);    
       res = InsertBRIfNeeded(aSelection);
       return res;
     } else if (wsType == WSType::special || wsType == WSType::br ||