From 0fcd743a640b2ec9f5cb7268833020ea40de4ec2 Mon Sep 17 00:00:00 2001 From: Elliott Sprehn Date: Tue, 20 Jan 2015 14:26:54 -0800 Subject: [PATCH] Remove Element::textFromChildren(). Instead just use textContent() and teach HTMLStyleElement how to atomize its children. I also added the optimization to Node::textContent so that Text just returns its value instead of allocating a new buffer which avoids malloc and copy when getting the textContent of text. R=abarth@chromium.org Review URL: https://codereview.chromium.org/864613002 --- engine/core/dom/Element.cpp | 40 --------------------------- engine/core/dom/Element.h | 2 -- engine/core/dom/Node.cpp | 2 ++ engine/core/html/HTMLStyleElement.cpp | 6 ++-- 4 files changed, 6 insertions(+), 44 deletions(-) diff --git a/engine/core/dom/Element.cpp b/engine/core/dom/Element.cpp index cd282835c46..2a9d24f901e 100644 --- a/engine/core/dom/Element.cpp +++ b/engine/core/dom/Element.cpp @@ -1305,46 +1305,6 @@ void Element::dispatchFocusOutEvent(const AtomicString& eventType, Element* newF dispatchScopedEventDispatchMediator(FocusOutEventDispatchMediator::create(FocusEvent::create(eventType, true, false, document().domWindow(), 0, newFocusedElement))); } -String Element::textFromChildren() -{ - Text* firstTextNode = 0; - bool foundMultipleTextNodes = false; - unsigned totalLength = 0; - - for (Node* child = firstChild(); child; child = child->nextSibling()) { - if (!child->isTextNode()) - continue; - Text* text = toText(child); - if (!firstTextNode) - firstTextNode = text; - else - foundMultipleTextNodes = true; - unsigned length = text->data().length(); - if (length > std::numeric_limits::max() - totalLength) - return emptyString(); - totalLength += length; - } - - if (!firstTextNode) - return emptyString(); - - if (firstTextNode && !foundMultipleTextNodes) { - firstTextNode->atomize(); - return firstTextNode->data(); - } - - StringBuilder content; - content.reserveCapacity(totalLength); - for (Node* child = firstTextNode; child; child = child->nextSibling()) { - if (!child->isTextNode()) - continue; - content.append(toText(child)->data()); - } - - ASSERT(content.length() == totalLength); - return content.toString(); -} - // FIXME(sky): Remove pseudoElementSpecifier. RenderStyle* Element::computedStyle(PseudoId pseudoElementSpecifier) { diff --git a/engine/core/dom/Element.h b/engine/core/dom/Element.h index a1823950b9c..16befaa1b6c 100644 --- a/engine/core/dom/Element.h +++ b/engine/core/dom/Element.h @@ -295,8 +295,6 @@ public: void click(); - String textFromChildren(); - protected: Element(const QualifiedName& tagName, Document*, ConstructionType); diff --git a/engine/core/dom/Node.cpp b/engine/core/dom/Node.cpp index e56d01eca8c..4bb54e7f4b9 100644 --- a/engine/core/dom/Node.cpp +++ b/engine/core/dom/Node.cpp @@ -908,6 +908,8 @@ ContainerNode* Node::ownerScope() const String Node::textContent() const { + if (isTextNode()) + return toText(this)->data(); StringBuilder content; for (const Node* node = this; node; node = NodeTraversal::next(*node, this)) { if (node->isTextNode()) diff --git a/engine/core/html/HTMLStyleElement.cpp b/engine/core/html/HTMLStyleElement.cpp index e3b70a4faca..757c6127652 100644 --- a/engine/core/html/HTMLStyleElement.cpp +++ b/engine/core/html/HTMLStyleElement.cpp @@ -30,6 +30,7 @@ #include "sky/engine/core/dom/Document.h" #include "sky/engine/core/dom/Element.h" #include "sky/engine/core/dom/StyleEngine.h" +#include "sky/engine/core/dom/Text.h" #include "sky/engine/core/dom/shadow/ShadowRoot.h" #include "sky/engine/core/frame/LocalFrame.h" #include "sky/engine/platform/TraceEvent.h" @@ -124,8 +125,9 @@ void HTMLStyleElement::process() MediaQueryEvaluator screenEval("screen", true); if (screenEval.eval(mediaQueries.get())) { - const String& text = textFromChildren(); - m_sheet = document().styleEngine()->createSheet(this, text); + if (hasOneTextChild()) + toText(firstChild())->atomize(); + m_sheet = document().styleEngine()->createSheet(this, textContent()); m_sheet->setMediaQueries(mediaQueries.release()); }