Sync vendored modules from upstream
Paul O’Shannessy committed
Jan 6, 2014 at 11:55 UTC
c2be8ba42d3ceaa7a37700b7ea6b723780dab9c1
7 files changed
+119
-116
src/vendor/core/$.js
+21
-6
@@ -17,7 +17,6 @@
17
* @typechecks
18
*/
19
20
-var ge = require('ge');
20
var ex = require('ex');
21
22
/**
@@ -28,12 +27,9 @@ var ex = require('ex');
27
*
28
* If you're not sure whether or not the element exists, use ge instead, and
29
* manually check for the element's existence in your application code.
31
- *
32
- * @param {string|DOMDocument|DOMElement|DOMTextNode|Comment} id
33
- * @return {DOMDocument|DOMElement|DOMTextNode|Comment}
30
*/
35
-function $(id) {
36
- var element = ge(id);
31
+function getRequiredElement(id) {
32
+ var element = typeof id === 'string' ? document.getElementById(id) : id;
33
if (!element) {
34
throw new Error(ex(
35
'Tried to get element with id of "%s" but it is not present on the page.',
@@ -43,4 +39,23 @@ function $(id) {
39
return element;
40
}
41
42
+/**
43
+ * Find a node by ID with typechecked input and output.
44
+ *
45
+ * @param {string|DOMDocument|DOMElement|DOMTextNode|Comment} id
46
+ * @return {DOMDocument|DOMElement|DOMTextNode|Comment}
47
+ */
48
+function $(id) {
49
+ return getRequiredElement(id);
50
+}
51
+
52
+/**
53
+ * Find a node by ID without typechecks.
54
+ *
55
+ * This is micro-optimization for the small subset of users who have typechecks
56
+ * enabled. It should only be used by frequently called core modules that are
57
+ * already checking their params and return values.
58
+ */
59
+$.unsafe = getRequiredElement;
60
+
61
module.exports = $;
src/vendor/core/createArrayFrom.js
+10
-13
@@ -17,11 +17,9 @@
17
* @typechecks
18
*/
19
20
+var toArray = require('toArray');
21
+
22
/**
21
- * NOTE: if you are a previous user of this function, it has been considered
22
- * unsafe because it's inconsistent across browsers for some inputs.
23
- * Instead use `Array.isArray()`.
24
- *
23
* Perform a heuristic test to determine if an object is "array-like".
24
*
25
* A monk asked Joshu, a Zen master, "Has a dog Buddha nature?"
@@ -31,6 +29,8 @@
29
* true if the argument is an actual array, an `arguments' object, or an
30
* HTMLCollection (e.g. node.childNodes or node.getElementsByTagName()).
31
*
32
+ * It will return false for other array-like objects like Filelist.
33
+ *
34
* @param {*} obj
35
* @return {boolean}
36
*/
@@ -73,8 +73,8 @@ function hasArrayNature(obj) {
73
*
74
* This allows you to treat `things' as an array, but accept scalars in the API.
75
*
76
- * This is also good for converting certain pseudo-arrays, like `arguments` or
77
- * HTMLCollections, into arrays.
76
+ * If you need to convert an array-like object, like `arguments`, into an array
77
+ * use toArray instead.
78
*
79
* @param {*} obj
80
* @return {array}
@@ -82,14 +82,11 @@ function hasArrayNature(obj) {
82
function createArrayFrom(obj) {
83
if (!hasArrayNature(obj)) {
84
return [obj];
85
+ } else if (Array.isArray(obj)) {
86
+ return obj.slice();
87
+ } else {
88
+ return toArray(obj);
89
}
86
- if (obj.item) {
87
- // IE does not support Array#slice on HTMLCollections
88
- var l = obj.length, ret = new Array(l);
89
- while (l--) { ret[l] = obj[l]; }
90
- return ret;
91
- }
92
- return Array.prototype.slice.call(obj);
90
}
91
92
module.exports = createArrayFrom;
src/vendor/core/dom/getActiveElement.js
+4
-3
@@ -20,14 +20,15 @@
20
/**
21
* Same as document.activeElement but wraps in a try-catch block. In IE it is
22
* not safe to call document.activeElement if there is nothing focused.
23
+ *
24
+ * The activeElement will be null only if the document body is not yet defined.
25
*/
26
function getActiveElement() /*?DOMElement*/ {
27
try {
26
- return document.activeElement;
28
+ return document.activeElement || document.body;
29
} catch (e) {
28
- return null;
30
+ return document.body;
31
}
32
}
33
34
module.exports = getActiveElement;
33
-
src/vendor/core/ge.js
deleted
-76
@@ -1,76 +0,0 @@
1
-/**
2
- * Copyright 2013 Facebook, Inc.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- *
16
- * @providesModule ge
17
- */
18
-
19
-/**
20
- * Find a node by ID. Optionally search a sub-tree outside of the document
21
- *
22
- * Use ge if you're not sure whether or not the element exists. You can test
23
- * for existence yourself in your application code.
24
- *
25
- * If your application code depends on the existence of the element, use $
26
- * instead, which will throw in DEV if the element doesn't exist.
27
- */
28
-function ge(arg, root, tag) {
29
- return typeof arg != 'string' ? arg :
30
- !root ? document.getElementById(arg) :
31
- _geFromSubtree(arg, root, tag);
32
-}
33
-
34
-function _geFromSubtree(id, root, tag) {
35
- var elem, children, ii;
36
-
37
- if (_getNodeID(root) == id) {
38
- return root;
39
- } else if (root.getElementsByTagName) {
40
- // All Elements implement this, which does an iterative DFS, which is
41
- // faster than recursion and doesn't run into stack depth issues.
42
- children = root.getElementsByTagName(tag || '*');
43
- for (ii = 0; ii < children.length; ii++) {
44
- if (_getNodeID(children[ii]) == id) {
45
- return children[ii];
46
- }
47
- }
48
- } else {
49
- // DocumentFragment does not implement getElementsByTagName, so
50
- // recurse over its children. Its children must be Elements, so
51
- // each child will use the getElementsByTagName case instead.
52
- children = root.childNodes;
53
- for (ii = 0; ii < children.length; ii++) {
54
- elem = _geFromSubtree(id, children[ii]);
55
- if (elem) {
56
- return elem;
57
- }
58
- }
59
- }
60
-
61
- return null;
62
-}
63
-
64
-/**
65
- * Return the ID value for a given node. This allows us to avoid issues
66
- * with forms that contain inputs with name="id".
67
- *
68
- * @return string (null if attribute not set)
69
- */
70
-function _getNodeID(node) {
71
- // #document and #document-fragment do not have getAttributeNode.
72
- var id = node.getAttributeNode && node.getAttributeNode('id');
73
- return id ? id.value : null;
74
-}
75
-
76
-module.exports = ge;
src/vendor/core/getMarkupWrap.js
+2
@@ -39,6 +39,7 @@ var shouldWrap = {
39
'g': true,
40
'line': true,
41
'path': true,
42
+ 'polygon': true,
43
'polyline': true,
44
'rect': true,
45
'text': true
@@ -75,6 +76,7 @@ var markupWrap = {
76
'g': svgWrap,
77
'line': svgWrap,
78
'path': svgWrap,
79
+ 'polygon': svgWrap,
80
'polyline': svgWrap,
81
'rect': svgWrap,
82
'text': svgWrap
src/vendor/core/invariant.js
+9
-18
@@ -1,26 +1,13 @@
1
/**
2
- * Copyright 2013 Facebook, Inc.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- *
2
* @providesModule invariant
3
*/
4
5
/**
6
* Use invariant() to assert state which your program assumes to be true.
7
*
22
- * Provide sprintf style format and arguments to provide information about
23
- * what broke and what you were expecting.
8
+ * Provide sprintf-style format (only %s is supported) and arguments
9
+ * to provide information about what broke and what you were
10
+ * expecting.
11
*
12
* The invariant message will be stripped in production, but the invariant
13
* will remain to ensure logic does not differ in production.
@@ -28,7 +15,9 @@
15
16
function invariant(condition) {
17
if (!condition) {
31
- throw new Error('Invariant Violation');
18
+ var error = new Error('Invariant Violation');
19
+ error.framesToPop = 1;
20
+ throw error;
21
}
22
}
23
@@ -43,10 +32,12 @@ if (__DEV__) {
32
if (!condition) {
33
var args = [a, b, c, d, e, f];
34
var argIndex = 0;
46
- throw new Error(
35
+ var error = new Error(
36
'Invariant Violation: ' +
37
format.replace(/%s/g, function() { return args[argIndex++]; })
38
);
39
+ error.framesToPop = 1; // we don't care about invariant's own frame
40
+ throw error;
41
}
42
};
43
src/vendor/core/toArray.js
new
+73
@@ -0,0 +1,73 @@
1
+/**
2
+ * Copyright 2014 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule toArray
17
+ * @typechecks
18
+ */
19
+
20
+var invariant = require('invariant');
21
+
22
+/**
23
+ * Convert array-like objects to arrays.
24
+ *
25
+ * This API assumes the caller knows the contents of the data type. For less
26
+ * well defined inputs use createArrayFrom.
27
+ *
28
+ * @param {object|function} obj
29
+ * @return {array}
30
+ */
31
+function toArray(obj) {
32
+ var length = obj.length;
33
+
34
+ // Some browse builtin objects can report typeof 'function' (e.g. NodeList in
35
+ // old versions of Safari).
36
+ invariant(
37
+ !Array.isArray(obj) &&
38
+ (typeof obj === 'object' || typeof obj === 'function'),
39
+ 'toArray: Array-like object expected'
40
+ );
41
+
42
+ invariant(
43
+ typeof length === 'number',
44
+ 'toArray: Object needs a length property'
45
+ );
46
+
47
+ invariant(
48
+ length === 0 ||
49
+ (length - 1) in obj,
50
+ 'toArray: Object should have keys for indices'
51
+ );
52
+
53
+ // Old IE doesn't give collections access to hasOwnProperty. Assume inputs
54
+ // without method will throw during the slice call and skip straight to the
55
+ // fallback.
56
+ if (obj.hasOwnProperty) {
57
+ try {
58
+ return Array.prototype.slice.call(obj);
59
+ } catch (e) {
60
+ // IE < 9 does not support Array#slice on collections objects
61
+ }
62
+ }
63
+
64
+ // Fall back to copying key by key. This assumes all keys have a value,
65
+ // so will not preserve sparsely populated inputs.
66
+ var ret = Array(length);
67
+ for (var ii = 0; ii < length; ii++) {
68
+ ret[ii] = obj[ii];
69
+ }
70
+ return ret;
71
+}
72
+
73
+module.exports = toArray;