Bug 755200 - Simplify SetSelectionAroundHeadChildren; r=ehsan
authorMs2ger <ms2ger@gmail.com>
Fri, 18 May 2012 10:29:39 +0200
changeset 94314 ab544a056d6b02317f7e2bbcaaad39b87f864ffa
parent 94313 c5d2792ed0f4a05af24d023ea86ece657dbc38a4
child 94315 1dac5c37d56020773a6ecf7470d5cd7385ce1d81
push id22706
push userMs2ger@gmail.com
push dateFri, 18 May 2012 08:31:47 +0000
treeherdermozilla-central@4b74c82dd5ab [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs755200
milestone15.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 755200 - Simplify SetSelectionAroundHeadChildren; r=ehsan
content/base/public/nsISelection.idl
content/base/public/nsISelectionPrivate.idl
editor/libeditor/html/nsHTMLEditor.cpp
layout/generic/nsSelection.cpp
--- a/content/base/public/nsISelection.idl
+++ b/content/base/public/nsISelection.idl
@@ -45,17 +45,17 @@ interface nsINode;
 
 /**
  * Interface for manipulating and querying the current selected range
  * of nodes within the document.
  *
  * @version 1.0
  */
 
-[scriptable, uuid(7f30b791-9f43-4bba-9ca3-079dc2ea7479)]
+[scriptable, uuid(dd40d5b8-1fe1-487f-b66e-28f4b837024f)]
 interface nsISelection : nsISupports
 {
     /**
      * Returns the node in which the selection begins.
      */
     readonly attribute nsIDOMNode anchorNode;
 
     /**
@@ -102,16 +102,17 @@ interface nsISelection : nsISupports
     /**
      * Extends the selection by moving the selection end to the specified node and offset,
      * preserving the selection begin position. The new selection end result will always
      * be from the anchorNode to the new focusNode, regardless of direction.
      * @param parentNode      The node where the selection will be extended to
      * @param offset          Where in node to place the offset in the new selection end
      */
     void extend(in nsIDOMNode parentNode, in long offset);
+    [noscript] void extendNative(in nsINode parentNode, in long offset);
 
     /**
      * Collapses the whole selection to a single point at the start
      * of the current selection (irrespective of direction).  If content
      * is focused and editable, the caret will blink there.
      */
     void collapseToStart();
 
--- a/content/base/public/nsISelectionPrivate.idl
+++ b/content/base/public/nsISelectionPrivate.idl
@@ -61,17 +61,17 @@ struct ScrollAxis;
 [ptr] native nsIFrame(nsIFrame);
 [ptr] native nsIPresShell(nsIPresShell);
 [ptr] native RangeArray(nsTArray<nsRange*>);
 [ref] native constTextRangeStyleRef(const nsTextRangeStyle);
 [ref] native nsPointRef(nsPoint);
 native nsDirection(nsDirection);
 native ScrollAxis(nsIPresShell::ScrollAxis);
 
-[scriptable, uuid(0733a4dc-2801-4752-a489-b68c918a7ccb)]
+[scriptable, uuid(719a803f-aa1e-436c-8919-c42908f00599)]
 interface nsISelectionPrivate : nsISelection
  {
     const short ENDOFPRECEDINGLINE=0;
     const short STARTOFNEXTLINE=1;
     
     attribute boolean interlinePosition;
 
     /* startBatchChanges
--- a/editor/libeditor/html/nsHTMLEditor.cpp
+++ b/editor/libeditor/html/nsHTMLEditor.cpp
@@ -3579,50 +3579,34 @@ nsHTMLEditor::IsModifiableNode(nsIDOMNod
 }
 
 bool
 nsHTMLEditor::IsModifiableNode(nsINode *aNode)
 {
   return !aNode || aNode->IsEditable();
 }
 
-static nsresult SetSelectionAroundHeadChildren(nsCOMPtr<nsISelection> aSelection, nsWeakPtr aDocWeak)
+static nsresult
+SetSelectionAroundHeadChildren(nsISelection* aSelection,
+                               nsIWeakReference* aDocWeak)
 {
-  nsresult res = NS_OK;
   // Set selection around <head> node
-  nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(aDocWeak);
+  nsCOMPtr<nsIDocument> doc = do_QueryReferent(aDocWeak);
   NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
 
-  nsCOMPtr<nsIDOMNodeList>nodeList; 
-  res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList));
-  NS_ENSURE_SUCCESS(res, res);
-  NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
-
-  PRUint32 count; 
-  nodeList->GetLength(&count);
-  if (count < 1) return NS_ERROR_FAILURE;
-
-  nsCOMPtr<nsIDOMNode> headNode;
-  res = nodeList->Item(0, getter_AddRefs(headNode)); 
-  NS_ENSURE_SUCCESS(res, res);
-  NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
+  dom::Element* headNode = doc->GetHeadElement();
+  NS_ENSURE_STATE(headNode);
 
   // Collapse selection to before first child of the head,
-  res = aSelection->Collapse(headNode, 0);
-  NS_ENSURE_SUCCESS(res, res);
-
-  //  then extend it to just after
-  nsCOMPtr<nsIDOMNodeList> childNodes;
-  res = headNode->GetChildNodes(getter_AddRefs(childNodes));
-  NS_ENSURE_SUCCESS(res, res);
-  NS_ENSURE_TRUE(childNodes, NS_ERROR_NULL_POINTER);
-  PRUint32 childCount;
-  childNodes->GetLength(&childCount);
-
-  return aSelection->Extend(headNode, childCount+1);
+  nsresult rv = aSelection->CollapseNative(headNode, 0);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // Then extend it to just after.
+  PRUint32 childCount = headNode->GetChildCount();
+  return aSelection->ExtendNative(headNode, childCount + 1);
 }
 
 NS_IMETHODIMP
 nsHTMLEditor::GetHeadContentsAsHTML(nsAString& aOutputString)
 {
   nsCOMPtr<nsISelection> selection;
   nsresult res = GetSelection(getter_AddRefs(selection));
   NS_ENSURE_SUCCESS(res, res);
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -4884,16 +4884,22 @@ 2  1  a = continue selection from 2 to 1
  */
 NS_IMETHODIMP
 nsTypedSelection::Extend(nsIDOMNode* aParentNode, PRInt32 aOffset)
 {
   nsCOMPtr<nsINode> parentNode = do_QueryInterface(aParentNode);
   return Extend(parentNode, aOffset);
 }
 
+NS_IMETHODIMP
+nsTypedSelection::ExtendNative(nsINode* aParentNode, PRInt32 aOffset)
+{
+  return Extend(aParentNode, aOffset);
+}
+
 nsresult
 nsTypedSelection::Extend(nsINode* aParentNode, PRInt32 aOffset)
 {
   if (!aParentNode)
     return NS_ERROR_INVALID_ARG;
 
   // First, find the range containing the old focus point:
   if (!mAnchorFocusRange)