Bug 1442499 - EditorBase::IsPreformatted should return bool. r?masayuki
It is unnecessary to get PresShell for nsStyleText, so we can return bool
instead of nsresult and can change it to static method.
MozReview-Commit-ID: LGrdcaJuLO
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -4014,50 +4014,46 @@ EditorBase::GetEndChildNode(Selection* a
NS_IF_ADDREF(*aEndNode = range->GetChildAtEndOffset());
return NS_OK;
}
/**
* IsPreformatted() checks the style info for the node for the preformatted
* text style.
*/
-nsresult
-EditorBase::IsPreformatted(nsIDOMNode* aNode,
- bool* aResult)
-{
- nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
-
- NS_ENSURE_TRUE(aResult && content, NS_ERROR_NULL_POINTER);
-
- nsCOMPtr<nsIPresShell> ps = GetPresShell();
- NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
-
+// static
+bool
+EditorBase::IsPreformatted(nsINode* aNode)
+{
+ if (NS_WARN_IF(!aNode)) {
+ return false;
+ }
// Look at the node (and its parent if it's not an element), and grab its
// ComputedStyle.
RefPtr<ComputedStyle> elementStyle;
- if (!content->IsElement()) {
- content = content->GetParent();
- }
- if (content && content->IsElement()) {
- elementStyle =
- nsComputedDOMStyle::GetComputedStyleNoFlush(content->AsElement(), nullptr);
- }
-
+ Element* element = aNode->IsElement() ? aNode->AsElement() : nullptr;
+ if (!element) {
+ element = aNode->GetParentElement();
+ if (!element) {
+ return false;
+ }
+ }
+
+ elementStyle =
+ nsComputedDOMStyle::GetComputedStyleNoFlush(element, nullptr);
if (!elementStyle) {
// Consider nodes without a ComputedStyle to be NOT preformatted:
// For instance, this is true of JS tags inside the body (which show
// up as #text nodes but have no ComputedStyle).
- *aResult = false;
- return NS_OK;
+ return false;
}
const nsStyleText* styleText = elementStyle->StyleText();
- *aResult = styleText->WhiteSpaceIsSignificant();
- return NS_OK;
+ return styleText->WhiteSpaceIsSignificant();
}
template<typename PT, typename CT>
SplitNodeResult
EditorBase::SplitNodeDeep(
nsIContent& aMostAncestorToSplit,
const EditorDOMPointBase<PT, CT>& aStartOfDeepestRightNode,
SplitAtEdges aSplitAtEdges)
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -1243,17 +1243,17 @@ public:
nsresult AppendNodeToSelectionAsRange(nsIDOMNode *aNode);
/**
* When you are using AppendNodeToSelectionAsRange(), call this first to
* start a new selection.
*/
nsresult ClearSelection();
- nsresult IsPreformatted(nsIDOMNode* aNode, bool* aResult);
+ static bool IsPreformatted(nsINode* aNode);
/**
* SplitNodeDeep() splits aMostAncestorToSplit deeply.
*
* @param aMostAncestorToSplit The most ancestor node which should be
* split.
* @param aStartOfDeepestRightNode The start point of deepest right node.
* This point must be descendant of
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -1369,21 +1369,17 @@ HTMLEditRules::WillInsertText(EditAction
// aAction == kInsertText
// find where we are
EditorDOMPoint currentPoint(pointToInsert);
// is our text going to be PREformatted?
// We remember this so that we know how to handle tabs.
- bool isPRE;
- NS_ENSURE_STATE(mHTMLEditor);
- rv = mHTMLEditor->IsPreformatted(GetAsDOMNode(pointToInsert.GetContainer()),
- &isPRE);
- NS_ENSURE_SUCCESS(rv, rv);
+ bool isPRE = EditorBase::IsPreformatted(pointToInsert.GetContainer());
// turn off the edit listener: we know how to
// build the "doc changed range" ourselves, and it's
// must faster to do it once here than to track all
// the changes one at a time.
AutoLockListener lockit(&mListenerEnabled);
// don't change my selection in subtransactions
@@ -5991,30 +5987,27 @@ HTMLEditRules::GetPromotedPoint(RulesEnd
if (NS_WARN_IF(!point.AdvanceOffset())) {
break;
}
if (htmlEditor->IsVisibleBRElement(nextNode)) {
break;
}
// Check for newlines in pre-formatted text nodes.
- bool isPRE;
- htmlEditor->IsPreformatted(nextNode->AsDOMNode(), &isPRE);
- if (isPRE) {
- if (EditorBase::IsTextNode(nextNode)) {
- nsAutoString tempString;
- nextNode->GetAsText()->GetData(tempString);
- int32_t newlinePos = tempString.FindChar(nsCRT::LF);
- if (newlinePos >= 0) {
- if (static_cast<uint32_t>(newlinePos) + 1 == tempString.Length()) {
- // No need for special processing if the newline is at the end.
- break;
- }
- return EditorDOMPoint(nextNode, newlinePos + 1);
+ if (EditorBase::IsPreformatted(nextNode) &&
+ EditorBase::IsTextNode(nextNode)) {
+ nsAutoString tempString;
+ nextNode->GetAsText()->GetData(tempString);
+ int32_t newlinePos = tempString.FindChar(nsCRT::LF);
+ if (newlinePos >= 0) {
+ if (static_cast<uint32_t>(newlinePos) + 1 == tempString.Length()) {
+ // No need for special processing if the newline is at the end.
+ break;
}
+ return EditorDOMPoint(nextNode, newlinePos + 1);
}
}
nextNode = htmlEditor->GetNextEditableHTMLNodeInBlock(point);
}
// finding the real end for this point. look up the tree for as long as we
// are the last node in the container, and as long as we haven't hit the body
// node.
--- a/editor/libeditor/WSRunObject.cpp
+++ b/editor/libeditor/WSRunObject.cpp
@@ -903,17 +903,17 @@ WSRunObject::GetWSNodes()
}
void
WSRunObject::GetRuns()
{
ClearRuns();
// handle some easy cases first
- mHTMLEditor->IsPreformatted(GetAsDOMNode(mNode), &mPRE);
+ mPRE = EditorBase::IsPreformatted(mNode);
// if it's preformatedd, or if we are surrounded by text or special, it's all one
// big normal ws run
if (mPRE ||
((mStartReason == WSType::text || mStartReason == WSType::special) &&
(mEndReason == WSType::text || mEndReason == WSType::special ||
mEndReason == WSType::br))) {
MakeSingleWSRun(WSType::normalWS);
return;