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

Unified 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, 3 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/dom/DocumentStatisticsCollector.cpp
diff --git a/third_party/WebKit/Source/core/dom/DocumentStatisticsCollector.cpp b/third_party/WebKit/Source/core/dom/DocumentStatisticsCollector.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..76a408099a2c1c7cf72656761475b71850a395ec
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/DocumentStatisticsCollector.cpp
@@ -0,0 +1,95 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "DocumentStatisticsCollector.h"
+
+#include "core/HTMLNames.h"
+#include "core/editing/EphemeralRange.h"
+#include "core/editing/iterators/TextIterator.h"
+#include "core/editing/iterators/WordAwareIterator.h"
+#include "core/inspector/ConsoleMessage.h"
+#include "platform/text/TextBreakIterator.h"
+#include "wtf/text/StringBuilder.h"
+
+using namespace WTF;
+using namespace Unicode;
+
+namespace blink {
+
+using namespace HTMLNames;
+
+
+DocumentStatisticsCollector::DocumentStatisticsCollector()
+ : m_statisticsCollected(false)
+ , m_readyToCollect(false)
+{
+}
+
+void DocumentStatisticsCollector::collectStatistics(Document& document)
+{
+ if (!m_readyToCollect)
+ return;
+
+ if (m_statisticsCollected)
+ return;
+
+ if (!document.frame() || !document.frame()->isMainFrame())
+ return;
+
+ if (!document.hasFinishedParsing())
+ return;
+
+ ASSERT(document.body());
+
+ StringBuilder message;
+ message.append("Done: ");
+ message.appendNumber(m_statisticsCollected);
+
+ m_statisticsCollected = true;
+
+ DocumentStatistics statistics = {0};
+
+ // First, traverse the DOM tree and collect statistics on element counts and textContent length.
+ for (Node& node : NodeTraversal::descendantsOf(*document.body())) {
+ statistics.nodeCount++;
+ if (node.isElementNode()) {
+ statistics.elementCount++;
+ Element& element = toElement(node);
+ if (element.hasTagName(aTag))
+ statistics.anchorElementCount++;
+ else if (element.hasTagName(formTag))
+ statistics.formElementCount++;
+ } else if (node.isTextNode()) {
+ String text = toText(node).data();
+ statistics.textContentLength += text.length();
+ }
+ }
+
+ // Next, traverse the Layout tree and collect statistics on innerText length.
+ EphemeralRange range = EphemeralRange::rangeOfContents(*document.body());
+ WordAwareIterator it(range.startPosition(), range.endPosition());
+ for (; !it.atEnd(); it.advance()) {
+ statistics.innerTextLength += it.length();
+ }
+
+ message.append(". FinishedParsing: ");
+ message.appendNumber(document.hasFinishedParsing());
+ message.append(". Node: ");
+ message.appendNumber(statistics.nodeCount);
+ message.append(", elements: ");
+ message.appendNumber(statistics.elementCount);
+ message.append(", anchors: ");
+ message.appendNumber(statistics.anchorElementCount);
+ message.append(", forms: ");
+ message.appendNumber(statistics.formElementCount);
+ message.append(", textContentLength: ");
+ message.appendNumber(statistics.textContentLength);
+ message.append(", innerTextLength: ");
+ message.appendNumber(statistics.innerTextLength);
+ RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSource, DebugMessageLevel, message.toString());
+ // 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
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698