Skip to content

Commit

Permalink
style: Fix NFIX parser when parsing OR and add relative tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Louciole committed Dec 12, 2024
1 parent 5289455 commit 48730d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
19 changes: 13 additions & 6 deletions src/web/vaev-style/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "values.h"

namespace Vaev::Style {

static constexpr bool DEBUG_SELECTORS = false;

// MARK: Selector Specificity ---------------------------------------------------
Expand Down Expand Up @@ -240,7 +239,6 @@ static bool _matchLastOfType(Markup::Element const &e) {

static bool _match(Pseudo const &s, Markup::Element const &el) {
switch (s.type) {

case Pseudo::LINK:
return _matchLink(el);

Expand Down Expand Up @@ -367,6 +365,7 @@ static Selector _parseAttributeSelector(Slice<Css::Sst> content) {
};
}

// consume an Op Code
static OpCode _peekOpCode(Cursor<Css::Sst> &cur) {
if (cur.ended()) {
return OpCode::NOP;
Expand Down Expand Up @@ -442,6 +441,7 @@ static OpCode _peekOpCode(Cursor<Css::Sst> &cur) {

static Selector _parseInfixExpr(Selector lhs, Cursor<Css::Sst> &cur, OpCode opCode = OpCode::NOP);

// consume a selector element (everything that has a lesser priority than the current OP)
static Selector _parseSelectorElement(Cursor<Css::Sst> &cur, OpCode currentOp) {
if (cur.ended()) {
logErrorIf(DEBUG_SELECTORS, "ERROR : unterminated selector");
Expand Down Expand Up @@ -510,6 +510,7 @@ static Selector _parseSelectorElement(Cursor<Css::Sst> &cur, OpCode currentOp) {

static Selector _parseNfixExpr(Selector lhs, OpCode op, Cursor<Css::Sst> &cur) {
Vec<Selector> selectors = {lhs, _parseSelectorElement(cur, op)};
// all the selectors between the op eg : a,b.B,c -> [a,b.B,c]

while (not cur.ended()) {
Cursor<Css::Sst> rollBack = cur;
Expand All @@ -527,17 +528,24 @@ static Selector _parseNfixExpr(Selector lhs, OpCode op, Cursor<Css::Sst> &cur) {
cur = rollBack;
break;
}

last(selectors) = _parseNfixExpr(last(selectors), nextOpCode, cur);
} else {
// parse new infix
// parse new infix if the next op is more important

if (nextOpCode < op) {
cur = rollBack;
break;
}

selectors.pushBack(_parseInfixExpr(_parseSelectorElement(cur, op), cur, nextOpCode));
if (not(cur.rem() == 2 and cur.peek(1) == Css::Token::WHITESPACE)) [[likely]] {
last(selectors) = _parseInfixExpr(last(selectors), cur, nextOpCode);

// auto const lhs = _parseSelectorElement(cur, op);
// selectors.pushBack(_parseInfixExpr(lhs, cur, nextOpCode));
} else {
last(selectors) = _parseInfixExpr(last(selectors), cur, nextOpCode);
cur.next();
}
}
}

Expand Down Expand Up @@ -612,5 +620,4 @@ Selector Selector::parse(Str input) {
Io::SScan s{input};
return parse(s);
};

} // namespace Vaev::Style
25 changes: 16 additions & 9 deletions src/web/vaev-style/tests/test-parse-selectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <vaev-style/select.h>

namespace Vaev::Style::Tests {

test$("vaev-style-parse-simple-selectors") {
expectEq$(
Selector::parse(""),
Expand Down Expand Up @@ -187,6 +186,22 @@ test$("vaev-style-parse-mixed-selectors") {
)
);

expectEq$(
Selector::parse("td, .o_content .o .o_table th "),
Selector::or_(
{TypeSelector{Html::TD},
Selector::descendant(Selector::descendant(Selector::descendant(ClassSelector{"o_content"s}, ClassSelector{"o"s}), ClassSelector{"o_table"s}), TypeSelector{Html::TH})}
)
);

expectEq$(
Selector::parse("td, .o_content .o_table th "),
Selector::or_(
{TypeSelector{Html::TD},
Selector::descendant(Selector::descendant(ClassSelector{"o_content"s}, ClassSelector{"o_table"s}), TypeSelector{Html::TH})}
)
);

expectEq$(
Selector::parse(".o_content .o_table > thead > tr:not(:last-child) th:not(:first-child)"),
Selector::descendant(
Expand All @@ -201,13 +216,6 @@ test$("vaev-style-parse-mixed-selectors") {
)
);

expectEq$(
Selector::parse(".o_content .o_table thead, .o_content .o_table tbody, .o_content .o_table tfoot, .o_content .o_table tr, .o_content .o_table td, .o_content .o_table th "),
Selector::descendant(
Selector::and_({TypeSelector{Html::TR}, Selector::not_(Pseudo{Pseudo::LAST_CHILD})}),
Selector::and_({TypeSelector{Html::TH}, {Selector::not_(Pseudo{Pseudo::FIRST_CHILD})}})
)
);
return Ok();
}

Expand Down Expand Up @@ -342,5 +350,4 @@ test$("vaev-style-parse-attribute-selectors") {

return Ok();
}

} // namespace Vaev::Style::Tests

0 comments on commit 48730d5

Please sign in to comment.