summaryrefslogtreecommitdiff
path: root/src/parse/css21.c
blob: 40a728b3429651b270c4e8e69c70d12ea8a39700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * This file is part of LibCSS.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 */

#include <assert.h>
#include <string.h>

#include <parserutils/utils/stack.h>

#include "lex/lex.h"
#include "parse/css21.h"
#include "parse/parse.h"

#include "utils/parserutilserror.h"
#include "utils/utils.h"

enum {
	CHARSET, IMPORT, MEDIA, PAGE,

	LAST_KNOWN
};

static struct {
	const char *ptr;
	size_t len;
} stringmap[] = {
	{ "charset", SLEN("charset") },
	{ "import", SLEN("import") },
	{ "media", SLEN("media") },
	{ "page", SLEN("page") },
};

typedef struct context_entry {
	css_parser_event type;		/**< Type of entry */
	const uint8_t *data;		/**< Pointer to interned string */
} context_entry;

/**
 * Context for a CSS 2.1 parser
 */
struct css_css21 {
	css_stylesheet *sheet;		/**< The stylesheet to parse for */
	css_parser *parser;		/**< The underlying core parser */

#define STACK_CHUNK 32
	parserutils_stack *context;	/**< Context stack */

	enum {
		BEFORE_CHARSET,
		BEFORE_RULES,
		HAD_RULE,
	} state;			/**< State flag, for at-rule handling */

	const uint8_t *strings[LAST_KNOWN];	/**< Interned strings */

	css_alloc alloc;		/**< Memory (de)allocation function */
	void *pw;			/**< Client's private data */
};

static css_error css21_handle_event(css_parser_event type, 
		const parserutils_vector *tokens, void *pw);
