HTMLElement.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class HTMLElement
  26. : public DOM::Element
  27. , public HTML::GlobalEventHandlers
  28. , public HTML::HTMLOrSVGElement<HTMLElement> {
  29. WEB_PLATFORM_OBJECT(HTMLElement, DOM::Element);
  30. GC_DECLARE_ALLOCATOR(HTMLElement);
  31. public:
  32. virtual ~HTMLElement() override;
  33. Optional<String> title() const { return attribute(HTML::AttributeNames::title); }
  34. StringView dir() const;
  35. void set_dir(String const&);
  36. virtual bool is_editable() const final;
  37. virtual bool is_focusable() const override;
  38. bool is_content_editable() const;
  39. StringView content_editable() const;
  40. ContentEditableState content_editable_state() const { return m_content_editable_state; }
  41. WebIDL::ExceptionOr<void> set_content_editable(StringView);
  42. String inner_text();
  43. void set_inner_text(StringView);
  44. [[nodiscard]] String outer_text();
  45. WebIDL::ExceptionOr<void> set_outer_text(String const&);
  46. int offset_top() const;
  47. int offset_left() const;
  48. int offset_width() const;
  49. int offset_height() const;
  50. GC::Ptr<Element> offset_parent() const;
  51. bool cannot_navigate() const;
  52. void click();
  53. [[nodiscard]] String access_key_label() const;
  54. bool fire_a_synthetic_pointer_event(FlyString const& type, DOM::Element& target, bool not_trusted);
  55. // https://html.spec.whatwg.org/multipage/forms.html#category-label
  56. virtual bool is_labelable() const { return false; }
  57. GC::Ptr<DOM::NodeList> labels();
  58. virtual Optional<ARIA::Role> default_role() const override;
  59. String get_an_elements_target() const;
  60. TokenizedFeature::NoOpener get_an_elements_noopener(StringView target) const;
  61. WebIDL::ExceptionOr<GC::Ref<ElementInternals>> attach_internals();
  62. WebIDL::ExceptionOr<void> set_popover(Optional<String> value);
  63. Optional<String> popover() const;
  64. protected:
  65. HTMLElement(DOM::Document&, DOM::QualifiedName);
  66. virtual void initialize(JS::Realm&) override;
  67. virtual void attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
  68. virtual WebIDL::ExceptionOr<void> cloned(DOM::Node&, bool) override;
  69. virtual void inserted() override;
  70. virtual void visit_edges(Cell::Visitor&) override;
  71. private:
  72. virtual bool is_html_element() const final { return true; }
  73. virtual void adjust_computed_style(CSS::StyleProperties&) override;
  74. // ^HTML::GlobalEventHandlers
  75. virtual GC::Ptr<DOM::EventTarget> global_event_handlers_to_event_target(FlyString const&) override { return *this; }
  76. virtual void did_receive_focus() override;
  77. virtual void did_lose_focus() override;
  78. [[nodiscard]] String get_the_text_steps();
  79. GC::Ref<DOM::DocumentFragment> rendered_text_fragment(StringView input);
  80. GC::Ptr<DOM::NodeList> m_labels;
  81. // https://html.spec.whatwg.org/multipage/custom-elements.html#attached-internals
  82. GC::Ptr<ElementInternals> m_attached_internals;
  83. // https://html.spec.whatwg.org/#attr-contenteditable
  84. ContentEditableState m_content_editable_state { ContentEditableState::Inherit };
  85. // https://html.spec.whatwg.org/multipage/interaction.html#click-in-progress-flag
  86. bool m_click_in_progress { false };
  87. };
  88. }
  89. namespace Web::DOM {
  90. template<>
  91. inline bool Node::fast_is<HTML::HTMLElement>() const { return is_html_element(); }
  92. }