Bug 987177 - invalidate wrapper's node reference, r=jaws

This commit is contained in:
Gijs Kruitbosch 2014-03-26 00:28:58 +00:00
parent f232797b24
commit e43ba3332f
3 changed files with 121 additions and 8 deletions

View File

@ -3430,7 +3430,7 @@ function XULWidgetGroupWrapper(aWidgetId) {
instance = aWindow.gNavToolbox.palette.getElementsByAttribute("id", aWidgetId)[0];
}
let wrapper = new XULWidgetSingleWrapper(aWidgetId, instance);
let wrapper = new XULWidgetSingleWrapper(aWidgetId, instance, aWindow.document);
wrapperMap.set(aWidgetId, wrapper);
return wrapper;
};
@ -3456,14 +3456,47 @@ function XULWidgetGroupWrapper(aWidgetId) {
* A XULWidgetSingleWrapper is a wrapper around a single instance of a XUL
* widget in a particular window.
*/
function XULWidgetSingleWrapper(aWidgetId, aNode) {
function XULWidgetSingleWrapper(aWidgetId, aNode, aDocument) {
this.isGroup = false;
this.id = aWidgetId;
this.type = "custom";
this.provider = CustomizableUI.PROVIDER_XUL;
this.node = aNode;
let weakDoc = Cu.getWeakReference(aDocument);
// If we keep a strong ref, the weak ref will never die, so null it out:
aDocument = null;
this.__defineGetter__("node", function() {
// If we've set this to null (further down), we're sure there's nothing to
// be gotten here, so bail out early:
if (!weakDoc) {
return null;
}
if (aNode) {
// Return the last known node if it's still in the DOM...
if (aNode.ownerDocument.contains(aNode)) {
return aNode;
}
// ... or the toolbox
let toolbox = aNode.ownerDocument.defaultView.gNavToolbox;
if (toolbox && toolbox.palette && aNode.parentNode == toolbox.palette) {
return aNode;
}
// If it isn't, clear the cached value and fall through to the "slow" case:
aNode = null;
}
let doc = weakDoc.get();
if (doc) {
// Store locally so we can cache the result:
aNode = CustomizableUIInternal.findWidgetInWindow(aWidgetId, doc.defaultView);
return aNode;
}
// The weakref to the document is dead, we're done here forever more:
weakDoc = null;
return null;
});
this.__defineGetter__("anchor", function() {
let anchorId;
@ -3472,16 +3505,21 @@ function XULWidgetSingleWrapper(aWidgetId, aNode) {
if (placement) {
anchorId = gAreas.get(placement.area).get("anchor");
}
if (!anchorId) {
anchorId = aNode.getAttribute("cui-anchorid");
let node = this.node;
if (!anchorId && node) {
anchorId = node.getAttribute("cui-anchorid");
}
return anchorId ? aNode.ownerDocument.getElementById(anchorId)
: aNode;
return (anchorId && node) ? node.ownerDocument.getElementById(anchorId) : node;
});
this.__defineGetter__("overflowed", function() {
return aNode.getAttribute("overflowedItem") == "true";
let node = this.node;
if (!node) {
return false;
}
return node.getAttribute("overflowedItem") == "true";
});
Object.freeze(this);

View File

@ -90,4 +90,5 @@ skip-if = os == "linux"
[browser_981418-widget-onbeforecreated-handler.js]
[browser_985815_propagate_setToolbarVisibility.js]
[browser_981305_separator_insertion.js]
[browser_987177_xul_wrapper_updating.js]
[browser_panel_toggle.js]

View File

@ -0,0 +1,74 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const BUTTONID = "test-XUL-wrapper-widget";
add_task(function() {
let btn = createDummyXULButton(BUTTONID, "XUL btn");
gNavToolbox.palette.appendChild(btn);
let groupWrapper = CustomizableUI.getWidget(BUTTONID);
ok(groupWrapper, "Should get a group wrapper");
let singleWrapper = groupWrapper.forWindow(window);
ok(singleWrapper, "Should get a single wrapper");
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
CustomizableUI.addWidgetToArea(BUTTONID, CustomizableUI.AREA_NAVBAR);
let otherSingleWrapper = groupWrapper.forWindow(window);
is(singleWrapper, otherSingleWrapper, "Should get the same wrapper after adding the node to the navbar.");
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
CustomizableUI.removeWidgetFromArea(BUTTONID);
otherSingleWrapper = groupWrapper.forWindow(window);
isnot(singleWrapper, otherSingleWrapper, "Shouldn't get the same wrapper after removing it from the navbar.");
singleWrapper = otherSingleWrapper;
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
btn.remove();
otherSingleWrapper = groupWrapper.forWindow(window);
is(singleWrapper, otherSingleWrapper, "Should get the same wrapper after physically removing the node.");
is(singleWrapper.node, null, "Wrapper's node should be null now that it's left the DOM.");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, null, "That instance should be null.");
btn = createDummyXULButton(BUTTONID, "XUL btn");
gNavToolbox.palette.appendChild(btn);
otherSingleWrapper = groupWrapper.forWindow(window);
is(singleWrapper, otherSingleWrapper, "Should get the same wrapper after readding the node.");
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
CustomizableUI.addWidgetToArea(BUTTONID, CustomizableUI.AREA_NAVBAR);
otherSingleWrapper = groupWrapper.forWindow(window);
is(singleWrapper, otherSingleWrapper, "Should get the same wrapper after adding the node to the navbar.");
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
CustomizableUI.removeWidgetFromArea(BUTTONID);
otherSingleWrapper = groupWrapper.forWindow(window);
isnot(singleWrapper, otherSingleWrapper, "Shouldn't get the same wrapper after removing it from the navbar.");
singleWrapper = otherSingleWrapper;
is(singleWrapper.node, btn, "Node should be in the wrapper");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, btn, "Button should be that instance.");
btn.remove();
otherSingleWrapper = groupWrapper.forWindow(window);
is(singleWrapper, otherSingleWrapper, "Should get the same wrapper after physically removing the node.");
is(singleWrapper.node, null, "Wrapper's node should be null now that it's left the DOM.");
is(groupWrapper.instances.length, 1, "There should be 1 instance on the group wrapper");
is(groupWrapper.instances[0].node, null, "That instance should be null.");
});