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

Unified Diff: chrome/browser/resources/md_history/history_item.js

Issue 1643693003: MD History: Implement search functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@patch_to_be_uploaded
Patch Set: Fix commenting. Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/md_history/history_item.js
diff --git a/chrome/browser/resources/md_history/history_item.js b/chrome/browser/resources/md_history/history_item.js
index 242660db8aaf7a02a7c50c78b4db2e6fa31360de..a12844bbb8c0dab280e3c6094ea1c4ca7eb88fcc 100644
--- a/chrome/browser/resources/md_history/history_item.js
+++ b/chrome/browser/resources/md_history/history_item.js
@@ -13,7 +13,8 @@ Polymer({
websiteTitle_: {
type: String,
- value: ''
+ value: '',
+ observer: 'setSearchedTextToBold_'
},
// Domain is the website text shown on the history-item next to the title.
@@ -47,6 +48,13 @@ Polymer({
value: 0
},
+ // Search term used in the chrome.send to obtain this history-item.
+ searchTerm: {
+ type: String,
+ value: '',
+ observer: 'setSearchedTextToBold_'
+ },
+
selected: {
type: Boolean,
value: false,
@@ -90,5 +98,45 @@ Polymer({
// Stops the 'tap' event from closing the menu when it opens.
e.stopPropagation();
+ },
+
+ /**
+ * If the results shown are search results set the search term to be bold
+ * where it is displayed in the history-item title.
+ * @private
+ */
+ setSearchedTextToBold_: function() {
+ var i = 0;
calamity 2016/02/05 02:30:10 Pull this.$.title into a local var here.
tsergeant 2016/02/08 22:59:01 Bump on the comments in this file.
hsampson 2016/02/09 02:17:51 Done.
+ if ((this.searchTerm != '') && (this.searchTerm != null)) {
calamity 2016/02/05 02:30:10 nit: less parens.
hsampson 2016/02/09 02:17:51 Done.
+ var searchText = new RegExp(this.quoteString_(this.searchTerm), 'gim');
calamity 2016/02/05 02:30:10 nit: TBH, I kinda like just calling this re. searc
hsampson 2016/02/09 02:17:51 Done.
+ var match;
+ this.$.title.textContent = '';
+ while (match = searchText.exec(this.websiteTitle_)) {
+ if (match.index > i)
calamity 2016/02/05 02:30:10 nit: This one needs braces since the content is 2
hsampson 2016/02/09 02:17:51 NA now.
+ this.$.title.appendChild(document.createTextNode(
+ this.websiteTitle_.slice(i, match.index)));
+ i = searchText.lastIndex;
+ // Mark the highlighted text in bold.
+ var b = document.createElement('b');
+ b.textContent = this.websiteTitle_.substring(match.index, i);
+ this.$.title.appendChild(b);
+ }
+ if (i < this.websiteTitle_.length)
+ this.$.title.appendChild(document.createTextNode(
+ this.websiteTitle_.slice(i)));
+ } else {
+ this.$.title.textContent = this.websiteTitle_;
calamity 2016/02/05 02:30:10 Invert the condition above and early return in thi
hsampson 2016/02/09 02:17:51 No idea what that is :P but done I think.
+ }
+ },
+
+ /**
+ * Quote a string so it can be used in a regular expression.
+ * @param {string} searchTerm The source string.
+ * @return {string} The escaped string.
+ * @private
+ */
+ quoteString_: function(searchTerm) {
+ return searchTerm.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g,
+ '\\$1');
}
});

Powered by Google App Engine
This is Rietveld 408576698