Skip to content

Commit

Permalink
Do not render name attribute on INPUT if it is not supplied. Closes #…
Browse files Browse the repository at this point in the history
…7198. (#7199)

(cherry picked from commit 0d892c0)
  • Loading branch information
okonet authored and zpao committed Jul 8, 2016
1 parent 219e838 commit 9db68fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/renderers/dom/client/wrappers/ReactDOMInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
};

Expand Down
33 changes: 30 additions & 3 deletions src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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(<input type="text" name="name" />, 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 = <input type="text" name="name" />;
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(<input type="text" />, container);
expect(container.firstChild.getAttribute('name')).toBe(null);
});

it('should not render name attribute if it is not supplied for SSR', function() {
var element = <input type="text" />;
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() {
Expand Down Expand Up @@ -672,7 +701,7 @@ describe('ReactDOMInput', function() {
value="value"
defaultChecked={true}
checked={false}
onChange={() => null}
onChange={() => null}
/>, container);
expect(console.error.calls.count()).toBe(0);
});
Expand Down Expand Up @@ -718,10 +747,8 @@ describe('ReactDOMInput', function() {
'set type',
'set value',
'set value',
'set name',
'set checked',
'set checked',
'set name',
]);
});

Expand Down

0 comments on commit 9db68fc

Please sign in to comment.