diff options
author | bptato <nincsnevem662@gmail.com> | 2022-07-21 01:06:35 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2022-07-21 01:06:35 +0200 |
commit | c69a8ab7576e2053afc5dfcee5c7152a07c31230 (patch) | |
tree | 750156d832cb31a3dc08d2c33f27752403ab0728 /src/css/select.nim | |
parent | 062027c84176f8a6fa8870d7c527ece3aee7b519 (diff) | |
download | chawan-c69a8ab7576e2053afc5dfcee5c7152a07c31230.tar.gz |
Optimize dom
Diffstat (limited to 'src/css/select.nim')
-rw-r--r-- | src/css/select.nim | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/css/select.nim b/src/css/select.nim index f8769418..b43604c4 100644 --- a/src/css/select.nim +++ b/src/css/select.nim @@ -30,7 +30,14 @@ func pseudoSelectorMatches(elem: Element, sel: Selector): bool = of PSEUDO_ONLY_CHILD: return elem.parentNode.firstElementChild == elem and elem.parentNode.lastElementChild == elem of PSEUDO_HOVER: return elem.hover of PSEUDO_ROOT: return elem == elem.document.html - of PSEUDO_NTH_CHILD: return int64(sel.pseudonum - 1) in elem.parentNode.children.low..elem.parentNode.children.high and elem.parentNode.children[int64(sel.pseudonum - 1)] == elem + of PSEUDO_NTH_CHILD: + let n = int64(sel.pseudonum - 1) + var i = 0 + for child in elem.parentNode.children: + if i == n: + return child == elem + inc i + return false of PSEUDO_CHECKED: if elem.tagType == TAG_INPUT: return HTMLInputElement(elem).checked @@ -74,18 +81,22 @@ func combinatorSelectorMatches(elem: Element, sel: Selector): bool = dec i e = e.parentElement of NEXT_SIBLING_COMBINATOR: - var e = elem.previousElementSibling - while e != nil and i >= 0: - if not e.selectorsMatch(sel.csels[i]): - return false - dec i - e = e.previousElementSibling - of SUBSEQ_SIBLING_COMBINATOR: - var e = elem.previousElementSibling - while e != nil and i >= 0: - if e.selectorsMatch(sel.csels[i]): + var found = false + for child in elem.parentElement.children_rev: + if found: + if not child.selectorsMatch(sel.csels[i]): + return false dec i - e = e.previousElementSibling + if child == elem: + found = true + of SUBSEQ_SIBLING_COMBINATOR: + var found = false + for child in elem.parentElement.children_rev: + if found: + if child.selectorsMatch(sel.csels[i]): + dec i + if child == elem: + found = true return i == -1 return false |