Chromium Code Reviews| 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
|
| +} |
| + |
| +} |