Skip to content

Commit eefe45c

Browse files
authored
Use a srcdoc attribute on iframes (#96)
1 parent 1a4b712 commit eefe45c

File tree

1 file changed

+7
-24
lines changed

1 file changed

+7
-24
lines changed

branca/element.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
"""
88

99
import base64
10+
from html import escape
1011
import json
1112
import warnings
1213
from collections import OrderedDict
13-
import urllib.parse
1414
from urllib.request import urlopen
1515
from uuid import uuid4
1616

@@ -320,43 +320,26 @@ def render(self, **kwargs):
320320
return self._template.render(this=self, kwargs=kwargs)
321321

322322
def _repr_html_(self, **kwargs):
323-
"""Displays the Figure in a Jupyter notebook.
324-
325-
Percent-encoded HTML is stored in data-html attribute, which is used to populate
326-
the iframe. This approach does not encounter the 2MB limit in Chrome for storing
327-
the HTML in the src attribute with a data URI. The alternative of using a srcdoc
328-
attribute is not supported in Microsoft Internet Explorer and Edge.
329-
330-
"""
331-
html = urllib.parse.quote(self.render(**kwargs))
332-
onload = (
333-
'this.contentDocument.open();'
334-
'this.contentDocument.write('
335-
' decodeURIComponent(this.getAttribute(\'data-html\'))'
336-
');'
337-
'this.contentDocument.close();'
338-
)
339-
323+
"""Displays the Figure in a Jupyter notebook."""
324+
html = escape(self.render(**kwargs))
340325
if self.height is None:
341326
iframe = (
342327
'<div style="width:{width};">'
343328
'<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa
344329
'<span style="color:#565656">Make this Notebook Trusted to load map: File -> Trust Notebook</span>' # noqa
345-
'<iframe src="about:blank" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
330+
'<iframe srcdoc="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;' # noqa
346331
'border:none !important;" '
347-
'data-html={html} onload="{onload}" '
348332
'allowfullscreen webkitallowfullscreen mozallowfullscreen>'
349333
'</iframe>'
350334
'</div></div>'
351-
).format(html=html, onload=onload, width=self.width, ratio=self.ratio)
335+
).format(html=html, width=self.width, ratio=self.ratio)
352336
else:
353337
iframe = (
354-
'<iframe src="about:blank" width="{width}" height="{height}"'
338+
'<iframe srcdoc="{html}" width="{width}" height="{height}"'
355339
'style="border:none !important;" '
356-
'data-html={html} onload="{onload}" '
357340
'"allowfullscreen" "webkitallowfullscreen" "mozallowfullscreen">'
358341
'</iframe>'
359-
).format(html=html, onload=onload, width=self.width, height=self.height)
342+
).format(html=html, width=self.width, height=self.height)
360343
return iframe
361344

362345
def add_subplot(self, x, y, n, margin=0.05):

0 commit comments

Comments
 (0)