Add simple support for the display CSS property
[elinks/images.git] / src / document / css / apply.c
blob5c17d1e829c939acd79595ad2ef6516efc6c1582
1 /* CSS style applier */
2 /* $Id: apply.c,v 1.97 2005/07/15 19:53:40 miciah Exp $ */
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
8 #include <stdlib.h>
9 #include <string.h>
11 #include "elinks.h"
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);
43 static void
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;
53 static void
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;
64 static void
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) {
71 case CSS_DISP_INLINE:
72 element->linebreak = 0;
73 break;
74 case CSS_DISP_BLOCK:
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.
77 * YMMV. */
78 element->linebreak = 2;
79 break;
80 default:
81 INTERNAL("Bad prop->value.display %d", prop->value.display);
82 break;
86 static void
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. */
97 static void
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. */
120 static void
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;
127 unsigned char *code;
129 #ifdef DEBUG_CSS
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))
133 #else
134 #define dbginfo(sel, type, base)
135 #endif
137 #define process_found_selector(sel, type, base) \
138 if (selector) { \
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, \
156 html_stack); \
157 examine_element(html_context, base, \
158 CST_ELEMENT, CSR_PARENT, \
159 &sel->leaves, element->next, \
160 html_stack); \
162 /* More specific matches? */ \
163 examine_element(html_context, base, type + 1, \
164 CSR_SPECIFITY, \
165 &sel->leaves, element, html_stack); \
168 if (seltype <= CST_ELEMENT && element->namelen) {
169 selector = find_css_selector(selectors, CST_ELEMENT, rel,
170 "*", 1);
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)
179 return;
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
206 #undef dbginfo
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)
215 unsigned char *code;
216 struct css_selector *selector;
218 assert(element && element->options && css);
220 selector = init_css_selector(NULL, CST_ELEMENT, NULL, 0);
221 if (!selector)
222 return NULL;
224 #ifdef DEBUG_CSS
225 DBG("Applying to element %.*s...", element->namelen, element->name);
226 #endif
228 examine_element(html_context, selector, CST_ELEMENT, CSR_ROOT,
229 &css->selectors, element, html_stack);
231 #ifdef DEBUG_CSS
232 DBG("Element %.*s applied.", element->namelen, element->name);
233 #endif
235 code = get_attr_val(element->options, "style", html_context->options);
236 if (code) {
237 struct css_selector *stylesel;
238 struct scanner scanner;
240 stylesel = init_css_selector(NULL, CST_ELEMENT, NULL, 0);
242 if (stylesel) {
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);
248 mem_free(code);
251 return selector;
254 void
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);
271 void
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,
278 html_stack);
279 if (!selector) return;
281 apply_css_selector_style(html_context, element, selector);
283 done_css_selector(selector);