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

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

#include "bytecode/bytecode.h"
#include "parse/parse.h"
#include "select/hash.h"

typedef struct css_rule css_rule;
typedef struct css_selector css_selector;

typedef struct css_style {
	css_code_t *bytecode;	      /**< Pointer to bytecode */
	uint32_t used;		      /**< number of code entries used */
	uint32_t allocated;	      /**< number of allocated code entries */
	struct css_stylesheet *sheet; /**< containing sheet */
} css_style;

typedef enum css_selector_type {
	CSS_SELECTOR_ELEMENT,
	CSS_SELECTOR_CLASS,
	CSS_SELECTOR_ID,
	CSS_SELECTOR_PSEUDO_CLASS,
	CSS_SELECTOR_PSEUDO_ELEMENT,
	CSS_SELECTOR_ATTRIBUTE,
	CSS_SELECTOR_ATTRIBUTE_EQUAL,
	CSS_SELECTOR_ATTRIBUTE_DASHMATCH,
	CSS_SELECTOR_ATTRIBUTE_INCLUDES,
	CSS_SELECTOR_ATTRIBUTE_PREFIX,
	CSS_SELECTOR_ATTRIBUTE_SUFFIX,
	CSS_SELECTOR_ATTRIBUTE_SUBSTRING
} css_selector_type;

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

typedef enum css_selector_detail_value_type {
	CSS_SELECTOR_DETAIL_VALUE_STRING,
	CSS_SELECTOR_DETAIL_VALUE_NTH
} css_selector_detail_value_type;

typedef union css_selector_detail_value {
	lwc_string *string;		/**< Interned string, or NULL */
	struct {
		int32_t a;
		int32_t b;
	} nth;				/**< Data for x = an + b */
} css_selector_detail_value;

typedef struct css_selector_detail {
	css_qname qname;			/**< Interned name */
	css_selector_detail_value value;	/**< Detail value */

	unsigned int type       : 4,		/**< Type of selector */
		     comb       : 3,		/**< Type of combinator */
		     next       : 1,		/**< Another selector detail
						 * follows */
		     value_type : 1,		/**< Type of value field */
		     negate     : 1;		/**< Detail match is inverted */
} css_selector_detail;

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

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

#define CSS_SPECIFICITY_A 0x01000000
#define CSS_SPECIFICITY_B 0x00010000
#define CSS_SPECIFICITY_C 0x00000100
#define CSS_SPECIFICITY_D 0x00000001
	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;

typedef enum css_rule_parent_type {
	CSS_RULE_PARENT_STYLESHEET,
	CSS_RULE_PARENT_RULE
} css_rule_parent_type;

struct css_rule {
	void *parent;				/**< containing rule or owning
						 * stylesheet (defined by ptype)
						 */
	css_rule *next;				/**< next in list */
	css_rule *prev;				/**< previous in list */

	unsigned int type  :  4,		/**< css_rule_type */
		     index : 16,		/**< index in sheet */
		     items :  8,		/**< # items in rule */
		     ptype :  1;		/**< css_rule_parent_type */
} _ALIGNED;

typedef struct css_rule_selector {
	css_rule base;

	css_selector **selectors;
	css_style *style;
} css_rule_selector;

typedef struct css_rule_media {
	css_rule base;

	uint64_t media;

	css_rule *first_child;
	css_rule *last_child;
} css_rule_media;

typedef struct css_rule_font_face {
	css_rule base;

	css_font_face *font_face;
} css_rule_font_face;

typedef struct css_rule_page {
	css_rule base;

	css_selector *selector;
	css_style *style;
} css_rule_page;

typedef struct css_rule_import {
	css_rule base;

	lwc_string *url;
	uint64_t media;

	css_stylesheet *sheet;
} css_rule_import;

typedef struct css_rule_charset {
	css_rule base;

	lwc_string *encoding;	/** \todo use MIB enum? */
} css_rule_charset;

