diff --git a/editor/libeditor/html/nsHTMLDataTransfer.cpp b/editor/libeditor/html/nsHTMLDataTransfer.cpp index 21bf670db4ce..0413b6498779 100644 --- a/editor/libeditor/html/nsHTMLDataTransfer.cpp +++ b/editor/libeditor/html/nsHTMLDataTransfer.cpp @@ -149,7 +149,9 @@ static nsCOMPtr GetListParent(nsIDOMNode* aNode) aNode->GetParentNode(getter_AddRefs(parent)); while (parent) { - if (nsHTMLEditUtils::IsList(parent)) return parent; + if (nsHTMLEditUtils::IsList(parent)) { + return parent; + } parent->GetParentNode(getter_AddRefs(tmp)); parent = tmp; } @@ -163,7 +165,9 @@ static nsCOMPtr GetTableParent(nsIDOMNode* aNode) aNode->GetParentNode(getter_AddRefs(parent)); while (parent) { - if (nsHTMLEditUtils::IsTable(parent)) return parent; + if (nsHTMLEditUtils::IsTable(parent)) { + return parent; + } parent->GetParentNode(getter_AddRefs(tmp)); parent = tmp; } @@ -179,62 +183,65 @@ NS_IMETHODIMP nsHTMLEditor::LoadHTML(const nsAString & aInputString) ForceCompositionEnd(); nsAutoEditBatch beginBatching(this); nsAutoRules beginRulesSniffing(this, kOpLoadHTML, nsIEditor::eNext); - + // Get selection nsCOMPtrselection; - nsresult res = GetSelection(getter_AddRefs(selection)); - NS_ENSURE_SUCCESS(res, res); - + nsresult rv = GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, rv); + nsTextRulesInfo ruleInfo(nsTextEditRules::kLoadHTML); bool cancel, handled; - res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); - NS_ENSURE_SUCCESS(res, res); - if (cancel) return NS_OK; // rules canceled the operation + rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + NS_ENSURE_SUCCESS(rv, rv); + if (cancel) { + return NS_OK; // rules canceled the operation + } + if (!handled) { bool isCollapsed; - res = selection->GetIsCollapsed(&isCollapsed); - NS_ENSURE_SUCCESS(res, res); + rv = selection->GetIsCollapsed(&isCollapsed); + NS_ENSURE_SUCCESS(rv, rv); // Delete Selection, but only if it isn't collapsed, see bug #106269 if (!isCollapsed) { - res = DeleteSelection(eNone); - NS_ENSURE_SUCCESS(res, res); + rv = DeleteSelection(eNone); + NS_ENSURE_SUCCESS(rv, rv); } // Get the first range in the selection, for context: nsCOMPtr range; - res = selection->GetRangeAt(0, getter_AddRefs(range)); - NS_ENSURE_SUCCESS(res, res); + rv = selection->GetRangeAt(0, getter_AddRefs(range)); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER); // create fragment for pasted html nsCOMPtr docfrag; { - res = range->CreateContextualFragment(aInputString, getter_AddRefs(docfrag)); - NS_ENSURE_SUCCESS(res, res); + rv = range->CreateContextualFragment(aInputString, getter_AddRefs(docfrag)); + NS_ENSURE_SUCCESS(rv, rv); } // put the fragment into the document nsCOMPtr parent, junk; - res = range->GetStartContainer(getter_AddRefs(parent)); - NS_ENSURE_SUCCESS(res, res); + rv = range->GetStartContainer(getter_AddRefs(parent)); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER); PRInt32 childOffset; - res = range->GetStartOffset(&childOffset); - NS_ENSURE_SUCCESS(res, res); + rv = range->GetStartOffset(&childOffset); + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr nodeToInsert; docfrag->GetFirstChild(getter_AddRefs(nodeToInsert)); while (nodeToInsert) { - res = InsertNode(nodeToInsert, parent, childOffset++); - NS_ENSURE_SUCCESS(res, res); + rv = InsertNode(nodeToInsert, parent, childOffset++); + NS_ENSURE_SUCCESS(rv, rv); docfrag->GetFirstChild(getter_AddRefs(nodeToInsert)); } } - return mRules->DidDoAction(selection, &ruleInfo, res); + return mRules->DidDoAction(selection, &ruleInfo, rv); } @@ -279,25 +286,24 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, ForceCompositionEnd(); nsAutoEditBatch beginBatching(this); nsAutoRules beginRulesSniffing(this, kOpHTMLPaste, nsIEditor::eNext); - + // Get selection - nsresult res; nsCOMPtrselection; - res = GetSelection(getter_AddRefs(selection)); - NS_ENSURE_SUCCESS(res, res); - + nsresult rv = GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, rv); + // create a dom document fragment that represents the structure to paste nsCOMPtr fragmentAsNode, streamStartParent, streamEndParent; PRInt32 streamStartOffset = 0, streamEndOffset = 0; - res = CreateDOMFragmentFromPaste(aInputString, aContextStr, aInfoStr, - address_of(fragmentAsNode), - address_of(streamStartParent), - address_of(streamEndParent), - &streamStartOffset, - &streamEndOffset, - aTrustedInput); - NS_ENSURE_SUCCESS(res, res); + rv = CreateDOMFragmentFromPaste(aInputString, aContextStr, aInfoStr, + address_of(fragmentAsNode), + address_of(streamStartParent), + address_of(streamEndParent), + &streamStartOffset, + &streamEndOffset, + aTrustedInput); + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr targetNode, tempNode; PRInt32 targetOffset=0; @@ -306,9 +312,9 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, { // if caller didn't provide the destination/target node, // fetch the paste insertion point from our selection - res = GetStartNodeAndOffset(selection, getter_AddRefs(targetNode), &targetOffset); - if (!targetNode) res = NS_ERROR_FAILURE; - NS_ENSURE_SUCCESS(res, res); + rv = GetStartNodeAndOffset(selection, getter_AddRefs(targetNode), &targetOffset); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(targetNode, NS_ERROR_FAILURE); } else { @@ -318,16 +324,16 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, bool doContinue = true; - res = DoContentFilterCallback(aFlavor, aSourceDoc, aDeleteSelection, - (nsIDOMNode **)address_of(fragmentAsNode), - (nsIDOMNode **)address_of(streamStartParent), - &streamStartOffset, - (nsIDOMNode **)address_of(streamEndParent), - &streamEndOffset, - (nsIDOMNode **)address_of(targetNode), - &targetOffset, &doContinue); + rv = DoContentFilterCallback(aFlavor, aSourceDoc, aDeleteSelection, + (nsIDOMNode **)address_of(fragmentAsNode), + (nsIDOMNode **)address_of(streamStartParent), + &streamStartOffset, + (nsIDOMNode **)address_of(streamEndParent), + &streamEndOffset, + (nsIDOMNode **)address_of(targetNode), + &targetOffset, &doContinue); - NS_ENSURE_SUCCESS(res, res); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(doContinue, NS_OK); // if we have a destination / target node, we want to insert there @@ -343,12 +349,12 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // Use an auto tracker so that our drop point is correctly // positioned after the delete. nsAutoTrackDOMPoint tracker(mRangeUpdater, &targetNode, &targetOffset); - res = DeleteSelection(eNone); - NS_ENSURE_SUCCESS(res, res); + rv = DeleteSelection(eNone); + NS_ENSURE_SUCCESS(rv, rv); } - res = selection->Collapse(targetNode, targetOffset); - NS_ENSURE_SUCCESS(res, res); + rv = selection->Collapse(targetNode, targetOffset); + NS_ENSURE_SUCCESS(rv, rv); } // we need to recalculate various things based on potentially new offsets @@ -356,28 +362,29 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // make a list of what nodes in docFrag we need to move nsCOMArray nodeList; - res = CreateListOfNodesToPaste(fragmentAsNode, nodeList, - streamStartParent, streamStartOffset, - streamEndParent, streamEndOffset); - NS_ENSURE_SUCCESS(res, res); + rv = CreateListOfNodesToPaste(fragmentAsNode, nodeList, + streamStartParent, streamStartOffset, + streamEndParent, streamEndOffset); + NS_ENSURE_SUCCESS(rv, rv); - if (nodeList.Count() == 0) + if (nodeList.Count() == 0) { return NS_OK; + } - // are there any table elements in the list? + // Are there any table elements in the list? // node and offset for insertion nsCOMPtr parentNode; PRInt32 offsetOfNewNode; - + // check for table cell selection mode bool cellSelectionMode = false; nsCOMPtr cell; - res = GetFirstSelectedCell(nsnull, getter_AddRefs(cell)); - if (NS_SUCCEEDED(res) && cell) + rv = GetFirstSelectedCell(nsnull, getter_AddRefs(cell)); + if (NS_SUCCEEDED(rv) && cell) { cellSelectionMode = true; } - + if (cellSelectionMode) { // do we have table content to paste? If so, we want to delete @@ -392,40 +399,42 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, if (!cellSelectionMode) { - res = DeleteSelectionAndPrepareToCreateNode(parentNode, offsetOfNewNode); - NS_ENSURE_SUCCESS(res, res); + rv = DeleteSelectionAndPrepareToCreateNode(parentNode, offsetOfNewNode); + NS_ENSURE_SUCCESS(rv, rv); // pasting does not inherit local inline styles - res = RemoveAllInlineProperties(); - NS_ENSURE_SUCCESS(res, res); + rv = RemoveAllInlineProperties(); + NS_ENSURE_SUCCESS(rv, rv); } else { // delete whole cells: we will replace with new table content - if (1) - { + { // Braces for artificial block to scope nsAutoSelectionReset. // Save current selection since DeleteTableCell perturbs it nsAutoSelectionReset selectionResetter(selection, this); - res = DeleteTableCell(1); - NS_ENSURE_SUCCESS(res, res); + rv = DeleteTableCell(1); + NS_ENSURE_SUCCESS(rv, rv); } // collapse selection to beginning of deleted table content selection->CollapseToStart(); } - + // give rules a chance to handle or cancel nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement); bool cancel, handled; - res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); - NS_ENSURE_SUCCESS(res, res); - if (cancel) return NS_OK; // rules canceled the operation + rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + NS_ENSURE_SUCCESS(rv, rv); + if (cancel) { + return NS_OK; // rules canceled the operation + } + if (!handled) { - // The rules code (WillDoAction above) might have changed the selection. + // The rules code (WillDoAction above) might have changed the selection. // refresh our memory... - res = GetStartNodeAndOffset(selection, getter_AddRefs(parentNode), &offsetOfNewNode); - if (!parentNode) res = NS_ERROR_FAILURE; - NS_ENSURE_SUCCESS(res, res); + rv = GetStartNodeAndOffset(selection, getter_AddRefs(parentNode), &offsetOfNewNode); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(parentNode, NS_ERROR_FAILURE); // Adjust position based on the first node we are going to insert. NormalizeEOLInsertPosition(nodeList[0], address_of(parentNode), &offsetOfNewNode); @@ -437,36 +446,36 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, if (nsTextEditUtils::IsBreak(wsObj.mEndReasonNode) && !IsVisBreak(wsObj.mEndReasonNode) ) { - res = DeleteNode(wsObj.mEndReasonNode); - NS_ENSURE_SUCCESS(res, res); + rv = DeleteNode(wsObj.mEndReasonNode); + NS_ENSURE_SUCCESS(rv, rv); } - - // remeber if we are in a link. + + // Remember if we are in a link. bool bStartedInLink = IsInLink(parentNode); - - // are we in a text node? If so, split it. + + // Are we in a text node? If so, split it. if (IsTextNode(parentNode)) { nsCOMPtr temp; - res = SplitNodeDeep(parentNode, parentNode, offsetOfNewNode, &offsetOfNewNode); - NS_ENSURE_SUCCESS(res, res); - res = parentNode->GetParentNode(getter_AddRefs(temp)); - NS_ENSURE_SUCCESS(res, res); + rv = SplitNodeDeep(parentNode, parentNode, offsetOfNewNode, &offsetOfNewNode); + NS_ENSURE_SUCCESS(rv, rv); + rv = parentNode->GetParentNode(getter_AddRefs(temp)); + NS_ENSURE_SUCCESS(rv, rv); parentNode = temp; } // build up list of parents of first node in list that are either // lists or tables. First examine front of paste node list. nsCOMArray startListAndTableArray; - res = GetListAndTableParents(false, nodeList, startListAndTableArray); - NS_ENSURE_SUCCESS(res, res); - + rv = GetListAndTableParents(false, nodeList, startListAndTableArray); + NS_ENSURE_SUCCESS(rv, rv); + // remember number of lists and tables above us PRInt32 highWaterMark = -1; if (startListAndTableArray.Count() > 0) { - res = DiscoverPartialListsAndTables(nodeList, startListAndTableArray, &highWaterMark); - NS_ENSURE_SUCCESS(res, res); + rv = DiscoverPartialListsAndTables(nodeList, startListAndTableArray, &highWaterMark); + NS_ENSURE_SUCCESS(rv, rv); } // if we have pieces of tables or lists to be inserted, let's force the paste @@ -474,28 +483,28 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // table or list contents outside the table or list. if (highWaterMark >= 0) { - res = ReplaceOrphanedStructure(false, nodeList, startListAndTableArray, highWaterMark); - NS_ENSURE_SUCCESS(res, res); + rv = ReplaceOrphanedStructure(false, nodeList, startListAndTableArray, highWaterMark); + NS_ENSURE_SUCCESS(rv, rv); } - + // Now go through the same process again for the end of the paste node list. nsCOMArray endListAndTableArray; - res = GetListAndTableParents(true, nodeList, endListAndTableArray); - NS_ENSURE_SUCCESS(res, res); + rv = GetListAndTableParents(true, nodeList, endListAndTableArray); + NS_ENSURE_SUCCESS(rv, rv); highWaterMark = -1; - + // remember number of lists and tables above us if (endListAndTableArray.Count() > 0) { - res = DiscoverPartialListsAndTables(nodeList, endListAndTableArray, &highWaterMark); - NS_ENSURE_SUCCESS(res, res); + rv = DiscoverPartialListsAndTables(nodeList, endListAndTableArray, &highWaterMark); + NS_ENSURE_SUCCESS(rv, rv); } - + // don't orphan partial list or table structure if (highWaterMark >= 0) { - res = ReplaceOrphanedStructure(true, nodeList, endListAndTableArray, highWaterMark); - NS_ENSURE_SUCCESS(res, res); + rv = ReplaceOrphanedStructure(true, nodeList, endListAndTableArray, highWaterMark); + NS_ENSURE_SUCCESS(rv, rv); } // Loop over the node list and paste the nodes: @@ -506,7 +515,7 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, parentBlock = parentNode; else parentBlock = GetBlockNodeParent(parentNode); - + for (j=0; jGetFirstChild(getter_AddRefs(child)); while (child) { - res = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); - if (NS_FAILED(res)) + rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); + if (NS_FAILED(rv)) break; bDidInsert = true; @@ -558,13 +567,13 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, { if (nsHTMLEditUtils::IsListItem(child) || nsHTMLEditUtils::IsList(child)) { - // check if we are pasting into empty list item. If so + // Check if we are pasting into empty list item. If so // delete it and paste into parent list instead. if (nsHTMLEditUtils::IsListItem(parentNode)) { bool isEmpty; - res = IsEmptyNode(parentNode, &isEmpty, true); - if ((NS_SUCCEEDED(res)) && isEmpty) + rv = IsEmptyNode(parentNode, &isEmpty, true); + if (NS_SUCCEEDED(rv) && isEmpty) { nsCOMPtr listNode; PRInt32 newOffset; @@ -576,9 +585,9 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, offsetOfNewNode = newOffset; } } - } - res = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); - if (NS_FAILED(res)) + } + rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); + if (NS_FAILED(rv)) break; bDidInsert = true; @@ -591,17 +600,17 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, } curNode->GetFirstChild(getter_AddRefs(child)); } - + } - // check for pre's going into pre's. + // Check for pre's going into pre's. else if (nsHTMLEditUtils::IsPre(parentBlock) && nsHTMLEditUtils::IsPre(curNode)) { nsCOMPtr child, tmp; curNode->GetFirstChild(getter_AddRefs(child)); while (child) { - res = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); - if (NS_FAILED(res)) + rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true); + if (NS_FAILED(rv)) break; bDidInsert = true; @@ -612,11 +621,11 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, } } - if (!bDidInsert || NS_FAILED(res)) + if (!bDidInsert || NS_FAILED(rv)) { // try to insert - res = InsertNodeAtPoint(curNode, address_of(parentNode), &offsetOfNewNode, true); - if (NS_SUCCEEDED(res)) + rv = InsertNodeAtPoint(curNode, address_of(parentNode), &offsetOfNewNode, true); + if (NS_SUCCEEDED(rv)) { bDidInsert = true; lastInsertNode = curNode; @@ -625,13 +634,13 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // Assume failure means no legal parent in the document hierarchy, // try again with the parent of curNode in the paste hierarchy. nsCOMPtr parent; - while (NS_FAILED(res) && curNode) + while (NS_FAILED(rv) && curNode) { curNode->GetParentNode(getter_AddRefs(parent)); if (parent && !nsTextEditUtils::IsBody(parent)) { - res = InsertNodeAtPoint(parent, address_of(parentNode), &offsetOfNewNode, true); - if (NS_SUCCEEDED(res)) + rv = InsertNodeAtPoint(parent, address_of(parentNode), &offsetOfNewNode, true); + if (NS_SUCCEEDED(rv)) { bDidInsert = true; insertedContextParent = parent; @@ -643,8 +652,8 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, } if (lastInsertNode) { - res = GetNodeLocation(lastInsertNode, address_of(parentNode), &offsetOfNewNode); - NS_ENSURE_SUCCESS(res, res); + rv = GetNodeLocation(lastInsertNode, address_of(parentNode), &offsetOfNewNode); + NS_ENSURE_SUCCESS(rv, rv); offsetOfNewNode++; } } @@ -655,12 +664,12 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // set selection to the end of what we just pasted. nsCOMPtr selNode, tmp, visNode, highTable; PRInt32 selOffset; - + // but don't cross tables if (!nsHTMLEditUtils::IsTable(lastInsertNode)) { - res = GetLastEditableLeaf(lastInsertNode, address_of(selNode)); - NS_ENSURE_SUCCESS(res, res); + rv = GetLastEditableLeaf(lastInsertNode, address_of(selNode)); + NS_ENSURE_SUCCESS(rv, rv); tmp = selNode; while (tmp && (tmp != lastInsertNode)) { @@ -673,27 +682,27 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, if (highTable) selNode = highTable; } - if (!selNode) + if (!selNode) selNode = lastInsertNode; - if (IsTextNode(selNode) || (IsContainer(selNode) && !nsHTMLEditUtils::IsTable(selNode))) + if (IsTextNode(selNode) || (IsContainer(selNode) && !nsHTMLEditUtils::IsTable(selNode))) { - res = GetLengthOfDOMNode(selNode, (PRUint32&)selOffset); - NS_ENSURE_SUCCESS(res, res); + rv = GetLengthOfDOMNode(selNode, (PRUint32&)selOffset); + NS_ENSURE_SUCCESS(rv, rv); } else // we need to find a container for selection. Look up. { tmp = selNode; - res = GetNodeLocation(tmp, address_of(selNode), &selOffset); + rv = GetNodeLocation(tmp, address_of(selNode), &selOffset); ++selOffset; // want to be *after* last leaf node in paste - NS_ENSURE_SUCCESS(res, res); + NS_ENSURE_SUCCESS(rv, rv); } - + // make sure we don't end up with selection collapsed after an invisible break node nsWSRunObject wsRunObj(this, selNode, selOffset); PRInt32 outVisOffset=0; PRInt16 visType=0; - res = wsRunObj.PriorVisibleNode(selNode, selOffset, address_of(visNode), &outVisOffset, &visType); - NS_ENSURE_SUCCESS(res, res); + rv = wsRunObj.PriorVisibleNode(selNode, selOffset, address_of(visNode), &outVisOffset, &visType); + NS_ENSURE_SUCCESS(rv, rv); if (visType == nsWSRunObject::eBreak) { // we are after a break. Is it visible? Despite the name, @@ -704,11 +713,11 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, { // don't leave selection past an invisible break; // reset {selNode,selOffset} to point before break - res = GetNodeLocation(wsRunObj.mStartReasonNode, address_of(selNode), &selOffset); + rv = GetNodeLocation(wsRunObj.mStartReasonNode, address_of(selNode), &selOffset); // we want to be inside any inline style prior to break nsWSRunObject wsRunObj(this, selNode, selOffset); - res = wsRunObj.PriorVisibleNode(selNode, selOffset, address_of(visNode), &outVisOffset, &visType); - NS_ENSURE_SUCCESS(res, res); + rv = wsRunObj.PriorVisibleNode(selNode, selOffset, address_of(visNode), &outVisOffset, &visType); + NS_ENSURE_SUCCESS(rv, rv); if (visType == nsWSRunObject::eText || visType == nsWSRunObject::eNormalWS) { @@ -719,13 +728,13 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, { // prior visible thing is an image or some other non-text thingy. // We want to be right after it. - res = GetNodeLocation(wsRunObj.mStartReasonNode, address_of(selNode), &selOffset); + rv = GetNodeLocation(wsRunObj.mStartReasonNode, address_of(selNode), &selOffset); ++selOffset; } } } selection->Collapse(selNode, selOffset); - + // if we just pasted a link, discontinue link style nsCOMPtr link; if (!bStartedInLink && IsInLink(selNode, address_of(link))) @@ -736,17 +745,16 @@ nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString, // inside that. So I split it instead. nsCOMPtr leftLink; PRInt32 linkOffset; - res = SplitNodeDeep(link, selNode, selOffset, &linkOffset, true, address_of(leftLink)); - NS_ENSURE_SUCCESS(res, res); - res = GetNodeLocation(leftLink, address_of(selNode), &selOffset); - NS_ENSURE_SUCCESS(res, res); + rv = SplitNodeDeep(link, selNode, selOffset, &linkOffset, true, address_of(leftLink)); + NS_ENSURE_SUCCESS(rv, rv); + rv = GetNodeLocation(leftLink, address_of(selNode), &selOffset); + NS_ENSURE_SUCCESS(rv, rv); selection->Collapse(selNode, selOffset+1); } } } - - res = mRules->DidDoAction(selection, &ruleInfo, res); - return res; + + return mRules->DidDoAction(selection, &ruleInfo, rv); } // returns empty string if nothing to modify on node @@ -805,21 +813,21 @@ nsHTMLEditor::GetAttributeToModifyOnNode(nsIDOMNode *aNode, nsAString &aAttr) aAttr = srcStr; return NS_OK; } - + nsCOMPtr nodeAsEmbed = do_QueryInterface(aNode); if (nodeAsEmbed) { aAttr = srcStr; return NS_OK; } - + nsCOMPtr nodeAsObject = do_QueryInterface(aNode); if (nodeAsObject) { aAttr.AssignLiteral("data"); return NS_OK; } - + nsCOMPtr nodeAsLink = do_QueryInterface(aNode); if (nodeAsLink) { @@ -892,20 +900,18 @@ nsHTMLEditor::AddInsertionListener(nsIContentFilter *aListener) // don't let a listener be added more than once if (mContentFilters.IndexOfObject(aListener) == -1) { - if (!mContentFilters.AppendObject(aListener)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mContentFilters.AppendObject(aListener), NS_ERROR_FAILURE); } return NS_OK; } - + nsresult nsHTMLEditor::RemoveInsertionListener(nsIContentFilter *aListener) { NS_ENSURE_TRUE(aListener, NS_ERROR_FAILURE); - if (!mContentFilters.RemoveObject(aListener)) - return NS_ERROR_FAILURE; + NS_ENSURE_TRUE(mContentFilters.RemoveObject(aListener), NS_ERROR_FAILURE); return NS_OK; } @@ -968,7 +974,6 @@ nsHTMLEditor::StripFormattingNodes(nsIDOMNode *aNode, bool aListOnly) { NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER); - nsresult res = NS_OK; nsCOMPtr content = do_QueryInterface(aNode); if (content->TextIsOnlyWhitespace()) { @@ -976,27 +981,28 @@ nsHTMLEditor::StripFormattingNodes(nsIDOMNode *aNode, bool aListOnly) aNode->GetParentNode(getter_AddRefs(parent)); if (parent) { - if (!aListOnly || nsHTMLEditUtils::IsList(parent)) - res = parent->RemoveChild(aNode, getter_AddRefs(ignored)); - return res; + if (!aListOnly || nsHTMLEditUtils::IsList(parent)) { + return parent->RemoveChild(aNode, getter_AddRefs(ignored)); + } + return NS_OK; } } - + if (!nsHTMLEditUtils::IsPre(aNode)) { nsCOMPtr child; aNode->GetLastChild(getter_AddRefs(child)); - + while (child) { nsCOMPtr tmp; child->GetPreviousSibling(getter_AddRefs(tmp)); - res = StripFormattingNodes(child, aListOnly); - NS_ENSURE_SUCCESS(res, res); + nsresult rv = StripFormattingNodes(child, aListOnly); + NS_ENSURE_SUCCESS(rv, rv); child = tmp; } } - return res; + return NS_OK; } NS_IMETHODIMP nsHTMLEditor::PrepareTransferable(nsITransferable **transferable) @@ -1061,12 +1067,12 @@ FindIntegerAfterString(const char *aLeadingString, PRInt32 numFront = aCStr.Find(aLeadingString); if (numFront == -1) return false; - numFront += strlen(aLeadingString); - + numFront += strlen(aLeadingString); + PRInt32 numBack = aCStr.FindCharInSet(CRLF, numFront); if (numBack == -1) return false; - + nsCAutoString numStr(Substring(aCStr, numFront, numBack-numFront)); PRInt32 errorCode; foundNumber = numStr.ToInteger(&errorCode); @@ -1083,14 +1089,14 @@ RemoveFragComments(nsCString & aStr) PRInt32 startCommentEnd = aStr.Find("-->", false, startCommentIndx); if (startCommentEnd > startCommentIndx) aStr.Cut(startCommentIndx, (startCommentEnd+3)-startCommentIndx); - } + } PRInt32 endCommentIndx = aStr.Find("", false, endCommentIndx); if (endCommentEnd > endCommentIndx) aStr.Cut(endCommentIndx, (endCommentEnd+3)-endCommentIndx); - } + } return NS_OK; } @@ -1103,10 +1109,10 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic startHTML < -1) return NS_ERROR_FAILURE; if (!FindIntegerAfterString("EndHTML:", aCfhtml, endHTML) || - endHTML < -1) + endHTML < -1) return NS_ERROR_FAILURE; if (!FindIntegerAfterString("StartFragment:", aCfhtml, startFragment) || - startFragment < 0) + startFragment < 0) return NS_ERROR_FAILURE; if (!FindIntegerAfterString("EndFragment:", aCfhtml, endFragment) || startFragment < 0) @@ -1118,13 +1124,13 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic if (startHTML == -1) { startHTML = aCfhtml.Find(""); if (startHTML == -1) - return false; + return NS_OK; } if (endHTML == -1) { const char endFragmentMarker[] = ""; endHTML = aCfhtml.Find(endFragmentMarker); if (endHTML == -1) - return false; + return NS_OK; endHTML += ArrayLength(endFragmentMarker) - 1; } @@ -1165,7 +1171,7 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic // create fragment string nsCAutoString fragmentUTF8(Substring(aCfhtml, startFragment, endFragment-startFragment)); - + // remove the StartFragment/EndFragment comments from the fragment, if present RemoveFragComments(fragmentUTF8); @@ -1175,7 +1181,7 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic // convert both strings to usc2 const nsAFlatString& fragUcs2Str = NS_ConvertUTF8toUTF16(fragmentUTF8); const nsAFlatString& cntxtUcs2Str = NS_ConvertUTF8toUTF16(contextUTF8); - + // translate platform linebreaks for fragment PRInt32 oldLengthInChars = fragUcs2Str.Length() + 1; // +1 to include null terminator PRInt32 newLengthInChars = 0; @@ -1183,11 +1189,8 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic nsLinebreakConverter::eLinebreakAny, nsLinebreakConverter::eLinebreakContent, oldLengthInChars, &newLengthInChars); - if (!*aStuffToPaste) - { - return NS_ERROR_FAILURE; - } - + NS_ENSURE_TRUE(*aStuffToPaste, NS_ERROR_FAILURE); + // translate platform linebreaks for context oldLengthInChars = cntxtUcs2Str.Length() + 1; // +1 to include null terminator newLengthInChars = 0; @@ -1196,8 +1199,8 @@ nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, PRUnichar **aStuffToPaste, PRUnic nsLinebreakConverter::eLinebreakContent, oldLengthInChars, &newLengthInChars); // it's ok for context to be empty. frag might be whole doc and contain all its context. - - // we're done! + + // we're done! return NS_OK; } @@ -1209,11 +1212,11 @@ bool nsHTMLEditor::IsSafeToInsertData(nsIDOMDocument* aSourceDoc) nsCOMPtr destdoc = GetDocument(); NS_ASSERTION(destdoc, "Where is our destination doc?"); nsCOMPtr container = destdoc->GetContainer(); - nsCOMPtr dsti(do_QueryInterface(container)); + nsCOMPtr dsti = do_QueryInterface(container); nsCOMPtr root; if (dsti) dsti->GetRootTreeItem(getter_AddRefs(root)); - nsCOMPtr docShell(do_QueryInterface(root)); + nsCOMPtr docShell = do_QueryInterface(root); PRUint32 appType; if (docShell && NS_SUCCEEDED(docShell->GetAppType(&appType))) isSafe = appType == nsIDocShell::APP_TYPE_EDITOR; @@ -1245,7 +1248,7 @@ nsresult nsHTMLEditor::InsertObject(const char* aType, nsISupports* aObject, boo nsCOMPtr fileURI; if (0 == nsCRT::strcmp(type, kFileMime)) { - nsCOMPtr fileObj(do_QueryInterface(aObject)); + nsCOMPtr fileObj = do_QueryInterface(aObject); if (fileObj) { rv = NS_NewFileURI(getter_AddRefs(fileURI), fileObj); @@ -1321,7 +1324,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable nsXPIDLCString bestFlavor; nsCOMPtr genericDataObj; PRUint32 len = 0; - if ( NS_SUCCEEDED(transferable->GetAnyTransferData(getter_Copies(bestFlavor), getter_AddRefs(genericDataObj), &len)) ) + if (NS_SUCCEEDED(transferable->GetAnyTransferData(getter_Copies(bestFlavor), getter_AddRefs(genericDataObj), &len))) { nsAutoTxnsConserveSelection dontSpazMySelection(this); nsAutoString flavor; @@ -1333,7 +1336,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable bool isSafe = IsSafeToInsertData(aSourceDoc); - if (0 == nsCRT::strcmp(bestFlavor, kFileMime) || + if (0 == nsCRT::strcmp(bestFlavor, kFileMime) || 0 == nsCRT::strcmp(bestFlavor, kJPEGImageMime) || 0 == nsCRT::strcmp(bestFlavor, kPNGImageMime) || 0 == nsCRT::strcmp(bestFlavor, kGIFImageMime)) { @@ -1343,14 +1346,14 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable else if (0 == nsCRT::strcmp(bestFlavor, kNativeHTMLMime)) { // note cf_html uses utf8, hence use length = len, not len/2 as in flavors below - nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); + nsCOMPtr textDataObj = do_QueryInterface(genericDataObj); if (textDataObj && len > 0) { nsCAutoString cfhtml; textDataObj->GetData(cfhtml); NS_ASSERTION(cfhtml.Length() <= (len), "Invalid length!"); nsXPIDLString cfcontext, cffragment, cfselection; // cfselection left emtpy for now - + rv = ParseCFHTML(cfhtml, getter_Copies(cffragment), getter_Copies(cfcontext)); if (NS_SUCCEEDED(rv) && !cffragment.IsEmpty()) { @@ -1367,7 +1370,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable else if (0 == nsCRT::strcmp(bestFlavor, kHTMLMime) || 0 == nsCRT::strcmp(bestFlavor, kUnicodeMime) || 0 == nsCRT::strcmp(bestFlavor, kMozTextInternal)) { - nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); + nsCOMPtr textDataObj = do_QueryInterface(genericDataObj); if (textDataObj && len > 0) { nsAutoString text; @@ -1453,7 +1456,7 @@ nsresult nsHTMLEditor::InsertFromDataTransfer(nsIDOMDataTransfer *aDataTransfer, NS_ConvertUTF16toUTF8 cfhtml(text); nsXPIDLString cfcontext, cffragment, cfselection; // cfselection left emtpy for now - + rv = ParseCFHTML(cfhtml, getter_Copies(cffragment), getter_Copies(cfcontext)); if (NS_SUCCEEDED(rv) && !cffragment.IsEmpty()) { @@ -1507,17 +1510,17 @@ bool nsHTMLEditor::HavePrivateHTMLFlavor(nsIClipboard *aClipboard) { // check the clipboard for our special kHTMLContext flavor. If that is there, we know // we have our own internal html format on clipboard. - + NS_ENSURE_TRUE(aClipboard, false); bool bHavePrivateHTMLFlavor = false; - + const char* flavArray[] = { kHTMLContext }; - + if (NS_SUCCEEDED(aClipboard->HasDataMatchingFlavors(flavArray, ArrayLength(flavArray), nsIClipboard::kGlobalClipboard, - &bHavePrivateHTMLFlavor ))) + &bHavePrivateHTMLFlavor))) return bHavePrivateHTMLFlavor; - + return false; } @@ -1531,7 +1534,7 @@ NS_IMETHODIMP nsHTMLEditor::Paste(PRInt32 aSelectionType) nsresult rv; nsCOMPtr clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - + // find out if we have our internal html flavor on the clipboard. We don't want to mess // around with cfhtml if we do. bool bHavePrivateHTMLFlavor = HavePrivateHTMLFlavor(clipboard); @@ -1539,65 +1542,66 @@ NS_IMETHODIMP nsHTMLEditor::Paste(PRInt32 aSelectionType) // Get the nsITransferable interface for getting the data from the clipboard nsCOMPtr trans; rv = PrepareHTMLTransferable(getter_AddRefs(trans), bHavePrivateHTMLFlavor); - if (NS_SUCCEEDED(rv) && trans) + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); + // Get the Data from the clipboard + rv = clipboard->GetData(trans, aSelectionType); + NS_ENSURE_SUCCESS(rv, rv); + if (!IsModifiable()) { + return NS_OK; + } + + // also get additional html copy hints, if present + nsAutoString contextStr, infoStr; + + // also get additional html copy hints, if present + if (bHavePrivateHTMLFlavor) { - // Get the Data from the clipboard - if (NS_SUCCEEDED(clipboard->GetData(trans, aSelectionType)) && IsModifiable()) + nsCOMPtr contextDataObj, infoDataObj; + PRUint32 contextLen, infoLen; + nsCOMPtr textDataObj; + + nsCOMPtr contextTrans = + do_CreateInstance("@mozilla.org/widget/transferable;1"); + NS_ENSURE_TRUE(contextTrans, NS_ERROR_NULL_POINTER); + contextTrans->AddDataFlavor(kHTMLContext); + clipboard->GetData(contextTrans, aSelectionType); + contextTrans->GetTransferData(kHTMLContext, getter_AddRefs(contextDataObj), &contextLen); + + nsCOMPtr infoTrans = + do_CreateInstance("@mozilla.org/widget/transferable;1"); + NS_ENSURE_TRUE(infoTrans, NS_ERROR_NULL_POINTER); + infoTrans->AddDataFlavor(kHTMLInfo); + clipboard->GetData(infoTrans, aSelectionType); + infoTrans->GetTransferData(kHTMLInfo, getter_AddRefs(infoDataObj), &infoLen); + + if (contextDataObj) { - // also get additional html copy hints, if present - nsAutoString contextStr, infoStr; + nsAutoString text; + textDataObj = do_QueryInterface(contextDataObj); + textDataObj->GetData(text); + NS_ASSERTION(text.Length() <= (contextLen/2), "Invalid length!"); + contextStr.Assign(text.get(), contextLen / 2); + } - // also get additional html copy hints, if present - if (bHavePrivateHTMLFlavor) - { - nsCOMPtr contextDataObj, infoDataObj; - PRUint32 contextLen, infoLen; - nsCOMPtr textDataObj; - - nsCOMPtr contextTrans = - do_CreateInstance("@mozilla.org/widget/transferable;1"); - NS_ENSURE_TRUE(contextTrans, NS_ERROR_NULL_POINTER); - contextTrans->AddDataFlavor(kHTMLContext); - clipboard->GetData(contextTrans, aSelectionType); - contextTrans->GetTransferData(kHTMLContext, getter_AddRefs(contextDataObj), &contextLen); - - nsCOMPtr infoTrans = - do_CreateInstance("@mozilla.org/widget/transferable;1"); - NS_ENSURE_TRUE(infoTrans, NS_ERROR_NULL_POINTER); - infoTrans->AddDataFlavor(kHTMLInfo); - clipboard->GetData(infoTrans, aSelectionType); - infoTrans->GetTransferData(kHTMLInfo, getter_AddRefs(infoDataObj), &infoLen); - - if (contextDataObj) - { - nsAutoString text; - textDataObj = do_QueryInterface(contextDataObj); - textDataObj->GetData(text); - NS_ASSERTION(text.Length() <= (contextLen/2), "Invalid length!"); - contextStr.Assign(text.get(), contextLen / 2); - } - - if (infoDataObj) - { - nsAutoString text; - textDataObj = do_QueryInterface(infoDataObj); - textDataObj->GetData(text); - NS_ASSERTION(text.Length() <= (infoLen/2), "Invalid length!"); - infoStr.Assign(text.get(), infoLen / 2); - } - } - - // handle transferable hooks - nsCOMPtr domdoc = GetDOMDocument(); - if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, trans)) - return NS_OK; - - rv = InsertFromTransferable(trans, nsnull, contextStr, infoStr, - nsnull, 0, true); + if (infoDataObj) + { + nsAutoString text; + textDataObj = do_QueryInterface(infoDataObj); + textDataObj->GetData(text); + NS_ASSERTION(text.Length() <= (infoLen/2), "Invalid length!"); + infoStr.Assign(text.get(), infoLen / 2); } } - return rv; + // handle transferable hooks + nsCOMPtr domdoc; + GetDocument(getter_AddRefs(domdoc)); + if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, trans)) + return NS_OK; + + return InsertFromTransferable(trans, nsnull, contextStr, infoStr, + nsnull, 0, true); } NS_IMETHODIMP nsHTMLEditor::PasteTransferable(nsITransferable *aTransferable) @@ -1626,7 +1630,7 @@ NS_IMETHODIMP nsHTMLEditor::PasteNoFormatting(PRInt32 aSelectionType) nsresult rv; nsCOMPtr clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - + // Get the nsITransferable interface for getting the data from the clipboard. // use nsPlaintextEditor::PrepareTransferable() to force unicode plaintext data. nsCOMPtr trans; @@ -1660,8 +1664,9 @@ NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, bool *aCanPaste) *aCanPaste = false; // can't paste if readonly - if (!IsModifiable()) + if (!IsModifiable()) { return NS_OK; + } nsresult rv; nsCOMPtr clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); @@ -1678,9 +1683,9 @@ NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, bool *aCanPaste) rv = clipboard->HasDataMatchingFlavors(textHtmlEditorFlavors, ArrayLength(textHtmlEditorFlavors), aSelectionType, &haveFlavors); - + NS_ENSURE_SUCCESS(rv, rv); - + *aCanPaste = haveFlavors; return NS_OK; } @@ -1725,13 +1730,13 @@ NS_IMETHODIMP nsHTMLEditor::CanPasteTransferable(nsITransferable *aTransferable, return NS_OK; } } - + *aCanPaste = false; return NS_OK; } -// +// // HTML PasteAsQuotation: Paste in a blockquote type=cite // NS_IMETHODIMP nsHTMLEditor::PasteAsQuotation(PRInt32 aSelectionType) @@ -1751,43 +1756,35 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsAString & aCitation, // get selection nsCOMPtr selection; - nsresult res = GetSelection(getter_AddRefs(selection)); - NS_ENSURE_SUCCESS(res, res); + nsresult rv = GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); // give rules a chance to handle or cancel nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement); bool cancel, handled; - res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); - NS_ENSURE_SUCCESS(res, res); - if (cancel) return NS_OK; // rules canceled the operation - if (!handled) - { - nsCOMPtr newNode; - res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode)); - NS_ENSURE_SUCCESS(res, res); - NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); - - // Try to set type=cite. Ignore it if this fails. - nsCOMPtr newElement (do_QueryInterface(newNode)); - if (newElement) - { - newElement->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("cite")); - } - - // Set the selection to the underneath the node we just inserted: - res = selection->Collapse(newNode, 0); - if (NS_FAILED(res)) - { -#ifdef DEBUG_akkana - printf("Couldn't collapse"); -#endif - // XXX: error result: should res be returned here? - } - - res = Paste(aSelectionType); + rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + NS_ENSURE_SUCCESS(rv, rv); + if (cancel || handled) { + return NS_OK; // rules canceled the operation } - return res; + + nsCOMPtr newNode; + rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); + + // Try to set type=cite. Ignore it if this fails. + nsCOMPtr newElement = do_QueryInterface(newNode); + if (newElement) { + newElement->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("cite")); + } + + // Set the selection to the underneath the node we just inserted: + rv = selection->Collapse(newNode, 0); + NS_ENSURE_SUCCESS(rv, rv); + + return Paste(aSelectionType); } // @@ -1803,47 +1800,40 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation(PRInt32 aSelectionType) // Create generic Transferable for getting the data nsCOMPtr trans = do_CreateInstance("@mozilla.org/widget/transferable;1", &rv); - if (NS_SUCCEEDED(rv) && trans) + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); + + // We only handle plaintext pastes here + trans->AddDataFlavor(kUnicodeMime); + + // Get the Data from the clipboard + clipboard->GetData(trans, aSelectionType); + + // Now we ask the transferable for the data + // it still owns the data, we just have a pointer to it. + // If it can't support a "text" output of the data the call will fail + nsCOMPtr genericDataObj; + PRUint32 len = 0; + char* flav = 0; + rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj), &len); + NS_ENSURE_SUCCESS(rv, rv); + + if (flav && 0 == nsCRT::strcmp(flav, kUnicodeMime)) { - // We only handle plaintext pastes here - trans->AddDataFlavor(kUnicodeMime); - - // Get the Data from the clipboard - clipboard->GetData(trans, aSelectionType); - - // Now we ask the transferable for the data - // it still owns the data, we just have a pointer to it. - // If it can't support a "text" output of the data the call will fail - nsCOMPtr genericDataObj; - PRUint32 len = 0; - char* flav = 0; - rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj), - &len); - if (NS_FAILED(rv)) - { -#ifdef DEBUG_akkana - printf("PasteAsPlaintextQuotation: GetAnyTransferData failed, %d\n", rv); -#endif - return rv; - } - - if (flav && 0 == nsCRT::strcmp((flav), kUnicodeMime)) - { #ifdef DEBUG_clipboard printf("Got flavor [%s]\n", flav); #endif - nsCOMPtr textDataObj(do_QueryInterface(genericDataObj)); - if (textDataObj && len > 0) - { - nsAutoString stuffToPaste; - textDataObj->GetData(stuffToPaste); - NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!"); - nsAutoEditBatch beginBatching(this); - rv = InsertAsPlaintextQuotation(stuffToPaste, true, 0); - } + nsCOMPtr textDataObj = do_QueryInterface(genericDataObj); + if (textDataObj && len > 0) + { + nsAutoString stuffToPaste; + textDataObj->GetData(stuffToPaste); + NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!"); + nsAutoEditBatch beginBatching(this); + rv = InsertAsPlaintextQuotation(stuffToPaste, true, 0); } - NS_Free(flav); } + NS_Free(flav); return rv; } @@ -1962,7 +1952,7 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText, if (mWrapToWindow) return nsPlaintextEditor::InsertAsQuotation(aQuotedText, aNodeInserted); - nsCOMPtr preNode; + nsCOMPtr newNode; // get selection nsCOMPtr selection; nsresult rv = GetSelection(getter_AddRefs(selection)); @@ -1977,66 +1967,66 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText, bool cancel, handled; rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); NS_ENSURE_SUCCESS(rv, rv); - if (cancel) return NS_OK; // rules canceled the operation - if (!handled) + if (cancel || handled) { + return NS_OK; // rules canceled the operation + } + + // Wrap the inserted quote in a so it won't be wrapped: + rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("span"), getter_AddRefs(newNode)); + + // If this succeeded, then set selection inside the pre + // so the inserted text will end up there. + // If it failed, we don't care what the return value was, + // but we'll fall through and try to insert the text anyway. + if (NS_SUCCEEDED(rv) && newNode) { - // Wrap the inserted quote in a so it won't be wrapped: - rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("span"), getter_AddRefs(preNode)); - - // If this succeeded, then set selection inside the pre - // so the inserted text will end up there. - // If it failed, we don't care what the return value was, - // but we'll fall through and try to insert the text anyway. - if (NS_SUCCEEDED(rv) && preNode) + // Add an attribute on the pre node so we'll know it's a quotation. + // Do this after the insertion, so that + nsCOMPtr preElement = do_QueryInterface(newNode); + if (preElement) { - // Add an attribute on the pre node so we'll know it's a quotation. - // Do this after the insertion, so that - nsCOMPtr preElement(do_QueryInterface(preNode)); - if (preElement) - { - preElement->SetAttribute(NS_LITERAL_STRING("_moz_quote"), - NS_LITERAL_STRING("true")); - // turn off wrapping on spans - preElement->SetAttribute(NS_LITERAL_STRING("style"), - NS_LITERAL_STRING("white-space: pre;")); - } - // and set the selection inside it: - selection->Collapse(preNode, 0); + preElement->SetAttribute(NS_LITERAL_STRING("_moz_quote"), + NS_LITERAL_STRING("true")); + // turn off wrapping on spans + preElement->SetAttribute(NS_LITERAL_STRING("style"), + NS_LITERAL_STRING("white-space: pre;")); } + // and set the selection inside it: + selection->Collapse(newNode, 0); + } - if (aAddCites) - rv = nsPlaintextEditor::InsertAsQuotation(aQuotedText, aNodeInserted); - else - rv = nsPlaintextEditor::InsertText(aQuotedText); - // Note that if !aAddCites, aNodeInserted isn't set. - // That's okay because the routines that use aAddCites - // don't need to know the inserted node. + if (aAddCites) + rv = nsPlaintextEditor::InsertAsQuotation(aQuotedText, aNodeInserted); + else + rv = nsPlaintextEditor::InsertText(aQuotedText); + // Note that if !aAddCites, aNodeInserted isn't set. + // That's okay because the routines that use aAddCites + // don't need to know the inserted node. - if (aNodeInserted && NS_SUCCEEDED(rv)) - { - *aNodeInserted = preNode; - NS_IF_ADDREF(*aNodeInserted); - } + if (aNodeInserted && NS_SUCCEEDED(rv)) + { + *aNodeInserted = newNode; + NS_IF_ADDREF(*aNodeInserted); } // Set the selection to just after the inserted node: - if (NS_SUCCEEDED(rv) && preNode) + if (NS_SUCCEEDED(rv) && newNode) { nsCOMPtr parent; PRInt32 offset; - if (NS_SUCCEEDED(GetNodeLocation(preNode, address_of(parent), &offset)) && parent) + if (NS_SUCCEEDED(GetNodeLocation(newNode, address_of(parent), &offset)) && parent) selection->Collapse(parent, offset+1); } return rv; } -NS_IMETHODIMP +NS_IMETHODIMP nsHTMLEditor::StripCites() { return nsPlaintextEditor::StripCites(); } -NS_IMETHODIMP +NS_IMETHODIMP nsHTMLEditor::Rewrap(bool aRespectNewlines) { return nsPlaintextEditor::Rewrap(aRespectNewlines); @@ -2056,82 +2046,72 @@ nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText, } nsCOMPtr newNode; - nsresult res = NS_OK; // get selection nsCOMPtr selection; - res = GetSelection(getter_AddRefs(selection)); - NS_ENSURE_SUCCESS(res, res); - if (!selection) - { - NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); + nsresult rv = GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); + + nsAutoEditBatch beginBatching(this); + nsAutoRules beginRulesSniffing(this, kOpInsertQuotation, nsIEditor::eNext); + + // give rules a chance to handle or cancel + nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement); + bool cancel, handled; + rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); + NS_ENSURE_SUCCESS(rv, rv); + if (cancel || handled) { + return NS_OK; // rules canceled the operation } - else + + rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); + + // Try to set type=cite. Ignore it if this fails. + nsCOMPtr newElement = do_QueryInterface(newNode); + if (newElement) { - nsAutoEditBatch beginBatching(this); - nsAutoRules beginRulesSniffing(this, kOpInsertQuotation, nsIEditor::eNext); + NS_NAMED_LITERAL_STRING(citeStr, "cite"); + newElement->SetAttribute(NS_LITERAL_STRING("type"), citeStr); - // give rules a chance to handle or cancel - nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement); - bool cancel, handled; - res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled); - NS_ENSURE_SUCCESS(res, res); - if (cancel) return NS_OK; // rules canceled the operation - if (!handled) - { - res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode)); - NS_ENSURE_SUCCESS(res, res); - NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); + if (!aCitation.IsEmpty()) + newElement->SetAttribute(citeStr, aCitation); - // Try to set type=cite. Ignore it if this fails. - nsCOMPtr newElement (do_QueryInterface(newNode)); - if (newElement) - { - NS_NAMED_LITERAL_STRING(citestr, "cite"); - newElement->SetAttribute(NS_LITERAL_STRING("type"), citestr); + // Set the selection inside the blockquote so aQuotedText will go there: + selection->Collapse(newNode, 0); + } - if (!aCitation.IsEmpty()) - newElement->SetAttribute(citestr, aCitation); + if (aInsertHTML) + rv = LoadHTML(aQuotedText); + else + rv = InsertText(aQuotedText); // XXX ignore charset - // Set the selection inside the blockquote so aQuotedText will go there: - selection->Collapse(newNode, 0); - } - - if (aInsertHTML) - res = LoadHTML(aQuotedText); - - else - res = InsertText(aQuotedText); // XXX ignore charset - - if (aNodeInserted) - { - if (NS_SUCCEEDED(res)) - { - *aNodeInserted = newNode; - NS_IF_ADDREF(*aNodeInserted); - } - } - } + if (aNodeInserted && NS_SUCCEEDED(rv)) + { + *aNodeInserted = newNode; + NS_IF_ADDREF(*aNodeInserted); } // Set the selection to just after the inserted node: - if (NS_SUCCEEDED(res) && newNode) + if (NS_SUCCEEDED(rv) && newNode) { nsCOMPtr parent; PRInt32 offset; if (NS_SUCCEEDED(GetNodeLocation(newNode, address_of(parent), &offset)) && parent) selection->Collapse(parent, offset+1); } - return res; + return rv; } void RemoveBodyAndHead(nsIDOMNode *aNode) { - if (!aNode) + if (!aNode) return; - - nsCOMPtr tmp, child, body, head; + + nsCOMPtr tmp, child, body, head; // find the body and head nodes if any. // look only at immediate children of aNode. aNode->GetFirstChild(getter_AddRefs(child)); @@ -2148,7 +2128,7 @@ void RemoveBodyAndHead(nsIDOMNode *aNode) child->GetNextSibling(getter_AddRefs(tmp)); child = tmp; } - if (head) + if (head) { aNode->RemoveChild(head, getter_AddRefs(tmp)); } @@ -2242,31 +2222,32 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, { NS_ENSURE_TRUE(outFragNode && outStartNode && outEndNode, NS_ERROR_NULL_POINTER); nsCOMPtr docfrag; - nsCOMPtr contextAsNode, tmp; - nsresult res = NS_OK; + nsCOMPtr contextAsNode, tmp; + nsresult rv = NS_OK; nsCOMPtr doc = GetDocument(); NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE); - + // if we have context info, create a fragment for that nsCOMPtr contextfrag; nsCOMPtr contextLeaf, junk; if (!aContextStr.IsEmpty()) { - res = ParseFragment(aContextStr, nsnull, doc, address_of(contextAsNode), - aTrustedInput); - NS_ENSURE_SUCCESS(res, res); + rv = ParseFragment(aContextStr, nsnull, doc, address_of(contextAsNode), + aTrustedInput); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(contextAsNode, NS_ERROR_FAILURE); - res = StripFormattingNodes(contextAsNode); - NS_ENSURE_SUCCESS(res, res); + rv = StripFormattingNodes(contextAsNode); + NS_ENSURE_SUCCESS(rv, rv); RemoveBodyAndHead(contextAsNode); - res = FindTargetNode(contextAsNode, contextLeaf); - if (res == NS_FOUND_TARGET) - res = NS_OK; - NS_ENSURE_SUCCESS(res, res); + rv = FindTargetNode(contextAsNode, contextLeaf); + if (rv == NS_FOUND_TARGET) { + rv = NS_OK; + } + NS_ENSURE_SUCCESS(rv, rv); } nsCOMPtr contextLeafAsContent = do_QueryInterface(contextLeaf); @@ -2281,12 +2262,12 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, } else { contextAtom = nsGkAtoms::body; } - res = ParseFragment(aInputString, - contextAtom, - doc, - outFragNode, - aTrustedInput); - NS_ENSURE_SUCCESS(res, res); + rv = ParseFragment(aInputString, + contextAtom, + doc, + outFragNode, + aTrustedInput); + NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(*outFragNode, NS_ERROR_FAILURE); RemoveBodyAndHead(*outFragNode); @@ -2298,8 +2279,8 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, *outFragNode = contextAsNode; } - res = StripFormattingNodes(*outFragNode, true); - NS_ENSURE_SUCCESS(res, res); + rv = StripFormattingNodes(*outFragNode, true); + NS_ENSURE_SUCCESS(rv, rv); // If there was no context, then treat all of the data we did get as the // pasted data. @@ -2338,7 +2319,7 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, } GetLengthOfDOMNode(*outEndNode, (PRUint32&)*outEndOffset); - return res; + return NS_OK; } @@ -2348,18 +2329,17 @@ nsresult nsHTMLEditor::ParseFragment(const nsAString & aFragStr, nsCOMPtr *outNode, bool aTrustedInput) { - nsresult rv; nsCOMPtr frag; NS_NewDocumentFragment(getter_AddRefs(frag), aTargetDocument->NodeInfoManager()); nsCOMPtr fragment = do_QueryInterface(frag); - rv = nsContentUtils::ParseFragmentHTML(aFragStr, - fragment, - aContextLocalName ? - aContextLocalName : nsGkAtoms::body, - kNameSpaceID_XHTML, - false, - true); + nsresult rv = nsContentUtils::ParseFragmentHTML(aFragStr, + fragment, + aContextLocalName ? + aContextLocalName : nsGkAtoms::body, + kNameSpaceID_XHTML, + false, + true); if (!aTrustedInput) { nsTreeSanitizer sanitizer(aContextLocalName ? nsIParserUtils::SanitizerAllowStyle : @@ -2379,15 +2359,15 @@ nsresult nsHTMLEditor::CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode, { NS_ENSURE_TRUE(aFragmentAsNode, NS_ERROR_NULL_POINTER); - nsresult res; + nsresult rv; // if no info was provided about the boundary between context and stream, // then assume all is stream. if (!aStartNode) { PRInt32 fragLen; - res = GetLengthOfDOMNode(aFragmentAsNode, (PRUint32&)fragLen); - NS_ENSURE_SUCCESS(res, res); + rv = GetLengthOfDOMNode(aFragmentAsNode, (PRUint32&)fragLen); + NS_ENSURE_SUCCESS(rv, rv); aStartNode = aFragmentAsNode; aStartOffset = 0; @@ -2395,45 +2375,38 @@ nsresult nsHTMLEditor::CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode, aEndOffset = fragLen; } - nsRefPtr docFragRange = new nsRange(); - res = docFragRange->SetStart(aStartNode, aStartOffset); - NS_ENSURE_SUCCESS(res, res); - res = docFragRange->SetEnd(aEndNode, aEndOffset); - NS_ENSURE_SUCCESS(res, res); + nsRefPtr docFragRange; + rv = nsRange::CreateRange(aStartNode, aStartOffset, aEndNode, aEndOffset, getter_AddRefs(docFragRange)); + NS_ENSURE_SUCCESS(rv, rv); // now use a subtree iterator over the range to create a list of nodes nsTrivialFunctor functor; nsDOMSubtreeIterator iter; - res = iter.Init(docFragRange); - NS_ENSURE_SUCCESS(res, res); - res = iter.AppendList(functor, outNodeList); + rv = iter.Init(docFragRange); + NS_ENSURE_SUCCESS(rv, rv); - return res; + return iter.AppendList(functor, outNodeList); } -nsresult -nsHTMLEditor::GetListAndTableParents(bool aEnd, +nsresult +nsHTMLEditor::GetListAndTableParents(bool aEnd, nsCOMArray& aListOfNodes, nsCOMArray& outArray) { PRInt32 listCount = aListOfNodes.Count(); - if (listCount <= 0) - return NS_ERROR_FAILURE; // no empty lists, please - - // build up list of parents of first (or last) node in list - // that are either lists, or tables. + NS_ENSURE_TRUE(listCount > 0, NS_ERROR_FAILURE); // no empty lists, please + + // build up list of parents of first (or last) node in list + // that are either lists, or tables. PRInt32 idx = 0; if (aEnd) idx = listCount-1; - - nsCOMPtr pNode = aListOfNodes[idx]; + + nsCOMPtr pNode = aListOfNodes[idx]; while (pNode) { if (nsHTMLEditUtils::IsList(pNode) || nsHTMLEditUtils::IsTable(pNode)) { - if (!outArray.AppendObject(pNode)) - { - return NS_ERROR_FAILURE; - } + NS_ENSURE_TRUE(outArray.AppendObject(pNode), NS_ERROR_FAILURE); } nsCOMPtr parent; pNode->GetParentNode(getter_AddRefs(parent)); @@ -2518,8 +2491,8 @@ nsHTMLEditor::ScanForListAndTableStructure( bool aEnd, nsCOMPtr originalNode = pNode; while (pNode) { - if ( (bList && nsHTMLEditUtils::IsListItem(pNode)) || - (!bList && (nsHTMLEditUtils::IsTableElement(pNode) && !nsHTMLEditUtils::IsTable(pNode))) ) + if ((bList && nsHTMLEditUtils::IsListItem(pNode)) || + (!bList && (nsHTMLEditUtils::IsTableElement(pNode) && !nsHTMLEditUtils::IsTable(pNode)))) { nsCOMPtr structureNode; if (bList) structureNode = GetListParent(pNode); @@ -2538,7 +2511,7 @@ nsHTMLEditor::ScanForListAndTableStructure( bool aEnd, pNode = parent; } return NS_OK; -} +} nsresult nsHTMLEditor::ReplaceOrphanedStructure(bool aEnd, @@ -2548,14 +2521,14 @@ nsHTMLEditor::ReplaceOrphanedStructure(bool aEnd, { nsCOMPtr curNode = aListAndTableArray[aHighWaterMark]; NS_ENSURE_TRUE(curNode, NS_ERROR_NULL_POINTER); - + nsCOMPtr replaceNode, originalNode; - + // find substructure of list or table that must be included in paste. - nsresult res = ScanForListAndTableStructure(aEnd, aNodeArray, + nsresult rv = ScanForListAndTableStructure(aEnd, aNodeArray, curNode, address_of(replaceNode)); - NS_ENSURE_SUCCESS(res, res); - + NS_ENSURE_SUCCESS(rv, rv); + // if we found substructure, paste it instead of its descendants if (replaceNode) { @@ -2571,7 +2544,7 @@ nsHTMLEditor::ReplaceOrphanedStructure(bool aEnd, else break; } while(endpoint); - + // now replace the removed nodes with the structural parent if (aEnd) aNodeArray.AppendObject(replaceNode); else aNodeArray.InsertObjectAt(replaceNode, 0); @@ -2583,13 +2556,13 @@ nsIDOMNode* nsHTMLEditor::GetArrayEndpoint(bool aEnd, nsCOMArray& aNodeArray) { PRInt32 listCount = aNodeArray.Count(); - if (listCount <= 0) + if (listCount <= 0) { return nsnull; + } - if (aEnd) - { + if (aEnd) { return aNodeArray[listCount-1]; } - + return aNodeArray[0]; }