OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @param {string} toTest The string to be tested. | 6 * @param {string} toTest The string to be tested. |
7 * @return {boolean} True if |toTest| contains only digits. Leading and trailing | 7 * @return {boolean} True if |toTest| contains only digits. Leading and trailing |
8 * whitespace is allowed. | 8 * whitespace is allowed. |
9 */ | 9 */ |
10 function isInteger(toTest) { | 10 function isInteger(toTest) { |
(...skipping 28 matching lines...) Expand all Loading... |
39 /** | 39 /** |
40 * Returns true if the contents of the two page ranges are equal. | 40 * Returns true if the contents of the two page ranges are equal. |
41 * @param {Array} array1 The first array. | 41 * @param {Array} array1 The first array. |
42 * @param {Array} array2 The second array. | 42 * @param {Array} array2 The second array. |
43 * @return {boolean} true if the arrays are equal. | 43 * @return {boolean} true if the arrays are equal. |
44 */ | 44 */ |
45 function areRangesEqual(array1, array2) { | 45 function areRangesEqual(array1, array2) { |
46 if (array1.length != array2.length) | 46 if (array1.length != array2.length) |
47 return false; | 47 return false; |
48 for (var i = 0; i < array1.length; i++) | 48 for (var i = 0; i < array1.length; i++) |
49 if (array1[i].from != array2[i].from || | 49 if (array1[i].from != array2[i].from || array1[i].to != array2[i].to) { |
50 array1[i].to != array2[i].to) { | 50 return false; |
51 return false; | 51 } |
52 } | |
53 return true; | 52 return true; |
54 } | 53 } |
55 | 54 |
56 /** | 55 /** |
57 * Removes duplicate elements from |inArray| and returns a new array. | 56 * Removes duplicate elements from |inArray| and returns a new array. |
58 * |inArray| is not affected. It assumes that |inArray| is already sorted. | 57 * |inArray| is not affected. It assumes that |inArray| is already sorted. |
59 * @param {!Array<number>} inArray The array to be processed. | 58 * @param {!Array<number>} inArray The array to be processed. |
60 * @return {!Array<number>} The array after processing. | 59 * @return {!Array<number>} The array after processing. |
61 */ | 60 */ |
62 function removeDuplicates(inArray) { | 61 function removeDuplicates(inArray) { |
63 var out = []; | 62 var out = []; |
64 | 63 |
65 if (inArray.length == 0) | 64 if (inArray.length == 0) |
66 return out; | 65 return out; |
67 | 66 |
68 out.push(inArray[0]); | 67 out.push(inArray[0]); |
69 for (var i = 1; i < inArray.length; ++i) | 68 for (var i = 1; i < inArray.length; ++i) |
70 if (inArray[i] != inArray[i - 1]) | 69 if (inArray[i] != inArray[i - 1]) |
71 out.push(inArray[i]); | 70 out.push(inArray[i]); |
72 return out; | 71 return out; |
73 } | 72 } |
74 | 73 |
75 /** @enum {number} */ | 74 /** @enum {number} */ |
76 var PageRangeStatus = { | 75 var PageRangeStatus = {NO_ERROR: 0, SYNTAX_ERROR: -1, LIMIT_ERROR: -2}; |
77 NO_ERROR: 0, | |
78 SYNTAX_ERROR: -1, | |
79 LIMIT_ERROR: -2 | |
80 }; | |
81 | 76 |
82 /** | 77 /** |
83 * Returns a list of ranges in |pageRangeText|. The ranges are | 78 * Returns a list of ranges in |pageRangeText|. The ranges are |
84 * listed in the order they appear in |pageRangeText| and duplicates are not | 79 * listed in the order they appear in |pageRangeText| and duplicates are not |
85 * eliminated. If |pageRangeText| is not valid, PageRangeStatus.SYNTAX_ERROR | 80 * eliminated. If |pageRangeText| is not valid, PageRangeStatus.SYNTAX_ERROR |
86 * is returned. | 81 * is returned. |
87 * A valid selection has a parsable format and every page identifier is | 82 * A valid selection has a parsable format and every page identifier is |
88 * greater than 0 unless wildcards are used(see examples). | 83 * greater than 0 unless wildcards are used(see examples). |
89 * If a page is greater than |totalPageCount|, PageRangeStatus.LIMIT_ERROR | 84 * If a page is greater than |totalPageCount|, PageRangeStatus.LIMIT_ERROR |
90 * is returned. | 85 * is returned. |
(...skipping 13 matching lines...) Expand all Loading... |
104 * @param {string} pageRangeText The text to be checked. | 99 * @param {string} pageRangeText The text to be checked. |
105 * @param {number=} opt_totalPageCount The total number of pages. | 100 * @param {number=} opt_totalPageCount The total number of pages. |
106 * @return {!PageRangeStatus|!Array<{from: number, to: number}>} | 101 * @return {!PageRangeStatus|!Array<{from: number, to: number}>} |
107 */ | 102 */ |
108 function pageRangeTextToPageRanges(pageRangeText, opt_totalPageCount) { | 103 function pageRangeTextToPageRanges(pageRangeText, opt_totalPageCount) { |
109 if (pageRangeText == '') { | 104 if (pageRangeText == '') { |
110 return []; | 105 return []; |
111 } | 106 } |
112 | 107 |
113 var MAX_PAGE_NUMBER = 1000000000; | 108 var MAX_PAGE_NUMBER = 1000000000; |
114 var totalPageCount = opt_totalPageCount ? opt_totalPageCount : | 109 var totalPageCount = |
115 MAX_PAGE_NUMBER; | 110 opt_totalPageCount ? opt_totalPageCount : MAX_PAGE_NUMBER; |
116 | 111 |
117 var regex = /^\s*([0-9]*)\s*-\s*([0-9]*)\s*$/; | 112 var regex = /^\s*([0-9]*)\s*-\s*([0-9]*)\s*$/; |
118 var parts = pageRangeText.split(/,/); | 113 var parts = pageRangeText.split(/,/); |
119 | 114 |
120 var pageRanges = []; | 115 var pageRanges = []; |
121 for (var i = 0; i < parts.length; ++i) { | 116 for (var i = 0; i < parts.length; ++i) { |
122 var match = parts[i].match(regex); | 117 var match = parts[i].match(regex); |
123 if (match) { | 118 if (match) { |
124 if (!isPositiveInteger(match[1]) && match[1] !== '') | 119 if (!isPositiveInteger(match[1]) && match[1] !== '') |
125 return PageRangeStatus.SYNTAX_ERROR; | 120 return PageRangeStatus.SYNTAX_ERROR; |
(...skipping 26 matching lines...) Expand all Loading... |
152 * See pageRangeTextToPageRanges for details. | 147 * See pageRangeTextToPageRanges for details. |
153 * @param {string} pageRangeText The text to be checked. | 148 * @param {string} pageRangeText The text to be checked. |
154 * @param {number} totalPageCount The total number of pages. | 149 * @param {number} totalPageCount The total number of pages. |
155 * @return {!Array<number>} A list of all pages. | 150 * @return {!Array<number>} A list of all pages. |
156 */ | 151 */ |
157 function pageRangeTextToPageList(pageRangeText, totalPageCount) { | 152 function pageRangeTextToPageList(pageRangeText, totalPageCount) { |
158 var pageRanges = pageRangeTextToPageRanges(pageRangeText, totalPageCount); | 153 var pageRanges = pageRangeTextToPageRanges(pageRangeText, totalPageCount); |
159 var pageList = []; | 154 var pageList = []; |
160 if (pageRanges instanceof Array) { | 155 if (pageRanges instanceof Array) { |
161 for (var i = 0; i < pageRanges.length; ++i) { | 156 for (var i = 0; i < pageRanges.length; ++i) { |
162 for (var j = pageRanges[i].from; j <= Math.min(pageRanges[i].to, | 157 for (var j = pageRanges[i].from; |
163 totalPageCount); ++j) { | 158 j <= Math.min(pageRanges[i].to, totalPageCount); ++j) { |
164 pageList.push(j); | 159 pageList.push(j); |
165 } | 160 } |
166 } | 161 } |
167 } | 162 } |
168 if (pageList.length == 0) { | 163 if (pageList.length == 0) { |
169 for (var j = 1; j <= totalPageCount; ++j) | 164 for (var j = 1; j <= totalPageCount; ++j) |
170 pageList.push(j); | 165 pageList.push(j); |
171 } | 166 } |
172 return pageList; | 167 return pageList; |
173 } | 168 } |
174 | 169 |
175 /** | 170 /** |
176 * @param {!Array<number>} pageList The list to be processed. | 171 * @param {!Array<number>} pageList The list to be processed. |
177 * @return {!Array<number>} The contents of |pageList| in ascending order and | 172 * @return {!Array<number>} The contents of |pageList| in ascending order and |
178 * without any duplicates. |pageList| is not affected. | 173 * without any duplicates. |pageList| is not affected. |
179 */ | 174 */ |
180 function pageListToPageSet(pageList) { | 175 function pageListToPageSet(pageList) { |
181 var pageSet = []; | 176 var pageSet = []; |
182 if (pageList.length == 0) | 177 if (pageList.length == 0) |
183 return pageSet; | 178 return pageSet; |
184 pageSet = pageList.slice(0); | 179 pageSet = pageList.slice(0); |
185 pageSet.sort(function(a, b) { | 180 pageSet.sort(function(a, b) { |
186 return /** @type {number} */(a) - /** @type {number} */(b); | 181 return /** @type {number} */ (a) - /** @type {number} */ (b); |
187 }); | 182 }); |
188 pageSet = removeDuplicates(pageSet); | 183 pageSet = removeDuplicates(pageSet); |
189 return pageSet; | 184 return pageSet; |
190 } | 185 } |
191 | 186 |
192 /** | 187 /** |
193 * @param {!HTMLElement} element Element to check for visibility. | 188 * @param {!HTMLElement} element Element to check for visibility. |
194 * @return {boolean} Whether the given element is visible. | 189 * @return {boolean} Whether the given element is visible. |
195 */ | 190 */ |
196 function getIsVisible(element) { | 191 function getIsVisible(element) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 228 |
234 /** | 229 /** |
235 * @param {!Array<!{locale: string, value: string}>} localizedStrings An array | 230 * @param {!Array<!{locale: string, value: string}>} localizedStrings An array |
236 * of strings with corresponding locales. | 231 * of strings with corresponding locales. |
237 * @return {string} A string for the current locale. An empty string if there's | 232 * @return {string} A string for the current locale. An empty string if there's |
238 * no string for the current locale found. | 233 * no string for the current locale found. |
239 */ | 234 */ |
240 function getStringForCurrentLocale(localizedStrings) { | 235 function getStringForCurrentLocale(localizedStrings) { |
241 // First try to find an exact match and then look for the language only. | 236 // First try to find an exact match and then look for the language only. |
242 return getStringForLocale(localizedStrings, navigator.language) || | 237 return getStringForLocale(localizedStrings, navigator.language) || |
243 getStringForLocale(localizedStrings, | 238 getStringForLocale(localizedStrings, navigator.language.split('-')[0]); |
244 navigator.language.split('-')[0]); | |
245 } | 239 } |
OLD | NEW |