Sfoglia il codice sorgente

LibWeb: Update the <textarea> shadow DOM when the value attribute is set

Otherwise, setting the value attribute after the element is added to the
DOM is not visibile. The logic here was stolen from the <input> element.
Timothy Flynn 1 anno fa
parent
commit
abc1be5b9e

+ 11 - 0
Tests/LibWeb/Layout/expected/textarea-content.txt

@@ -5,6 +5,7 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
       frag 1 from TextNode start: 0, length: 1, rect: [254,8 10x22] baseline: 17
           " "
       frag 2 from BlockContainer start: 0, length: 0, rect: [267,11 240x44] baseline: 17
+      frag 3 from BlockContainer start: 0, length: 0, rect: [513,11 240x44] baseline: 17
       TextNode <#text>
       BlockContainer <textarea> at (11,11) content-size 240x44 inline-block [BFC] children: not-inline
         BlockContainer <div> at (11,11) content-size 240x22 children: not-inline
@@ -19,6 +20,12 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
             frag 0 from TextNode start: 0, length: 19, rect: [267,11 177.6875x22] baseline: 17
                 "Well hello friends!"
             TextNode <#text>
+      BlockContainer <textarea> at (513,11) content-size 240x44 inline-block [BFC] children: not-inline
+        BlockContainer <div> at (513,11) content-size 240x22 children: not-inline
+          BlockContainer <div> at (513,11) content-size 240x22 children: inline
+            frag 0 from TextNode start: 0, length: 16, rect: [513,11 154.0625x22] baseline: 17
+                "Tja hej vänner!"
+            TextNode <#text>
       TextNode <#text>
 
 ViewportPaintable (Viewport<#document>) [0,0 800x600]
@@ -33,3 +40,7 @@ ViewportPaintable (Viewport<#document>) [0,0 800x600]
         PaintableWithLines (BlockContainer<DIV>) [267,11 240x22]
           PaintableWithLines (BlockContainer<DIV>) [267,11 240x22]
             TextPaintable (TextNode<#text>)
+      PaintableWithLines (BlockContainer<TEXTAREA>) [510,8 246x50]
+        PaintableWithLines (BlockContainer<DIV>) [513,11 240x22]
+          PaintableWithLines (BlockContainer<DIV>) [513,11 240x22]
+            TextPaintable (TextNode<#text>)

+ 7 - 3
Tests/LibWeb/Layout/input/textarea-content.html

@@ -5,7 +5,11 @@
 </style></head><body>
 <textarea>Bonjour mon amis!</textarea>
 <script>
-    const textarea = document.createElement("textarea");
-    textarea.innerText = "Well hello friends!";
-    document.body.appendChild(textarea);
+    const textarea1 = document.createElement("textarea");
+    textarea1.innerText = "Well hello friends!";
+    document.body.appendChild(textarea1);
+
+    const textarea2 = document.createElement("textarea");
+    document.body.appendChild(textarea2);
+    textarea2.value = "Tja hej vänner!";
 </script>

+ 15 - 3
Userland/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp

@@ -133,7 +133,10 @@ String HTMLTextAreaElement::value() const
 // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
 void HTMLTextAreaElement::set_value(String const& value)
 {
-    // FIXME: 1. Let oldAPIValue be this element's API value.
+    auto& realm = this->realm();
+
+    // 1. Let oldAPIValue be this element's API value.
+    auto old_api_value = api_value();
 
     // 2. Set this element's raw value to the new value.
     set_raw_value(value);
@@ -141,8 +144,17 @@ void HTMLTextAreaElement::set_value(String const& value)
     // 3. Set this element's dirty value flag to true.
     m_dirty_value = true;
 
-    // FIXME: 4. If the new API value is different from oldAPIValue, then move the text entry cursor position to the end of the text control, unselecting any selected text and resetting the selection direction to "none".
-    update_placeholder_visibility();
+    // 4. If the new API value is different from oldAPIValue, then move the text entry cursor position to the end of
+    //    the text control, unselecting any selected text and resetting the selection direction to "none".
+    if (api_value() != old_api_value) {
+        if (m_text_node) {
+            m_text_node->set_data(m_raw_value);
+            update_placeholder_visibility();
+
+            if (auto* browsing_context = document().browsing_context())
+                browsing_context->set_cursor_position(DOM::Position::create(realm, *m_text_node, m_text_node->data().bytes().size()));
+        }
+    }
 }
 
 void HTMLTextAreaElement::set_raw_value(String value)