Bug 1010760 - Add nsINode::GetAsText; r=smaug

This commit is contained in:
Aryeh Gregor 2014-08-13 14:53:32 +03:00
parent 93bf3f1a27
commit 91963e0976
3 changed files with 26 additions and 10 deletions

View File

@ -74,6 +74,7 @@ class Element;
class EventHandlerNonNull;
class OnErrorEventHandlerNonNull;
template<typename T> class Optional;
class Text;
class TextOrElementOrDocument;
struct DOMPointInit;
} // namespace dom
@ -426,6 +427,13 @@ public:
return const_cast<nsINode*>(this)->AsContent();
}
/**
* Return this node as Text if it is one, otherwise null. This is defined
* inline in Text.h.
*/
mozilla::dom::Text* GetAsText();
const mozilla::dom::Text* GetAsText() const;
virtual nsIDOMNode* AsDOMNode() = 0;
/**

View File

@ -40,4 +40,16 @@ public:
} // namespace dom
} // namespace mozilla
inline mozilla::dom::Text* nsINode::GetAsText()
{
return IsNodeOfType(eTEXT) ? static_cast<mozilla::dom::Text*>(this)
: nullptr;
}
inline const mozilla::dom::Text* nsINode::GetAsText() const
{
return IsNodeOfType(eTEXT) ? static_cast<const mozilla::dom::Text*>(this)
: nullptr;
}
#endif // mozilla_dom_Text_h

View File

@ -628,8 +628,7 @@ nsWSRunObject::GetWSNodes()
nsCOMPtr<nsINode> wsBoundingParent = GetWSBoundingParent();
// first look backwards to find preceding ws nodes
if (mNode->IsNodeOfType(nsINode::eTEXT)) {
nsRefPtr<Text> textNode(static_cast<Text*>(mNode.get()));
if (nsRefPtr<Text> textNode = mNode->GetAsText()) {
const nsTextFragment* textFrag = textNode->GetText();
mNodeArray.InsertElementAt(0, textNode);
@ -675,8 +674,7 @@ nsWSRunObject::GetWSNodes()
mStartOffset = start.offset;
mStartReason = WSType::otherBlock;
mStartReasonNode = priorNode;
} else if (priorNode->IsNodeOfType(nsINode::eTEXT)) {
nsRefPtr<Text> textNode(static_cast<Text*>(priorNode.get()));
} else if (nsRefPtr<Text> textNode = priorNode->GetAsText()) {
mNodeArray.InsertElementAt(0, textNode);
const nsTextFragment *textFrag;
if (!textNode || !(textFrag = textNode->GetText())) {
@ -738,9 +736,8 @@ nsWSRunObject::GetWSNodes()
}
// then look ahead to find following ws nodes
if (mNode->IsNodeOfType(nsINode::eTEXT)) {
if (nsRefPtr<Text> textNode = mNode->GetAsText()) {
// don't need to put it on list. it already is from code above
nsRefPtr<Text> textNode(static_cast<Text*>(mNode.get()));
const nsTextFragment *textFrag = textNode->GetText();
uint32_t len = textNode->TextLength();
@ -786,8 +783,7 @@ nsWSRunObject::GetWSNodes()
mEndOffset = end.offset;
mEndReason = WSType::otherBlock;
mEndReasonNode = nextNode;
} else if (nextNode->IsNodeOfType(nsINode::eTEXT)) {
nsRefPtr<Text> textNode(static_cast<Text*>(nextNode.get()));
} else if (nsRefPtr<Text> textNode = nextNode->GetAsText()) {
mNodeArray.AppendElement(textNode);
const nsTextFragment *textFrag;
if (!textNode || !(textFrag = textNode->GetText())) {
@ -1342,8 +1338,8 @@ nsWSRunObject::DeleteChars(nsINode* aStartNode, int32_t aStartOffset,
idx = 0;
}
if (aStartNode == aEndNode && aStartNode->IsNodeOfType(nsINode::eTEXT)) {
return mHTMLEditor->DeleteText(static_cast<Text*>(aStartNode),
if (aStartNode == aEndNode && aStartNode->GetAsText()) {
return mHTMLEditor->DeleteText(aStartNode->GetAsText(),
(uint32_t)aStartOffset, (uint32_t)(aEndOffset - aStartOffset));
}