From d53346d783906d827707fe9cc86fb073983dffa6 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 1 Aug 2014 09:42:28 +0900 Subject: [PATCH] limit url unquoting to image tags this prevents random text like %20 in a field from being converted when note is saved --- anki/media.py | 8 ++++++-- aqt/editor.py | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/anki/media.py b/anki/media.py index 0c39687ff..d19295e99 100644 --- a/anki/media.py +++ b/anki/media.py @@ -221,14 +221,18 @@ create table meta (dirMod int, lastUsn int); insert into meta values (0, 0); txt = re.sub(reg, "", txt) return txt - def escapeImages(self, string): + def escapeImages(self, string, unescape=False): + if unescape: + fn = urllib.unquote + else: + fn = urllib.quote def repl(match): tag = match.group(0) fname = match.group("fname") if re.match("(https?|ftp)://", fname): return tag return tag.replace( - fname, urllib.quote(fname.encode("utf-8"))) + fname, fn(fname.encode("utf-8"))) for reg in self.imgRegexps: string = re.sub(reg, repl, string) return string diff --git a/aqt/editor.py b/aqt/editor.py index bd509338c..b83c74678 100644 --- a/aqt/editor.py +++ b/aqt/editor.py @@ -450,8 +450,7 @@ class Editor(object): # misbehaving apps may include a null byte in the text txt = txt.replace("\x00", "") # reverse the url quoting we added to get images to display - txt = unicode(urllib2.unquote( - txt.encode("utf8")), "utf8", "replace") + txt = self.mw.col.media.escapeImages(txt, unescape=True) self.note.fields[self.currentField] = txt if not self.addMode: self.note.flush()