-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99082a7
commit cbdd458
Showing
6 changed files
with
639 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.