Ensure optimized isPlainObject works with objects from other documents.

Former-commit-id: 2f782b3dfc19e7ea3274132c31cd408ee2387021
This commit is contained in:
John-David Dalton
2012-09-02 02:35:02 -07:00
parent c2117ef4fd
commit f3bec4fc37
4 changed files with 75 additions and 71 deletions

View File

@@ -33,7 +33,7 @@
delete Object._keys;
// set to test `_.noConflict`
_ = 1;
_ = {};
// load Lo-Dash again to overwrite the existing `_` value
document.write('<script src="../' + QUnit.config.lodashFilename + '.js"><\/script>');

View File

@@ -83,6 +83,23 @@
/*--------------------------------------------------------------------------*/
// add object from iframe
(function() {
if (!window.document) {
return;
}
var body = document.body,
iframe = document.createElement('iframe');
iframe.frameBorder = iframe.height = iframe.width = 0;
body.appendChild(iframe);
var idoc = (idoc = iframe.contentDocument || iframe.contentWindow).document || idoc;
idoc.write("<script>parent._._object = { 'a': 1, 'b': 2, 'c': 3 };<\/script>");
idoc.close();
}());
/*--------------------------------------------------------------------------*/
// explicitly call `QUnit.module()` instead of `module()`
// in case we are in a CLI environment
QUnit.module('lodash');
@@ -191,6 +208,7 @@
'boolean object': Object(false),
'an object': { 'a': 0, 'b': 1, 'c': 3 },
'an object with object values': { 'a': /a/, 'b': ['B'], 'c': { 'C': 1 } },
'an object from another document': _._object || {},
'null': null,
'a number': 3,
'a number object': Object(3),
@@ -205,12 +223,8 @@
_.forOwn(objects, function(object, key) {
test('should deep clone ' + key + ' correctly', function() {
var clone = _.clone(object, true);
ok(_.isEqual(object, clone));
if (object == null) {
equal(clone, object);
} else {
deepEqual(clone.valueOf(), object.valueOf());
}
if (_.isObject(object)) {
ok(clone !== object);
} else {
@@ -747,21 +761,10 @@
});
test('should return `true` for like-objects from different documents', function() {
if (window.document) {
var body = document.body,
iframe = document.createElement('iframe'),
object = { 'a': 1, 'b': 2, 'c': 3 };
body.appendChild(iframe);
var idoc = (idoc = iframe.contentDocument || iframe.contentWindow).document || idoc;
idoc.write("<script>parent._._object = { 'a': 1, 'b': 2, 'c': 3 };<\/script>");
idoc.close();
}
// ensure `_._object` is assigned (unassigned in Opera 10.00)
if (_._object) {
var object = { 'a': 1, 'b': 2, 'c': 3 };
equal(_.isEqual(object, _._object), true);
body.removeChild(iframe);
delete _._object;
}
else {
skipTest();