| OLD | NEW |
| 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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 386 |
| 387 /** | 387 /** |
| 388 * Quote a string so it can be used in a regular expression. | 388 * Quote a string so it can be used in a regular expression. |
| 389 * @param {string} str The source string. | 389 * @param {string} str The source string. |
| 390 * @return {string} The escaped string. | 390 * @return {string} The escaped string. |
| 391 */ | 391 */ |
| 392 function quoteString(str) { | 392 function quoteString(str) { |
| 393 return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1'); | 393 return str.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1'); |
| 394 } | 394 } |
| 395 | 395 |
| 396 /** |
| 397 * Calls |callback| and stops listening the first time any event in |eventNames| |
| 398 * is triggered on |target|. |
| 399 * @param {!EventTarget} target |
| 400 * @param {!Array<string>|string} eventNames Array or space-delimited string of |
| 401 * event names to listen to (e.g. 'click mousedown'). |
| 402 * @param {function(!Event)} callback Called at most once. The |
| 403 * optional return value is passed on by the listener. |
| 404 */ |
| 405 function listenOnce(target, eventNames, callback) { |
| 406 if (!Array.isArray(eventNames)) |
| 407 eventNames = eventNames.split(/ +/); |
| 408 |
| 409 var removeAllAndCallCallback = function(event) { |
| 410 eventNames.forEach(function(eventName) { |
| 411 target.removeEventListener(eventName, removeAllAndCallCallback, false); |
| 412 }); |
| 413 return callback(event); |
| 414 }; |
| 415 |
| 416 eventNames.forEach(function(eventName) { |
| 417 target.addEventListener(eventName, removeAllAndCallCallback, false); |
| 418 }); |
| 419 } |
| 420 |
| 396 // <if expr="is_ios"> | 421 // <if expr="is_ios"> |
| 397 // Polyfill 'key' in KeyboardEvent for iOS. | 422 // Polyfill 'key' in KeyboardEvent for iOS. |
| 398 // This function is not intended to be complete but should | 423 // This function is not intended to be complete but should |
| 399 // be sufficient enough to have iOS work correctly while | 424 // be sufficient enough to have iOS work correctly while |
| 400 // it does not support key yet. | 425 // it does not support key yet. |
| 401 if (!('key' in KeyboardEvent.prototype)) { | 426 if (!('key' in KeyboardEvent.prototype)) { |
| 402 Object.defineProperty(KeyboardEvent.prototype, 'key', { | 427 Object.defineProperty(KeyboardEvent.prototype, 'key', { |
| 403 /** @this {KeyboardEvent} */ | 428 /** @this {KeyboardEvent} */ |
| 404 get: function () { | 429 get: function () { |
| 405 // 0-9 | 430 // 0-9 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 case 0xdb: return '['; | 477 case 0xdb: return '['; |
| 453 case 0xdd: return ']'; | 478 case 0xdd: return ']'; |
| 454 } | 479 } |
| 455 return 'Unidentified'; | 480 return 'Unidentified'; |
| 456 } | 481 } |
| 457 }); | 482 }); |
| 458 } else { | 483 } else { |
| 459 window.console.log("KeyboardEvent.Key polyfill not required"); | 484 window.console.log("KeyboardEvent.Key polyfill not required"); |
| 460 } | 485 } |
| 461 // </if> /* is_ios */ | 486 // </if> /* is_ios */ |
| OLD | NEW |