From db5ed905784cbedfe7fa0c9aaf1ce1d20e5f9a24 Mon Sep 17 00:00:00 2001 From: Andrey Okonetchnikov Date: Wed, 6 Jul 2016 19:11:46 +0200 Subject: [PATCH] Do not render `name` attribute on INPUT if it is not supplied. Closes #7198. (#7199) --- .../dom/client/wrappers/ReactDOMInput.js | 8 +++-- .../wrappers/__tests__/ReactDOMInput-test.js | 33 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/renderers/dom/client/wrappers/ReactDOMInput.js b/src/renderers/dom/client/wrappers/ReactDOMInput.js index bd407343d564d..d8730c2a21c84 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMInput.js +++ b/src/renderers/dom/client/wrappers/ReactDOMInput.js @@ -237,10 +237,14 @@ var ReactDOMInput = { // Reference: https://bugs.chromium.org/p/chromium/issues/detail?id=608416 // We need to temporarily unset name to avoid disrupting radio button groups. var name = node.name; - node.name = undefined; + if (name !== '') { + node.name = ''; + } node.defaultChecked = !node.defaultChecked; node.defaultChecked = !node.defaultChecked; - node.name = name; + if (name !== '') { + node.name = name; + } }, }; diff --git a/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js b/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js index 863969f0a23d1..0e80605b2c791 100644 --- a/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js +++ b/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js @@ -111,6 +111,35 @@ describe('ReactDOMInput', function() { expect(div.firstChild.getAttribute('defaultValue')).toBe(null); }); + it('should render name attribute if it is supplied', function() { + var container = document.createElement('div'); + var node = ReactDOM.render(, container); + expect(node.name).toBe('name'); + expect(container.firstChild.getAttribute('name')).toBe('name'); + }); + + it('should render name attribute if it is supplied for SSR', function() { + var element = ; + var markup = ReactDOMServer.renderToString(element); + var div = document.createElement('div'); + div.innerHTML = markup; + expect(div.firstChild.getAttribute('name')).toBe('name'); + }); + + it('should not render name attribute if it is not supplied', function() { + var container = document.createElement('div'); + ReactDOM.render(, container); + expect(container.firstChild.getAttribute('name')).toBe(null); + }); + + it('should not render name attribute if it is not supplied for SSR', function() { + var element = ; + var markup = ReactDOMServer.renderToString(element); + var div = document.createElement('div'); + div.innerHTML = markup; + expect(div.firstChild.getAttribute('name')).toBe(null); + }); + it('should display "foobar" for `defaultValue` of `objToString`', function() { var objToString = { toString: function() { @@ -681,7 +710,7 @@ describe('ReactDOMInput', function() { value="value" defaultChecked={true} checked={false} - onChange={() => null} + onChange={() => null} />, container); expect(console.error.calls.count()).toBe(0); }); @@ -727,10 +756,8 @@ describe('ReactDOMInput', function() { 'set type', 'set value', 'set value', - 'set name', 'set checked', 'set checked', - 'set name', ]); });