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

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

Issue 2184123003: Use event path to detect if anchor has been clicked in WebUIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@close_dialog_on_query
Patch Set: add <b> test Created 4 years, 4 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
« no previous file with comments | « chrome/test/data/webui/util_test.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return assertInstanceof(element, HTMLElement, 209 return assertInstanceof(element, HTMLElement,
210 'Missing required element: ' + selectors); 210 'Missing required element: ' + selectors);
211 } 211 }
212 212
213 // Handle click on a link. If the link points to a chrome: or file: url, then 213 // Handle click on a link. If the link points to a chrome: or file: url, then
214 // call into the browser to do the navigation. 214 // call into the browser to do the navigation.
215 document.addEventListener('click', function(e) { 215 document.addEventListener('click', function(e) {
216 if (e.defaultPrevented) 216 if (e.defaultPrevented)
217 return; 217 return;
218 218
219 var eventPath = e.path;
220 var anchor = null;
221 if (eventPath) {
222 for (var i = 0; i < eventPath.length; i++) {
223 var element = eventPath[i];
224 if (element.tagName === 'A' && element.href) {
225 anchor = element;
226 break;
227 }
228 }
229 }
230
231 // Fallback if Event.path is not available.
219 var el = e.target; 232 var el = e.target;
220 if (el.nodeType == Node.ELEMENT_NODE && 233 if (!anchor && el.nodeType == Node.ELEMENT_NODE &&
221 el.webkitMatchesSelector('A, A *')) { 234 el.webkitMatchesSelector('A, A *')) {
222 while (el.tagName != 'A') { 235 while (el.tagName != 'A') {
223 el = el.parentElement; 236 el = el.parentElement;
224 } 237 }
238 anchor = el;
239 }
225 240
226 if ((el.protocol == 'file:' || el.protocol == 'about:') && 241 if (!anchor)
227 (e.button == 0 || e.button == 1)) { 242 return;
228 chrome.send('navigateToUrl', [ 243
229 el.href, 244 anchor = /** @type {!HTMLAnchorElement} */(anchor);
230 el.target, 245 if ((anchor.protocol == 'file:' || anchor.protocol == 'about:') &&
231 e.button, 246 (e.button == 0 || e.button == 1)) {
232 e.altKey, 247 chrome.send('navigateToUrl', [
233 e.ctrlKey, 248 anchor.href,
234 e.metaKey, 249 anchor.target,
235 e.shiftKey 250 e.button,
236 ]); 251 e.altKey,
237 e.preventDefault(); 252 e.ctrlKey,
238 } 253 e.metaKey,
254 e.shiftKey
255 ]);
256 e.preventDefault();
239 } 257 }
240 }); 258 });
241 259
242 /** 260 /**
243 * Creates a new URL which is the old URL with a GET param of key=value. 261 * Creates a new URL which is the old URL with a GET param of key=value.
244 * @param {string} url The base URL. There is not sanity checking on the URL so 262 * @param {string} url The base URL. There is not sanity checking on the URL so
245 * it must be passed in a proper format. 263 * it must be passed in a proper format.
246 * @param {string} key The key of the param. 264 * @param {string} key The key of the param.
247 * @param {string} value The value of the param. 265 * @param {string} value The value of the param.
248 * @return {string} The new URL. 266 * @return {string} The new URL.
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 case 0xdb: return '['; 450 case 0xdb: return '[';
433 case 0xdd: return ']'; 451 case 0xdd: return ']';
434 } 452 }
435 return 'Unidentified'; 453 return 'Unidentified';
436 } 454 }
437 }); 455 });
438 } else { 456 } else {
439 window.console.log("KeyboardEvent.Key polyfill not required"); 457 window.console.log("KeyboardEvent.Key polyfill not required");
440 } 458 }
441 // </if> /* is_ios */ 459 // </if> /* is_ios */
OLDNEW
« no previous file with comments | « chrome/test/data/webui/util_test.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698