2005-12-18 21:44:18 -05:00
|
|
|
/* DOM node selection */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
#include "dom/css/scanner.h"
|
2006-01-31 16:38:06 -05:00
|
|
|
#include "dom/code.h"
|
2005-12-28 08:05:14 -05:00
|
|
|
#include "dom/node.h"
|
|
|
|
#include "dom/scanner.h"
|
|
|
|
#include "dom/select.h"
|
|
|
|
#include "dom/stack.h"
|
2005-12-28 14:45:55 -05:00
|
|
|
#include "dom/string.h"
|
2005-12-18 21:44:18 -05:00
|
|
|
#include "util/memory.h"
|
|
|
|
|
|
|
|
|
2005-12-22 18:11:25 -05:00
|
|
|
/* Selector parsing: */
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Maps the content of a scanner token to a pseudo-class or -element ID. */
|
2022-01-27 12:07:52 -05:00
|
|
|
static /*enum dom_select_pseudo*/ unsigned int
|
2005-12-28 08:05:14 -05:00
|
|
|
get_dom_select_pseudo(struct dom_scanner_token *token)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
|
|
|
static struct {
|
|
|
|
struct dom_string string;
|
2022-01-27 12:07:52 -05:00
|
|
|
/*enum dom_select_pseudo*/ unsigned int pseudo;
|
2005-12-18 21:44:18 -05:00
|
|
|
} pseudo_info[] = {
|
|
|
|
|
|
|
|
#define INIT_DOM_SELECT_PSEUDO_STRING(str, type) \
|
2006-01-28 16:55:15 -05:00
|
|
|
{ STATIC_DOM_STRING(str), DOM_SELECT_PSEUDO_##type }
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("first-line", FIRST_LINE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("first-letter", FIRST_LETTER),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("selection", SELECTION),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("after", AFTER),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("before", BEFORE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("link", LINK),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("visited", VISITED),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("active", ACTIVE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("hover", HOVER),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("focus", FOCUS),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("target", TARGET),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("enabled", ENABLED),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("disabled", DISABLED),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("checked", CHECKED),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("indeterminate", INDETERMINATE),
|
|
|
|
|
|
|
|
/* Content pseudo-classes: */
|
|
|
|
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("contains", CONTAINS),
|
|
|
|
|
|
|
|
/* Structural pseudo-classes: */
|
|
|
|
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("nth-child", NTH_CHILD),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("nth-last-child", NTH_LAST_CHILD),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("first-child", FIRST_CHILD),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("last-child", LAST_CHILD),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("only-child", ONLY_CHILD),
|
|
|
|
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("nth-of-type", NTH_TYPE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("nth-last-of-type",NTH_LAST_TYPE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("first-of-type", FIRST_TYPE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("last-of-type", LAST_TYPE),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("only-of-type", ONLY_TYPE),
|
|
|
|
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("root", ROOT),
|
|
|
|
INIT_DOM_SELECT_PSEUDO_STRING("empty", EMPTY),
|
|
|
|
|
|
|
|
#undef INIT_DOM_SELECT_PSEUDO_STRING
|
|
|
|
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof_array(pseudo_info); i++)
|
2005-12-28 10:23:36 -05:00
|
|
|
if (!dom_string_casecmp(&pseudo_info[i].string, &token->string))
|
2005-12-18 21:44:18 -05:00
|
|
|
return pseudo_info[i].pseudo;
|
|
|
|
|
|
|
|
return DOM_SELECT_PSEUDO_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Parses attribute selector. For example '[foo="bar"]' or '[foo|="boo"]'. */
|
2006-01-31 16:01:35 -05:00
|
|
|
static enum dom_code
|
2005-12-28 08:05:14 -05:00
|
|
|
parse_dom_select_attribute(struct dom_select_node *sel, struct dom_scanner *scanner)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
2005-12-28 08:05:14 -05:00
|
|
|
struct dom_scanner_token *token = get_dom_scanner_token(scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get '['. */
|
|
|
|
|
2005-12-18 21:44:18 -05:00
|
|
|
if (token->type != '[')
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get the attribute name. */
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
if (!token || token->type != CSS_TOKEN_IDENT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 10:23:36 -05:00
|
|
|
copy_dom_string(&sel->node.string, &token->string);
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get the optional '=' combo or ending ']'. */
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
switch (token->type) {
|
|
|
|
case ']':
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_ANY;
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
case CSS_TOKEN_SELECT_SPACE_LIST:
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_SPACE_LIST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CSS_TOKEN_SELECT_HYPHEN_LIST:
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_HYPHEN_LIST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CSS_TOKEN_SELECT_BEGIN:
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_BEGIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CSS_TOKEN_SELECT_END:
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_END;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CSS_TOKEN_SELECT_CONTAINS:
|
|
|
|
sel->match.attribute |= DOM_SELECT_ATTRIBUTE_CONTAINS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get the required value. */
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
switch (token->type) {
|
|
|
|
case CSS_TOKEN_IDENT:
|
|
|
|
case CSS_TOKEN_STRING:
|
2005-12-28 10:23:36 -05:00
|
|
|
copy_dom_string(&sel->node.data.attribute.value, &token->string);
|
2005-12-18 21:44:18 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get the ending ']'. */
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
if (token && token->type == ']')
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse:
|
|
|
|
*
|
2006-01-01 18:15:20 -05:00
|
|
|
* 0n+1 / 1
|
|
|
|
* 2n+0 / 2n
|
|
|
|
* 2n+1
|
|
|
|
* -0n+2
|
|
|
|
* -0n+1 / -1
|
|
|
|
* 1n+0 / n+0 / n
|
|
|
|
* 0n+0
|
2005-12-18 21:44:18 -05:00
|
|
|
*/
|
2005-12-19 19:50:39 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
/* FIXME: Move somewhere else? dom/scanner.h? */
|
2005-12-19 19:50:39 -05:00
|
|
|
static size_t
|
2005-12-28 08:05:14 -05:00
|
|
|
get_scanner_token_number(struct dom_scanner_token *token)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
2005-12-19 19:50:39 -05:00
|
|
|
size_t number = 0;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 10:23:36 -05:00
|
|
|
while (token->string.length > 0 && isdigit(token->string.string[0])) {
|
2005-12-19 19:50:39 -05:00
|
|
|
size_t old_number = number;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
number *= 10;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
/* -E2BIG */
|
|
|
|
if (old_number > number)
|
|
|
|
return -1;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 10:23:36 -05:00
|
|
|
number += token->string.string[0] - '0';
|
2005-12-28 10:34:42 -05:00
|
|
|
skip_dom_scanner_token_char(token);
|
2005-12-19 19:50:39 -05:00
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
return number;
|
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Parses the '(...)' part of ':nth-of-type(...)' and ':nth-child(...)'. */
|
2006-01-31 16:01:35 -05:00
|
|
|
static enum dom_code
|
2005-12-28 08:05:14 -05:00
|
|
|
parse_dom_select_nth_arg(struct dom_select_nth_match *nth, struct dom_scanner *scanner)
|
2005-12-19 19:50:39 -05:00
|
|
|
{
|
2005-12-28 08:05:14 -05:00
|
|
|
struct dom_scanner_token *token = get_next_dom_scanner_token(scanner);
|
2005-12-19 19:50:39 -05:00
|
|
|
int sign = 1;
|
|
|
|
int number = -1;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (!token || token->type != '(')
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2005-12-19 19:50:39 -05:00
|
|
|
if (!token)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
switch (token->type) {
|
|
|
|
case CSS_TOKEN_IDENT:
|
2005-12-28 08:05:14 -05:00
|
|
|
if (dom_scanner_token_contains(token, "even")) {
|
2005-12-19 19:50:39 -05:00
|
|
|
nth->step = 2;
|
|
|
|
nth->index = 0;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
} else if (dom_scanner_token_contains(token, "odd")) {
|
2005-12-19 19:50:39 -05:00
|
|
|
nth->step = 2;
|
|
|
|
nth->index = 1;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
} else {
|
|
|
|
/* Check for 'n' ident below. */
|
|
|
|
break;
|
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (skip_css_tokens(scanner, ')'))
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
case '-':
|
|
|
|
sign = -1;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (token->type != CSS_TOKEN_IDENT)
|
|
|
|
break;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (token->type != CSS_TOKEN_NUMBER)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-19 19:50:39 -05:00
|
|
|
/* Fall-through */
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
case CSS_TOKEN_NUMBER:
|
|
|
|
number = get_scanner_token_number(token);
|
|
|
|
if (number < 0)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_VALUE_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-19 19:50:39 -05:00
|
|
|
break;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
default:
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-19 19:50:39 -05:00
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
/* The rest can contain n+ part */
|
|
|
|
switch (token->type) {
|
|
|
|
case CSS_TOKEN_IDENT:
|
2005-12-28 08:05:14 -05:00
|
|
|
if (!dom_scanner_token_contains(token, "n"))
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
nth->step = sign * number;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-19 19:50:39 -05:00
|
|
|
|
|
|
|
if (token->type != '+')
|
|
|
|
break;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (!token) return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (token->type != CSS_TOKEN_NUMBER)
|
|
|
|
break;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
number = get_scanner_token_number(token);
|
|
|
|
if (number < 0)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_VALUE_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
nth->index = sign * number;
|
|
|
|
break;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
default:
|
|
|
|
nth->step = 0;
|
|
|
|
nth->index = sign * number;
|
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-19 19:50:39 -05:00
|
|
|
if (skip_css_tokens(scanner, ')'))
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Parse a pseudo-class or -element with the syntax: ':<ident>'. */
|
2006-01-31 16:01:35 -05:00
|
|
|
static enum dom_code
|
2005-12-18 21:44:18 -05:00
|
|
|
parse_dom_select_pseudo(struct dom_select *select, struct dom_select_node *sel,
|
2005-12-28 08:05:14 -05:00
|
|
|
struct dom_scanner *scanner)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
2005-12-28 08:05:14 -05:00
|
|
|
struct dom_scanner_token *token = get_dom_scanner_token(scanner);
|
2022-01-27 12:07:52 -05:00
|
|
|
/*enum dom_select_pseudo*/ unsigned int pseudo;
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code code;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Skip double :'s in front of some pseudo's (::first-line, etc.) */
|
2005-12-18 21:44:18 -05:00
|
|
|
do {
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
} while (token && token->type == ':');
|
|
|
|
|
2005-12-19 18:01:18 -05:00
|
|
|
if (!token || token->type != CSS_TOKEN_IDENT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
pseudo = get_dom_select_pseudo(token);
|
|
|
|
switch (pseudo) {
|
|
|
|
case DOM_SELECT_PSEUDO_UNKNOWN:
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_CONTAINS:
|
|
|
|
/* FIXME: E:contains("text") */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_NTH_CHILD:
|
|
|
|
case DOM_SELECT_PSEUDO_NTH_LAST_CHILD:
|
|
|
|
code = parse_dom_select_nth_arg(&sel->nth_child, scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (code != DOM_CODE_OK)
|
2005-12-18 21:44:18 -05:00
|
|
|
return code;
|
|
|
|
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_CHILD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_FIRST_CHILD:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_CHILD;
|
|
|
|
set_dom_select_nth_match(&sel->nth_child, 0, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_LAST_CHILD:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_CHILD;
|
|
|
|
set_dom_select_nth_match(&sel->nth_child, 0, -1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_ONLY_CHILD:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_CHILD;
|
|
|
|
set_dom_select_nth_match(&sel->nth_child, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_NTH_TYPE:
|
|
|
|
case DOM_SELECT_PSEUDO_NTH_LAST_TYPE:
|
|
|
|
code = parse_dom_select_nth_arg(&sel->nth_type, scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (code != DOM_CODE_OK)
|
2005-12-18 21:44:18 -05:00
|
|
|
return code;
|
|
|
|
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_TYPE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_FIRST_TYPE:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_TYPE;
|
|
|
|
set_dom_select_nth_match(&sel->nth_type, 0, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_LAST_TYPE:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_TYPE;
|
|
|
|
set_dom_select_nth_match(&sel->nth_type, 0, -1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_ONLY_TYPE:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_NTH_TYPE;
|
|
|
|
set_dom_select_nth_match(&sel->nth_type, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_ROOT:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_ROOT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_SELECT_PSEUDO_EMPTY:
|
|
|
|
sel->match.element |= DOM_SELECT_ELEMENT_EMPTY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* It's a bitflag! */
|
|
|
|
select->pseudo |= pseudo;
|
|
|
|
}
|
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* The element relation flags are mutual exclusive. This macro can be used
|
|
|
|
* for checking if anyflag is set. */
|
2005-12-18 21:44:18 -05:00
|
|
|
#define get_element_relation(sel) \
|
|
|
|
((sel)->match.element & DOM_SELECT_RELATION_FLAGS)
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Parse a CSS3 selector and add selector nodes to the @select struct. */
|
2006-01-31 16:01:35 -05:00
|
|
|
static enum dom_code
|
2005-12-21 17:26:22 -05:00
|
|
|
parse_dom_select(struct dom_select *select, struct dom_stack *stack,
|
2005-12-28 09:57:37 -05:00
|
|
|
struct dom_string *string)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
2005-12-28 08:05:14 -05:00
|
|
|
struct dom_scanner scanner;
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select_node sel;
|
|
|
|
|
2006-01-06 22:21:39 -05:00
|
|
|
init_dom_scanner(&scanner, &dom_css_scanner_info, string, 0, 0, 1, 0, 0);
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
memset(&sel, 0, sizeof(sel));
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
while (dom_scanner_has_tokens(&scanner)) {
|
|
|
|
struct dom_scanner_token *token = get_dom_scanner_token(&scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code code;
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select_node *select_node;
|
|
|
|
|
|
|
|
assert(token);
|
|
|
|
|
|
|
|
if (token->type == '{'
|
|
|
|
|| token->type == '}'
|
|
|
|
|| token->type == ';'
|
|
|
|
|| token->type == ',')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Examine the selector fragment */
|
|
|
|
|
|
|
|
switch (token->type) {
|
|
|
|
case CSS_TOKEN_IDENT:
|
|
|
|
sel.node.type = DOM_NODE_ELEMENT;
|
2005-12-28 10:23:36 -05:00
|
|
|
copy_dom_string(&sel.node.string, &token->string);
|
2005-12-28 10:34:42 -05:00
|
|
|
if (dom_scanner_token_contains(token, "*"))
|
2005-12-18 21:44:18 -05:00
|
|
|
sel.match.element |= DOM_SELECT_ELEMENT_UNIVERSAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CSS_TOKEN_HASH:
|
|
|
|
case CSS_TOKEN_HEX_COLOR:
|
|
|
|
/* ID fragment */
|
|
|
|
sel.node.type = DOM_NODE_ATTRIBUTE;
|
|
|
|
sel.match.attribute |= DOM_SELECT_ATTRIBUTE_ID;
|
|
|
|
/* Skip the leading '#'. */
|
2005-12-28 10:34:42 -05:00
|
|
|
skip_dom_scanner_token_char(token);
|
2005-12-18 21:44:18 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
sel.node.type = DOM_NODE_ATTRIBUTE;
|
|
|
|
code = parse_dom_select_attribute(&sel, &scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (code != DOM_CODE_OK)
|
2005-12-18 21:44:18 -05:00
|
|
|
return code;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
2005-12-28 08:05:14 -05:00
|
|
|
token = get_next_dom_scanner_token(&scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
if (!token || token->type != CSS_TOKEN_IDENT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
sel.node.type = DOM_NODE_ATTRIBUTE;
|
|
|
|
sel.match.attribute |= DOM_SELECT_ATTRIBUTE_SPACE_LIST;
|
2022-01-31 10:02:30 -05:00
|
|
|
set_dom_string(&sel.node.string, (char *)"class", -1);
|
2006-01-01 18:15:20 -05:00
|
|
|
copy_dom_string(&sel.node.data.attribute.value, &token->string);
|
2005-12-18 21:44:18 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
code = parse_dom_select_pseudo(select, &sel, &scanner);
|
2006-01-31 16:01:35 -05:00
|
|
|
if (code != DOM_CODE_OK)
|
2005-12-18 21:44:18 -05:00
|
|
|
return code;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
2005-12-24 21:48:53 -05:00
|
|
|
if (get_element_relation(&sel) != DOM_SELECT_RELATION_DESCENDANT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
sel.match.element |= DOM_SELECT_RELATION_DIRECT_CHILD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
2005-12-24 21:48:53 -05:00
|
|
|
if (get_element_relation(&sel) != DOM_SELECT_RELATION_DESCENDANT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
sel.match.element |= DOM_SELECT_RELATION_DIRECT_ADJACENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '~':
|
2005-12-24 21:48:53 -05:00
|
|
|
if (get_element_relation(&sel) != DOM_SELECT_RELATION_DESCENDANT)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
sel.match.element |= DOM_SELECT_RELATION_INDIRECT_ADJACENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_SYNTAX_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
skip_dom_scanner_token(&scanner);
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
if (sel.node.type == DOM_NODE_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
|
2022-01-16 15:08:50 -05:00
|
|
|
select_node = (struct dom_select_node *)mem_calloc(1, sizeof(*select_node));
|
2005-12-18 21:44:18 -05:00
|
|
|
copy_struct(select_node, &sel);
|
|
|
|
|
2005-12-29 20:59:34 -05:00
|
|
|
if (!dom_stack_is_empty(stack)) {
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node *node = &select_node->node;
|
2005-12-29 20:59:34 -05:00
|
|
|
struct dom_node *parent = get_dom_stack_top(stack)->node;
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node_list **list = get_dom_node_list(parent, node);
|
|
|
|
int sort = (node->type == DOM_NODE_ATTRIBUTE);
|
|
|
|
int index;
|
|
|
|
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(list != NULL, "Adding node to bad parent [%d -> %d]",
|
2005-12-29 20:59:34 -05:00
|
|
|
node->type, parent->type);
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
index = *list && (*list)->size > 0 && sort
|
|
|
|
? get_dom_node_map_index(*list, node) : -1;
|
|
|
|
|
|
|
|
if (!add_to_dom_node_list(list, node, index)) {
|
|
|
|
done_dom_node(node);
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_ALLOC_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
2005-12-29 20:59:34 -05:00
|
|
|
|
|
|
|
node->parent = parent;
|
|
|
|
|
2005-12-18 21:44:18 -05:00
|
|
|
} else {
|
|
|
|
assert(!select->selector);
|
|
|
|
select->selector = select_node;
|
|
|
|
}
|
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
code = push_dom_node(stack, &select_node->node);
|
|
|
|
if (code != DOM_CODE_OK)
|
|
|
|
return code;
|
2005-12-29 20:59:34 -05:00
|
|
|
|
|
|
|
if (select_node->node.type != DOM_NODE_ELEMENT)
|
|
|
|
pop_dom_node(stack);
|
|
|
|
|
2005-12-18 21:44:18 -05:00
|
|
|
memset(&sel, 0, sizeof(sel));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (select->selector)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_ERR;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Basically this is just a wrapper for parse_dom_select() to ease error
|
|
|
|
* handling. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select *
|
2005-12-28 09:57:37 -05:00
|
|
|
init_dom_select(enum dom_select_syntax syntax, struct dom_string *string)
|
2005-12-18 21:44:18 -05:00
|
|
|
{
|
2022-01-16 15:08:50 -05:00
|
|
|
struct dom_select *select = (struct dom_select *)mem_calloc(1, sizeof(select));
|
2005-12-21 17:26:22 -05:00
|
|
|
struct dom_stack stack;
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code code;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2006-01-03 14:35:32 -05:00
|
|
|
init_dom_stack(&stack, DOM_STACK_FLAG_NONE);
|
2005-12-29 21:29:17 -05:00
|
|
|
add_dom_stack_tracer(&stack, "init-select: ");
|
2005-12-21 17:26:22 -05:00
|
|
|
|
2005-12-28 09:57:37 -05:00
|
|
|
code = parse_dom_select(select, &stack, string);
|
2005-12-21 17:26:22 -05:00
|
|
|
done_dom_stack(&stack);
|
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
if (code == DOM_CODE_OK)
|
2005-12-18 21:44:18 -05:00
|
|
|
return select;
|
|
|
|
|
|
|
|
done_dom_select(select);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_dom_select(struct dom_select *select)
|
|
|
|
{
|
|
|
|
if (select->selector) {
|
|
|
|
struct dom_node *node = (struct dom_node *) select->selector;
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* This will recursively free all children select nodes. */
|
2005-12-18 21:44:18 -05:00
|
|
|
done_dom_node(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_free(select);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-22 18:11:25 -05:00
|
|
|
/* DOM node selection: */
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* This struct stores data related to the 'application' of a DOM selector
|
|
|
|
* on a DOM tree or stream. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select_data {
|
2005-12-21 21:19:53 -05:00
|
|
|
/* The selector matching stack. The various selector nodes are pushed
|
|
|
|
* onto this stack as they are matched (and later popped when they are
|
|
|
|
* no longer 'reachable', that is, has been popped from the DOM tree or
|
|
|
|
* stream. This way the selector can match each selector node multiple
|
|
|
|
* times and the selection is a simple matter of matching the current
|
|
|
|
* node against each state on this stack. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_stack stack;
|
2005-12-21 21:19:53 -05:00
|
|
|
|
|
|
|
/* Reference to the selector. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select *select;
|
2005-12-21 21:19:53 -05:00
|
|
|
|
|
|
|
/* The list of nodes who have been matched / selected. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node_list *list;
|
|
|
|
};
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* This state struct is used for the select data stack and holds info about the
|
|
|
|
* node that was matched. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select_state {
|
2005-12-21 21:19:53 -05:00
|
|
|
/* The matched node. This is always an element node. */
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node *node;
|
|
|
|
};
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Get a child node of a given type. By design, a selector node can
|
|
|
|
* only have one child per type of node. */
|
2005-12-18 21:44:18 -05:00
|
|
|
static struct dom_select_node *
|
|
|
|
get_child_dom_select_node(struct dom_select_node *selector,
|
|
|
|
enum dom_node_type type)
|
|
|
|
{
|
|
|
|
struct dom_node_list *children = selector->node.data.element.children;
|
2005-12-19 19:48:21 -05:00
|
|
|
struct dom_node *node;
|
|
|
|
int index;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
if (!children)
|
|
|
|
return NULL;
|
|
|
|
|
2005-12-19 19:48:21 -05:00
|
|
|
foreach_dom_node (children, node, index) {
|
|
|
|
if (node->type == type)
|
2005-12-18 21:44:18 -05:00
|
|
|
return (struct dom_select_node *) node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define has_attribute_match(selector, name) \
|
|
|
|
((selector)->match.attribute & (name))
|
|
|
|
|
2005-12-22 18:11:25 -05:00
|
|
|
static int
|
|
|
|
match_attribute_value(struct dom_select_node *selector, struct dom_node *node)
|
|
|
|
{
|
2005-12-24 22:38:30 -05:00
|
|
|
struct dom_string str;
|
2005-12-22 18:11:25 -05:00
|
|
|
struct dom_string *selvalue = &selector->node.data.attribute.value;
|
2005-12-22 18:52:52 -05:00
|
|
|
struct dom_string *value = &node->data.attribute.value;
|
2005-12-24 22:38:30 -05:00
|
|
|
unsigned char separator;
|
|
|
|
int do_compare;
|
|
|
|
|
|
|
|
assert(selvalue->length);
|
2005-12-22 18:11:25 -05:00
|
|
|
|
|
|
|
/* The attribute selector value should atleast be contained in the
|
|
|
|
* attribute value. */
|
|
|
|
if (value->length < selvalue->length)
|
|
|
|
return 0;
|
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
/* The following three matching methods requires the selector value to
|
|
|
|
* match a substring at a well-defined offset. */
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_EXACT)) {
|
|
|
|
return !dom_string_casecmp(value, selvalue);
|
|
|
|
}
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_BEGIN)) {
|
|
|
|
set_dom_string(&str, value->string, selvalue->length);
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
return !dom_string_casecmp(&str, selvalue);
|
|
|
|
}
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_END)) {
|
|
|
|
size_t offset = value->length - selvalue->length;
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
set_dom_string(&str, value->string + offset, selvalue->length);
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
return !dom_string_casecmp(&str, selvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The 3 following matching methods requires the selector value to be a
|
|
|
|
* substring of the value enclosed in a specific separator (with the
|
|
|
|
* begining and ending of the attribute value both being valid
|
|
|
|
* separators). */
|
|
|
|
|
|
|
|
set_dom_string(&str, value->string, value->length);
|
|
|
|
|
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_HYPHEN_LIST)) {
|
|
|
|
separator = '-';
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
} else if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_CONTAINS)) {
|
|
|
|
separator = '\0';
|
|
|
|
|
|
|
|
} if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_SPACE_LIST)) {
|
|
|
|
separator = ' ';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
INTERNAL("No attribute selector matching method defined");
|
2005-12-22 18:11:25 -05:00
|
|
|
return 0;
|
2005-12-24 22:38:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
do_compare = 1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (do_compare
|
|
|
|
&& !dom_string_ncasecmp(&str, selvalue, selvalue->length)) {
|
|
|
|
/* "Contains" matches no matter what comes after. */
|
|
|
|
if (str.length == selvalue->length)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
switch (separator) {
|
|
|
|
case '\0':
|
|
|
|
/* "Contains" matches no matter what comes after. */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
if (str.string[str.length] == separator)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-06-28 14:25:06 -04:00
|
|
|
if (isspace((unsigned char)str.string[str.length]))
|
2005-12-24 22:38:30 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (separator) {
|
|
|
|
case '\0':
|
|
|
|
do_compare = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
do_compare = (str.string[0] == '-');
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-06-28 14:25:06 -04:00
|
|
|
do_compare = isspace((unsigned char)str.string[0]);
|
2005-12-24 22:38:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
str.length--, str.string++;
|
|
|
|
|
|
|
|
} while (str.length >= selvalue->length);
|
2005-12-22 18:11:25 -05:00
|
|
|
|
2005-12-24 22:38:30 -05:00
|
|
|
return 0;
|
2005-12-22 18:11:25 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Match the attribute of an element @node against attribute selector nodes
|
|
|
|
* of a given @base. */
|
2005-12-18 21:44:18 -05:00
|
|
|
static int
|
|
|
|
match_attribute_selectors(struct dom_select_node *base, struct dom_node *node)
|
|
|
|
{
|
|
|
|
struct dom_node_list *attrs = node->data.element.map;
|
|
|
|
struct dom_node_list *selnodes = base->node.data.element.map;
|
|
|
|
struct dom_node *selnode;
|
|
|
|
size_t index;
|
|
|
|
|
|
|
|
assert(base->node.type == DOM_NODE_ELEMENT
|
|
|
|
&& node->type == DOM_NODE_ELEMENT);
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* If there are no attribute selectors that is a clean match ... */
|
2005-12-18 21:44:18 -05:00
|
|
|
if (!selnodes)
|
|
|
|
return 1;
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* ... the opposite goes if there are no attributes to match. */
|
2005-12-18 21:44:18 -05:00
|
|
|
if (!attrs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
foreach_dom_node (selnodes, selnode, index) {
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_node *selector = (struct dom_select_node *) selnode;
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node *attr;
|
|
|
|
|
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_ID)) {
|
|
|
|
size_t idindex;
|
|
|
|
|
2005-12-19 16:13:23 -05:00
|
|
|
attr = NULL;
|
2005-12-18 21:44:18 -05:00
|
|
|
foreach_dom_node (attrs, attr, idindex) {
|
|
|
|
if (attr->data.attribute.id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dom_node_list_member(attrs, idindex))
|
|
|
|
attr = NULL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
attr = get_dom_node_map_entry(attrs, DOM_NODE_ATTRIBUTE,
|
|
|
|
selnode->data.attribute.type,
|
|
|
|
&selnode->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (has_attribute_match(selector, DOM_SELECT_ATTRIBUTE_ANY))
|
|
|
|
continue;
|
|
|
|
|
2005-12-22 18:11:25 -05:00
|
|
|
if (!match_attribute_value(selector, attr))
|
2005-12-18 21:44:18 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-12-24 21:48:53 -05:00
|
|
|
/* XXX: Assume the first context is the one! */
|
|
|
|
#define get_dom_select_state(stack, state) \
|
2005-12-26 23:59:12 -05:00
|
|
|
((struct dom_select_state *) get_dom_stack_state_data((stack)->contexts[0], state))
|
2005-12-24 21:48:53 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
match_element_relation(struct dom_select_node *selector, struct dom_node *node,
|
|
|
|
struct dom_stack *stack)
|
|
|
|
{
|
|
|
|
struct dom_stack_state *state;
|
2022-01-26 13:16:32 -05:00
|
|
|
/*enum dom_select_element_match*/ unsigned int relation = get_element_relation(selector);
|
2005-12-24 21:48:53 -05:00
|
|
|
int i, index;
|
|
|
|
|
|
|
|
assert(relation);
|
|
|
|
|
|
|
|
/* When matching any relation there must be a parent, either so that
|
|
|
|
* the node is a descendant or it is possible to check for siblings. */
|
|
|
|
if (!node->parent)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (relation != DOM_SELECT_RELATION_DIRECT_CHILD) {
|
|
|
|
/* When looking for preceeding siblings of the current node,
|
|
|
|
* the current node cannot be first or not in the list (-1). */
|
|
|
|
index = get_dom_node_list_index(node->parent, node);
|
|
|
|
if (index < 1)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find states which hold the parent of the current selector
|
|
|
|
* and check if the parent selector's node is the parent of the
|
|
|
|
* current node. */
|
|
|
|
foreachback_dom_stack_state(stack, state, i) {
|
|
|
|
struct dom_node *selnode;
|
|
|
|
|
|
|
|
/* We are only interested in states which hold the parent of
|
|
|
|
* the current selector. */
|
|
|
|
if (state->node != selector->node.parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
selnode = get_dom_select_state(stack, state)->node;
|
|
|
|
|
|
|
|
if (relation == DOM_SELECT_RELATION_DIRECT_CHILD) {
|
|
|
|
/* Check if the parent selector's node is the parent of
|
|
|
|
* the current node. */
|
|
|
|
if (selnode == node->parent)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int sibindex;
|
|
|
|
|
|
|
|
/* Check if they are siblings. */
|
|
|
|
if (selnode->parent != node->parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sibindex = get_dom_node_list_index(node->parent, selnode);
|
|
|
|
|
|
|
|
if (relation == DOM_SELECT_RELATION_DIRECT_ADJACENT) {
|
|
|
|
/* Check if the sibling node immediately
|
|
|
|
* preceeds the current node. */
|
|
|
|
if (sibindex + 1 == index)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else { /* DOM_SELECT_RELATION_INDIRECT_ADJACENT */
|
|
|
|
/* Check if the sibling node preceeds the
|
|
|
|
* current node. */
|
|
|
|
if (sibindex < index)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-18 21:44:18 -05:00
|
|
|
#define has_element_match(selector, name) \
|
|
|
|
((selector)->match.element & (name))
|
|
|
|
|
2005-12-22 18:51:57 -05:00
|
|
|
static int
|
2005-12-24 21:48:53 -05:00
|
|
|
match_element_selector(struct dom_select_node *selector, struct dom_node *node,
|
|
|
|
struct dom_stack *stack)
|
2005-12-22 18:51:57 -05:00
|
|
|
{
|
2005-12-22 19:03:39 -05:00
|
|
|
assert(node && node->type == DOM_NODE_ELEMENT);
|
|
|
|
|
2005-12-22 18:51:57 -05:00
|
|
|
if (!has_element_match(selector, DOM_SELECT_ELEMENT_UNIVERSAL)
|
|
|
|
&& dom_node_casecmp(&selector->node, node))
|
|
|
|
return 0;
|
|
|
|
|
2005-12-24 21:48:53 -05:00
|
|
|
if (get_element_relation(selector) != DOM_SELECT_RELATION_DESCENDANT
|
|
|
|
&& !match_element_relation(selector, node, stack))
|
|
|
|
return 0;
|
2005-12-22 18:51:57 -05:00
|
|
|
|
2005-12-22 18:59:56 -05:00
|
|
|
/* Root nodes either have no parents or are the single child of the
|
|
|
|
* document node. */
|
2005-12-22 18:51:57 -05:00
|
|
|
if (has_element_match(selector, DOM_SELECT_ELEMENT_ROOT)
|
2005-12-22 18:59:56 -05:00
|
|
|
&& node->parent) {
|
|
|
|
if (node->parent->type != DOM_NODE_DOCUMENT
|
2005-12-24 07:07:57 -05:00
|
|
|
|| node->parent->data.document.children->size > 1)
|
2005-12-22 18:59:56 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2005-12-22 18:51:57 -05:00
|
|
|
|
|
|
|
if (has_element_match(selector, DOM_SELECT_ELEMENT_EMPTY)
|
2005-12-22 19:03:39 -05:00
|
|
|
&& node->data.element.children
|
|
|
|
&& node->data.element.children->size > 0)
|
2005-12-22 18:51:57 -05:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (has_element_match(selector, DOM_SELECT_ELEMENT_NTH_CHILD)) {
|
|
|
|
/* FIXME */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_element_match(selector, DOM_SELECT_ELEMENT_NTH_TYPE)) {
|
|
|
|
/* FIXME */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check attribute selectors. */
|
|
|
|
if (selector->node.data.element.map
|
|
|
|
&& !match_attribute_selectors(selector, node))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-12-22 18:53:31 -05:00
|
|
|
|
2005-12-24 20:43:08 -05:00
|
|
|
#define get_dom_select_data(stack) ((stack)->current->data)
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Matches an element node being visited against the current selector stack. */
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code
|
2005-12-18 21:44:18 -05:00
|
|
|
dom_select_push_element(struct dom_stack *stack, struct dom_node *node, void *data)
|
|
|
|
{
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_data *select_data = (struct dom_select_data *)get_dom_select_data(stack);
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_stack_state *state;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
foreach_dom_stack_state(&select_data->stack, state, pos) {
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_node *selector = (struct dom_select_node *) state->node;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* FIXME: Since the same dom_select_node can be multiple times
|
|
|
|
* on the select_data->stack, cache what select nodes was
|
|
|
|
* matches so that it is only checked once. */
|
|
|
|
|
2005-12-24 21:48:53 -05:00
|
|
|
if (!match_element_selector(selector, node, &select_data->stack))
|
2005-12-18 21:44:18 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
WDBG("Matched element: %.*s.", node->string.length, node->string.string);
|
|
|
|
/* This node is matched, so push the next selector node to
|
|
|
|
* match on the stack. */
|
|
|
|
selector = get_child_dom_select_node(selector, DOM_NODE_ELEMENT);
|
|
|
|
if (selector)
|
|
|
|
push_dom_node(&select_data->stack, &selector->node);
|
|
|
|
}
|
2006-01-15 18:55:58 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Ensures that nodes, no longer 'reachable' on the stack do not have any
|
|
|
|
* states associated with them on the select data stack. */
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code
|
2005-12-18 21:44:18 -05:00
|
|
|
dom_select_pop_element(struct dom_stack *stack, struct dom_node *node, void *data)
|
|
|
|
{
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_data *select_data = (struct dom_select_data *)get_dom_select_data(stack);
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_stack_state *state;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
stack = &select_data->stack;
|
|
|
|
|
|
|
|
foreachback_dom_stack_state (stack, state, index) {
|
|
|
|
struct dom_select_state *select_state;
|
|
|
|
|
2005-12-24 22:40:58 -05:00
|
|
|
select_state = get_dom_select_state(stack, state);
|
2005-12-18 21:44:18 -05:00
|
|
|
if (select_state->node == node) {
|
|
|
|
pop_dom_state(stack, state);
|
|
|
|
WDBG("Remove element.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-01-15 18:55:58 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* For now this is only for matching the ':contains(<string>)' pseudo-class.
|
|
|
|
* Any node which can contain text and thus characters from the given <string>
|
|
|
|
* are handled in this common callback. */
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code
|
2005-12-18 21:44:18 -05:00
|
|
|
dom_select_push_text(struct dom_stack *stack, struct dom_node *node, void *data)
|
|
|
|
{
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_data *select_data = (struct dom_select_data *)get_dom_select_data(stack);
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_stack_state *state = get_dom_stack_top(&select_data->stack);
|
2022-01-24 14:24:44 -05:00
|
|
|
struct dom_select_node *selector = (struct dom_select_node *) state->node;
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_select_node *text_sel = get_child_dom_select_node(selector, DOM_NODE_TEXT);
|
|
|
|
|
|
|
|
WDBG("Text node: %d chars", node->string.length);
|
|
|
|
|
|
|
|
if (!text_sel)
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
|
|
|
|
switch (node->type) {
|
|
|
|
case DOM_NODE_TEXT:
|
|
|
|
case DOM_NODE_CDATA_SECTION:
|
|
|
|
case DOM_NODE_ENTITY_REFERENCE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("Unhandled type");
|
|
|
|
}
|
2006-01-15 18:55:58 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
return DOM_CODE_OK;
|
2005-12-18 21:44:18 -05:00
|
|
|
}
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Context info for interacting with the DOM tree or stream stack. */
|
2005-12-20 18:58:22 -05:00
|
|
|
static struct dom_stack_context_info dom_select_context_info = {
|
2005-12-20 22:38:04 -05:00
|
|
|
/* Object size: */ 0,
|
2005-12-20 13:20:04 -05:00
|
|
|
/* Push: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ dom_select_push_element,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ dom_select_push_text,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ dom_select_push_text,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ dom_select_push_text,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
},
|
|
|
|
/* Pop: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ dom_select_pop_element,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ NULL,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
}
|
2005-12-18 21:44:18 -05:00
|
|
|
};
|
|
|
|
|
2005-12-21 21:19:53 -05:00
|
|
|
/* Context info related to the private select data stack of matched nodes. */
|
2005-12-20 21:57:17 -05:00
|
|
|
static struct dom_stack_context_info dom_select_data_context_info = {
|
2005-12-20 22:38:04 -05:00
|
|
|
/* Object size: */ sizeof(struct dom_select_state),
|
2005-12-20 21:57:17 -05:00
|
|
|
/* Push: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ NULL,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ NULL,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
},
|
|
|
|
/* Pop: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ NULL,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ NULL,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-12-22 18:53:31 -05:00
|
|
|
|
2005-12-18 21:44:18 -05:00
|
|
|
struct dom_node_list *
|
|
|
|
select_dom_nodes(struct dom_select *select, struct dom_node *root)
|
|
|
|
{
|
|
|
|
struct dom_select_data select_data;
|
|
|
|
struct dom_stack stack;
|
|
|
|
|
|
|
|
memset(&select_data, 0, sizeof(select_data));
|
|
|
|
|
|
|
|
select_data.select = select;;
|
|
|
|
|
2006-01-03 14:35:32 -05:00
|
|
|
init_dom_stack(&stack, DOM_STACK_FLAG_NONE);
|
2005-12-20 19:25:50 -05:00
|
|
|
add_dom_stack_context(&stack, &select_data,
|
|
|
|
&dom_select_context_info);
|
2005-12-29 21:29:17 -05:00
|
|
|
add_dom_stack_tracer(&stack, "select-tree: ");
|
2005-12-20 14:27:20 -05:00
|
|
|
|
2006-01-03 14:35:32 -05:00
|
|
|
init_dom_stack(&select_data.stack, DOM_STACK_FLAG_NONE);
|
2005-12-21 17:26:22 -05:00
|
|
|
add_dom_stack_context(&select_data.stack, &select_data,
|
2005-12-20 21:57:17 -05:00
|
|
|
&dom_select_data_context_info);
|
2005-12-29 21:29:17 -05:00
|
|
|
add_dom_stack_tracer(&select_data.stack, "select-match: ");
|
2005-12-20 21:57:17 -05:00
|
|
|
|
2006-01-31 16:01:35 -05:00
|
|
|
if (push_dom_node(&select_data.stack, &select->selector->node) == DOM_CODE_OK) {
|
2005-12-18 21:44:18 -05:00
|
|
|
get_dom_stack_top(&select_data.stack)->immutable = 1;
|
|
|
|
walk_dom_nodes(&stack, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_dom_stack(&select_data.stack);
|
|
|
|
done_dom_stack(&stack);
|
|
|
|
|
|
|
|
return select_data.list;
|
|
|
|
}
|