Browse Source

report page: sync js state with browser-restored form controls after reload

Alexander Korolev 1 tháng trước cách đây
mục cha
commit
d481969512
2 tập tin đã thay đổi với 14 bổ sung và 0 xóa
  1. 5 0
      src/report/page.html
  2. 9 0
      src/report/tests.rs

+ 5 - 0
src/report/page.html

@@ -71,6 +71,11 @@ function cell(r, k) {
 
 const S = { q: "", group: "mq", sort: "name", dir: 1, hidden: new Set(["title", "image", "port"]),
             page: 0, size: 50, closed: new Set(), openRow: null };
+// the browser restores form controls across page reloads — follow them
+// (otherwise the select shows the old choice while the table stays grouped
+// by the hardcoded default)
+S.q = document.getElementById("q").value;
+S.group = document.getElementById("group").value;
 
 const esc = s => String(s).replace(/[&<>"]/g, c => ({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"}[c]));
 

+ 9 - 0
src/report/tests.rs

@@ -271,6 +271,15 @@ fn html_page_embeds_data_escaped() {
     assert!(!html_page(&evil).contains("</script>b"));
 }
 
+#[test]
+fn html_page_syncs_state_from_restored_form_controls() {
+    // after a browser page reload the select/input are restored but the JS
+    // state defaults are stale — the state must follow the DOM
+    let page = html_page(&[]);
+    assert!(page.contains("S.q = document.getElementById(\"q\").value;"));
+    assert!(page.contains("S.group = document.getElementById(\"group\").value;"));
+}
+
 #[test]
 fn report_keys_fall_back_outside_en() {
     let _ = fluent_i18n::set_locale(Some("ru"));