ShadowRoot.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. virtual void finalize() override;
  32. protected:
  33. virtual void visit_edges(Cell::Visitor&) override;
  34. private:
  35. ShadowRoot(Document&, Element& host, Bindings::ShadowRootMode);
  36. virtual void initialize(JS::Realm&) override;
  37. // ^Node
  38. virtual FlyString node_name() const override { return "#shadow-root"_fly_string; }
  39. virtual bool is_shadow_root() const final { return true; }
  40. // NOTE: The specification doesn't seem to specify a default value for mode. Assuming closed for now.
  41. Bindings::ShadowRootMode m_mode { Bindings::ShadowRootMode::Closed };
  42. Bindings::SlotAssignmentMode m_slot_assignment { Bindings::SlotAssignmentMode::Named };
  43. bool m_delegates_focus { false };
  44. bool m_available_to_element_internals { false };
  45. JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
  46. mutable JS::GCPtr<WebIDL::ObservableArray> m_adopted_style_sheets;
  47. };
  48. template<>
  49. inline bool Node::fast_is<ShadowRoot>() const { return node_type() == to_underlying(NodeType::DOCUMENT_FRAGMENT_NODE) && is_shadow_root(); }
  50. template<typename Callback>
  51. inline IterationDecision Node::for_each_shadow_including_inclusive_descendant(Callback callback)
  52. {
  53. if (callback(*this) == IterationDecision::Break)
  54. return IterationDecision::Break;
  55. for (auto* child = first_child(); child; child = child->next_sibling()) {
  56. if (child->is_element()) {
  57. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root_internal()) {
  58. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  59. return IterationDecision::Break;
  60. }
  61. }
  62. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  63. return IterationDecision::Break;
  64. }
  65. return IterationDecision::Continue;
  66. }
  67. template<typename Callback>
  68. inline IterationDecision Node::for_each_shadow_including_descendant(Callback callback)
  69. {
  70. for (auto* child = first_child(); child; child = child->next_sibling()) {
  71. if (child->is_element()) {
  72. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root()) {
  73. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  74. return IterationDecision::Break;
  75. }
  76. }
  77. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  78. return IterationDecision::Break;
  79. }
  80. return IterationDecision::Continue;
  81. }
  82. }