ShadowRoot.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/DOM/DocumentFragment.h>
  8. namespace Web::DOM {
  9. class ShadowRoot final : public DocumentFragment {
  10. WEB_PLATFORM_OBJECT(ShadowRoot, DocumentFragment);
  11. public:
  12. bool closed() const { return m_closed; }
  13. bool delegates_focus() const { return m_delegates_focus; }
  14. void set_delegates_focus(bool delegates_focus) { m_delegates_focus = delegates_focus; }
  15. bool available_to_element_internals() const { return m_available_to_element_internals; }
  16. void set_available_to_element_internals(bool available_to_element_internals) { m_available_to_element_internals = available_to_element_internals; }
  17. // ^EventTarget
  18. virtual EventTarget* get_parent(Event const&) override;
  19. // NOTE: This is intended for the JS bindings.
  20. String mode() const { return m_closed ? "closed" : "open"; }
  21. String inner_html() const;
  22. WebIDL::ExceptionOr<void> set_inner_html(String const&);
  23. private:
  24. ShadowRoot(Document&, Element&);
  25. // ^Node
  26. virtual FlyString node_name() const override { return "#shadow-root"; }
  27. virtual bool is_shadow_root() const final { return true; }
  28. // NOTE: The specification doesn't seem to specify a default value for closed. Assuming false for now.
  29. bool m_closed { false };
  30. bool m_delegates_focus { false };
  31. bool m_available_to_element_internals { false };
  32. };
  33. template<>
  34. inline bool Node::fast_is<ShadowRoot>() const { return is_shadow_root(); }
  35. template<typename Callback>
  36. inline IterationDecision Node::for_each_shadow_including_descendant(Callback callback)
  37. {
  38. if (callback(*this) == IterationDecision::Break)
  39. return IterationDecision::Break;
  40. for (auto* child = first_child(); child; child = child->next_sibling()) {
  41. if (child->is_element()) {
  42. if (JS::GCPtr<ShadowRoot> shadow_root = static_cast<Element*>(child)->shadow_root()) {
  43. if (shadow_root->for_each_shadow_including_descendant(callback) == IterationDecision::Break)
  44. return IterationDecision::Break;
  45. }
  46. }
  47. if (child->for_each_shadow_including_descendant(callback) == IterationDecision::Break)
  48. return IterationDecision::Break;
  49. }
  50. return IterationDecision::Continue;
  51. }
  52. }