Skip to content

Specifying whether textarea.value assignment creates a new TextNode or updates textNode.data #551

Closed

Description

@nateps

I discovered a bug in our bindings library with the root cause being a difference in how IE (including Edge) handles updates to <textarea> text content when setting textarea.value. While this does not appear to be specified in the HTML spec, Chrome, Safari, and Firefox all behave differently. Would be nice to have this specified so that libraries don't have to work around the inconsistency.

Test case:

var textarea = document.createElement('textarea');
var textNode = document.createTextNode('Hi');
textarea.appendChild(textNode);
console.assert(textarea.firstChild === textNode, 'Appended text node is first child of textarea.');
textarea.value = 'Yo';
console.assert(textarea.firstChild === textNode, 'After setting value, original text node remains first child of textarea.');

The above test passes in Chrome, Safari, and Firefox. In IE 11 Windows 7 Edge mode (and probably other IE versions), the TextNode previously a child of the textarea is replaced with a new TextNode instance. This new node has the data (Yo) and the previous text node's data is set to empty string (textNode.data === '').

In contrast, Chrome does not modify the children of the textarea when setting its value. The TextNode data is not updated at all (textNode.data === 'Hi'). Similarly, a user typing in a textarea does not update the TextNode data and only updates the textarea value.

As far as I can find, these behaviors are currently unspecified.

I would propose that a consistent relationship between editing the value of a textarea and its contained TextNode(s) be specified in 4.10.11 to eliminate these inconsistencies.

Activity

  1. foolip commented on Jan 26, 2016

    @foolip
    Member

    The textarea.value setter is defined in the spec as "it must set the element's raw value to the new value, set the element's dirty value flag to true, and should then move the text entry cursor position to the end of the text field, unselecting any selected text and resetting the selection direction to none."

    The raw value is an internal slot, like the value slot of input elements. This is how it related to the child text nodes, AFACIT:

    • "When the textarea element's textContent IDL attribute changes value, if the element's dirty value flag is false, then the element's raw value must be set to the value of the element's textContent IDL attribute." (spec quote)
    • "The reset algorithm for textarea elements is to set the element's raw value to the value of the element's textContent IDL attribute." (spec quote; the reset algorithm is mostly for form.reset(), but see below)
    • "When a textarea element is popped off the stack of open elements of an HTML parser or XML parser, then the user agent must invoke the element's reset algorithm." (so the raw value will be set during parsing, although this seems redundant with the textContent integration)

    So, per the spec, your test case should log true twice, because setting textarea.value shouldn't affect the DOM at all, only the internal raw value slot.

    There may be things left undefined by the spec, but the interaction with child text nodes looks fairly clear.

    Can you see if this behavior is covered in any of the tests in https://github.com/w3c/web-platform-tests/tree/master/html/semantics/forms/the-textarea-element/? If it's not, then writing those tests would be a good first step to getting this fixed across browsers.

  2. domenic commented on Jan 30, 2016

    @domenic
    Member

    Closing since it seems this has been answered. Happy to continue discussion though, or reopen if we got something wrong.

  3. Ms2ger commented on Feb 1, 2016

    @Ms2ger
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions