Conversion: Fix incorrect resolution of references to resources in HTML files that exist in a folder level above the OPF file. This could lead to styles being incorrectly processed in such HTML files.

This commit is contained in:
Kovid Goyal 2016-10-01 22:45:57 +05:30
parent 18c98916b4
commit 89b6941583

View file

@ -1062,24 +1062,7 @@ def relhref(self, href):
"""Convert the URL provided in :param:`href` from a book-absolute
reference to a reference relative to this manifest item.
"""
if urlparse(href).scheme:
return href
if '/' not in self.href:
return href
base = filter(None, os.path.dirname(os.path.normpath(self.href)).replace(os.sep, '/').split('/'))
target, frag = urldefrag(href)
target = target.split('/')
index = 0
for index in xrange(min(len(base), len(target))):
if base[index] != target[index]:
break
else:
index += 1
relhref = (['..'] * (len(base) - index)) + target[index:]
relhref = '/'.join(relhref)
if frag:
relhref = '#'.join((relhref, frag))
return relhref
return rel_href(self.href, href)
def abshref(self, href):
"""Convert the URL provided in :param:`href` from a reference
@ -1932,3 +1915,37 @@ def to_opf2(self, page_map=False):
if self.spine.page_progression_direction in {'ltr', 'rtl'}:
spine.attrib['page-progression-direction'] = self.spine.page_progression_direction
return results
def rel_href(base_href, href):
"""Convert the URL provided in :param:`href` to a URL relative to the URL
in :param:`base_href` """
if urlparse(href).scheme:
return href
if '/' not in base_href:
return href
base = filter(lambda x: x and x != '.', os.path.dirname(os.path.normpath(base_href)).replace(os.sep, '/').split('/'))
while True:
try:
idx = base.index('..')
except ValueError:
break
if idx > 0:
del base[idx-1:idx+1]
else:
break
if not base:
return href
target, frag = urldefrag(href)
target = target.split('/')
index = 0
for index in xrange(min(len(base), len(target))):
if base[index] != target[index]:
break
else:
index += 1
relhref = (['..'] * (len(base) - index)) + target[index:]
relhref = '/'.join(relhref)
if frag:
relhref = '#'.join((relhref, frag))
return relhref