From 0ee480bf67a386f49bed3a443eb3d80d6e5efe7f Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Fri, 10 Jul 2009 14:07:04 +1200 Subject: [PATCH] Bug 502156. Make editor use nsContentUtils::CreateContextualFragment instead of instantiating a parser directly, when HTML content is pasted/dropped. This removes some code and lets the editor use the HTML5 parser if enabled. r+sr=peterv --- editor/libeditor/html/nsHTMLDataTransfer.cpp | 101 ++----------------- editor/libeditor/html/nsHTMLEditor.h | 5 - 2 files changed, 10 insertions(+), 96 deletions(-) diff --git a/editor/libeditor/html/nsHTMLDataTransfer.cpp b/editor/libeditor/html/nsHTMLDataTransfer.cpp index 9c46f727deac..b34c9cab0f37 100644 --- a/editor/libeditor/html/nsHTMLDataTransfer.cpp +++ b/editor/libeditor/html/nsHTMLDataTransfer.cpp @@ -2545,24 +2545,22 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, { if (!outFragNode || !outStartNode || !outEndNode) return NS_ERROR_NULL_POINTER; - nsCOMPtr docfrag; nsCOMPtr contextAsNode, tmp; nsresult res = NS_OK; nsCOMPtr domDoc; GetDocument(getter_AddRefs(domDoc)); - nsCOMPtr doc = do_QueryInterface(domDoc); - NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE); - // if we have context info, create a fragment for that - nsAutoTArray tagStack; - nsCOMPtr contextfrag; nsCOMPtr contextLeaf, junk; if (!aContextStr.IsEmpty()) { - res = ParseFragment(aContextStr, tagStack, doc, address_of(contextAsNode)); + nsCOMPtr df; + res = nsContentUtils::CreateContextualFragment(domDoc, aContextStr, + PR_FALSE, + getter_AddRefs(df)); NS_ENSURE_SUCCESS(res, res); + contextAsNode = do_QueryInterface(df); NS_ENSURE_TRUE(contextAsNode, NS_ERROR_FAILURE); res = StripFormattingNodes(contextAsNode); @@ -2576,13 +2574,12 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, NS_ENSURE_SUCCESS(res, res); } - // get the tagstack for the context - res = CreateTagStack(tagStack, contextLeaf); - NS_ENSURE_SUCCESS(res, res); - - // create fragment for pasted html - res = ParseFragment(aInputString, tagStack, doc, outFragNode); + nsCOMPtr df; + res = nsContentUtils::CreateContextualFragment(contextLeaf, aInputString, + PR_FALSE, + getter_AddRefs(df)); NS_ENSURE_SUCCESS(res, res); + *outFragNode = df; NS_ENSURE_TRUE(*outFragNode, NS_ERROR_FAILURE); RemoveBodyAndHead(*outFragNode); @@ -2639,84 +2636,6 @@ nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString, return res; } - -nsresult nsHTMLEditor::ParseFragment(const nsAString & aFragStr, - nsTArray &aTagStack, - nsIDocument* aTargetDocument, - nsCOMPtr *outNode) -{ - // figure out if we are parsing full context or not - PRBool bContext = aTagStack.IsEmpty(); - - // create the parser to do the conversion. - nsresult res; - nsCOMPtr parser = do_CreateInstance(kCParserCID, &res); - NS_ENSURE_SUCCESS(res, res); - NS_ENSURE_TRUE(parser, NS_ERROR_FAILURE); - - // create the html fragment sink - nsCOMPtr sink; - if (bContext) - sink = do_CreateInstance(NS_HTMLFRAGMENTSINK2_CONTRACTID); - else - sink = do_CreateInstance(NS_HTMLFRAGMENTSINK_CONTRACTID); - - NS_ENSURE_TRUE(sink, NS_ERROR_FAILURE); - nsCOMPtr fragSink(do_QueryInterface(sink)); - NS_ENSURE_TRUE(fragSink, NS_ERROR_FAILURE); - - fragSink->SetTargetDocument(aTargetDocument); - - // parse the fragment - parser->SetContentSink(sink); - if (bContext) - parser->Parse(aFragStr, (void*)0, NS_LITERAL_CSTRING("text/html"), PR_TRUE, eDTDMode_fragment); - else - parser->ParseFragment(aFragStr, 0, aTagStack, PR_FALSE, NS_LITERAL_CSTRING("text/html"), eDTDMode_quirks); - // get the fragment node - nsCOMPtr contextfrag; - res = fragSink->GetFragment(PR_TRUE, getter_AddRefs(contextfrag)); - NS_ENSURE_SUCCESS(res, res); - *outNode = do_QueryInterface(contextfrag); - - return res; -} - -nsresult nsHTMLEditor::CreateTagStack(nsTArray &aTagStack, nsIDOMNode *aNode) -{ - nsresult res = NS_OK; - nsCOMPtr node= aNode; - PRBool bSeenBody = PR_FALSE; - - while (node) - { - if (nsTextEditUtils::IsBody(node)) - bSeenBody = PR_TRUE; - nsCOMPtr temp = node; - PRUint16 nodeType; - - node->GetNodeType(&nodeType); - if (nsIDOMNode::ELEMENT_NODE == nodeType) - { - nsString* tagName = aTagStack.AppendElement(); - NS_ENSURE_TRUE(tagName, NS_ERROR_OUT_OF_MEMORY); - - node->GetNodeName(*tagName); - // printf("%s\n",NS_LossyConvertUTF16toASCII(tagName).get()); - } - - res = temp->GetParentNode(getter_AddRefs(node)); - NS_ENSURE_SUCCESS(res, res); - } - - if (!bSeenBody) - { - aTagStack.AppendElement(NS_LITERAL_STRING("BODY")); - } - return res; -} - - nsresult nsHTMLEditor::CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode, nsCOMArray& outNodeList, nsIDOMNode *aStartNode, diff --git a/editor/libeditor/html/nsHTMLEditor.h b/editor/libeditor/html/nsHTMLEditor.h index 61a692bb3d1d..a0fbd35d3369 100644 --- a/editor/libeditor/html/nsHTMLEditor.h +++ b/editor/libeditor/html/nsHTMLEditor.h @@ -608,17 +608,12 @@ protected: nsCOMPtr *outEndNode, PRInt32 *outStartOffset, PRInt32 *outEndOffset); - nsresult ParseFragment(const nsAString & aStr, nsTArray &aTagStack, - nsIDocument* aTargetDoc, - nsCOMPtr *outNode); nsresult CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode, nsCOMArray& outNodeList, nsIDOMNode *aStartNode, PRInt32 aStartOffset, nsIDOMNode *aEndNode, PRInt32 aEndOffset); - nsresult CreateTagStack(nsTArray &aTagStack, - nsIDOMNode *aNode); nsresult GetListAndTableParents( PRBool aEnd, nsCOMArray& aListOfNodes, nsCOMArray& outArray);