Merge pull request #4758 from tk0miya/epub_escape_toc.ncx

epub: Fix docTitle elements of toc.ncx is not escaped
This commit is contained in:
Takeshi KOMIYA 2018-03-20 23:08:10 +09:00 committed by GitHub
commit cf3de7d62b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 2 deletions

View File

@ -33,6 +33,7 @@ Bugs fixed
* #4688: Error to download remote images having long URL * #4688: Error to download remote images having long URL
* #4754: sphinx/pycode/__init__.py raises AttributeError * #4754: sphinx/pycode/__init__.py raises AttributeError
* #1435: qthelp builder should htmlescape keywords * #1435: qthelp builder should htmlescape keywords
* epub: Fix docTitle elements of toc.ncx is not escaped
Testing Testing
-------- --------

View File

@ -672,7 +672,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
""" """
metadata = {} # type: Dict[unicode, Any] metadata = {} # type: Dict[unicode, Any]
metadata['uid'] = self.config.epub_uid metadata['uid'] = self.config.epub_uid
metadata['title'] = self.config.epub_title metadata['title'] = self.esc(self.config.epub_title)
metadata['level'] = level metadata['level'] = level
metadata['navpoints'] = navpoints metadata['navpoints'] = navpoints
return metadata return metadata

View File

@ -29,7 +29,7 @@ def runnable(command):
class EPUBElementTree(object): class EPUBElementTree(object):
"""Test helper for content.opf and tox.ncx""" """Test helper for content.opf and toc.ncx"""
namespaces = { namespaces = {
'idpf': 'http://www.idpf.org/2007/opf', 'idpf': 'http://www.idpf.org/2007/opf',
'dc': 'http://purl.org/dc/elements/1.1/', 'dc': 'http://purl.org/dc/elements/1.1/',
@ -226,6 +226,62 @@ def test_nested_toc(app):
assert navinfo(grandchild[0]) == ('foo.xhtml#foo-1-1', 'foo.1-1') assert navinfo(grandchild[0]) == ('foo.xhtml#foo-1-1', 'foo.1-1')
@pytest.mark.sphinx('epub', testroot='need-escaped')
def test_escaped_toc(app):
app.build()
# toc.ncx
toc = EPUBElementTree.fromstring((app.outdir / 'toc.ncx').bytes())
assert toc.find("./ncx:docTitle/ncx:text").text == ('need <b>"escaped"</b> '
'project documentation')
# toc.ncx / navPoint
def navinfo(elem):
label = elem.find("./ncx:navLabel/ncx:text")
content = elem.find("./ncx:content")
return (elem.get('id'), elem.get('playOrder'),
content.get('src'), label.text)
navpoints = toc.findall("./ncx:navMap/ncx:navPoint")
assert len(navpoints) == 4
assert navinfo(navpoints[0]) == ('navPoint1', '1', 'index.xhtml',
u"Welcome to Sphinx Tests's documentation!")
assert navpoints[0].findall("./ncx:navPoint") == []
# toc.ncx / nested navPoints
assert navinfo(navpoints[1]) == ('navPoint2', '2', 'foo.xhtml', '<foo>')
navchildren = navpoints[1].findall("./ncx:navPoint")
assert len(navchildren) == 4
assert navinfo(navchildren[0]) == ('navPoint3', '2', 'foo.xhtml', '<foo>')
assert navinfo(navchildren[1]) == ('navPoint4', '3', 'quux.xhtml', 'quux')
assert navinfo(navchildren[2]) == ('navPoint5', '4', 'foo.xhtml#foo-1', u'foo “1”')
assert navinfo(navchildren[3]) == ('navPoint8', '6', 'foo.xhtml#foo-2', 'foo.2')
# nav.xhtml / nav
def navinfo(elem):
anchor = elem.find("./xhtml:a")
return (anchor.get('href'), anchor.text)
nav = EPUBElementTree.fromstring((app.outdir / 'nav.xhtml').bytes())
toc = nav.findall("./xhtml:body/xhtml:nav/xhtml:ol/xhtml:li")
assert len(toc) == 4
assert navinfo(toc[0]) == ('index.xhtml',
"Welcome to Sphinx Tests's documentation!")
assert toc[0].findall("./xhtml:ol") == []
# nav.xhtml / nested toc
assert navinfo(toc[1]) == ('foo.xhtml', '<foo>')
tocchildren = toc[1].findall("./xhtml:ol/xhtml:li")
assert len(tocchildren) == 3
assert navinfo(tocchildren[0]) == ('quux.xhtml', 'quux')
assert navinfo(tocchildren[1]) == ('foo.xhtml#foo-1', u'foo “1”')
assert navinfo(tocchildren[2]) == ('foo.xhtml#foo-2', 'foo.2')
grandchild = tocchildren[1].findall("./xhtml:ol/xhtml:li")
assert len(grandchild) == 1
assert navinfo(grandchild[0]) == ('foo.xhtml#foo-1-1', 'foo.1-1')
@pytest.mark.sphinx('epub', testroot='basic') @pytest.mark.sphinx('epub', testroot='basic')
def test_epub_writing_mode(app): def test_epub_writing_mode(app):
# horizontal (default) # horizontal (default)