Sfoglia il codice sorgente

#341 improve performance of style parsing

Falko Schindler 2 anni fa
parent
commit
8124ce1f40
2 ha cambiato i file con 8 aggiunte e 6 eliminazioni
  1. 7 6
      nicegui/element.py
  2. 1 0
      tests/test_element.py

+ 7 - 6
nicegui/element.py

@@ -98,7 +98,13 @@ class Element(ABC, Visibility):
 
     @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.
@@ -211,8 +217,3 @@ class Element(ABC, Visibility):
 
         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 - 0
tests/test_element.py

@@ -34,6 +34,7 @@ def test_classes(screen: Screen):
 
 
 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%)'}