Add support of list-style-type

This commit is contained in:
Kibzik
2023-03-01 13:09:24 +03:00
parent 91b8a25e5a
commit f1b23bf1e7
2 changed files with 17 additions and 3 deletions

View File

@@ -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": [],
}

View File

@@ -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