Bug 1334088 Part 1: Correct BoxToRectAndText to respect content offset and length when returning text.
MozReview-Commit-ID: Kd3DIqZdBFY
--- 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)
{