Bug 1476030 - Part 2 - Implement xulStore.persist as an alternative to document.persist;r=Gijs

MozReview-Commit-ID: CDK4I3189mN

--HG--
extra : rebase_source : 227e20b5e3a58c0dc9dc6a87ea2b0894b9b6872e
This commit is contained in:
Brian Grinstead 2018-07-18 09:43:40 -07:00
Родитель 8ceaae11ca
Коммит 681b3ba7f6
4 изменённых файлов: 54 добавлений и 16 удалений

Просмотреть файл

@ -83,8 +83,7 @@ XULStore.prototype = {
log(message) {
if (!debugMode)
return;
dump("XULStore: " + message + "\n");
Services.console.logStringMessage("XULStore: " + message);
console.log("XULStore: " + message);
},
readFile() {
@ -129,6 +128,30 @@ XULStore.prototype = {
/* ---------- interface implementation ---------- */
persist(node, attr) {
if (!node.id) {
throw new Error("Node without ID passed into persist()");
}
const uri = node.ownerDocument.documentURI;
const value = node.getAttribute(attr);
if (node.localName == "window") {
this.log("Persisting attributes to windows is handled by nsXULWindow.");
return;
}
// See Bug 1476680 - we could drop the `hasValue` check so that
// any time there's an empty attribute it gets removed from the
// store. Since this is copying behavior from document.persist,
// callers would need to be updated with that change.
if (!value && this.hasValue(uri, node.id, attr)) {
this.removeValue(uri, node.id, attr);
} else {
this.setValue(uri, node.id, attr, value);
}
},
setValue(docURI, id, attr, value) {
this.log("Saving " + attr + "=" + value + " for id=" + id + ", doc=" + docURI);

Просмотреть файл

@ -5,6 +5,7 @@
#include "nsISupports.idl"
interface nsIStringEnumerator;
webidl Node;
/**
* The XUL store is used to store information related to a XUL document/application.
@ -17,6 +18,18 @@ interface nsIStringEnumerator;
interface nsIXULStore: nsISupports
{
/**
* Sets a value for a specified node's attribute, except in
* the case below (following the original XULDocument::persist):
* If the value is empty and if calling `hasValue` with the node's
* document and ID and `attr` would return true, then the
* value instead gets removed from the store (see Bug 1476680).
*
* @param node - DOM node
* @param attr - attribute to store
*/
void persist(in Node aNode, in AString attr);
/**
* Sets a value in the store.
*

Просмотреть файл

@ -24,30 +24,32 @@ function opened()
function runTest()
{
var firstRun = window.arguments[0];
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
if (firstRun) {
document.getElementById("button1").setAttribute("value", "Pressed");
document.getElementById("button2").removeAttribute("value");
button1.setAttribute("value", "Pressed");
button2.removeAttribute("value");
document.getElementById("button2").setAttribute("foo", "bar");
document.persist("button2", "foo");
is(XULStore.getValue(URI, "button2", "foo"), "bar", "attribute persisted")
document.getElementById("button2").removeAttribute("foo");
document.persist("button2", "foo");
is(XULStore.hasValue(URI, "button2", "foo"), false, "attribute removed")
button2.setAttribute("foo", "bar");
XULStore.persist(button2, "foo");
is(XULStore.getValue(URI, "button2", "foo"), "bar", "attribute persisted");
button2.removeAttribute("foo");
XULStore.persist(button2, "foo");
is(XULStore.hasValue(URI, "button2", "foo"), false, "attribute removed");
window.close();
window.opener.windowOpened();
}
else {
is(document.getElementById("button1").getAttribute("value"), "Pressed",
is(button1.getAttribute("value"), "Pressed",
"Attribute set");
is(document.getElementById("button2").hasAttribute("value"), true,
is(button2.hasAttribute("value"), true,
"Attribute cleared");
is(document.getElementById("button2").getAttribute("value"), "",
is(button2.getAttribute("value"), "",
"Attribute cleared");
is(document.getElementById("button2").hasAttribute("foo"), false,
is(button2.hasAttribute("foo"), false,
"Attribute cleared");
is(document.getElementById("button2").getAttribute("foo"), "",
is(button2.getAttribute("foo"), "",
"Attribute cleared");
window.close();

Просмотреть файл

@ -11,7 +11,7 @@ async function test() {
let aWindow = await open_manager(null);
var categories = aWindow.document.getElementById("categories");
categories.setAttribute("last-selected", "foo");
aWindow.document.persist("categories", "last-selected");
Services.xulStore.persist(categories, "last-selected");
await close_manager(aWindow);
Services.prefs.clearUserPref(PREF_UI_LASTCATEGORY);