Bug 537289 - Only save form data for fields that aren't the default value [r=zeniko]

Note: This also modifies the test for bug 456342.
This commit is contained in:
Paul O’Shannessy 2010-03-19 12:28:35 -07:00
Родитель 7f5366ce89
Коммит 387eb612ce
3 изменённых файлов: 76 добавлений и 31 удалений

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

@ -1587,25 +1587,60 @@ SessionStoreService.prototype = {
let data = {}; let data = {};
do { do {
let nId = node.id;
let hasDefaultValue = true;
let value;
// Only generate a limited number of XPath expressions for perf reasons (cf. bug 477564) // Only generate a limited number of XPath expressions for perf reasons (cf. bug 477564)
if (!node.id && ++generatedCount > MAX_GENERATED_XPATHS) if (!nId && generatedCount > MAX_GENERATED_XPATHS)
continue; continue;
let id = node.id ? "#" + node.id : XPathHelper.generate(node); if (node instanceof Ci.nsIDOMHTMLInputElement ||
if (node instanceof Ci.nsIDOMHTMLInputElement) { node instanceof Ci.nsIDOMHTMLTextAreaElement) {
if (node.type != "file") switch (node.type) {
data[id] = node.type == "checkbox" || node.type == "radio" ? node.checked : node.value; case "checkbox":
else case "radio":
data[id] = { type: "file", fileList: node.mozGetFileNameArray() }; value = node.checked;
hasDefaultValue = value == node.defaultChecked;
break;
case "file":
value = { type: "file", fileList: node.mozGetFileNameArray() };
hasDefaultValue = !value.fileList.length;
break;
default: // text, textarea
value = node.value;
hasDefaultValue = value == node.defaultValue;
break;
}
}
else if (!node.multiple) {
// <select>s without the multiple attribute are hard to determine the
// default value, so assume we don't have the default.
hasDefaultValue = false;
value = node.selectedIndex;
} }
else if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
data[id] = node.value;
else if (!node.multiple)
data[id] = node.selectedIndex;
else { else {
let options = Array.map(node.options, function(aOpt, aIx) aOpt.selected ? aIx : -1); // <select>s with the multiple attribute are easier to determine the
data[id] = options.filter(function(aIx) aIx >= 0); // default value since each <option> has a defaultSelected
let options = Array.map(node.options, function(aOpt, aIx) {
let oSelected = aOpt.selected;
hasDefaultValue = hasDefaultValue && (oSelected == aOpt.defaultSelected);
return oSelected ? aIx : -1;
});
value = options.filter(function(aIx) aIx >= 0);
} }
// In order to reduce XPath generation (which is slow), we only save data
// for form fields that have been changed. (cf. bug 537289)
if (!hasDefaultValue) {
if (nId) {
data["#" + nId] = value;
}
else {
generatedCount++;
data[XPathHelper.generate(node)] = value;
}
}
} while ((node = formNodes.iterateNext())); } while ((node = formNodes.iterateNext()));
return data; return data;

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

@ -47,6 +47,14 @@ function test() {
let tab = gBrowser.addTab(testURL); let tab = gBrowser.addTab(testURL);
tab.linkedBrowser.addEventListener("load", function(aEvent) { tab.linkedBrowser.addEventListener("load", function(aEvent) {
this.removeEventListener("load", arguments.callee, true); this.removeEventListener("load", arguments.callee, true);
let expectedValue = "try to save me";
// Since bug 537289 we only save non-default values, so we need to set each
// form field's value after load.
let formEls = aEvent.originalTarget.forms[0].elements;
for (let i = 0; i < formEls.length; i++)
formEls[i].value = expectedValue;
gBrowser.removeTab(tab); gBrowser.removeTab(tab);
let ss = Cc["@mozilla.org/browser/sessionstore;1"] let ss = Cc["@mozilla.org/browser/sessionstore;1"]
@ -56,7 +64,7 @@ function test() {
let countGood = 0, countBad = 0; let countGood = 0, countBad = 0;
for each (let value in savedFormData) { for each (let value in savedFormData) {
if (value == "save me") if (value == expectedValue)
countGood++; countGood++;
else else
countBad++; countBad++;

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

@ -5,22 +5,24 @@
<head><title>Test for bug 456342</title></head> <head><title>Test for bug 456342</title></head>
<body> <body>
<form>
<h3>Non-standard &lt;input&gt;s</h3> <h3>Non-standard &lt;input&gt;s</h3>
<p>Search <input type="search" value="save me" id="searchTerm"/></p> <p>Search <input type="search" id="searchTerm"/></p>
<p>Image Search: <input type="image search" value="save me" /></p> <p>Image Search: <input type="image search" /></p>
<p>Autocomplete: <input type="autocomplete" value="save me" name="fill-in"/></p> <p>Autocomplete: <input type="autocomplete" name="fill-in"/></p>
<p>Mistyped: <input type="txet" value="save me" name="mistyped"/></p> <p>Mistyped: <input type="txet" name="mistyped"/></p>
<h3>Ignored types</h3> <h3>Ignored types</h3>
<input type="hidden" value="don't save" name="hideme"/> <input type="hidden" name="hideme"/>
<input type="HIDDEN" value="don't save" name="hideme2"/> <input type="HIDDEN" name="hideme2"/>
<input type="submit" value="don't save" name="submit"/> <input type="submit" name="submit"/>
<input type="reset" value="don't save" name="reset"/> <input type="reset" name="reset"/>
<input type="image" value="don't save" name="image"/> <input type="image" name="image"/>
<input type="button" value="don't save" name="button"/> <input type="button" name="button"/>
<input type="password" value="don't save" name="password"/> <input type="password" name="password"/>
<input type="PassWord" value="don't save" name="password2"/> <input type="PassWord" name="password2"/>
<input type="PASSWORD" value="don't save" name="password3"/> <input type="PASSWORD" name="password3"/>
</form>
</body> </body>
</html> </html>