From d12e14b2362ca5d18f16460d08e7ddf5b4eb5a95 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Sep 2021 20:34:37 +0530 Subject: [PATCH] Edit book: When renaming classes in style sheets only recognize class names preceded by a period. Fixes #1944562 [Renaming class called "body" renames rules for body element](https://bugs.launchpad.net/calibre/+bug/1944562) --- src/calibre/ebooks/oeb/polish/css.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/oeb/polish/css.py b/src/calibre/ebooks/oeb/polish/css.py index 5c8a17cd5e..be78c33f9c 100644 --- a/src/calibre/ebooks/oeb/polish/css.py +++ b/src/calibre/ebooks/oeb/polish/css.py @@ -501,7 +501,10 @@ def add_stylesheet_links(container, name, text): def rename_class_in_rule_list(css_rules, old_name, new_name): - pat = re.compile(rf'\b{re.escape(old_name)}\b') + # this regex will not match class names inside attribute value selectors + # and it will match id selectors that contain .old_name but its the best + # that can be done without implementing a full parser for CSS selectors + pat = re.compile(rf'(?<=\.){re.escape(old_name)}\b') changed = False for rule in css_rules: if rule.type == rule.STYLE_RULE: