Bug 722412 - Cleanup nsPlaintextEditor::SetDocumentCharacterSet; r=ehsan

This commit is contained in:
Ms2ger 2012-02-01 11:54:22 +01:00
parent a10b7e2138
commit 0df4df210c
2 changed files with 95 additions and 79 deletions

View File

@ -232,94 +232,107 @@ nsPlaintextEditor::EndEditorInit()
NS_IMETHODIMP
nsPlaintextEditor::SetDocumentCharacterSet(const nsACString& characterSet)
{
nsresult result;
nsresult rv = nsEditor::SetDocumentCharacterSet(characterSet);
NS_ENSURE_SUCCESS(rv, rv);
result = nsEditor::SetDocumentCharacterSet(characterSet);
// update META charset tag
if (NS_SUCCEEDED(result)) {
// Update META charset element.
nsCOMPtr<nsIDOMDocument> domdoc;
result = GetDocument(getter_AddRefs(domdoc));
if (NS_SUCCEEDED(result) && domdoc) {
nsCOMPtr<nsIDOMNodeList>metaList;
nsCOMPtr<nsIDOMElement>metaElement;
bool newMetaCharset = true;
rv = GetDocument(getter_AddRefs(domdoc));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(domdoc, NS_ERROR_FAILURE);
if (UpdateMetaCharset(domdoc, characterSet)) {
return NS_OK;
}
nsCOMPtr<nsIDOMNodeList> headList;
rv = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(headList));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(headList, NS_OK);
nsCOMPtr<nsIDOMNode> headNode;
headList->Item(0, getter_AddRefs(headNode));
NS_ENSURE_TRUE(headNode, NS_OK);
// Create a new meta charset tag
nsCOMPtr<nsIDOMNode> 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);
// Set attributes to the created element
if (characterSet.IsEmpty()) {
return NS_OK;
}
nsCOMPtr<dom::Element> metaElement = do_QueryInterface(resultNode);
if (!metaElement) {
return NS_OK;
}
// 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;
}
bool
nsPlaintextEditor::UpdateMetaCharset(nsIDOMDocument* aDocument,
const nsACString& aCharacterSet)
{
// 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<nsIDOMNodeList> metaList;
nsresult rv = aDocument->GetElementsByTagName(NS_LITERAL_STRING("meta"),
getter_AddRefs(metaList));
NS_ENSURE_SUCCESS(rv, false);
NS_ENSURE_TRUE(metaList, false);
nsCOMPtr<nsIDOMNode>metaNode;
for (PRUint32 i = 0; i < listLength; i++) {
metaList->Item(i, getter_AddRefs(metaNode));
if (!metaNode) continue;
metaElement = do_QueryInterface(metaNode);
if (!metaElement) continue;
PRUint32 listLength = 0;
metaList->GetLength(&listLength);
for (PRUint32 i = 0; i < listLength; ++i) {
nsCOMPtr<nsIContent> metaNode = metaList->GetNodeAt(i);
MOZ_ASSERT(metaNode);
if (!metaNode->IsElement()) {
continue;
}
nsAutoString currentValue;
if (NS_FAILED(metaElement->GetAttribute(NS_LITERAL_STRING("http-equiv"), currentValue))) continue;
metaNode->GetAttr(kNameSpaceID_None, nsGkAtoms::httpEquiv, currentValue);
if (FindInReadable(NS_LITERAL_STRING("content-type"),
if (!FindInReadable(NS_LITERAL_STRING("content-type"),
currentValue,
nsCaseInsensitiveStringComparator())) {
NS_NAMED_LITERAL_STRING(content, "content");
if (NS_FAILED(metaElement->GetAttribute(content, currentValue))) continue;
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,
if (!FindInReadable(charsetEquals, start, end,
nsCaseInsensitiveStringComparator())) {
continue;
}
// set attribute to <original prefix> charset=text/html
result = nsEditor::SetAttribute(metaElement, content,
nsCOMPtr<nsIDOMElement> metaElement = do_QueryInterface(metaNode);
MOZ_ASSERT(metaElement);
rv = nsEditor::SetAttribute(metaElement, NS_LITERAL_STRING("content"),
Substring(originalStart, start) +
charsetEquals + NS_ConvertASCIItoUTF16(characterSet));
if (NS_SUCCEEDED(result))
newMetaCharset = false;
break;
charsetEquals +
NS_ConvertASCIItoUTF16(aCharacterSet));
return NS_SUCCEEDED(rv);
}
return false;
}
}
}
if (newMetaCharset) {
nsCOMPtr<nsIDOMNodeList>headList;
result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList));
if (NS_SUCCEEDED(result) && headList) {
nsCOMPtr<nsIDOMNode>headNode;
headList->Item(0, getter_AddRefs(headNode));
if (headNode) {
nsCOMPtr<nsIDOMNode>resultNode;
// Create a new meta charset tag
result = CreateNode(NS_LITERAL_STRING("meta"), headNode, 0, getter_AddRefs(resultNode));
NS_ENSURE_SUCCESS(result, NS_ERROR_FAILURE);
// 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));
}
}
}
}
}
}
}
}
return result;
}
NS_IMETHODIMP nsPlaintextEditor::InitRules()
{

View File

@ -221,6 +221,9 @@ protected:
bool CanCutOrCopy();
bool FireClipboardEvent(PRInt32 aType);
bool UpdateMetaCharset(nsIDOMDocument* aDocument,
const nsACString& aCharacterSet);
// Data members
protected: