From 306bb0ff2f021f0b00914942bdc9b15a00aad64d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Dec 2016 11:23:20 +0530 Subject: [PATCH] Edit Book: Fix the "Search ignoring markup tool" not ignoring comments/processing instructions, etc. Fixes #1651160 [Search ignoring HTML markup finds within ' import sys, re from operator import itemgetter +from itertools import chain from cssutils import parseStyle from PyQt5.Qt import QTextEdit, Qt, QTextCursor @@ -700,6 +701,16 @@ class Smarts(NullSmarts): c.setPosition(cstart) block = c.block() in_text = find_tag_definition(block, 0)[0] is None + if in_text: + # Check if we are in comment/PI/etc. + pb = block.previous() + while pb.isValid(): + boundaries = pb.userData().non_tag_structures + if boundaries: + if boundaries[-1].is_start: + in_text = False + break + pb = pb.previous() def append(text, start): text = text.replace(PARAGRAPH_SEPARATOR, '\n') @@ -714,7 +725,8 @@ class Smarts(NullSmarts): chunks.append((text, start + max(extra, 0))) while block.isValid() and block.position() <= cend: - boundaries = sorted(block.userData().tags, key=get_offset) + ud = block.userData() + boundaries = sorted(chain(ud.tags, ud.non_tag_structures), key=get_offset) if not boundaries: # Add the whole line if in_text: diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.py b/src/calibre/gui2/tweak_book/editor/syntax/html.py index 46a00943d4..800af7f689 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.py +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.py @@ -51,6 +51,7 @@ CSS = 11 TagStart = namedtuple('TagStart', 'offset prefix name closing is_start') TagEnd = namedtuple('TagEnd', 'offset self_closing is_start') +NonTagBoundary = namedtuple('NonTagBoundary', 'offset is_start type') Attr = namedtuple('Attr', 'offset type data') LINK_ATTRS = frozenset(('href', 'src', 'poster', 'xlink:href')) @@ -62,6 +63,7 @@ def refresh_spell_check_status(): global do_spell_check do_spell_check = tprefs['inline_spell_check'] and hasattr(dictionaries, 'active_user_dictionaries') + from calibre.constants import plugins _speedup = plugins['html'][0] @@ -223,12 +225,13 @@ class HTMLUserData(QTextBlockUserData): QTextBlockUserData.__init__(self) self.tags = [] self.attributes = [] + self.non_tag_structures = [] self.state = State() self.css_user_data = None self.doc_name = None def clear(self, state=None, doc_name=None): - self.tags, self.attributes = [], [] + self.tags, self.attributes, self.non_tag_structures = [], [], [] self.state = State() if state is None else state self.doc_name = doc_name @@ -247,6 +250,7 @@ class XMLUserData(HTMLUserData): def add_tag_data(user_data, tag): user_data.tags.append(tag) + ATTR_NAME, ATTR_VALUE, ATTR_START, ATTR_END = object(), object(), object(), object() @@ -333,14 +337,17 @@ def normal(state, text, i, formats, user_data): if ch == '<': if text[i:i+4] == '