Bug 1088054 part 8 - Clean up nsHTMLEditor::GetFirst/LastEditableLeaf; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-11-02 14:04:13 +02:00
parent 9ac29614a9
commit 9fe7e46891
4 changed files with 29 additions and 64 deletions

View File

@ -629,8 +629,9 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString,
// but don't cross tables // but don't cross tables
if (!nsHTMLEditUtils::IsTable(lastInsertNode)) if (!nsHTMLEditUtils::IsTable(lastInsertNode))
{ {
rv = GetLastEditableLeaf(lastInsertNode, address_of(selNode)); nsCOMPtr<nsINode> lastInsertNode_ = do_QueryInterface(lastInsertNode);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_STATE(lastInsertNode_ || !lastInsertNode);
selNode = GetAsDOMNode(GetLastEditableLeaf(*lastInsertNode_));
tmp = selNode; tmp = selNode;
while (tmp && (tmp != lastInsertNode)) while (tmp && (tmp != lastInsertNode))
{ {

View File

@ -2203,16 +2203,14 @@ nsHTMLEditRules::WillDeleteSelection(Selection* aSelection,
if (aAction == nsIEditor::ePrevious) if (aAction == nsIEditor::ePrevious)
{ {
NS_ENSURE_STATE(mHTMLEditor); NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetLastEditableLeaf( visNode, address_of(leafNode)); leafNode = GetAsDOMNode(mHTMLEditor->GetLastEditableLeaf(*visNode_));
NS_ENSURE_SUCCESS(res, res);
leftNode = leafNode; leftNode = leafNode;
rightNode = startNode; rightNode = startNode;
} }
else else
{ {
NS_ENSURE_STATE(mHTMLEditor); NS_ENSURE_STATE(mHTMLEditor);
res = mHTMLEditor->GetFirstEditableLeaf( visNode, address_of(leafNode)); leafNode = GetAsDOMNode(mHTMLEditor->GetFirstEditableLeaf(*visNode_));
NS_ENSURE_SUCCESS(res, res);
leftNode = startNode; leftNode = startNode;
rightNode = leafNode; rightNode = leafNode;
} }

View File

@ -4188,71 +4188,37 @@ nsHTMLEditor::GetLastEditableChild(nsINode& aNode)
return child; return child;
} }
nsresult nsIContent*
nsHTMLEditor::GetFirstEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstLeaf) nsHTMLEditor::GetFirstEditableLeaf(nsINode& aNode)
{ {
// check parms nsCOMPtr<nsIContent> child = GetLeftmostChild(&aNode);
nsCOMPtr<nsINode> node = do_QueryInterface(aNode); while (child && (!IsEditable(child) || child->HasChildren())) {
NS_ENSURE_TRUE(node && aOutFirstLeaf, NS_ERROR_NULL_POINTER); child = GetNextHTMLNode(child);
// init out parms // Only accept nodes that are descendants of aNode
*aOutFirstLeaf = aNode; if (!aNode.Contains(child)) {
return nullptr;
// find leftmost leaf
nsresult res = NS_OK;
nsCOMPtr<nsIDOMNode> child = GetAsDOMNode(GetLeftmostChild(node));
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
{
nsCOMPtr<nsIDOMNode> tmp;
res = GetNextHTMLNode(child, address_of(tmp));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
// only accept nodes that are descendants of aNode
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
child = tmp;
else
{
child = nullptr; // this will abort the loop
} }
} }
*aOutFirstLeaf = child; return child;
return res;
} }
nsresult nsIContent*
nsHTMLEditor::GetLastEditableLeaf(nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastLeaf) nsHTMLEditor::GetLastEditableLeaf(nsINode& aNode)
{ {
// check parms nsCOMPtr<nsIContent> child = GetRightmostChild(&aNode, false);
nsCOMPtr<nsINode> node = do_QueryInterface(aNode); while (child && (!IsEditable(child) || child->HasChildren())) {
NS_ENSURE_TRUE(node && aOutLastLeaf, NS_ERROR_NULL_POINTER); child = GetPriorHTMLNode(child);
// init out parms // Only accept nodes that are descendants of aNode
*aOutLastLeaf = nullptr; if (!aNode.Contains(child)) {
return nullptr;
// find rightmost leaf
nsCOMPtr<nsIDOMNode> child = GetAsDOMNode(GetRightmostChild(node, false));
nsresult res = NS_OK;
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
{
nsCOMPtr<nsIDOMNode> tmp;
res = GetPriorHTMLNode(child, address_of(tmp));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
// only accept nodes that are descendants of aNode
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
child = tmp;
else
{
child = nullptr;
} }
} }
*aOutLastLeaf = child; return child;
return res;
} }

View File

@ -715,8 +715,8 @@ protected:
nsIContent* GetFirstEditableChild(nsINode& aNode); nsIContent* GetFirstEditableChild(nsINode& aNode);
nsIContent* GetLastEditableChild(nsINode& aNode); nsIContent* GetLastEditableChild(nsINode& aNode);
nsresult GetFirstEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstLeaf); nsIContent* GetFirstEditableLeaf(nsINode& aNode);
nsresult GetLastEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastLeaf); nsIContent* GetLastEditableLeaf(nsINode& aNode);
nsresult GetInlinePropertyBase(nsIAtom *aProperty, nsresult GetInlinePropertyBase(nsIAtom *aProperty,
const nsAString *aAttribute, const nsAString *aAttribute,