Changeset 293673 in webkit


Ignore:
Timestamp:
May 2, 2022 12:12:06 PM (3 months ago)
Author:
Ziran Sun
Message:

[selection] Set correct selection range for TEXTAREA when updating default value
https://bugs.webkit.org/show_bug.cgi?id=237525

Reviewed by Chris Dumez.

LayoutTests/imported/w3c:

  • web-platform-tests/html/semantics/forms/textfieldselection/selection-start-end-extra-expected.txt:

Source/WebCore:

Updating defaultValue should keep selectionStart/End. We need clamp them if the new value is shorter than the
selectionStart/End. This change is to be in line with [1] & [2].

[1] https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:dom-textarea-defaultvalue-2
[2] https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:concept-textarea/input-relevant-value

Part of this change is an import of Chromium CL at
https://github.com/chromium/chromium/commit/bb27a500d07f8b3c567e84857a40c3ce42fa454a

  • html/HTMLTextAreaElement.cpp:

(WebCore::HTMLTextAreaElement::childrenChanged):
(WebCore::HTMLTextAreaElement::setValueCommon):

  • html/HTMLTextFormControlElement.h:
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r293670 r293673  
     12022-05-02  Ziran Sun  <zsun@igalia.com>
     2
     3        [selection] Set correct selection range for TEXTAREA when updating default value
     4        https://bugs.webkit.org/show_bug.cgi?id=237525
     5
     6        Reviewed by  Chris Dumez.
     7
     8        * web-platform-tests/html/semantics/forms/textfieldselection/selection-start-end-extra-expected.txt:
     9
    1102022-05-02  Oriol Brufau  <obrufau@igalia.com>
    211
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/semantics/forms/textfieldselection/selection-start-end-extra-expected.txt

    r291396 r293673  
    66PASS Removing children from a textarea should NOT update selection{Start,End}
    77PASS Setting the same value (with different newlines) in a textarea should NOT update selection{Start,End}
    8 FAIL Removing child nodes in non-dirty textarea should make selection{Start,End} 0 assert_equals: selectionStart after node removal expected 0 but got 3
     8PASS Removing child nodes in non-dirty textarea should make selection{Start,End} 0
    99PASS Setting value to a shorter string than defaultValue should correct the cursor position
    1010PASS Shortening value by turning the input type into 'url' should correct selection{Start,End}
  • trunk/Source/WebCore/ChangeLog

    r293672 r293673  
     12022-05-02  Ziran Sun  <zsun@igalia.com>
     2
     3        [selection] Set correct selection range for TEXTAREA when updating default value
     4        https://bugs.webkit.org/show_bug.cgi?id=237525
     5
     6        Reviewed by  Chris Dumez.
     7
     8        Updating defaultValue should keep selectionStart/End. We need clamp them if the new value is shorter than the
     9        selectionStart/End. This change is to be in line with [1] & [2].
     10       
     11        [1] https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:dom-textarea-defaultvalue-2
     12        [2] https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:concept-textarea/input-relevant-value
     13
     14        Part of this change is an import of Chromium CL at
     15        https://github.com/chromium/chromium/commit/bb27a500d07f8b3c567e84857a40c3ce42fa454a
     16
     17        * html/HTMLTextAreaElement.cpp:
     18        (WebCore::HTMLTextAreaElement::childrenChanged):
     19        (WebCore::HTMLTextAreaElement::setValueCommon):
     20        * html/HTMLTextFormControlElement.h:
     21
    1222022-05-02  Fujii Hironori  <Hironori.Fujii@sony.com>
    223
  • trunk/Source/WebCore/html/HTMLTextAreaElement.cpp

    r293656 r293673  
    137137        setInnerTextValue(value());
    138138    else
    139         setNonDirtyValue(defaultValue(),  TextControlSetValueSelection::DoNotSet);
     139        setNonDirtyValue(defaultValue(), TextControlSetValueSelection::Clamp);
    140140}
    141141
     
    402402        return;
    403403
     404    bool shouldClamp = selection == TextControlSetValueSelection::Clamp;
     405    auto selectionStartValue = shouldClamp ? computeSelectionStart() : 0;
     406    auto selectionEndValue = shouldClamp ? computeSelectionEnd() : 0;
     407
    404408    m_value = normalizedValue;
    405409    setInnerTextValue(String { m_value });
     
    409413    setFormControlValueMatchesRenderer(true);
    410414
    411     if (document().focusedElement() == this) {
    412         unsigned endOfString = m_value.length();
     415    auto endOfString = m_value.length();
     416    if (document().focusedElement() == this)
    413417        setSelectionRange(endOfString, endOfString);
    414     else if (selection == TextControlSetValueSelection::SetSelectionToEnd) {
     418    else if (selection == TextControlSetValueSelection::SetSelectionToEnd) {
    415419        // We don't change text selection here but need to update caret to
    416420        // the end of the text value except for initialize.
    417         unsigned endOfString = m_value.length();
    418421        cacheSelection(endOfString, endOfString, SelectionHasNoDirection);
    419     }
     422    } else if (shouldClamp)
     423        cacheSelection(std::min(endOfString, selectionStartValue), std::min(endOfString, selectionEndValue), SelectionHasNoDirection);
    420424
    421425    setTextAsOfLastFormControlChangeEvent(normalizedValue);
  • trunk/Source/WebCore/html/HTMLTextFormControlElement.h

    r293059 r293673  
    4040enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
    4141enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
    42 enum TextControlSetValueSelection { SetSelectionToEnd, DoNotSet };
     42enum TextControlSetValueSelection { SetSelectionToEnd, Clamp, DoNotSet };
    4343
    4444class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
     
    124124    bool hasCachedSelection() const { return m_hasCachedSelection; }
    125125
     126    unsigned computeSelectionStart() const;
     127    unsigned computeSelectionEnd() const;
     128
    126129    virtual void subtreeHasChanged() = 0;
    127130
     
    140143    bool isTextFormControlElement() const final { return true; }
    141144
    142     unsigned computeSelectionStart() const;
    143     unsigned computeSelectionEnd() const;
    144145    TextFieldSelectionDirection computeSelectionDirection() const;
    145146
Note: See TracChangeset for help on using the changeset viewer.