Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: mo.plain_text rendering (#5764)

Myles Scolnick8d8dc970d31b
diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.py
index ec6fc06629..1888973ab1 100644
--- a/marimo/_output/formatting.py
+++ b/marimo/_output/formatting.py
@@ -233,7 +233,7 @@ def try_format(
return (
FormattedOutput(
mimetype="text/html",
- data=plain_text(escape(data_str)).text,
+ data=plain_text(data_str).text,
)
if data_str
else FormattedOutput.empty()
diff --git a/marimo/_plugins/stateless/plain_text.py b/marimo/_plugins/stateless/plain_text.py
index be33d0aeb1..c086165705 100644
--- a/marimo/_plugins/stateless/plain_text.py
+++ b/marimo/_plugins/stateless/plain_text.py
@@ -1,6 +1,8 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+import html
+
from marimo._output.builder import h
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
@@ -18,5 +20,5 @@ def plain_text(text: str) -> Html:
An `Html` object representing the text.
"""
styles = create_style({"font-size": "12px"})
- img = h.pre(child=text, style=styles)
+ img = h.pre(child=html.escape(text), style=styles)
return Html(img)
diff --git a/tests/_plugins/stateless/test_plain_text.py b/tests/_plugins/stateless/test_plain_text.py
new file mode 100644
index 0000000000..ceeead5ae6
--- /dev/null
+++ b/tests/_plugins/stateless/test_plain_text.py
@@ -0,0 +1,46 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from marimo._plugins.stateless.plain_text import plain_text
+
+
+def test_plain_text_basic() -> None:
+ """Test basic plain_text functionality."""
+ result = plain_text("Hello, World!")
+ assert "Hello, World!" in result.text
+ assert "<pre" in result.text
+ assert "</pre>" in result.text
+
+
+def test_plain_text_html_escaping() -> None:
+ """Test that plain_text properly escapes HTML characters."""
+ # Test the reported bug case
+ result = plain_text("<x")
+ assert "&lt;x" in result.text
+ assert "<x" not in result.text or result.text.count("<x") == 0
+
+ # Test other HTML characters
+ result = plain_text("</script>")
+ assert "&lt;/script&gt;" in result.text
+
+ result = plain_text("<div>content</div>")
+ assert "&lt;div&gt;content&lt;/div&gt;" in result.text
+
+ result = plain_text("&amp;")
+ assert "&amp;amp;" in result.text
+
+
+def test_plain_text_preserves_whitespace() -> None:
+ """Test that plain_text preserves spaces and newlines."""
+ text_with_spaces = "line 1\n line 2 with spaces\nline 3"
+ result = plain_text(text_with_spaces)
+ # The text should contain the original spacing
+ assert "line 1" in result.text
+ assert " line 2 with spaces" in result.text
+ assert "line 3" in result.text
+
+
+def test_plain_text_empty_string() -> None:
+ """Test plain_text with empty string."""
+ result = plain_text("")
+ assert result.text == "<pre style='font-size: 12px'></pre>"