Bug 1727844 - part 7: Make HTMLEditor related classes use constants in HTMLEditUtils to use specific character code point r=m_kato

Although we should move it into `EditorUtils`, currently, this is enough.

Differential Revision: https://phabricator.services.mozilla.com/D123874
This commit is contained in:
Masayuki Nakano 2021-08-31 17:37:42 +00:00
parent 62f8f1ee2c
commit ed2f2242d1
8 changed files with 67 additions and 66 deletions

View File

@ -5,6 +5,7 @@
#include "mozilla/CSSEditUtils.h"
#include "HTMLEditUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ChangeStyleTransaction.h"
#include "mozilla/HTMLEditor.h"
@ -971,7 +972,7 @@ nsresult CSSEditUtils::GetCSSEquivalentToHTMLInlineStyleSetInternal(
}
// append the value to aValue (possibly with a leading white-space)
if (index) {
aValue.Append(char16_t(' '));
aValue.Append(HTMLEditUtils::kSpace);
}
aValue.Append(valueString);
}
@ -1102,7 +1103,7 @@ bool CSSEditUtils::IsCSSEquivalentToHTMLInlineStyleSetInternal(
} else if (nsGkAtoms::font == aHTMLProperty && aAttribute &&
aAttribute == nsGkAtoms::face) {
if (!htmlValueString.IsEmpty()) {
const char16_t commaSpace[] = {char16_t(','), char16_t(' '), 0};
const char16_t commaSpace[] = {char16_t(','), HTMLEditUtils::kSpace, 0};
const char16_t comma[] = {char16_t(','), 0};
htmlValueString.ReplaceSubstring(commaSpace, comma);
nsAutoString valueStringNorm(aValue);

View File

@ -5,6 +5,7 @@
#include "ChangeStyleTransaction.h"
#include "HTMLEditUtils.h"
#include "mozilla/Logging.h"
#include "mozilla/ToString.h"
#include "mozilla/dom/Element.h" // for Element
@ -154,7 +155,7 @@ void ChangeStyleTransaction::RemoveValueFromListOfValues(
if (start < end && !aRemoveValue.Equals(start)) {
outString.Append(start);
outString.Append(' ');
outString.Append(HTMLEditUtils::kSpace);
}
start = ++end;
@ -336,7 +337,7 @@ void ChangeStyleTransaction::AddValueToMultivalueProperty(
aValues.Assign(aNewValue);
} else if (!ValueIncludes(aValues, aNewValue)) {
// We already have another value but not this one; add it
aValues.Append(char16_t(' '));
aValues.Append(HTMLEditUtils::kSpace);
aValues.Append(aNewValue);
}
}

View File

@ -35,8 +35,11 @@ class HTMLEditUtils final {
using Selection = dom::Selection;
public:
static const char16_t kSpace = 0x0020;
static const char16_t kNBSP = 0x00A0;
static constexpr char16_t kNewLine = '\n';
static constexpr char16_t kCarridgeReturn = '\r';
static constexpr char16_t kSpace = ' ';
static constexpr char16_t kNBSP = 0x00A0;
static constexpr char16_t kGreaterThan = '>';
/**
* IsSimplyEditableNode() returns true when aNode is simply editable.
@ -1460,7 +1463,7 @@ class HTMLEditUtils final {
MOZ_ASSERT(aOffset <= textFragment.GetLength());
for (uint32_t i = aOffset; i; i--) {
char16_t ch = textFragment.CharAt(i - 1);
if (!nsCRT::IsAsciiSpace(ch) && ch != kNBSP) {
if (!nsCRT::IsAsciiSpace(ch) && ch != HTMLEditUtils::kNBSP) {
return Some(i - 1);
}
}
@ -1484,7 +1487,7 @@ class HTMLEditUtils final {
MOZ_ASSERT(aOffset <= textFragment.GetLength());
for (uint32_t i = aOffset; i < textFragment.GetLength(); i++) {
char16_t ch = textFragment.CharAt(i);
if (!nsCRT::IsAsciiSpace(ch) && ch != kNBSP) {
if (!nsCRT::IsAsciiSpace(ch) && ch != HTMLEditUtils::kNBSP) {
return Some(i);
}
}

View File

@ -75,8 +75,6 @@ using LeafNodeType = HTMLEditUtils::LeafNodeType;
using LeafNodeTypes = HTMLEditUtils::LeafNodeTypes;
using WalkTreeOption = HTMLEditUtils::WalkTreeOption;
const char16_t kNBSP = 160;
// Some utilities to handle overloading of "A" tag for link and named anchor.
static bool IsLinkTag(const nsAtom& aTagName) {
return &aTagName == nsGkAtoms::href;
@ -5138,7 +5136,7 @@ nsresult HTMLEditor::SetAttributeOrEquivalent(Element* aElement,
// style attribute's value
nsAutoString existingValue;
aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::style, existingValue);
existingValue.Append(' ');
existingValue.Append(HTMLEditUtils::kSpace);
existingValue.Append(aValue);
if (aSuppressTransaction) {
nsresult rv = aElement->SetAttr(kNameSpaceID_None, nsGkAtoms::style,

View File

@ -2584,8 +2584,9 @@ nsresult HTMLEditor::InsertWithQuotationsAsSubAction(
// It's best to put a blank line after the quoted text so that mails
// written without thinking won't be so ugly.
if (!aQuotedText.IsEmpty() && (aQuotedText.Last() != char16_t('\n'))) {
quotedStuff.Append(char16_t('\n'));
if (!aQuotedText.IsEmpty() &&
(aQuotedText.Last() != HTMLEditUtils::kNewLine)) {
quotedStuff.Append(HTMLEditUtils::kNewLine);
}
IgnoredErrorResult ignoredError;
@ -2679,7 +2680,7 @@ nsresult HTMLEditor::InsertTextWithQuotationsInternal(
// there aren't any there:
#ifdef DEBUG
nsAString::const_iterator dbgStart(hunkStart);
if (FindCharInReadable('\r', dbgStart, strEnd)) {
if (FindCharInReadable(HTMLEditUtils::kCarridgeReturn, dbgStart, strEnd)) {
NS_ASSERTION(
false,
"Return characters in DOM! InsertTextWithQuotations may be wrong");
@ -2692,13 +2693,13 @@ nsresult HTMLEditor::InsertTextWithQuotationsInternal(
// We will break from inside when we run out of newlines.
for (;;) {
// Search for the end of this line (dom newlines, see above):
bool found = FindCharInReadable('\n', lineStart, strEnd);
bool found = FindCharInReadable(HTMLEditUtils::kNewLine, lineStart, strEnd);
bool quoted = false;
if (found) {
// if there's another newline, lineStart now points there.
// Loop over any consecutive newline chars:
nsAString::const_iterator firstNewline(lineStart);
while (*lineStart == '\n') {
while (*lineStart == HTMLEditUtils::kNewLine) {
++lineStart;
}
quoted = (*lineStart == cite);
@ -3426,8 +3427,7 @@ nsresult HTMLEditor::HTMLWithContextInserter::FragmentFromPasteCreator::
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::HTMLWithContextInserter::FragmentFromPasteCreator::"
"RemoveNonPreWhiteSpaceOnlyTextNodesForIgnoringInvisibleWhiteSpaces()"
" "
"RemoveNonPreWhiteSpaceOnlyTextNodesForIgnoringInvisibleWhiteSpaces() "
"failed");
return rv;
}
@ -3447,8 +3447,8 @@ nsresult HTMLEditor::HTMLWithContextInserter::FragmentFromPasteCreator::
if (NS_FAILED(rv)) {
NS_WARNING(
"HTMLEditor::HTMLWithContextInserter::FragmentFromPasteCreator::"
"RemoveNonPreWhiteSpaceOnlyTextNodesForIgnoringInvisibleWhiteSpaces()"
" failed");
"RemoveNonPreWhiteSpaceOnlyTextNodesForIgnoringInvisibleWhiteSpaces() "
"failed");
return rv;
}

View File

@ -1291,8 +1291,7 @@ nsresult HTMLEditor::SetFinalSizeWithTransaction(int32_t aX, int32_t aY) {
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"CSSEditUtils::SetCSSPropertyPixelsWithTransaction(nsGkAtoms::left)"
" "
"CSSEditUtils::SetCSSPropertyPixelsWithTransaction(nsGkAtoms::left) "
"failed, but ignored");
}
}

View File

@ -5,6 +5,7 @@
#include "InternetCiter.h"
#include "HTMLEditUtils.h"
#include "nsAString.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
@ -18,11 +19,6 @@
namespace mozilla {
const char16_t gt('>');
const char16_t space(' ');
const char16_t nl('\n');
const char16_t cr('\r');
/**
* Mail citations using the Internet style: > This is a citation.
*/
@ -30,25 +26,26 @@ const char16_t cr('\r');
nsresult InternetCiter::GetCiteString(const nsAString& aInString,
nsAString& aOutString) {
aOutString.Truncate();
char16_t uch = nl;
char16_t uch = HTMLEditUtils::kNewLine;
// Strip trailing new lines which will otherwise turn up
// as ugly quoted empty lines.
nsReadingIterator<char16_t> beginIter, endIter;
aInString.BeginReading(beginIter);
aInString.EndReading(endIter);
while (beginIter != endIter && (*endIter == cr || *endIter == nl)) {
while (beginIter != endIter && (*endIter == HTMLEditUtils::kCarridgeReturn ||
*endIter == HTMLEditUtils::kNewLine)) {
--endIter;
}
// Loop over the string:
while (beginIter != endIter) {
if (uch == nl) {
aOutString.Append(gt);
if (uch == HTMLEditUtils::kNewLine) {
aOutString.Append(HTMLEditUtils::kGreaterThan);
// No space between >: this is ">>> " style quoting, for
// compatibility with RFC 2646 and format=flowed.
if (*beginIter != gt) {
aOutString.Append(space);
if (*beginIter != HTMLEditUtils::kGreaterThan) {
aOutString.Append(HTMLEditUtils::kSpace);
}
}
@ -58,24 +55,24 @@ nsresult InternetCiter::GetCiteString(const nsAString& aInString,
aOutString += uch;
}
if (uch != nl) {
aOutString += nl;
if (uch != HTMLEditUtils::kNewLine) {
aOutString += HTMLEditUtils::kNewLine;
}
return NS_OK;
}
static void AddCite(nsAString& aOutString, int32_t citeLevel) {
for (int32_t i = 0; i < citeLevel; ++i) {
aOutString.Append(gt);
aOutString.Append(HTMLEditUtils::kGreaterThan);
}
if (citeLevel > 0) {
aOutString.Append(space);
aOutString.Append(HTMLEditUtils::kSpace);
}
}
static inline void BreakLine(nsAString& aOutString, uint32_t& outStringCol,
uint32_t citeLevel) {
aOutString.Append(nl);
aOutString.Append(HTMLEditUtils::kNewLine);
if (citeLevel > 0) {
AddCite(aOutString, citeLevel);
outStringCol = citeLevel + 1;
@ -85,8 +82,8 @@ static inline void BreakLine(nsAString& aOutString, uint32_t& outStringCol,
}
static inline bool IsSpace(char16_t c) {
const char16_t nbsp(0xa0);
return (nsCRT::IsAsciiSpace(c) || (c == nl) || (c == cr) || (c == nbsp));
return (nsCRT::IsAsciiSpace(c) || (c == HTMLEditUtils::kNewLine) ||
(c == HTMLEditUtils::kCarridgeReturn) || (c == HTMLEditUtils::kNBSP));
}
nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
@ -95,8 +92,8 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
// There shouldn't be returns in this string, only dom newlines.
// Check to make sure:
#ifdef DEBUG
int32_t cr = aInString.FindChar(char16_t('\r'));
NS_ASSERTION((cr < 0), "Rewrap: CR in string gotten from DOM!\n");
int32_t crPosition = aInString.FindChar(HTMLEditUtils::kCarridgeReturn);
NS_ASSERTION(crPosition < 0, "Rewrap: CR in string gotten from DOM!\n");
#endif /* DEBUG */
aOutString.Truncate();
@ -117,10 +114,12 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
while (posInString < length) {
// Get the new cite level here since we're at the beginning of a line
uint32_t newCiteLevel = 0;
while (posInString < length && tString[posInString] == gt) {
while (posInString < length &&
tString[posInString] == HTMLEditUtils::kGreaterThan) {
++newCiteLevel;
++posInString;
while (posInString < length && tString[posInString] == space) {
while (posInString < length &&
tString[posInString] == HTMLEditUtils::kSpace) {
++posInString;
}
}
@ -130,12 +129,13 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
// Special case: if this is a blank line, maintain a blank line
// (retain the original paragraph breaks)
if (tString[posInString] == nl && !aOutString.IsEmpty()) {
if (aOutString.Last() != nl) {
aOutString.Append(nl);
if (tString[posInString] == HTMLEditUtils::kNewLine &&
!aOutString.IsEmpty()) {
if (aOutString.Last() != HTMLEditUtils::kNewLine) {
aOutString.Append(HTMLEditUtils::kNewLine);
}
AddCite(aOutString, newCiteLevel);
aOutString.Append(nl);
aOutString.Append(HTMLEditUtils::kNewLine);
++posInString;
outStringCol = 0;
@ -160,12 +160,13 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
// the output string, add a space to separate new text from the
// previous text.
else if (outStringCol > citeLevel) {
aOutString.Append(space);
aOutString.Append(HTMLEditUtils::kSpace);
++outStringCol;
}
// find the next newline -- don't want to go farther than that
int32_t nextNewline = tString.FindChar(nl, posInString);
int32_t nextNewline =
tString.FindChar(HTMLEditUtils::kNewLine, posInString);
if (nextNewline < 0) {
nextNewline = length;
}
@ -182,7 +183,7 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
Substring(tString, posInString, nextNewline - posInString));
outStringCol += nextNewline - posInString;
if (nextNewline != (int32_t)length) {
aOutString.Append(nl);
aOutString.Append(HTMLEditUtils::kNewLine);
outStringCol = 0;
}
posInString = nextNewline + 1;
@ -204,7 +205,7 @@ nsresult InternetCiter::Rewrap(const nsAString& aInString, uint32_t aWrapCol,
// If this short line is the final one in the in string,
// then we need to include the final newline, if any:
if (nextNewline + 1 == (int32_t)length &&
tString[nextNewline - 1] == nl) {
tString[nextNewline - 1] == HTMLEditUtils::kNewLine) {
++nextNewline;
}
// Trim trailing spaces:

View File

@ -40,8 +40,6 @@ using LeafNodeType = HTMLEditUtils::LeafNodeType;
using LeafNodeTypes = HTMLEditUtils::LeafNodeTypes;
using WalkTreeOption = HTMLEditUtils::WalkTreeOption;
const char16_t kNBSP = 160;
template WSScanResult WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundaryFrom(
const EditorDOMPoint& aPoint) const;
template WSScanResult WSRunScanner::ScanPreviousVisibleNodeOrBlockBoundaryFrom(
@ -749,7 +747,7 @@ Result<RefPtr<Element>, nsresult> WhiteSpaceVisibilityKeeper::InsertBRElement(
aHTMLEditor,
EditorDOMRangeInTexts(atNextCharOfInsertionPoint,
endOfCollapsibleASCIIWhiteSpaces),
nsDependentSubstring(&kNBSP, 1));
nsDependentSubstring(&HTMLEditUtils::kNBSP, 1));
if (NS_FAILED(rv)) {
NS_WARNING(
"WhiteSpaceVisibilityKeeper::"
@ -994,7 +992,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
// If inserting string will follow some invisible leading white-spaces, the
// string needs to start with an NBSP.
if (invisibleLeadingWhiteSpaceRangeAtStart.IsPositioned()) {
theString.SetCharAt(kNBSP, 0);
theString.SetCharAt(HTMLEditUtils::kNBSP, 0);
}
// If inserting around visible white-spaces, check whether the previous
// character of insertion point is an NBSP or an ASCII white-space.
@ -1006,7 +1004,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
textFragmentDataAtStart.GetPreviousEditableCharPoint(pointToInsert);
if (atPreviousChar.IsSet() && !atPreviousChar.IsEndOfContainer() &&
atPreviousChar.IsCharASCIISpace()) {
theString.SetCharAt(kNBSP, 0);
theString.SetCharAt(HTMLEditUtils::kNBSP, 0);
}
}
// If the insertion point is (was) before the start of text and it's
@ -1014,7 +1012,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
// be replaced with an NBSP for making it visible.
else if (textFragmentDataAtStart.StartsFromHardLineBreak() &&
isInsertionPointEqualsOrIsBeforeStartOfText) {
theString.SetCharAt(kNBSP, 0);
theString.SetCharAt(HTMLEditUtils::kNBSP, 0);
}
}
@ -1025,7 +1023,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
// If inserting string will be followed by some invisible trailing
// white-spaces, the string needs to end with an NBSP.
if (invisibleTrailingWhiteSpaceRangeAtEnd.IsPositioned()) {
theString.SetCharAt(kNBSP, lastCharIndex);
theString.SetCharAt(HTMLEditUtils::kNBSP, lastCharIndex);
}
// If inserting around visible white-spaces, check whether the inclusive
// next character of end of replaced range is an NBSP or an ASCII
@ -1039,7 +1037,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
pointToInsert);
if (atNextChar.IsSet() && !atNextChar.IsEndOfContainer() &&
atNextChar.IsCharASCIISpace()) {
theString.SetCharAt(kNBSP, lastCharIndex);
theString.SetCharAt(HTMLEditUtils::kNBSP, lastCharIndex);
}
}
// If the end of replacing range is (was) after the end of text and it's
@ -1047,7 +1045,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
// be replaced with an NBSP for making it visible.
else if (textFragmentDataAtEnd.EndsByBlockBoundary() &&
isInsertionPointEqualsOrAfterEndOfText) {
theString.SetCharAt(kNBSP, lastCharIndex);
theString.SetCharAt(HTMLEditUtils::kNBSP, lastCharIndex);
}
}
@ -1060,7 +1058,7 @@ nsresult WhiteSpaceVisibilityKeeper::ReplaceText(
if (nsCRT::IsAsciiSpace(theString[i])) {
if (prevWS) {
// i - 1 can't be negative because prevWS starts out false
theString.SetCharAt(kNBSP, i - 1);
theString.SetCharAt(HTMLEditUtils::kNBSP, i - 1);
} else {
prevWS = true;
}
@ -2195,7 +2193,7 @@ WSRunScanner::TextFragmentData::GetReplaceRangeDataAtEndOfDeletionRange(
nextCharOfStartOfEnd);
return ReplaceRangeData(nextCharOfStartOfEnd,
endOfCollapsibleASCIIWhiteSpaces,
nsDependentSubstring(&kNBSP, 1));
nsDependentSubstring(&HTMLEditUtils::kNBSP, 1));
}
ReplaceRangeData
@ -2272,7 +2270,7 @@ WSRunScanner::TextFragmentData::GetReplaceRangeDataAtStartOfDeletionRange(
GetEndOfCollapsibleASCIIWhiteSpaces(atPreviousCharOfStart);
return ReplaceRangeData(atPreviousCharOfStart,
endOfCollapsibleASCIIWhiteSpaces,
nsDependentSubstring(&kNBSP, 1));
nsDependentSubstring(&HTMLEditUtils::kNBSP, 1));
}
// static
@ -2329,7 +2327,7 @@ WhiteSpaceVisibilityKeeper::MakeSureToKeepVisibleWhiteSpacesVisibleAfterSplit(
aHTMLEditor,
EditorDOMRangeInTexts(atNextCharOfStart,
endOfCollapsibleASCIIWhiteSpaces),
nsDependentSubstring(&kNBSP, 1));
nsDependentSubstring(&HTMLEditUtils::kNBSP, 1));
if (NS_FAILED(rv)) {
NS_WARNING(
"WhiteSpaceVisibilityKeeper::ReplaceTextAndRemoveEmptyTextNodes() "
@ -2363,7 +2361,7 @@ WhiteSpaceVisibilityKeeper::MakeSureToKeepVisibleWhiteSpacesVisibleAfterSplit(
aHTMLEditor,
EditorDOMRangeInTexts(atPreviousCharOfStart,
endOfCollapsibleASCIIWhiteSpaces),
nsDependentSubstring(&kNBSP, 1));
nsDependentSubstring(&HTMLEditUtils::kNBSP, 1));
if (NS_FAILED(rv)) {
NS_WARNING(
"WhiteSpaceVisibilityKeeper::ReplaceTextAndRemoveEmptyTextNodes() "