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

This commit is contained in:
Ms2ger 2012-02-01 11:54:22 +01:00
Родитель 1415cbeb8e
Коммит 51cc2598c8
2 изменённых файлов: 95 добавлений и 79 удалений

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

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

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

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