From e073ff0623a4f4a448cbdb107d2fe6baec7037b3 Mon Sep 17 00:00:00 2001 From: Kibzik Date: Fri, 17 Mar 2023 12:59:17 +0300 Subject: [PATCH] Fix double tags --- src/inline_style_processor.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/inline_style_processor.py b/src/inline_style_processor.py index 6fc4aff..395a7d7 100644 --- a/src/inline_style_processor.py +++ b/src/inline_style_processor.py @@ -179,20 +179,28 @@ class InlineStyleProcessor: def change_attrs_with_corresponding_tags(self): # adds , , instead of styles styles_to_remove = self.check_style_to_be_tag(self.tag_inline_style.attrs['style']) - for i, (attr, value) in enumerate(styles_to_remove): - self.tag_inline_style.attrs["style"] = self.tag_inline_style.attrs["style"]\ - .replace(f"{attr}:{value};", "").strip() - corr_tag_name = LiveCartaConfig.STYLE_ATTRS_TO_TAGS[( - attr, value)] + style_attr = self.tag_inline_style.attrs.get('style', '') + # Replace each style with its corresponding tag + for attr, value in styles_to_remove: + # Remove the attribute and value from the style attribute + self.tag_inline_style.attrs["style"] = '; '.join( + [s for s in self.tag_inline_style.attrs.get('style', '').split(';') + if f'{attr}:{value}' not in s]).strip() + + # Create a new tag for the corresponding style + corr_tag_name = LiveCartaConfig.STYLE_ATTRS_TO_TAGS[(attr, value)] correspond_tag = BeautifulSoup(features="lxml").new_tag(corr_tag_name) + + # Move the contents of the original tag into the new tag for content in reversed(self.tag_inline_style.contents): correspond_tag.insert(0, content.extract()) + + # Add the new tag to the original tag self.tag_inline_style.append(correspond_tag) @staticmethod def wrap_span_in_tag_to_save_style_attrs(initial_tag: Tag) -> Tag: """Function designed to save style attrs that cannot be in tag.name -> span""" - # Compile a regex pattern to match tag names that can have certain style attributes dictkeys_pattern = re.compile("|".join(LiveCartaConfig.TAGS_TO_STYLE_ATTRS_CAN_BE_IN_TAG))