From f1b23bf1e7b29e893c9a0e73e227fd170b075a1c Mon Sep 17 00:00:00 2001 From: Kibzik Date: Wed, 1 Mar 2023 13:09:24 +0300 Subject: [PATCH] Add support of list-style-type --- src/livecarta_config.py | 9 ++++++++- src/style_reader.py | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/livecarta_config.py b/src/livecarta_config.py index 51e8679..0d5f0b5 100644 --- a/src/livecarta_config.py +++ b/src/livecarta_config.py @@ -41,6 +41,13 @@ class LiveCartaConfig: r"(^h[1-9]$)": ["list-style-type"] } + """ + Dictionary LIVECARTA_STYLE_ATTRS_REPLACE = { css property: css property to replace with } + """ + LIVECARTA_STYLE_ATTRS_REPLACE = { + "list-style": "list-style-type", + } + """ Dictionary LIVECARTA_STYLE_ATTRS = { css property: value } Style properties that can be used to fit LiveCarta css style convention. @@ -84,6 +91,6 @@ class LiveCartaConfig: "border-right": [], "border-left": [], "border-bottom": [], - "list-style-type": [], + "list-style-type": ["circle", "disc", "square", "decimal"], "list-style-image": [], } diff --git a/src/style_reader.py b/src/style_reader.py index ffa1372..fbe723c 100644 --- a/src/style_reader.py +++ b/src/style_reader.py @@ -46,7 +46,7 @@ class StyleReader: "border-right": self.convert_tag_style_values, "border-left": self.convert_tag_style_values, "border-bottom": self.convert_tag_style_values, - "list-style-type": lambda x: x if x in LiveCartaConfig.list_types else "disc", + "list-style-type": lambda x: x, "list-style-image": lambda x: "disc" } @@ -104,11 +104,14 @@ class StyleReader: @staticmethod def clean_value(style_value: str, style_name: str): + def replace_str(str, lst): + return next((elem for elem in lst if elem in str), str) cleaned_value = style_value.replace("\"", "") if style_name == 'font-family': for symbol in ["+", "*", ".", "%", "?", "$", "^", "[", "]"]: cleaned_value = re.sub( re.escape(f"{symbol}"), rf"\\{symbol}", cleaned_value) + cleaned_value = replace_str(cleaned_value, LiveCartaConfig.LIVECARTA_STYLE_ATTRS[style_name]) return cleaned_value @staticmethod @@ -122,6 +125,8 @@ class StyleReader: def update_inline_styles_to_livecarta_convention(self, split_style: list) -> list: try: for i, style in reversed(list(enumerate(split_style))): + if style.split(":")[0] in LiveCartaConfig.LIVECARTA_STYLE_ATTRS_REPLACE: + style = LiveCartaConfig.LIVECARTA_STYLE_ATTRS_REPLACE[style.split(":")[0]] + ":" + style.split(":")[1] style_name, style_value = style.split(":") if style_name not in LiveCartaConfig.LIVECARTA_STYLE_ATTRS: # property not in LIVECARTA_STYLE_ATTRS, remove @@ -189,11 +194,13 @@ class StyleReader: def update_css_styles_to_livecarta_convention(self, css_rule: cssutils.css.CSSStyleRule, style_type: cssutils.css.property.Property): + if style_type.name in LiveCartaConfig.LIVECARTA_STYLE_ATTRS_REPLACE: + # attributes to replace + style_type.name = LiveCartaConfig.LIVECARTA_STYLE_ATTRS_REPLACE[style_type.name] if style_type.name not in LiveCartaConfig.LIVECARTA_STYLE_ATTRS: # property not in LIVECARTA_STYLE_ATTRS, remove from css file css_rule.style[style_type.name] = "" return - cleaned_value = self.clean_value(style_type.value, style_type.name) if all(self.style_conditions(cleaned_value, style_type.name)): # there are constraints + value not in LIVECARTA_STYLE_ATTRS, remove from css file