react-test-renderer 15.5.0 → 15.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/lib/ReactShallowRenderer.js +6 -0
  2. package/lib/shallow/ARIADOMPropertyConfig.js +73 -0
  3. package/lib/shallow/AutoFocusUtils.js +23 -0
  4. package/lib/shallow/BeforeInputEventPlugin.js +384 -0
  5. package/lib/shallow/CSSProperty.js +147 -0
  6. package/lib/shallow/CSSPropertyOperations.js +208 -0
  7. package/lib/shallow/CallbackQueue.js +118 -0
  8. package/lib/shallow/ChangeEventPlugin.js +345 -0
  9. package/lib/shallow/DOMChildrenOperations.js +224 -0
  10. package/lib/shallow/DOMLazyTree.js +117 -0
  11. package/lib/shallow/DOMNamespaces.js +19 -0
  12. package/lib/shallow/DOMProperty.js +209 -0
  13. package/lib/shallow/DOMPropertyOperations.js +236 -0
  14. package/lib/shallow/Danger.js +47 -0
  15. package/lib/shallow/DefaultEventPluginOrder.js +25 -0
  16. package/lib/shallow/EnterLeaveEventPlugin.js +99 -0
  17. package/lib/shallow/EventConstants.js +91 -0
  18. package/lib/shallow/EventPluginHub.js +277 -0
  19. package/lib/shallow/EventPluginRegistry.js +254 -0
  20. package/lib/shallow/EventPluginUtils.js +225 -0
  21. package/lib/shallow/EventPropagators.js +133 -0
  22. package/lib/shallow/FallbackCompositionState.js +94 -0
  23. package/lib/shallow/HTMLDOMPropertyConfig.js +235 -0
  24. package/lib/shallow/KeyEscapeUtils.js +58 -0
  25. package/lib/shallow/LinkedValueUtils.js +137 -0
  26. package/lib/shallow/PluginModuleType.js +12 -0
  27. package/lib/shallow/PooledClass.js +111 -0
  28. package/lib/shallow/ReactBrowserEventEmitter.js +327 -0
  29. package/lib/shallow/ReactChildFiber.js +243 -0
  30. package/lib/shallow/ReactChildReconciler.js +153 -0
  31. package/lib/shallow/ReactComponentBrowserEnvironment.js +29 -0
  32. package/lib/shallow/ReactComponentEnvironment.js +45 -0
  33. package/lib/shallow/ReactCompositeComponent.js +901 -0
  34. package/lib/shallow/ReactCoroutine.js +80 -0
  35. package/lib/shallow/ReactDOM.js +110 -0
  36. package/lib/shallow/ReactDOMComponent.js +1000 -0
  37. package/lib/shallow/ReactDOMComponentFlags.js +17 -0
  38. package/lib/shallow/ReactDOMComponentTree.js +194 -0
  39. package/lib/shallow/ReactDOMContainerInfo.js +32 -0
  40. package/lib/shallow/ReactDOMEmptyComponent.js +59 -0
  41. package/lib/shallow/ReactDOMFeatureFlags.js +18 -0
  42. package/lib/shallow/ReactDOMFiber.js +97 -0
  43. package/lib/shallow/ReactDOMIDOperations.js +33 -0
  44. package/lib/shallow/ReactDOMInput.js +284 -0
  45. package/lib/shallow/ReactDOMInvalidARIAHook.js +92 -0
  46. package/lib/shallow/ReactDOMNullInputValuePropHook.js +42 -0
  47. package/lib/shallow/ReactDOMOption.js +122 -0
  48. package/lib/shallow/ReactDOMSelect.js +199 -0
  49. package/lib/shallow/ReactDOMSelection.js +211 -0
  50. package/lib/shallow/ReactDOMServer.js +25 -0
  51. package/lib/shallow/ReactDOMTextComponent.js +163 -0
  52. package/lib/shallow/ReactDOMTextarea.js +159 -0
  53. package/lib/shallow/ReactDOMTreeTraversal.js +135 -0
  54. package/lib/shallow/ReactDOMUnknownPropertyHook.js +111 -0
  55. package/lib/shallow/ReactDebugTool.js +358 -0
  56. package/lib/shallow/ReactDefaultBatchingStrategy.js +67 -0
  57. package/lib/shallow/ReactDefaultInjection.js +85 -0
  58. package/lib/shallow/ReactElementSymbol.js +19 -0
  59. package/lib/shallow/ReactEmptyComponent.js +29 -0
  60. package/lib/shallow/ReactErrorUtils.js +75 -0
  61. package/lib/shallow/ReactEventEmitterMixin.js +32 -0
  62. package/lib/shallow/ReactEventListener.js +154 -0
  63. package/lib/shallow/ReactFeatureFlags.js +21 -0
  64. package/lib/shallow/ReactFiber.js +190 -0
  65. package/lib/shallow/ReactFiberBeginWork.js +380 -0
  66. package/lib/shallow/ReactFiberCommitWork.js +78 -0
  67. package/lib/shallow/ReactFiberCompleteWork.js +208 -0
  68. package/lib/shallow/ReactFiberReconciler.js +64 -0
  69. package/lib/shallow/ReactFiberRoot.js +29 -0
  70. package/lib/shallow/ReactFiberScheduler.js +331 -0
  71. package/lib/shallow/ReactFiberUpdateQueue.js +77 -0
  72. package/lib/shallow/ReactHostComponent.js +67 -0
  73. package/lib/shallow/ReactHostOperationHistoryHook.js +33 -0
  74. package/lib/shallow/ReactInjection.js +33 -0
  75. package/lib/shallow/ReactInputSelection.js +123 -0
  76. package/lib/shallow/ReactInstanceMap.js +47 -0
  77. package/lib/shallow/ReactInstanceType.js +12 -0
  78. package/lib/shallow/ReactInstrumentation.js +23 -0
  79. package/lib/shallow/ReactInvalidSetStateWarningHook.js +36 -0
  80. package/lib/shallow/ReactMarkupChecksum.js +49 -0
  81. package/lib/shallow/ReactMount.js +537 -0
  82. package/lib/shallow/ReactMultiChild.js +449 -0
  83. package/lib/shallow/ReactMultiChildUpdateTypes.js +18 -0
  84. package/lib/shallow/ReactNodeTypes.js +39 -0
  85. package/lib/shallow/ReactOwner.js +93 -0
  86. package/lib/shallow/ReactPerf.js +500 -0
  87. package/lib/shallow/ReactPriorityLevel.js +20 -0
  88. package/lib/shallow/ReactPropTypeLocationNames.js +24 -0
  89. package/lib/shallow/ReactPropTypeLocations.js +12 -0
  90. package/lib/shallow/ReactPropTypesSecret.js +16 -0
  91. package/lib/shallow/ReactReconcileTransaction.js +177 -0
  92. package/lib/shallow/ReactReconciler.js +167 -0
  93. package/lib/shallow/ReactRef.js +88 -0
  94. package/lib/shallow/ReactReifiedYield.js +30 -0
  95. package/lib/shallow/ReactServerBatchingStrategy.js +21 -0
  96. package/lib/shallow/ReactServerRendering.js +89 -0
  97. package/lib/shallow/ReactServerRenderingTransaction.js +89 -0
  98. package/lib/shallow/ReactServerUpdateQueue.js +138 -0
  99. package/lib/shallow/ReactShallowRenderer.js +150 -0
  100. package/lib/shallow/ReactSimpleEmptyComponent.js +36 -0
  101. package/lib/shallow/ReactSyntheticEventType.js +14 -0
  102. package/lib/shallow/ReactTestEmptyComponent.js +36 -0
  103. package/lib/shallow/ReactTestMount.js +131 -0
  104. package/lib/shallow/ReactTestReconcileTransaction.js +128 -0
  105. package/lib/shallow/ReactTestRenderer.js +139 -0
  106. package/lib/shallow/ReactTestTextComponent.js +40 -0
  107. package/lib/shallow/ReactTypeOfWork.js +23 -0
  108. package/lib/shallow/ReactTypes.js +12 -0
  109. package/lib/shallow/ReactUpdateQueue.js +235 -0
  110. package/lib/shallow/ReactUpdates.js +250 -0
  111. package/lib/shallow/ReactVersion.js +13 -0
  112. package/lib/shallow/ResponderEventPlugin.js +507 -0
  113. package/lib/shallow/ResponderSyntheticEvent.js +38 -0
  114. package/lib/shallow/ResponderTouchHistoryStore.js +183 -0
  115. package/lib/shallow/SVGDOMPropertyConfig.js +301 -0
  116. package/lib/shallow/SelectEventPlugin.js +190 -0
  117. package/lib/shallow/SimpleEventPlugin.js +227 -0
  118. package/lib/shallow/SyntheticAnimationEvent.js +38 -0
  119. package/lib/shallow/SyntheticClipboardEvent.js +37 -0
  120. package/lib/shallow/SyntheticCompositionEvent.js +35 -0
  121. package/lib/shallow/SyntheticDragEvent.js +35 -0
  122. package/lib/shallow/SyntheticEvent.js +267 -0
  123. package/lib/shallow/SyntheticFocusEvent.js +35 -0
  124. package/lib/shallow/SyntheticInputEvent.js +36 -0
  125. package/lib/shallow/SyntheticKeyboardEvent.js +83 -0
  126. package/lib/shallow/SyntheticMouseEvent.js +71 -0
  127. package/lib/shallow/SyntheticTouchEvent.js +44 -0
  128. package/lib/shallow/SyntheticTransitionEvent.js +38 -0
  129. package/lib/shallow/SyntheticUIEvent.js +58 -0
  130. package/lib/shallow/SyntheticWheelEvent.js +53 -0
  131. package/lib/shallow/TapEventPlugin.js +115 -0
  132. package/lib/shallow/TouchHistoryMath.js +98 -0
  133. package/lib/shallow/Transaction.js +224 -0
  134. package/lib/shallow/ViewportMetrics.js +26 -0
  135. package/lib/shallow/accumulate.js +45 -0
  136. package/lib/shallow/accumulateInto.js +57 -0
  137. package/lib/shallow/adler32.js +43 -0
  138. package/lib/shallow/canDefineProperty.js +25 -0
  139. package/lib/shallow/checkReactTypeSpec.js +86 -0
  140. package/lib/shallow/createMicrosoftUnsafeLocalFunction.js +31 -0
  141. package/lib/shallow/dangerousStyleValue.js +78 -0
  142. package/lib/shallow/deprecated.js +56 -0
  143. package/lib/shallow/escapeTextContentForBrowser.js +122 -0
  144. package/lib/shallow/findDOMNode.js +59 -0
  145. package/lib/shallow/flattenChildren.js +75 -0
  146. package/lib/shallow/forEachAccumulated.js +30 -0
  147. package/lib/shallow/getEventCharCode.js +49 -0
  148. package/lib/shallow/getEventKey.js +101 -0
  149. package/lib/shallow/getEventModifierState.js +42 -0
  150. package/lib/shallow/getEventTarget.js +34 -0
  151. package/lib/shallow/getHostComponentFromComposite.js +29 -0
  152. package/lib/shallow/getIteratorFn.js +40 -0
  153. package/lib/shallow/getNodeForCharacterOffset.js +73 -0
  154. package/lib/shallow/getTextContentAccessor.js +32 -0
  155. package/lib/shallow/getVendorPrefixedEventName.js +100 -0
  156. package/lib/shallow/instantiateReactComponent.js +128 -0
  157. package/lib/shallow/isEventSupported.js +59 -0
  158. package/lib/shallow/isTextInputElement.js +50 -0
  159. package/lib/shallow/quoteAttributeValueForBrowser.js +25 -0
  160. package/lib/shallow/reactProdInvariant.js +38 -0
  161. package/lib/shallow/renderSubtreeIntoContainer.js +15 -0
  162. package/lib/shallow/setInnerHTML.js +97 -0
  163. package/lib/shallow/setTextContent.js +51 -0
  164. package/lib/shallow/shouldUpdateReactComponent.js +41 -0
  165. package/lib/shallow/traverseAllChildren.js +175 -0
  166. package/lib/shallow/validateDOMNesting.js +376 -0
  167. package/package.json +1 -1
  168. package/shallow.js +1 -1
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Copyright 2013-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ var CSSProperty = require('./CSSProperty');
14
+ var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
15
+ var ReactInstrumentation = require('./ReactInstrumentation');
16
+
17
+ var camelizeStyleName = require('fbjs/lib/camelizeStyleName');
18
+ var dangerousStyleValue = require('./dangerousStyleValue');
19
+ var hyphenateStyleName = require('fbjs/lib/hyphenateStyleName');
20
+ var memoizeStringOnly = require('fbjs/lib/memoizeStringOnly');
21
+ var warning = require('fbjs/lib/warning');
22
+
23
+ var processStyleName = memoizeStringOnly(function (styleName) {
24
+ return hyphenateStyleName(styleName);
25
+ });
26
+
27
+ var hasShorthandPropertyBug = false;
28
+ var styleFloatAccessor = 'cssFloat';
29
+ if (ExecutionEnvironment.canUseDOM) {
30
+ var tempStyle = document.createElement('div').style;
31
+ try {
32
+ // IE8 throws "Invalid argument." if resetting shorthand style properties.
33
+ tempStyle.font = '';
34
+ } catch (e) {
35
+ hasShorthandPropertyBug = true;
36
+ }
37
+ // IE8 only supports accessing cssFloat (standard) as styleFloat
38
+ if (document.documentElement.style.cssFloat === undefined) {
39
+ styleFloatAccessor = 'styleFloat';
40
+ }
41
+ }
42
+
43
+ if (process.env.NODE_ENV !== 'production') {
44
+ // 'msTransform' is correct, but the other prefixes should be capitalized
45
+ var badVendoredStyleNamePattern = /^(?:webkit|moz|o)[A-Z]/;
46
+
47
+ // style values shouldn't contain a semicolon
48
+ var badStyleValueWithSemicolonPattern = /;\s*$/;
49
+
50
+ var warnedStyleNames = {};
51
+ var warnedStyleValues = {};
52
+ var warnedForNaNValue = false;
53
+
54
+ var warnHyphenatedStyleName = function (name, owner) {
55
+ if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
56
+ return;
57
+ }
58
+
59
+ warnedStyleNames[name] = true;
60
+ process.env.NODE_ENV !== 'production' ? warning(false, 'Unsupported style property %s. Did you mean %s?%s', name, camelizeStyleName(name), checkRenderMessage(owner)) : void 0;
61
+ };
62
+
63
+ var warnBadVendoredStyleName = function (name, owner) {
64
+ if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) {
65
+ return;
66
+ }
67
+
68
+ warnedStyleNames[name] = true;
69
+ process.env.NODE_ENV !== 'production' ? warning(false, 'Unsupported vendor-prefixed style property %s. Did you mean %s?%s', name, name.charAt(0).toUpperCase() + name.slice(1), checkRenderMessage(owner)) : void 0;
70
+ };
71
+
72
+ var warnStyleValueWithSemicolon = function (name, value, owner) {
73
+ if (warnedStyleValues.hasOwnProperty(value) && warnedStyleValues[value]) {
74
+ return;
75
+ }
76
+
77
+ warnedStyleValues[value] = true;
78
+ process.env.NODE_ENV !== 'production' ? warning(false, 'Style property values shouldn\'t contain a semicolon.%s ' + 'Try "%s: %s" instead.', checkRenderMessage(owner), name, value.replace(badStyleValueWithSemicolonPattern, '')) : void 0;
79
+ };
80
+
81
+ var warnStyleValueIsNaN = function (name, value, owner) {
82
+ if (warnedForNaNValue) {
83
+ return;
84
+ }
85
+
86
+ warnedForNaNValue = true;
87
+ process.env.NODE_ENV !== 'production' ? warning(false, '`NaN` is an invalid value for the `%s` css style property.%s', name, checkRenderMessage(owner)) : void 0;
88
+ };
89
+
90
+ var checkRenderMessage = function (owner) {
91
+ if (owner) {
92
+ var name = owner.getName();
93
+ if (name) {
94
+ return ' Check the render method of `' + name + '`.';
95
+ }
96
+ }
97
+ return '';
98
+ };
99
+
100
+ /**
101
+ * @param {string} name
102
+ * @param {*} value
103
+ * @param {ReactDOMComponent} component
104
+ */
105
+ var warnValidStyle = function (name, value, component) {
106
+ var owner;
107
+ if (component) {
108
+ owner = component._currentElement._owner;
109
+ }
110
+ if (name.indexOf('-') > -1) {
111
+ warnHyphenatedStyleName(name, owner);
112
+ } else if (badVendoredStyleNamePattern.test(name)) {
113
+ warnBadVendoredStyleName(name, owner);
114
+ } else if (badStyleValueWithSemicolonPattern.test(value)) {
115
+ warnStyleValueWithSemicolon(name, value, owner);
116
+ }
117
+
118
+ if (typeof value === 'number' && isNaN(value)) {
119
+ warnStyleValueIsNaN(name, value, owner);
120
+ }
121
+ };
122
+ }
123
+
124
+ /**
125
+ * Operations for dealing with CSS properties.
126
+ */
127
+ var CSSPropertyOperations = {
128
+
129
+ /**
130
+ * Serializes a mapping of style properties for use as inline styles:
131
+ *
132
+ * > createMarkupForStyles({width: '200px', height: 0})
133
+ * "width:200px;height:0;"
134
+ *
135
+ * Undefined values are ignored so that declarative programming is easier.
136
+ * The result should be HTML-escaped before insertion into the DOM.
137
+ *
138
+ * @param {object} styles
139
+ * @param {ReactDOMComponent} component
140
+ * @return {?string}
141
+ */
142
+ createMarkupForStyles: function (styles, component) {
143
+ var serialized = '';
144
+ for (var styleName in styles) {
145
+ if (!styles.hasOwnProperty(styleName)) {
146
+ continue;
147
+ }
148
+ var styleValue = styles[styleName];
149
+ if (process.env.NODE_ENV !== 'production') {
150
+ warnValidStyle(styleName, styleValue, component);
151
+ }
152
+ if (styleValue != null) {
153
+ serialized += processStyleName(styleName) + ':';
154
+ serialized += dangerousStyleValue(styleName, styleValue, component) + ';';
155
+ }
156
+ }
157
+ return serialized || null;
158
+ },
159
+
160
+ /**
161
+ * Sets the value for multiple styles on a node. If a value is specified as
162
+ * '' (empty string), the corresponding style property will be unset.
163
+ *
164
+ * @param {DOMElement} node
165
+ * @param {object} styles
166
+ * @param {ReactDOMComponent} component
167
+ */
168
+ setValueForStyles: function (node, styles, component) {
169
+ if (process.env.NODE_ENV !== 'production') {
170
+ ReactInstrumentation.debugTool.onHostOperation({
171
+ instanceID: component._debugID,
172
+ type: 'update styles',
173
+ payload: styles
174
+ });
175
+ }
176
+
177
+ var style = node.style;
178
+ for (var styleName in styles) {
179
+ if (!styles.hasOwnProperty(styleName)) {
180
+ continue;
181
+ }
182
+ if (process.env.NODE_ENV !== 'production') {
183
+ warnValidStyle(styleName, styles[styleName], component);
184
+ }
185
+ var styleValue = dangerousStyleValue(styleName, styles[styleName], component);
186
+ if (styleName === 'float' || styleName === 'cssFloat') {
187
+ styleName = styleFloatAccessor;
188
+ }
189
+ if (styleValue) {
190
+ style[styleName] = styleValue;
191
+ } else {
192
+ var expansion = hasShorthandPropertyBug && CSSProperty.shorthandPropertyExpansions[styleName];
193
+ if (expansion) {
194
+ // Shorthand property that IE8 won't like unsetting, so unset each
195
+ // component to placate it
196
+ for (var individualStyleName in expansion) {
197
+ style[individualStyleName] = '';
198
+ }
199
+ } else {
200
+ style[styleName] = '';
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ };
207
+
208
+ module.exports = CSSPropertyOperations;
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Copyright 2013-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ *
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ var _prodInvariant = require('./reactProdInvariant');
15
+
16
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
+
18
+ var PooledClass = require('./PooledClass');
19
+
20
+ var invariant = require('fbjs/lib/invariant');
21
+
22
+ /**
23
+ * A specialized pseudo-event module to help keep track of components waiting to
24
+ * be notified when their DOM representations are available for use.
25
+ *
26
+ * This implements `PooledClass`, so you should never need to instantiate this.
27
+ * Instead, use `CallbackQueue.getPooled()`.
28
+ *
29
+ * @class ReactMountReady
30
+ * @implements PooledClass
31
+ * @internal
32
+ */
33
+
34
+ var CallbackQueue = function () {
35
+ function CallbackQueue(arg) {
36
+ _classCallCheck(this, CallbackQueue);
37
+
38
+ this._callbacks = null;
39
+ this._contexts = null;
40
+ this._arg = arg;
41
+ }
42
+
43
+ /**
44
+ * Enqueues a callback to be invoked when `notifyAll` is invoked.
45
+ *
46
+ * @param {function} callback Invoked when `notifyAll` is invoked.
47
+ * @param {?object} context Context to call `callback` with.
48
+ * @internal
49
+ */
50
+
51
+
52
+ CallbackQueue.prototype.enqueue = function enqueue(callback, context) {
53
+ this._callbacks = this._callbacks || [];
54
+ this._callbacks.push(callback);
55
+ this._contexts = this._contexts || [];
56
+ this._contexts.push(context);
57
+ };
58
+
59
+ /**
60
+ * Invokes all enqueued callbacks and clears the queue. This is invoked after
61
+ * the DOM representation of a component has been created or updated.
62
+ *
63
+ * @internal
64
+ */
65
+
66
+
67
+ CallbackQueue.prototype.notifyAll = function notifyAll() {
68
+ var callbacks = this._callbacks;
69
+ var contexts = this._contexts;
70
+ var arg = this._arg;
71
+ if (callbacks && contexts) {
72
+ !(callbacks.length === contexts.length) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Mismatched list of contexts in callback queue') : _prodInvariant('24') : void 0;
73
+ this._callbacks = null;
74
+ this._contexts = null;
75
+ for (var i = 0; i < callbacks.length; i++) {
76
+ callbacks[i].call(contexts[i], arg);
77
+ }
78
+ callbacks.length = 0;
79
+ contexts.length = 0;
80
+ }
81
+ };
82
+
83
+ CallbackQueue.prototype.checkpoint = function checkpoint() {
84
+ return this._callbacks ? this._callbacks.length : 0;
85
+ };
86
+
87
+ CallbackQueue.prototype.rollback = function rollback(len) {
88
+ if (this._callbacks && this._contexts) {
89
+ this._callbacks.length = len;
90
+ this._contexts.length = len;
91
+ }
92
+ };
93
+
94
+ /**
95
+ * Resets the internal queue.
96
+ *
97
+ * @internal
98
+ */
99
+
100
+
101
+ CallbackQueue.prototype.reset = function reset() {
102
+ this._callbacks = null;
103
+ this._contexts = null;
104
+ };
105
+
106
+ /**
107
+ * `PooledClass` looks for this.
108
+ */
109
+
110
+
111
+ CallbackQueue.prototype.destructor = function destructor() {
112
+ this.reset();
113
+ };
114
+
115
+ return CallbackQueue;
116
+ }();
117
+
118
+ module.exports = PooledClass.addPoolingTo(CallbackQueue);
@@ -0,0 +1,345 @@
1
+ /**
2
+ * Copyright 2013-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ var EventPluginHub = require('./EventPluginHub');
14
+ var EventPropagators = require('./EventPropagators');
15
+ var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
16
+ var ReactDOMComponentTree = require('./ReactDOMComponentTree');
17
+ var ReactUpdates = require('./ReactUpdates');
18
+ var SyntheticEvent = require('./SyntheticEvent');
19
+
20
+ var getEventTarget = require('./getEventTarget');
21
+ var isEventSupported = require('./isEventSupported');
22
+ var isTextInputElement = require('./isTextInputElement');
23
+
24
+ var eventTypes = {
25
+ change: {
26
+ phasedRegistrationNames: {
27
+ bubbled: 'onChange',
28
+ captured: 'onChangeCapture'
29
+ },
30
+ dependencies: ['topBlur', 'topChange', 'topClick', 'topFocus', 'topInput', 'topKeyDown', 'topKeyUp', 'topSelectionChange']
31
+ }
32
+ };
33
+
34
+ /**
35
+ * For IE shims
36
+ */
37
+ var activeElement = null;
38
+ var activeElementInst = null;
39
+ var activeElementValue = null;
40
+ var activeElementValueProp = null;
41
+
42
+ /**
43
+ * SECTION: handle `change` event
44
+ */
45
+ function shouldUseChangeEvent(elem) {
46
+ var nodeName = elem.nodeName && elem.nodeName.toLowerCase();
47
+ return nodeName === 'select' || nodeName === 'input' && elem.type === 'file';
48
+ }
49
+
50
+ var doesChangeEventBubble = false;
51
+ if (ExecutionEnvironment.canUseDOM) {
52
+ // See `handleChange` comment below
53
+ doesChangeEventBubble = isEventSupported('change') && (!document.documentMode || document.documentMode > 8);
54
+ }
55
+
56
+ function manualDispatchChangeEvent(nativeEvent) {
57
+ var event = SyntheticEvent.getPooled(eventTypes.change, activeElementInst, nativeEvent, getEventTarget(nativeEvent));
58
+ EventPropagators.accumulateTwoPhaseDispatches(event);
59
+
60
+ // If change and propertychange bubbled, we'd just bind to it like all the
61
+ // other events and have it go through ReactBrowserEventEmitter. Since it
62
+ // doesn't, we manually listen for the events and so we have to enqueue and
63
+ // process the abstract event manually.
64
+ //
65
+ // Batching is necessary here in order to ensure that all event handlers run
66
+ // before the next rerender (including event handlers attached to ancestor
67
+ // elements instead of directly on the input). Without this, controlled
68
+ // components don't work properly in conjunction with event bubbling because
69
+ // the component is rerendered and the value reverted before all the event
70
+ // handlers can run. See https://github.com/facebook/react/issues/708.
71
+ ReactUpdates.batchedUpdates(runEventInBatch, event);
72
+ }
73
+
74
+ function runEventInBatch(event) {
75
+ EventPluginHub.enqueueEvents(event);
76
+ EventPluginHub.processEventQueue(false);
77
+ }
78
+
79
+ function startWatchingForChangeEventIE8(target, targetInst) {
80
+ activeElement = target;
81
+ activeElementInst = targetInst;
82
+ activeElement.attachEvent('onchange', manualDispatchChangeEvent);
83
+ }
84
+
85
+ function stopWatchingForChangeEventIE8() {
86
+ if (!activeElement) {
87
+ return;
88
+ }
89
+ activeElement.detachEvent('onchange', manualDispatchChangeEvent);
90
+ activeElement = null;
91
+ activeElementInst = null;
92
+ }
93
+
94
+ function getTargetInstForChangeEvent(topLevelType, targetInst) {
95
+ if (topLevelType === 'topChange') {
96
+ return targetInst;
97
+ }
98
+ }
99
+ function handleEventsForChangeEventIE8(topLevelType, target, targetInst) {
100
+ if (topLevelType === 'topFocus') {
101
+ // stopWatching() should be a noop here but we call it just in case we
102
+ // missed a blur event somehow.
103
+ stopWatchingForChangeEventIE8();
104
+ startWatchingForChangeEventIE8(target, targetInst);
105
+ } else if (topLevelType === 'topBlur') {
106
+ stopWatchingForChangeEventIE8();
107
+ }
108
+ }
109
+
110
+ /**
111
+ * SECTION: handle `input` event
112
+ */
113
+ var isInputEventSupported = false;
114
+ if (ExecutionEnvironment.canUseDOM) {
115
+ // IE9 claims to support the input event but fails to trigger it when
116
+ // deleting text, so we ignore its input events.
117
+ // IE10+ fire input events to often, such when a placeholder
118
+ // changes or when an input with a placeholder is focused.
119
+ isInputEventSupported = isEventSupported('input') && (!document.documentMode || document.documentMode > 11);
120
+ }
121
+
122
+ /**
123
+ * (For IE <=11) Replacement getter/setter for the `value` property that gets
124
+ * set on the active element.
125
+ */
126
+ var newValueProp = {
127
+ get: function () {
128
+ return activeElementValueProp.get.call(this);
129
+ },
130
+ set: function (val) {
131
+ // Cast to a string so we can do equality checks.
132
+ activeElementValue = '' + val;
133
+ activeElementValueProp.set.call(this, val);
134
+ }
135
+ };
136
+
137
+ /**
138
+ * (For IE <=11) Starts tracking propertychange events on the passed-in element
139
+ * and override the value property so that we can distinguish user events from
140
+ * value changes in JS.
141
+ */
142
+ function startWatchingForValueChange(target, targetInst) {
143
+ activeElement = target;
144
+ activeElementInst = targetInst;
145
+ activeElementValue = target.value;
146
+ activeElementValueProp = Object.getOwnPropertyDescriptor(target.constructor.prototype, 'value');
147
+
148
+ // Not guarded in a canDefineProperty check: IE8 supports defineProperty only
149
+ // on DOM elements
150
+ Object.defineProperty(activeElement, 'value', newValueProp);
151
+ if (activeElement.attachEvent) {
152
+ activeElement.attachEvent('onpropertychange', handlePropertyChange);
153
+ } else {
154
+ activeElement.addEventListener('propertychange', handlePropertyChange, false);
155
+ }
156
+ }
157
+
158
+ /**
159
+ * (For IE <=11) Removes the event listeners from the currently-tracked element,
160
+ * if any exists.
161
+ */
162
+ function stopWatchingForValueChange() {
163
+ if (!activeElement) {
164
+ return;
165
+ }
166
+
167
+ // delete restores the original property definition
168
+ delete activeElement.value;
169
+
170
+ if (activeElement.detachEvent) {
171
+ activeElement.detachEvent('onpropertychange', handlePropertyChange);
172
+ } else {
173
+ activeElement.removeEventListener('propertychange', handlePropertyChange, false);
174
+ }
175
+
176
+ activeElement = null;
177
+ activeElementInst = null;
178
+ activeElementValue = null;
179
+ activeElementValueProp = null;
180
+ }
181
+
182
+ /**
183
+ * (For IE <=11) Handles a propertychange event, sending a `change` event if
184
+ * the value of the active element has changed.
185
+ */
186
+ function handlePropertyChange(nativeEvent) {
187
+ if (nativeEvent.propertyName !== 'value') {
188
+ return;
189
+ }
190
+ var value = nativeEvent.srcElement.value;
191
+ if (value === activeElementValue) {
192
+ return;
193
+ }
194
+ activeElementValue = value;
195
+
196
+ manualDispatchChangeEvent(nativeEvent);
197
+ }
198
+
199
+ /**
200
+ * If a `change` event should be fired, returns the target's ID.
201
+ */
202
+ function getTargetInstForInputEvent(topLevelType, targetInst) {
203
+ if (topLevelType === 'topInput') {
204
+ // In modern browsers (i.e., not IE8 or IE9), the input event is exactly
205
+ // what we want so fall through here and trigger an abstract event
206
+ return targetInst;
207
+ }
208
+ }
209
+
210
+ function handleEventsForInputEventIE(topLevelType, target, targetInst) {
211
+ if (topLevelType === 'topFocus') {
212
+ // In IE8, we can capture almost all .value changes by adding a
213
+ // propertychange handler and looking for events with propertyName
214
+ // equal to 'value'
215
+ // In IE9-11, propertychange fires for most input events but is buggy and
216
+ // doesn't fire when text is deleted, but conveniently, selectionchange
217
+ // appears to fire in all of the remaining cases so we catch those and
218
+ // forward the event if the value has changed
219
+ // In either case, we don't want to call the event handler if the value
220
+ // is changed from JS so we redefine a setter for `.value` that updates
221
+ // our activeElementValue variable, allowing us to ignore those changes
222
+ //
223
+ // stopWatching() should be a noop here but we call it just in case we
224
+ // missed a blur event somehow.
225
+ stopWatchingForValueChange();
226
+ startWatchingForValueChange(target, targetInst);
227
+ } else if (topLevelType === 'topBlur') {
228
+ stopWatchingForValueChange();
229
+ }
230
+ }
231
+
232
+ // For IE8 and IE9.
233
+ function getTargetInstForInputEventIE(topLevelType, targetInst) {
234
+ if (topLevelType === 'topSelectionChange' || topLevelType === 'topKeyUp' || topLevelType === 'topKeyDown') {
235
+ // On the selectionchange event, the target is just document which isn't
236
+ // helpful for us so just check activeElement instead.
237
+ //
238
+ // 99% of the time, keydown and keyup aren't necessary. IE8 fails to fire
239
+ // propertychange on the first input event after setting `value` from a
240
+ // script and fires only keydown, keypress, keyup. Catching keyup usually
241
+ // gets it and catching keydown lets us fire an event for the first
242
+ // keystroke if user does a key repeat (it'll be a little delayed: right
243
+ // before the second keystroke). Other input methods (e.g., paste) seem to
244
+ // fire selectionchange normally.
245
+ if (activeElement && activeElement.value !== activeElementValue) {
246
+ activeElementValue = activeElement.value;
247
+ return activeElementInst;
248
+ }
249
+ }
250
+ }
251
+
252
+ /**
253
+ * SECTION: handle `click` event
254
+ */
255
+ function shouldUseClickEvent(elem) {
256
+ // Use the `click` event to detect changes to checkbox and radio inputs.
257
+ // This approach works across all browsers, whereas `change` does not fire
258
+ // until `blur` in IE8.
259
+ return elem.nodeName && elem.nodeName.toLowerCase() === 'input' && (elem.type === 'checkbox' || elem.type === 'radio');
260
+ }
261
+
262
+ function getTargetInstForClickEvent(topLevelType, targetInst) {
263
+ if (topLevelType === 'topClick') {
264
+ return targetInst;
265
+ }
266
+ }
267
+
268
+ function handleControlledInputBlur(inst, node) {
269
+ // TODO: In IE, inst is occasionally null. Why?
270
+ if (inst == null) {
271
+ return;
272
+ }
273
+
274
+ // Fiber and ReactDOM keep wrapper state in separate places
275
+ var state = inst._wrapperState || node._wrapperState;
276
+
277
+ if (!state || !state.controlled || node.type !== 'number') {
278
+ return;
279
+ }
280
+
281
+ // If controlled, assign the value attribute to the current value on blur
282
+ var value = '' + node.value;
283
+ if (node.getAttribute('value') !== value) {
284
+ node.setAttribute('value', value);
285
+ }
286
+ }
287
+
288
+ /**
289
+ * This plugin creates an `onChange` event that normalizes change events
290
+ * across form elements. This event fires at a time when it's possible to
291
+ * change the element's value without seeing a flicker.
292
+ *
293
+ * Supported elements are:
294
+ * - input (see `isTextInputElement`)
295
+ * - textarea
296
+ * - select
297
+ */
298
+ var ChangeEventPlugin = {
299
+
300
+ eventTypes: eventTypes,
301
+
302
+ extractEvents: function (topLevelType, targetInst, nativeEvent, nativeEventTarget) {
303
+ var targetNode = targetInst ? ReactDOMComponentTree.getNodeFromInstance(targetInst) : window;
304
+
305
+ var getTargetInstFunc, handleEventFunc;
306
+ if (shouldUseChangeEvent(targetNode)) {
307
+ if (doesChangeEventBubble) {
308
+ getTargetInstFunc = getTargetInstForChangeEvent;
309
+ } else {
310
+ handleEventFunc = handleEventsForChangeEventIE8;
311
+ }
312
+ } else if (isTextInputElement(targetNode)) {
313
+ if (isInputEventSupported) {
314
+ getTargetInstFunc = getTargetInstForInputEvent;
315
+ } else {
316
+ getTargetInstFunc = getTargetInstForInputEventIE;
317
+ handleEventFunc = handleEventsForInputEventIE;
318
+ }
319
+ } else if (shouldUseClickEvent(targetNode)) {
320
+ getTargetInstFunc = getTargetInstForClickEvent;
321
+ }
322
+
323
+ if (getTargetInstFunc) {
324
+ var inst = getTargetInstFunc(topLevelType, targetInst);
325
+ if (inst) {
326
+ var event = SyntheticEvent.getPooled(eventTypes.change, inst, nativeEvent, nativeEventTarget);
327
+ event.type = 'change';
328
+ EventPropagators.accumulateTwoPhaseDispatches(event);
329
+ return event;
330
+ }
331
+ }
332
+
333
+ if (handleEventFunc) {
334
+ handleEventFunc(topLevelType, targetNode, targetInst);
335
+ }
336
+
337
+ // When blurring, set the value attribute for number inputs
338
+ if (topLevelType === 'topBlur') {
339
+ handleControlledInputBlur(targetInst, targetNode);
340
+ }
341
+ }
342
+
343
+ };
344
+
345
+ module.exports = ChangeEventPlugin;