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 | |
michael@0 | 2 | const nsISupports = Components.interfaces.nsISupports; |
michael@0 | 3 | const nsIAutoCompleteResult = Components.interfaces.nsIAutoCompleteResult; |
michael@0 | 4 | const nsIAutoCompleteSearch = Components.interfaces.nsIAutoCompleteSearch; |
michael@0 | 5 | const nsIFactory = Components.interfaces.nsIFactory; |
michael@0 | 6 | const nsIUUIDGenerator = Components.interfaces.nsIUUIDGenerator; |
michael@0 | 7 | const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar; |
michael@0 | 8 | |
michael@0 | 9 | var gDefaultAutoCompleteSearch = null; |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * Register 'test-a11y-search' AutoCompleteSearch. |
michael@0 | 13 | * |
michael@0 | 14 | * @param aValues [in] set of possible results values |
michael@0 | 15 | * @param aComments [in] set of possible results descriptions |
michael@0 | 16 | */ |
michael@0 | 17 | function initAutoComplete(aValues, aComments) |
michael@0 | 18 | { |
michael@0 | 19 | var allResults = new ResultsHeap(aValues, aComments); |
michael@0 | 20 | gDefaultAutoCompleteSearch = |
michael@0 | 21 | new AutoCompleteSearch("test-a11y-search", allResults); |
michael@0 | 22 | registerAutoCompleteSearch(gDefaultAutoCompleteSearch, |
michael@0 | 23 | "Accessibility Test AutoCompleteSearch"); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * Unregister 'test-a11y-search' AutoCompleteSearch. |
michael@0 | 28 | */ |
michael@0 | 29 | function shutdownAutoComplete() |
michael@0 | 30 | { |
michael@0 | 31 | unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch); |
michael@0 | 32 | gDefaultAutoCompleteSearch.cid = null; |
michael@0 | 33 | gDefaultAutoCompleteSearch = null; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Register the given AutoCompleteSearch. |
michael@0 | 39 | * |
michael@0 | 40 | * @param aSearch [in] AutoCompleteSearch object |
michael@0 | 41 | * @param aDescription [in] description of the search object |
michael@0 | 42 | */ |
michael@0 | 43 | function registerAutoCompleteSearch(aSearch, aDescription) |
michael@0 | 44 | { |
michael@0 | 45 | var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
michael@0 | 46 | |
michael@0 | 47 | var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"]. |
michael@0 | 48 | getService(nsIUUIDGenerator); |
michael@0 | 49 | var cid = uuidGenerator.generateUUID(); |
michael@0 | 50 | |
michael@0 | 51 | var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar); |
michael@0 | 52 | componentManager.registerFactory(cid, aDescription, name, aSearch); |
michael@0 | 53 | |
michael@0 | 54 | // Keep the id on the object so we can unregister later. |
michael@0 | 55 | aSearch.cid = cid; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Unregister the given AutoCompleteSearch. |
michael@0 | 60 | */ |
michael@0 | 61 | function unregisterAutoCompleteSearch(aSearch) |
michael@0 | 62 | { |
michael@0 | 63 | var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar); |
michael@0 | 64 | componentManager.unregisterFactory(aSearch.cid, aSearch); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * A container to keep all possible results of autocomplete search. |
michael@0 | 70 | */ |
michael@0 | 71 | function ResultsHeap(aValues, aComments) |
michael@0 | 72 | { |
michael@0 | 73 | this.values = aValues; |
michael@0 | 74 | this.comments = aComments; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | ResultsHeap.prototype = |
michael@0 | 78 | { |
michael@0 | 79 | constructor: ResultsHeap, |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Return AutoCompleteResult for the given search string. |
michael@0 | 83 | */ |
michael@0 | 84 | getAutoCompleteResultFor: function(aSearchString) |
michael@0 | 85 | { |
michael@0 | 86 | var values = [], comments = []; |
michael@0 | 87 | for (var idx = 0; idx < this.values.length; idx++) { |
michael@0 | 88 | if (this.values[idx].indexOf(aSearchString) != -1) { |
michael@0 | 89 | values.push(this.values[idx]); |
michael@0 | 90 | comments.push(this.comments[idx]); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | return new AutoCompleteResult(values, comments); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * nsIAutoCompleteSearch implementation. |
michael@0 | 100 | * |
michael@0 | 101 | * @param aName [in] the name of autocomplete search |
michael@0 | 102 | * @param aAllResults [in] ResultsHeap object |
michael@0 | 103 | */ |
michael@0 | 104 | function AutoCompleteSearch(aName, aAllResults) |
michael@0 | 105 | { |
michael@0 | 106 | this.name = aName; |
michael@0 | 107 | this.allResults = aAllResults; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | AutoCompleteSearch.prototype = |
michael@0 | 111 | { |
michael@0 | 112 | constructor: AutoCompleteSearch, |
michael@0 | 113 | |
michael@0 | 114 | // nsIAutoCompleteSearch implementation |
michael@0 | 115 | startSearch: function(aSearchString, aSearchParam, aPreviousResult, |
michael@0 | 116 | aListener) |
michael@0 | 117 | { |
michael@0 | 118 | var result = this.allResults.getAutoCompleteResultFor(aSearchString); |
michael@0 | 119 | aListener.onSearchResult(this, result); |
michael@0 | 120 | }, |
michael@0 | 121 | |
michael@0 | 122 | stopSearch: function() {}, |
michael@0 | 123 | |
michael@0 | 124 | // nsISupports implementation |
michael@0 | 125 | QueryInterface: function(iid) |
michael@0 | 126 | { |
michael@0 | 127 | if (iid.equals(nsISupports) || |
michael@0 | 128 | iid.equals(nsIFactory) || |
michael@0 | 129 | iid.equals(nsIAutoCompleteSearch)) |
michael@0 | 130 | return this; |
michael@0 | 131 | |
michael@0 | 132 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 133 | }, |
michael@0 | 134 | |
michael@0 | 135 | // nsIFactory implementation |
michael@0 | 136 | createInstance: function(outer, iid) |
michael@0 | 137 | { |
michael@0 | 138 | return this.QueryInterface(iid); |
michael@0 | 139 | }, |
michael@0 | 140 | |
michael@0 | 141 | // Search name. Used by AutoCompleteController. |
michael@0 | 142 | name: null, |
michael@0 | 143 | |
michael@0 | 144 | // Results heap. |
michael@0 | 145 | allResults: null |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * nsIAutoCompleteResult implementation. |
michael@0 | 151 | */ |
michael@0 | 152 | function AutoCompleteResult(aValues, aComments) |
michael@0 | 153 | { |
michael@0 | 154 | this.values = aValues; |
michael@0 | 155 | this.comments = aComments; |
michael@0 | 156 | |
michael@0 | 157 | if (this.values.length > 0) |
michael@0 | 158 | this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS; |
michael@0 | 159 | else |
michael@0 | 160 | this.searchResult = nsIAutoCompleteResult.NOMATCH; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | AutoCompleteResult.prototype = |
michael@0 | 164 | { |
michael@0 | 165 | constructor: AutoCompleteResult, |
michael@0 | 166 | |
michael@0 | 167 | searchString: "", |
michael@0 | 168 | searchResult: null, |
michael@0 | 169 | |
michael@0 | 170 | defaultIndex: 0, |
michael@0 | 171 | |
michael@0 | 172 | get matchCount() |
michael@0 | 173 | { |
michael@0 | 174 | return this.values.length; |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | getValueAt: function(aIndex) |
michael@0 | 178 | { |
michael@0 | 179 | return this.values[aIndex]; |
michael@0 | 180 | }, |
michael@0 | 181 | |
michael@0 | 182 | getLabelAt: function(aIndex) |
michael@0 | 183 | { |
michael@0 | 184 | return this.getValueAt(aIndex); |
michael@0 | 185 | }, |
michael@0 | 186 | |
michael@0 | 187 | getCommentAt: function(aIndex) |
michael@0 | 188 | { |
michael@0 | 189 | return this.comments[aIndex]; |
michael@0 | 190 | }, |
michael@0 | 191 | |
michael@0 | 192 | getStyleAt: function(aIndex) |
michael@0 | 193 | { |
michael@0 | 194 | return null; |
michael@0 | 195 | }, |
michael@0 | 196 | |
michael@0 | 197 | getImageAt: function(aIndex) |
michael@0 | 198 | { |
michael@0 | 199 | return ""; |
michael@0 | 200 | }, |
michael@0 | 201 | |
michael@0 | 202 | getFinalCompleteValueAt: function(aIndex) |
michael@0 | 203 | { |
michael@0 | 204 | return this.getValueAt(aIndex); |
michael@0 | 205 | }, |
michael@0 | 206 | |
michael@0 | 207 | removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
michael@0 | 208 | |
michael@0 | 209 | // nsISupports implementation |
michael@0 | 210 | QueryInterface: function(iid) { |
michael@0 | 211 | if (iid.equals(nsISupports) || |
michael@0 | 212 | iid.equals(nsIAutoCompleteResult)) |
michael@0 | 213 | return this; |
michael@0 | 214 | |
michael@0 | 215 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 216 | }, |
michael@0 | 217 | |
michael@0 | 218 | // Data |
michael@0 | 219 | values: null, |
michael@0 | 220 | comments: null |
michael@0 | 221 | } |