Commit c542d374 authored by bugsnash's avatar bugsnash Committed by Commit bot

Made ElementRareData store ComputedStyle on LayoutObject if possible.

Made ElementRareData store ComputedStyle on LayoutObject of it's base
class NodeRareDataBase if it has a LayoutObject.
Otherwise it is stored on the ComputedStyle member of ElementRareData.

The reason for this is to make it consistent with storage of
ComputedStyle on Node, which stores a union that can be one of:
1) a ComputedStyle
2) a LayoutObject (ComputedStyle can be stored on this)
3) a NodeRareData (LayoutObject can be stored on this which stores
ComputedStyle or if no LayoutObject ComputedStyle can be stored
directly on ElementRareData)

This patch encapsulates the logic of where to store the ComputedStyle
on the ElementRareData so that it doesn't need to be done at the call
site (e.g. in new method Node::setComputedStyle from
https://codereview.chromium.org/2001453002)

The clearComputedStyle method is special and only clears the
ComputedStyle member as its current use is not intended to clear a
LayoutObject's ComputedStyle.

BUG=595137

Review-Url: https://codereview.chromium.org/2293713002
Cr-Commit-Position: refs/heads/master@{#418453}
parent 219a575e
......@@ -993,6 +993,7 @@ source_set("unit_tests") {
"dom/DOMImplementationTest.cpp",
"dom/DocumentStatisticsCollectorTest.cpp",
"dom/DocumentTest.cpp",
"dom/ElementRareDataTest.cpp",
"dom/ElementTest.cpp",
"dom/ExecutionContextTaskTest.cpp",
"dom/MainThreadTaskRunnerTest.cpp",
......
......@@ -1561,7 +1561,7 @@ void Element::attachLayoutTree(const AttachContext& context)
// need to clear any state that's been added since then.
if (hasRareData() && getStyleChangeType() == NeedsReattachStyleChange) {
ElementRareData* data = elementRareData();
data->clearComputedStyle();
data->clearComputedStyleIfNoLayoutObject();
}
if (!isSlotOrActiveInsertionPoint())
......@@ -1607,7 +1607,7 @@ void Element::detachLayoutTree(const AttachContext& context)
// attachLayoutTree() will clear the computed style for us when inside recalcStyle.
if (!document().inStyleRecalc())
data->clearComputedStyle();
data->clearComputedStyleIfNoLayoutObject();
if (ElementAnimations* elementAnimations = data->elementAnimations()) {
if (context.performingReattach) {
......@@ -1737,7 +1737,7 @@ void Element::recalcStyle(StyleRecalcChange change, Text* nextTextSibling)
if (hasRareData()) {
ElementRareData* data = elementRareData();
if (change != IndependentInherit)
data->clearComputedStyle();
data->clearComputedStyleIfNoLayoutObject();
if (change >= IndependentInherit) {
if (ElementAnimations* elementAnimations = data->elementAnimations())
......
......@@ -91,9 +91,25 @@ public:
NamedNodeMap* attributeMap() const { return m_attributeMap.get(); }
void setAttributeMap(NamedNodeMap* attributeMap) { m_attributeMap = attributeMap; }
ComputedStyle* computedStyle() const { return m_computedStyle.get(); }
void setComputedStyle(PassRefPtr<ComputedStyle> computedStyle) { m_computedStyle = computedStyle; }
void clearComputedStyle() { m_computedStyle = nullptr; }
ComputedStyle* computedStyle() const
{
DCHECK(!(layoutObject() && m_computedStyle));
if (layoutObject())
return layoutObject()->mutableStyle();
return m_computedStyle.get();
}
void setComputedStyle(PassRefPtr<ComputedStyle> computedStyle)
{
if (layoutObject())
layoutObject()->setStyleInternal(computedStyle);
else
m_computedStyle = computedStyle;
}
void clearComputedStyleIfNoLayoutObject()
{
DCHECK(!(layoutObject() && m_computedStyle));
m_computedStyle = nullptr;
}
ClassList* classList() const { return m_classList.get(); }
void setClassList(ClassList* classList) { m_classList = classList; }
......
// Copyright 2016 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 "core/dom/ElementRareData.h"
#include "core/layout/LayoutInline.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(ElementRareDataTest, ComputedStyleStorage)
{
// Without LayoutObject
ElementRareData* rareData = ElementRareData::create(nullptr);
RefPtr<ComputedStyle> style = ComputedStyle::create();
rareData->setComputedStyle(style);
EXPECT_EQ(style.get(), rareData->computedStyle());
// With LayoutObject
LayoutObject* layoutObject = new LayoutInline(nullptr);
rareData = ElementRareData::create(layoutObject);
rareData->setComputedStyle(style);
EXPECT_EQ(style.get(), rareData->computedStyle());
rareData->setLayoutObject(nullptr);
delete layoutObject;
}
} // namespace blink
......@@ -110,7 +110,7 @@ enum class CustomElementState {
NotDefinedFlag = 2 << nodeCustomElementShift,
};
class NodeRareDataBase {
class CORE_EXPORT NodeRareDataBase {
public:
LayoutObject* layoutObject() const { return m_layoutObject; }
void setLayoutObject(LayoutObject* layoutObject) { m_layoutObject = layoutObject; }
......
......@@ -60,7 +60,7 @@ private:
NodeMutationObserverData() { }
};
class NodeRareData : public GarbageCollectedFinalized<NodeRareData>, public NodeRareDataBase {
class CORE_EXPORT NodeRareData : public GarbageCollectedFinalized<NodeRareData>, public NodeRareDataBase {
WTF_MAKE_NONCOPYABLE(NodeRareData);
public:
static NodeRareData* create(LayoutObject* layoutObject)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment