Bug 1334088 Part 1: Correct BoxToRectAndText to respect content offset and length when returning text. draft
authorBrad Werth <bwerth@mozilla.com>
Fri, 17 Feb 2017 14:57:37 -0800
changeset 488787 ba4f09fb6e1c091314a97a7f7542936bebd43847
parent 488741 22ec1dab9e821676f4204d36ce9801803032f504
child 488788 18d2d6f230ce49d83827051e03c17b049a2b569d
push id46641
push userbwerth@mozilla.com
push dateThu, 23 Feb 2017 18:58:17 +0000
bugs1334088
milestone54.0a1
Bug 1334088 Part 1: Correct BoxToRectAndText to respect content offset and length when returning text. MozReview-Commit-ID: Kd3DIqZdBFY
layout/base/nsLayoutUtils.cpp
--- a/layout/base/nsLayoutUtils.cpp
+++ b/layout/base/nsLayoutUtils.cpp
@@ -3974,24 +3974,49 @@ struct BoxToRect : public nsLayoutUtils:
 
 struct BoxToRectAndText : public BoxToRect {
   mozilla::dom::DOMStringList* mTextList;
 
   BoxToRectAndText(nsIFrame* aRelativeTo, nsLayoutUtils::RectCallback* aCallback,
                    mozilla::dom::DOMStringList* aTextList, uint32_t aFlags)
     : BoxToRect(aRelativeTo, aCallback, aFlags), mTextList(aTextList) {}
 
+  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);
+    }
+
+    for (nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
+         child;
+         child = child->GetNextSibling()) {
+      AccumulateText(child, aResult);
+    }
+  }
+
   virtual void AddBox(nsIFrame* aFrame) override {
     BoxToRect::AddBox(aFrame);
     if (mTextList) {
-      nsIContent* content = aFrame->GetContent();
-      nsAutoString textContent;
-      mozilla::ErrorResult err; // ignored
-      content->GetTextContent(textContent, err);
-      mTextList->Add(textContent);
+      nsAutoString textForFrame;
+      AccumulateText(aFrame, textForFrame);
+      mTextList->Add(textForFrame);
     }
   }
 };
 
 void
 nsLayoutUtils::GetAllInFlowRects(nsIFrame* aFrame, nsIFrame* aRelativeTo,
                                  RectCallback* aCallback, uint32_t aFlags)
 {