ShadowRoot.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. namespace Web::DOM {
  10. class ShadowRoot final : public DocumentFragment {
  11. WEB_PLATFORM_OBJECT(ShadowRoot, DocumentFragment);
  12. public:
  13. Bindings::ShadowRootMode mode() const { return m_mode; }
  14. Bindings::SlotAssignmentMode slot_assignment() const { return m_slot_assignment; }
  15. void set_slot_assignment(Bindings::SlotAssignmentMode slot_assignment) { m_slot_assignment = slot_assignment; }
  16. bool delegates_focus() const { return m_delegates_focus; }
  17. void set_delegates_focus(bool delegates_focus) { m_delegates_focus = delegates_focus; }
  18. bool available_to_element_internals() const { return m_available_to_element_internals; }
  19. void set_available_to_element_internals(bool available_to_element_internals) { m_available_to_element_internals = available_to_element_internals; }
  20. // ^EventTarget
  21. virtual EventTarget* get_parent(Event const&) override;
  22. WebIDL::ExceptionOr<DeprecatedString> inner_html() const;
  23. WebIDL::ExceptionOr<void> set_inner_html(StringView);
  24. private:
  25. ShadowRoot(Document&, Element& host, Bindings::ShadowRootMode);
  26. virtual void initialize(JS::Realm&) override;
  27. // ^Node
  28. virtual FlyString node_name() const override { return "#shadow-root"_fly_string; }
  29. virtual bool is_shadow_root() const final { return true; }
  30. // NOTE: The specification doesn't seem to specify a default value for mode. Assuming closed for now.
  31. Bindings::ShadowRootMode m_mode { Bindings::ShadowRootMode::Closed };
  32. Bindings::SlotAssignmentMode m_slot_assignment { Bindings::SlotAssignmentMode::Named };
  33. bool m_delegates_focus { false };
  34. bool m_available_to_element_internals { false };
  35. };
  36. template<>
  37. inline bool Node::fast_is<ShadowRoot>() const { return is_shadow_root(); }
  38. template<typename Callback>
  39. inline IterationDecision Node::for_each_shadow_including_inclusive_descendant(Callback callback)
  40. {
  41. if (callback(*this) == IterationDecision::Break)
  42. return IterationDecision::Break;
  43. for (auto* child = first_child(); child; child = child->next_sibling()) {
  44. if (child->is_element()) {
  45. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root_internal()) {
  46. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  47. return IterationDecision::Break;
  48. }
  49. }
  50. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  51. return IterationDecision::Break;
  52. }
  53. return IterationDecision::Continue;
  54. }
  55. template<typename Callback>
  56. inline IterationDecision Node::for_each_shadow_including_descendant(Callback callback)
  57. {
  58. for (auto* child = first_child(); child; child = child->next_sibling()) {
  59. if (child->is_element()) {
  60. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root()) {
  61. if (shadow_root->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  62. return IterationDecision::Break;
  63. }
  64. }
  65. if (child->for_each_shadow_including_inclusive_descendant(callback) == IterationDecision::Break)
  66. return IterationDecision::Break;
  67. }
  68. return IterationDecision::Continue;
  69. }
  70. }