Skip to content

Commit

Permalink
Sync vendored modules from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
zpao committed Jan 6, 2014
1 parent 627d7eb commit c2be8ba
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 116 deletions.
27 changes: 21 additions & 6 deletions src/vendor/core/$.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @typechecks
*/

var ge = require('ge');
var ex = require('ex');

/**
Expand All @@ -28,12 +27,9 @@ var ex = require('ex');
*
* If you're not sure whether or not the element exists, use ge instead, and
* manually check for the element's existence in your application code.
*
* @param {string|DOMDocument|DOMElement|DOMTextNode|Comment} id
* @return {DOMDocument|DOMElement|DOMTextNode|Comment}
*/
function $(id) {
var element = ge(id);
function getRequiredElement(id) {
var element = typeof id === 'string' ? document.getElementById(id) : id;
if (!element) {
throw new Error(ex(
'Tried to get element with id of "%s" but it is not present on the page.',
Expand All @@ -43,4 +39,23 @@ function $(id) {
return element;
}

/**
* Find a node by ID with typechecked input and output.
*
* @param {string|DOMDocument|DOMElement|DOMTextNode|Comment} id
* @return {DOMDocument|DOMElement|DOMTextNode|Comment}
*/
function $(id) {
return getRequiredElement(id);
}

/**
* Find a node by ID without typechecks.
*
* This is micro-optimization for the small subset of users who have typechecks
* enabled. It should only be used by frequently called core modules that are
* already checking their params and return values.
*/
$.unsafe = getRequiredElement;

module.exports = $;
23 changes: 10 additions & 13 deletions src/vendor/core/createArrayFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
* @typechecks
*/

var toArray = require('toArray');

/**
* NOTE: if you are a previous user of this function, it has been considered
* unsafe because it's inconsistent across browsers for some inputs.
* Instead use `Array.isArray()`.
*
* Perform a heuristic test to determine if an object is "array-like".
*
* A monk asked Joshu, a Zen master, "Has a dog Buddha nature?"
Expand All @@ -31,6 +29,8 @@
* true if the argument is an actual array, an `arguments' object, or an
* HTMLCollection (e.g. node.childNodes or node.getElementsByTagName()).
*
* It will return false for other array-like objects like Filelist.
*
* @param {*} obj
* @return {boolean}
*/
Expand Down Expand Up @@ -73,23 +73,20 @@ function hasArrayNature(obj) {
*
* This allows you to treat `things' as an array, but accept scalars in the API.
*
* This is also good for converting certain pseudo-arrays, like `arguments` or
* HTMLCollections, into arrays.
* If you need to convert an array-like object, like `arguments`, into an array
* use toArray instead.
*
* @param {*} obj
* @return {array}
*/
function createArrayFrom(obj) {
if (!hasArrayNature(obj)) {
return [obj];
} else if (Array.isArray(obj)) {
return obj.slice();
} else {
return toArray(obj);
}
if (obj.item) {
// IE does not support Array#slice on HTMLCollections
var l = obj.length, ret = new Array(l);
while (l--) { ret[l] = obj[l]; }
return ret;
}
return Array.prototype.slice.call(obj);
}

module.exports = createArrayFrom;
7 changes: 4 additions & 3 deletions src/vendor/core/dom/getActiveElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
/**
* Same as document.activeElement but wraps in a try-catch block. In IE it is
* not safe to call document.activeElement if there is nothing focused.
*
* The activeElement will be null only if the document body is not yet defined.
*/
function getActiveElement() /*?DOMElement*/ {
try {
return document.activeElement;
return document.activeElement || document.body;
} catch (e) {
return null;
return document.body;
}
}

module.exports = getActiveElement;

76 changes: 0 additions & 76 deletions src/vendor/core/ge.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/vendor/core/getMarkupWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var shouldWrap = {
'g': true,
'line': true,
'path': true,
'polygon': true,
'polyline': true,
'rect': true,
'text': true
Expand Down Expand Up @@ -75,6 +76,7 @@ var markupWrap = {
'g': svgWrap,
'line': svgWrap,
'path': svgWrap,
'polygon': svgWrap,
'polyline': svgWrap,
'rect': svgWrap,
'text': svgWrap
Expand Down
27 changes: 9 additions & 18 deletions src/vendor/core/invariant.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
/**
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule invariant
*/

/**
* Use invariant() to assert state which your program assumes to be true.
*
* Provide sprintf style format and arguments to provide information about
* what broke and what you were expecting.
* Provide sprintf-style format (only %s is supported) and arguments
* to provide information about what broke and what you were
* expecting.
*
* The invariant message will be stripped in production, but the invariant
* will remain to ensure logic does not differ in production.
*/

function invariant(condition) {
if (!condition) {
throw new Error('Invariant Violation');
var error = new Error('Invariant Violation');
error.framesToPop = 1;
throw error;
}
}

Expand All @@ -43,10 +32,12 @@ if (__DEV__) {
if (!condition) {
var args = [a, b, c, d, e, f];
var argIndex = 0;
throw new Error(
var error = new Error(
'Invariant Violation: ' +
format.replace(/%s/g, function() { return args[argIndex++]; })
);
error.framesToPop = 1; // we don't care about invariant's own frame
throw error;
}
};

Expand Down
73 changes: 73 additions & 0 deletions src/vendor/core/toArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule toArray
* @typechecks
*/

var invariant = require('invariant');

/**
* Convert array-like objects to arrays.
*
* This API assumes the caller knows the contents of the data type. For less
* well defined inputs use createArrayFrom.
*
* @param {object|function} obj
* @return {array}
*/
function toArray(obj) {
var length = obj.length;

// Some browse builtin objects can report typeof 'function' (e.g. NodeList in
// old versions of Safari).
invariant(
!Array.isArray(obj) &&
(typeof obj === 'object' || typeof obj === 'function'),
'toArray: Array-like object expected'
);

invariant(
typeof length === 'number',
'toArray: Object needs a length property'
);

invariant(
length === 0 ||
(length - 1) in obj,
'toArray: Object should have keys for indices'
);

// Old IE doesn't give collections access to hasOwnProperty. Assume inputs
// without method will throw during the slice call and skip straight to the
// fallback.
if (obj.hasOwnProperty) {
try {
return Array.prototype.slice.call(obj);
} catch (e) {
// IE < 9 does not support Array#slice on collections objects
}
}

// Fall back to copying key by key. This assumes all keys have a value,
// so will not preserve sparsely populated inputs.
var ret = Array(length);
for (var ii = 0; ii < length; ii++) {
ret[ii] = obj[ii];
}
return ret;
}

module.exports = toArray;

0 comments on commit c2be8ba

Please sign in to comment.