Bug 1343695 Part 1: Retrieve text content with GetRenderedText. draft
authorBrad Werth <bwerth@mozilla.com>
Wed, 01 Mar 2017 16:15:22 -0800
changeset 498501 6f57521c9a2f11b9ee6cd7af723b53096f044dfa
parent 498360 2baef2ffbaedb7354286726660ebd36e84b432f0
child 498502 8871b8a6c46eb8af87e3f5cd5cb43b23991baf24
push id49216
push userbwerth@mozilla.com
push dateTue, 14 Mar 2017 21:42:09 +0000
bugs1343695
milestone55.0a1
Bug 1343695 Part 1: Retrieve text content with GetRenderedText. MozReview-Commit-ID: 4VLoaTlDELG
dom/base/nsRange.cpp
layout/base/nsLayoutUtils.cpp
--- a/dom/base/nsRange.cpp
+++ b/dom/base/nsRange.cpp
@@ -2968,23 +2968,16 @@ GetTextFrameForContent(nsIContent* aCont
 static nsresult GetPartialTextRect(nsLayoutUtils::RectCallback* aCallback,
                                    Sequence<nsString>* aTextList,
                                    nsIContent* aContent, int32_t aStartOffset,
                                    int32_t aEndOffset, bool aClampToEdge,
                                    bool aFlushLayout)
 {
   nsTextFrame* textFrame = GetTextFrameForContent(aContent, aFlushLayout);
   if (textFrame) {
-    // If we'll need it later, collect the full content text now.
-    nsAutoString textContent;
-    if (aTextList) {
-      mozilla::ErrorResult err; // ignored
-      aContent->GetTextContent(textContent, err);
-    }
-
     nsIFrame* relativeTo = nsLayoutUtils::GetContainingBlockForClientRect(textFrame);
     for (nsTextFrame* f = textFrame; f; f = static_cast<nsTextFrame*>(f->GetNextContinuation())) {
       int32_t fstart = f->GetContentOffset(), fend = f->GetContentEnd();
       if (fend <= aStartOffset || fstart >= aEndOffset)
         continue;
 
       // Calculate the text content offsets we'll need if text is requested.
       int32_t textContentStart = fstart;
@@ -3005,21 +2998,23 @@ static nsresult GetPartialTextRect(nsLay
         ExtractRectFromOffset(f, aEndOffset, &r, !rtl, aClampToEdge);
         textContentEnd = aEndOffset;
       }
       r = nsLayoutUtils::TransformFrameRectToAncestor(f, r, relativeTo);
       aCallback->AddRect(r);
 
       // Finally capture the text, if requested.
       if (aTextList) {
-        const nsAString& textSubstring =
-          Substring(textContent,
-                    textContentStart,
-                    (textContentEnd - textContentStart));
-        aTextList->AppendElement(textSubstring, fallible);
+        nsIFrame::RenderedText renderedText = f->GetRenderedText(
+          textContentStart,
+          textContentEnd,
+          nsIFrame::TextOffsetType::OFFSETS_IN_CONTENT_TEXT,
+          nsIFrame::TrailingWhitespace::DONT_TRIM_TRAILING_WHITESPACE);
+
+        aTextList->AppendElement(renderedText.mString, fallible);
       }
     }
   }
   return NS_OK;
 }
 
 /* static */ void
 nsRange::CollectClientRectsAndText(nsLayoutUtils::RectCallback* aCollector,
--- a/layout/base/nsLayoutUtils.cpp
+++ b/layout/base/nsLayoutUtils.cpp
@@ -4013,26 +4013,23 @@ struct MOZ_RAII BoxToRectAndText : publi
   static void AccumulateText(nsIFrame* aFrame, nsAString& aResult) {
     MOZ_ASSERT(aFrame);
 
     // Get all the text in aFrame and child frames, while respecting
     // the content offsets in each of the nsTextFrames.
     if (aFrame->GetType() == nsGkAtoms::textFrame) {
       nsTextFrame* textFrame = static_cast<nsTextFrame*>(aFrame);
 
-      nsIContent* content = textFrame->GetContent();
-      nsAutoString textContent;
-      mozilla::ErrorResult err; // ignored
-      content->GetTextContent(textContent, err);
-
-      const nsAString& textSubstring =
-        Substring(textContent,
-                  textFrame->GetContentOffset(),
-                  textFrame->GetContentLength());
-      aResult.Append(textSubstring);
+      nsIFrame::RenderedText renderedText = textFrame->GetRenderedText(
+        textFrame->GetContentOffset(),
+        textFrame->GetContentOffset() + textFrame->GetContentLength(),
+        nsIFrame::TextOffsetType::OFFSETS_IN_CONTENT_TEXT,
+        nsIFrame::TrailingWhitespace::DONT_TRIM_TRAILING_WHITESPACE);
+
+      aResult.Append(renderedText.mString);
     }
 
     for (nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
          child;
          child = child->GetNextSibling()) {
       AccumulateText(child, aResult);
     }
   }