From 1aa2dc2a3cca578b4606331081dcde61be0eedfa Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 6 Dec 2011 10:09:52 +0530 Subject: [PATCH] MOBI Output: Handle links to inline anchors placed inside large blocks of text correctly, i.e. the link should not point to the start of the block. Fixes #899831 (hyperlinks sometimes broken mobi output) --- src/calibre/ebooks/mobi/mobiml.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py index ca48fb0a7f..1a822ce1bd 100644 --- a/src/calibre/ebooks/mobi/mobiml.py +++ b/src/calibre/ebooks/mobi/mobiml.py @@ -30,6 +30,8 @@ CONTENT_TAGS = set(['img', 'hr', 'br']) NOT_VTAGS = HEADER_TAGS | NESTABLE_TAGS | TABLE_TAGS | SPECIAL_TAGS | \ CONTENT_TAGS +LEAF_TAGS = set(['base', 'basefont', 'frame', 'link', 'meta', 'area', 'br', +'col', 'hr', 'img', 'input', 'param']) PAGE_BREAKS = set(['always', 'left', 'right']) COLLAPSE = re.compile(r'[ \t\r\n\v]+') @@ -246,7 +248,17 @@ class MobiMLizer(object): last.text = None else: last = bstate.body[-1] - last.addprevious(anchor) + # We use append instead of addprevious so that inline + # anchors in large blocks point to the correct place. See + # https://bugs.launchpad.net/calibre/+bug/899831 + # This could potentially break if inserting an anchor at + # this point in the markup is illegal, but I cannot think + # of such a case offhand. + if barename(last.tag) in LEAF_TAGS: + last.addprevious(anchor) + else: + last.append(anchor) + istate.ids.clear() if not text: return