StyleInvalidator.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSStyleRule.h>
  7. #include <LibWeb/CSS/StyleInvalidator.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. namespace Web::CSS {
  11. StyleInvalidator::StyleInvalidator(DOM::Document& document)
  12. : m_document(document)
  13. {
  14. if (!m_document.should_invalidate_styles_on_attribute_changes())
  15. return;
  16. auto& style_computer = m_document.style_computer();
  17. m_document.for_each_in_inclusive_subtree_of_type<DOM::Element>([&](auto& element) {
  18. m_elements_and_matching_rules_before.set(&element, style_computer.collect_matching_rules(element));
  19. return IterationDecision::Continue;
  20. });
  21. }
  22. StyleInvalidator::~StyleInvalidator()
  23. {
  24. if (!m_document.should_invalidate_styles_on_attribute_changes())
  25. return;
  26. auto& style_computer = m_document.style_computer();
  27. m_document.for_each_in_inclusive_subtree_of_type<DOM::Element>([&](auto& element) {
  28. auto maybe_matching_rules_before = m_elements_and_matching_rules_before.get(&element);
  29. if (!maybe_matching_rules_before.has_value()) {
  30. element.set_needs_style_update(true);
  31. return IterationDecision::Continue;
  32. }
  33. auto& matching_rules_before = maybe_matching_rules_before.value();
  34. auto matching_rules_after = style_computer.collect_matching_rules(element);
  35. if (matching_rules_before.size() != matching_rules_after.size()) {
  36. element.set_needs_style_update(true);
  37. return IterationDecision::Continue;
  38. }
  39. style_computer.sort_matching_rules(matching_rules_before);
  40. style_computer.sort_matching_rules(matching_rules_after);
  41. for (size_t i = 0; i < matching_rules_before.size(); ++i) {
  42. if (matching_rules_before[i].rule != matching_rules_after[i].rule) {
  43. element.set_needs_style_update(true);
  44. break;
  45. }
  46. }
  47. return IterationDecision::Continue;
  48. });
  49. }
  50. }