• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

修订版0579eecf8e202f0e0ab0e14dbcd7079ec8740418 (tree)
时间2017-09-25 04:27:16
作者umorigu <umorigu@gmai...>
Commiterumorigu

Log Message

Color search text in body

更改概述

差异

--- a/skin/search2.js
+++ b/skin/search2.js
@@ -136,7 +136,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
136136 results.forEach(function(val, index) {
137137 var fragment = document.createDocumentFragment();
138138 var li = document.createElement('li');
139- var href = val.url;
139+ var href = val.url + '#q=' + encodeSearchTextForHash(searchText);
140140 var decoratedName = findAndDecorateText(val.name, searchRegex);
141141 if (! decoratedName) {
142142 decoratedName = escapeHTML(val.name);
@@ -148,7 +148,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
148148 } else {
149149 updatedAt = val.updated_at;
150150 }
151- var liHtml = '<a href="' + href + '">' + decoratedName + '</a> ' +
151+ var liHtml = '<a href="' + escapeHTML(href) + '">' + decoratedName + '</a> ' +
152152 getPassage(now, updatedAt);
153153 li.innerHTML = liHtml;
154154 fragment.appendChild(li);
@@ -210,6 +210,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
210210 kanaMap = map;
211211 }
212212 function textToRegex(searchText) {
213+ if (!searchText) return null;
213214 var regEscape = /[\\^$.*+?()[\]{}|]/g;
214215 // 1:Symbol 2:Katakana 3:Hiragana
215216 var regRep = /([\\^$.*+?()[\]{}|])|([\u30a1-\u30f6])|([\u3041-\u3096])/g;
@@ -336,29 +337,6 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
336337 return blocks;
337338 //return foundLines.join('\n');
338339 }
339- function findAndDecorateText(text, searchRegex) {
340- var isReplaced = false;
341- var lastIndex = 0;
342- var m;
343- var decorated = '';
344- searchRegex.lastIndex = 0;
345- while ((m = searchRegex.exec(text)) !== null) {
346- isReplaced = true;
347- var pre = text.substring(lastIndex, m.index);
348- decorated += escapeHTML(pre);
349- for (var i = 1; i < m.length; i++) {
350- if (m[i]) {
351- decorated += '<strong class="word' + (i - 1) + '">' + escapeHTML(m[i]) + '</strong>'
352- }
353- }
354- lastIndex = searchRegex.lastIndex;
355- }
356- if (isReplaced) {
357- decorated += escapeHTML(text.substr(lastIndex));
358- return decorated;
359- }
360- return null;
361- }
362340 function getSummary(bodyText, searchRegex) {
363341 return getTargetLines(bodyText, searchRegex);
364342 }
@@ -453,7 +431,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
453431 var loc = document.location;
454432 var url = loc.protocol + '//' + loc.host + loc.pathname +
455433 '?cmd=search2' +
456- '&q=' + encodeSearthText(q) +
434+ '&q=' + encodeSearchText(q) +
457435 (base ? '&base=' + encodeURIComponent(base) : '');
458436 e.preventDefault();
459437 setTimeout(function() {
@@ -485,13 +463,84 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
485463 }
486464 }
487465 });
488- function encodeSearthText(q) {
489- var sp = q.split(/\s+/);
490- for (var i = 0; i < sp.length; i++) {
491- sp[i] = encodeURIComponent(sp[i]);
466+ }
467+ function encodeSearchText(q) {
468+ var sp = q.split(/\s+/);
469+ for (var i = 0; i < sp.length; i++) {
470+ sp[i] = encodeURIComponent(sp[i]);
471+ }
472+ return sp.join('+');
473+ }
474+ function encodeSearchTextForHash(q) {
475+ var sp = q.split(/\s+/);
476+ return sp.join('+');
477+ }
478+ function findAndDecorateText(text, searchRegex) {
479+ var isReplaced = false;
480+ var lastIndex = 0;
481+ var m;
482+ var decorated = '';
483+ searchRegex.lastIndex = 0;
484+ while ((m = searchRegex.exec(text)) !== null) {
485+ isReplaced = true;
486+ var pre = text.substring(lastIndex, m.index);
487+ decorated += escapeHTML(pre);
488+ for (var i = 1; i < m.length; i++) {
489+ if (m[i]) {
490+ decorated += '<strong class="word' + (i - 1) + '">' + escapeHTML(m[i]) + '</strong>'
491+ }
492+ }
493+ lastIndex = searchRegex.lastIndex;
494+ }
495+ if (isReplaced) {
496+ decorated += escapeHTML(text.substr(lastIndex));
497+ return decorated;
498+ }
499+ return null;
500+ }
501+ function getSearchTextInLocationHash() {
502+ // TODO Cross browser
503+ var hash = location.hash;
504+ var q = '';
505+ if (hash.substr(0, 3) === '#q=') {
506+ q = hash.substr(3).replace(/\+/g, ' ');
507+ }
508+ return q;
509+ }
510+ function colorSearchTextInBody() {
511+ var searchText = getSearchTextInLocationHash();
512+ if (!searchText) return;
513+ var searchRegex = textToRegex(removeSearchOperators(searchText));
514+ var headReText = '([\\s\\b]|^)';
515+ var tailReText = '\\b';
516+ var ignoreTags = ['INPUT', 'TEXTAREA', 'BUTTON',
517+ 'SCRIPT', 'FRAME', 'IFRAME'];
518+ function colorSearchText(element, searchRegex) {
519+ var decorated = findAndDecorateText(element.nodeValue, searchRegex);
520+ if (decorated) {
521+ var span = document.createElement('span');
522+ span.innerHTML = decorated;
523+ element.parentNode.replaceChild(span, element);
524+ }
525+ }
526+ function walkElement(element) {
527+ var e = element.firstChild;
528+ while (e) {
529+ if (e.nodeType == 3 && e.nodeValue &&
530+ e.nodeValue.length >= 2 && /\S/.test(e.nodeValue)) {
531+ var next = e.nextSibling;
532+ colorSearchText(e, searchRegex);
533+ e = next;
534+ } else {
535+ if (e.nodeType == 1 && ignoreTags.indexOf(e.tagName) == -1) {
536+ walkElement(e);
537+ }
538+ e = e.nextSibling;
539+ }
492540 }
493- return sp.join('+');
494541 }
542+ var target = document.getElementById('body');
543+ walkElement(target);
495544 }
496545 function isEnabledFetchFunctions() {
497546 if (window.fetch && document.querySelector) {
@@ -508,6 +557,7 @@ window.addEventListener && window.addEventListener('DOMContentLoaded', function(
508557 if (props.json_enabled) return true;
509558 return false;
510559 }
560+ colorSearchTextInBody();
511561 if (! isEnabledFetchFunctions()) return;
512562 if (! isEnableServerFunctions()) return;
513563 replaceSearchWithSearch2();