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

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: address comments 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 * @return {!HTMLElement} the Element. 205 * @return {!HTMLElement} the Element.
206 */ 206 */
207 function queryRequiredElement(selectors, opt_context) { 207 function queryRequiredElement(selectors, opt_context) {
208 var element = (opt_context || document).querySelector(selectors); 208 var element = (opt_context || document).querySelector(selectors);
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) {
Dan Beam 2016/08/08 19:29:47 can we make this a static function so we can call
calamity 2016/08/09 08:39:07 Do you mean pulling the event handler out into a s
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 }
Dan Beam 2016/08/08 19:29:48 could this work? /** * @param {!Node} node * @r
Dan Beam 2016/08/08 21:28:00 actually, Array#find might also suck wrt compat UG
230
219 var el = e.target; 231 var el = e.target;
220 if (el.nodeType == Node.ELEMENT_NODE && 232 if (!anchor && el.nodeType == Node.ELEMENT_NODE &&
221 el.webkitMatchesSelector('A, A *')) { 233 el.webkitMatchesSelector('A, A *')) {
222 while (el.tagName != 'A') { 234 while (el.tagName != 'A') {
223 el = el.parentElement; 235 el = el.parentElement;
236 anchor = element;
237 break;
224 } 238 }
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
Dan Beam 2016/08/08 19:29:47 nit: anchor = /** @type {!HTMLAnchorElement} */(an
calamity 2016/08/09 08:39:07 Done.
229 el.href, 244 if ((anchor.protocol == 'file:' || anchor.protocol == 'about:') &&
230 el.target, 245 (e.button == 0 || e.button == 1)) {
231 e.button, 246 chrome.send('navigateToUrl', [
232 e.altKey, 247 anchor.href,
233 e.ctrlKey, 248 anchor.target,
234 e.metaKey, 249 e.button,
235 e.shiftKey 250 e.altKey,
236 ]); 251 e.ctrlKey,
237 e.preventDefault(); 252 e.metaKey,
238 } 253 e.shiftKey
254 ]);
255 e.preventDefault();
239 } 256 }
240 }); 257 });
241 258
242 /** 259 /**
243 * Creates a new URL which is the old URL with a GET param of key=value. 260 * 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 261 * @param {string} url The base URL. There is not sanity checking on the URL so
245 * it must be passed in a proper format. 262 * it must be passed in a proper format.
246 * @param {string} key The key of the param. 263 * @param {string} key The key of the param.
247 * @param {string} value The value of the param. 264 * @param {string} value The value of the param.
248 * @return {string} The new URL. 265 * @return {string} The new URL.
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 case 0xdb: return '['; 449 case 0xdb: return '[';
433 case 0xdd: return ']'; 450 case 0xdd: return ']';
434 } 451 }
435 return 'Unidentified'; 452 return 'Unidentified';
436 } 453 }
437 }); 454 });
438 } else { 455 } else {
439 window.console.log("KeyboardEvent.Key polyfill not required"); 456 window.console.log("KeyboardEvent.Key polyfill not required");
440 } 457 }
441 // </if> /* is_ios */ 458 // </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