Skip to content

Commit

Permalink
vaev-style: Fixed infix selectors matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Oct 4, 2024
1 parent 7168dd9 commit 9b320f0
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/vaev-style/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ Spec spec(Selector const &s) {

// https://www.w3.org/TR/selectors-4/#descendant-combinators
static bool _matchDescendant(Selector const &s, Markup::Element const &e) {
Markup::Node const *curr = &e;
Cursor<Markup::Node> curr = e;
while (curr->hasParent()) {
auto &parent = curr->parentNode();
if (auto el = parent.is<Markup::Element>())
if (s.match(*el))
return true;
curr = &parent;
curr = parent;
}
return false;
}
Expand Down Expand Up @@ -100,21 +100,21 @@ static bool _matchSubsequent(Selector const &s, Markup::Element const &e) {
}

static bool _match(Infix const &s, Markup::Element const &e) {
if (not s.lhs->match(e))
if (not s.rhs->match(e))
return false;

switch (s.type) {
case Infix::Type::DESCENDANT:
return _matchDescendant(*s.rhs, e);
case Infix::Type::DESCENDANT: // ' '
return _matchDescendant(*s.lhs, e);

case Infix::Type::CHILD:
return _matchChild(*s.rhs, e);
case Infix::Type::CHILD: // >
return _matchChild(*s.lhs, e);

case Infix::Type::ADJACENT:
return _matchAdjacent(*s.rhs, e);
case Infix::Type::ADJACENT: // +
return _matchAdjacent(*s.lhs, e);

case Infix::Type::SUBSEQUENT:
return _matchSubsequent(*s.rhs, e);
case Infix::Type::SUBSEQUENT: // ~
return _matchSubsequent(*s.lhs, e);

default:
logWarn("unimplemented selector: {}", s);
Expand Down
59 changes: 59 additions & 0 deletions tests/css/selectors/class.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<test id="slect-class">
<container>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<style>
body {
margin: 0px;
}

section {
width: 200px;
height: 200px;
background: #ddd;
position: relative;
}

div {
width: 100px;
height: 100px;
}
</style>
</head>

<body>
<section>
<slot />
</section>
</body>

</html>
</container>

<rendering>
<div class="klass" style="background-color: green;" />
</rendering>

<rendering>
<div class="klass" />
<style>
.klass {
background-color: green;
}
</style>
</rendering>


<error>
<div id="klass" />
<style>
.klass {
background-color: green;
}
</style>
</error>

<error>
</error>
</test>
84 changes: 84 additions & 0 deletions tests/css/selectors/descendant.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<test id="select-descendant">
<container>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<style>
body {
margin: 0px;
}

section {
width: 200px;
height: 200px;
background: #ddd;
position: relative;
}

div {
width: 100px;
height: 100px;
}
</style>
</head>

<body>
<section>
<slot />
</section>
</body>

</html>
</container>

<rendering>
<div class="klass" style="background-color: green;" />
</rendering>

<error>
</error>

<rendering>
<div class="cool">
<div class="klass" />
</div>

<style>
.cool .klass {
background-color: green;
}
</style>
</rendering>

<error>
<div>
<div class="klass" />
</div>

<style>
.cool .klass {
background-color: green;
}
</style>
</error>

<rendering>
<div class="cool">
<div>
<div>
<div>
<div>
<div class="klass" /> <!-- This is peek react dev right here :^) -->
</div>
</div>
</div>
</div>
</div>

<style>
.cool .klass {
background-color: green;
}
</style>
</rendering>
</test>

0 comments on commit 9b320f0

Please sign in to comment.