diff --git a/grunt/tasks/npm-react-addons.js b/grunt/tasks/npm-react-addons.js index 0f3d088d9c..44d327fba3 100644 --- a/grunt/tasks/npm-react-addons.js +++ b/grunt/tasks/npm-react-addons.js @@ -44,6 +44,7 @@ var addons = { shallowCompare: { module: 'shallowCompare', name: 'shallow-compare', + docs: 'shallow-compare', }, updates: { module: 'update', diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8c2f98b7c8..1812c1e450 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "react-build", - "version": "15.3.1", + "version": "15.3.2-rc.1", "dependencies": { "art": { "version": "0.10.1", diff --git a/package.json b/package.json index dc872e2208..a7b1252c39 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-build", "private": true, - "version": "15.3.1", + "version": "15.3.2-rc.1", "devDependencies": { "art": "^0.10.1", "async": "^1.5.0", diff --git a/packages/react-addons/package.json b/packages/react-addons/package.json index 017f4544e0..e61bdd86ec 100644 --- a/packages/react-addons/package.json +++ b/packages/react-addons/package.json @@ -1,6 +1,6 @@ { "name": "react-addons-template", - "version": "15.3.1", + "version": "15.3.2-rc.1", "main": "index.js", "repository": "facebook/react", "keywords": [ @@ -10,7 +10,7 @@ "license": "BSD-3-Clause", "dependencies": {}, "peerDependencies": { - "react": "^15.3.1" + "react": "^15.3.2-rc.1" }, "files": [ "LICENSE", diff --git a/packages/react-dom/package.json b/packages/react-dom/package.json index 215ee56fcf..82da9cfd78 100644 --- a/packages/react-dom/package.json +++ b/packages/react-dom/package.json @@ -1,6 +1,6 @@ { "name": "react-dom", - "version": "15.3.1", + "version": "15.3.2-rc.1", "description": "React package for working with the DOM.", "main": "index.js", "repository": "facebook/react", @@ -14,7 +14,7 @@ "homepage": "https://facebook.github.io/react/", "dependencies": {}, "peerDependencies": { - "react": "^15.3.1" + "react": "^15.3.2-rc.1" }, "files": [ "LICENSE", diff --git a/packages/react-native-renderer/package.json b/packages/react-native-renderer/package.json index 750202b4f8..252ff465dd 100644 --- a/packages/react-native-renderer/package.json +++ b/packages/react-native-renderer/package.json @@ -1,6 +1,6 @@ { "name": "react-native-renderer", - "version": "15.3.1", + "version": "15.3.2-rc.1", "description": "React package for use inside react-native.", "main": "index.js", "repository": "facebook/react", @@ -14,7 +14,7 @@ }, "homepage": "https://facebook.github.io/react-native/", "dependencies": { - "react": "^15.3.1" + "react": "^15.3.2-rc.1" }, "files": [ "LICENSE", diff --git a/packages/react-test-renderer/package.json b/packages/react-test-renderer/package.json index 317cba02df..407f99aaf4 100644 --- a/packages/react-test-renderer/package.json +++ b/packages/react-test-renderer/package.json @@ -1,6 +1,6 @@ { "name": "react-test-renderer", - "version": "15.3.1", + "version": "15.3.2-rc.1", "description": "React package for snapshot testing.", "main": "index.js", "repository": "facebook/react", @@ -15,7 +15,7 @@ }, "homepage": "https://facebook.github.io/react/", "peerDependencies": { - "react": "^15.3.1" + "react": "^15.3.2-rc.1" }, "files": [ "LICENSE", diff --git a/packages/react/package.json b/packages/react/package.json index 92386412e2..13343c363e 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -1,7 +1,7 @@ { "name": "react", "description": "React is a JavaScript library for building user interfaces.", - "version": "15.3.1", + "version": "15.3.2-rc.1", "keywords": [ "react" ], diff --git a/src/ReactVersion.js b/src/ReactVersion.js index 62f35ea6ac..66cfffd74a 100644 --- a/src/ReactVersion.js +++ b/src/ReactVersion.js @@ -11,4 +11,4 @@ 'use strict'; -module.exports = '15.3.1'; +module.exports = '15.3.2-rc.1'; diff --git a/src/isomorphic/classic/element/ReactElement.js b/src/isomorphic/classic/element/ReactElement.js index 3a9d11d84c..64a0e13e14 100644 --- a/src/isomorphic/classic/element/ReactElement.js +++ b/src/isomorphic/classic/element/ReactElement.js @@ -204,16 +204,6 @@ ReactElement.createElement = function(type, config, children) { var source = null; if (config != null) { - if (__DEV__) { - warning( - /* eslint-disable no-proto */ - config.__proto__ == null || config.__proto__ === Object.prototype, - /* eslint-enable no-proto */ - 'React.createElement(...): Expected props argument to be a plain object. ' + - 'Properties defined in its prototype chain will be ignored.' - ); - } - if (hasValidRef(config)) { ref = config.ref; } @@ -334,16 +324,6 @@ ReactElement.cloneElement = function(element, config, children) { var owner = element._owner; if (config != null) { - if (__DEV__) { - warning( - /* eslint-disable no-proto */ - config.__proto__ == null || config.__proto__ === Object.prototype, - /* eslint-enable no-proto */ - 'React.cloneElement(...): Expected props argument to be a plain object. ' + - 'Properties defined in its prototype chain will be ignored.' - ); - } - if (hasValidRef(config)) { // Silently steal the ref from the parent. ref = config.ref; diff --git a/src/isomorphic/classic/element/__tests__/ReactElement-test.js b/src/isomorphic/classic/element/__tests__/ReactElement-test.js index 353989ad6b..7ba957d157 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElement-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElement-test.js @@ -188,18 +188,6 @@ describe('ReactElement', function() { expect(element.props.foo).toBe(1); }); - it('warns if the config object inherits from any type other than Object', function() { - spyOn(console, 'error'); - React.createElement('div', {foo: 1}); - expect(console.error).not.toHaveBeenCalled(); - React.createElement('div', Object.create({foo: 1})); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( - 'React.createElement(...): Expected props argument to be a plain object. ' + - 'Properties defined in its prototype chain will be ignored.' - ); - }); - it('extracts key and ref from the config', function() { var element = React.createFactory(ComponentClass)({ key: '12', diff --git a/src/isomorphic/classic/element/__tests__/ReactElementClone-test.js b/src/isomorphic/classic/element/__tests__/ReactElementClone-test.js index df62052aee..af5417a3cf 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElementClone-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElementClone-test.js @@ -75,18 +75,6 @@ describe('ReactElementClone', function() { expect(ReactDOM.findDOMNode(component).childNodes[0].className).toBe('xyz'); }); - it('should warn if the config object inherits from any type other than Object', function() { - spyOn(console, 'error'); - React.cloneElement('div', {foo: 1}); - expect(console.error).not.toHaveBeenCalled(); - React.cloneElement('div', Object.create({foo: 1})); - expect(console.error.calls.count()).toBe(1); - expect(console.error.calls.argsFor(0)[0]).toContain( - 'React.cloneElement(...): Expected props argument to be a plain object. ' + - 'Properties defined in its prototype chain will be ignored.' - ); - }); - it('does not fail if config has no prototype', function() { var config = Object.create(null, {foo: {value: 1, enumerable: true}}); React.cloneElement(
, config); diff --git a/src/renderers/dom/__tests__/ReactDOMProduction-test.js b/src/renderers/dom/__tests__/ReactDOMProduction-test.js index a50e7eedc0..5b1d4ebf94 100644 --- a/src/renderers/dom/__tests__/ReactDOMProduction-test.js +++ b/src/renderers/dom/__tests__/ReactDOMProduction-test.js @@ -86,6 +86,94 @@ describe('ReactDOMProduction', function() { expect(container.childNodes.length).toBe(0); }); + it('should call lifecycle methods', function() { + var log = []; + class Component extends React.Component { + state = {y: 1}; + shouldComponentUpdate(nextProps, nextState) { + log.push(['shouldComponentUpdate', nextProps, nextState]); + return nextProps.x !== this.props.x || nextState.y !== this.state.y; + } + componentWillMount() { + log.push(['componentWillMount']); + } + componentDidMount() { + log.push(['componentDidMount']); + } + componentWillReceiveProps(nextProps) { + log.push(['componentWillReceiveProps', nextProps]); + } + componentWillUpdate(nextProps, nextState) { + log.push(['componentWillUpdate', nextProps, nextState]); + } + componentDidUpdate(prevProps, prevState) { + log.push(['componentDidUpdate', prevProps, prevState]); + } + componentWillUnmount() { + log.push(['componentWillUnmount']); + } + render() { + log.push(['render']); + return null; + } + } + + var container = document.createElement('div'); + var inst = ReactDOM.render( + , + container + ); + expect(log).toEqual([ + ['componentWillMount'], + ['render'], + ['componentDidMount'], + ]); + log = []; + + inst.setState({y: 2}); + expect(log).toEqual([ + ['shouldComponentUpdate', {x: 1}, {y: 2}], + ['componentWillUpdate', {x: 1}, {y: 2}], + ['render'], + ['componentDidUpdate', {x: 1}, {y: 1}], + ]); + log = []; + + inst.setState({y: 2}); + expect(log).toEqual([ + ['shouldComponentUpdate', {x: 1}, {y: 2}], + ]); + log = []; + + ReactDOM.render( + , + container + ); + expect(log).toEqual([ + ['componentWillReceiveProps', {x: 2}], + ['shouldComponentUpdate', {x: 2}, {y: 2}], + ['componentWillUpdate', {x: 2}, {y: 2}], + ['render'], + ['componentDidUpdate', {x: 1}, {y: 2}], + ]); + log = []; + + ReactDOM.render( + , + container + ); + expect(log).toEqual([ + ['componentWillReceiveProps', {x: 2}], + ['shouldComponentUpdate', {x: 2}, {y: 2}], + ]); + log = []; + + ReactDOM.unmountComponentAtNode(container); + expect(log).toEqual([ + ['componentWillUnmount'], + ]); + }); + it('should throw with an error code in production', function() { expect(function() { class Component extends React.Component { diff --git a/src/renderers/dom/client/ReactBrowserEventEmitter.js b/src/renderers/dom/client/ReactBrowserEventEmitter.js index dd7334f484..8f6bd8d8e6 100644 --- a/src/renderers/dom/client/ReactBrowserEventEmitter.js +++ b/src/renderers/dom/client/ReactBrowserEventEmitter.js @@ -346,6 +346,19 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, { ); }, + /** + * Protect against document.createEvent() returning null + * Some popup blocker extensions appear to do this: + * https://github.com/facebook/react/issues/6887 + */ + supportsEventPageXY: function() { + if (!document.createEvent) { + return false; + } + var ev = document.createEvent('MouseEvent'); + return ev != null && 'pageX' in ev; + }, + /** * Listens to window scroll and resize events. We cache scroll values so that * application code can access them without triggering reflows. @@ -359,8 +372,7 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, { */ ensureScrollValueMonitoring: function() { if (hasEventPageXY === undefined) { - hasEventPageXY = - document.createEvent && 'pageX' in document.createEvent('MouseEvent'); + hasEventPageXY = ReactBrowserEventEmitter.supportsEventPageXY(); } if (!hasEventPageXY && !isMonitoringScrollValue) { var refresh = ViewportMetrics.refreshScrollValues; diff --git a/src/renderers/dom/client/__tests__/ReactBrowserEventEmitter-test.js b/src/renderers/dom/client/__tests__/ReactBrowserEventEmitter-test.js index 009bc5d223..a0f2cf123d 100644 --- a/src/renderers/dom/client/__tests__/ReactBrowserEventEmitter-test.js +++ b/src/renderers/dom/client/__tests__/ReactBrowserEventEmitter-test.js @@ -454,4 +454,20 @@ describe('ReactBrowserEventEmitter', function() { expect(idCallOrder[2]).toBe(getInternal(GRANDPARENT)); }); + it('should not crash ensureScrollValueMonitoring when createEvent returns null', function() { + var originalCreateEvent = document.createEvent; + document.createEvent = function() { + return null; + }; + spyOn(document, 'createEvent'); + + try { + var hasEventPageXY = ReactBrowserEventEmitter.supportsEventPageXY(); + expect(document.createEvent.calls.count()).toBe(1); + expect(hasEventPageXY).toBe(false); + } finally { + document.createEvent = originalCreateEvent; + } + }); + }); diff --git a/src/renderers/dom/client/eventPlugins/BeforeInputEventPlugin.js b/src/renderers/dom/client/eventPlugins/BeforeInputEventPlugin.js index 794e13661d..d5cedd6f62 100644 --- a/src/renderers/dom/client/eventPlugins/BeforeInputEventPlugin.js +++ b/src/renderers/dom/client/eventPlugins/BeforeInputEventPlugin.js @@ -348,11 +348,12 @@ function getNativeBeforeInputChars(topLevelType, nativeEvent) { function getFallbackBeforeInputChars(topLevelType, nativeEvent) { // If we are currently composing (IME) and using a fallback to do so, // try to extract the composed characters from the fallback object. + // If composition event is available, we extract a string only at + // compositionevent, otherwise extract it at fallback events. if (currentComposition) { - if ( - topLevelType === topLevelTypes.topCompositionEnd || - isFallbackCompositionEnd(topLevelType, nativeEvent) - ) { + if (topLevelType === topLevelTypes.topCompositionEnd + || (!canUseCompositionEvent + && isFallbackCompositionEnd(topLevelType, nativeEvent))) { var chars = currentComposition.getData(); FallbackCompositionState.release(currentComposition); currentComposition = null; diff --git a/src/renderers/dom/client/eventPlugins/ChangeEventPlugin.js b/src/renderers/dom/client/eventPlugins/ChangeEventPlugin.js index 1a6855d286..f9977e13fb 100644 --- a/src/renderers/dom/client/eventPlugins/ChangeEventPlugin.js +++ b/src/renderers/dom/client/eventPlugins/ChangeEventPlugin.js @@ -68,7 +68,7 @@ var doesChangeEventBubble = false; if (ExecutionEnvironment.canUseDOM) { // See `handleChange` comment below doesChangeEventBubble = isEventSupported('change') && ( - !('documentMode' in document) || document.documentMode > 8 + !document.documentMode || document.documentMode > 8 ); } @@ -149,7 +149,7 @@ if (ExecutionEnvironment.canUseDOM) { // IE10+ fire input events to often, such when a placeholder // changes or when an input with a placeholder is focused. isInputEventSupported = isEventSupported('input') && ( - !('documentMode' in document) || document.documentMode > 11 + !document.documentMode || document.documentMode > 11 ); } diff --git a/src/renderers/dom/client/eventPlugins/SelectEventPlugin.js b/src/renderers/dom/client/eventPlugins/SelectEventPlugin.js index 9aa84b2130..8bbafec052 100644 --- a/src/renderers/dom/client/eventPlugins/SelectEventPlugin.js +++ b/src/renderers/dom/client/eventPlugins/SelectEventPlugin.js @@ -42,6 +42,7 @@ var eventTypes = { topLevelTypes.topContextMenu, topLevelTypes.topFocus, topLevelTypes.topKeyDown, + topLevelTypes.topKeyUp, topLevelTypes.topMouseDown, topLevelTypes.topMouseUp, topLevelTypes.topSelectionChange, diff --git a/src/renderers/dom/client/eventPlugins/__tests__/BeforeInputEventPlugin-test.js b/src/renderers/dom/client/eventPlugins/__tests__/BeforeInputEventPlugin-test.js new file mode 100644 index 0000000000..d39527f5c0 --- /dev/null +++ b/src/renderers/dom/client/eventPlugins/__tests__/BeforeInputEventPlugin-test.js @@ -0,0 +1,209 @@ +/** + * Copyright 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails react-core + */ + +'use strict'; + +var React = require('React'); +var ReactTestUtils = require('ReactTestUtils'); + +var EventMapping = { + compositionstart : 'topCompositionStart', + compositionend : 'topCompositionEnd', + keyup : 'topKeyUp', + keydown : 'topKeyDown', + textInput : 'topTextInput', + textinput : null, // Not defined now +}; + +describe('BeforeInputEventPlugin', function() { + var ModuleCache; + + function simulateIE11() { + document.documentMode = 11; + window.CompositionEvent = {}; + delete window.TextEvent; + } + + function simulateWebkit() { + delete document.documentMode; + window.CompositionEvent = {}; + window.TextEvent = {}; + } + + function initialize(simulator) { + // Need to delete cached modules before executing simulator + jest.resetModuleRegistry(); + + // Initialize variables in the scope of BeforeInputEventPlugin + simulator(); + + // Modules which have dependency on BeforeInputEventPlugin are stored + // in ModuleCache so that we can use these modules ouside test functions. + this.ReactDOM = require('ReactDOM'); + this.ReactDOMComponentTree = require('ReactDOMComponentTree'); + this.SyntheticCompositionEvent = require('SyntheticCompositionEvent'); + this.SyntheticInputEvent = require('SyntheticInputEvent'); + this.BeforeInputEventPlugin = require('BeforeInputEventPlugin'); + } + + function extract(node, eventType, optionalData) { + var evt = document.createEvent('HTMLEvents'); + evt.initEvent(eventType, true, true); + evt = Object.assign(evt, optionalData); + return ModuleCache.BeforeInputEventPlugin.extractEvents( + EventMapping[eventType], + ModuleCache.ReactDOMComponentTree.getInstanceFromNode(node), + evt, + node + ); + } + + function setElementText(node) { + return (args) => node.innerHTML = args; + } + + function accumulateEvents(node, events) { + // We don't use accumulateInto module to apply partial application. + return function() { + var newArgs = [node].concat(Array.prototype.slice.call(arguments)); + var newEvents = extract.apply(this, newArgs); + Array.prototype.push.apply(events, newEvents); + }; + } + + function EventMismatchError(idx, message) { + this.name = 'EventMismatchError'; + this.message = '[' + idx + '] ' + message; + } + EventMismatchError.prototype = Object.create(Error.prototype); + + function verifyEvents(actualEvents, expectedEvents) { + expect(actualEvents.length).toBe(expectedEvents.length); + expectedEvents.forEach(function(expected, idx) { + var actual = actualEvents[idx]; + expect(function() { + if (actual === null && expected.type === null) { + // Both are null. Expected. + } else if (actual === null) { + throw new EventMismatchError(idx, 'Expected not to be null'); + } else if (expected.type === null + || !(actual instanceof expected.type)) { + throw new EventMismatchError(idx, 'Unexpected type: ' + actual); + } else { + // Type match. + Object.keys(expected.data).forEach(function(expectedKey) { + if (!(expectedKey in actual)) { + throw new EventMismatchError(idx, 'KeyNotFound: ' + expectedKey); + } else if (actual[expectedKey] !== expected.data[expectedKey]) { + throw new EventMismatchError(idx, + 'ValueMismatch: ' + actual[expectedKey]); + } + }); + } + }).not.toThrow(); + }); + } + + // IE fires an event named `textinput` with all lowercase characters, + // instead of a standard name `textInput`. As of now, React does not have + // a corresponding topEvent to IE's textinput, but both events are added to + // this scenario data for future use. + var Scenario_Composition = [ + {run: accumulateEvents, arg: ['compositionstart', {data: ''}]}, + {run: accumulateEvents, arg: ['textInput', {data: 'A'}]}, + {run: accumulateEvents, arg: ['textinput', {data: 'A'}]}, + {run: accumulateEvents, arg: ['keyup', {keyCode: 65}]}, + {run: setElementText, arg: ['ABC']}, + {run: accumulateEvents, arg: ['textInput', {data: 'abc'}]}, + {run: accumulateEvents, arg: ['textinput', {data: 'abc'}]}, + {run: accumulateEvents, arg: ['keyup', {keyCode: 32}]}, + {run: setElementText, arg: ['XYZ']}, + {run: accumulateEvents, arg: ['textInput', {data: 'xyz'}]}, + {run: accumulateEvents, arg: ['textinput', {data: 'xyz'}]}, + {run: accumulateEvents, arg: ['keyup', {keyCode: 32}]}, + {run: accumulateEvents, arg: ['compositionend', {data: 'Hello'}]}, + ]; + + /* Defined expected results as a factory of result data because we need + lazy evaluation for event modules. + Event modules are reloaded to simulate a different platform per testcase. + If we define expected results as a simple dictionary here, the comparison + of 'instanceof' fails after module cache is reset. */ + + // Webkit behavior is simple. We expect SyntheticInputEvent at each + // textInput, SyntheticCompositionEvent at composition, and nothing from + // keyUp. + var Expected_Webkit = () => [ + {type: ModuleCache.SyntheticCompositionEvent, data: {}}, {type: null}, + {type: null}, {type: ModuleCache.SyntheticInputEvent, data: {data: 'A'}}, + {type: null}, {type: null}, // textinput of A + {type: null}, {type: null}, // keyUp of 65 + {type: null}, {type: ModuleCache.SyntheticInputEvent, data: {data: 'abc'}}, + {type: null}, {type: null}, // textinput of abc + {type: null}, {type: null}, // keyUp of 32 + {type: null}, {type: ModuleCache.SyntheticInputEvent, data: {data: 'xyz'}}, + {type: null}, {type: null}, // textinput of xyz + {type: null}, {type: null}, // keyUp of 32 + {type: ModuleCache.SyntheticCompositionEvent, data: {data: 'Hello'}}, + {type: null}, + ]; + + // For IE11, we use fallback data instead of IE's textinput events. + // We expect no SyntheticInputEvent from textinput. Fallback beforeInput is + // expected to be triggered at compositionend with a text of the target + // element, not event data. + var Expected_IE11 = () => [ + {type: ModuleCache.SyntheticCompositionEvent, data: {}}, {type: null}, + {type: null}, {type: null}, // textInput of A + {type: null}, {type: null}, // textinput of A + {type: null}, {type: null}, // keyUp of 65 + {type: null}, {type: null}, // textInput of abc + {type: null}, {type: null}, // textinput of abc + + // fallbackData should NOT be set at keyUp with any of END_KEYCODES + {type: null}, {type: null}, // keyUp of 32 + + {type: null}, {type: null}, // textInput of xyz + {type: null}, {type: null}, // textinput of xyz + {type: null}, {type: null}, // keyUp of 32 + + // fallbackData is retrieved from the element, which is XYZ, + // at a time of compositionend + {type: ModuleCache.SyntheticCompositionEvent, data: {}}, + {type: ModuleCache.SyntheticInputEvent, data: {data: 'XYZ'}}, + ]; + + function TestEditableReactComponent(Emulator, Scenario, ExpectedResult) { + ModuleCache = new initialize(Emulator); + + var EditableDiv = React.createClass({ + render: () => (
), + }); + var rendered = ReactTestUtils.renderIntoDocument(); + + var node = ModuleCache.ReactDOM.findDOMNode(rendered); + var events = []; + + Scenario.forEach((el) => + el.run.call(this, node, events).apply(this, el.arg)); + verifyEvents(events, ExpectedResult()); + } + + it('extract onBeforeInput from native textinput events', function() { + TestEditableReactComponent( + simulateWebkit, Scenario_Composition, Expected_Webkit); + }); + + it('extract onBeforeInput from fallback objects', function() { + TestEditableReactComponent( + simulateIE11, Scenario_Composition, Expected_IE11); + }); +}); diff --git a/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js b/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js index 1b01eebd91..6c7d4fe16b 100644 --- a/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js +++ b/src/renderers/dom/client/syntheticEvents/SyntheticEvent.js @@ -121,7 +121,7 @@ Object.assign(SyntheticEvent.prototype, { if (event.preventDefault) { event.preventDefault(); - } else { + } else if (typeof event.returnValue !== 'unknown') { // eslint-disable-line valid-typeof event.returnValue = false; } this.isDefaultPrevented = emptyFunction.thatReturnsTrue; diff --git a/src/renderers/dom/client/utils/__tests__/setInnerHTML.js b/src/renderers/dom/client/utils/__tests__/setInnerHTML-test.js similarity index 89% rename from src/renderers/dom/client/utils/__tests__/setInnerHTML.js rename to src/renderers/dom/client/utils/__tests__/setInnerHTML-test.js index 2f4eccec4e..32756c6d5e 100644 --- a/src/renderers/dom/client/utils/__tests__/setInnerHTML.js +++ b/src/renderers/dom/client/utils/__tests__/setInnerHTML-test.js @@ -25,7 +25,9 @@ describe('setInnerHTML', function() { }); describe('when the node does not have an innerHTML property', () => { - it('sets innerHTML on it', function() { + // Disabled. JSDOM doesn't seem to remove nodes when using appendChild to + // move existing nodes. + xit('sets innerHTML on it', function() { // Create a mock node that looks like an SVG in IE (without innerHTML) var node = { namespaceURI: DOMNamespaces.svg, diff --git a/src/renderers/dom/client/utils/setInnerHTML.js b/src/renderers/dom/client/utils/setInnerHTML.js index 59348fe3c3..616d37fc32 100644 --- a/src/renderers/dom/client/utils/setInnerHTML.js +++ b/src/renderers/dom/client/utils/setInnerHTML.js @@ -38,9 +38,9 @@ var setInnerHTML = createMicrosoftUnsafeLocalFunction( if (node.namespaceURI === DOMNamespaces.svg && !('innerHTML' in node)) { reusableSVGContainer = reusableSVGContainer || document.createElement('div'); reusableSVGContainer.innerHTML = '' + html + ''; - var newNodes = reusableSVGContainer.firstChild.childNodes; - for (var i = 0; i < newNodes.length; i++) { - node.appendChild(newNodes[i]); + var svgNode = reusableSVGContainer.firstChild; + while (svgNode.firstChild) { + node.appendChild(svgNode.firstChild); } } else { node.innerHTML = html; diff --git a/src/renderers/dom/client/validateDOMNesting.js b/src/renderers/dom/client/validateDOMNesting.js index 9422a079e1..860e37da1f 100644 --- a/src/renderers/dom/client/validateDOMNesting.js +++ b/src/renderers/dom/client/validateDOMNesting.js @@ -322,11 +322,24 @@ if (__DEV__) { var didWarn = {}; - validateDOMNesting = function(childTag, childInstance, ancestorInfo) { + validateDOMNesting = function( + childTag, + childText, + childInstance, + ancestorInfo + ) { ancestorInfo = ancestorInfo || emptyAncestorInfo; var parentInfo = ancestorInfo.current; var parentTag = parentInfo && parentInfo.tag; + if (childText != null) { + warning( + childTag == null, + 'validateDOMNesting: when childText is passed, childTag should be null' + ); + childTag = '#text'; + } + var invalidParent = isTagValidWithParent(childTag, parentTag) ? null : parentInfo; var invalidAncestor = @@ -385,7 +398,17 @@ if (__DEV__) { didWarn[warnKey] = true; var tagDisplayName = childTag; - if (childTag !== '#text') { + var whitespaceInfo = ''; + if (childTag === '#text') { + if (/\S/.test(childText)) { + tagDisplayName = 'Text nodes'; + } else { + tagDisplayName = 'Whitespace text nodes'; + whitespaceInfo = + ' Make sure you don\'t have any extra whitespace between tags on ' + + 'each line of your source code.'; + } + } else { tagDisplayName = '<' + childTag + '>'; } @@ -398,10 +421,11 @@ if (__DEV__) { } warning( false, - 'validateDOMNesting(...): %s cannot appear as a child of <%s>. ' + + 'validateDOMNesting(...): %s cannot appear as a child of <%s>.%s ' + 'See %s.%s', tagDisplayName, ancestorTag, + whitespaceInfo, ownerInfo, info ); diff --git a/src/renderers/dom/client/wrappers/ReactDOMInput.js b/src/renderers/dom/client/wrappers/ReactDOMInput.js index dfa860a2db..4350b4b050 100644 --- a/src/renderers/dom/client/wrappers/ReactDOMInput.js +++ b/src/renderers/dom/client/wrappers/ReactDOMInput.js @@ -36,7 +36,7 @@ function forceUpdateIfMounted() { function isControlled(props) { var usesChecked = props.type === 'checkbox' || props.type === 'radio'; - return usesChecked ? props.checked !== undefined : props.value !== undefined; + return usesChecked ? props.checked != null : props.value != null; } /** diff --git a/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js b/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js index 342d96e194..0195802484 100644 --- a/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js +++ b/src/renderers/dom/client/wrappers/__tests__/ReactDOMInput-test.js @@ -571,7 +571,7 @@ describe('ReactDOMInput', function() { expect(console.error.calls.count()).toBe(1); }); - it('should warn if controlled input switches to uncontrolled', function() { + it('should warn if controlled input switches to uncontrolled (value is undefined)', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -585,6 +585,20 @@ describe('ReactDOMInput', function() { ); }); + it('should warn if controlled input switches to uncontrolled (value is null)', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBeGreaterThan(0); + expect(console.error.calls.argsFor(1)[0]).toContain( + 'A component is changing a controlled input of type text to be uncontrolled. ' + + 'Input elements should not switch from controlled to uncontrolled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + it('should warn if controlled input switches to uncontrolled with defaultValue', function() { var stub = ; var container = document.createElement('div'); @@ -599,7 +613,7 @@ describe('ReactDOMInput', function() { ); }); - it('should warn if uncontrolled input switches to controlled', function() { + it('should warn if uncontrolled input (value is undefined) switches to controlled', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -613,7 +627,21 @@ describe('ReactDOMInput', function() { ); }); - it('should warn if controlled checkbox switches to uncontrolled', function() { + it('should warn if uncontrolled input (value is null) switches to controlled', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBeGreaterThan(0); + expect(console.error.calls.argsFor(1)[0]).toContain( + 'A component is changing an uncontrolled input of type text to be controlled. ' + + 'Input elements should not switch from uncontrolled to controlled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + + it('should warn if controlled checkbox switches to uncontrolled (checked is undefined)', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -627,6 +655,20 @@ describe('ReactDOMInput', function() { ); }); + it('should warn if controlled checkbox switches to uncontrolled (checked is null)', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'A component is changing a controlled input of type checkbox to be uncontrolled. ' + + 'Input elements should not switch from controlled to uncontrolled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + it('should warn if controlled checkbox switches to uncontrolled with defaultChecked', function() { var stub = ; var container = document.createElement('div'); @@ -641,7 +683,7 @@ describe('ReactDOMInput', function() { ); }); - it('should warn if uncontrolled checkbox switches to controlled', function() { + it('should warn if uncontrolled checkbox (checked is undefined) switches to controlled', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -655,7 +697,21 @@ describe('ReactDOMInput', function() { ); }); - it('should warn if controlled radio switches to uncontrolled', function() { + it('should warn if uncontrolled checkbox (checked is null) switches to controlled', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'A component is changing an uncontrolled input of type checkbox to be controlled. ' + + 'Input elements should not switch from uncontrolled to controlled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + + it('should warn if controlled radio switches to uncontrolled (checked is undefined)', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -669,6 +725,20 @@ describe('ReactDOMInput', function() { ); }); + it('should warn if controlled radio switches to uncontrolled (checked is null)', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'A component is changing a controlled input of type radio to be uncontrolled. ' + + 'Input elements should not switch from controlled to uncontrolled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + it('should warn if controlled radio switches to uncontrolled with defaultChecked', function() { var stub = ; var container = document.createElement('div'); @@ -683,7 +753,7 @@ describe('ReactDOMInput', function() { ); }); - it('should warn if uncontrolled radio switches to controlled', function() { + it('should warn if uncontrolled radio (checked is undefined) switches to controlled', function() { var stub = ; var container = document.createElement('div'); ReactDOM.render(stub, container); @@ -697,6 +767,20 @@ describe('ReactDOMInput', function() { ); }); + it('should warn if uncontrolled radio (checked is null) switches to controlled', function() { + var stub = ; + var container = document.createElement('div'); + ReactDOM.render(stub, container); + ReactDOM.render(, container); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'A component is changing an uncontrolled input of type radio to be controlled. ' + + 'Input elements should not switch from uncontrolled to controlled (or vice versa). ' + + 'Decide between using a controlled or uncontrolled input ' + + 'element for the lifetime of the component. More info: https://fb.me/react-controlled-components' + ); + }); + it('should not warn if radio value changes but never becomes controlled', function() { var container = document.createElement('div'); ReactDOM.render(, container); diff --git a/src/renderers/dom/shared/HTMLDOMPropertyConfig.js b/src/renderers/dom/shared/HTMLDOMPropertyConfig.js index c6cf0dcbca..31b2b9cd4a 100644 --- a/src/renderers/dom/shared/HTMLDOMPropertyConfig.js +++ b/src/renderers/dom/shared/HTMLDOMPropertyConfig.js @@ -36,6 +36,8 @@ var HTMLDOMPropertyConfig = { allowFullScreen: HAS_BOOLEAN_VALUE, allowTransparency: 0, alt: 0, + // specifies target context for links with `preload` type + as: 0, async: HAS_BOOLEAN_VALUE, autoComplete: 0, // autoFocus is polyfilled/normalized by AutoFocusUtils @@ -116,6 +118,7 @@ var HTMLDOMPropertyConfig = { optimum: 0, pattern: 0, placeholder: 0, + playsInline: HAS_BOOLEAN_VALUE, poster: 0, preload: 0, profile: 0, diff --git a/src/renderers/dom/shared/ReactDOMComponent.js b/src/renderers/dom/shared/ReactDOMComponent.js index 07a57b096b..5f3117bcc2 100644 --- a/src/renderers/dom/shared/ReactDOMComponent.js +++ b/src/renderers/dom/shared/ReactDOMComponent.js @@ -253,9 +253,9 @@ function optionPostMount() { ReactDOMOption.postMountWrapper(inst); } -var setContentChildForInstrumentation = emptyFunction; +var setAndValidateContentChildDev = emptyFunction; if (__DEV__) { - setContentChildForInstrumentation = function(content) { + setAndValidateContentChildDev = function(content) { var hasExistingContent = this._contentDebugID != null; var debugID = this._debugID; // This ID represents the inlined child that has no backing instance: @@ -269,6 +269,7 @@ if (__DEV__) { return; } + validateDOMNesting(null, String(content), this, this._ancestorInfo); this._contentDebugID = contentDebugID; if (hasExistingContent) { ReactInstrumentation.debugTool.onBeforeUpdateComponent(contentDebugID, content); @@ -492,7 +493,7 @@ function ReactDOMComponent(element) { this._flags = 0; if (__DEV__) { this._ancestorInfo = null; - setContentChildForInstrumentation.call(this, null); + setAndValidateContentChildDev.call(this, null); } } @@ -598,7 +599,7 @@ ReactDOMComponent.Mixin = { if (parentInfo) { // parentInfo should always be present except for the top-level // component when server rendering - validateDOMNesting(this._tag, this, parentInfo); + validateDOMNesting(this._tag, null, this, parentInfo); } this._ancestorInfo = validateDOMNesting.updatedAncestorInfo(parentInfo, this._tag, this); @@ -794,7 +795,7 @@ ReactDOMComponent.Mixin = { // TODO: Validate that text is allowed as a child of this node ret = escapeTextContentForBrowser(contentToUse); if (__DEV__) { - setContentChildForInstrumentation.call(this, contentToUse); + setAndValidateContentChildDev.call(this, contentToUse); } } else if (childrenToUse != null) { var mountImages = this.mountChildren( @@ -836,7 +837,7 @@ ReactDOMComponent.Mixin = { if (contentToUse != null) { // TODO: Validate that text is allowed as a child of this node if (__DEV__) { - setContentChildForInstrumentation.call(this, contentToUse); + setAndValidateContentChildDev.call(this, contentToUse); } DOMLazyTree.queueText(lazyTree, contentToUse); } else if (childrenToUse != null) { @@ -1110,7 +1111,7 @@ ReactDOMComponent.Mixin = { if (lastContent !== nextContent) { this.updateTextContent('' + nextContent); if (__DEV__) { - setContentChildForInstrumentation.call(this, nextContent); + setAndValidateContentChildDev.call(this, nextContent); } } } else if (nextHtml != null) { @@ -1122,7 +1123,7 @@ ReactDOMComponent.Mixin = { } } else if (nextChildren != null) { if (__DEV__) { - setContentChildForInstrumentation.call(this, null); + setAndValidateContentChildDev.call(this, null); } this.updateChildren(nextChildren, transaction, context); @@ -1185,7 +1186,7 @@ ReactDOMComponent.Mixin = { this._wrapperState = null; if (__DEV__) { - setContentChildForInstrumentation.call(this, null); + setAndValidateContentChildDev.call(this, null); } }, diff --git a/src/renderers/dom/shared/ReactDOMTextComponent.js b/src/renderers/dom/shared/ReactDOMTextComponent.js index 24b0027d26..860bc33496 100644 --- a/src/renderers/dom/shared/ReactDOMTextComponent.js +++ b/src/renderers/dom/shared/ReactDOMTextComponent.js @@ -75,7 +75,7 @@ Object.assign(ReactDOMTextComponent.prototype, { if (parentInfo) { // parentInfo should always be present except for the top-level // component when server rendering - validateDOMNesting('#text', this, parentInfo); + validateDOMNesting(null, this._stringText, this, parentInfo); } } diff --git a/src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js b/src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js index f87922d9e4..aa25db7983 100644 --- a/src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js +++ b/src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js @@ -777,7 +777,7 @@ describe('ReactDOMComponent', function() { }); it('should work error event on element', function() { - spyOn(console, 'error'); + spyOn(console, 'error'); var container = document.createElement('div'); ReactDOM.render(