* \brief Return whether selector matches at a given node in the document tree. */
| 125 | * \brief Return whether selector matches at a given node in the document tree. |
| 126 | */ |
| 127 | bool CssSelector::match (Doctree *docTree, const DoctreeNode *node, |
| 128 | int i, Combinator comb, MatchCache *matchCache) { |
| 129 | int *matchCacheEntry; |
| 130 | assert (node); |
| 131 | |
| 132 | if (i < 0) |
| 133 | return true; |
| 134 | |
| 135 | struct CombinatorAndSelector *cs = selectorList.getRef (i); |
| 136 | CssSimpleSelector *sel = cs->selector; |
| 137 | |
| 138 | switch (comb) { |
| 139 | case COMB_NONE: |
| 140 | break; |
| 141 | case COMB_CHILD: |
| 142 | node = docTree->parent (node); |
| 143 | break; |
| 144 | case COMB_ADJACENT_SIBLING: |
| 145 | node = docTree->sibling (node); |
| 146 | break; |
| 147 | case COMB_DESCENDANT: |
| 148 | node = docTree->parent (node); |
| 149 | matchCacheEntry = matchCache->getRef(matchCacheOffset + i); |
| 150 | |
| 151 | for (const DoctreeNode *n = node; |
| 152 | n && n->num > *matchCacheEntry; n = docTree->parent (n)) |
| 153 | if (sel->match (n) && |
| 154 | match (docTree, n, i - 1, cs->combinator, matchCache)) |
| 155 | return true; |
| 156 | |
| 157 | if (node) // remember that it didn't match to avoid future tests |
| 158 | *matchCacheEntry = node->num; |
| 159 | |
| 160 | return false; |
| 161 | break; |
| 162 | default: |
| 163 | return false; // \todo implement other combinators |
| 164 | } |
| 165 | |
| 166 | if (!node || !sel->match (node)) |
| 167 | return false; |
| 168 | |
| 169 | // tail recursion should be optimized by the compiler |
| 170 | return match (docTree, node, i - 1, cs->combinator, matchCache); |
| 171 | } |
| 172 | |
| 173 | void CssSelector::addSimpleSelector (Combinator c) { |
| 174 | struct CombinatorAndSelector *cs; |