Skip to content

Commit

Permalink
Call GetComputedStyle() on Element - various fixes
Browse files Browse the repository at this point in the history
Nothing common about these instances. Just several changes from calling
GetComputedStyle on Node to Element.

Bug: 1496510
Change-Id: I0c8c0253085b5f97b55620fed3f21f1e7b299f11
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4982290
Reviewed-by: Anders Hartvoll Ruud <andruud@chromium.org>
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1216827}
  • Loading branch information
Rune Lillesveen authored and Chromium LUCI CQ committed Oct 30, 2023
1 parent 5d72302 commit bcb74b4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ void PendingInvalidations::ScheduleInvalidationSetsForNode(
}
}
// No need to schedule descendant invalidations on display:none elements.
if (requires_descendant_invalidation && !node.GetComputedStyle() &&
!node.IsShadowRoot()) {
if (requires_descendant_invalidation && node.IsElementNode() &&
!To<Element>(node).GetComputedStyle()) {
requires_descendant_invalidation = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,8 @@ ComputedStyleBuilder StyleResolver::InitialStyleBuilderForElement() const {

const ComputedStyle* StyleResolver::StyleForText(Text* text_node) {
DCHECK(text_node);
if (Node* parent_node = LayoutTreeBuilderTraversal::Parent(*text_node)) {
const ComputedStyle* style = parent_node->GetComputedStyle();
if (Element* parent = LayoutTreeBuilderTraversal::ParentElement(*text_node)) {
const ComputedStyle* style = parent->GetComputedStyle();
if (style && !style->IsEnsuredInDisplayNone()) {
return style;
}
Expand Down
16 changes: 9 additions & 7 deletions third_party/blink/renderer/core/dom/container_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1099,13 +1099,15 @@ void ContainerNode::ChildrenChanged(const ChildrenChange& change) {
inserted_node->ClearFlatTreeNodeDataIfHostChanged(*this);
if (!InActiveDocument())
return;
if (IsElementNode() && !GetComputedStyle()) {
// There is no need to mark for style recalc if the parent element does not
// Already have a ComputedStyle. For instance if we insert nodes into a
// display:none subtree. If this ContainerNode gets a ComputedStyle during
// the next style recalc, we will traverse into the inserted children since
// the ComputedStyle goes from null to non-null.
return;
if (Element* element = DynamicTo<Element>(this)) {
if (!element->GetComputedStyle()) {
// There is no need to mark for style recalc if the parent element does
// not already have a ComputedStyle. For instance if we insert nodes into
// a display:none subtree. If this ContainerNode gets a ComputedStyle
// during the next style recalc, we will traverse into the inserted
// children since the ComputedStyle goes from null to non-null.
return;
}
}
if (inserted_node->IsContainerNode() || inserted_node->IsTextNode())
inserted_node->SetStyleChangeOnInsertion();
Expand Down
36 changes: 17 additions & 19 deletions third_party/blink/renderer/core/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3238,12 +3238,11 @@ void Element::MarkNonSlottedHostChildrenForStyleRecalc() {
if (child->NeedsStyleRecalc()) {
continue;
}
if (!child->IsElementNode()) {
continue;
}
if (auto* style = child->GetComputedStyle()) {
if (style->IsEnsuredOutsideFlatTree()) {
child->SetStyleChangeForNonSlotted();
if (auto* element = DynamicTo<Element>(child)) {
if (auto* style = element->GetComputedStyle()) {
if (style->IsEnsuredOutsideFlatTree()) {
child->SetStyleChangeForNonSlotted();
}
}
}
}
Expand Down Expand Up @@ -7057,8 +7056,7 @@ const ComputedStyle* Element::EnsureOwnComputedStyle(
style_request.type = StyleRequest::kForComputedStyle;
if (UsesHighlightPseudoInheritance(pseudo_element_specifier)) {
const ComputedStyle* highlight_element_style = nullptr;
ContainerNode* parent = LayoutTreeBuilderTraversal::Parent(*this);
if (parent) {
if (Element* parent = LayoutTreeBuilderTraversal::ParentElement(*this)) {
highlight_element_style =
parent->GetComputedStyle()->HighlightData().Style(
pseudo_element_specifier, pseudo_argument);
Expand Down Expand Up @@ -7568,8 +7566,8 @@ const ComputedStyle* Element::StyleForPseudoElement(
// outside the flat tree should return empty styles, but currently we do
// not. See issue https://crbug.com/831568. We can replace the if-test
// with DCHECK(layout_parent) when that issue is fixed.
if (Node* layout_parent =
LayoutTreeBuilderTraversal::LayoutParent(*this)) {
if (Element* layout_parent =
LayoutTreeBuilderTraversal::LayoutParentElement(*this)) {
layout_parent_style = layout_parent->GetComputedStyle();
}
}
Expand Down Expand Up @@ -9136,23 +9134,23 @@ bool Element::checkVisibility(CheckVisibilityOptions* options) const {
}

for (Node& ancestor : FlatTreeTraversal::InclusiveAncestorsOf(*this)) {
// Check for content-visibility:hidden
if (&ancestor != this) {
if (Element* ancestor_element = DynamicTo<Element>(ancestor)) {
if (Element* ancestor_element = DynamicTo<Element>(ancestor)) {
// Check for content-visibility:hidden
if (ancestor_element != this) {
if (auto* lock = ancestor_element->GetDisplayLockContext()) {
if (lock->IsLocked() &&
!lock->IsActivatable(DisplayLockActivationReason::kViewport)) {
return false;
}
}
}
}

// Check for opacity:0
if (options->checkOpacity()) {
if (style = ancestor.GetComputedStyle(); style) {
if (style->Opacity() == 0.f) {
return false;
// Check for opacity:0
if (options->checkOpacity()) {
if (style = ancestor_element->GetComputedStyle(); style) {
if (style->Opacity() == 0.f) {
return false;
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions third_party/blink/renderer/core/dom/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ void Node::MarkSubtreeNeedsStyleRecalcForFontUpdates() {
if (GetStyleChangeType() == kSubtreeStyleChange)
return;

if (IsElementNode()) {
const ComputedStyle* style = GetComputedStyle();
if (auto* element = DynamicTo<Element>(this)) {
const ComputedStyle* style = element->GetComputedStyle();
if (!style)
return;

Expand All @@ -974,7 +974,7 @@ void Node::MarkSubtreeNeedsStyleRecalcForFontUpdates() {
// other style computations are unaffected by font loading.
if (!NeedsStyleRecalc()) {
if (style->DependsOnFontMetrics() ||
To<Element>(this)->PseudoElementStylesDependOnFontMetrics()) {
element->PseudoElementStylesDependOnFontMetrics()) {
SetNeedsStyleRecalc(
kLocalStyleChange,
StyleChangeReasonForTracing::Create(style_change_reason::kFonts));
Expand Down Expand Up @@ -1425,8 +1425,8 @@ void Node::SetForceReattachLayoutTree() {
return;
if (!InActiveDocument())
return;
if (IsElementNode()) {
if (!GetComputedStyle()) {
if (Element* element = DynamicTo<Element>(this)) {
if (!element->GetComputedStyle()) {
DCHECK(!GetLayoutObject());
return;
}
Expand Down
17 changes: 7 additions & 10 deletions third_party/blink/renderer/core/dom/text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,13 @@ LayoutText* Text::CreateTextLayoutObject() {

void Text::AttachLayoutTree(AttachContext& context) {
if (context.parent) {
ContainerNode* style_parent = LayoutTreeBuilderTraversal::Parent(*this);
if (style_parent) {
// To handle <body> to <html> writing-mode propagation, we should use
// style in layout object instead of |Node::GetComputedStyle()|.
// See http://crbug.com/988585
if (Element* style_parent =
LayoutTreeBuilderTraversal::ParentElement(*this)) {
const ComputedStyle* const style =
IsA<HTMLHtmlElement>(style_parent) && style_parent->GetLayoutObject()
? style_parent->GetLayoutObject()->Style()
: style_parent->GetComputedStyle();
DCHECK(style);
CHECK(style);
if (TextLayoutObjectIsNeeded(context, *style)) {
LayoutTreeBuilderForText(*this, context, style).CreateLayoutObject();
context.previous_in_flow = GetLayoutObject();
Expand All @@ -356,11 +353,11 @@ void Text::AttachLayoutTree(AttachContext& context) {

void Text::ReattachLayoutTreeIfNeeded(AttachContext& context) {
bool layout_object_is_needed = false;
ContainerNode* style_parent = LayoutTreeBuilderTraversal::Parent(*this);
Element* style_parent = LayoutTreeBuilderTraversal::ParentElement(*this);
if (style_parent && context.parent) {
DCHECK(style_parent->GetComputedStyle());
layout_object_is_needed =
TextLayoutObjectIsNeeded(context, *style_parent->GetComputedStyle());
const ComputedStyle* style = style_parent->GetComputedStyle();
CHECK(style);
layout_object_is_needed = TextLayoutObjectIsNeeded(context, *style);
}

if (layout_object_is_needed == !!GetLayoutObject())
Expand Down
2 changes: 1 addition & 1 deletion third_party/blink/renderer/core/html/list_item_ordinal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int ListItemOrdinal::CalcValue(const Node& item_node) const {
auto* o_list_element = DynamicTo<HTMLOListElement>(list);
const bool is_reversed = o_list_element && o_list_element->IsReversed();
int value_step = is_reversed ? -1 : 1;
if (const auto* style = item_node.GetComputedStyle()) {
if (const auto* style = To<Element>(item_node).GetComputedStyle()) {
const auto directives =
style->GetCounterDirectives(AtomicString("list-item"));
if (directives.IsSet())
Expand Down
8 changes: 6 additions & 2 deletions third_party/blink/renderer/core/layout/layout_media.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,19 @@ bool LayoutMedia::IsChildAllowed(LayoutObject* child,
// internal.
if (child->GetNode()->IsMediaControls()) {
// LayoutObject::IsInline() doesn't work at this timing.
DCHECK(!child->GetNode()->GetComputedStyle()->IsDisplayInlineType());
DCHECK(!To<Element>(child->GetNode())
->GetComputedStyle()
->IsDisplayInlineType());
return child->IsFlexibleBox();
}

if (child->GetNode()->IsTextTrackContainer() ||
child->GetNode()->IsMediaRemotingInterstitial() ||
child->GetNode()->IsPictureInPictureInterstitial()) {
// LayoutObject::IsInline() doesn't work at this timing.
DCHECK(!child->GetNode()->GetComputedStyle()->IsDisplayInlineType());
DCHECK(!To<Element>(child->GetNode())
->GetComputedStyle()
->IsDisplayInlineType());
return true;
}

Expand Down

0 comments on commit bcb74b4

Please sign in to comment.