| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 cr.exportPath('settings'); | 5 cr.exportPath('settings'); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A data structure used by callers to combine the results of multiple search | 8 * A data structure used by callers to combine the results of multiple search |
| 9 * requests. | 9 * requests. |
| 10 * | 10 * |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 }.bind(this)); | 472 }.bind(this)); |
| 473 } | 473 } |
| 474 // Nothing to do otherwise. Since the request corresponding to this | 474 // Nothing to do otherwise. Since the request corresponding to this |
| 475 // queue was canceled, the queue is disposed along with the request. | 475 // queue was canceled, the queue is disposed along with the request. |
| 476 }.bind(this)); | 476 }.bind(this)); |
| 477 return; | 477 return; |
| 478 } | 478 } |
| 479 }, | 479 }, |
| 480 }; | 480 }; |
| 481 | 481 |
| 482 /** | 482 class SearchRequest { |
| 483 * @constructor | 483 /** |
| 484 * | 484 * @param {string} rawQuery |
| 485 * @param {string} rawQuery | 485 * @param {!HTMLElement} root |
| 486 * @param {!HTMLElement} root | 486 */ |
| 487 */ | 487 constructor(rawQuery, root) { |
| 488 var SearchRequest = function(rawQuery, root) { | 488 /** @private {string} */ |
| 489 /** @private {string} */ | 489 this.rawQuery_ = rawQuery; |
| 490 this.rawQuery_ = rawQuery; | |
| 491 | 490 |
| 492 /** @private {!HTMLElement} */ | 491 /** @private {!HTMLElement} */ |
| 493 this.root_ = root; | 492 this.root_ = root; |
| 494 | 493 |
| 495 /** @type {?RegExp} */ | 494 /** @type {?RegExp} */ |
| 496 this.regExp = this.generateRegExp_(); | 495 this.regExp = this.generateRegExp_(); |
| 497 | 496 |
| 498 /** | 497 /** |
| 499 * Whether this request was canceled before completing. | 498 * Whether this request was canceled before completing. |
| 500 * @type {boolean} | 499 * @type {boolean} |
| 501 */ | 500 */ |
| 502 this.canceled = false; | 501 this.canceled = false; |
| 503 | 502 |
| 504 /** @private {boolean} */ | 503 /** @private {boolean} */ |
| 505 this.foundMatches_ = false; | 504 this.foundMatches_ = false; |
| 506 | 505 |
| 507 /** @type {!PromiseResolver} */ | 506 /** @type {!PromiseResolver} */ |
| 508 this.resolver = new PromiseResolver(); | 507 this.resolver = new PromiseResolver(); |
| 509 | 508 |
| 510 /** @private {!TaskQueue} */ | 509 /** @private {!TaskQueue} */ |
| 511 this.queue_ = new TaskQueue(this); | 510 this.queue_ = new TaskQueue(this); |
| 512 this.queue_.onEmpty(function() { | 511 this.queue_.onEmpty(function() { |
| 513 this.resolver.resolve(this); | 512 this.resolver.resolve(this); |
| 514 }.bind(this)); | 513 }.bind(this)); |
| 515 }; | 514 } |
| 516 | 515 |
| 517 /** @private {!RegExp} */ | |
| 518 SearchRequest.SANITIZE_REGEX_ = /[-[\]{}()*+?.,\\^$|#\s]/g; | |
| 519 | |
| 520 SearchRequest.prototype = { | |
| 521 /** | 516 /** |
| 522 * Fires this search request. | 517 * Fires this search request. |
| 523 */ | 518 */ |
| 524 start: function() { | 519 start() { |
| 525 this.queue_.addTopLevelSearchTask( | 520 this.queue_.addTopLevelSearchTask( |
| 526 new TopLevelSearchTask(this, this.root_)); | 521 new TopLevelSearchTask(this, this.root_)); |
| 527 }, | 522 } |
| 528 | 523 |
| 529 /** | 524 /** |
| 530 * @return {?RegExp} | 525 * @return {?RegExp} |
| 531 * @private | 526 * @private |
| 532 */ | 527 */ |
| 533 generateRegExp_: function() { | 528 generateRegExp_() { |
| 534 var regExp = null; | 529 var regExp = null; |
| 535 | 530 |
| 536 // Generate search text by escaping any characters that would be | 531 // Generate search text by escaping any characters that would be |
| 537 // problematic for regular expressions. | 532 // problematic for regular expressions. |
| 538 var searchText = this.rawQuery_.trim().replace( | 533 var searchText = this.rawQuery_.trim().replace( |
| 539 SearchRequest.SANITIZE_REGEX_, '\\$&'); | 534 SearchRequest.SANITIZE_REGEX_, '\\$&'); |
| 540 if (searchText.length > 0) | 535 if (searchText.length > 0) |
| 541 regExp = new RegExp('(' + searchText + ')', 'i'); | 536 regExp = new RegExp('(' + searchText + ')', 'i'); |
| 542 | 537 |
| 543 return regExp; | 538 return regExp; |
| 544 }, | 539 } |
| 545 | 540 |
| 546 /** | 541 /** |
| 547 * @param {string} rawQuery | 542 * @param {string} rawQuery |
| 548 * @return {boolean} Whether this SearchRequest refers to an identical | 543 * @return {boolean} Whether this SearchRequest refers to an identical |
| 549 * query. | 544 * query. |
| 550 */ | 545 */ |
| 551 isSame: function(rawQuery) { | 546 isSame(rawQuery) { |
| 552 return this.rawQuery_ == rawQuery; | 547 return this.rawQuery_ == rawQuery; |
| 553 }, | 548 } |
| 554 | 549 |
| 555 /** | 550 /** |
| 556 * Updates the result for this search request. | 551 * Updates the result for this search request. |
| 557 * @param {boolean} found | 552 * @param {boolean} found |
| 558 */ | 553 */ |
| 559 updateMatches: function(found) { | 554 updateMatches(found) { |
| 560 this.foundMatches_ = this.foundMatches_ || found; | 555 this.foundMatches_ = this.foundMatches_ || found; |
| 561 }, | 556 } |
| 562 | 557 |
| 563 /** @return {boolean} Whether any matches were found. */ | 558 /** @return {boolean} Whether any matches were found. */ |
| 564 didFindMatches: function() { | 559 didFindMatches() { |
| 565 return this.foundMatches_; | 560 return this.foundMatches_; |
| 566 }, | 561 } |
| 567 }; | 562 } |
| 563 |
| 564 /** @private {!RegExp} */ |
| 565 SearchRequest.SANITIZE_REGEX_ = /[-[\]{}()*+?.,\\^$|#\s]/g; |
| 568 | 566 |
| 569 /** @interface */ | 567 /** @interface */ |
| 570 var SearchManager = function() {}; | 568 var SearchManager = function() {}; |
| 571 | 569 |
| 572 SearchManager.prototype = { | 570 SearchManager.prototype = { |
| 573 /** | 571 /** |
| 574 * @param {string} text The text to search for. | 572 * @param {string} text The text to search for. |
| 575 * @param {!Node} page | 573 * @param {!Node} page |
| 576 * @return {!Promise<!settings.SearchRequest>} A signal indicating that | 574 * @return {!Promise<!settings.SearchRequest>} A signal indicating that |
| 577 * searching finished. | 575 * searching finished. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 function setSearchManagerForTesting(searchManager) { | 627 function setSearchManagerForTesting(searchManager) { |
| 630 SearchManagerImpl.instance_ = searchManager; | 628 SearchManagerImpl.instance_ = searchManager; |
| 631 } | 629 } |
| 632 | 630 |
| 633 return { | 631 return { |
| 634 getSearchManager: getSearchManager, | 632 getSearchManager: getSearchManager, |
| 635 setSearchManagerForTesting: setSearchManagerForTesting, | 633 setSearchManagerForTesting: setSearchManagerForTesting, |
| 636 SearchRequest: SearchRequest, | 634 SearchRequest: SearchRequest, |
| 637 }; | 635 }; |
| 638 }); | 636 }); |
| OLD | NEW |