Skip to content

Commit 9d515ec

Browse files
Calme1709awesomekling
authored andcommitted
LibWeb: Share a ColorResolutionContext in apply_style
There is no need to recreate this each time we need it when we can instead create it once and then reuse it. This is stop-gap since we should resolve colors to their computed forms as part of StyleComputer::compute_properties Reduces time spent in ColorResolutionContext::for_layout_node from ~1.1% to ~0.1% when loading https://en.wikipedia.org/wiki/2023_in_American_television
1 parent dd9d6d2 commit 9d515ec

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Libraries/LibWeb/Layout/Node.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
388388
// NOTE: color must be set after font_size as `CalculatedStyleValue`s can rely on it being set for resolving lengths.
389389
computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::color()));
390390

391+
// NOTE: This color resolution context must be created after we set color above so that currentColor resolves correctly
392+
// FIXME: We should resolve colors to their absolute forms at compute time (i.e. by implementing the relevant absolutized methods)
393+
auto color_resolution_context = CSS::ColorResolutionContext::for_layout_node_with_style(*this);
394+
391395
computed_values.set_vertical_align(computed_style.vertical_align());
392396

393397
{
@@ -521,7 +525,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
521525

522526
computed_values.set_background_layers(move(layers));
523527
}
524-
computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::background_color()));
528+
computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, color_resolution_context, CSS::InitialValues::background_color()));
525529

526530
computed_values.set_box_sizing(computed_style.box_sizing());
527531

@@ -575,7 +579,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
575579
if (computed_style.filter().has_filters())
576580
computed_values.set_filter(computed_style.filter());
577581

578-
computed_values.set_flood_color(computed_style.color_or_fallback(CSS::PropertyID::FloodColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::flood_color()));
582+
computed_values.set_flood_color(computed_style.color_or_fallback(CSS::PropertyID::FloodColor, color_resolution_context, CSS::InitialValues::flood_color()));
579583
computed_values.set_flood_opacity(computed_style.flood_opacity());
580584

581585
computed_values.set_justify_content(computed_style.justify_content());
@@ -640,10 +644,10 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
640644
// FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
641645
// we just manually grab the value from `color`. This makes it dependent on `color` being
642646
// specified first, so it's far from ideal.
643-
computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color()));
647+
computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, color_resolution_context, computed_values.color()));
644648
computed_values.set_text_decoration_thickness(computed_style.text_decoration_thickness());
645649

646-
computed_values.set_webkit_text_fill_color(computed_style.color_or_fallback(CSS::PropertyID::WebkitTextFillColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color()));
650+
computed_values.set_webkit_text_fill_color(computed_style.color_or_fallback(CSS::PropertyID::WebkitTextFillColor, color_resolution_context, computed_values.color()));
647651

648652
computed_values.set_text_shadow(computed_style.text_shadow(*this));
649653

@@ -692,7 +696,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
692696
// FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
693697
// we just manually grab the value from `color`. This makes it dependent on `color` being
694698
// specified first, so it's far from ideal.
695-
border.color = computed_style.color_or_fallback(color_property, CSS::ColorResolutionContext::for_layout_node_with_style(*this), computed_values.color());
699+
border.color = computed_style.color_or_fallback(color_property, color_resolution_context, computed_values.color());
696700
border.line_style = computed_style.line_style(style_property);
697701

698702
// FIXME: Interpolation can cause negative values - we clamp here but should instead clamp as part of interpolation
@@ -705,7 +709,7 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
705709
do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
706710

707711
if (auto const& outline_color = computed_style.property(CSS::PropertyID::OutlineColor); outline_color.has_color())
708-
computed_values.set_outline_color(outline_color.to_color(CSS::ColorResolutionContext::for_layout_node_with_style(*this)).value());
712+
computed_values.set_outline_color(outline_color.to_color(color_resolution_context).value());
709713
if (auto const& outline_offset = computed_style.property(CSS::PropertyID::OutlineOffset); outline_offset.is_length())
710714
computed_values.set_outline_offset(outline_offset.as_length().length());
711715
computed_values.set_outline_style(computed_style.outline_style());
@@ -741,16 +745,16 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
741745

742746
auto const& fill = computed_style.property(CSS::PropertyID::Fill);
743747
if (fill.has_color())
744-
computed_values.set_fill(fill.to_color(CSS::ColorResolutionContext::for_layout_node_with_style(*this)).value());
748+
computed_values.set_fill(fill.to_color(color_resolution_context).value());
745749
else if (fill.is_url())
746750
computed_values.set_fill(fill.as_url().url());
747751
auto const& stroke = computed_style.property(CSS::PropertyID::Stroke);
748752
if (stroke.has_color())
749-
computed_values.set_stroke(stroke.to_color(CSS::ColorResolutionContext::for_layout_node_with_style(*this)).value());
753+
computed_values.set_stroke(stroke.to_color(color_resolution_context).value());
750754
else if (stroke.is_url())
751755
computed_values.set_stroke(stroke.as_url().url());
752756

753-
computed_values.set_stop_color(computed_style.color_or_fallback(CSS::PropertyID::StopColor, CSS::ColorResolutionContext::for_layout_node_with_style(*this), CSS::InitialValues::stop_color()));
757+
computed_values.set_stop_color(computed_style.color_or_fallback(CSS::PropertyID::StopColor, color_resolution_context, CSS::InitialValues::stop_color()));
754758

755759
auto const& stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
756760
// FIXME: Converting to pixels isn't really correct - values should be in "user units"

0 commit comments

Comments
 (0)