From b9d90fe9b37567a6aeeab9b5f1fedb8a85f1c179 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 Aug 2014 14:20:08 +0530 Subject: [PATCH] Edit Book: When highlighting the tag the cursor is currently inside, if the cursor is inside the definition of an opening tag, highlight that tag rather than its parent. --- src/calibre/gui2/tweak_book/editor/smart/html.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/tweak_book/editor/smart/html.py b/src/calibre/gui2/tweak_book/editor/smart/html.py index 1a5ce996c5..1892cda4f9 100644 --- a/src/calibre/gui2/tweak_book/editor/smart/html.py +++ b/src/calibre/gui2/tweak_book/editor/smart/html.py @@ -70,9 +70,15 @@ def find_closest_containing_tag(block, offset, max_tags=sys.maxint): if block is None: return None if boundary.is_start: - # We are inside a tag, therefore the containing tag is the parent tag of - # this tag - return find_closest_containing_tag(block, boundary.offset) + # We are inside a tag already + if boundary.closing: + return find_closest_containing_tag(block, boundary.offset) + eblock, eboundary = next_tag_boundary(block, boundary.offset) + if eblock is None or eboundary is None or eboundary.is_start: + return None + if eboundary.self_closing: + return Tag(block, boundary, eblock, eboundary, self_closing=True) + return find_closest_containing_tag(eblock, eboundary.offset + 1) stack = [] block, tag_end = block, boundary while block is not None and max_tags > 0: @@ -155,7 +161,8 @@ def find_closing_tag(tag, max_tags=sys.maxint): ''' Find the closing tag corresponding to the specified tag. To find it we search for the first closing tag after the specified tag that does not match a previous opening tag. Search through at most max_tags. ''' - + if tag.self_closing: + return None stack = [] block, offset = tag.end_block, tag.end_offset while block.isValid() and max_tags > 0: