From 6a85677f70a82ea4b8921474c1e4ff71300982f4 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Mon, 2 Dec 2024 11:51:55 +0100 Subject: [PATCH] LibWeb: Expose HTMLElement's content editable state --- Libraries/LibWeb/Editing/ExecCommand.cpp | 2 +- Libraries/LibWeb/Editing/Internal/Algorithms.cpp | 4 +++- Libraries/LibWeb/HTML/HTMLElement.h | 15 +++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Libraries/LibWeb/Editing/ExecCommand.cpp b/Libraries/LibWeb/Editing/ExecCommand.cpp index 1efa45e4888..ae709735709 100644 --- a/Libraries/LibWeb/Editing/ExecCommand.cpp +++ b/Libraries/LibWeb/Editing/ExecCommand.cpp @@ -137,7 +137,7 @@ bool Document::query_command_enabled(FlyString const& command) // NOTE: Commands can define additional conditions for being enabled, and currently the only condition mentioned in // the spec is that certain commands must not be enabled if the editing host is in the plaintext-only state. if (is(start_node_editing_host.ptr()) - && static_cast(*start_node_editing_host).content_editable() == "plaintext-only"sv + && static_cast(*start_node_editing_host).content_editable_state() == HTML::ContentEditableState::PlaintextOnly && command.is_one_of( Editing::CommandNames::backColor, Editing::CommandNames::bold, diff --git a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp index f85a3a74647..efd3106f89b 100644 --- a/Libraries/LibWeb/Editing/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/Editing/Internal/Algorithms.cpp @@ -819,7 +819,9 @@ bool is_editing_host(GC::Ref node) if (!is(*node)) return false; auto const& html_element = static_cast(*node); - return html_element.content_editable().is_one_of("true"sv, "plaintext-only"sv) || node->document().design_mode_enabled_state(); + return html_element.content_editable_state() == HTML::ContentEditableState::True + || html_element.content_editable_state() == HTML::ContentEditableState::PlaintextOnly + || node->document().design_mode_enabled_state(); } // https://w3c.github.io/editing/docs/execCommand/#element-with-inline-contents diff --git a/Libraries/LibWeb/HTML/HTMLElement.h b/Libraries/LibWeb/HTML/HTMLElement.h index 44abde261ed..9f106f54952 100644 --- a/Libraries/LibWeb/HTML/HTMLElement.h +++ b/Libraries/LibWeb/HTML/HTMLElement.h @@ -20,6 +20,14 @@ namespace Web::HTML { __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(rtl) \ __ENUMERATE_HTML_ELEMENT_DIR_ATTRIBUTE(auto) +// https://html.spec.whatwg.org/#attr-contenteditable +enum class ContentEditableState { + True, + False, + PlaintextOnly, + Inherit, +}; + class HTMLElement : public DOM::Element , public HTML::GlobalEventHandlers @@ -39,6 +47,7 @@ public: virtual bool is_focusable() const override; bool is_content_editable() const; StringView content_editable() const; + ContentEditableState content_editable_state() const { return m_content_editable_state; } WebIDL::ExceptionOr set_content_editable(StringView); String inner_text(); @@ -106,12 +115,6 @@ private: GC::Ptr m_attached_internals; // https://html.spec.whatwg.org/#attr-contenteditable - enum class ContentEditableState { - True, - False, - PlaintextOnly, - Inherit, - }; ContentEditableState m_content_editable_state { ContentEditableState::Inherit }; // https://html.spec.whatwg.org/multipage/interaction.html#click-in-progress-flag