Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions mkdocs_likec4/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def to_html(cls, opts: ViewOptions) -> str:
)

tag = f"{opts.project.lower()}-view" if valid_project else "likec4-view"
return (
f'<{tag} view-id="{escape(opts.view_id, quote=True)}" '
f'browser="{opts.browser}" dynamic-variant="{opts.dynamic_variant}"></{tag}>'
)
attrs = f'view-id="{escape(opts.view_id, quote=True)}"'
if opts.browser != "true":
attrs += f' browser="{opts.browser}"'
if opts.dynamic_variant != "diagram":
attrs += f' dynamic-variant="{opts.dynamic_variant}"'
return f"<{tag} {attrs}></{tag}>"
34 changes: 18 additions & 16 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,19 @@ def test_basic_html_output(self):
"""Test basic HTML output without project."""
opts = ViewOptions(view_id="my-view")
html = LikeC4Parser.to_html(opts)
assert (
html
== '<likec4-view view-id="my-view" browser="true" dynamic-variant="diagram"></likec4-view>'
)
assert html == '<likec4-view view-id="my-view"></likec4-view>'

def test_html_with_project(self):
"""Test HTML output with valid project."""
opts = ViewOptions(view_id="my-view", project="myproject")
html = LikeC4Parser.to_html(opts)
assert (
html
== '<myproject-view view-id="my-view" browser="true" dynamic-variant="diagram"></myproject-view>'
)
assert html == '<myproject-view view-id="my-view"></myproject-view>'

def test_html_with_uppercase_project(self):
"""Test HTML output with uppercase project (should be lowercased)."""
opts = ViewOptions(view_id="my-view", project="MyProject")
html = LikeC4Parser.to_html(opts)
assert (
html
== '<myproject-view view-id="my-view" browser="true" dynamic-variant="diagram"></myproject-view>'
)
assert html == '<myproject-view view-id="my-view"></myproject-view>'

def test_html_with_custom_options(self):
"""Test HTML output with custom options."""
Expand All @@ -288,14 +279,25 @@ def test_html_with_custom_options(self):
== '<proj-view view-id="test" browser="false" dynamic-variant="sequence"></proj-view>'
)

def test_html_omits_only_default_browser(self):
"""Only dynamic-variant is emitted when browser is at default."""
opts = ViewOptions(view_id="v", dynamic_variant="sequence")
html = LikeC4Parser.to_html(opts)
assert (
html == '<likec4-view view-id="v" dynamic-variant="sequence"></likec4-view>'
)

def test_html_omits_only_default_dynamic_variant(self):
"""Only browser is emitted when dynamic-variant is at default."""
opts = ViewOptions(view_id="v", browser="false")
html = LikeC4Parser.to_html(opts)
assert html == '<likec4-view view-id="v" browser="false"></likec4-view>'

def test_html_with_invalid_project_falls_back(self):
"""Test HTML output with invalid project name falls back to likec4-view."""
opts = ViewOptions(view_id="my-view", project="123invalid")
html = LikeC4Parser.to_html(opts)
assert (
html
== '<likec4-view view-id="my-view" browser="true" dynamic-variant="diagram"></likec4-view>'
)
assert html == '<likec4-view view-id="my-view"></likec4-view>'

def test_html_escapes_invalid_view_id_with_quotes(self):
"""Test that invalid view ID with quotes is escaped to prevent XSS."""
Expand Down
Loading