Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1093)

Side by Side Diff: third_party/WebKit/Source/core/dom/DocumentStatisticsCollector.cpp

Issue 1248643004: Test distillability without JavaScript (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@early
Patch Set: move tests, remove dbg msg Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "config.h"
6 #include "DocumentStatisticsCollector.h"
7
8 #include "core/HTMLNames.h"
9 #include "core/editing/EphemeralRange.h"
10 #include "core/editing/iterators/TextIterator.h"
11 #include "core/editing/iterators/WordAwareIterator.h"
12 #include "core/inspector/ConsoleMessage.h"
13 #include "platform/text/TextBreakIterator.h"
14 #include "wtf/text/StringBuilder.h"
15
16 using namespace WTF;
17 using namespace Unicode;
18
19 namespace blink {
20
21 using namespace HTMLNames;
22
23
24 DocumentStatisticsCollector::DocumentStatisticsCollector()
25 : m_statisticsCollected(false)
26 , m_readyToCollect(false)
27 {
28 }
29
30 void DocumentStatisticsCollector::collectStatistics(Document& document)
31 {
32 if (!m_readyToCollect)
33 return;
34
35 if (m_statisticsCollected)
36 return;
37
38 if (!document.frame() || !document.frame()->isMainFrame())
39 return;
40
41 if (!document.hasFinishedParsing())
42 return;
43
44 ASSERT(document.body());
45
46 StringBuilder message;
47 message.append("Done: ");
48 message.appendNumber(m_statisticsCollected);
49
50 m_statisticsCollected = true;
51
52 DocumentStatistics statistics = {0};
53
54 // First, traverse the DOM tree and collect statistics on element counts and textContent length.
55 for (Node& node : NodeTraversal::descendantsOf(*document.body())) {
56 statistics.nodeCount++;
57 if (node.isElementNode()) {
58 statistics.elementCount++;
59 Element& element = toElement(node);
60 if (element.hasTagName(aTag))
61 statistics.anchorElementCount++;
62 else if (element.hasTagName(formTag))
63 statistics.formElementCount++;
64 } else if (node.isTextNode()) {
65 String text = toText(node).data();
66 statistics.textContentLength += text.length();
67 }
68 }
69
70 // Next, traverse the Layout tree and collect statistics on innerText length .
71 EphemeralRange range = EphemeralRange::rangeOfContents(*document.body());
72 WordAwareIterator it(range.startPosition(), range.endPosition());
73 for (; !it.atEnd(); it.advance()) {
74 statistics.innerTextLength += it.length();
75 }
76
77 message.append(". FinishedParsing: ");
78 message.appendNumber(document.hasFinishedParsing());
79 message.append(". Node: ");
80 message.appendNumber(statistics.nodeCount);
81 message.append(", elements: ");
82 message.appendNumber(statistics.elementCount);
83 message.append(", anchors: ");
84 message.appendNumber(statistics.anchorElementCount);
85 message.append(", forms: ");
86 message.appendNumber(statistics.formElementCount);
87 message.append(", textContentLength: ");
88 message.appendNumber(statistics.textContentLength);
89 message.append(", innerTextLength: ");
90 message.appendNumber(statistics.innerTextLength);
91 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(C onsoleAPIMessageSource, DebugMessageLevel, message.toString());
92 // document.addConsoleMessage(consoleMessage);
mdjones 2015/09/29 17:31:42 Is this logging code permanent?
wychen 2015/09/29 21:46:57 The WebKit part is not done yet. This is just for
93 }
94
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698