Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #476 from ochameau/bug/769006-fix-MutationObserver

Bug: 769006: Ensure that MutationObserver works in content scripts. r=@gozala
  • Loading branch information...
commit 7e78b8a7916d51becedc0dcb6f71b0e5b34eb514 2 parents 983a9ef + 5d86f98
ochameau authored July 07, 2012
29  packages/api-utils/lib/content/content-proxy.js
@@ -542,7 +542,7 @@ const xRayWrappersMissFixes = [
542 542
   // Trap access to form["node name"]
543 543
   // http://mxr.mozilla.org/mozilla-central/source/dom/base/nsDOMClassInfo.cpp#9477
544 544
   function (obj, name) {
545  
-    if (typeof obj == "object" && obj.tagName == "FORM") {
  545
+    if (typeof obj == "object" && "tagName" in obj && obj.tagName == "FORM") {
546 546
       let match = obj.wrappedJSObject[name];
547 547
       let nodes = obj.ownerDocument.getElementsByName(name);
548 548
       for (let i = 0, l = nodes.length; i < l; i++) {
@@ -660,6 +660,33 @@ const xRayWrappersMethodsFixes = {
660 660
     };
661 661
 
662 662
     return getProxyForFunction(f, NativeFunctionWrapper(f));
  663
+  },
  664
+
  665
+  // Bug 769006: nsIDOMMutationObserver.observe fails with proxy as options
  666
+  // attributes
  667
+  observe: function observe(obj) {
  668
+    // Ensure that we are on a DOMMutation object
  669
+    try {
  670
+      // nsIDOMMutationObserver starts with FF14
  671
+      if ("nsIDOMMutationObserver" in Ci)
  672
+        obj.QueryInterface(Ci.nsIDOMMutationObserver);
  673
+      else
  674
+        return null;
  675
+    }
  676
+    catch(e) {
  677
+      return null;
  678
+    }
  679
+    return function nsIDOMMutationObserverObserveFix(target, options) {
  680
+      // Gets native/unwrapped this
  681
+      let self = this && typeof this.valueOf == "function" ?
  682
+                 this.valueOf(UNWRAP_ACCESS_KEY) : this;
  683
+      // Unwrap the xraywrapper target out of JS proxy
  684
+      let targetXray = unwrap(target);
  685
+      // But do not wrap `options` through ContentScriptObjectWrapper
  686
+      let result = wrap(self.observe(targetXray, options));
  687
+      // Finally wrap result into JS proxies
  688
+      return wrap(result);
  689
+    };
663 690
   }
664 691
 };
665 692
 
39  packages/api-utils/tests/test-content-proxy.js
@@ -808,3 +808,42 @@ exports.testCrossDomainIframe = createProxyTest("", function (helper) {
808 808
   worker.postMessage("http://localhost:" + serverPort + "/");
809 809
 
810 810
 });
  811
+
  812
+// Bug 769006: Ensure that MutationObserver works fine with proxies
  813
+let html = '<a href="foo">link</a>';
  814
+exports.testMutationObvserver = createProxyTest(html, function (helper) {
  815
+
  816
+  helper.createWorker(
  817
+    'new ' + function ContentScriptScope() {
  818
+      if (typeof MutationObserver == "undefined") {
  819
+        assert(true, "No MutationObserver for this FF version");
  820
+        done();
  821
+        return;
  822
+      }
  823
+      let link = document.getElementsByTagName("a")[0];
  824
+
  825
+      // Register a Mutation observer
  826
+      let obs = new MutationObserver(function(mutations){
  827
+        // Ensure that mutation data are valid
  828
+        assert(mutations.length == 1, "only one attribute mutation");
  829
+        let mutation = mutations[0];
  830
+        assert(mutation.type == "attributes", "check `type`");
  831
+        assert(mutation.target == link, "check `target`");
  832
+        assert(mutation.attributeName == "href", "check `attributeName`");
  833
+        assert(mutation.oldValue == "foo", "check `oldValue`");
  834
+        obs.disconnect();
  835
+        done();
  836
+      });
  837
+      obs.observe(document, {
  838
+        subtree: true,
  839
+        attributes: true,
  840
+        attributeOldValue: true,
  841
+        attributeFilter: ["href"]
  842
+      });
  843
+
  844
+      // Modify the DOM
  845
+      link.setAttribute("href", "bar");
  846
+    }
  847
+  );
  848
+
  849
+});

0 notes on commit 7e78b8a

Please sign in to comment.
Something went wrong with that request. Please try again.