Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape tooltips for any HTML special characters. #249

Merged
merged 1 commit into from May 29, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion sphinx_gallery/backreferences.py
Expand Up @@ -18,6 +18,14 @@
import cPickle as pickle
except ImportError:
import pickle
# Try Python 3 first, otherwise load from Python 2
try:
from html import escape
except ImportError:
from functools import partial
from xml.sax.saxutils import escape

escape = partial(escape, entities={'"': '"'})


class NameFinder(ast.NodeVisitor):
Expand Down Expand Up @@ -169,7 +177,8 @@ def _thumbnail_div(full_dir, fname, snippet, is_backref=False):
ref_name = os.path.join(full_dir, fname).replace(os.path.sep, '_')

template = BACKREF_THUMBNAIL_TEMPLATE if is_backref else THUMBNAIL_TEMPLATE
return template.format(snippet=snippet, thumbnail=thumb, ref_name=ref_name)
return template.format(snippet=escape(snippet),
thumbnail=thumb, ref_name=ref_name)


def write_backreferences(seen_backrefs, gallery_conf,
Expand Down
4 changes: 2 additions & 2 deletions sphinx_gallery/tests/test_backreferences.py
Expand Up @@ -12,12 +12,12 @@
def test_thumbnail_div():
"""Test if the thumbnail div generates the correct string"""

html_div = sg._thumbnail_div('fake_dir', 'test_file.py', 'test formating')
html_div = sg._thumbnail_div('fake_dir', 'test_file.py', '<"test">')

reference = r"""
.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="test formating">
<div class="sphx-glr-thumbcontainer" tooltip="&lt;&quot;test&quot;&gt;">

.. only:: html

Expand Down