From 2f022ab1faeb4703ecd9d7ca6b912bbe397531f6 Mon Sep 17 00:00:00 2001 From: Gaming4JC Date: Fri, 22 May 2020 18:38:33 -0400 Subject: Issue #1621 - Part 3: Use nsIAtom to change attirbute if possible. We can replace old nsIEditor API with nsIAtom version. Ref: Bug 1324996 --- editor/libeditor/EditorBase.cpp | 14 ++++---- editor/libeditor/HTMLEditRules.cpp | 51 +++++++++++++--------------- editor/libeditor/HTMLEditor.cpp | 5 ++- editor/libeditor/HTMLEditorObjectResizer.cpp | 29 +++++++--------- editor/libeditor/HTMLStyleEditor.cpp | 8 ++--- editor/libeditor/TextEditRules.cpp | 4 +-- editor/libeditor/TextEditor.cpp | 4 +-- 7 files changed, 51 insertions(+), 64 deletions(-) (limited to 'editor/libeditor') diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index fd970390f..0cd3f01b7 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -2289,11 +2289,9 @@ EditorBase::CloneAttributes(Element* aDest, RefPtr destAttributes = aDest->Attributes(); while (RefPtr attr = destAttributes->Item(0)) { if (destInBody) { - RemoveAttribute(static_cast(GetAsDOMNode(aDest)), - attr->NodeName()); + RemoveAttribute(aDest, attr->NodeInfo()->NameAtom()); } else { - ErrorResult ignored; - aDest->RemoveAttribute(attr->NodeName(), ignored); + aDest->UnsetAttr(kNameSpaceID_None, attr->NodeInfo()->NameAtom(), true); } } @@ -2305,13 +2303,13 @@ EditorBase::CloneAttributes(Element* aDest, nsAutoString value; attr->GetValue(value); if (destInBody) { - SetAttributeOrEquivalent(static_cast(GetAsDOMNode(aDest)), - attr->NodeName(), value, false); + SetAttributeOrEquivalent(aDest, attr->NodeInfo()->NameAtom(), value, + false); } else { // The element is not inserted in the document yet, we don't want to put // a transaction on the UndoStack - SetAttributeOrEquivalent(static_cast(GetAsDOMNode(aDest)), - attr->NodeName(), value, true); + SetAttributeOrEquivalent(aDest, attr->NodeInfo()->NameAtom(), value, + true); } } } diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp index b6f95f503..b6dad01c5 100644 --- a/editor/libeditor/HTMLEditRules.cpp +++ b/editor/libeditor/HTMLEditRules.cpp @@ -3302,15 +3302,15 @@ HTMLEditRules::WillMakeList(Selection* aSelection, } } NS_ENSURE_STATE(mHTMLEditor); - nsCOMPtr curElement = do_QueryInterface(curNode); - NS_NAMED_LITERAL_STRING(typestr, "type"); + nsCOMPtr curElement = do_QueryInterface(curNode); if (aBulletType && !aBulletType->IsEmpty()) { - rv = mHTMLEditor->SetAttribute(curElement, typestr, *aBulletType); + rv = mHTMLEditor->SetAttribute(curElement, nsGkAtoms::type, + *aBulletType); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } else { - rv = mHTMLEditor->RemoveAttribute(curElement, typestr); + rv = mHTMLEditor->RemoveAttribute(curElement, nsGkAtoms::type); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -4835,7 +4835,6 @@ HTMLEditRules::AlignBlockContents(nsIDOMNode* aNode, nsCOMPtr node = do_QueryInterface(aNode); NS_ENSURE_TRUE(node && alignType, NS_ERROR_NULL_POINTER); nsCOMPtr firstChild, lastChild; - nsCOMPtr divNode; bool useCSS = mHTMLEditor->IsCSSEnabled(); @@ -4843,24 +4842,25 @@ HTMLEditRules::AlignBlockContents(nsIDOMNode* aNode, firstChild = mHTMLEditor->GetFirstEditableChild(*node); NS_ENSURE_STATE(mHTMLEditor); lastChild = mHTMLEditor->GetLastEditableChild(*node); - NS_NAMED_LITERAL_STRING(attr, "align"); if (!firstChild) { // this cell has no content, nothing to align } else if (firstChild == lastChild && firstChild->IsHTMLElement(nsGkAtoms::div)) { // the cell already has a div containing all of its content: just // act on this div. - nsCOMPtr divElem = do_QueryInterface(firstChild); + RefPtr divElem = firstChild->AsElement(); if (useCSS) { NS_ENSURE_STATE(mHTMLEditor); - nsresult rv = mHTMLEditor->SetAttributeOrEquivalent(divElem, attr, + nsresult rv = mHTMLEditor->SetAttributeOrEquivalent(divElem, + nsGkAtoms::align, *alignType, false); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } else { NS_ENSURE_STATE(mHTMLEditor); - nsresult rv = mHTMLEditor->SetAttribute(divElem, attr, *alignType); + nsresult rv = mHTMLEditor->SetAttribute(divElem, nsGkAtoms::align, + *alignType); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -4868,28 +4868,29 @@ HTMLEditRules::AlignBlockContents(nsIDOMNode* aNode, } else { // else we need to put in a div, set the alignment, and toss in all the children NS_ENSURE_STATE(mHTMLEditor); - divNode = mHTMLEditor->CreateNode(nsGkAtoms::div, node, 0); - NS_ENSURE_STATE(divNode); + RefPtr divElem = mHTMLEditor->CreateNode(nsGkAtoms::div, node, 0); + NS_ENSURE_STATE(divElem); // set up the alignment on the div - nsCOMPtr divElem = do_QueryInterface(divNode); if (useCSS) { NS_ENSURE_STATE(mHTMLEditor); nsresult rv = - mHTMLEditor->SetAttributeOrEquivalent(divElem, attr, *alignType, false); + mHTMLEditor->SetAttributeOrEquivalent(divElem, nsGkAtoms::align, + *alignType, false); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } else { NS_ENSURE_STATE(mHTMLEditor); - nsresult rv = mHTMLEditor->SetAttribute(divElem, attr, *alignType); + nsresult rv = + mHTMLEditor->SetAttribute(divElem, nsGkAtoms::align, *alignType); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } } // tuck the children into the end of the active div - while (lastChild && (lastChild != divNode)) { + while (lastChild && (lastChild != divElem)) { NS_ENSURE_STATE(mHTMLEditor); - nsresult rv = mHTMLEditor->MoveNode(lastChild, divNode, 0); + nsresult rv = mHTMLEditor->MoveNode(lastChild, divElem, 0); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_STATE(mHTMLEditor); lastChild = mHTMLEditor->GetLastEditableChild(*node); @@ -6492,9 +6493,9 @@ HTMLEditRules::SplitParagraph(nsIDOMNode *aPara, } // remove ID attribute on the paragraph we just created - nsCOMPtr rightElt = do_QueryInterface(rightPara); + RefPtr rightElt = rightPara->AsElement(); NS_ENSURE_STATE(mHTMLEditor); - rv = mHTMLEditor->RemoveAttribute(rightElt, NS_LITERAL_STRING("id")); + rv = mHTMLEditor->RemoveAttribute(rightElt, nsGkAtoms::id); NS_ENSURE_SUCCESS(rv, rv); // check both halves of para to see if we need mozBR @@ -8355,18 +8356,18 @@ HTMLEditRules::RemoveAlignment(nsIDOMNode* aNode, NS_ENSURE_SUCCESS(rv, rv); } else if (isBlock || HTMLEditUtils::IsHR(child)) { // the current node is a block element - nsCOMPtr curElem = do_QueryInterface(child); + nsCOMPtr curElem = do_QueryInterface(child); if (HTMLEditUtils::SupportsAlignAttr(child)) { // remove the ALIGN attribute if this element can have it NS_ENSURE_STATE(mHTMLEditor); - rv = mHTMLEditor->RemoveAttribute(curElem, NS_LITERAL_STRING("align")); + rv = mHTMLEditor->RemoveAttribute(curElem, nsGkAtoms::align); NS_ENSURE_SUCCESS(rv, rv); } if (useCSS) { if (HTMLEditUtils::IsTable(child) || HTMLEditUtils::IsHR(child)) { NS_ENSURE_STATE(mHTMLEditor); rv = mHTMLEditor->SetAttributeOrEquivalent(curElem, - NS_LITERAL_STRING("align"), + nsGkAtoms::align, aAlignType, false); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; @@ -8486,21 +8487,17 @@ HTMLEditRules::AlignBlock(Element& aElement, nsresult rv = RemoveAlignment(aElement.AsDOMNode(), aAlignType, aContentsOnly == ContentsOnly::yes); NS_ENSURE_SUCCESS(rv, rv); - NS_NAMED_LITERAL_STRING(attr, "align"); if (htmlEditor->IsCSSEnabled()) { // Let's use CSS alignment; we use margin-left and margin-right for tables // and text-align for other block-level elements rv = htmlEditor->SetAttributeOrEquivalent( - static_cast(aElement.AsDOMNode()), - attr, aAlignType, false); + &aElement, nsGkAtoms::align, aAlignType, false); NS_ENSURE_SUCCESS(rv, rv); } else { // HTML case; this code is supposed to be called ONLY if the element // supports the align attribute but we'll never know... if (HTMLEditUtils::SupportsAlignAttr(aElement.AsDOMNode())) { - rv = htmlEditor->SetAttribute( - static_cast(aElement.AsDOMNode()), - attr, aAlignType); + rv = htmlEditor->SetAttribute(&aElement, nsGkAtoms::align, aAlignType); NS_ENSURE_SUCCESS(rv, rv); } } diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index cf0be447d..0a2ec023a 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -2584,7 +2584,7 @@ HTMLEditor::CreateElementWithDefaults(const nsAString& aTagName) // New call to use instead to get proper HTML element, bug 39919 nsCOMPtr realTagAtom = NS_Atomize(realTagName); - nsCOMPtr newElement = CreateHTMLContent(realTagAtom); + RefPtr newElement = CreateHTMLContent(realTagAtom); if (!newElement) { return nullptr; } @@ -2616,8 +2616,7 @@ HTMLEditor::CreateElementWithDefaults(const nsAString& aTagName) } else if (tagName.EqualsLiteral("td")) { nsresult rv = SetAttributeOrEquivalent( - static_cast(newElement->AsDOMNode()), - NS_LITERAL_STRING("valign"), NS_LITERAL_STRING("top"), true); + newElement, nsGkAtoms::valign, NS_LITERAL_STRING("top"), true); NS_ENSURE_SUCCESS(rv, nullptr); } // ADD OTHER TAGS HERE diff --git a/editor/libeditor/HTMLEditorObjectResizer.cpp b/editor/libeditor/HTMLEditorObjectResizer.cpp index 111a3f975..8ed5b6e4d 100644 --- a/editor/libeditor/HTMLEditorObjectResizer.cpp +++ b/editor/libeditor/HTMLEditorObjectResizer.cpp @@ -926,35 +926,30 @@ HTMLEditor::SetFinalSize(int32_t aX, // we want one transaction only from a user's point of view AutoEditBatch batchIt(this); - NS_NAMED_LITERAL_STRING(widthStr, "width"); - NS_NAMED_LITERAL_STRING(heightStr, "height"); - - nsCOMPtr resizedObject = do_QueryInterface(mResizedObject); - NS_ENSURE_TRUE(resizedObject, ); if (mResizedObjectIsAbsolutelyPositioned) { if (setHeight) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::top, y); + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::top, y); } if (setWidth) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::left, x); + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::left, x); } } if (IsCSSEnabled() || mResizedObjectIsAbsolutelyPositioned) { if (setWidth && mResizedObject->HasAttr(kNameSpaceID_None, nsGkAtoms::width)) { - RemoveAttribute(static_cast(GetAsDOMNode(mResizedObject)), widthStr); + RemoveAttribute(mResizedObject, nsGkAtoms::width); } if (setHeight && mResizedObject->HasAttr(kNameSpaceID_None, nsGkAtoms::height)) { - RemoveAttribute(static_cast(GetAsDOMNode(mResizedObject)), heightStr); + RemoveAttribute(mResizedObject, nsGkAtoms::height); } if (setWidth) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::width, + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::width, width); } if (setHeight) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::height, + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::height, height); } } else { @@ -964,30 +959,30 @@ HTMLEditor::SetFinalSize(int32_t aX, // triggering an immediate reflow; otherwise, we have problems // with asynchronous reflow if (setWidth) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::width, + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::width, width); } if (setHeight) { - mCSSEditUtils->SetCSSPropertyPixels(*resizedObject, *nsGkAtoms::height, + mCSSEditUtils->SetCSSPropertyPixels(*mResizedObject, *nsGkAtoms::height, height); } if (setWidth) { nsAutoString w; w.AppendInt(width); - SetAttribute(static_cast(GetAsDOMNode(mResizedObject)), widthStr, w); + SetAttribute(mResizedObject, nsGkAtoms::width, w); } if (setHeight) { nsAutoString h; h.AppendInt(height); - SetAttribute(static_cast(GetAsDOMNode(mResizedObject)), heightStr, h); + SetAttribute(mResizedObject, nsGkAtoms::height, h); } if (setWidth) { - mCSSEditUtils->RemoveCSSProperty(*resizedObject, *nsGkAtoms::width, + mCSSEditUtils->RemoveCSSProperty(*mResizedObject, *nsGkAtoms::width, EmptyString()); } if (setHeight) { - mCSSEditUtils->RemoveCSSProperty(*resizedObject, *nsGkAtoms::height, + mCSSEditUtils->RemoveCSSProperty(*mResizedObject, *nsGkAtoms::height, EmptyString()); } } diff --git a/editor/libeditor/HTMLStyleEditor.cpp b/editor/libeditor/HTMLStyleEditor.cpp index 593ef3d27..1a10dc830 100644 --- a/editor/libeditor/HTMLStyleEditor.cpp +++ b/editor/libeditor/HTMLStyleEditor.cpp @@ -742,8 +742,6 @@ HTMLEditor::RemoveStyleInside(nsIContent& aNode, // if we weren't passed an attribute, then we want to // remove any matching inlinestyles entirely if (!aAttribute || aAttribute->IsEmpty()) { - NS_NAMED_LITERAL_STRING(styleAttr, "style"); - NS_NAMED_LITERAL_STRING(classAttr, "class"); bool hasStyleAttr = aNode.HasAttr(kNameSpaceID_None, nsGkAtoms::style); bool hasClassAttr = aNode.HasAttr(kNameSpaceID_None, nsGkAtoms::_class); @@ -752,14 +750,14 @@ HTMLEditor::RemoveStyleInside(nsIContent& aNode, // just remove the element... We need to create above the element // a span that will carry those styles or class, then we can delete // the node. - nsCOMPtr spanNode = + RefPtr spanNode = InsertContainerAbove(&aNode, nsGkAtoms::span); NS_ENSURE_STATE(spanNode); nsresult rv = - CloneAttribute(styleAttr, spanNode->AsDOMNode(), aNode.AsDOMNode()); + CloneAttribute(nsGkAtoms::style, spanNode, aNode.AsElement()); NS_ENSURE_SUCCESS(rv, rv); rv = - CloneAttribute(classAttr, spanNode->AsDOMNode(), aNode.AsDOMNode()); + CloneAttribute(nsGkAtoms::_class, spanNode, aNode.AsElement()); NS_ENSURE_SUCCESS(rv, rv); } nsresult rv = RemoveContainer(&aNode); diff --git a/editor/libeditor/TextEditRules.cpp b/editor/libeditor/TextEditRules.cpp index 4ffcd2618..d441b49eb 100644 --- a/editor/libeditor/TextEditRules.cpp +++ b/editor/libeditor/TextEditRules.cpp @@ -1425,9 +1425,9 @@ TextEditRules::CreateMozBR(nsIDOMNode* inParent, NS_ENSURE_SUCCESS(rv, rv); // give it special moz attr - nsCOMPtr brElem = do_QueryInterface(brNode); + nsCOMPtr brElem = do_QueryInterface(brNode); if (brElem) { - rv = mTextEditor->SetAttribute(brElem, NS_LITERAL_STRING("type"), + rv = mTextEditor->SetAttribute(brElem, nsGkAtoms::type, NS_LITERAL_STRING("_moz")); NS_ENSURE_SUCCESS(rv, rv); } diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index 7eb91af64..d7284c4c2 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -310,9 +310,9 @@ TextEditor::UpdateMetaCharset(nsIDOMDocument* aDocument, } // set attribute to charset=text/html - nsCOMPtr metaElement = do_QueryInterface(metaNode); + RefPtr metaElement = metaNode->AsElement(); MOZ_ASSERT(metaElement); - rv = EditorBase::SetAttribute(metaElement, NS_LITERAL_STRING("content"), + rv = EditorBase::SetAttribute(metaElement, nsGkAtoms::content, Substring(originalStart, start) + charsetEquals + NS_ConvertASCIItoUTF16(aCharacterSet)); -- cgit v1.2.3