CSSRule.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2022-2024, Sam Atkins <sam@ladybird.org>
  4. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/CSSRulePrototype.h>
  9. #include <LibWeb/CSS/CSSLayerBlockRule.h>
  10. #include <LibWeb/CSS/CSSRule.h>
  11. #include <LibWeb/CSS/CSSStyleSheet.h>
  12. namespace Web::CSS {
  13. CSSRule::CSSRule(JS::Realm& realm, Type type)
  14. : PlatformObject(realm)
  15. , m_type(type)
  16. {
  17. }
  18. void CSSRule::visit_edges(Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_parent_style_sheet);
  22. visitor.visit(m_parent_rule);
  23. }
  24. // https://www.w3.org/TR/cssom/#dom-cssrule-type
  25. WebIDL::UnsignedShort CSSRule::type_for_bindings() const
  26. {
  27. // NOTE: Types that aren't defined in the spec must return 0.
  28. // To do this, we arbitrarily make non-spec ones start at 100.
  29. auto type = to_underlying(m_type);
  30. if (type >= 100)
  31. return 0;
  32. return type;
  33. }
  34. // https://www.w3.org/TR/cssom/#dom-cssrule-csstext
  35. String CSSRule::css_text() const
  36. {
  37. // The cssText attribute must return a serialization of the CSS rule.
  38. return serialized();
  39. }
  40. // https://www.w3.org/TR/cssom/#dom-cssrule-csstext
  41. void CSSRule::set_css_text(StringView)
  42. {
  43. // On setting the cssText attribute must do nothing.
  44. }
  45. void CSSRule::set_parent_rule(CSSRule* parent_rule)
  46. {
  47. m_parent_rule = parent_rule;
  48. clear_caches();
  49. }
  50. void CSSRule::set_parent_style_sheet(CSSStyleSheet* parent_style_sheet)
  51. {
  52. m_parent_style_sheet = parent_style_sheet;
  53. clear_caches();
  54. }
  55. void CSSRule::clear_caches()
  56. {
  57. m_cached_layer_name.clear();
  58. }
  59. FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
  60. {
  61. Vector<FlyString> layer_names;
  62. for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
  63. switch (rule->type()) {
  64. case Type::Import:
  65. // TODO: Handle `layer(foo)` in import rules once we implement that.
  66. break;
  67. case Type::LayerBlock: {
  68. auto& layer_block = static_cast<CSSLayerBlockRule const&>(*rule);
  69. layer_names.append(layer_block.internal_name());
  70. break;
  71. }
  72. // Ignore everything else
  73. // Note that LayerStatement cannot have child rules so we still ignore it here.
  74. case Type::LayerStatement:
  75. case Type::Style:
  76. case Type::Media:
  77. case Type::FontFace:
  78. case Type::Keyframes:
  79. case Type::Keyframe:
  80. case Type::Namespace:
  81. case Type::Supports:
  82. case Type::NestedDeclarations:
  83. case Type::Property:
  84. break;
  85. }
  86. }
  87. m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse()));
  88. return m_cached_layer_name.value();
  89. }
  90. }