diff options
author | Linus Groh <mail@linusgroh.de> | 2020-12-14 21:31:10 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-14 23:38:08 +0100 |
commit | 5e7945e26f1c81816758046bd45f32a384824527 (patch) | |
tree | f743ad6dda5e5d6363a5a46f61f421002bfcca9a /Libraries/LibWeb/CSS/StyleInvalidator.cpp | |
parent | 9a9d655abe88c156cb2eb3896512efe8f91d6d72 (diff) | |
download | serenity-5e7945e26f1c81816758046bd45f32a384824527.zip |
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.
Diffstat (limited to 'Libraries/LibWeb/CSS/StyleInvalidator.cpp')
-rw-r--r-- | Libraries/LibWeb/CSS/StyleInvalidator.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Libraries/LibWeb/CSS/StyleInvalidator.cpp b/Libraries/LibWeb/CSS/StyleInvalidator.cpp new file mode 100644 index 0000000000..0dd48840d5 --- /dev/null +++ b/Libraries/LibWeb/CSS/StyleInvalidator.cpp @@ -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; + }); +} + +} |