1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

LibWeb: Add a simple StyleInvalidator class

This patch adds a simple, naive & inefficient class for document-wide
style invalidation, e.g. after element attribute updates. During
construction it collects a HashMap of a document's elements and their
matching rules, during destruction it does the same and then compares
the results; dirtying all elements that have a different number or order
of matching rules afterwards.

Much room for improvement, but it solves the problem of stale element
styling after attribute updates for now :^)

Fixes #4404.
This commit is contained in:
Linus Groh 2020-12-14 21:31:10 +00:00 committed by Andreas Kling
parent 9a9d655abe
commit 5e7945e26f
6 changed files with 142 additions and 16 deletions

View file

@ -28,6 +28,7 @@
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleInvalidator.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentFragment.h>
@ -83,6 +84,8 @@ String Element::attribute(const FlyString& name) const
void Element::set_attribute(const FlyString& name, const String& value)
{
CSS::StyleInvalidator style_invalidator(document());
if (auto* attribute = find_attribute(name))
attribute->set_value(value);
else
@ -93,11 +96,15 @@ void Element::set_attribute(const FlyString& name, const String& value)
void Element::remove_attribute(const FlyString& name)
{
CSS::StyleInvalidator style_invalidator(document());
m_attributes.remove_first_matching([&](auto& attribute) { return attribute.name() == name; });
}
void Element::set_attributes(Vector<Attribute>&& attributes)
{
CSS::StyleInvalidator style_invalidator(document());
m_attributes = move(attributes);
for (auto& attribute : m_attributes)
@ -161,12 +168,9 @@ void Element::parse_attribute(const FlyString& name, const String& value)
for (auto& new_class : new_classes) {
m_classes.unchecked_append(new_class);
}
set_needs_style_update(true);
} else if (name == HTML::AttributeNames::style) {
m_inline_style = parse_css_declaration(CSS::ParsingContext(document()), value);
set_needs_style_update(true);
} else if (name == HTML::AttributeNames::id) {
set_needs_style_update(true);
}
}