ShadowRoot.h 3.5 KB

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