Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: patch MutationObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 17, 2014
1 parent 8827e06 commit 1c4e85e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
36 changes: 36 additions & 0 deletions test/zone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,42 @@ describe('Zone.patch', function () {

});

describe('MutationObserver', function () {
it('should work', function () {
if (!window.MutationObserver) {
console.log('WARNING: skipping MutationObserver test (missing this API)');
return;
}

var flag = false,
elt = document.createElement('div'),
hasParent;

runs(function () {
var ob = new MutationObserver(function () {
hasParent = !!window.zone.parent;
dump('yo')
flag = true;
});

ob.observe(elt, {
childList: true
});

elt.innerHTML = '<p>hey</p>';
});

waitsFor(function() {
return flag;
}, 'promise to resolve', 100);

runs(function() {
expect(hasParent).toBe(true);
});

});
});

describe('XMLHttpRequest', function () {

it('should work with onreadystatechange', function () {
Expand Down
18 changes: 16 additions & 2 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ Zone.patch = function patch () {
'catch'
]);
}
if (window.MutationObserver) {
Zone.patchClass('MutationObserver');
}
};

//
Expand Down Expand Up @@ -315,11 +318,22 @@ Zone.patchViaCapturingAllTheEvents = function () {
// TODO: wrap some native API
Zone.patchClass = function (className) {
var OriginalClass = window[className];
if (!OriginalClass) {
return;
}
window[className] = function () {
this._o = new OriginalClass();
var a = Zone.bindArguments(arguments);
switch (a.length) {
case 0: this._o = new OriginalClass(); break;
case 1: this._o = new OriginalClass(a[0]); break;
case 2: this._o = new OriginalClass(a[0], a[1]); break;
case 3: this._o = new OriginalClass(a[0], a[1], a[2]); break;
case 4: this._o = new OriginalClass(a[0], a[1], a[2], a[3]); break;
default: throw new Error('what are you even doing?');
}
};

var instance = (new OriginalClass());
var instance = new OriginalClass(className === 'MutationObserver' ? function () {} : undefined);

var prop;
for (prop in instance) {
Expand Down

0 comments on commit 1c4e85e

Please sign in to comment.