Bug 1627175 - part 19: Move EditorBase::IsPreformatted() to EditorUtils r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D75889
This commit is contained in:
Masayuki Nakano 2020-05-20 04:45:42 +00:00
parent a97bc5aea1
commit 1a48bee3ed
6 changed files with 41 additions and 41 deletions

View File

@ -30,7 +30,6 @@
#include "mozilla/BasePrincipal.h" // for BasePrincipal
#include "mozilla/CheckedInt.h" // for CheckedInt
#include "mozilla/ComposerCommandsUpdater.h" // for ComposerCommandsUpdater
#include "mozilla/ComputedStyle.h" // for ComputedStyle
#include "mozilla/CSSEditUtils.h" // for CSSEditUtils
#include "mozilla/EditAction.h" // for EditSubAction
#include "mozilla/EditorDOMPoint.h" // for EditorDOMPoint
@ -79,7 +78,6 @@
#include "nsCaseTreatment.h"
#include "nsCharTraits.h" // for NS_IS_HIGH_SURROGATE, etc.
#include "nsComponentManagerUtils.h" // for do_CreateInstance
#include "nsComputedDOMStyle.h" // for nsComputedDOMStyle
#include "nsContentUtils.h" // for nsContentUtils
#include "nsDOMString.h" // for DOMStringIsNull
#include "nsDebug.h" // for NS_WARNING, etc.
@ -3174,36 +3172,6 @@ nsresult EditorBase::GetEndChildNode(const Selection& aSelection,
return NS_OK;
}
/**
* IsPreformatted() checks the style info for the node for the preformatted
* text style.
*/
// 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.
Element* element = aNode->GetAsElementOrParentElement();
if (!element) {
return false;
}
RefPtr<ComputedStyle> 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).
return false;
}
const nsStyleText* styleText = elementStyle->StyleText();
return styleText->WhiteSpaceIsSignificant();
}
nsresult EditorBase::EnsureNoPaddingBRElementForEmptyEditor() {
MOZ_ASSERT(IsEditActionDataAvailable());

View File

@ -1744,8 +1744,6 @@ class EditorBase : public nsIEditor,
*/
nsresult CollapseSelectionToEnd();
static bool IsPreformatted(nsINode* aNode);
/**
* AllowsTransactionsToChangeSelection() returns true if editor allows any
* transactions to change Selection. Otherwise, transactions shouldn't

View File

@ -3,22 +3,25 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/EditorUtils.h"
#include "EditorUtils.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/ContentIterator.h"
#include "mozilla/EditorDOMPoint.h"
#include "mozilla/OwningNonNull.h"
#include "mozilla/TextEditor.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/HTMLBRElement.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/dom/Text.h"
#include "nsContentUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsComputedDOMStyle.h"
#include "nsError.h"
#include "nsIContent.h"
#include "mozilla/dom/Document.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsINode.h"
#include "nsStyleStruct.h"
class nsISupports;
class nsRange;
@ -230,4 +233,25 @@ void EditorUtils::MaskString(nsString& aString, Text* aText,
}
}
// static
bool EditorUtils::IsContentPreformatted(nsIContent& aContent) {
// Look at the node (and its parent if it's not an element), and grab its
// ComputedStyle.
Element* element = aContent.GetAsElementOrParentElement();
if (!element) {
return false;
}
RefPtr<ComputedStyle> 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).
return false;
}
return elementStyle->StyleText()->WhiteSpaceIsSignificant();
}
} // namespace mozilla

View File

@ -855,6 +855,12 @@ class EditorUtils final {
!EditorUtils::IsPaddingBRElementForEmptyEditor(aContent);
}
/**
* IsContentPreformatted() checks the style info for the node for the
* preformatted text style. This does NOT flush layout.
*/
static bool IsContentPreformatted(nsIContent& aContent);
/**
* Helper method for `AppendString()` and `AppendSubString()`. This should
* be called only when `aText` is in a password field. This method masks

View File

@ -1455,7 +1455,8 @@ EditActionResult HTMLEditor::HandleInsertText(
}
EditorDOMPoint pointToInsert(firstRange->StartRef());
if (NS_WARN_IF(!pointToInsert.IsSet())) {
if (NS_WARN_IF(!pointToInsert.IsSet()) ||
NS_WARN_IF(!pointToInsert.IsInContentNode())) {
return EditActionHandled(NS_ERROR_FAILURE);
}
MOZ_ASSERT(pointToInsert.IsSetAndValid());
@ -1523,7 +1524,8 @@ EditActionResult HTMLEditor::HandleInsertText(
// is our text going to be PREformatted?
// We remember this so that we know how to handle tabs.
bool isPRE = EditorBase::IsPreformatted(pointToInsert.GetContainer());
bool isPRE =
EditorUtils::IsContentPreformatted(*pointToInsert.ContainerAsContent());
// turn off the edit listener: we know how to
// build the "doc changed range" ourselves, and it's
@ -8146,8 +8148,8 @@ EditorDOMPoint HTMLEditor::GetCurrentHardLineEndPoint(
}
// Check for newlines in pre-formatted text nodes.
if (EditorBase::IsPreformatted(nextEditableContent) &&
nextEditableContent->IsText()) {
if (nextEditableContent->IsText() &&
EditorUtils::IsContentPreformatted(*nextEditableContent)) {
nsAutoString textContent;
nextEditableContent->GetAsText()->GetData(textContent);
int32_t newlinePos = textContent.FindChar(nsCRT::LF);

View File

@ -980,7 +980,9 @@ void WSRunScanner::GetRuns() {
// the scan range isn't in preformatted element, we need to check only the
// style at mScanStartPoint since the range would be replaced and the start
// style will be applied to all new string.
mPRE = EditorBase::IsPreformatted(mScanStartPoint.GetContainer());
mPRE =
mScanStartPoint.IsInContentNode() &&
EditorUtils::IsContentPreformatted(*mScanStartPoint.ContainerAsContent());
// if it's preformatedd, or if we are surrounded by text or special, it's all
// one big normal ws run
if (mPRE ||