Skip to content

Fix JS error when hasFocus method is overwritten on search index load #2735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn create_files(
// To reduce the size of the generated JSON by preventing all `"` characters to be
// escaped, we instead surround the string with much less common `'` character.
format!(
"window.search = JSON.parse('{}');",
"window.search = Object.assign(window.search, JSON.parse('{}'));",
index.replace("\\", "\\\\").replace("'", "\\'")
)
.as_bytes(),
Expand Down
4 changes: 4 additions & 0 deletions tests/gui/search.goml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This tests basic search behavior.

fail-on-js-error: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make this value the default in browser-ui-test because it would have helped me catch this error much sooner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #2736.

go-to: |DOC_PATH| + "index.html"

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

// Now we press `Escape` to ensure that the search input disappears again.
press-key: 'Escape'
Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::path::{Path, PathBuf};
fn read_book_index(root: &Path) -> serde_json::Value {
let index = root.join("book/searchindex.js");
let index = std::fs::read_to_string(index).unwrap();
let index = index.trim_start_matches("window.search = JSON.parse('");
let index = index.trim_end_matches("');");
let index =
index.trim_start_matches("window.search = Object.assign(window.search, JSON.parse('");
let index = index.trim_end_matches("'));");
// We need unescape the string as it's supposed to be an escaped JS string.
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
}
Expand Down
Loading