tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit cfacf6f690a59adbd8de17a29e89ad80141f1e85
parent 194f39d870caae856234f0a611c05a4a5c9792fc
Author: Edgar Chen <echen@mozilla.com>
Date:   Tue, 14 Oct 2025 07:44:10 +0000

Bug 1992067 - Simplify nsPlainTextSerializer::DoAddText() a bit; r=masayuki

Differential Revision: https://phabricator.services.mozilla.com/D267175

Diffstat:
Mdom/serializers/nsPlainTextSerializer.cpp | 90+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mdom/serializers/nsPlainTextSerializer.h | 13++++++-------
2 files changed, 53 insertions(+), 50 deletions(-)

diff --git a/dom/serializers/nsPlainTextSerializer.cpp b/dom/serializers/nsPlainTextSerializer.cpp @@ -230,11 +230,8 @@ uint32_t nsPlainTextSerializer::OutputManager::GetOutputLength() const { nsPlainTextSerializer::nsPlainTextSerializer() : mFloatingLines(-1), - mLineBreakDue(false), kSpace(u" "_ns) // Init of "constant" { - mHeadLevel = 0; - mHasWrittenCiteBlockquote = false; mSpanLevel = 0; for (int32_t i = 0; i <= 6; i++) { mHeaderCounter[i] = 0; @@ -255,8 +252,6 @@ nsPlainTextSerializer::nsPlainTextSerializer() mIgnoreAboveIndex = (uint32_t)kNotFound; mULCount = 0; - - mIgnoredChildNodeLevel = 0; } nsPlainTextSerializer::~nsPlainTextSerializer() { @@ -458,6 +453,21 @@ nsPlainTextSerializer::AppendText(Text* aText, int32_t aStartOffset, return NS_OK; } + // If we don't want any output, just return. + if (!DoOutput()) { + return NS_OK; + } + + if (mLineBreakDue) { + EnsureVerticalSpace(mFloatingLines); + } + + // Check whether this text node is under an element that doesn’t need to be + // serialized. If so, we can return early here. + if (MustSuppressLeaf()) { + return NS_OK; + } + nsAutoString textstr; if (characterDataBuffer->Is2b()) { textstr.Assign(characterDataBuffer->Get2b() + aStartOffset, length); @@ -479,11 +489,11 @@ nsPlainTextSerializer::AppendText(Text* aText, int32_t aStartOffset, while (offset != kNotFound) { if (offset > start) { // Pass in the line - DoAddText(false, Substring(textstr, start, offset - start)); + DoAddText(Substring(textstr, start, offset - start)); } // Pass in a newline - DoAddText(); + DoAddLineBreak(); start = offset + 1; offset = textstr.FindCharInSet(u"\n\r", start); @@ -492,9 +502,9 @@ nsPlainTextSerializer::AppendText(Text* aText, int32_t aStartOffset, // Consume the last bit of the string if there's any left if (start < length) { if (start) { - DoAddText(false, Substring(textstr, start, length - start)); + DoAddText(Substring(textstr, start, length - start)); } else { - DoAddText(false, textstr); + DoAddText(textstr); } } @@ -1108,43 +1118,37 @@ bool nsPlainTextSerializer::MustSuppressLeaf() const { return false; } -void nsPlainTextSerializer::DoAddText() { DoAddText(true, u""_ns); } - -void nsPlainTextSerializer::DoAddText(bool aIsLineBreak, - const nsAString& aText) { - // If we don't want any output, just return - if (!DoOutput()) { - return; - } - - if (!aIsLineBreak) { - // Make sure to reset this, since it's no longer true. - mHasWrittenCiteBlockquote = false; +void nsPlainTextSerializer::DoAddLineBreak() { + MOZ_ASSERT(DoOutput()); + MOZ_ASSERT(!mLineBreakDue); + MOZ_ASSERT(mIgnoreAboveIndex == (uint32_t)kNotFound); + MOZ_ASSERT(!MustSuppressLeaf()); + + // The only times we want to pass along whitespace from the original + // html source are if we're forced into preformatted mode via flags, + // or if we're prettyprinting and we're inside a <pre>. + // Otherwise, either we're collapsing to minimal text, or we're + // prettyprinting to mimic the html format, and in neither case + // does the formatting of the html source help us. + if (mSettings.HasFlag(nsIDocumentEncoder::OutputPreformatted) || + (mPreFormattedMail && !mSettings.GetWrapColumn()) || + IsElementPreformatted()) { + EnsureVerticalSpace(mEmptyLines + 1); + } else if (!mInWhitespace) { + Write(kSpace); + mInWhitespace = true; } +} - if (mLineBreakDue) EnsureVerticalSpace(mFloatingLines); +void nsPlainTextSerializer::DoAddText(const nsAString& aText) { + MOZ_ASSERT(DoOutput()); + MOZ_ASSERT(!mLineBreakDue); + MOZ_ASSERT(mIgnoreAboveIndex == (uint32_t)kNotFound); + MOZ_ASSERT(!MustSuppressLeaf()); - if (MustSuppressLeaf()) { - return; - } - - if (aIsLineBreak) { - // The only times we want to pass along whitespace from the original - // html source are if we're forced into preformatted mode via flags, - // or if we're prettyprinting and we're inside a <pre>. - // Otherwise, either we're collapsing to minimal text, or we're - // prettyprinting to mimic the html format, and in neither case - // does the formatting of the html source help us. - if (mSettings.HasFlag(nsIDocumentEncoder::OutputPreformatted) || - (mPreFormattedMail && !mSettings.GetWrapColumn()) || - IsElementPreformatted()) { - EnsureVerticalSpace(mEmptyLines + 1); - } else if (!mInWhitespace) { - Write(kSpace); - mInWhitespace = true; - } - return; - } + // Reset this, as it’s no longer true after serializing texts, so the next + // <pre> element will get a leading newline. + mHasWrittenCiteBlockquote = false; Write(aText); } diff --git a/dom/serializers/nsPlainTextSerializer.h b/dom/serializers/nsPlainTextSerializer.h @@ -123,9 +123,8 @@ class nsPlainTextSerializer final : public nsIContentSerializer { void CloseContainerForOutputFormatted(const nsAtom* aTag); nsresult DoAddLeaf(const nsAtom* aTag); - void DoAddText(); - // @param aText Ignored if aIsLineBreak is true. - void DoAddText(bool aIsLineBreak, const nsAString& aText); + void DoAddText(const nsAString& aText); + void DoAddLineBreak(); inline bool DoOutput() const { return mHeadLevel == 0; } @@ -150,7 +149,7 @@ class nsPlainTextSerializer final : public nsIContentSerializer { static bool IsCssBlockLevelElement(mozilla::dom::Element* aElement); private: - uint32_t mHeadLevel; + uint32_t mHeadLevel = 0; class Settings { public: @@ -323,7 +322,7 @@ class nsPlainTextSerializer final : public nsIContentSerializer { // If we've just written out a cite blockquote, we need to remember it // so we don't duplicate spaces before a <pre wrap> (which mail uses to quote // old messages). - bool mHasWrittenCiteBlockquote; + bool mHasWrittenCiteBlockquote = false; int32_t mFloatingLines; // To store the number of lazy line breaks @@ -343,7 +342,7 @@ class nsPlainTextSerializer final : public nsIContentSerializer { // is due because of a closing tag. Setting it to "TRUE" while closing the // tags. Hence opening tags are guaranteed to start with appropriate line // breaks. - bool mLineBreakDue; + bool mLineBreakDue = false; bool mPreformattedBlockBoundary; @@ -391,7 +390,7 @@ class nsPlainTextSerializer final : public nsIContentSerializer { // serializer enters those specific nodes, mIgnoredChildNodeLevel increases // and is greater than 0. Otherwise when serializer leaves those nodes, // mIgnoredChildNodeLevel decreases. - uint32_t mIgnoredChildNodeLevel; + uint32_t mIgnoredChildNodeLevel = 0; }; nsresult NS_NewPlainTextSerializer(nsIContentSerializer** aSerializer);