static inline css_error handleStartStylesheet(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleEndStylesheet(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleStartRuleset(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleEndRuleset(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleStartAtRule(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleEndAtRule(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleStartBlock(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleEndBlock(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleBlockContent(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleSelector(css_css21 *c, 
		const parserutils_vector *vector);
static inline css_error handleDeclaration(css_css21 *c, 
		const parserutils_vector *vector);

/**
 * Create a CSS 2.1 parser
 *
 * \param sheet   The stylesheet object to parse for
 * \param parser  The core parser object to use
 * \param alloc   Memory (de)allocation function
 * \param pw      Pointer to client-specific private data
 * \return Pointer to parser object, or NULL on failure
 */
css_css21 *css_css21_create(css_stylesheet *sheet, css_parser *parser,
		css_alloc alloc, void *pw)
{
	css_css21 *css21;
	css_parser_optparams params;
	css_error error;

	if (sheet == NULL || parser == NULL || alloc == NULL)
		return NULL;

	css21 = alloc(NULL, sizeof(css_css21), pw);
	if (css21 == NULL)
		return NULL;

	css21->context = parserutils_stack_create(sizeof(context_entry), 
			STACK_CHUNK, (parserutils_alloc) alloc, pw);
	if (css21->context == NULL) {
		alloc(css21, 0, pw);
		return NULL;
	}

	/* Intern all known strings */
	for (int i = 0; i < LAST_KNOWN; i++) {
		css21->strings[i] = css_parser_dict_add(parser,
				(const uint8_t *) stringmap[i].ptr, 
				stringmap[i].len);
		if (css21->strings[i] == NULL) {
			parserutils_stack_destroy(css21->context);
			alloc(css21, 0, pw);
			return NULL;
		}
	}

	params.event_handler.handler = css21_handle_event;
	params.event_handler.pw = css21;
	error = css_parser_setopt(parser, CSS_PARSER_EVENT_HANDLER, &params);
	if (error != CSS_OK) {
		parserutils_stack_destroy(css21->context);
		alloc(css21, 0, pw);
		return NULL;
	}

	css21->sheet = sheet;
	css21->parser = parser;
	css21->state = BEFORE_CHARSET;
	css21->alloc = alloc;
	css21->pw = pw;

	return css21;
}

/**
 * Destroy a CSS 2.1 parser
 *
 * \param css21  The parser to destroy
 */
void css_css21_destroy(css_css21 *css21)
{
	if (css21 == NULL)
		return;

	parserutils_stack_destroy(css21->context);

	css21->alloc(css21, 0, css21->pw);
}

/**
 * Handler for core parser events
 *
 * \param type    The event type
 * \param tokens  Vector of tokens read since last event, or NULL
 * \param pw      Pointer to handler context
 * \return CSS_OK on success, CSS_INVALID to indicate parse error, 
 *         appropriate error otherwise.
 */
css_error css21_handle_event(css_parser_event type, 
		const parserutils_vector *tokens, void *pw)
{
	css_css21 *css21 = (css_css21 *) pw;

	switch (type) {
	case CSS_PARSER_START_STYLESHEET:
		return handleStartStylesheet(css21, tokens);
	case CSS_PARSER_END_STYLESHEET:
		return handleEndStylesheet(css21, tokens);
	case CSS_PARSER_START_RULESET:
		return handleStartRuleset(css21, tokens);
	case CSS_PARSER_END_RULESET:
		return handleEndRuleset(css21, tokens);
	case CSS_PARSER_START_ATRULE:
		return handleStartAtRule(css21, tokens);
	case CSS_PARSER_END_ATRULE:
		return handleEndAtRule(css21, tokens);
	case CSS_PARSER_START_BLOCK:
		return handleStartBlock(css21, tokens);
	case CSS_PARSER_END_BLOCK:
		return handleEndBlock(css21, tokens);
	case CSS_PARSER_BLOCK_CONTENT:
		return handleBlockContent(css21, tokens);
	case CSS_PARSER_DECLARATION:
		return handleDeclaration(css21, tokens);
	}

	return CSS_OK;
}

/******************************************************************************
 * Parser stages                                                              *
 ******************************************************************************/

css_error handleStartStylesheet(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	context_entry entry = { CSS_PARSER_START_STYLESHEET, NULL };

	UNUSED(vector);

	assert(c != NULL);

	perror = parserutils_stack_push(c->context, (void *) &entry);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleEndStylesheet(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	context_entry *entry;

	UNUSED(vector);

	assert(c != NULL);

	entry = parserutils_stack_get_current(c->context);
	if (entry == NULL || entry->type != CSS_PARSER_START_STYLESHEET)
		return CSS_INVALID;

	perror = parserutils_stack_pop(c->context, NULL);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleStartRuleset(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	css_error error;
	context_entry entry = { CSS_PARSER_START_RULESET, NULL };

	assert(c != NULL);

	error = handleSelector(c, vector);
	if (error != CSS_OK)
		return error;

	perror = parserutils_stack_push(c->context, (void *) &entry);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleEndRuleset(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	context_entry *entry;

	UNUSED(vector);

	assert(c != NULL);

	entry = parserutils_stack_get_current(c->context);
	if (entry == NULL || entry->type != CSS_PARSER_START_RULESET)
		return CSS_INVALID;

	perror = parserutils_stack_pop(c->context, NULL);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleStartAtRule(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	context_entry entry = { CSS_PARSER_START_ATRULE, NULL };

	UNUSED(vector);

	assert(c != NULL);

	/* vector contains: ATKEYWORD ws any0 */
	const css_token *token = NULL;
	const css_token *atkeyword = NULL;
	int32_t any = 0;
	int32_t ctx = 0;

	do {
		any = ctx;

		token = parserutils_vector_iterate(vector, &ctx);
		if (token == NULL)
			break;

		if (atkeyword == NULL)
			atkeyword = token;
		else if (token->type != CSS_TOKEN_S)
			break;
	} while (token != NULL);

	/* We now have an ATKEYWORD and the context for the start of any0, if 
	 * there is one */
	assert(atkeyword != NULL && atkeyword->type == CSS_TOKEN_ATKEYWORD);

	if (atkeyword->lower.ptr == c->strings[CHARSET]) {
		if (c->state == BEFORE_CHARSET) {
			/* any0 = STRING */
			if (any == 0)
				return CSS_INVALID;

			token = parserutils_vector_iterate(vector, &any);
			if (token == NULL || token->type != CSS_TOKEN_STRING)
				return CSS_INVALID;

			token = parserutils_vector_iterate(vector, &any);
			if (token != NULL)
				return CSS_INVALID;

			c->state = BEFORE_RULES;
		} else {
			return CSS_INVALID;
		}
	} else if (atkeyword->lower.ptr == c->strings[IMPORT]) {
		if (c->state != HAD_RULE) {
			/* any0 = (STRING | URI) ws 
			 *        (IDENT ws (',' ws IDENT ws)* )? */
			const css_token *uri = 
				parserutils_vector_iterate(vector, &any);
			if (uri == NULL || (token->type != CSS_TOKEN_STRING && 
					token->type != CSS_TOKEN_URI))
				return CSS_INVALID;

			/* Whitespace */
			do {
				token = parserutils_vector_iterate(vector, 
						&any);
				if (token == NULL || token->type != CSS_TOKEN_S)
					break;
			} while (token != NULL);

			/** \todo Media list */
			if (token != NULL) {
			}

			/** \todo trigger fetch of imported sheet */

			c->state = BEFORE_RULES;
		} else {
			return CSS_INVALID;
		}
	} else if (atkeyword->lower.ptr == c->strings[MEDIA]) {
		/** \todo any0 = IDENT ws (',' ws IDENT ws)* */
	} else if (atkeyword->lower.ptr == c->strings[PAGE]) {
		/** \todo any0 = (':' IDENT)? ws */
	} else {
		return CSS_INVALID;
	}

	entry.data = atkeyword->lower.ptr;

	perror = parserutils_stack_push(c->context, (void *) &entry);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleEndAtRule(css_css21 *c, const parserutils_vector *vector)
{
	parserutils_error perror;
	context_entry *entry;

	UNUSED(vector);

	assert(c != NULL);

	entry = parserutils_stack_get_current(c->context);
	if (entry == NULL || entry->type != CSS_PARSER_START_ATRULE)
		return CSS_INVALID;

	perror = parserutils_stack_pop(c->context, NULL);
	if (perror != PARSERUTILS_OK) {
		return css_error_from_parserutils_error(perror);
	}

	return CSS_OK;
}

css_error handleStartBlock(css_css21 *c, const parserutils_vector *vector)
{
	UNUSED(c);
	UNUSED(vector);

	/* We don't care about blocks. In CSS2.1 they're always attached to 
	 * rulesets or at-rules. */

	return CSS_OK;
}

css_error handleEndBlock(css_css21 *c, const parserutils_vector *vector)
{
	UNUSED(c);
	UNUSED(vector);

	/* We don't care about blocks. In CSS 2.1 they're always attached to 
	 * rulesets or at-rules. */

	return CSS_OK;
}

css_error handleBlockContent(css_css21 *c, const parserutils_vector *vector)
{
	UNUSED(c);
	UNUSED(vector);

	/* In CSS 2.1, block content comprises either declarations (if the 
	 * current block is associated with @page or a selector), or rulesets 
	 * (if the current block is associated with @media). */

	return CSS_OK;
}

css_error handleSelector(css_css21 *c, const parserutils_vector *vector)
{
	UNUSED(c);
	UNUSED(vector);

	/* CSS 2.1 selector syntax:
	 *
	 * selector_list   -> selector [ ',' ws selector ]*
	 * selector        -> simple_selector [ combinator simple_selector ]*
	 * simple_selector -> element_name [ HASH | class | attrib | pseudo ]*
	 *                 -> [ HASH | class | attrib | pseudo ]+
	 * combinator      -> '+' ws | '>' ws | ws1
	 * element_name    -> IDENT | '*'
	 * class           -> '.' IDENT
	 * attrib          -> '[' ws IDENT ws [ 
	 *                           [ '=' | INCLUDES | DASHMATCH ] ws 
	 *                           [ IDENT | STRING ] ws ]? ']' 
	 * pseudo          -> ':' [ IDENT | FUNCTION ws IDENT? ws ')' ]
	 */

	return CSS_OK;
}

css_error handleDeclaration(css_css21 *c, const parserutils_vector *vector)
{
	UNUSED(c);
	UNUSED(vector);

	/* Locations where declarations are permitted:
	 *
	 * + In @page
	 * + In ruleset
	 */

	/* IDENT ws ':' ws value 
	 * 
	 * In CSS 2.1, value is any1, so '{' or ATKEYWORD => parse error
	 */

	return CSS_OK;
}