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

This commit is contained in:
Ms2ger 2012-01-11 09:23:07 +01:00
parent b36f3f9abc
commit 03f1927241
3 changed files with 57 additions and 58 deletions

View File

@ -1052,52 +1052,36 @@ nsTextEditRules::RemoveRedundantTrailingBR()
if (IsSingleLineEditor())
return NS_OK;
nsCOMPtr<nsIDOMNode> body = do_QueryInterface(mEditor->GetRoot());
nsRefPtr<dom::Element> 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<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);
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<nsIDOMNode> 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<nsIDOMElement> elem = do_QueryInterface(child);
if (elem) {
elem->RemoveAttribute(NS_LITERAL_STRING("type"));
NS_ENSURE_SUCCESS(res, res);
// set mBogusNode to be this <br>
mBogusNode = elem;
// give it the bogus node attribute
nsCOMPtr<nsIContent> content = do_QueryInterface(elem);
content->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false);
}
}
return NS_OK;
}
nsRefPtr<nsIContent> 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 <br>
mBogusNode = do_QueryInterface(elem);
// give it the bogus node attribute
elem->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false);
return NS_OK;
}

View File

@ -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

View File

@ -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__ */