Skip to content

Commit

Permalink
Handle indirect sibling selector
Browse files Browse the repository at this point in the history
Adds parsing and handling of the indirect sibling selector, this should
mean we can at least parse all CSS3 selectors even if we do not yet
support all of them.

Also adds tests for previously added CSS3 selectors.

Change-Id: I1ce9afb9466044a38bdec167affc21a87837e4a4
Reviewed-by: Simon Hausmann <[email protected]>
  • Loading branch information
carewolf authored and Allan Sandfeld Jensen committed Jul 27, 2016
1 parent f2922c8 commit 997fa05
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 379 deletions.
21 changes: 14 additions & 7 deletions src/gui/text/qcssparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,12 +1867,15 @@ bool StyleSelector::selectorMatches(const Selector &selector, NodePtr node)
do {
match = basicSelectorMatches(sel, node);
if (!match) {
if (sel.relationToNext == BasicSelector::MatchNextSelectorIfParent
|| i == selector.basicSelectors.count() - 1) // first element must always match!
if (i == selector.basicSelectors.count() - 1) // first element must always match!
break;
if (sel.relationToNext != BasicSelector::MatchNextSelectorIfAncestor &&
sel.relationToNext != BasicSelector::MatchNextSelectorIfIndirectAdjecent)
break;
}

if (match || sel.relationToNext != BasicSelector::MatchNextSelectorIfAncestor)
if (match || (sel.relationToNext != BasicSelector::MatchNextSelectorIfAncestor &&
sel.relationToNext != BasicSelector::MatchNextSelectorIfIndirectAdjecent))
--i;

if (i < 0)
Expand All @@ -1885,16 +1888,18 @@ bool StyleSelector::selectorMatches(const Selector &selector, NodePtr node)
NodePtr nextParent = parentNode(node);
freeNode(node);
node = nextParent;
} else if (sel.relationToNext == BasicSelector::MatchNextSelectorIfPreceeds) {
} else if (sel.relationToNext == BasicSelector::MatchNextSelectorIfDirectAdjecent
|| sel.relationToNext == BasicSelector::MatchNextSelectorIfIndirectAdjecent) {
NodePtr previousSibling = previousSiblingNode(node);
freeNode(node);
node = previousSibling;
}
}
if (isNullNode(node)) {
match = false;
break;
}
} while (i >= 0 && (match || sel.relationToNext == BasicSelector::MatchNextSelectorIfAncestor));
} while (i >= 0 && (match || sel.relationToNext == BasicSelector::MatchNextSelectorIfAncestor
|| sel.relationToNext == BasicSelector::MatchNextSelectorIfIndirectAdjecent));

freeNode(node);

Expand Down Expand Up @@ -2356,9 +2361,11 @@ bool Parser::parseCombinator(BasicSelector::Relation *relation)
prev();
}
if (test(PLUS)) {
*relation = BasicSelector::MatchNextSelectorIfPreceeds;
*relation = BasicSelector::MatchNextSelectorIfDirectAdjecent;
} else if (test(GREATER)) {
*relation = BasicSelector::MatchNextSelectorIfParent;
} else if (test(TILDE)) {
*relation = BasicSelector::MatchNextSelectorIfIndirectAdjecent;
}
skipSpace();
return true;
Expand Down
6 changes: 4 additions & 2 deletions src/gui/text/qcssparser_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ struct BasicSelector
NoRelation,
MatchNextSelectorIfAncestor,
MatchNextSelectorIfParent,
MatchNextSelectorIfPreceeds
MatchNextSelectorIfDirectAdjecent,
MatchNextSelectorIfIndirectAdjecent,
};

QString elementName;
Expand Down Expand Up @@ -690,6 +691,7 @@ enum TokenType {
PLUS,
GREATER,
COMMA,
TILDE,

STRING,
INVALID,
Expand Down Expand Up @@ -789,7 +791,7 @@ class Q_GUI_EXPORT Parser
inline bool testImport() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("import")); }
inline bool testMedia() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("media")); }
inline bool testPage() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("page")); }
inline bool testCombinator() { return test(PLUS) || test(GREATER) || test(S); }
inline bool testCombinator() { return test(PLUS) || test(GREATER) || test(TILDE) || test(S); }
inline bool testProperty() { return test(IDENT); }
bool testTerm();
inline bool testExpr() { return testTerm(); }
Expand Down
Loading

0 comments on commit 997fa05

Please sign in to comment.