more selectors

This commit is contained in:
Will McGugan
2021-11-02 16:05:10 +00:00
parent 945d75d91f
commit 9f26936bdc
3 changed files with 17 additions and 26 deletions

View File

@@ -17,9 +17,7 @@ def match(selector_sets: Iterable[SelectorSet], node: DOMNode):
def _check_selectors(selectors: list[Selector], node: DOMNode) -> bool:
SAME = CombinatorType.SAME
DESCENDENT = CombinatorType.DESCENDENT
CHILD = CombinatorType.CHILD
css_path = node.css_path
path_count = len(css_path)
@@ -33,34 +31,30 @@ def _check_selectors(selectors: list[Selector], node: DOMNode) -> bool:
while stack:
selector_index, node_index = stack[-1]
if selector_index == selector_count:
return node_index + 1 == path_count
if node_index >= path_count:
if selector_index == selector_count or node_index == path_count:
pop()
continue
selector = selectors[selector_index]
last_selector = selector_index == selector_count - 1
path_node = css_path[node_index]
selector = selectors[selector_index]
advance = selector.advance
combinator = selector.combinator
stack[-1] = (selector_index, node_index)
if combinator == SAME:
# Check current node again
if selector.check(path_node):
stack[-1] = (selector_index + 1, node_index + selector.advance)
else:
pop()
elif combinator == DESCENDENT:
if combinator == DESCENDENT:
# Find a matching descendent
if selector.check(path_node):
pop()
push((selector_index + 1, node_index + selector.advance + 1))
push((selector_index + 1, node_index + selector.advance))
if path_node is node and last_selector:
return True
stack[-1] = (selector_index + 1, node_index + advance)
push((selector_index, node_index + 1))
else:
stack[-1] = (selector_index, node_index + selector.advance + 1)
elif combinator == CHILD:
stack[-1] = (selector_index, node_index + 1)
else:
# Match the next node
if selector.check(path_node):
stack[-1] = (selector_index + 1, node_index + selector.advance)
if path_node is node and last_selector:
return True
stack[-1] = (selector_index + 1, node_index + advance)
else:
pop()
return False

View File

@@ -44,7 +44,7 @@ class Selector:
pseudo_classes: list[str] = field(default_factory=list)
specificity: Specificity3 = field(default_factory=lambda: (0, 0, 0))
_name_lower: str = field(default="", repr=False)
advance: int = 0
advance: int = 1
@property
def css(self) -> str:
@@ -109,6 +109,7 @@ class SelectorSet:
def __post_init__(self) -> None:
SAME = CombinatorType.SAME
# self.selectors[-1].advance = 1
for selector, next_selector in zip(self.selectors, self.selectors[1:]):
selector.advance = 0 if next_selector.combinator == SAME else 1

View File

@@ -124,11 +124,7 @@ if __name__ == "__main__":
# print(DOMQuery(selector="App", nodes=[sub_view]))
tests = [
"App > View",
"Widget.float",
".float.transient",
]
tests = ["View", "App > View", "Widget.float", ".float.transient", "*"]
for test in tests:
print("")