Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed May 7, 2024
1 parent 99082a7 commit cbdd458
Show file tree
Hide file tree
Showing 6 changed files with 639 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/web/web-dom/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ struct Node :
return *_parent;
}

Node const &parentNode() const {
if (not _parent)
panic("node has no parent");
return *_parent;
}

usize _parentIndex() {
return indexOf(parentNode()._children, *this).unwrap();
}
Expand Down
59 changes: 59 additions & 0 deletions src/web/web-select/match.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <web-dom/element.h>

#include "select2.h"

namespace Web::Select {

// 17. Calculating a selector’s specificity
// https://www.w3.org/TR/selectors-4/#specificity-rules
struct Spec {
bool match;
isize a, b, c;

static Spec NOMATCH;
static Spec MATCH;

Spec operator and(Spec const &other) const {
return Spec{
match && other.match,
a + other.a,
b + other.b,
c + other.c
};
}

Spec operator or(Spec const &other) const {
return Spec{
match || other.match,
a + other.a,
b + other.b,
c + other.c
};
}
};

Spec match(Selector const &sel, Dom::Element &el) {
return sel.visit(Visitor{
[&](Combinator const &s) -> Spec {
switch (s.type) {
case Combinator::LIST:
return match(*s.lhs, el) or match(*s.rhs, el);

case Combinator::DESCENDANT:
return match(*s.lhs, el) or el.parentNode().map([&](auto &parent) {
return match(*s.rhs, parent);
}).value_or(Spec::NOMATCH);

default:
return Spec::NOMATCH;
}
},
[&](auto &) -> Spec {
return Spec::NOMATCH;
}
});
}

} // namespace Web::Select
Loading

0 comments on commit cbdd458

Please sign in to comment.