Bug 1647556 - part 6: Make `WSRunObject::AdjustWhiteSpace()` use `TextFragmentData::CreateWSFragmentForVisibleAndMiddleOfLine()` instead of scanning all `WSFragment`s r=m_kato

It scans all `WSFragment`s in the array and call `NormalizeWhiteSpacesAtEndOf()`
only with visible instance.  However, as we know, there is at most one such
instance.  Therefore, it does not need to scan the fragments (i.e., don't need
to create all fragments).

Differential Revision: https://phabricator.services.mozilla.com/D82279
This commit is contained in:
Masayuki Nakano 2020-07-08 10:11:35 +00:00
Родитель 61003960ec
Коммит b546936467
3 изменённых файлов: 49 добавлений и 42 удалений

Просмотреть файл

@ -544,47 +544,41 @@ nsresult HTMLEditor::OnEndHandlingTopLevelEditSubActionInternal() {
return NS_ERROR_FAILURE;
}
}
rv = WSRunObject(*this, pointToAdjust).AdjustWhiteSpace();
if (NS_WARN_IF(Destroyed())) {
return NS_ERROR_EDITOR_DESTROYED;
}
rv = WSRunObject::NormalizeWhiteSpacesAround(*this, pointToAdjust);
if (NS_FAILED(rv)) {
NS_WARNING("WSRunObject::AdjustWhiteSpace() failed");
NS_WARNING("WSRunObject::NormalizeWhiteSpacesAround() failed");
return rv;
}
// also do this for original selection endpoints.
// XXX Hmm, if `AdjustWhiteSpace()` runs mutation event listener
// and that causes changing `mSelectedRange`, what we should do?
// XXX Hmm, if `NormalizeWhiteSpacesAround()` runs mutation event
// listener and that causes changing `mSelectedRange`, what we
// should do?
if (NS_WARN_IF(
!TopLevelEditSubActionDataRef().mSelectedRange->IsSet())) {
return NS_ERROR_FAILURE;
}
DebugOnly<nsresult> rvIgnored =
WSRunObject(
rv = WSRunObject::NormalizeWhiteSpacesAround(
*this,
TopLevelEditSubActionDataRef().mSelectedRange->StartRawPoint())
.AdjustWhiteSpace();
if (NS_WARN_IF(Destroyed())) {
TopLevelEditSubActionDataRef().mSelectedRange->StartRawPoint());
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"WSRunObject::AdjustWhiteSpace() failed, but ignored");
NS_SUCCEEDED(rv),
"WSRunObject::NormalizeWhiteSpacesAround() failed, but ignored");
// we only need to handle old selection endpoint if it was different
// from start
if (TopLevelEditSubActionDataRef().mSelectedRange->IsCollapsed()) {
DebugOnly<nsresult> rvIgnored =
WSRunObject(
nsresult rv = WSRunObject::NormalizeWhiteSpacesAround(
*this,
TopLevelEditSubActionDataRef().mSelectedRange->EndRawPoint())
.AdjustWhiteSpace();
if (NS_WARN_IF(Destroyed())) {
TopLevelEditSubActionDataRef().mSelectedRange->EndRawPoint());
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"WSRunObject::AdjustWhiteSpace() failed, but ignored");
NS_SUCCEEDED(rv),
"WSRunObject::NormalizeWhiteSpacesAround() failed, but ignored");
}
break;
}

Просмотреть файл

