Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | "use strict"; |
michael@0 | 7 | |
michael@0 | 8 | this.EXPORTED_SYMBOLS = [ |
michael@0 | 9 | "_", |
michael@0 | 10 | "assert", |
michael@0 | 11 | "log", |
michael@0 | 12 | "text", |
michael@0 | 13 | "wire", |
michael@0 | 14 | "showFilePicker" |
michael@0 | 15 | ]; |
michael@0 | 16 | |
michael@0 | 17 | const Cc = Components.classes; |
michael@0 | 18 | const Ci = Components.interfaces; |
michael@0 | 19 | const Cu = Components.utils; |
michael@0 | 20 | |
michael@0 | 21 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 22 | |
michael@0 | 23 | const PROPERTIES_URL = "chrome://browser/locale/devtools/styleeditor.properties"; |
michael@0 | 24 | |
michael@0 | 25 | const console = Services.console; |
michael@0 | 26 | const gStringBundle = Services.strings.createBundle(PROPERTIES_URL); |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * Returns a localized string with the given key name from the string bundle. |
michael@0 | 31 | * |
michael@0 | 32 | * @param aName |
michael@0 | 33 | * @param ...rest |
michael@0 | 34 | * Optional arguments to format in the string. |
michael@0 | 35 | * @return string |
michael@0 | 36 | */ |
michael@0 | 37 | this._ = function _(aName) |
michael@0 | 38 | { |
michael@0 | 39 | |
michael@0 | 40 | if (arguments.length == 1) { |
michael@0 | 41 | return gStringBundle.GetStringFromName(aName); |
michael@0 | 42 | } |
michael@0 | 43 | let rest = Array.prototype.slice.call(arguments, 1); |
michael@0 | 44 | return gStringBundle.formatStringFromName(aName, rest, rest.length); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | /** |
michael@0 | 48 | * Assert an expression is true or throw if false. |
michael@0 | 49 | * |
michael@0 | 50 | * @param aExpression |
michael@0 | 51 | * @param aMessage |
michael@0 | 52 | * Optional message. |
michael@0 | 53 | * @return aExpression |
michael@0 | 54 | */ |
michael@0 | 55 | this.assert = function assert(aExpression, aMessage) |
michael@0 | 56 | { |
michael@0 | 57 | if (!!!(aExpression)) { |
michael@0 | 58 | let msg = aMessage ? "ASSERTION FAILURE:" + aMessage : "ASSERTION FAILURE"; |
michael@0 | 59 | log(msg); |
michael@0 | 60 | throw new Error(msg); |
michael@0 | 61 | } |
michael@0 | 62 | return aExpression; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Retrieve or set the text content of an element. |
michael@0 | 67 | * |
michael@0 | 68 | * @param DOMElement aRoot |
michael@0 | 69 | * The element to use for querySelector. |
michael@0 | 70 | * @param string aSelector |
michael@0 | 71 | * Selector string for the element to get/set the text content. |
michael@0 | 72 | * @param string aText |
michael@0 | 73 | * Optional text to set. |
michael@0 | 74 | * @return string |
michael@0 | 75 | * Text content of matching element or null if there were no element |
michael@0 | 76 | * matching aSelector. |
michael@0 | 77 | */ |
michael@0 | 78 | this.text = function text(aRoot, aSelector, aText) |
michael@0 | 79 | { |
michael@0 | 80 | let element = aRoot.querySelector(aSelector); |
michael@0 | 81 | if (!element) { |
michael@0 | 82 | return null; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if (aText === undefined) { |
michael@0 | 86 | return element.textContent; |
michael@0 | 87 | } |
michael@0 | 88 | element.textContent = aText; |
michael@0 | 89 | return aText; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Iterates _own_ properties of an object. |
michael@0 | 94 | * |
michael@0 | 95 | * @param aObject |
michael@0 | 96 | * The object to iterate. |
michael@0 | 97 | * @param function aCallback(aKey, aValue) |
michael@0 | 98 | */ |
michael@0 | 99 | function forEach(aObject, aCallback) |
michael@0 | 100 | { |
michael@0 | 101 | for (let key in aObject) { |
michael@0 | 102 | if (aObject.hasOwnProperty(key)) { |
michael@0 | 103 | aCallback(key, aObject[key]); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Log a message to the console. |
michael@0 | 110 | * |
michael@0 | 111 | * @param ...rest |
michael@0 | 112 | * One or multiple arguments to log. |
michael@0 | 113 | * If multiple arguments are given, they will be joined by " " in the log. |
michael@0 | 114 | */ |
michael@0 | 115 | this.log = function log() |
michael@0 | 116 | { |
michael@0 | 117 | console.logStringMessage(Array.prototype.slice.call(arguments).join(" ")); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | /** |
michael@0 | 121 | * Wire up element(s) matching selector with attributes, event listeners, etc. |
michael@0 | 122 | * |
michael@0 | 123 | * @param DOMElement aRoot |
michael@0 | 124 | * The element to use for querySelectorAll. |
michael@0 | 125 | * Can be null if aSelector is a DOMElement. |
michael@0 | 126 | * @param string|DOMElement aSelectorOrElement |
michael@0 | 127 | * Selector string or DOMElement for the element(s) to wire up. |
michael@0 | 128 | * @param object aDescriptor |
michael@0 | 129 | * An object describing how to wire matching selector, supported properties |
michael@0 | 130 | * are "events" and "attributes" taking objects themselves. |
michael@0 | 131 | * Each key of properties above represents the name of the event or |
michael@0 | 132 | * attribute, with the value being a function used as an event handler or |
michael@0 | 133 | * string to use as attribute value. |
michael@0 | 134 | * If aDescriptor is a function, the argument is equivalent to : |
michael@0 | 135 | * {events: {'click': aDescriptor}} |
michael@0 | 136 | */ |
michael@0 | 137 | this.wire = function wire(aRoot, aSelectorOrElement, aDescriptor) |
michael@0 | 138 | { |
michael@0 | 139 | let matches; |
michael@0 | 140 | if (typeof(aSelectorOrElement) == "string") { // selector |
michael@0 | 141 | matches = aRoot.querySelectorAll(aSelectorOrElement); |
michael@0 | 142 | if (!matches.length) { |
michael@0 | 143 | return; |
michael@0 | 144 | } |
michael@0 | 145 | } else { |
michael@0 | 146 | matches = [aSelectorOrElement]; // element |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | if (typeof(aDescriptor) == "function") { |
michael@0 | 150 | aDescriptor = {events: {click: aDescriptor}}; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | for (let i = 0; i < matches.length; i++) { |
michael@0 | 154 | let element = matches[i]; |
michael@0 | 155 | forEach(aDescriptor.events, function (aName, aHandler) { |
michael@0 | 156 | element.addEventListener(aName, aHandler, false); |
michael@0 | 157 | }); |
michael@0 | 158 | forEach(aDescriptor.attributes, element.setAttribute); |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * Show file picker and return the file user selected. |
michael@0 | 164 | * |
michael@0 | 165 | * @param mixed file |
michael@0 | 166 | * Optional nsIFile or string representing the filename to auto-select. |
michael@0 | 167 | * @param boolean toSave |
michael@0 | 168 | * If true, the user is selecting a filename to save. |
michael@0 | 169 | * @param nsIWindow parentWindow |
michael@0 | 170 | * Optional parent window. If null the parent window of the file picker |
michael@0 | 171 | * will be the window of the attached input element. |
michael@0 | 172 | * @param callback |
michael@0 | 173 | * The callback method, which will be called passing in the selected |
michael@0 | 174 | * file or null if the user did not pick one. |
michael@0 | 175 | * @param AString suggestedFilename |
michael@0 | 176 | * The suggested filename when toSave is true. |
michael@0 | 177 | */ |
michael@0 | 178 | this.showFilePicker = function showFilePicker(path, toSave, parentWindow, |
michael@0 | 179 | callback, suggestedFilename) |
michael@0 | 180 | { |
michael@0 | 181 | if (typeof(path) == "string") { |
michael@0 | 182 | try { |
michael@0 | 183 | if (Services.io.extractScheme(path) == "file") { |
michael@0 | 184 | let uri = Services.io.newURI(path, null, null); |
michael@0 | 185 | let file = uri.QueryInterface(Ci.nsIFileURL).file; |
michael@0 | 186 | callback(file); |
michael@0 | 187 | return; |
michael@0 | 188 | } |
michael@0 | 189 | } catch (ex) { |
michael@0 | 190 | callback(null); |
michael@0 | 191 | return; |
michael@0 | 192 | } |
michael@0 | 193 | try { |
michael@0 | 194 | let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); |
michael@0 | 195 | file.initWithPath(path); |
michael@0 | 196 | callback(file); |
michael@0 | 197 | return; |
michael@0 | 198 | } catch (ex) { |
michael@0 | 199 | callback(null); |
michael@0 | 200 | return; |
michael@0 | 201 | } |
michael@0 | 202 | } |
michael@0 | 203 | if (path) { // "path" is an nsIFile |
michael@0 | 204 | callback(path); |
michael@0 | 205 | return; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); |
michael@0 | 209 | let mode = toSave ? fp.modeSave : fp.modeOpen; |
michael@0 | 210 | let key = toSave ? "saveStyleSheet" : "importStyleSheet"; |
michael@0 | 211 | let fpCallback = function(result) { |
michael@0 | 212 | if (result == Ci.nsIFilePicker.returnCancel) { |
michael@0 | 213 | callback(null); |
michael@0 | 214 | } else { |
michael@0 | 215 | callback(fp.file); |
michael@0 | 216 | } |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | if (toSave && suggestedFilename) { |
michael@0 | 220 | fp.defaultString = suggestedFilename; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | fp.init(parentWindow, _(key + ".title"), mode); |
michael@0 | 224 | fp.appendFilters(_(key + ".filter"), "*.css"); |
michael@0 | 225 | fp.appendFilters(fp.filterAll); |
michael@0 | 226 | fp.open(fpCallback); |
michael@0 | 227 | return; |
michael@0 | 228 | } |