Skip to content

Commit

Permalink
#341 improve performance of style parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Feb 4, 2023
1 parent 47b0f46 commit 8124ce1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
13 changes: 7 additions & 6 deletions nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ def classes(self, add: Optional[str] = None, *, remove: Optional[str] = None, re

@staticmethod
def _parse_style(text: Optional[str]) -> Dict[str, str]:
return dict(_split(part, ':') for part in text.strip('; ').split(';')) if text else {}
result = {}
for word in (text or '').split(';'):
word = word.strip()
if word:
key, value = word.split(':', 1)
result[key.strip()] = value.strip()
return result

def style(self, add: Optional[str] = None, *, remove: Optional[str] = None, replace: Optional[str] = None):
'''CSS style sheet definitions to modify the look of the element.
Expand Down Expand Up @@ -211,8 +217,3 @@ def delete(self) -> None:
Can be overridden to perform cleanup.
"""


def _split(text: str, separator: str) -> Tuple[str, str]:
words = text.split(separator, 1)
return words[0].strip(), words[1].strip()
1 change: 1 addition & 0 deletions tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def assert_classes(classes: str) -> None:


def test_style_parsing():
assert Element._parse_style(None) == {}
assert Element._parse_style('color: red; background-color: green') == {'color': 'red', 'background-color': 'green'}
assert Element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
assert Element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
Expand Down

0 comments on commit 8124ce1

Please sign in to comment.