Skip to content

Commit 6b5500e

Browse files
authored
fix: don't sanitize icons in markdown (#7001)
Fixes #6999 Don't sanitize `iconify-icon`. Some styling changes to be aligned inside buttons. <img width="741" height="412" alt="image" src="https://github.com/user-attachments/assets/5a545ce9-58e6-4843-a409-50c978cc1c6a" />
1 parent 46bd376 commit 6b5500e

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

frontend/src/css/md.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ a .markdown iconify-icon:first-child {
7878
margin-inline-end: 0.4em;
7979
}
8080

81+
iconify-icon {
82+
display: inline-flex;
83+
align-items: center;
84+
}
85+
8186
/* align icons with buttons better */
8287
button .markdown .paragraph {
88+
display: inline-flex;
8389
align-items: baseline;
8490
gap: 0.4em;
8591

frontend/src/plugins/core/__test__/sanitize.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,31 @@ describe("sanitizeHtml", () => {
462462
`"<details><summary>Click me</summary><p>Hidden content</p></details>"`,
463463
);
464464
});
465+
466+
test("preserves iconify-icon custom element", () => {
467+
const html = '<iconify-icon icon="lucide:leaf"></iconify-icon>';
468+
expect(sanitizeHtml(html)).toMatchInlineSnapshot(
469+
`"<iconify-icon icon="lucide:leaf"></iconify-icon>"`,
470+
);
471+
});
472+
473+
test("preserves iconify-icon with all attributes", () => {
474+
const html =
475+
'<iconify-icon icon="lucide:rocket" width="24px" height="24px" inline="" flip="horizontal" rotate="90deg" style="color: blue;"></iconify-icon>';
476+
expect(sanitizeHtml(html)).toMatchInlineSnapshot(
477+
`"<iconify-icon icon="lucide:rocket" width="24px" height="24px" inline="" flip="horizontal" rotate="90deg" style="color: blue;"></iconify-icon>"`,
478+
);
479+
});
480+
481+
test("preserves self-closing iconify-icon", () => {
482+
const html = '<iconify-icon icon="lucide:star" />';
483+
expect(sanitizeHtml(html)).toMatchInlineSnapshot(
484+
`"<iconify-icon icon="lucide:star"></iconify-icon>"`,
485+
);
486+
});
487+
488+
test("still removes other non-marimo/non-iconify custom elements", () => {
489+
const html = "<some-custom-element>Content</some-custom-element>";
490+
expect(sanitizeHtml(html)).toMatchInlineSnapshot(`"Content"`);
491+
});
465492
});

frontend/src/plugins/core/sanitize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function sanitizeHtml(html: string) {
7575
// glue elements like style, script or others to document.body and prevent unintuitive browser behavior in several edge-cases
7676
FORCE_BODY: true,
7777
CUSTOM_ELEMENT_HANDLING: {
78-
tagNameCheck: /^marimo-[A-Za-z][\w-]*$/,
78+
tagNameCheck: /^(marimo-[A-Za-z][\w-]*|iconify-icon)$/,
7979
attributeNameCheck: /^[A-Za-z][\w-]*$/,
8080
},
8181
};

marimo/_smoke_tests/icons.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import marimo
1010

11-
__generated_with = "0.15.5"
11+
__generated_with = "0.17.3"
1212
app = marimo.App()
1313

1414

@@ -92,11 +92,7 @@ def _(mo):
9292

9393
@app.cell
9494
def _(mo):
95-
mo.md(
96-
f"""
97-
## {mo.icon('material-symbols:edit')} Icons in markdown
98-
"""
99-
)
95+
mo.md(f"""## {mo.icon('material-symbols:edit')} Icons in markdown""")
10096
return
10197

10298

0 commit comments

Comments
 (0)