Sfoglia il codice sorgente

LibWeb: Implement document.execCommand("defaultParagraphSeparator")

Jelle Raaijmakers 8 mesi fa
parent
commit
4a64557876

+ 7 - 0
Libraries/LibWeb/DOM/Document.h

@@ -749,6 +749,10 @@ public:
 
     GC::Ref<EditingHostManager> editing_host_manager() const { return *m_editing_host_manager; }
 
+    // // https://w3c.github.io/editing/docs/execCommand/#default-single-line-container-name
+    FlyString const& default_single_line_container_name() const { return m_default_single_line_container_name; }
+    void set_default_single_line_container_name(FlyString const& name) { m_default_single_line_container_name = name; }
+
 protected:
     virtual void initialize(JS::Realm&) override;
     virtual void visit_edges(Cell::Visitor&) override;
@@ -1039,6 +1043,9 @@ private:
     mutable OwnPtr<Unicode::Segmenter> m_word_segmenter;
 
     GC::Ref<EditingHostManager> m_editing_host_manager;
+
+    // https://w3c.github.io/editing/docs/execCommand/#default-single-line-container-name
+    FlyString m_default_single_line_container_name { HTML::TagNames::div };
 };
 
 template<>

+ 29 - 0
Libraries/LibWeb/Editing/Commands.cpp

@@ -19,6 +19,34 @@
 
 namespace Web::Editing {
 
+// https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command
+bool command_default_paragraph_separator_action(DOM::Document& document, String const& input_value)
+{
+    // Let value be converted to ASCII lowercase.
+    auto value = input_value.to_ascii_lowercase();
+
+    // If value is then equal to "p" or "div", set the context object's default single-line
+    // container name to value, then return true.
+    if (value == HTML::TagNames::p) {
+        document.set_default_single_line_container_name(HTML::TagNames::p);
+        return true;
+    }
+    if (value == HTML::TagNames::div) {
+        document.set_default_single_line_container_name(HTML::TagNames::div);
+        return true;
+    }
+
+    // Otherwise, return false.
+    return false;
+}
+
+// https://w3c.github.io/editing/docs/execCommand/#the-defaultparagraphseparator-command
+String command_default_paragraph_separator_value(DOM::Document const& document)
+{
+    // Return the context object's default single-line container name.
+    return document.default_single_line_container_name().to_string();
+}
+
 // https://w3c.github.io/editing/docs/execCommand/#the-delete-command
 bool command_delete_action(DOM::Document& document, String const&)
 {
@@ -326,6 +354,7 @@ bool command_delete_action(DOM::Document& document, String const&)
 
 static Array const commands {
     CommandDefinition { CommandNames::delete_, command_delete_action, {}, {}, {} },
+    CommandDefinition { CommandNames::defaultParagraphSeparator, command_default_paragraph_separator_action, {}, {}, command_default_paragraph_separator_value },
 };
 
 Optional<CommandDefinition const&> find_command_definition(FlyString const& command)

+ 2 - 0
Libraries/LibWeb/Editing/Commands.h

@@ -21,6 +21,8 @@ struct CommandDefinition {
 Optional<CommandDefinition const&> find_command_definition(FlyString const&);
 
 // Command implementations
+bool command_default_paragraph_separator_action(DOM::Document&, String const&);
+String command_default_paragraph_separator_value(DOM::Document const&);
 bool command_delete_action(DOM::Document&, String const&);
 
 }