mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-08 04:02:52 +00:00
Many times, attribute mutation doesn't necessitate a full style invalidation on the element. However, the conditions are pretty elaborate, so this first version has a lot of false positives. We only need to invalidate style when any of these things apply: 1. The change may affect the match state of a selector somewhere. 2. The change may affect presentational hints applied to the element. For (1) in this first version, we have a fixed list of attribute names that may affect selectors. We also collect all names referenced by attribute selectors anywhere in the document. For (2), we add a new Element::is_presentational_hint() virtual that tells us whether a given attribute name is a presentational hint. This drastically reduces style work on many websites. As an example, https://cnn.com/ is once again browseable.
86 lines
2.8 KiB
C++
86 lines
2.8 KiB
C++
/*
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
#include <LibWeb/Bindings/SVGStopElementPrototype.h>
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
#include <LibWeb/SVG/AttributeParser.h>
|
|
#include <LibWeb/SVG/SVGStopElement.h>
|
|
|
|
namespace Web::SVG {
|
|
|
|
GC_DEFINE_ALLOCATOR(SVGStopElement);
|
|
|
|
SVGStopElement::SVGStopElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
|
: SVGElement(document, qualified_name)
|
|
{
|
|
}
|
|
|
|
void SVGStopElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_)
|
|
{
|
|
Base::attribute_changed(name, old_value, value, namespace_);
|
|
|
|
if (name == SVG::AttributeNames::offset) {
|
|
m_offset = AttributeParser::parse_number_percentage(value.value_or(String {}));
|
|
}
|
|
}
|
|
|
|
bool SVGStopElement::is_presentational_hint(FlyString const& name) const
|
|
{
|
|
if (Base::is_presentational_hint(name))
|
|
return true;
|
|
|
|
return first_is_one_of(name,
|
|
"stop-color"sv,
|
|
"stop-opacity"sv);
|
|
}
|
|
|
|
void SVGStopElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
|
|
{
|
|
CSS::Parser::ParsingContext parsing_context { document() };
|
|
for_each_attribute([&](auto& name, auto& value) {
|
|
CSS::Parser::ParsingContext parsing_context { document() };
|
|
if (name.equals_ignoring_ascii_case("stop-color"sv)) {
|
|
if (auto stop_color = parse_css_value(parsing_context, value, CSS::PropertyID::StopColor)) {
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::StopColor, stop_color.release_nonnull());
|
|
}
|
|
} else if (name.equals_ignoring_ascii_case("stop-opacity"sv)) {
|
|
if (auto stop_opacity = parse_css_value(parsing_context, value, CSS::PropertyID::StopOpacity)) {
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::StopOpacity, stop_opacity.release_nonnull());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
Gfx::Color SVGStopElement::stop_color() const
|
|
{
|
|
if (auto computed_properties = this->computed_properties())
|
|
return computed_properties->stop_color();
|
|
return Color::Black;
|
|
}
|
|
|
|
float SVGStopElement::stop_opacity() const
|
|
{
|
|
if (auto computed_properties = this->computed_properties())
|
|
return computed_properties->stop_opacity();
|
|
return 1;
|
|
}
|
|
|
|
GC::Ref<SVGAnimatedNumber> SVGStopElement::offset() const
|
|
{
|
|
// FIXME: Implement this properly.
|
|
return SVGAnimatedNumber::create(realm(), 0, 0);
|
|
}
|
|
|
|
void SVGStopElement::initialize(JS::Realm& realm)
|
|
{
|
|
Base::initialize(realm);
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGStopElement);
|
|
}
|
|
|
|
}
|