Commit 1c348868 authored by dpapad's avatar dpapad Committed by Commit Bot

WebUI cleanup: Move getUrlForCss() from util.js to icon.js.

This was residing in util.js and was used by icon.js (3 calls) and
ntp4/new_tab.js (1 call). Given that this is icon related, moving to icon.js
and updating ntp4/ accordingly.

Rationale:
Generic "util" files are generally an anti-pattern, and often end up as a bag of
unrelated functionality. Given that icon.js already exists and holds
icon-related functionality, moving getUrlForCss() over there makes sense.

Bug: None
Change-Id: I586b2653ba09d6d144f1d99aa9ab331f96095726
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1706807Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#678531}
parent 8f0bd4da
...@@ -32,6 +32,7 @@ js_library("apps_page") { ...@@ -32,6 +32,7 @@ js_library("apps_page") {
"//ui/webui/resources/js/cr/ui/position_util.js", "//ui/webui/resources/js/cr/ui/position_util.js",
"//ui/webui/resources/js/cr/ui/touch_handler.js", "//ui/webui/resources/js/cr/ui/touch_handler.js",
"//ui/webui/resources/js/event_tracker.js", "//ui/webui/resources/js/event_tracker.js",
"//ui/webui/resources/js/icon.js",
"//ui/webui/resources/js/load_time_data.js", "//ui/webui/resources/js/load_time_data.js",
"//ui/webui/resources/js/parse_html_subset.js", "//ui/webui/resources/js/parse_html_subset.js",
"//ui/webui/resources/js/promise_resolver.js", "//ui/webui/resources/js/promise_resolver.js",
......
...@@ -34,6 +34,7 @@ document.write('<link id="themecss" rel="stylesheet" ' + ...@@ -34,6 +34,7 @@ document.write('<link id="themecss" rel="stylesheet" ' +
<script src="../../../../ui/webui/resources/js/action_link.js"></script> <script src="../../../../ui/webui/resources/js/action_link.js"></script>
<script src="../../../../ui/webui/resources/js/event_tracker.js"></script> <script src="../../../../ui/webui/resources/js/event_tracker.js"></script>
<script src="../../../../ui/webui/resources/js/util.js"></script> <script src="../../../../ui/webui/resources/js/util.js"></script>
<script src="../../../../ui/webui/resources/js/icon.js"></script>
<script src="../../../../ui/webui/resources/js/cr.js"></script> <script src="../../../../ui/webui/resources/js/cr.js"></script>
<script src="../../../../ui/webui/resources/js/cr/event_target.js"></script> <script src="../../../../ui/webui/resources/js/cr/event_target.js"></script>
......
...@@ -258,7 +258,7 @@ cr.define('ntp', function() { ...@@ -258,7 +258,7 @@ cr.define('ntp', function() {
const headerContainer = $('login-status-header-container'); const headerContainer = $('login-status-header-container');
headerContainer.classList.toggle('login-status-icon', !!iconURL); headerContainer.classList.toggle('login-status-icon', !!iconURL);
headerContainer.style.backgroundImage = headerContainer.style.backgroundImage =
iconURL ? getUrlForCss(iconURL) : 'none'; iconURL ? cr.icon.getUrlForCss(iconURL) : 'none';
} }
} }
......
...@@ -30,6 +30,25 @@ cr.define('cr.icon', function() { ...@@ -30,6 +30,25 @@ cr.define('cr.icon', function() {
return supportedScaleFactors; return supportedScaleFactors;
} }
/**
* Generates a CSS url string.
* @param {string} s The URL to generate the CSS url for.
* @return {string} The CSS url string.
*/
function getUrlForCss(s) {
// http://www.w3.org/TR/css3-values/#uris
// Parentheses, commas, whitespace characters, single quotes (') and double
// quotes (") appearing in a URI must be escaped with a backslash
let s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
// WebKit has a bug when it comes to URLs that end with \
// https://bugs.webkit.org/show_bug.cgi?id=28885
if (/\\\\$/.test(s2)) {
// Add a space to work around the WebKit bug.
s2 += ' ';
}
return 'url("' + s2 + '")';
}
/** /**
* A URL for the filetype icon for |filePath|. OS and theme dependent. * A URL for the filetype icon for |filePath|. OS and theme dependent.
* @param {string} filePath * @param {string} filePath
...@@ -122,8 +141,9 @@ cr.define('cr.icon', function() { ...@@ -122,8 +141,9 @@ cr.define('cr.icon', function() {
} }
return { return {
getImage: getImage,
getFavicon: getFavicon, getFavicon: getFavicon,
getFileIconUrl: getFileIconUrl, getFileIconUrl: getFileIconUrl,
getImage: getImage,
getUrlForCss: getUrlForCss,
}; };
}); });
...@@ -63,25 +63,6 @@ function announceAccessibleMessage(msg) { ...@@ -63,25 +63,6 @@ function announceAccessibleMessage(msg) {
}, 50); }, 50);
} }
/**
* Generates a CSS url string.
* @param {string} s The URL to generate the CSS url for.
* @return {string} The CSS url string.
*/
function getUrlForCss(s) {
// http://www.w3.org/TR/css3-values/#uris
// Parentheses, commas, whitespace characters, single quotes (') and double
// quotes (") appearing in a URI must be escaped with a backslash
let s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
// WebKit has a bug when it comes to URLs that end with \
// https://bugs.webkit.org/show_bug.cgi?id=28885
if (/\\\\$/.test(s2)) {
// Add a space to work around the WebKit bug.
s2 += ' ';
}
return 'url("' + s2 + '")';
}
/** /**
* Parses query parameters from Location. * Parses query parameters from Location.
* @param {Location} location The URL to generate the CSS url for. * @param {Location} location The URL to generate the CSS url for.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment