Skip to content

Commit

Permalink
Ignore script/style when searching for strings; make sure 'void' elem…
Browse files Browse the repository at this point in the history
…ents don't produce extra spaces
  • Loading branch information
cpsievert committed Apr 27, 2022
1 parent 28e4bb6 commit 893cf99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 6 additions & 3 deletions shiny/ui/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def get_window_title(


def _find_child_strings(x: Union[Tag, TagList, TagChild]) -> str:
if isinstance(x, Tag):
if isinstance(x, Tag) and x.name not in ("script", "style"):
x = x.children
if isinstance(x, TagList):
return " ".join([_find_child_strings(y) for y in x])
return x if isinstance(x, str) else ""
strings = [_find_child_strings(y) for y in x]
return " ".join(filter(lambda x: x != "", strings))
if isinstance(x, str):
return x
return ""
31 changes: 30 additions & 1 deletion tests/test_ui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import textwrap

from shiny import ui
from htmltools import HTMLDocument
from htmltools import HTMLDocument, TagList, tags


def test_panel_title():
Expand All @@ -20,3 +20,32 @@ def test_panel_title():
</body>
</html>"""
)

title = TagList(
tags.h1("A title"),
tags.script("foo"),
tags.style("foo"),
tags.h5(tags.script("foo"), "A subtitle"),
)

x = HTMLDocument(ui.panel_title(title)).render()["html"]
assert x == textwrap.dedent(
"""\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="application/html-dependencies">headcontent_9e055af8ef9fa0fb1ba72eeba8053498fbc0d075[0.0]</script>
<title>A title A subtitle</title>
</head>
<body>
<h1>A title</h1>
<script>foo</script>
<style>foo</style>
<h5>
<script>foo</script>
A subtitle
</h5>
</body>
</html>"""
)

0 comments on commit 893cf99

Please sign in to comment.