From 11885d653e135cbab557a4ec03d4e60aa28a6307 Mon Sep 17 00:00:00 2001 From: Kibzik Date: Wed, 12 Apr 2023 14:29:10 +0300 Subject: [PATCH] LAW-6611 | Add ability to remove attr value --- src/html_presets_processor.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/html_presets_processor.py b/src/html_presets_processor.py index 5afe214..2a8e426 100644 --- a/src/html_presets_processor.py +++ b/src/html_presets_processor.py @@ -16,7 +16,7 @@ class HtmlPresetsProcessor: "table_wrapper": self._process_tag_using_table, "decomposer": self._decompose_tag, "replacer": self._replace_tag, - "attr_remover": self._remove_attrs, + "attr_remover": self._remove_attr, "attr_replacer": self._replace_attr, "unwrapper": self._unwrap_tag, "inserter": self._insert_tag, @@ -155,8 +155,20 @@ class HtmlPresetsProcessor: kwargs["found_tag"].attrs = dict_attributes @staticmethod - def _remove_attrs(**kwargs): - kwargs["found_tag"].attrs = {} + def _remove_attr(**kwargs): + """Deletes an attribute or part of an attribute value from an HTML tag""" + for attr in kwargs["rule"]["condition"]["attrs"]: + attr_name, attr_value = \ + attr["name"], attr["value"] + if attr["value"] is None: + # delete entire attribute + del kwargs["found_tag"].attrs[attr_name] + else: + # delete part of attribute value + pattern = rf"{attr_value}:\s*[0-9.-]+[a-z%]*;*" + attr_string = str(kwargs["found_tag"][attr_name]) + new_value = re.sub(pattern, "", attr_string) + kwargs["found_tag"][attr_name] = new_value @staticmethod def _replace_attr(**kwargs):