Skip to content

Commit 4e8d00f

Browse files
Fix malformed fullscreen attributes on Figure iframe with a height
Figure._repr_html_ has two iframe templates. The one used when height is None emits the bare boolean attributes allowfullscreen webkitallowfullscreen mozallowfullscreen but the one used when a height is set wrapped each in quotes: "allowfullscreen" "webkitallowfullscreen" "mozallowfullscreen" so the attribute names came out with literal quote characters around them and browsers ignored all three -- fullscreen did not work whenever a height was given. The same branch was also missing the space before the style attribute, giving height="..."style="...". Emit the attributes the same way as the height-less branch. Output is now identical to that branch apart from the width/height. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent c3a01b3 commit 4e8d00f

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎branca/element.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ def _repr_html_(self, **kwargs) -> str:
425425
).format(html=html, width=self.width, ratio=self.ratio)
426426
else:
427427
iframe = (
428-
'<iframe srcdoc="{html}" width="{width}" height="{height}"'
428+
'<iframe srcdoc="{html}" width="{width}" height="{height}" '
429429
'style="border:none !important;" '
430-
'"allowfullscreen" "webkitallowfullscreen" "mozallowfullscreen">'
430+
"allowfullscreen webkitallowfullscreen mozallowfullscreen>"
431431
"</iframe>"
432432
).format(html=html, width=self.width, height=self.height)
433433
return iframe

‎tests/test_element.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Tests for branca.element
3+
------------------------
4+
"""
5+
6+
from html.parser import HTMLParser
7+
8+
import branca.element as elem
9+
10+
11+
class _IframeAttrs(HTMLParser):
12+
"""Collect the attributes of every <iframe> start tag."""
13+
14+
def __init__(self):
15+
super().__init__()
16+
self.iframes = []
17+
18+
def handle_starttag(self, tag, attrs):
19+
if tag == "iframe":
20+
self.iframes.append(dict(attrs))
21+
22+
23+
def _iframe_attrs(html):
24+
parser = _IframeAttrs()
25+
parser.feed(html)
26+
assert len(parser.iframes) == 1
27+
return parser.iframes[0]
28+
29+
30+
def test_figure_repr_html_fullscreen_attrs_without_height():
31+
attrs = _iframe_attrs(elem.Figure()._repr_html_())
32+
for name in ("allowfullscreen", "webkitallowfullscreen", "mozallowfullscreen"):
33+
assert name in attrs
34+
35+
36+
def test_figure_repr_html_fullscreen_attrs_with_height():
37+
# The height branch used to quote the boolean attributes, producing
38+
# <iframe ... "allowfullscreen" ...>, so the attribute names came out
39+
# wrapped in literal quotes and the browser ignored them.
40+
attrs = _iframe_attrs(elem.Figure(height="400px")._repr_html_())
41+
for name in ("allowfullscreen", "webkitallowfullscreen", "mozallowfullscreen"):
42+
assert name in attrs, f"{name!r} missing; got {sorted(attrs)}"
43+
assert attrs["height"] == "400px"
44+
assert attrs["width"] == "100%"

0 commit comments

Comments
 (0)