Proper escaping for filename completion candidates

This commit is contained in:
Kovid Goyal 2014-12-24 10:38:33 +05:30
parent 20bc198cb7
commit ba6a257331
2 changed files with 5 additions and 3 deletions

View File

@ -78,11 +78,11 @@ def name_to_abspath(name, root):
def abspath_to_name(path, root):
return relpath(os.path.abspath(path), root).replace(os.sep, '/')
def name_to_href(name, root, base=None):
def name_to_href(name, root, base=None, quote=urlquote):
fullpath = name_to_abspath(name, root)
basepath = root if base is None else os.path.dirname(name_to_abspath(base, root))
path = relpath(fullpath, basepath).replace(os.sep, '/')
return urlquote(path)
return quote(path)
def href_to_name(href, root, base=None):
base = root if base is None else os.path.dirname(name_to_abspath(base, root))

View File

@ -11,6 +11,7 @@ from collections import namedtuple, OrderedDict
from PyQt5.Qt import QObject, pyqtSignal, Qt
from calibre import prepare_string_for_xml
from calibre.ebooks.oeb.polish.container import OEB_STYLES, OEB_FONTS, name_to_href
from calibre.gui2 import is_gui_thread
from calibre.gui2.tweak_book import current_container
@ -62,8 +63,9 @@ def complete_names(names_data, data_conn):
for n in names_cache[x]:
d[n] = desc
names_type, base, root = names_data
quote = (lambda x:x) if base.lower().endswith('.css') else prepare_string_for_xml
names = names_cache.get(names_type, names_cache[None])
nmap = {name:name_to_href(name, root, base) for name in names}
nmap = {name:name_to_href(name, root, base, quote) for name in names}
items = frozenset(nmap.itervalues())
descriptions = {href:names_cache.get(name) for name, href in nmap.iteritems()}
return items, descriptions, {}