1
- from typing import List , Optional , Tuple , Union
1
+ from typing import Generator , List , Optional , Tuple , Union
2
2
from warnings import warn
3
3
4
4
import napari .qt
@@ -47,6 +47,21 @@ def __contains__(self, val: int) -> bool:
47
47
return True
48
48
49
49
50
+ def _logical_lines (
51
+ nodes : List [tinycss2 .ast .Node ],
52
+ ) -> Generator [Tuple [tinycss2 .ast .Node , tinycss2 .ast .Node ], None , None ]:
53
+ """Generator to provide logical lines (thing: value) of css (terminated by ';')"""
54
+ ident , dimension = None , None
55
+ for node in nodes :
56
+ if node == ";" :
57
+ yield (ident , dimension )
58
+ ident , dimension = None , None
59
+ elif node .type == "ident" :
60
+ ident = node
61
+ elif node .type == "dimension" :
62
+ dimension = node
63
+
64
+
50
65
def _has_id (nodes : List [tinycss2 .ast .Node ], id_name : str ) -> bool :
51
66
"""
52
67
Is `id_name` in IdentTokens in the list of CSS `nodes`?
@@ -66,13 +81,8 @@ def _get_dimension(
66
81
-------
67
82
None if no IdentToken is found.
68
83
"""
69
- cleaned_nodes = [node for node in nodes if node .type != "whitespace" ]
70
- for name , _ , value , _ in zip (* (iter (cleaned_nodes ),) * 4 ):
71
- if (
72
- name .type == "ident"
73
- and value .type == "dimension"
74
- and name .value == id_name
75
- ):
84
+ for name , value in _logical_lines (nodes ):
85
+ if name .value == id_name :
76
86
return value .int_value
77
87
warn (f"Unable to find DimensionToken for { id_name } " , RuntimeWarning )
78
88
return None
0 commit comments