Skip to content

Self-hosted AI workspace.

Python

fix: langIcon throws on an explicit null opts argument (#1740)

Afonso Coutinhobabfb6cfa7e4
diff --git a/static/js/langIcons.js b/static/js/langIcons.js
index d34fae149..c2afdf809 100644
--- a/static/js/langIcons.js
+++ b/static/js/langIcons.js
@@ -175,8 +175,8 @@ export function langIcon(lang, size = 14, opts = {}) {
const key = String(lang).toLowerCase();
const inner = ICONS[key] || ICONS[ALIASES[key]] || '';
if (!inner) return '';
- const cls = opts.className ? ` class="${opts.className}"` : '';
- const style = opts.style ? ` style="${opts.style}"` : '';
+ const cls = (opts && opts.className) ? ` class="${opts.className}"` : '';
+ const style = (opts && opts.style) ? ` style="${opts.style}"` : '';
return (
`<svg${cls}${style} width="${size}" height="${size}" viewBox="0 0 24 24" ` +
`fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">` +
diff --git a/tests/test_lang_icon_null_opts_js.py b/tests/test_lang_icon_null_opts_js.py
new file mode 100644
index 000000000..b66fd2851
--- /dev/null
+++ b/tests/test_lang_icon_null_opts_js.py
@@ -0,0 +1,40 @@
+"""Pin langIcon (static/js/langIcons.js) against an explicit null opts.
+Driven through `node --input-type=module`; skips without node.
+"""
+import json
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+_REPO = Path(__file__).resolve().parent.parent
+_HELPER = _REPO / "static" / "js" / "langIcons.js"
+_HAS_NODE = shutil.which("node") is not None
+
+
+def _icon(lang, size, opts):
+ js = f"""
+ import {{ langIcon }} from '{_HELPER.as_posix()}';
+ console.log(langIcon({json.dumps(lang)}, {json.dumps(size)}, {json.dumps(opts)}));
+ """
+ proc = subprocess.run(
+ ["node", "--input-type=module"],
+ input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30,
+ )
+ assert proc.returncode == 0, proc.stderr
+ return proc.stdout.strip()
+
+
[email protected](not _HAS_NODE, reason="node binary not on PATH")
+def test_lang_icon_tolerates_null_opts():
+ # `opts = {}` default only applies when the arg is omitted; an explicit
+ # null (easy to pass) hit opts.className and threw a TypeError.
+ out = _icon("python", 14, None)
+ assert out.startswith("<svg")
+ assert "class=" not in out
+
+
[email protected](not _HAS_NODE, reason="node binary not on PATH")
+def test_lang_icon_applies_opts_when_given():
+ assert 'class="ic"' in _icon("python", 14, {"className": "ic"})