界面:全局隐藏输入框 Enter 提示,连接信息框内 ✕ 清除;修复内联 SVG 导致全局样式被 DOMPurify 丢弃

This commit is contained in:
nixevol
2026-09-17 09:50:46 +08:00
parent d946473ae3
commit 75af8f9f76
3 changed files with 18 additions and 12 deletions
+6
View File
@@ -207,6 +207,12 @@ class PageTest(unittest.TestCase):
"id": "20260815-a0000001", "kind": "market", "status": "success"
}))
def test_global_css_survives_streamlit_sanitizer(self):
# st.html 经 DOMPurify 清洗,样式里出现 "<"(如内联 SVG)会让整段 <style> 被丢弃。
from ui.layout import COMPACT_CSS
self.assertNotIn("<", COMPACT_CSS)
self.assertIn('[data-testid="InputInstructions"]', COMPACT_CSS)
def test_uploaded_file_is_stored_once_with_exact_bytes(self):
from streamlit.testing.v1 import AppTest
from ui import inputs
+3 -12
View File
@@ -32,18 +32,9 @@ def save_connection(values):
CONNECTION_FILE.write_text(json.dumps(values, ensure_ascii=False, indent=2), encoding="utf-8")
def _clear(state_key):
st.session_state[state_key] = ""
def connection_input(label, key, placeholder=None):
"""带一键清除按钮的文本框;按钮不能放在 form 内,所以连接信息独立于表单。"""
state_key = f"conn_{key}"
text_col, clear_col = st.columns([14, 1], vertical_alignment="bottom")
value = text_col.text_input(label, key=state_key, placeholder=placeholder)
clear_col.button("", icon=":material/close:", key=f"clear_{key}", help="清除",
on_click=_clear, args=(state_key,), use_container_width=True)
return value
"""type=search 的输入框在框内右侧自带 ✕ 清除按钮(样式见 layout.py)。"""
return st.text_input(label, key=f"conn_{key}", placeholder=placeholder, type="search")
def render():
@@ -56,7 +47,7 @@ def render():
st.markdown("**连接信息**(默认显示脚本内配置或上次保存值;修改后保存到本地,下次自动读取)")
url = connection_input("服务地址", "URL", "https://...wfs?...ak=...&sig=...")
cookies = connection_input("登录信息(Cookies)", "COOKIES", "CASTGC=...")
referer = connection_input("来源页(可留空)", "REFERER")
referer = connection_input("来源页(可留空)", "REFERER", "可留空")
with st.form("building_run"):
c1, c2, c3 = st.columns(3)
start = c1.number_input("起始行", 1, value=1)
+9
View File
@@ -124,9 +124,18 @@ hr {
[data-testid="InputInstructions"] {
display: none !important;
}
/* type=search 输入框自带框内右侧 ✕ 清除按钮;隐藏左侧放大镜图标 */
[data-testid="stTextInputRootElement"]:has(input[type="search"]) [data-testid="stTextInputIcon"] {
display: none !important;
}
[data-testid="stTextInputRootElement"]:has(input[type="search"]) {
padding-left: 0 !important;
}
"""
def apply_compact_layout():
# st.html 比 markdown 更稳定地注入样式,避免侧栏折叠按钮样式被忽略。
# 注意:内容经 DOMPurify 清洗,CSS 中不能出现 "<"(例如内联 SVG data URL),否则整段样式会被丢弃。
assert "<" not in COMPACT_CSS, "COMPACT_CSS 不能包含 '<'"
st.html(f"<style>{COMPACT_CSS}</style>")