ShadowRoot.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Bindings/ShadowRootPrototype.h>
  8. #include <LibWeb/DOM/DocumentFragment.h>
  9. #include <LibWeb/WebIDL/ObservableArray.h>
  10. namespace Web::DOM {
  11. class ShadowRoot final : public DocumentFragment {
  12. WEB_PLATFORM_OBJECT(ShadowRoot, DocumentFragment);
  13. JS_DECLARE_ALLOCATOR(ShadowRoot);
  14. public:
  15. Bindings::ShadowRootMode mode() const { return m_mode; }
  16. Bindings::SlotAssignmentMode slot_assignment() const { return m_slot_assignment; }
  17. void set_slot_assignment(Bindings::SlotAssignmentMode slot_assignment) { m_slot_assignment = slot_assignment; }
  18. bool delegates_focus() const { return m_delegates_focus; }
  19. void set_delegates_focus(bool delegates_focus) { m_delegates_focus = delegates_focus; }
  20. bool available_to_element_internals() const { return m_available_to_element_internals; }
  21. void set_available_to_element_internals(bool available_to_element_internals) { m_available_to_element_internals = available_to_element_internals; }
  22. // ^EventTarget
  23. virtual EventTarget* get_parent(Event const&) override;
  24. WebIDL::ExceptionOr<String> inner_html() const;
  25. WebIDL::ExceptionOr<void> set_inner_html(StringView);
  26. CSS::StyleSheetList& style_sheets();
  27. CSS::StyleSheetList const& style_sheets() const;
  28. CSS::StyleSheetList* style_sheets_for_bindings() { return &style_sheets(); }
  29. JS::NonnullGCPtr<WebIDL::ObservableArray> adopted_style_sheets() const;
  30. WebIDL::ExceptionOr<void> set_adopted_style_sheets(JS::Value);
  31. void for_each_css_style_sheet(Function<void(CSS::CSSStyleSheet&)>&& callback) const;
  32. virtual void finalize() override;
  33. protected:
  34. virtual void visit_edges(Cell::Visitor&) override;
  35. private:
  36. ShadowRoot(Document&, Element& host, Bindings::ShadowRootMode);
  37. virtual void initialize(JS::Realm&) override;
  38. // ^Node
  39. virtual FlyString node_name() const override { return "#shadow-root"_fly_string; }
  40. virtual bool is_shadow_root() const final { return true; }
  41. // NOTE: The specification doesn't seem to specify a default value for mode. Assuming closed for now.
  42. Bindings::ShadowRootMode m_mode { Bindings::ShadowRootMode::Closed };
  43. Bindings::SlotAssignmentMode m_slot_assignment { Bindings::SlotAssignmentMode::Named };
  44. bool m_delegates_focus { false };
  45. bool m_available_to_element_internals { false };
  46. JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
  47. mutable JS::GCPtr<WebIDL::ObservableArray> m_adopted_style_sheets;
  48. };
  49. template<>
  50. inline bool Node::fast_is<ShadowRoot>() const { return node_type() == to_underlying(NodeType::DOCUMENT_FRAGMENT_NODE) && is_shadow_root(); }
  51. template<typename Callback>
  52. inline IterationDecision Node::for_each_shadow_including_inclusive_descendant(Callback callback)
  53. {
  54. if (callback(*this) == IterationDecision::Break)
  55. return IterationDecision::Break;
  56. for (auto* child = first_child(); child; child = child->next_sibling()) {
  57. if (child->is_element()) {
  58. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root_internal()) {
  59. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  60. return IterationDecision::Break;
  61. }
  62. }
  63. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  64. return IterationDecision::Break;
  65. }
  66. return IterationDecision::Continue;
  67. }
  68. template<typename Callback>
  69. inline IterationDecision Node::for_each_shadow_including_descendant(Callback callback)
  70. {
  71. for (auto* child = first_child(); child; child = child->next_sibling()) {
  72. if (child->is_element()) {
  73. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root()) {
  74. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  75. return IterationDecision::Break;
  76. }
  77. }
  78. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  79. return IterationDecision::Break;
  80. }
  81. return IterationDecision::Continue;
  82. }
  83. }