StyleSheetList.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/StyleSheetListPrototype.h>
  9. #include <LibWeb/CSS/StyleComputer.h>
  10. #include <LibWeb/CSS/StyleSheetList.h>
  11. #include <LibWeb/DOM/Document.h>
  12. namespace Web::CSS {
  13. void StyleSheetList::add_sheet(CSSStyleSheet& sheet)
  14. {
  15. sheet.set_style_sheet_list({}, this);
  16. if (m_sheets.is_empty()) {
  17. // This is the first sheet, append it to the list.
  18. m_sheets.append(sheet);
  19. } else {
  20. // We have sheets from before. Insert the new sheet in the correct position (DOM tree order).
  21. bool did_insert = false;
  22. for (ssize_t i = m_sheets.size() - 1; i >= 0; --i) {
  23. auto& existing_sheet = *m_sheets[i];
  24. auto position = existing_sheet.owner_node()->compare_document_position(sheet.owner_node());
  25. if (position & DOM::Node::DocumentPosition::DOCUMENT_POSITION_FOLLOWING) {
  26. m_sheets.insert(i + 1, sheet);
  27. did_insert = true;
  28. break;
  29. }
  30. }
  31. if (!did_insert)
  32. m_sheets.prepend(sheet);
  33. }
  34. if (sheet.rules().length() == 0) {
  35. // NOTE: If the added sheet has no rules, we don't have to invalidate anything.
  36. return;
  37. }
  38. m_document->style_computer().invalidate_rule_cache();
  39. m_document->style_computer().load_fonts_from_sheet(sheet);
  40. m_document->invalidate_style();
  41. }
  42. void StyleSheetList::remove_sheet(CSSStyleSheet& sheet)
  43. {
  44. sheet.set_style_sheet_list({}, nullptr);
  45. m_sheets.remove_first_matching([&](auto& entry) { return entry.ptr() == &sheet; });
  46. if (sheet.rules().length() == 0) {
  47. // NOTE: If the removed sheet had no rules, we don't have to invalidate anything.
  48. return;
  49. }
  50. sort_sheets();
  51. m_document->style_computer().invalidate_rule_cache();
  52. m_document->invalidate_style();
  53. }
  54. JS::NonnullGCPtr<StyleSheetList> StyleSheetList::create(DOM::Document& document)
  55. {
  56. auto& realm = document.realm();
  57. return realm.heap().allocate<StyleSheetList>(realm, document);
  58. }
  59. StyleSheetList::StyleSheetList(DOM::Document& document)
  60. : Bindings::LegacyPlatformObject(document.realm())
  61. , m_document(document)
  62. {
  63. }
  64. void StyleSheetList::initialize(JS::Realm& realm)
  65. {
  66. Base::initialize(realm);
  67. set_prototype(&Bindings::ensure_web_prototype<Bindings::StyleSheetListPrototype>(realm, "StyleSheetList"));
  68. }
  69. void StyleSheetList::visit_edges(Cell::Visitor& visitor)
  70. {
  71. Base::visit_edges(visitor);
  72. visitor.visit(m_document);
  73. for (auto sheet : m_sheets)
  74. visitor.visit(sheet.ptr());
  75. }
  76. // https://www.w3.org/TR/cssom/#ref-for-dfn-supported-property-indices%E2%91%A1
  77. bool StyleSheetList::is_supported_property_index(u32 index) const
  78. {
  79. // The object’s supported property indices are the numbers in the range zero to one less than the number of CSS style sheets represented by the collection.
  80. // If there are no such CSS style sheets, then there are no supported property indices.
  81. if (m_sheets.is_empty())
  82. return false;
  83. return index < m_sheets.size();
  84. }
  85. WebIDL::ExceptionOr<JS::Value> StyleSheetList::item_value(size_t index) const
  86. {
  87. if (index >= m_sheets.size())
  88. return JS::js_undefined();
  89. return m_sheets[index].ptr();
  90. }
  91. void StyleSheetList::sort_sheets()
  92. {
  93. quick_sort(m_sheets, [](JS::NonnullGCPtr<StyleSheet> a, JS::NonnullGCPtr<StyleSheet> b) {
  94. return a->owner_node()->is_before(*b->owner_node());
  95. });
  96. }
  97. }