From 0df4df210c4594998882fa9d7c75d069a7ca346e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 1 Feb 2012 11:54:22 +0100 Subject: [PATCH] Bug 722412 - Cleanup nsPlaintextEditor::SetDocumentCharacterSet; r=ehsan --- editor/libeditor/text/nsPlaintextEditor.cpp | 171 +++++++++++--------- editor/libeditor/text/nsPlaintextEditor.h | 3 + 2 files changed, 95 insertions(+), 79 deletions(-) diff --git a/editor/libeditor/text/nsPlaintextEditor.cpp b/editor/libeditor/text/nsPlaintextEditor.cpp index ba46da26625..42c78858292 100644 --- a/editor/libeditor/text/nsPlaintextEditor.cpp +++ b/editor/libeditor/text/nsPlaintextEditor.cpp @@ -229,97 +229,110 @@ nsPlaintextEditor::EndEditorInit() return res; } -NS_IMETHODIMP -nsPlaintextEditor::SetDocumentCharacterSet(const nsACString & characterSet) -{ - nsresult result; +NS_IMETHODIMP +nsPlaintextEditor::SetDocumentCharacterSet(const nsACString& characterSet) +{ + nsresult rv = nsEditor::SetDocumentCharacterSet(characterSet); + NS_ENSURE_SUCCESS(rv, rv); - result = nsEditor::SetDocumentCharacterSet(characterSet); + // Update META charset element. + nsCOMPtr domdoc; + rv = GetDocument(getter_AddRefs(domdoc)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(domdoc, NS_ERROR_FAILURE); - // update META charset tag - if (NS_SUCCEEDED(result)) { - nsCOMPtrdomdoc; - result = GetDocument(getter_AddRefs(domdoc)); - if (NS_SUCCEEDED(result) && domdoc) { - nsCOMPtrmetaList; - nsCOMPtrmetaElement; - bool newMetaCharset = true; + if (UpdateMetaCharset(domdoc, characterSet)) { + return NS_OK; + } - // get a list of META tags - result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("meta"), getter_AddRefs(metaList)); - if (NS_SUCCEEDED(result) && metaList) { - PRUint32 listLength = 0; - (void) metaList->GetLength(&listLength); + nsCOMPtr headList; + rv = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(headList)); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(headList, NS_OK); - nsCOMPtrmetaNode; - for (PRUint32 i = 0; i < listLength; i++) { - metaList->Item(i, getter_AddRefs(metaNode)); - if (!metaNode) continue; - metaElement = do_QueryInterface(metaNode); - if (!metaElement) continue; + nsCOMPtr headNode; + headList->Item(0, getter_AddRefs(headNode)); + NS_ENSURE_TRUE(headNode, NS_OK); - nsAutoString currentValue; - if (NS_FAILED(metaElement->GetAttribute(NS_LITERAL_STRING("http-equiv"), currentValue))) continue; + // Create a new meta charset tag + nsCOMPtr resultNode; + rv = CreateNode(NS_LITERAL_STRING("meta"), headNode, 0, getter_AddRefs(resultNode)); + NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); + NS_ENSURE_TRUE(resultNode, NS_OK); - if (FindInReadable(NS_LITERAL_STRING("content-type"), - currentValue, - nsCaseInsensitiveStringComparator())) { - NS_NAMED_LITERAL_STRING(content, "content"); - if (NS_FAILED(metaElement->GetAttribute(content, currentValue))) continue; + // Set attributes to the created element + if (characterSet.IsEmpty()) { + return NS_OK; + } - NS_NAMED_LITERAL_STRING(charsetEquals, "charset="); - nsAString::const_iterator originalStart, start, end; - originalStart = currentValue.BeginReading(start); - currentValue.EndReading(end); - if (FindInReadable(charsetEquals, start, end, - nsCaseInsensitiveStringComparator())) { + nsCOMPtr metaElement = do_QueryInterface(resultNode); + if (!metaElement) { + return NS_OK; + } - // set attribute to charset=text/html - result = nsEditor::SetAttribute(metaElement, content, - Substring(originalStart, start) + - charsetEquals + NS_ConvertASCIItoUTF16(characterSet)); - if (NS_SUCCEEDED(result)) - newMetaCharset = false; - break; - } - } - } - } + // not undoable, undo should undo CreateNode + metaElement->SetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, + NS_LITERAL_STRING("Content-Type"), true); + metaElement->SetAttr(kNameSpaceID_None, nsGkAtoms::content, + NS_LITERAL_STRING("text/html;charset=") + + NS_ConvertASCIItoUTF16(characterSet), + true); + return NS_OK; +} - if (newMetaCharset) { - nsCOMPtrheadList; - result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList)); - if (NS_SUCCEEDED(result) && headList) { - nsCOMPtrheadNode; - headList->Item(0, getter_AddRefs(headNode)); - if (headNode) { - nsCOMPtrresultNode; - // Create a new meta charset tag - result = CreateNode(NS_LITERAL_STRING("meta"), headNode, 0, getter_AddRefs(resultNode)); - NS_ENSURE_SUCCESS(result, NS_ERROR_FAILURE); +bool +nsPlaintextEditor::UpdateMetaCharset(nsIDOMDocument* aDocument, + const nsACString& aCharacterSet) +{ + // get a list of META tags + nsCOMPtr metaList; + nsresult rv = aDocument->GetElementsByTagName(NS_LITERAL_STRING("meta"), + getter_AddRefs(metaList)); + NS_ENSURE_SUCCESS(rv, false); + NS_ENSURE_TRUE(metaList, false); - // Set attributes to the created element - if (resultNode && !characterSet.IsEmpty()) { - metaElement = do_QueryInterface(resultNode); - if (metaElement) { - // not undoable, undo should undo CreateNode - result = metaElement->SetAttribute(NS_LITERAL_STRING("http-equiv"), NS_LITERAL_STRING("Content-Type")); - if (NS_SUCCEEDED(result)) { - // not undoable, undo should undo CreateNode - result = metaElement->SetAttribute(NS_LITERAL_STRING("content"), - NS_LITERAL_STRING("text/html;charset=") + NS_ConvertASCIItoUTF16(characterSet)); - } - } - } - } - } - } - } - } + PRUint32 listLength = 0; + metaList->GetLength(&listLength); - return result; -} + for (PRUint32 i = 0; i < listLength; ++i) { + nsCOMPtr metaNode = metaList->GetNodeAt(i); + MOZ_ASSERT(metaNode); + if (!metaNode->IsElement()) { + continue; + } + + nsAutoString currentValue; + metaNode->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, currentValue); + + if (!FindInReadable(NS_LITERAL_STRING("content-type"), + currentValue, + nsCaseInsensitiveStringComparator())) { + continue; + } + + metaNode->GetAttr(kNameSpaceID_None, nsGkAtoms::content, currentValue); + + NS_NAMED_LITERAL_STRING(charsetEquals, "charset="); + nsAString::const_iterator originalStart, start, end; + originalStart = currentValue.BeginReading(start); + currentValue.EndReading(end); + if (!FindInReadable(charsetEquals, start, end, + nsCaseInsensitiveStringComparator())) { + continue; + } + + // set attribute to charset=text/html + nsCOMPtr metaElement = do_QueryInterface(metaNode); + MOZ_ASSERT(metaElement); + rv = nsEditor::SetAttribute(metaElement, NS_LITERAL_STRING("content"), + Substring(originalStart, start) + + charsetEquals + + NS_ConvertASCIItoUTF16(aCharacterSet)); + return NS_SUCCEEDED(rv); + } + return false; +} NS_IMETHODIMP nsPlaintextEditor::InitRules() { diff --git a/editor/libeditor/text/nsPlaintextEditor.h b/editor/libeditor/text/nsPlaintextEditor.h index f56ff520e39..5f6082b0099 100644 --- a/editor/libeditor/text/nsPlaintextEditor.h +++ b/editor/libeditor/text/nsPlaintextEditor.h @@ -221,6 +221,9 @@ protected: bool CanCutOrCopy(); bool FireClipboardEvent(PRInt32 aType); + bool UpdateMetaCharset(nsIDOMDocument* aDocument, + const nsACString& aCharacterSet); + // Data members protected: