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 удалений

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

@ -1581,33 +1581,68 @@ SessionStoreService.prototype = {
let node = formNodes.iterateNext();
if (!node)
return null;
const MAX_GENERATED_XPATHS = 100;
let generatedCount = 0;
let data = {};
do {
let nId = node.id;
let hasDefaultValue = true;
let value;
// 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;
let id = node.id ? "#" + node.id : XPathHelper.generate(node);
if (node instanceof Ci.nsIDOMHTMLInputElement) {
if (node.type != "file")
data[id] = node.type == "checkbox" || node.type == "radio" ? node.checked : node.value;
else
data[id] = { type: "file", fileList: node.mozGetFileNameArray() };
if (node instanceof Ci.nsIDOMHTMLInputElement ||
node instanceof Ci.nsIDOMHTMLTextAreaElement) {
switch (node.type) {
case "checkbox":
case "radio":
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 {
let options = Array.map(node.options, function(aOpt, aIx) aOpt.selected ? aIx : -1);
data[id] = options.filter(function(aIx) aIx >= 0);
// <select>s with the multiple attribute are easier to determine the
// 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()));
return data;
},

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

@ -47,6 +47,14 @@ function test() {
let tab = gBrowser.addTab(testURL);
tab.linkedBrowser.addEventListener("load", function(aEvent) {
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);
let ss = Cc["@mozilla.org/browser/sessionstore;1"]
@ -56,7 +64,7 @@ function test() {
let countGood = 0, countBad = 0;
for each (let value in savedFormData) {
if (value == "save me")
if (value == expectedValue)
countGood++;
else
countBad++;

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

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