Skip to content

Form input name='nodeName' breaks onSubmit event handling #6284

Open

Description

@insonifi

It happened that I stumbled on following edge case. If you add name='nodeName' attribute to form's input, at some point of React event handling (ChangeEventPlugin.js: shouldUseChangeEvent()) it will call: elem.nodeName && elem.nodeName.toLowerCase(), but coincidentally nodeName property refers to input and invocation fails.

Here's a jsFiddle example

Activity

  1. zpao commented on Mar 21, 2016

    @zpao
    Contributor

    Ah, nice find. The issue is that elem in that code is actually window (or form) at that point and thanks to the magic of named inputs as properties on window, this is screwing up. I wonder if there are other cases like this…

  2. jimfb commented on Mar 21, 2016

    @jimfb
    Contributor

    @zpao That was my first thought too, in which case we could special-case window, but I don't think that's quite right.

    ReactDOM.render(
      <form><input name='nodeName' /><input type='submit' /></form>,
      document.getElementById('container')
    );
    console.log(window.nodeName);
    

    I think the issue occurs as we traverse the DOM hierarchy for the purposes of synthetically bubbling the event. We are hitting the form node, which is the one that has magic expando properties. It's a bit more complicated to identify form nodes when tagName and nodeName can both be overwritten by named inputs.

    In shouldUseChangeEvent, we are searching for input and select nodes. Cleanest solution I can think of at the moment is to return false for any node where typeof nodeName !== 'string'. The sucky thing is that this is not a general solution and there are likely other places in the codebase where we're going to need to play whack-a-bug.

  3. zpao commented on Mar 21, 2016

    @zpao
    Contributor

    Yea, I updated my comment and mentioned the form. Otherwise window properties are due to ids, not name, whereas form properties are there for names. Maybe something with the iframes + debugger screwed me up.

  4. zpao commented on Mar 21, 2016

    @zpao
    Contributor

    So what we could do is instead of ever checking nodeName is do instanceof checks. So this code would become

    function shouldUseChangeEvent(elem) {
      return (
        elem instanceof HTMLSelectElement ||
        (elem instanceof HTMLInputElement && elem.type === 'file')
      );
    }

    The risk is that we're in an environment that doesn't have the globals we expect. Maybe we read them from window instead & wrap potential problems in canUseDOM checks. We shouldn't hit this path at all in node anyway so I don't think it's a problem in this case, maybe in others.

  5. benjycui commented on Sep 9, 2016

    @benjycui
    Contributor

    The following code will cause the same error:

    <input id="nodeName" />

    It seems that jQuery had met the same issue and fixed, see: http://stackoverflow.com/questions/17329025/is-id-nodename-reserved-in-html5

  6. shaozj commented on Jul 5, 2017

    @shaozj

    I write this code:

    var form = document.createElement('form');
    var input = document.createElement('input');
    input.setAttribute('id', 'nodeName');
    form.appendChild(input);
    console.log(form);

    run the code on chrome,
    output: TypeError: (var).nodeName.toLowerCase is not a function
    at :5:9

    I think this is a browser bug.

  7. nanjixiong218 commented on Aug 22, 2017

    @nanjixiong218
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
        <div>
            <form id="container">
                <div>
                    <input type="text" name="nodeName"/>
                </div>
    
            </form>
        </div>
    </body>
    </html>
    

    image

    it maybe a bug of chrome. the input has the id or name of 'nodeName' will override the nodeName of parent form.

    but it won't affect the render of page。

    but in react, if the outermost layer element is the form, react will check the nodeName ,so will throw error:

    t.node.nodeName.toLowerCase is not a function
    

    image

  8. fritx commented on May 3, 2018

    @fritx

    +1, same problem here.

    cc @dvajs, @ant-design, @sorrycc

    related: ant-design/ant-design#2950, ant-design/ant-design#2103

    # error
    react-dom.development.js:2586 Uncaught TypeError: elem.nodeName.toLowerCase is not a function
        at shouldUseChangeEvent (react-dom.development.js:2586)
        at Object.extractEvents (react-dom.development.js:2770)
        at extractEvents (react-dom.development.js:1072)
    
    const formFields = [
      { input: 'text', title: '节点名称', key: 'nodeName', required: true },
      // ...
    ]
    
    <Form.Item key={item.key} label={item.title}>
      {getFieldDecorator(item.key, {
        initialValue: (defaults || {})[item.key],
        rules: [{
          required: item.required,
          message: '请填写必填项',
        }],
      })(
        <Input />
      )}
    </Form.Item>

    Workaround: avoid naming with the keyword nodeName

    const formFields = [
      // Form input name='nodeName' breaks onSubmit event handling
      // https://github.com/facebook/react/issues/6284
      { input: 'text', title: '节点名称', key: 'nodeName_fix', required: true },
      // ...
    ]
    
    function toEditModel(obj) {
      const ret = _.cloneDeep(obj);
      // ...
      ['nodeName'].forEach((k) => {
        const v = ret[k];
        ret[`${k}_fix`] = v;
      });
      return ret;
    }
    function fromEditModel(obj) {
      const ret = _.cloneDeep(obj);
      // ...
      ['nodeName'].forEach((k) => {
        const v = ret[`${k}_fix`];
        ret[k] = v;
      });
      return ret;
    }
  9. dustinmoorenet commented on Apr 16, 2020

    @dustinmoorenet

    I have found a similar issue with using style as an id in a form element while trying to set inline styles on the form element.

    function App() {
      return (
        <>
          <div style={{backgroundColor: 'red'}}>
            <input id="style" defaultValue="should have a red background"/>
          </div>
          <form style={{backgroundColor: 'green'}}>
            <input id="style" defaultValue="my background wont be green"/>
          </form>
          <form style={{backgroundColor: 'blue'}}>
            <input id="stylez" defaultValue="should have a blue background"/>
          </form>
        </>
      );
    }

    The middle form can't set it's inline styles because a child element occupies the form property style

    image

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions