1 /* CSS style applier */
2 /* $Id: apply.c,v 1.97 2005/07/15 19:53:40 miciah Exp $ */
13 #include "document/css/apply.h"
14 #include "document/css/css.h"
15 #include "document/css/parser.h"
16 #include "document/css/property.h"
17 #include "document/css/scanner.h"
18 #include "document/css/stylesheet.h"
19 #include "document/html/parser/parse.h"
20 #include "document/options.h"
21 #include "util/align.h"
22 #include "util/color.h"
23 #include "util/lists.h"
24 #include "util/error.h"
25 #include "util/memory.h"
26 #include "util/string.h"
28 /* XXX: Some strange dependency makes it necessary to this include last. */
29 #include "document/html/internal.h"
31 /* #define DEBUG_CSS */
34 /* TODO: A way to disable CSS completely, PLUS a way to stop various property
35 * groups from taking effect. (Ie. way to turn out effect of 'display: none'
36 * or aligning or colors but keeping all the others.) --pasky */
39 typedef void (*css_applier_T
)(struct html_context
*html_context
,
40 struct html_element
*element
,
41 struct css_property
*prop
);
44 css_apply_color(struct html_context
*html_context
, struct html_element
*element
,
45 struct css_property
*prop
)
47 assert(prop
->value_type
== CSS_VT_COLOR
);
49 if (use_document_fg_colors(html_context
->options
))
50 element
->attr
.style
.fg
= prop
->value
.color
;
54 css_apply_background_color(struct html_context
*html_context
,
55 struct html_element
*element
,
56 struct css_property
*prop
)
58 assert(prop
->value_type
== CSS_VT_COLOR
);
60 if (use_document_bg_colors(html_context
->options
))
61 element
->attr
.style
.bg
= prop
->value
.color
;
65 css_apply_display(struct html_context
*html_context
, struct html_element
*element
,
66 struct css_property
*prop
)
68 assert(prop
->value_type
== CSS_VT_DISPLAY
);
70 switch (prop
->value
.display
) {
72 element
->linebreak
= 0;
75 /* 1 or 2, that is the question. I went for 2 since it
76 * gives a more "blocky" feel and it's more common.
78 element
->linebreak
= 2;
81 INTERNAL("Bad prop->value.display %d", prop
->value
.display
);
87 css_apply_font_attribute(struct html_context
*html_context
,
88 struct html_element
*element
, struct css_property
*prop
)
90 assert(prop
->value_type
== CSS_VT_FONT_ATTRIBUTE
);
91 element
->attr
.style
.attr
|= prop
->value
.font_attribute
.add
;
92 element
->attr
.style
.attr
&= ~prop
->value
.font_attribute
.rem
;
95 /* FIXME: Because the current CSS doesn't provide reasonable defaults for each
96 * HTML element this applier will cause bad rendering of <pre> tags. */
98 css_apply_text_align(struct html_context
*html_context
,
99 struct html_element
*element
, struct css_property
*prop
)
101 assert(prop
->value_type
== CSS_VT_TEXT_ALIGN
);
102 element
->parattr
.align
= prop
->value
.text_align
;
105 /* XXX: Sort like the css_property_type */
106 static css_applier_T css_appliers
[CSS_PT_LAST
] = {
107 /* CSS_PT_NONE */ NULL
,
108 /* CSS_PT_BACKGROUND */ css_apply_background_color
,
109 /* CSS_PT_BACKGROUND_COLOR */ css_apply_background_color
,
110 /* CSS_PT_COLOR */ css_apply_color
,
111 /* CSS_PT_DISPLAY */ css_apply_display
,
112 /* CSS_PT_FONT_STYLE */ css_apply_font_attribute
,
113 /* CSS_PT_FONT_WEIGHT */ css_apply_font_attribute
,
114 /* CSS_PT_TEXT_ALIGN */ css_apply_text_align
,
115 /* CSS_PT_TEXT_DECORATION */ css_apply_font_attribute
,
116 /* CSS_PT_WHITE_SPACE */ css_apply_font_attribute
,
119 /* This looks for a match in list of selectors. */
121 examine_element(struct html_context
*html_context
, struct css_selector
*base
,
122 enum css_selector_type seltype
, enum css_selector_relation rel
,
123 struct list_head
*selectors
, struct html_element
*element
,
124 struct list_head
*html_stack
)
126 struct css_selector
*selector
;
130 DBG("examine_element(%p, %s, %d, %d, %p, %.*s);", html_context
, base
->name
, seltype
, rel
, selectors
, element
->namelen
, element
->name
);
131 #define dbginfo(sel, type_, base) \
132 DBG("Matched selector %s (rel %d type %d [m%d])! Children %p !!%d, props !!%d", sel->name, sel->relation, sel->type, sel->type == type_, &sel->leaves, !list_empty(sel->leaves), !list_empty(sel->properties))
134 #define dbginfo(sel, type, base)
137 #define process_found_selector(sel, type, base) \
139 dbginfo(sel, type, base); \
140 merge_css_selectors(base, sel); \
141 /* Ancestor matches? */ \
142 if ((struct list_head *) element->next != html_stack) { \
143 struct html_element *ancestor; \
144 /* This is less effective than doing reverse iterations,
145 * first over sel->leaves and then over the HTML stack,
146 * which shines in the most common case where there are
147 * no CSR_ANCESTOR selector leaves. However we would
148 * have to duplicate the whole examine_element(), so if
149 * profiles won't show it really costs... */ \
150 for (ancestor = element->next; \
151 (struct list_head *) ancestor != html_stack;\
152 ancestor = ancestor->next) \
153 examine_element(html_context, base, \
154 CST_ELEMENT, CSR_ANCESTOR, \
155 &sel->leaves, ancestor, \
157 examine_element(html_context, base, \
158 CST_ELEMENT, CSR_PARENT, \
159 &sel->leaves, element->next, \
162 /* More specific matches? */ \
163 examine_element(html_context, base, type + 1, \
165 &sel->leaves, element, html_stack); \
168 if (seltype
<= CST_ELEMENT
&& element
->namelen
) {
169 selector
= find_css_selector(selectors
, CST_ELEMENT
, rel
,
171 process_found_selector(selector
, CST_ELEMENT
, base
);
173 selector
= find_css_selector(selectors
, CST_ELEMENT
, rel
,
174 element
->name
, element
->namelen
);
175 process_found_selector(selector
, CST_ELEMENT
, base
);
178 if (!element
->options
)
181 /* TODO: More pseudo-classess. --pasky */
182 if (element
->pseudo_class
& ELEMENT_LINK
) {
183 selector
= find_css_selector(selectors
, CST_PSEUDO
, rel
, "link", -1);
184 process_found_selector(selector
, CST_PSEUDO
, base
);
186 if (element
->pseudo_class
& ELEMENT_VISITED
) {
187 selector
= find_css_selector(selectors
, CST_PSEUDO
, rel
, "visited", -1);
188 process_found_selector(selector
, CST_PSEUDO
, base
);
191 code
= get_attr_val(element
->options
, "class", html_context
->options
);
192 if (code
&& seltype
<= CST_CLASS
) {
193 selector
= find_css_selector(selectors
, CST_CLASS
, rel
, code
, -1);
194 process_found_selector(selector
, CST_CLASS
, base
);
196 if (code
) mem_free(code
);
198 code
= get_attr_val(element
->options
, "id", html_context
->options
);
199 if (code
&& seltype
<= CST_ID
) {
200 selector
= find_css_selector(selectors
, CST_ID
, rel
, code
, -1);
201 process_found_selector(selector
, CST_ID
, base
);
203 if (code
) mem_free(code
);
205 #undef process_found_selector
209 struct css_selector
*
210 get_css_selector_for_element(struct html_context
*html_context
,
211 struct html_element
*element
,
212 struct css_stylesheet
*css
,
213 struct list_head
*html_stack
)
216 struct css_selector
*selector
;
218 assert(element
&& element
->options
&& css
);
220 selector
= init_css_selector(NULL
, CST_ELEMENT
, NULL
, 0);
225 DBG("Applying to element %.*s...", element
->namelen
, element
->name
);
228 examine_element(html_context
, selector
, CST_ELEMENT
, CSR_ROOT
,
229 &css
->selectors
, element
, html_stack
);
232 DBG("Element %.*s applied.", element
->namelen
, element
->name
);
235 code
= get_attr_val(element
->options
, "style", html_context
->options
);
237 struct css_selector
*stylesel
;
238 struct scanner scanner
;
240 stylesel
= init_css_selector(NULL
, CST_ELEMENT
, NULL
, 0);
243 init_scanner(&scanner
, &css_scanner_info
, code
, NULL
);
244 css_parse_properties(&stylesel
->properties
, &scanner
);
245 merge_css_selectors(selector
, stylesel
);
246 done_css_selector(stylesel
);
255 apply_css_selector_style(struct html_context
*html_context
,
256 struct html_element
*element
,
257 struct css_selector
*selector
)
259 struct css_property
*property
;
261 foreach (property
, selector
->properties
) {
262 assert(property
->type
< CSS_PT_LAST
);
263 /* We don't assert general prop->value_type here because I
264 * don't want hinder properties' ability to potentially make
265 * use of multiple value types. */
266 assert(css_appliers
[property
->type
]);
267 css_appliers
[property
->type
](html_context
, element
, property
);
272 css_apply(struct html_context
*html_context
, struct html_element
*element
,
273 struct css_stylesheet
*css
, struct list_head
*html_stack
)
275 struct css_selector
*selector
;
277 selector
= get_css_selector_for_element(html_context
, element
, css
,
279 if (!selector
) return;
281 apply_css_selector_style(html_context
, element
, selector
);
283 done_css_selector(selector
);