Skip to content

Commit e395341

Browse files
authored
Merge pull request #2735 from GuillaumeGomez/fix-js-error
Fix JS error when `hasFocus` method is overwritten on search index load
2 parents e8dff6f + f4c5417 commit e395341

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

src/renderer/html_handlebars/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn create_files(
6969
// To reduce the size of the generated JSON by preventing all `"` characters to be
7070
// escaped, we instead surround the string with much less common `'` character.
7171
format!(
72-
"window.search = JSON.parse('{}');",
72+
"window.search = Object.assign(window.search, JSON.parse('{}'));",
7373
index.replace("\\", "\\\\").replace("'", "\\'")
7474
)
7575
.as_bytes(),

tests/gui/search.goml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This tests basic search behavior.
22

3+
fail-on-js-error: true
34
go-to: |DOC_PATH| + "index.html"
45

56
define-function: (
@@ -41,6 +42,9 @@ press-key: 's'
4142
wait-for-css-false: ("#search-wrapper", {"display": "none"})
4243
// We ensure the search bar has the focus.
4344
assert: "#searchbar:focus"
45+
// Pressing a key will therefore update the search input.
46+
press-key: 't'
47+
assert-text: ("#searchbar", "t")
4448

4549
// Now we press `Escape` to ensure that the search input disappears again.
4650
press-key: 'Escape'

tests/testsuite/search.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use std::path::{Path, PathBuf};
99
fn read_book_index(root: &Path) -> serde_json::Value {
1010
let index = root.join("book/searchindex.js");
1111
let index = std::fs::read_to_string(index).unwrap();
12-
let index = index.trim_start_matches("window.search = JSON.parse('");
13-
let index = index.trim_end_matches("');");
12+
let index =
13+
index.trim_start_matches("window.search = Object.assign(window.search, JSON.parse('");
14+
let index = index.trim_end_matches("'));");
1415
// We need unescape the string as it's supposed to be an escaped JS string.
1516
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
1617
}

0 commit comments

Comments
 (0)