Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: ui/webui/resources/js/util.js

Issue 1919183005: Cleanup: Extract icon related methods outside of util.js (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename favicon.html to icon.html Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // <include src="assert.js"> 5 // <include src="assert.js">
6 6
7 /** 7 /**
8 * Alias for document.getElementById. Found elements must be HTMLElements. 8 * Alias for document.getElementById. Found elements must be HTMLElements.
9 * @param {string} id The ID of the element to find. 9 * @param {string} id The ID of the element to find.
10 * @return {HTMLElement} The found element or null if not found. 10 * @return {HTMLElement} The found element or null if not found.
(...skipping 28 matching lines...) Expand all
39 element.style.left = '-9999px'; 39 element.style.left = '-9999px';
40 element.style.height = '0px'; 40 element.style.height = '0px';
41 element.innerText = msg; 41 element.innerText = msg;
42 document.body.appendChild(element); 42 document.body.appendChild(element);
43 window.setTimeout(function() { 43 window.setTimeout(function() {
44 document.body.removeChild(element); 44 document.body.removeChild(element);
45 }, 0); 45 }, 0);
46 } 46 }
47 47
48 /** 48 /**
49 * Returns the scale factors supported by this platform for webui
50 * resources.
51 * @return {Array} The supported scale factors.
52 */
53 function getSupportedScaleFactors() {
54 var supportedScaleFactors = [];
55 if (cr.isMac || cr.isChromeOS || cr.isWindows || cr.isLinux) {
56 // All desktop platforms support zooming which also updates the
57 // renderer's device scale factors (a.k.a devicePixelRatio), and
58 // these platforms has high DPI assets for 2.0x. Use 1x and 2x in
59 // image-set on these platforms so that the renderer can pick the
60 // closest image for the current device scale factor.
61 supportedScaleFactors.push(1);
62 supportedScaleFactors.push(2);
63 } else {
64 // For other platforms that use fixed device scale factor, use
65 // the window's device pixel ratio.
66 // TODO(oshima): Investigate if Android/iOS need to use image-set.
67 supportedScaleFactors.push(window.devicePixelRatio);
68 }
69 return supportedScaleFactors;
70 }
71
72 /**
73 * Generates a CSS url string. 49 * Generates a CSS url string.
74 * @param {string} s The URL to generate the CSS url for. 50 * @param {string} s The URL to generate the CSS url for.
75 * @return {string} The CSS url string. 51 * @return {string} The CSS url string.
76 */ 52 */
77 function url(s) { 53 function url(s) {
78 // http://www.w3.org/TR/css3-values/#uris 54 // http://www.w3.org/TR/css3-values/#uris
79 // Parentheses, commas, whitespace characters, single quotes (') and double 55 // Parentheses, commas, whitespace characters, single quotes (') and double
80 // quotes (") appearing in a URI must be escaped with a backslash 56 // quotes (") appearing in a URI must be escaped with a backslash
81 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1'); 57 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
82 // WebKit has a bug when it comes to URLs that end with \ 58 // WebKit has a bug when it comes to URLs that end with \
83 // https://bugs.webkit.org/show_bug.cgi?id=28885 59 // https://bugs.webkit.org/show_bug.cgi?id=28885
84 if (/\\\\$/.test(s2)) { 60 if (/\\\\$/.test(s2)) {
85 // Add a space to work around the WebKit bug. 61 // Add a space to work around the WebKit bug.
86 s2 += ' '; 62 s2 += ' ';
87 } 63 }
88 return 'url("' + s2 + '")'; 64 return 'url("' + s2 + '")';
89 } 65 }
90 66
91 /** 67 /**
92 * Returns the URL of the image, or an image set of URLs for the profile avatar.
93 * Default avatars have resources available for multiple scalefactors, whereas
94 * the GAIA profile image only comes in one size.
95 *
96 * @param {string} path The path of the image.
97 * @return {string} The url, or an image set of URLs of the avatar image.
98 */
99 function getProfileAvatarIcon(path) {
100 var chromeThemePath = 'chrome://theme';
101 var isDefaultAvatar =
102 (path.slice(0, chromeThemePath.length) == chromeThemePath);
103 return isDefaultAvatar ? imageset(path + '@scalefactorx'): url(path);
104 }
105
106 /**
107 * Generates a CSS -webkit-image-set for a chrome:// url.
108 * An entry in the image set is added for each of getSupportedScaleFactors().
109 * The scale-factor-specific url is generated by replacing the first instance of
110 * 'scalefactor' in |path| with the numeric scale factor.
111 * @param {string} path The URL to generate an image set for.
112 * 'scalefactor' should be a substring of |path|.
113 * @return {string} The CSS -webkit-image-set.
114 */
115 function imageset(path) {
116 var supportedScaleFactors = getSupportedScaleFactors();
117
118 var replaceStartIndex = path.indexOf('scalefactor');
119 if (replaceStartIndex < 0)
120 return url(path);
121
122 var s = '';
123 for (var i = 0; i < supportedScaleFactors.length; ++i) {
124 var scaleFactor = supportedScaleFactors[i];
125 var pathWithScaleFactor = path.substr(0, replaceStartIndex) + scaleFactor +
126 path.substr(replaceStartIndex + 'scalefactor'.length);
127
128 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x';
129
130 if (i != supportedScaleFactors.length - 1)
131 s += ', ';
132 }
133 return '-webkit-image-set(' + s + ')';
134 }
135
136 /**
137 * Parses query parameters from Location. 68 * Parses query parameters from Location.
138 * @param {Location} location The URL to generate the CSS url for. 69 * @param {Location} location The URL to generate the CSS url for.
139 * @return {Object} Dictionary containing name value pairs for URL 70 * @return {Object} Dictionary containing name value pairs for URL
140 */ 71 */
141 function parseQueryParams(location) { 72 function parseQueryParams(location) {
142 var params = {}; 73 var params = {};
143 var query = unescape(location.search.substring(1)); 74 var query = unescape(location.search.substring(1));
144 var vars = query.split('&'); 75 var vars = query.split('&');
145 for (var i = 0; i < vars.length; i++) { 76 for (var i = 0; i < vars.length; i++) {
146 var pair = vars[i].split('='); 77 var pair = vars[i].split('=');
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 */ 249 */
319 function appendParam(url, key, value) { 250 function appendParam(url, key, value) {
320 var param = encodeURIComponent(key) + '=' + encodeURIComponent(value); 251 var param = encodeURIComponent(key) + '=' + encodeURIComponent(value);
321 252
322 if (url.indexOf('?') == -1) 253 if (url.indexOf('?') == -1)
323 return url + '?' + param; 254 return url + '?' + param;
324 return url + '&' + param; 255 return url + '&' + param;
325 } 256 }
326 257
327 /** 258 /**
328 * A regular expression for identifying favicon URLs.
329 * @const {!RegExp}
330 * TODO(dpapad): Move all favicon related methods to a new favicon_util.js file
331 * and make this RegExp private.
332 */
333 var FAVICON_URL_REGEX = /\.ico$/i;
334
335 /**
336 * Creates a CSS -webkit-image-set for a favicon request.
337 * @param {string} url Either the URL of the original page or of the favicon
338 * itself.
339 * @param {number=} opt_size Optional preferred size of the favicon.
340 * @param {string=} opt_type Optional type of favicon to request. Valid values
341 * are 'favicon' and 'touch-icon'. Default is 'favicon'.
342 * @return {string} -webkit-image-set for the favicon.
343 */
344 function getFaviconImageSet(url, opt_size, opt_type) {
345 var size = opt_size || 16;
346 var type = opt_type || 'favicon';
347
348 return imageset(
349 'chrome://' + type + '/size/' + size + '@scalefactorx/' +
350 // Note: Literal 'iconurl' must match |kIconURLParameter| in
351 // components/favicon_base/favicon_url_parser.cc.
352 (FAVICON_URL_REGEX.test(url) ? 'iconurl/' : '') + url);
353 }
354
355 /**
356 * Creates an element of a specified type with a specified class name. 259 * Creates an element of a specified type with a specified class name.
357 * @param {string} type The node type. 260 * @param {string} type The node type.
358 * @param {string} className The class name to use. 261 * @param {string} className The class name to use.
359 * @return {Element} The created element. 262 * @return {Element} The created element.
360 */ 263 */
361 function createElementWithClassName(type, className) { 264 function createElementWithClassName(type, className) {
362 var elm = document.createElement(type); 265 var elm = document.createElement(type);
363 elm.className = className; 266 elm.className = className;
364 return elm; 267 return elm;
365 } 268 }
(...skipping 16 matching lines...) Expand all
382 opt_timeOut += 50; 285 opt_timeOut += 50;
383 } 286 }
384 287
385 var fired = false; 288 var fired = false;
386 el.addEventListener('webkitTransitionEnd', function f(e) { 289 el.addEventListener('webkitTransitionEnd', function f(e) {
387 el.removeEventListener('webkitTransitionEnd', f); 290 el.removeEventListener('webkitTransitionEnd', f);
388 fired = true; 291 fired = true;
389 }); 292 });
390 window.setTimeout(function() { 293 window.setTimeout(function() {
391 if (!fired) 294 if (!fired)
392 cr.dispatchSimpleEvent(el, 'webkitTransitionEnd', true); 295 cr.dispatchSimpleEvent(el, 'webkitTransitionEnd', true);
Dan Beam 2016/05/06 22:39:06 because ^
dpapad 2016/05/06 22:56:33 Expanding on previous comment, this file as a whol
393 }, opt_timeOut); 296 }, opt_timeOut);
394 } 297 }
395 298
396 /** 299 /**
397 * Alias for document.scrollTop getter. 300 * Alias for document.scrollTop getter.
398 * @param {!HTMLDocument} doc The document node where information will be 301 * @param {!HTMLDocument} doc The document node where information will be
399 * queried from. 302 * queried from.
400 * @return {number} The Y document scroll offset. 303 * @return {number} The Y document scroll offset.
401 */ 304 */
402 function scrollTopForDocument(doc) { 305 function scrollTopForDocument(doc) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 } 365 }
463 366
464 /** 367 /**
465 * Quote a string so it can be used in a regular expression. 368 * Quote a string so it can be used in a regular expression.
466 * @param {string} str The source string. 369 * @param {string} str The source string.
467 * @return {string} The escaped string. 370 * @return {string} The escaped string.
468 */ 371 */
469 function quoteString(str) { 372 function quoteString(str) {
470 return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1'); 373 return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1');
471 } 374 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698