Bug 769967 part 1 - Clean up nsEditor::GetChildOffset; r=ehsan

This commit is contained in:
Aryeh Gregor 2012-07-02 14:42:19 +03:00
parent 355d4ca236
commit e44ac36034
6 changed files with 40 additions and 74 deletions

View File

@ -1463,16 +1463,15 @@ nsEditor::JoinNodes(nsIDOMNode * aLeftNode,
nsIDOMNode * aRightNode,
nsIDOMNode * aParent)
{
PRInt32 i, offset;
PRInt32 i;
nsAutoRules beginRulesSniffing(this, kOpJoinNode, nsIEditor::ePrevious);
// remember some values; later used for saved selection updating.
// find the offset between the nodes to be joined.
nsresult result = GetChildOffset(aRightNode, aParent, offset);
NS_ENSURE_SUCCESS(result, result);
PRInt32 offset = GetChildOffset(aRightNode, aParent);
// find the number of children of the lefthand node
PRUint32 oldLeftNodeLen;
result = GetLengthOfDOMNode(aLeftNode, oldLeftNodeLen);
nsresult result = GetLengthOfDOMNode(aLeftNode, oldLeftNodeLen);
NS_ENSURE_SUCCESS(result, result);
for (i = 0; i < mActionListeners.Count(); i++)
@ -3132,18 +3131,18 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep,
}
nsresult
nsEditor::GetChildOffset(nsIDOMNode *aChild, nsIDOMNode *aParent, PRInt32 &aOffset)
PRInt32
nsEditor::GetChildOffset(nsIDOMNode* aChild, nsIDOMNode* aParent)
{
NS_ASSERTION((aChild && aParent), "bad args");
MOZ_ASSERT(aChild && aParent);
nsCOMPtr<nsIContent> content = do_QueryInterface(aParent);
nsCOMPtr<nsIContent> cChild = do_QueryInterface(aChild);
NS_ENSURE_TRUE(cChild && content, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
nsCOMPtr<nsINode> child = do_QueryInterface(aChild);
MOZ_ASSERT(parent && child);
aOffset = content->IndexOf(cChild);
return NS_OK;
PRInt32 idx = parent->IndexOf(child);
MOZ_ASSERT(idx != -1);
return idx;
}
nsresult
@ -3156,7 +3155,7 @@ nsEditor::GetNodeLocation(nsIDOMNode *inChild, nsCOMPtr<nsIDOMNode> *outParent,
result = inChild->GetParentNode(getter_AddRefs(*outParent));
if ((NS_SUCCEEDED(result)) && (*outParent))
{
result = GetChildOffset(inChild, *outParent, *outOffset);
*outOffset = GetChildOffset(inChild, *outParent);
}
}
return result;
@ -4935,9 +4934,7 @@ nsEditor::AppendNodeToSelectionAsRange(nsIDOMNode *aNode)
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(parentNode, NS_ERROR_NULL_POINTER);
PRInt32 offset;
res = GetChildOffset(aNode, parentNode, offset);
NS_ENSURE_SUCCESS(res, res);
PRInt32 offset = GetChildOffset(aNode, parentNode);
nsCOMPtr<nsIDOMRange> range;
res = CreateRange(parentNode, offset, parentNode, offset+1, getter_AddRefs(range));

View File

@ -436,12 +436,11 @@ public:
bool aNodeToKeepIsFirst);
/**
* Set aOffset to the offset of aChild in aParent.
* Returns an error if aChild is not an immediate child of aParent.
* Return the offset of aChild in aParent. Asserts fatally if parent or
* child is null, or parent is not child's parent.
*/
static nsresult GetChildOffset(nsIDOMNode *aChild,
nsIDOMNode *aParent,
PRInt32 &aOffset);
static PRInt32 GetChildOffset(nsIDOMNode *aChild,
nsIDOMNode *aParent);
/**
* Set aParent to the parent of aChild.

View File

@ -14,9 +14,9 @@ Private Editor interface for a class that can provide helper functions
*/
#define NS_IEDITORSUPPORT_IID \
{/* 89b999b0-c529-11d2-86da-000064657374*/ \
0x89b999b0, 0xc529, 0x11d2, \
{0x86, 0xda, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74} }
{/* c4cbcda8-58ec-4f03-9c99-5e46b6828b7a*/ \
0xc4cbcda8, 0x58ec, 0x4f03, \
{0x0c, 0x99, 0x5e, 0x46, 0xb6, 0x82, 0x8b, 0x7a} }
/**
@ -53,10 +53,7 @@ public:
nsIDOMNode *aParent,
bool aNodeToKeepIsFirst)=0;
static nsresult GetChildOffset(nsIDOMNode *aChild, nsIDOMNode *aParent, PRInt32 &aOffset);
static PRInt32 GetChildOffset(nsIDOMNode* aChild, nsIDOMNode* aParent);
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsIEditorSupport, NS_IEDITORSUPPORT_IID)

View File

@ -1659,17 +1659,13 @@ nsHTMLEditor::SelectElement(nsIDOMElement* aElement)
res = aElement->GetParentNode(getter_AddRefs(parent));
if (NS_SUCCEEDED(res) && parent)
{
PRInt32 offsetInParent;
res = GetChildOffset(aElement, parent, offsetInParent);
PRInt32 offsetInParent = GetChildOffset(aElement, parent);
if (NS_SUCCEEDED(res))
{
// Collapse selection to just before desired element,
res = selection->Collapse(parent, offsetInParent);
if (NS_SUCCEEDED(res)) {
// then extend it to just after
res = selection->Extend(parent, offsetInParent+1);
}
// Collapse selection to just before desired element,
res = selection->Collapse(parent, offsetInParent);
if (NS_SUCCEEDED(res)) {
// then extend it to just after
res = selection->Extend(parent, offsetInParent + 1);
}
}
}
@ -1691,26 +1687,9 @@ nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement)
res = aElement->GetParentNode(getter_AddRefs(parent));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
PRInt32 offsetInParent;
res = GetChildOffset(aElement, parent, offsetInParent);
if (NS_SUCCEEDED(res))
{
// Collapse selection to just after desired element,
res = selection->Collapse(parent, offsetInParent+1);
#if 0 //def DEBUG_cmanske
{
nsAutoString name;
parent->GetNodeName(name);
printf("SetCaretAfterElement: Parent node: ");
wprintf(name.get());
printf(" Offset: %d\n\nHTML:\n", offsetInParent+1);
nsAutoString Format("text/html");
nsAutoString ContentsAs;
OutputToString(Format, 2, ContentsAs);
wprintf(ContentsAs.get());
}
#endif
}
PRInt32 offsetInParent = GetChildOffset(aElement, parent);
// Collapse selection to just after desired element,
res = selection->Collapse(parent, offsetInParent + 1);
}
return res;
}

View File

@ -1035,8 +1035,7 @@ bool nsHTMLEditor::IsAtFrontOfNode(nsIDOMNode *aNode, PRInt32 aOffset)
nsCOMPtr<nsIDOMNode> firstNode;
GetFirstEditableChild(aNode, address_of(firstNode));
NS_ENSURE_TRUE(firstNode, true);
PRInt32 offset;
nsEditor::GetChildOffset(firstNode, aNode, offset);
PRInt32 offset = GetChildOffset(firstNode, aNode);
if (offset < aOffset) return false;
return true;
}
@ -1058,8 +1057,7 @@ bool nsHTMLEditor::IsAtEndOfNode(nsIDOMNode *aNode, PRInt32 aOffset)
nsCOMPtr<nsIDOMNode> lastNode;
GetLastEditableChild(aNode, address_of(lastNode));
NS_ENSURE_TRUE(lastNode, true);
PRInt32 offset;
nsEditor::GetChildOffset(lastNode, aNode, offset);
PRInt32 offset = GetChildOffset(lastNode, aNode);
if (offset < aOffset) return true;
return false;
}

View File

@ -97,10 +97,7 @@ nsHTMLEditor::InsertCell(nsIDOMElement *aCell, PRInt32 aRowSpan, PRInt32 aColSpa
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(cellParent, NS_ERROR_NULL_POINTER);
PRInt32 cellOffset;
res = GetChildOffset(aCell, cellParent, cellOffset);
NS_ENSURE_SUCCESS(res, res);
PRInt32 cellOffset = GetChildOffset(aCell, cellParent);
nsCOMPtr<nsIDOMElement> newCell;
if (aIsHeader)
@ -660,8 +657,7 @@ nsHTMLEditor::InsertTableRow(PRInt32 aNumber, bool aAfter)
parentRow->GetParentNode(getter_AddRefs(parentOfRow));
NS_ENSURE_TRUE(parentOfRow, NS_ERROR_NULL_POINTER);
res = GetChildOffset(parentRow, parentOfRow, newRowOffset);
NS_ENSURE_SUCCESS(res, res);
newRowOffset = GetChildOffset(parentRow, parentOfRow);
// Adjust for when adding past the end
if (aAfter && startRowIndex >= rowCount)
@ -2870,8 +2866,9 @@ nsHTMLEditor::GetCellContext(nsISelection **aSelection,
*aCellParent = cellParent.get();
NS_ADDREF(*aCellParent);
if (aCellOffset)
res = GetChildOffset(cell, cellParent, *aCellOffset);
if (aCellOffset) {
*aCellOffset = GetChildOffset(cell, cellParent);
}
}
return res;
@ -3139,12 +3136,11 @@ nsHTMLEditor::SetSelectionAfterTableEdit(nsIDOMElement* aTable, PRInt32 aRow, PR
// We didn't find a cell
// Set selection to just before the table
nsCOMPtr<nsIDOMNode> tableParent;
PRInt32 tableOffset;
res = aTable->GetParentNode(getter_AddRefs(tableParent));
if(NS_SUCCEEDED(res) && tableParent)
{
if(NS_SUCCEEDED(GetChildOffset(aTable, tableParent, tableOffset)))
return selection->Collapse(tableParent, tableOffset);
PRInt32 tableOffset = GetChildOffset(aTable, tableParent);
return selection->Collapse(tableParent, tableOffset);
}
// Last resort: Set selection to start of doc
// (it's very bad to not have a valid selection!)