From e9391081ffba864b14eb3fb3eeef7a6cbac546a7 Mon Sep 17 00:00:00 2001 From: Kibzik Date: Mon, 29 May 2023 12:26:55 +0300 Subject: [PATCH] LAW-6736 | Change process of replacing text from string to nodes --- src/html_presets_processor.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/html_presets_processor.py b/src/html_presets_processor.py index 56318a2..7e1bec1 100644 --- a/src/html_presets_processor.py +++ b/src/html_presets_processor.py @@ -262,15 +262,10 @@ class HtmlPresetsProcessor: @staticmethod def _replace_text(**kwargs): - found_tag_text = kwargs["found_tag"].string if kwargs["found_tag"].string is not None\ - else kwargs["found_tag"].text - if found_tag_text is not None and re.search(re.compile(kwargs["rule"]["condition"]["text"]), found_tag_text): - new_text = re.sub(re.compile( - kwargs["rule"]["condition"]["text"]), kwargs["rule"]["text_to_replace"], found_tag_text) - if kwargs["found_tag"].string is not None: - kwargs["found_tag"].string.replace_with(new_text) - else: - kwargs["found_tag"].string = new_text + # Replace the regex pattern with "approaches" in the text nodes within the tag + for text_node in kwargs["found_tag"].find_all(text=True): + text_node.replace_with(re.sub(kwargs["rule"]["condition"]["text"], + kwargs["rule"]["text_to_replace"], str(text_node))) def process_tags(self, body_tag: BeautifulSoup,