HTMLElement.h 5.7 KB

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