OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "public/web/WebFrameContentDumper.h" |
| 6 |
| 7 #include "core/editing/EphemeralRange.h" |
| 8 #include "core/editing/iterators/TextIterator.h" |
| 9 #include "core/editing/serializers/Serialization.h" |
| 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/layout/LayoutPart.h" |
| 12 #include "core/layout/LayoutTreeAsText.h" |
| 13 #include "core/layout/LayoutView.h" |
| 14 #include "public/web/WebDocument.h" |
| 15 #include "public/web/WebLocalFrame.h" |
| 16 #include "web/WebLocalFrameImpl.h" |
| 17 #include "wtf/text/WTFString.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 static void frameContentAsPlainText(size_t maxChars, LocalFrame* frame, StringBu
ilder& output) |
| 22 { |
| 23 Document* document = frame->document(); |
| 24 if (!document) |
| 25 return; |
| 26 |
| 27 if (!frame->view()) |
| 28 return; |
| 29 |
| 30 // Select the document body. |
| 31 if (document->body()) { |
| 32 const EphemeralRange range = EphemeralRange::rangeOfContents(*document->
body()); |
| 33 |
| 34 // The text iterator will walk nodes giving us text. This is similar to |
| 35 // the plainText() function in core/editing/TextIterator.h, but we imple
ment the maximum |
| 36 // size and also copy the results directly into a wstring, avoiding the |
| 37 // string conversion. |
| 38 for (TextIterator it(range.startPosition(), range.endPosition()); !it.at
End(); it.advance()) { |
| 39 it.text().appendTextToStringBuilder(output, 0, maxChars - output.len
gth()); |
| 40 if (output.length() >= maxChars) |
| 41 return; // Filled up the buffer. |
| 42 } |
| 43 } |
| 44 |
| 45 // The separator between frames when the frames are converted to plain text. |
| 46 const LChar frameSeparator[] = { '\n', '\n' }; |
| 47 const size_t frameSeparatorLength = WTF_ARRAY_LENGTH(frameSeparator); |
| 48 |
| 49 // Recursively walk the children. |
| 50 const FrameTree& frameTree = frame->tree(); |
| 51 for (Frame* curChild = frameTree.firstChild(); curChild; curChild = curChild
->tree().nextSibling()) { |
| 52 if (!curChild->isLocalFrame()) |
| 53 continue; |
| 54 LocalFrame* curLocalChild = toLocalFrame(curChild); |
| 55 // Ignore the text of non-visible frames. |
| 56 LayoutView* contentLayoutObject = curLocalChild->contentLayoutObject(); |
| 57 LayoutPart* ownerLayoutObject = curLocalChild->ownerLayoutObject(); |
| 58 if (!contentLayoutObject || !contentLayoutObject->size().width() || !con
tentLayoutObject->size().height() |
| 59 || (contentLayoutObject->location().x() + contentLayoutObject->size(
).width() <= 0) || (contentLayoutObject->location().y() + contentLayoutObject->s
ize().height() <= 0) |
| 60 || (ownerLayoutObject && ownerLayoutObject->style() && ownerLayoutOb
ject->style()->visibility() != VISIBLE)) { |
| 61 continue; |
| 62 } |
| 63 |
| 64 // Make sure the frame separator won't fill up the buffer, and give up i
f |
| 65 // it will. The danger is if the separator will make the buffer longer t
han |
| 66 // maxChars. This will cause the computation above: |
| 67 // maxChars - output->size() |
| 68 // to be a negative number which will crash when the subframe is added. |
| 69 if (output.length() >= maxChars - frameSeparatorLength) |
| 70 return; |
| 71 |
| 72 output.append(frameSeparator, frameSeparatorLength); |
| 73 frameContentAsPlainText(maxChars, curLocalChild, output); |
| 74 if (output.length() >= maxChars) |
| 75 return; // Filled up the buffer. |
| 76 } |
| 77 } |
| 78 |
| 79 WebString WebFrameContentDumper::dumpFrameTreeAsText(WebLocalFrame* frame, size_
t maxChars) |
| 80 { |
| 81 if (!frame) |
| 82 return WebString(); |
| 83 StringBuilder text; |
| 84 frameContentAsPlainText(maxChars, toWebLocalFrameImpl(frame)->frame(), text)
; |
| 85 return text.toString(); |
| 86 } |
| 87 |
| 88 WebString WebFrameContentDumper::dumpAsMarkup(WebLocalFrame* frame) |
| 89 { |
| 90 if (!frame) |
| 91 return WebString(); |
| 92 return createMarkup(toWebLocalFrameImpl(frame)->frame()->document()); |
| 93 } |
| 94 |
| 95 WebString WebFrameContentDumper::dumpLayoutTreeAsText(WebLocalFrame* frame, Layo
utAsTextControls toShow) |
| 96 { |
| 97 if (!frame) |
| 98 return WebString(); |
| 99 LayoutAsTextBehavior behavior = LayoutAsTextShowAllLayers; |
| 100 |
| 101 if (toShow & LayoutAsTextWithLineTrees) |
| 102 behavior |= LayoutAsTextShowLineTrees; |
| 103 |
| 104 if (toShow & LayoutAsTextDebug) |
| 105 behavior |= LayoutAsTextShowCompositedLayers | LayoutAsTextShowAddresses
| LayoutAsTextShowIDAndClass | LayoutAsTextShowLayerNesting; |
| 106 |
| 107 if (toShow & LayoutAsTextPrinting) |
| 108 behavior |= LayoutAsTextPrintingMode; |
| 109 |
| 110 return externalRepresentation(toWebLocalFrameImpl(frame)->frame(), behavior)
; |
| 111 } |
| 112 } |
OLD | NEW |