Revision | 6d89ff3537c66f4ee4f37ed0256302ef781f04df (tree) |
---|---|
Time | 2017-09-25 04:57:08 |
Author | umorigu <umorigu@gmai...> |
Commiter | umorigu |
search2: Highlight search text for Firefox (auto encodeURIComponent)
@@ -136,7 +136,8 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
136 | 136 | results.forEach(function(val, index) { |
137 | 137 | var fragment = document.createDocumentFragment(); |
138 | 138 | var li = document.createElement('li'); |
139 | - var href = val.url + '#q=' + encodeSearchTextForHash(searchText); | |
139 | + var hash = '#q=' + encodeSearchTextForHash(searchText); | |
140 | + var href = val.url + hash; | |
140 | 141 | var decoratedName = findAndDecorateText(val.name, searchRegex); |
141 | 142 | if (! decoratedName) { |
142 | 143 | decoratedName = escapeHTML(val.name); |
@@ -151,6 +152,13 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
151 | 152 | var liHtml = '<a href="' + escapeHTML(href) + '">' + decoratedName + '</a> ' + |
152 | 153 | getPassage(now, updatedAt); |
153 | 154 | li.innerHTML = liHtml; |
155 | + var a = li.querySelector('a'); | |
156 | + if (a && a.hash) { | |
157 | + if (a.hash !== hash) { | |
158 | + // Some browser execute encodeHTML(hash) automatically. Support them. | |
159 | + a.href = val.url + '#encq=' + encodeSearchTextForHash(searchText); | |
160 | + } | |
161 | + } | |
154 | 162 | fragment.appendChild(li); |
155 | 163 | var div = document.createElement('div'); |
156 | 164 | div.classList.add('search-result-detail'); |
@@ -501,9 +509,12 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function( | ||
501 | 509 | function getSearchTextInLocationHash() { |
502 | 510 | // TODO Cross browser |
503 | 511 | var hash = location.hash; |
512 | + if (!hash) return ''; | |
504 | 513 | var q = ''; |
505 | 514 | if (hash.substr(0, 3) === '#q=') { |
506 | 515 | q = hash.substr(3).replace(/\+/g, ' '); |
516 | + } else if (hash.substr(0, 6) === '#encq=') { | |
517 | + q = decodeURIComponent(hash.substr(6).replace(/\+/g, ' ')); | |
507 | 518 | } |
508 | 519 | return q; |
509 | 520 | } |