Merge pull request #2735 from GuillaumeGomez/fix-js-error

Fix JS error when `hasFocus` method is overwritten on search index load
This commit is contained in:
Eric Huss 2025-06-28 19:55:37 +00:00 committed by GitHub
commit e395341210
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 9 additions and 4 deletions

View file

@ -69,7 +69,7 @@ pub fn create_files(
// To reduce the size of the generated JSON by preventing all `"` characters to be // 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. // escaped, we instead surround the string with much less common `'` character.
format!( format!(
"window.search = JSON.parse('{}');", "window.search = Object.assign(window.search, JSON.parse('{}'));",
index.replace("\\", "\\\\").replace("'", "\\'") index.replace("\\", "\\\\").replace("'", "\\'")
) )
.as_bytes(), .as_bytes(),

View file

@ -1,5 +1,6 @@
// This tests basic search behavior. // This tests basic search behavior.
fail-on-js-error: true
go-to: |DOC_PATH| + "index.html" go-to: |DOC_PATH| + "index.html"
define-function: ( define-function: (
@ -41,6 +42,9 @@ press-key: 's'
wait-for-css-false: ("#search-wrapper", {"display": "none"}) wait-for-css-false: ("#search-wrapper", {"display": "none"})
// We ensure the search bar has the focus. // We ensure the search bar has the focus.
assert: "#searchbar: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. // Now we press `Escape` to ensure that the search input disappears again.
press-key: 'Escape' press-key: 'Escape'

View file

@ -9,8 +9,9 @@ use std::path::{Path, PathBuf};
fn read_book_index(root: &Path) -> serde_json::Value { fn read_book_index(root: &Path) -> serde_json::Value {
let index = root.join("book/searchindex.js"); let index = root.join("book/searchindex.js");
let index = std::fs::read_to_string(index).unwrap(); let index = std::fs::read_to_string(index).unwrap();
let index = index.trim_start_matches("window.search = JSON.parse('"); let index =
let index = index.trim_end_matches("');"); 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. // We need unescape the string as it's supposed to be an escaped JS string.
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap() serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
} }

File diff suppressed because one or more lines are too long