Bug 1002429 part 6 - Clean up nsHTMLEditor::ReplaceHeadContentsWithHTML; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-04-28 14:54:46 +03:00
Родитель 8af202298a
Коммит 6432779bfe
3 изменённых файлов: 44 добавлений и 60 удалений

Просмотреть файл

@ -1379,6 +1379,13 @@ NS_IMETHODIMP nsEditor::CreateNode(const nsAString& aTag,
} }
nsresult
nsEditor::InsertNode(nsIContent* aContent, nsINode* aParent, int32_t aPosition)
{
MOZ_ASSERT(aContent && aParent);
return InsertNode(GetAsDOMNode(aContent), GetAsDOMNode(aParent), aPosition);
}
NS_IMETHODIMP nsEditor::InsertNode(nsIDOMNode * aNode, NS_IMETHODIMP nsEditor::InsertNode(nsIDOMNode * aNode,
nsIDOMNode * aParent, nsIDOMNode * aParent,
int32_t aPosition) int32_t aPosition)

Просмотреть файл

@ -206,6 +206,8 @@ public:
/* helper routines for node/parent manipulations */ /* helper routines for node/parent manipulations */
nsresult DeleteNode(nsINode* aNode); nsresult DeleteNode(nsINode* aNode);
nsresult InsertNode(nsIContent* aContent, nsINode* aParent,
int32_t aPosition);
nsresult ReplaceContainer(nsINode* inNode, nsresult ReplaceContainer(nsINode* inNode,
mozilla::dom::Element** outNode, mozilla::dom::Element** outNode,
const nsAString& aNodeType, const nsAString& aNodeType,

Просмотреть файл

@ -46,7 +46,6 @@
#include "nsISupportsArray.h" #include "nsISupportsArray.h"
#include "nsContentUtils.h" #include "nsContentUtils.h"
#include "nsIDocumentEncoder.h" #include "nsIDocumentEncoder.h"
#include "nsIDOMDocumentFragment.h"
#include "nsIPresShell.h" #include "nsIPresShell.h"
#include "nsPresContext.h" #include "nsPresContext.h"
#include "SetDocTitleTxn.h" #include "SetDocTitleTxn.h"
@ -70,6 +69,7 @@
#include "nsIFrame.h" #include "nsIFrame.h"
#include "nsIParserService.h" #include "nsIParserService.h"
#include "mozilla/dom/Selection.h" #include "mozilla/dom/Selection.h"
#include "mozilla/dom/DocumentFragment.h"
#include "mozilla/dom/Element.h" #include "mozilla/dom/Element.h"
#include "mozilla/dom/EventTarget.h" #include "mozilla/dom/EventTarget.h"
#include "mozilla/dom/HTMLBodyElement.h" #include "mozilla/dom/HTMLBodyElement.h"
@ -1163,42 +1163,36 @@ nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(
} }
// This is mostly like InsertHTMLWithCharsetAndContext, /**
// but we can't use that because it is selection-based and * This is mostly like InsertHTMLWithCharsetAndContext, but we can't use that
// the rules code won't let us edit under the <head> node * because it is selection-based and the rules code won't let us edit under the
* <head> node
*/
NS_IMETHODIMP NS_IMETHODIMP
nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert) nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
{ {
nsAutoRules beginRulesSniffing(this, EditAction::ignore, nsIEditor::eNone); // don't do any post processing, rules get confused // don't do any post processing, rules get confused
nsCOMPtr<nsISelection> selection; nsAutoRules beginRulesSniffing(this, EditAction::ignore, nsIEditor::eNone);
nsresult res = GetSelection(getter_AddRefs(selection)); nsRefPtr<Selection> selection = GetSelection();
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
ForceCompositionEnd(); ForceCompositionEnd();
// Do not use nsAutoRules -- rules code won't let us insert in <head> // Do not use nsAutoRules -- rules code won't let us insert in <head>. Use
// Use the head node as a parent and delete/insert directly // the head node as a parent and delete/insert directly.
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak); nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED); NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
nsCOMPtr<nsIDOMNodeList>nodeList; nsRefPtr<nsContentList> nodeList =
res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList)); doc->GetElementsByTagName(NS_LITERAL_STRING("head"));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
uint32_t count; nsCOMPtr<nsIContent> headNode = nodeList->Item(0);
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); NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
// First, make sure there are no return chars in the source. // First, make sure there are no return chars in the source. Bad things
// Bad things happen if you insert returns (instead of dom newlines, \n) // happen if you insert returns (instead of dom newlines, \n) into an editor
// into an editor document. // document.
nsAutoString inputString (aSourceToInsert); // hope this does copy-on-write nsAutoString inputString (aSourceToInsert); // hope this does copy-on-write
// Windows linebreaks: Map CRLF to LF: // Windows linebreaks: Map CRLF to LF:
@ -1211,61 +1205,42 @@ nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
nsAutoEditBatch beginBatching(this); nsAutoEditBatch beginBatching(this);
res = GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(res, res);
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
// Get the first range in the selection, for context: // Get the first range in the selection, for context:
nsCOMPtr<nsIDOMRange> range; nsRefPtr<nsRange> range = selection->GetRangeAt(0);
res = selection->GetRangeAt(0, getter_AddRefs(range)); NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
NS_ENSURE_SUCCESS(res, res);
nsCOMPtr<nsIDOMDocumentFragment> docfrag; ErrorResult err;
res = range->CreateContextualFragment(inputString, nsRefPtr<DocumentFragment> docfrag =
getter_AddRefs(docfrag)); range->CreateContextualFragment(inputString, err);
// XXXX BUG 50965: This is not returning the text between <title>...</title> // XXXX BUG 50965: This is not returning the text between <title>...</title>
// Special code is needed in JS to handle title anyway, so it really doesn't matter! // Special code is needed in JS to handle title anyway, so it doesn't matter!
if (NS_FAILED(res)) if (err.Failed()) {
{
#ifdef DEBUG #ifdef DEBUG
printf("Couldn't create contextual fragment: error was %X\n", printf("Couldn't create contextual fragment: error was %X\n",
static_cast<uint32_t>(res)); static_cast<uint32_t>(err.ErrorCode()));
#endif #endif
return res; return err.ErrorCode();
} }
NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER); NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIDOMNode> child;
// First delete all children in head // First delete all children in head
do { while (nsCOMPtr<nsIContent> child = headNode->GetFirstChild()) {
res = headNode->GetFirstChild(getter_AddRefs(child)); nsresult res = DeleteNode(child);
NS_ENSURE_SUCCESS(res, res);
if (child)
{
res = DeleteNode(child);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
} while (child);
// Now insert the new nodes // Now insert the new nodes
int32_t offsetOfNewNode = 0; int32_t offsetOfNewNode = 0;
nsCOMPtr<nsIDOMNode> fragmentAsNode (do_QueryInterface(docfrag));
// Loop over the contents of the fragment and move into the document // Loop over the contents of the fragment and move into the document
do { while (nsCOMPtr<nsIContent> child = docfrag->GetFirstChild()) {
res = fragmentAsNode->GetFirstChild(getter_AddRefs(child)); nsresult res = InsertNode(child, headNode, offsetOfNewNode++);
NS_ENSURE_SUCCESS(res, res);
if (child)
{
res = InsertNode(child, headNode, offsetOfNewNode++);
NS_ENSURE_SUCCESS(res, res); NS_ENSURE_SUCCESS(res, res);
} }
} while (child);
return res; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP