Bug 716207 - Part b: Cleanup nsTextEditRules::RemoveRedundantTrailingBR; r=ehsan

This commit is contained in:
Ms2ger 2012-01-11 09:23:07 +01:00
Родитель d7ac30b1d2
Коммит c635e3e706
3 изменённых файлов: 57 добавлений и 58 удалений

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

@ -1052,52 +1052,36 @@ nsTextEditRules::RemoveRedundantTrailingBR()
if (IsSingleLineEditor()) if (IsSingleLineEditor())
return NS_OK; return NS_OK;
nsCOMPtr<nsIDOMNode> body = do_QueryInterface(mEditor->GetRoot()); nsRefPtr<dom::Element> body = mEditor->GetRoot();
if (!body) if (!body)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
bool hasChildren; PRUint32 childCount = body->GetChildCount();
nsresult res = body->HasChildNodes(&hasChildren); if (childCount > 1) {
NS_ENSURE_SUCCESS(res, res);
if (hasChildren) {
nsCOMPtr<nsIDOMNodeList> 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);
// The trailing br is redundant if it is the only remaining child node // The trailing br is redundant if it is the only remaining child node
if (childCount != 1)
return NS_OK; return NS_OK;
}
nsCOMPtr<nsIDOMNode> child; nsRefPtr<nsIContent> child = body->GetFirstChild();
res = body->GetFirstChild(getter_AddRefs(child)); if (!child || !child->IsElement()) {
NS_ENSURE_SUCCESS(res, res); return NS_OK;
}
dom::Element* elem = child->AsElement();
if (!nsTextEditUtils::IsMozBR(elem)) {
return NS_OK;
}
if (nsTextEditUtils::IsMozBR(child)) {
// Rather than deleting this node from the DOM tree we should instead // Rather than deleting this node from the DOM tree we should instead
// morph this br into the bogus node // morph this br into the bogus node
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(child); elem->UnsetAttr(kNameSpaceID_None, nsGkAtoms::type, true);
if (elem) {
elem->RemoveAttribute(NS_LITERAL_STRING("type"));
NS_ENSURE_SUCCESS(res, res);
// set mBogusNode to be this <br> // set mBogusNode to be this <br>
mBogusNode = elem; mBogusNode = do_QueryInterface(elem);
// give it the bogus node attribute // give it the bogus node attribute
nsCOMPtr<nsIContent> content = do_QueryInterface(elem); elem->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
content->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false); kMOZEditorBogusNodeValue, false);
}
}
}
return NS_OK; return NS_OK;
} }

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

@ -37,10 +37,14 @@
#include "nsTextEditUtils.h" #include "nsTextEditUtils.h"
#include "mozilla/dom/Element.h"
#include "nsEditor.h" #include "nsEditor.h"
#include "nsPlaintextEditor.h" #include "nsPlaintextEditor.h"
#include "nsEditProperty.h" #include "nsEditProperty.h"
using namespace mozilla;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// IsBody: true if node an html body node // IsBody: true if node an html body node
// //
@ -72,11 +76,19 @@ bool
nsTextEditUtils::IsMozBR(nsIDOMNode *node) nsTextEditUtils::IsMozBR(nsIDOMNode *node)
{ {
NS_PRECONDITION(node, "null node passed to nsHTMLEditUtils::IsMozBR"); NS_PRECONDITION(node, "null node passed to nsHTMLEditUtils::IsMozBR");
if (IsBreak(node) && HasMozAttr(node)) return true; return IsBreak(node) && HasMozAttr(node);
return false;
} }
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 // HasMozAttr: true if node has type attribute = _moz
// (used to indicate the div's and br's we use in // (used to indicate the div's and br's we use in

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

@ -38,11 +38,15 @@
#ifndef nsTextEditUtils_h__ #ifndef nsTextEditUtils_h__
#define nsTextEditUtils_h__ #define nsTextEditUtils_h__
#include "prtypes.h" // for bool
#include "nsError.h" // for nsresult #include "nsError.h" // for nsresult
#include "nsString.h" // for nsAString
namespace mozilla {
namespace dom {
class Element;
} // namespace dom
} // namespace mozilla
class nsIDOMNode; class nsIDOMNode;
class nsIEditor;
class nsPlaintextEditor; class nsPlaintextEditor;
class nsTextEditUtils class nsTextEditUtils
@ -52,12 +56,13 @@ public:
static bool IsBody(nsIDOMNode* aNode); static bool IsBody(nsIDOMNode* aNode);
static bool IsBreak(nsIDOMNode* aNode); static bool IsBreak(nsIDOMNode* aNode);
static bool IsMozBR(nsIDOMNode* aNode); static bool IsMozBR(nsIDOMNode* aNode);
static bool IsMozBR(mozilla::dom::Element* aNode);
static bool HasMozAttr(nsIDOMNode* aNode); static bool HasMozAttr(nsIDOMNode* aNode);
}; };
/*************************************************************************** /***************************************************************************
* stack based helper class for detecting end of editor initialization, in * 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 class nsAutoEditInitRulesTrigger
{ {
@ -69,6 +74,4 @@ class nsAutoEditInitRulesTrigger
~nsAutoEditInitRulesTrigger(); ~nsAutoEditInitRulesTrigger();
}; };
#endif /* nsTextEditUtils_h__ */ #endif /* nsTextEditUtils_h__ */