/* 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/. */ this.EXPORTED_SYMBOLS = [ "DocumentUtils" ]; const Cu = Components.utils; const Ci = Components.interfaces; Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource:///modules/sessionstore/XPathGenerator.jsm"); this.DocumentUtils = { /** * Obtain form data for a DOMDocument instance. * * The returned object has 2 keys, "id" and "xpath". Each key holds an object * which further defines form data. * * The "id" object maps element IDs to values. The "xpath" object maps the * XPath of an element to its value. * * @param aDocument * DOMDocument instance to obtain form data for. * @return object * Form data encoded in an object. */ getFormData: function(aDocument) { let formNodes = aDocument.evaluate( XPathGenerator.restorableFormNodes, aDocument, XPathGenerator.resolveNS, Ci.nsIDOMXPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); let node; let ret = {id: {}, xpath: {}}; // Limit the number of XPath expressions for performance reasons. See // bug 477564. const MAX_TRAVERSED_XPATHS = 100; let generatedCount = 0; while (node = formNodes.iterateNext()) { let nId = node.id; let hasDefaultValue = true; let value; // Only generate a limited number of XPath expressions for perf reasons // (cf. bug 477564) if (!nId && generatedCount > MAX_TRAVERSED_XPATHS) { continue; } 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) { // s with the multiple attribute are easier to determine the // default value since each