OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 Polymer({ |
| 6 is: 'history-searched-label', |
| 7 |
| 8 properties: { |
| 9 // The text to show in this label. |
| 10 title: String, |
| 11 |
| 12 // The search term to bold within the title. |
| 13 searchTerm: String, |
| 14 }, |
| 15 |
| 16 observers: ['setSearchedTextToBold_(title, searchTerm)'], |
| 17 |
| 18 /** |
| 19 * Updates the page title. If a search term is specified, highlights any |
| 20 * occurrences of the search term in bold. |
| 21 * @private |
| 22 */ |
| 23 setSearchedTextToBold_: function() { |
| 24 var i = 0; |
| 25 var titleElem = this.$.container; |
| 26 var titleText = this.title; |
| 27 |
| 28 if (this.searchTerm == '' || this.searchTerm == null) { |
| 29 titleElem.textContent = titleText; |
| 30 return; |
| 31 } |
| 32 |
| 33 var re = new RegExp(quoteString(this.searchTerm), 'gim'); |
| 34 var match; |
| 35 titleElem.textContent = ''; |
| 36 while (match = re.exec(titleText)) { |
| 37 if (match.index > i) |
| 38 titleElem.appendChild(document.createTextNode( |
| 39 titleText.slice(i, match.index))); |
| 40 i = re.lastIndex; |
| 41 // Mark the highlighted text in bold. |
| 42 var b = document.createElement('b'); |
| 43 b.textContent = titleText.substring(match.index, i); |
| 44 titleElem.appendChild(b); |
| 45 } |
| 46 if (i < titleText.length) |
| 47 titleElem.appendChild( |
| 48 document.createTextNode(titleText.slice(i))); |
| 49 }, |
| 50 }); |
OLD | NEW |