struct css_stylesheet {
	css_selector_hash *selectors;		/**< Hashtable of selectors */

	uint32_t rule_count;			/**< Number of rules in sheet */
	css_rule *rule_list;			/**< List of rules in sheet */
	css_rule *last_rule;			/**< Last rule in list */

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

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

	css_language_level level;		/**< Language level of sheet */
	css_parser *parser;			/**< Core parser for sheet */
	void *parser_frontend;			/**< Frontend parser */
	lwc_string **propstrings;		/**< Property strings, for parser */

	bool quirks_allowed;			/**< Quirks permitted */
	bool quirks_used;			/**< Quirks actually used */

	bool inline_style;			/**< Is an inline style */

	size_t size;				/**< Size, in bytes */

	css_import_notification_fn import;	/**< Import notification function */
	void *import_pw;			/**< Private word */

	css_url_resolution_fn resolve;		/**< URL resolution function */
	void *resolve_pw;			/**< Private word */

	css_color_resolution_fn color;		/**< Colour resolution function */
	void *color_pw;				/**< Private word */

	/** System font resolution function */
	css_font_resolution_fn font;
	void *font_pw;				/**< Private word */

	css_style *cached_style;		/**< Cache for style parsing */

	lwc_string **string_vector;             /**< Bytecode string vector */
	uint32_t string_vector_l;               /**< The string vector allocated
						 * length in entries */
	uint32_t string_vector_c;               /**< The number of string
						 * vector entries used */
};

css_error css__stylesheet_style_create(css_stylesheet *sheet,
		css_style **style);
css_error css__stylesheet_style_append(css_style *style, css_code_t code);
css_error css__stylesheet_style_vappend(css_style *style, uint32_t style_count,
		...);
css_error css__stylesheet_style_destroy(css_style *style);
css_error css__stylesheet_merge_style(css_style *target, css_style *style);

/** Helper function to avoid distinct buildOPV call */
static inline css_error css__stylesheet_style_appendOPV(css_style *style,
		opcode_t opcode, uint8_t flags, uint16_t value)
{
	return css__stylesheet_style_append(style,
			buildOPV(opcode, flags, value));
}

/** Helper function to set inherit flag */
static inline css_error css_stylesheet_style_inherit(css_style *style,
		opcode_t opcode)
{
	return css__stylesheet_style_append(style,
			buildOPV(opcode, FLAG_INHERIT, 0));
}

css_error css__stylesheet_selector_create(css_stylesheet *sheet,
		css_qname *qname, css_selector **selector);
css_error css__stylesheet_selector_destroy(css_stylesheet *sheet,
		css_selector *selector);

css_error css__stylesheet_selector_detail_init(css_stylesheet *sheet,
		css_selector_type type, css_qname *qname,
		css_selector_detail_value value,
		css_selector_detail_value_type value_type,
		bool negate, css_selector_detail *detail);

css_error css__stylesheet_selector_append_specific(css_stylesheet *sheet,
		css_selector **parent, const 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);

css_error css__stylesheet_rule_set_charset(css_stylesheet *sheet,
		css_rule *rule, lwc_string *charset);

css_error css__stylesheet_rule_set_nascent_import(css_stylesheet *sheet,
		css_rule *rule, lwc_string *url, uint64_t media);

css_error css__stylesheet_rule_set_media(css_stylesheet *sheet,
		css_rule *rule, uint64_t media);

css_error css__stylesheet_rule_set_page_selector(css_stylesheet *sheet,
		css_rule *rule, css_selector *sel);

css_error css__stylesheet_add_rule(css_stylesheet *sheet, css_rule *rule,
		css_rule *parent);
css_error css__stylesheet_remove_rule(css_stylesheet *sheet, css_rule *rule);

css_error css__stylesheet_string_get(css_stylesheet *sheet,
		uint32_t string_number, lwc_string **string);

css_error css__stylesheet_string_add(css_stylesheet *sheet,
		lwc_string *string, uint32_t *string_number);

#endif