Bug 1646296 - part 2: Split WSRunScanner::GetWSNodes() r=m_kato

Despite the name, it stores range of white-space sequence and/or start/end
reason.  Initializing start and end are completely independent.  Therefore,
we can move them into independent methods.

Depends on D79964

Differential Revision: https://phabricator.services.mozilla.com/D79965
This commit is contained in:
Masayuki Nakano 2020-06-18 07:20:54 +00:00
parent e7f3831299
commit abe7626ff4
3 changed files with 51 additions and 23 deletions

View File

@ -262,8 +262,8 @@ class HTMLEditUtils final {
* aStartContent is not a descendant of this.
*/
static nsIContent* GetNextLeafContentOrNextBlockElement(
nsIContent& aStartContent, nsIContent& aCurrentBlock,
Element* aAncestorLimiter = nullptr) {
const nsIContent& aStartContent, const nsIContent& aCurrentBlock,
const Element* aAncestorLimiter = nullptr) {
if (&aStartContent == aAncestorLimiter) {
return nullptr;
}
@ -315,8 +315,9 @@ class HTMLEditUtils final {
*/
template <typename PT, typename CT>
static nsIContent* GetNextLeafContentOrNextBlockElement(
const EditorDOMPointBase<PT, CT>& aStartPoint, nsIContent& aCurrentBlock,
Element* aAncestorLimiter = nullptr) {
const EditorDOMPointBase<PT, CT>& aStartPoint,
const nsIContent& aCurrentBlock,
const Element* aAncestorLimiter = nullptr) {
MOZ_ASSERT(aStartPoint.IsSet());
if (!aStartPoint.IsInContentNode()) {
@ -374,8 +375,8 @@ class HTMLEditUtils final {
* aStartContent is not a descendant of this.
*/
static nsIContent* GetPreviousLeafContentOrPreviousBlockElement(
nsIContent& aStartContent, nsIContent& aCurrentBlock,
Element* aAncestorLimiter = nullptr) {
const nsIContent& aStartContent, const nsIContent& aCurrentBlock,
const Element* aAncestorLimiter = nullptr) {
if (&aStartContent == aAncestorLimiter) {
return nullptr;
}
@ -427,8 +428,9 @@ class HTMLEditUtils final {
*/
template <typename PT, typename CT>
static nsIContent* GetPreviousLeafContentOrPreviousBlockElement(
const EditorDOMPointBase<PT, CT>& aStartPoint, nsIContent& aCurrentBlock,
Element* aAncestorLimiter = nullptr) {
const EditorDOMPointBase<PT, CT>& aStartPoint,
const nsIContent& aCurrentBlock,
const Element* aAncestorLimiter = nullptr) {
MOZ_ASSERT(aStartPoint.IsSet());
if (!aStartPoint.IsInContentNode()) {

View File

@ -748,11 +748,24 @@ nsresult WSRunScanner::GetWSNodes() {
editableBlockParentOrTopmotEditableInlineContent = scanStartContent;
}
InitializeRangeStart(mScanStartPoint,
*editableBlockParentOrTopmotEditableInlineContent);
InitializeRangeEnd(mScanStartPoint,
*editableBlockParentOrTopmotEditableInlineContent);
return NS_OK;
}
void WSRunScanner::InitializeRangeStart(
const EditorDOMPoint& aPoint,
const nsIContent& aEditableBlockParentOrTopmostEditableInlineContent) {
MOZ_ASSERT(aPoint.IsSetAndValid());
EditorDOMPoint start(aPoint);
// first look backwards to find preceding ws nodes
if (Text* textNode = mScanStartPoint.GetContainerAsText()) {
if (Text* textNode = aPoint.GetContainerAsText()) {
const nsTextFragment* textFrag = &textNode->TextFragment();
if (!mScanStartPoint.IsStartOfContainer()) {
for (uint32_t i = mScanStartPoint.Offset(); i; i--) {
if (!aPoint.IsStartOfContainer()) {
for (uint32_t i = aPoint.Offset(); i; i--) {
// sanity bounds check the char position. bug 136165
if (i > textFrag->GetLength()) {
MOZ_ASSERT_UNREACHABLE("looking beyond end of text fragment");
@ -765,7 +778,7 @@ nsresult WSRunScanner::GetWSNodes() {
mStartOffset = i;
mStartReason = WSType::NormalText;
mStartReasonContent = textNode;
break;
return;
}
// as we look backwards update our earliest found nbsp
mFirstNBSPNode = textNode;
@ -785,7 +798,7 @@ nsresult WSRunScanner::GetWSNodes() {
// we haven't found the start of ws yet. Keep looking
nsIContent* previousLeafContentOrBlock =
HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
start, *editableBlockParentOrTopmotEditableInlineContent,
start, aEditableBlockParentOrTopmostEditableInlineContent,
mEditingHost);
if (previousLeafContentOrBlock) {
if (HTMLEditUtils::IsBlockElement(*previousLeafContentOrBlock)) {
@ -817,7 +830,7 @@ nsresult WSRunScanner::GetWSNodes() {
mStartOffset = pos + 1;
mStartReason = WSType::NormalText;
mStartReasonContent = textNode;
break;
return;
}
// as we look backwards update our earliest found nbsp
mFirstNBSPNode = textNode;
@ -845,16 +858,24 @@ nsresult WSRunScanner::GetWSNodes() {
}
} else {
// no prior node means we exhausted
// editableBlockParentOrTopmotEditableInlineContent
// aEditableBlockParentOrTopmostEditableInlineContent
mStartNode = start.GetContainer();
mStartOffset = start.Offset();
mStartReason = WSType::CurrentBlockBoundary;
// mStartReasonContent can be either a block element or any non-editable
// content in this case.
mStartReasonContent = editableBlockParentOrTopmotEditableInlineContent;
mStartReasonContent = const_cast<nsIContent*>(
&aEditableBlockParentOrTopmostEditableInlineContent);
}
}
}
void WSRunScanner::InitializeRangeEnd(
const EditorDOMPoint& aPoint,
const nsIContent& aEditableBlockParentOrTopmostEditableInlineContent) {
MOZ_ASSERT(aPoint.IsSetAndValid());
EditorDOMPoint end(aPoint);
// then look ahead to find following ws nodes
if (Text* textNode = end.GetContainerAsText()) {
// don't need to put it on list. it already is from code above
@ -873,7 +894,7 @@ nsresult WSRunScanner::GetWSNodes() {
mEndOffset = i;
mEndReason = WSType::NormalText;
mEndReasonContent = textNode;
break;
return;
}
// as we look forwards update our latest found nbsp
mLastNBSPNode = textNode;
@ -893,7 +914,7 @@ nsresult WSRunScanner::GetWSNodes() {
// we haven't found the end of ws yet. Keep looking
nsIContent* nextLeafContentOrBlock =
HTMLEditUtils::GetNextLeafContentOrNextBlockElement(
end, *editableBlockParentOrTopmotEditableInlineContent,
end, aEditableBlockParentOrTopmostEditableInlineContent,
mEditingHost);
if (nextLeafContentOrBlock) {
if (HTMLEditUtils::IsBlockElement(*nextLeafContentOrBlock)) {
@ -926,7 +947,7 @@ nsresult WSRunScanner::GetWSNodes() {
mEndOffset = pos;
mEndReason = WSType::NormalText;
mEndReasonContent = textNode;
break;
return;
}
// as we look forwards update our latest found nbsp
mLastNBSPNode = textNode;
@ -955,17 +976,16 @@ nsresult WSRunScanner::GetWSNodes() {
}
} else {
// no next node means we exhausted
// editableBlockParentOrTopmotEditableInlineContent
// aEditableBlockParentOrTopmostEditableInlineContent
mEndNode = end.GetContainer();
mEndOffset = end.Offset();
mEndReason = WSType::CurrentBlockBoundary;
// mEndReasonContent can be either a block element or any non-editable
// content in this case.
mEndReasonContent = editableBlockParentOrTopmotEditableInlineContent;
mEndReasonContent = const_cast<nsIContent*>(
&aEditableBlockParentOrTopmostEditableInlineContent);
}
}
return NS_OK;
}
void WSRunScanner::GetRuns() {

View File

@ -638,6 +638,12 @@ class MOZ_STACK_CLASS WSRunScanner {
WSFragment::Visible aIsVisible,
WSFragment::StartOfHardLine aIsStartOfHardLine,
WSFragment::EndOfHardLine aIsEndOfHardLine);
void InitializeRangeStart(
const EditorDOMPoint& aPoint,
const nsIContent& aEditableBlockParentOrTopmostEditableInlineContent);
void InitializeRangeEnd(
const EditorDOMPoint& aPoint,
const nsIContent& aEditableBlockParentOrTopmostEditableInlineContent);
// The node passed to our constructor.
EditorDOMPoint mScanStartPoint;