1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
12 * Alias for document.getElementById.
13 * @param {string} id The ID of the element to find.
14 * @return {HTMLElement} The found element or null if not found.
17 return document.getElementById(id);
21 * Calls chrome.send with a callback and restores the original afterwards.
22 * @param {string} name The name of the message to send.
23 * @param {!Array} params The parameters to send.
24 * @param {string} callbackName The name of the function that the backend calls.
25 * @param {!Function} The function to call.
27 function chromeSend(name, params, callbackName, callback) {
28 var old = global[callbackName];
29 global[callbackName] = function() {
31 global[callbackName] = old;
33 var args = Array.prototype.slice.call(arguments);
34 return callback.apply(global, args);
36 chrome.send(name, params);
40 * Generates a CSS url string.
41 * @param {string} s The URL to generate the CSS url for.
42 * @return {string} The CSS url string.
45 // http://www.w3.org/TR/css3-values/#uris
46 // Parentheses, commas, whitespace characters, single quotes (') and double
47 // quotes (") appearing in a URI must be escaped with a backslash
48 var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
49 // WebKit has a bug when it comes to URLs that end with \
50 // https://bugs.webkit.org/show_bug.cgi?id=28885
51 if (/\\\\$/.test(s2)) {
52 // Add a space to work around the WebKit bug.
55 return 'url("' + s2 + '")';
59 * Parses query parameters from Location.
60 * @param {string} s The URL to generate the CSS url for.
61 * @return {object} Dictionary containing name value pairs for URL
63 function parseQueryParams(location) {
65 var query = unescape(location.search.substring(1));
66 var vars = query.split("&");
67 for (var i=0; i < vars.length; i++) {
68 var pair = vars[i].split("=");
69 params[pair[0]] = pair[1];
74 function findAncestorByClass(el, className) {
75 return findAncestor(el, function(el) {
77 return el.classList.contains(className);
83 * Return the first ancestor for which the {@code predicate} returns true.
84 * @param {Node} node The node to check.
85 * @param {function(Node) : boolean} predicate The function that tests the
87 * @return {Node} The found ancestor or null if not found.
89 function findAncestor(node, predicate) {
91 while (node != null && !(last = predicate(node))) {
92 node = node.parentNode;
94 return last ? node : null;
97 function swapDomNodes(a, b) {
98 var afterA = a.nextSibling;
103 var aParent = a.parentNode;
104 b.parentNode.replaceChild(a, b);
105 aParent.insertBefore(b, afterA);
109 * Disables text selection and dragging.
111 function disableTextSelectAndDrag() {
112 // Disable text selection.
113 document.onselectstart = function(e) {
118 document.ondragstart = function(e) {
123 // Handle click on a link. If the link points to a chrome: or file: url, then
124 // call into the browser to do the navigation.
125 document.addEventListener('click', function(e) {
126 // Allow preventDefault to work.
131 if (el.nodeType == Node.ELEMENT_NODE &&
132 el.webkitMatchesSelector('A, A *')) {
133 while (el.tagName != 'A') {
134 el = el.parentElement;
137 if ((el.protocol == 'file:' || el.protocol == 'about:') &&
138 (e.button == 0 || e.button == 1)) {
139 chrome.send('navigateToUrl', [