Commit 2db73868 authored by Ian Kilpatrick's avatar Ian Kilpatrick Committed by Commit Bot

[css-layout-api] Change FilteredComputedStyleMap to Prepopulated.

This changes the filtered computed-style-map to being pre-populated.

This shouldn't have any performance difference for the paint api, but
should have increased performance for the layout api.

Additionally this allows style for anonymous boxes to exist. Before we
always had a Node->ComputedStyleMap, however with the layout api it is
possible for access the style for an anonymous box.

Bug: 726125
Change-Id: I2820001f158ac0938a277af491a05f1a72b8c734
Reviewed-on: https://chromium-review.googlesource.com/937905
Commit-Queue: Ian Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: default avatarRune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539908}
parent 787994ad
......@@ -1704,7 +1704,7 @@ jumbo_source_set("unit_tests") {
"css/cssom/CSSResourceValueTest.cpp",
"css/cssom/CSSStyleImageValueTest.cpp",
"css/cssom/CSSUnitValueTest.cpp",
"css/cssom/FilteredComputedStylePropertyMapTest.cpp",
"css/cssom/PrepopulatedComputedStylePropertyMapTest.cpp",
"css/invalidation/InvalidationSetTest.cpp",
"css/invalidation/StyleInvalidatorTest.cpp",
"css/parser/CSSLazyParsingTest.cpp",
......
......@@ -372,10 +372,10 @@ blink_core_sources("css") {
"cssom/DeclaredStylePropertyMap.cpp",
"cssom/DeclaredStylePropertyMap.h",
"cssom/ElementComputedStyleMap.h",
"cssom/FilteredComputedStylePropertyMap.cpp",
"cssom/FilteredComputedStylePropertyMap.h",
"cssom/InlineStylePropertyMap.cpp",
"cssom/InlineStylePropertyMap.h",
"cssom/PrepopulatedComputedStylePropertyMap.cpp",
"cssom/PrepopulatedComputedStylePropertyMap.h",
"cssom/StylePropertyMap.cpp",
"cssom/StylePropertyMap.h",
"cssom/StylePropertyMapReadOnly.cpp",
......
// 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/css/cssom/FilteredComputedStylePropertyMap.h"
#include "core/css/CSSCustomPropertyDeclaration.h"
#include "core/css/cssom/CSSUnparsedValue.h"
namespace blink {
FilteredComputedStylePropertyMap::FilteredComputedStylePropertyMap(
Node* node,
const Vector<CSSPropertyID>& native_properties,
const Vector<AtomicString>& custom_properties)
: ComputedStylePropertyMap(node) {
for (const auto& native_property : native_properties) {
// Silently drop shorthand properties.
DCHECK_NE(native_property, CSSPropertyInvalid);
if (CSSProperty::Get(native_property).IsShorthand())
continue;
native_properties_.insert(native_property);
}
for (const auto& custom_property : custom_properties) {
custom_properties_.insert(custom_property);
}
}
unsigned int FilteredComputedStylePropertyMap::size() {
return native_properties_.size() + custom_properties_.size();
}
const CSSValue* FilteredComputedStylePropertyMap::GetProperty(
CSSPropertyID property_id) {
if (!native_properties_.Contains(property_id))
return nullptr;
return ComputedStylePropertyMap::GetProperty(property_id);
}
const CSSValue* FilteredComputedStylePropertyMap::GetCustomProperty(
AtomicString property_name) {
if (!custom_properties_.Contains(AtomicString(property_name)))
return nullptr;
const CSSValue* value =
ComputedStylePropertyMap::GetCustomProperty(property_name);
if (!value)
return CSSUnparsedValue::Create()->ToCSSValue();
return value;
}
void FilteredComputedStylePropertyMap::ForEachProperty(
const IterationCallback& callback) {
for (const auto property_id : native_properties_) {
if (const CSSValue* value = GetProperty(property_id)) {
callback(CSSProperty::Get(property_id).GetPropertyNameAtomicString(),
*value);
}
}
for (const auto& name : custom_properties_) {
const CSSValue* value = GetCustomProperty(name);
DCHECK(value);
callback(name, *value);
}
}
} // namespace blink
// 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.
#ifndef FilteredComputedStylePropertyMap_h
#define FilteredComputedStylePropertyMap_h
#include "base/macros.h"
#include "core/CoreExport.h"
#include "core/css/CSSPropertyIDTemplates.h"
#include "core/css/cssom/ComputedStylePropertyMap.h"
namespace blink {
class CORE_EXPORT FilteredComputedStylePropertyMap
: public ComputedStylePropertyMap {
public:
static FilteredComputedStylePropertyMap* Create(
Node* node,
const Vector<CSSPropertyID>& native_properties,
const Vector<AtomicString>& custom_properties) {
return new FilteredComputedStylePropertyMap(node, native_properties,
custom_properties);
}
unsigned int size() final;
private:
FilteredComputedStylePropertyMap(
Node*,
const Vector<CSSPropertyID>& native_properties,
const Vector<AtomicString>& custom_properties);
const CSSValue* GetProperty(CSSPropertyID) override;
const CSSValue* GetCustomProperty(AtomicString) override;
void ForEachProperty(const IterationCallback&) override;
HashSet<CSSPropertyID> native_properties_;
HashSet<AtomicString> custom_properties_;
DISALLOW_COPY_AND_ASSIGN(FilteredComputedStylePropertyMap);
};
} // namespace blink
#endif // FilteredComputedStylePropertyMap_h
// Copyright 2018 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/css/cssom/PrepopulatedComputedStylePropertyMap.h"
#include "core/css/CSSCustomPropertyDeclaration.h"
#include "core/css/CSSVariableData.h"
#include "core/css/ComputedStyleCSSValueMapping.h"
#include "core/css/cssom/CSSUnparsedValue.h"
#include "core/dom/Document.h"
#include "core/style/ComputedStyle.h"
namespace blink {
PrepopulatedComputedStylePropertyMap::PrepopulatedComputedStylePropertyMap(
const Document& document,
const ComputedStyle& style,
Node* styled_node,
const Vector<CSSPropertyID>& native_properties,
const Vector<AtomicString>& custom_properties)
: StylePropertyMapReadOnly(), styled_node_(styled_node) {
// NOTE: This may over-reserve as shorthand properties will get dropped from
// being in the map.
native_values_.ReserveCapacityForSize(native_properties.size());
custom_values_.ReserveCapacityForSize(custom_properties.size());
for (const auto& property_id : native_properties) {
// Silently drop shorthand properties.
DCHECK_NE(property_id, CSSPropertyInvalid);
if (CSSProperty::Get(property_id).IsShorthand())
continue;
UpdateNativeProperty(style, property_id);
}
for (const auto& property_name : custom_properties) {
UpdateCustomProperty(document, style, property_name);
}
}
unsigned PrepopulatedComputedStylePropertyMap::size() {
return native_values_.size() + custom_values_.size();
}
void PrepopulatedComputedStylePropertyMap::UpdateStyle(
const Document& document,
const ComputedStyle& style) {
for (const auto& property_id : native_values_.Keys()) {
DCHECK_NE(property_id, CSSPropertyInvalid);
UpdateNativeProperty(style, property_id);
}
for (const auto& property_name : custom_values_.Keys()) {
UpdateCustomProperty(document, style, property_name);
}
}
void PrepopulatedComputedStylePropertyMap::UpdateNativeProperty(
const ComputedStyle& style,
CSSPropertyID property_id) {
native_values_.Set(property_id,
CSSProperty::Get(property_id)
.CSSValueFromComputedStyle(
style, /* layout_object */ nullptr, styled_node_,
/* allow_visited_style */ false));
}
void PrepopulatedComputedStylePropertyMap::UpdateCustomProperty(
const Document& document,
const ComputedStyle& style,
const AtomicString& property_name) {
const CSSValue* value = ComputedStyleCSSValueMapping::Get(
property_name, style, document.GetPropertyRegistry());
if (!value)
value = CSSUnparsedValue::Create()->ToCSSValue();
custom_values_.Set(property_name, value);
}
const CSSValue* PrepopulatedComputedStylePropertyMap::GetProperty(
CSSPropertyID property_id) {
return native_values_.at(property_id);
}
const CSSValue* PrepopulatedComputedStylePropertyMap::GetCustomProperty(
AtomicString property_name) {
return custom_values_.at(property_name);
}
void PrepopulatedComputedStylePropertyMap::ForEachProperty(
const IterationCallback& callback) {
for (const auto& entry : native_values_) {
DCHECK(entry.value);
callback(CSSProperty::Get(entry.key).GetPropertyNameAtomicString(),
*entry.value);
}
for (const auto& entry : custom_values_) {
DCHECK(entry.value);
callback(entry.key, *entry.value);
}
}
void PrepopulatedComputedStylePropertyMap::Trace(blink::Visitor* visitor) {
visitor->Trace(styled_node_);
visitor->Trace(native_values_);
visitor->Trace(custom_values_);
StylePropertyMapReadOnly::Trace(visitor);
}
} // namespace blink
// Copyright 2018 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.
#ifndef PrepopulatedComputedStylePropertyMap_h
#define PrepopulatedComputedStylePropertyMap_h
#include "base/macros.h"
#include "core/css/CSSPropertyIDTemplates.h"
#include "core/css/cssom/StylePropertyMapReadOnly.h"
namespace blink {
// This class has the same behaviour as the ComputedStylePropertyMap, except it
// only contains the properties given to the constructor.
//
// It is to be used with the Houdini APIs (css-paint-api, css-layout-api) which
// require style maps with a subset of properties.
//
// It will pre-populate internal maps (property->CSSValue), as the above APIs
// have a high probability of querying the values inside the map, however as a
// result when the ComputedStyle changes UpdateStyle needs to be called to
// re-populate the internal maps.
class CORE_EXPORT PrepopulatedComputedStylePropertyMap
: public StylePropertyMapReadOnly {
public:
// NOTE: styled_node may be null, in the case where this map is for an
// anonymous box.
PrepopulatedComputedStylePropertyMap(
const Document&,
const ComputedStyle&,
Node* styled_node,
const Vector<CSSPropertyID>& native_properties,
const Vector<AtomicString>& custom_properties);
// Updates the values of the properties based on the new computed style.
void UpdateStyle(const Document&, const ComputedStyle&);
unsigned size() override;
void Trace(blink::Visitor*) override;
protected:
const CSSValue* GetProperty(CSSPropertyID) override;
const CSSValue* GetCustomProperty(AtomicString) override;
void ForEachProperty(const IterationCallback&) override;
private:
void UpdateNativeProperty(const ComputedStyle&, CSSPropertyID);
void UpdateCustomProperty(const Document&,
const ComputedStyle&,
const AtomicString& property_name);
Member<Node> styled_node_;
HeapHashMap<CSSPropertyID, Member<const CSSValue>> native_values_;
HeapHashMap<AtomicString, Member<const CSSValue>> custom_values_;
DISALLOW_COPY_AND_ASSIGN(PrepopulatedComputedStylePropertyMap);
};
} // namespace blink
#endif
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/cssom/FilteredComputedStylePropertyMap.h"
#include "core/css/cssom/PrepopulatedComputedStylePropertyMap.h"
#include <memory>
#include "core/css/CSSComputedStyleDeclaration.h"
......@@ -13,9 +13,9 @@
namespace blink {
class FilteredComputedStylePropertyMapTest : public PageTestBase {
class PrepopulatedComputedStylePropertyMapTest : public PageTestBase {
public:
FilteredComputedStylePropertyMapTest() = default;
PrepopulatedComputedStylePropertyMapTest() = default;
CSSComputedStyleDeclaration* Declaration() const {
return declaration_.Get();
......@@ -33,12 +33,17 @@ class FilteredComputedStylePropertyMapTest : public PageTestBase {
Persistent<CSSComputedStyleDeclaration> declaration_;
};
TEST_F(FilteredComputedStylePropertyMapTest, NativePropertyAccessors) {
TEST_F(PrepopulatedComputedStylePropertyMapTest, NativePropertyAccessors) {
Vector<CSSPropertyID> native_properties(
{CSSPropertyColor, CSSPropertyAlignItems});
Vector<AtomicString> empty_custom_properties;
FilteredComputedStylePropertyMap* map =
FilteredComputedStylePropertyMap::Create(PageNode(), native_properties,
Node* node = PageNode();
const ComputedStyle& style = *node->EnsureComputedStyle();
PrepopulatedComputedStylePropertyMap* map =
new PrepopulatedComputedStylePropertyMap(GetDocument(), style, node,
native_properties,
empty_custom_properties);
DummyExceptionStateForTesting exception_state;
......@@ -65,12 +70,17 @@ TEST_F(FilteredComputedStylePropertyMapTest, NativePropertyAccessors) {
exception_state.ClearException();
}
TEST_F(FilteredComputedStylePropertyMapTest, CustomPropertyAccessors) {
TEST_F(PrepopulatedComputedStylePropertyMapTest, CustomPropertyAccessors) {
Vector<CSSPropertyID> empty_native_properties;
Vector<AtomicString> custom_properties({"--foo", "--bar"});
FilteredComputedStylePropertyMap* map =
FilteredComputedStylePropertyMap::Create(
PageNode(), empty_native_properties, custom_properties);
Node* node = PageNode();
const ComputedStyle& style = *node->EnsureComputedStyle();
PrepopulatedComputedStylePropertyMap* map =
new PrepopulatedComputedStylePropertyMap(GetDocument(), style, node,
empty_native_properties,
custom_properties);
DummyExceptionStateForTesting exception_state;
......
......@@ -497,11 +497,14 @@ CSSValue* ComputedStyleUtils::ValueForReflection(
CSSValue* ComputedStyleUtils::MinWidthOrMinHeightAuto(
Node* styled_node,
const ComputedStyle& style) {
Node* parent = styled_node->parentNode();
const ComputedStyle* ensured_style =
parent ? parent->EnsureComputedStyle() : nullptr;
if (ensured_style && ensured_style->IsDisplayFlexibleOrGridBox())
return CSSIdentifierValue::Create(CSSValueAuto);
if (styled_node) {
Node* parent = styled_node->parentNode();
const ComputedStyle* ensured_style =
parent ? parent->EnsureComputedStyle() : nullptr;
if (ensured_style && ensured_style->IsDisplayFlexibleOrGridBox())
return CSSIdentifierValue::Create(CSSValueAuto);
}
return ZoomAdjustedPixelValue(0, style);
}
......
......@@ -8,7 +8,7 @@
#include "bindings/core/v8/DictionaryIterator.h"
#include "bindings/core/v8/V8BindingForCore.h"
#include "bindings/core/v8/V8FragmentResultOptions.h"
#include "core/css/cssom/FilteredComputedStylePropertyMap.h"
#include "core/css/cssom/PrepopulatedComputedStylePropertyMap.h"
#include "core/dom/ExecutionContext.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/layout/custom/FragmentResultOptions.h"
......@@ -71,7 +71,8 @@ bool CSSLayoutDefinition::Instance::Layout(
// This should be shared with the CSS paint, but without the invalidation.
DCHECK(layout_custom.GetNode());
StylePropertyMapReadOnly* style_map =
FilteredComputedStylePropertyMap::Create(
new PrepopulatedComputedStylePropertyMap(
layout_custom.GetDocument(), layout_custom.StyleRef(),
layout_custom.GetNode(), definition_->native_invalidation_properties_,
definition_->custom_invalidation_properties_);
......
......@@ -7,7 +7,7 @@
#include <memory>
#include "bindings/core/v8/V8BindingForCore.h"
#include "core/css/CSSComputedStyleDeclaration.h"
#include "core/css/cssom/FilteredComputedStylePropertyMap.h"
#include "core/css/cssom/PrepopulatedComputedStylePropertyMap.h"
#include "core/dom/ExecutionContext.h"
#include "core/layout/LayoutObject.h"
#include "modules/csspaint/PaintRenderingContext2D.h"
......@@ -97,9 +97,10 @@ scoped_refptr<Image> CSSPaintDefinition::Paint(
container_size, color_params, context_settings_, zoom);
PaintSize* paint_size = PaintSize::Create(specified_size);
StylePropertyMapReadOnly* style_map =
FilteredComputedStylePropertyMap::Create(layout_object.GetNode(),
native_invalidation_properties_,
custom_invalidation_properties_);
new PrepopulatedComputedStylePropertyMap(
layout_object.GetDocument(), layout_object.StyleRef(),
layout_object.GetNode(), native_invalidation_properties_,
custom_invalidation_properties_);
Vector<v8::Local<v8::Value>, 4> argv;
if (paint_arguments) {
......
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