HTMLElement.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/HTML/GlobalEventHandlers.h>
  11. #include <LibWeb/HTML/HTMLOrSVGElement.h>
  12. #include <LibWeb/HTML/ToggleTaskTracker.h>
  13. #include <LibWeb/HTML/TokenizedFeatures.h>
  14. namespace Web::HTML {
  15. // https://html.spec.whatwg.org/multipage/dom.html#attr-dir
  16. #define ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTES \
  17. __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(ltr) \
  18. __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(rtl) \
  19. __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(auto)
  20. // https://html.spec.whatwg.org/#attr-contenteditable
  21. enum class ContentEditableState {
  22. True,
  23. False,
  24. PlaintextOnly,
  25. Inherit,
  26. };
  27. struct ShowPopoverOptions {
  28. GC::Ptr<HTMLElement> source;
  29. };
  30. struct TogglePopoverOptions : public ShowPopoverOptions {
  31. Optional<bool> force {};
  32. };
  33. using TogglePopoverOptionsOrForceBoolean = Variant<TogglePopoverOptions, bool>;
  34. enum class ThrowExceptions {
  35. Yes,
  36. No,
  37. };
  38. enum class FocusPreviousElement {
  39. Yes,
  40. No,
  41. };
  42. enum class FireEvents {
  43. Yes,
  44. No,
  45. };
  46. enum class ExpectedToBeShowing {
  47. Yes,
  48. No,
  49. };
  50. class HTMLElement
  51. : public DOM::Element
  52. , public HTML::GlobalEventHandlers
  53. , public HTML::HTMLOrSVGElement<HTMLElement> {
  54. WEB_PLATFORM_OBJECT(HTMLElement, DOM::Element);
  55. GC_DECLARE_ALLOCATOR(HTMLElement);
  56. public:
  57. virtual ~HTMLElement() override;
  58. Optional<String> title() const { return attribute(HTML::AttributeNames::title); }
  59. StringView dir() const;
  60. void set_dir(String const&);
  61. virtual bool is_focusable() const override;
  62. bool is_content_editable() const;
  63. StringView content_editable() const;
  64. ContentEditableState content_editable_state() const { return m_content_editable_state; }
  65. WebIDL::ExceptionOr<void> set_content_editable(StringView);
  66. String inner_text();
  67. void set_inner_text(StringView);
  68. [[nodiscard]] String outer_text();
  69. WebIDL::ExceptionOr<void> set_outer_text(String const&);
  70. int offset_top() const;
  71. int offset_left() const;
  72. int offset_width() const;
  73. int offset_height() const;
  74. GC::Ptr<Element> offset_parent() const;
  75. bool cannot_navigate() const;
  76. void click();
  77. [[nodiscard]] String access_key_label() const;
  78. bool fire_a_synthetic_pointer_event(FlyString const& type, DOM::Element& target, bool not_trusted);
  79. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  80. virtual bool is_labelable() const { return false; }
  81. GC::Ptr<DOM::NodeList> labels();
  82. virtual Optional<ARIA::Role> default_role() const override;
  83. String get_an_elements_target() const;
  84. TokenizedFeature::NoOpener get_an_elements_noopener(StringView target) const;
  85. WebIDL::ExceptionOr<GC::Ref<ElementInternals>> attach_internals();
  86. WebIDL::ExceptionOr<void> set_popover(Optional<String> value);
  87. Optional<String> popover() const;
  88. virtual void removed_from(Node*) override;
  89. enum class PopoverVisibilityState {
  90. Hidden,
  91. Showing,
  92. };
  93. PopoverVisibilityState popover_visibility_state() const { return m_popover_visibility_state; }
  94. WebIDL::ExceptionOr<void> show_popover_for_bindings(ShowPopoverOptions const& = {});
  95. WebIDL::ExceptionOr<void> hide_popover_for_bindings();
  96. WebIDL::ExceptionOr<bool> toggle_popover(TogglePopoverOptionsOrForceBoolean const&);
  97. WebIDL::ExceptionOr<void> show_popover(ThrowExceptions throw_exceptions, GC::Ptr<HTMLElement> invoker);
  98. WebIDL::ExceptionOr<void> hide_popover(FocusPreviousElement focus_previous_element, FireEvents fire_events, ThrowExceptions throw_exceptions);
  99. protected:
  100. HTMLElement(DOM::Document&, DOM::QualifiedName);
  101. virtual void initialize(JS::Realm&) override;
  102. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
  103. virtual WebIDL::ExceptionOr<void> cloned(DOM::Node&, bool) override;
  104. virtual void inserted() override;
  105. virtual void visit_edges(Cell::Visitor&) override;
  106. private:
  107. virtual bool is_html_element() const final { return true; }
  108. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  109. // ^HTML::GlobalEventHandlers
  110. virtual GC::Ptr<DOM::EventTarget> global_event_handlers_to_event_target(FlyString const&) override { return *this; }
  111. virtual void did_receive_focus() override;
  112. virtual void did_lose_focus() override;
  113. [[nodiscard]] String get_the_text_steps();
  114. GC::Ref<DOM::DocumentFragment> rendered_text_fragment(StringView input);
  115. GC::Ptr<DOM::NodeList> m_labels;
  116. WebIDL::ExceptionOr<bool> check_popover_validity(ExpectedToBeShowing expected_to_be_showing, ThrowExceptions throw_exceptions, GC::Ptr<DOM::Document>);
  117. void queue_a_popover_toggle_event_task(String old_state, String new_state);
  118. // https://html.spec.whatwg.org/multipage/custom-elements.html#attached-internals
  119. GC::Ptr<ElementInternals> m_attached_internals;
  120. // https://html.spec.whatwg.org/#attr-contenteditable
  121. ContentEditableState m_content_editable_state { ContentEditableState::Inherit };
  122. // https://html.spec.whatwg.org/multipage/interaction.html#click-in-progress-flag
  123. bool m_click_in_progress { false };
  124. // Popover API
  125. // https://html.spec.whatwg.org/multipage/popover.html#popover-visibility-state
  126. PopoverVisibilityState m_popover_visibility_state { PopoverVisibilityState::Hidden };
  127. // https://html.spec.whatwg.org/multipage/popover.html#popover-invoker
  128. GC::Ptr<HTMLElement> m_popover_invoker;
  129. // https://html.spec.whatwg.org/multipage/popover.html#popover-showing-or-hiding
  130. bool m_popover_showing_or_hiding { false };
  131. // https://html.spec.whatwg.org/multipage/popover.html#the-popover-attribute:toggle-task-tracker
  132. Optional<ToggleTaskTracker> m_popover_toggle_task_tracker;
  133. // https://html.spec.whatwg.org/multipage/popover.html#popover-close-watcher
  134. GC::Ptr<CloseWatcher> m_popover_close_watcher;
  135. };
  136. }
  137. namespace Web::DOM {
  138. template<>
  139. inline bool Node::fast_is<HTML::HTMLElement>() const { return is_html_element(); }
  140. }