@ -65,6 +65,12 @@ template WSScanResult WSRunScanner::ScanNextVisibleNodeOrBlockBoundaryFrom(
const EditorDOMPoint& aPoint) const;
template WSScanResult WSRunScanner::ScanNextVisibleNodeOrBlockBoundaryFrom(
const EditorRawDOMPoint& aPoint) const;
template nsresult WSRunObject::NormalizeWhiteSpacesAround(
HTMLEditor& aHTMLEditor, const EditorDOMPoint& aScanStartPoint);
template nsresult WSRunObject::NormalizeWhiteSpacesAround(
HTMLEditor& aHTMLEditor, const EditorRawDOMPoint& aScanStartPoint);
template nsresult WSRunObject::NormalizeWhiteSpacesAround(
HTMLEditor& aHTMLEditor, const EditorDOMPointInText& aScanStartPoint);
template <typename PT, typename CT>
WSRunScanner::WSRunScanner(const HTMLEditor* aHTMLEditor,
@ -673,27 +679,33 @@ WSScanResult WSRunScanner::ScanNextVisibleNodeOrBlockBoundaryFrom(
return WSScanResult(mEnd.PointRef(), mEnd.RawReason());
}
nsresult WSRunObject::AdjustWhiteSpace() {
// static
template <typename EditorDOMPointType>
nsresult WSRunObject::NormalizeWhiteSpacesAround(
HTMLEditor& aHTMLEditor, const EditorDOMPointType& aScanStartPoint) {
WSRunObject wsRunObject(aHTMLEditor, aScanStartPoint);
// this routine examines a run of ws and tries to get rid of some unneeded
// nbsp's, replacing them with regular ascii space if possible. Keeping
// things simple for now and just trying to fix up the trailing ws in the run.
if (!mNBSPData.FoundNBSP()) {
if (!wsRunObject.mNBSPData.FoundNBSP()) {
// nothing to do!
return NS_OK;
}
for (const WSFragment& fragment : WSFragmentArrayRef()) {
if (!fragment.IsVisibleAndMiddleOfHardLine()) {
continue;
}
nsresult rv = NormalizeWhiteSpacesAtEndOf(fragment);
if (NS_FAILED(rv)) {
NS_WARNING("WSRunObject::NormalizeWhiteSpacesAtEndOf() failed");
return rv;
}
}
Maybe<WSFragment> visibleWSFragmentInMiddleOfLine =
TextFragmentData(wsRunObject.mStart, wsRunObject.mEnd,
wsRunObject.mNBSPData, wsRunObject.mPRE)
.CreateWSFragmentForVisibleAndMiddleOfLine();
if (visibleWSFragmentInMiddleOfLine.isNothing()) {
return NS_OK;
}
nsresult rv = wsRunObject.NormalizeWhiteSpacesAtEndOf(
visibleWSFragmentInMiddleOfLine.ref());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"WSRunObject::NormalizeWhiteSpacesAtEndOf() failed");
return rv;
}
//--------------------------------------------------------------------------------------------
// protected methods
//--------------------------------------------------------------------------------------------
@ -1803,10 +1815,7 @@ char16_t WSRunScanner::GetCharAt(Text* aTextNode, int32_t aOffset) const {
}
nsresult WSRunObject::NormalizeWhiteSpacesAtEndOf(const WSFragment& aRun) {
// Check if it's a visible fragment in a hard line.
if (!aRun.IsVisibleAndMiddleOfHardLine()) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(aRun.IsVisibleAndMiddleOfHardLine());
// Remove this block if we ship Blink-compat white-space normalization.
if (!StaticPrefs::editor_white_space_normalization_blink_compatible()) {

Просмотреть файл

@ -949,9 +949,13 @@ class MOZ_STACK_CLASS WSRunObject final : public WSRunScanner {
// makes any needed conversion to adjacent ws to retain its significance.
MOZ_CAN_RUN_SCRIPT nsresult DeleteWSForward();
// AdjustWhiteSpace examines the ws object for nbsp's that can
// be safely converted to regular ascii space and converts them.
MOZ_CAN_RUN_SCRIPT nsresult AdjustWhiteSpace();
/**
* NormalizeWhiteSpacesAround() tries to normalize white-space sequence
* around aScanStartPoint.
*/
template <typename EditorDOMPointType>
[[nodiscard]] MOZ_CAN_RUN_SCRIPT static nsresult NormalizeWhiteSpacesAround(
HTMLEditor& aHTMLEditor, const EditorDOMPointType& aSacnStartPoint);
protected:
MOZ_CAN_RUN_SCRIPT nsresult PrepareToDeleteRangePriv(WSRunObject* aEndObject);