зеркало из https://github.com/mozilla/gecko-dev.git
Bug 716207 - Part b: Cleanup nsTextEditRules::RemoveRedundantTrailingBR; r=ehsan
This commit is contained in:
Родитель
d7ac30b1d2
Коммит
c635e3e706
|
@ -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;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
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,37 +38,40 @@
|
||||||
#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
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// from nsTextEditRules:
|
// from nsTextEditRules:
|
||||||
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 HasMozAttr(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
|
* 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
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
nsPlaintextEditor *mEd;
|
nsPlaintextEditor* mEd;
|
||||||
nsresult &mRes;
|
nsresult& mRes;
|
||||||
public:
|
public:
|
||||||
nsAutoEditInitRulesTrigger( nsPlaintextEditor *aEd, nsresult &aRes);
|
nsAutoEditInitRulesTrigger(nsPlaintextEditor* aEd, nsresult& aRes);
|
||||||
~nsAutoEditInitRulesTrigger();
|
~nsAutoEditInitRulesTrigger();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif /* nsTextEditUtils_h__ */
|
#endif /* nsTextEditUtils_h__ */
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче