Edit book: Fix renaming of classes that start/end with non word characters not working

This commit is contained in:
Kovid Goyal 2022-01-23 12:20:50 +05:30
parent 0172b61217
commit 78ad0ae6f1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -502,12 +502,16 @@ def rename_class_in_rule_list(css_rules, old_name, new_name):
# this regex will not match class names inside attribute value selectors # 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 # 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 # that can be done without implementing a full parser for CSS selectors
pat = re.compile(rf'(?<=\.){re.escape(old_name)}\b') pat = re.compile(rf'(?<=\.){re.escape(old_name)}(?:\W|$)')
def repl(m):
return m.group().replace(old_name, new_name)
changed = False changed = False
for rule in css_rules: for rule in css_rules:
if rule.type == rule.STYLE_RULE: if rule.type == rule.STYLE_RULE:
old = rule.selectorText old = rule.selectorText
q = pat.sub(new_name, old) q = pat.sub(repl, old)
if q != old: if q != old:
changed = True changed = True
rule.selectorText = q rule.selectorText = q
@ -519,11 +523,15 @@ def rename_class_in_rule_list(css_rules, old_name, new_name):
def rename_class_in_doc(container, root, old_name, new_name): def rename_class_in_doc(container, root, old_name, new_name):
changed = False changed = False
pat = re.compile(rf'\b{re.escape(old_name)}\b') pat = re.compile(rf'(?:^|\W){re.escape(old_name)}(?:\W|$)')
def repl(m):
return m.group().replace(old_name, new_name)
for elem in root.xpath('//*[@class]'): for elem in root.xpath('//*[@class]'):
old = elem.get('class') old = elem.get('class')
if old: if old:
new = pat.sub(new_name, old) new = pat.sub(repl, old)
if new != old: if new != old:
changed = True changed = True
elem.set('class', new) elem.set('class', new)