diff --git a/editor/libeditor/text/nsTextEditRules.cpp b/editor/libeditor/text/nsTextEditRules.cpp index d6ea49e405e..9180ad2a60a 100644 --- a/editor/libeditor/text/nsTextEditRules.cpp +++ b/editor/libeditor/text/nsTextEditRules.cpp @@ -1052,52 +1052,36 @@ nsTextEditRules::RemoveRedundantTrailingBR() if (IsSingleLineEditor()) return NS_OK; - nsCOMPtr body = do_QueryInterface(mEditor->GetRoot()); + nsRefPtr body = mEditor->GetRoot(); if (!body) return NS_ERROR_NULL_POINTER; - bool hasChildren; - nsresult res = body->HasChildNodes(&hasChildren); - NS_ENSURE_SUCCESS(res, res); - - if (hasChildren) { - nsCOMPtr childList; - res = body->GetChildNodes(getter_AddRefs(childList)); - NS_ENSURE_SUCCESS(res, res); - - if (!childList) - return NS_ERROR_NULL_POINTER; - - PRUint32 childCount; - res = childList->GetLength(&childCount); - NS_ENSURE_SUCCESS(res, res); - + PRUint32 childCount = body->GetChildCount(); + if (childCount > 1) { // The trailing br is redundant if it is the only remaining child node - if (childCount != 1) - return NS_OK; - - nsCOMPtr child; - res = body->GetFirstChild(getter_AddRefs(child)); - NS_ENSURE_SUCCESS(res, res); - - if (nsTextEditUtils::IsMozBR(child)) { - // Rather than deleting this node from the DOM tree we should instead - // morph this br into the bogus node - nsCOMPtr elem = do_QueryInterface(child); - if (elem) { - elem->RemoveAttribute(NS_LITERAL_STRING("type")); - NS_ENSURE_SUCCESS(res, res); - - // set mBogusNode to be this
- mBogusNode = elem; - - // give it the bogus node attribute - nsCOMPtr content = do_QueryInterface(elem); - content->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom, - kMOZEditorBogusNodeValue, false); - } - } + return NS_OK; } + + nsRefPtr child = body->GetFirstChild(); + if (!child || !child->IsElement()) { + return NS_OK; + } + + dom::Element* elem = child->AsElement(); + if (!nsTextEditUtils::IsMozBR(elem)) { + return NS_OK; + } + + // Rather than deleting this node from the DOM tree we should instead + // morph this br into the bogus node + elem->UnsetAttr(kNameSpaceID_None, nsGkAtoms::type, true); + + // set mBogusNode to be this
+ mBogusNode = do_QueryInterface(elem); + + // give it the bogus node attribute + elem->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom, + kMOZEditorBogusNodeValue, false); return NS_OK; } diff --git a/editor/libeditor/text/nsTextEditUtils.cpp b/editor/libeditor/text/nsTextEditUtils.cpp index edbb0fbc8a7..71b0bc70090 100644 --- a/editor/libeditor/text/nsTextEditUtils.cpp +++ b/editor/libeditor/text/nsTextEditUtils.cpp @@ -37,10 +37,14 @@ #include "nsTextEditUtils.h" +#include "mozilla/dom/Element.h" + #include "nsEditor.h" #include "nsPlaintextEditor.h" #include "nsEditProperty.h" +using namespace mozilla; + /////////////////////////////////////////////////////////////////////////// // IsBody: true if node an html body node // @@ -72,11 +76,19 @@ bool nsTextEditUtils::IsMozBR(nsIDOMNode *node) { NS_PRECONDITION(node, "null node passed to nsHTMLEditUtils::IsMozBR"); - if (IsBreak(node) && HasMozAttr(node)) return true; - return false; + return IsBreak(node) && HasMozAttr(node); } +bool +nsTextEditUtils::IsMozBR(dom::Element* aNode) +{ + MOZ_ASSERT(aNode); + return aNode->IsHTML(nsGkAtoms::br) && + aNode->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + NS_LITERAL_STRING("_moz"), eIgnoreCase); +} + /////////////////////////////////////////////////////////////////////////// // HasMozAttr: true if node has type attribute = _moz // (used to indicate the div's and br's we use in diff --git a/editor/libeditor/text/nsTextEditUtils.h b/editor/libeditor/text/nsTextEditUtils.h index ce796603a8f..5dfa7eddcc8 100644 --- a/editor/libeditor/text/nsTextEditUtils.h +++ b/editor/libeditor/text/nsTextEditUtils.h @@ -38,37 +38,40 @@ #ifndef nsTextEditUtils_h__ #define nsTextEditUtils_h__ -#include "prtypes.h" // for bool #include "nsError.h" // for nsresult -#include "nsString.h" // for nsAString + +namespace mozilla { +namespace dom { +class Element; +} // namespace dom +} // namespace mozilla + class nsIDOMNode; -class nsIEditor; class nsPlaintextEditor; class nsTextEditUtils { public: // from nsTextEditRules: - static bool IsBody(nsIDOMNode *aNode); - static bool IsBreak(nsIDOMNode *aNode); - static bool IsMozBR(nsIDOMNode *aNode); - static bool HasMozAttr(nsIDOMNode *aNode); + static bool IsBody(nsIDOMNode* aNode); + static bool IsBreak(nsIDOMNode* aNode); + static bool IsMozBR(nsIDOMNode* aNode); + static bool IsMozBR(mozilla::dom::Element* aNode); + static bool HasMozAttr(nsIDOMNode* aNode); }; /*************************************************************************** * stack based helper class for detecting end of editor initialization, in - * order to triger "end of init" initialization of the edit rules. + * order to trigger "end of init" initialization of the edit rules. */ class nsAutoEditInitRulesTrigger { - private: - nsPlaintextEditor *mEd; - nsresult &mRes; - public: - nsAutoEditInitRulesTrigger( nsPlaintextEditor *aEd, nsresult &aRes); +private: + nsPlaintextEditor* mEd; + nsresult& mRes; +public: + nsAutoEditInitRulesTrigger(nsPlaintextEditor* aEd, nsresult& aRes); ~nsAutoEditInitRulesTrigger(); }; - #endif /* nsTextEditUtils_h__ */ -