mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:38:12 +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:
parent
9a9d655abe
commit
5e7945e26f
6 changed files with 142 additions and 16 deletions
70
Libraries/LibWeb/CSS/StyleInvalidator.cpp
Normal file
70
Libraries/LibWeb/CSS/StyleInvalidator.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibWeb/CSS/StyleInvalidator.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
StyleInvalidator::StyleInvalidator(DOM::Document& document)
|
||||
: m_document(document)
|
||||
{
|
||||
auto& style_resolver = m_document.style_resolver();
|
||||
m_document.for_each_in_subtree_of_type<DOM::Element>([&](auto& element) {
|
||||
m_elements_and_matching_rules_before.set(&element, style_resolver.collect_matching_rules(element));
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
StyleInvalidator::~StyleInvalidator()
|
||||
{
|
||||
auto& style_resolver = m_document.style_resolver();
|
||||
m_document.for_each_in_subtree_of_type<DOM::Element>([&](auto& element) {
|
||||
auto maybe_matching_rules_before = m_elements_and_matching_rules_before.get(&element);
|
||||
if (!maybe_matching_rules_before.has_value()) {
|
||||
element.set_needs_style_update(true);
|
||||
return IterationDecision::Continue;
|
||||
}
|
||||
auto& matching_rules_before = maybe_matching_rules_before.value();
|
||||
auto matching_rules_after = style_resolver.collect_matching_rules(element);
|
||||
if (matching_rules_before.size() != matching_rules_after.size()) {
|
||||
element.set_needs_style_update(true);
|
||||
return IterationDecision::Continue;
|
||||
}
|
||||
style_resolver.sort_matching_rules(matching_rules_before);
|
||||
style_resolver.sort_matching_rules(matching_rules_after);
|
||||
for (size_t i = 0; i < matching_rules_before.size(); ++i) {
|
||||
if (matching_rules_before[i].rule != matching_rules_after[i].rule) {
|
||||
element.set_needs_style_update(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue