Skip to content

Commit 4f0f6f3

Browse files
committed
fix: escape HTML in search keywords
1 parent c9301b0 commit 4f0f6f3

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/core/event/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export function Events(Base) {
478478
const newActive = dom
479479
.find(
480480
sidebar,
481-
`a[href="${href}"], a[href="${decodeURIComponent(href)}"]`,
481+
`a[href="${CSS.escape(href)}"], a[href="${CSS.escape(decodeURIComponent(href))}"]`,
482482
)
483483
?.closest('li');
484484

src/plugins/search/component.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { search } from './search.js';
1+
import { escapeHtml, search } from './search.js';
22
import cssText from './style.css';
33

44
let NO_DATA_TEXT = '';
@@ -75,11 +75,11 @@ function bindEvents() {
7575
let timeId;
7676

7777
/**
78-
Prevent to Fold sidebar.
79-
80-
When searching on the mobile end,
81-
the sidebar is collapsed when you click the INPUT box,
82-
making it impossible to search.
78+
* Prevent to Fold sidebar.
79+
*
80+
* When searching on the mobile end,
81+
* the sidebar is collapsed when you click the INPUT box,
82+
* making it impossible to search.
8383
*/
8484
Docsify.dom.on(
8585
$search,
@@ -129,10 +129,10 @@ export function init(opts, vm) {
129129
return;
130130
}
131131

132-
const keywords = vm.router.parse().query.s;
132+
const keywords = vm.router.parse().query.s || '';
133133

134134
Docsify.dom.style(cssText);
135-
tpl(vm, keywords);
135+
tpl(vm, escapeHtml(keywords));
136136
bindEvents();
137137
keywords && setTimeout(_ => doSearch(keywords), 500);
138138
}

src/plugins/search/search.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { markdownToTxt } from './markdown-to-txt.js';
66
import Dexie from 'dexie';
77

8-
let INDEXES = {};
8+
let INDEXES = [];
99

1010
const db = new Dexie('docsify');
1111
db.version(1).stores({
@@ -48,7 +48,7 @@ function resolveIndexKey(namespace) {
4848
: LOCAL_STORAGE.INDEX_KEY;
4949
}
5050

51-
function escapeHtml(string) {
51+
export function escapeHtml(string) {
5252
const entityMap = {
5353
'&': '&',
5454
'<': '&lt;',
@@ -102,7 +102,7 @@ function getListData(token) {
102102
export function genIndex(path, content = '', router, depth, indexKey) {
103103
const tokens = window.marked.lexer(content);
104104
const slugify = window.Docsify.slugify;
105-
const index = {};
105+
const index = [];
106106
let slug;
107107
let title = '';
108108

@@ -299,7 +299,7 @@ export async function init(config, vm) {
299299
INDEXES = await getData(indexKey);
300300

301301
if (isExpired) {
302-
INDEXES = {};
302+
INDEXES = [];
303303
} else if (!isAuto) {
304304
return;
305305
}

0 commit comments

Comments
 (0)