summaryrefslogtreecommitdiff
path: root/src/stylesheet.h
blob: c5c28127c89c45995f237dd1c0d2a8deeb0ac006 (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
/*
 * 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>
 */

#ifndef css_stylesheet_h_
#define css_stylesheet_h_

#include <inttypes.h>
#include <stdio.h>

#include <parserutils/utils/dict.h>

#include <libcss/errors.h>
#include <libcss/functypes.h>
#include <libcss/stylesheet.h>
#include <libcss/types.h>

#include "parse/parse.h"

typedef struct css_rule css_rule;
typedef struct css_selector css_selector;

/** \todo would a parserutils_buffer be better here? */
typedef struct css_style {
	uint32_t length;		/**< Length, in bytes, of bytecode */
	void *bytecode;			/**< Pointer to bytecode */
} css_style;

typedef enum css_selector_type {
	CSS_SELECTOR_ELEMENT,
	CSS_SELECTOR_CLASS,
	CSS_SELECTOR_ID,
	CSS_SELECTOR_PSEUDO,
	CSS_SELECTOR_ATTRIBUTE,
	CSS_SELECTOR_ATTRIBUTE_EQUAL,
	CSS_SELECTOR_ATTRIBUTE_DASHMATCH,
	CSS_SELECTOR_ATTRIBUTE_INCLUDES
} css_selector_type;

typedef enum css_combinator {
	CSS_COMBINATOR_NONE,
	CSS_COMBINATOR_ANCESTOR,
	CSS_COMBINATOR_PARENT,
	CSS_COMBINATOR_SIBLING
} css_combinator;

typedef struct css_selector_detail {
	const css_string *name;			/**< Interned name */
	const css_string *value;		/**< Interned value, or NULL */

	uint32_t type : 4,			/**< Type of selector */
	         comb : 2,			/**< Type of combinator */
	         next : 1;			/**< Another selector detail 
						 * follows */
} css_selector_detail;

struct css_selector {
	css_selector *combinator;		/**< Combining selector */

	css_rule *rule;				/**< Owning rule */

	uint32_t specificity;			/**< Specificity of selector */

	css_selector_detail data;		/**< Selector data */
};

typedef enum css_rule_type {
	CSS_RULE_UNKNOWN,
	CSS_RULE_SELECTOR,
	CSS_RULE_CHARSET,
	CSS_RULE_IMPORT,
	CSS_RULE_MEDIA,
	CSS_RULE_FONT_FACE,
	CSS_RULE_PAGE
} css_rule_type;

struct css_rule {
	css_rule_type type;			/**< Type of rule */

	union {
		struct {
			uint32_t selector_count;
			css_selector **selectors;
			css_style *style;
		} selector;
		struct {
			uint32_t media;
			uint32_t rule_count;
			css_rule **rules;	/** \todo why this? isn't the 
						 * child list sufficient? */
		} media;
		struct {
			css_style *style;
		} font_face;
		struct {
			uint32_t selector_count;
			css_selector **selectors;
			css_style *style;
		} page;
		struct {
			css_stylesheet *sheet;
		} import;
		struct {
			char *encoding;		/** \todo use MIB enum? */
		} charset;
	} data;					/**< Rule data */

	uint32_t index;				/**< Index of rule in sheet */

	css_stylesheet *owner;			/**< Owning sheet */

	css_rule *parent;			/**< Parent rule */
	css_rule *first_child;			/**< First in child list */
	css_rule *last_child;			/**< Last in child list */
	css_rule *next;				/**< Next rule */
	css_rule *prev;				/**< Previous rule */
};

struct css_stylesheet {
#define HASH_SIZE (37)
	css_selector *selectors[HASH_SIZE];	/**< Hashtable of selectors */

	uint32_t rule_count;			/**< Number of rules in sheet */
	css_rule *rule_list;			/**< List of rules in sheet */

	bool disabled;				/**< Whether this sheet is 
						 * disabled */

	char *url;				/**< URL of this sheet */
	char *title;				/**< Title of this sheet */

	css_origin origin;			/**< Origin of stylesheet */
	uint32_t media;				/**< Bitfield of media types */

	void *ownerNode;			/**< Owning node in document */
	css_rule *ownerRule;			/**< Owning rule in parent */

	css_stylesheet *parent;			/**< Parent sheet */
	css_stylesheet *first_child;		/**< First in child list */
	css_stylesheet *last_child;		/**< Last in child list */
	css_stylesheet *next;			/**< Next in sibling list */
	css_stylesheet *prev;			/**< Previous in sibling list */

	css_import_handler import;		/**< Import callback */
	void *import_pw;			/**< Import handler data */

	css_language_level level;		/**< Language level of sheet */
	css_parser *parser;			/**< Core parser for sheet */
	void *parser_frontend;			/**< Frontend parser */

	parserutils_dict *dictionary;		/**< String dictionary */

	css_alloc alloc;			/**< Allocation function */
	void *pw;				/**< Private word */
};

css_error css_stylesheet_style_create(css_stylesheet *sheet, uint32_t len,
		css_style **style);
css_error css_stylesheet_style_destroy(css_stylesheet *sheet, css_style *style);

css_error css_stylesheet_selector_create(css_stylesheet *sheet,
		css_selector_type type, const css_string *name, 
		const css_string *value, css_selector **selector);
css_error css_stylesheet_selector_destroy(css_stylesheet *sheet,
		css_selector *selector);

css_error css_stylesheet_selector_detail_create(css_stylesheet *sheet,
		css_selector_type type, const css_string *name,
		const css_string *value, css_selector_detail **detail);
css_error css_stylesheet_selector_detail_destroy(css_stylesheet *sheet,
		css_selector_detail *detail);

css_error css_stylesheet_selector_append_specific(css_stylesheet *sheet,
		css_selector **parent, css_selector_detail *specific);

css_error css_stylesheet_selector_combine(css_stylesheet *sheet,
		css_combinator type, css_selector *a, css_selector *b);

css_error css_stylesheet_rule_create(css_stylesheet *sheet, css_rule_type type,
		css_rule **rule);
css_error css_stylesheet_rule_destroy(css_stylesheet *sheet, css_rule *rule);

css_error css_stylesheet_rule_add_selector(css_stylesheet *sheet, 
		css_rule *rule, css_selector *selector);

css_error css_stylesheet_rule_append_style(css_stylesheet *sheet,
		css_rule *rule, css_style *style);

/** \todo registering other rule-type data with css_rules */

css_error css_stylesheet_add_rule(css_stylesheet *sheet, css_rule *rule);
css_error css_stylesheet_remove_rule(css_stylesheet *sheet, css_rule *rule);

void css_stylesheet_dump(css_stylesheet *sheet, FILE *target);

#endif