2020-12-14 21:31:10 +00:00
|
|
|
/*
|
2021-04-22 22:51:19 +02:00
|
|
|
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
2020-12-14 21:31:10 +00:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-14 21:31:10 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-03-07 15:00:02 +01:00
|
|
|
#include <LibWeb/CSS/CSSStyleRule.h>
|
2020-12-14 21:31:10 +00:00
|
|
|
#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)
|
|
|
|
|
{
|
2020-12-15 18:53:35 +01:00
|
|
|
if (!m_document.should_invalidate_styles_on_attribute_changes())
|
|
|
|
|
return;
|
2020-12-14 21:31:10 +00:00
|
|
|
auto& style_resolver = m_document.style_resolver();
|
2021-04-06 18:38:10 +01:00
|
|
|
m_document.for_each_in_inclusive_subtree_of_type<DOM::Element>([&](auto& element) {
|
2020-12-14 21:31:10 +00:00
|
|
|
m_elements_and_matching_rules_before.set(&element, style_resolver.collect_matching_rules(element));
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StyleInvalidator::~StyleInvalidator()
|
|
|
|
|
{
|
2020-12-15 18:53:35 +01:00
|
|
|
if (!m_document.should_invalidate_styles_on_attribute_changes())
|
|
|
|
|
return;
|
2020-12-14 21:31:10 +00:00
|
|
|
auto& style_resolver = m_document.style_resolver();
|
2021-04-06 18:38:10 +01:00
|
|
|
m_document.for_each_in_inclusive_subtree_of_type<DOM::Element>([&](auto& element) {
|
2020-12-14 21:31:10 +00:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|