grafthistory: support curl
[elinks/elinks-j605.git] / src / document / css / css.c
blob42c9ef3537cbe94ae3f1f22247ed8ccb39d55743
1 /* CSS module management */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdlib.h>
9 #include "elinks.h"
11 #include "cache/cache.h"
12 #include "config/home.h"
13 #include "config/options.h"
14 #include "document/css/css.h"
15 #include "document/css/parser.h"
16 #include "document/css/stylesheet.h"
17 #include "encoding/encoding.h"
18 #include "intl/gettext/libintl.h"
19 #include "main/module.h"
20 #include "network/connection.h"
21 #include "protocol/uri.h"
22 #include "util/error.h"
23 #include "util/memory.h"
24 #include "viewer/text/draw.h"
27 struct option_info css_options_info[] = {
28 INIT_OPT_TREE("document", N_("Cascading Style Sheets"),
29 "css", OPT_SORT,
30 N_("Options concerning how to use CSS for styling documents.")),
32 INIT_OPT_BOOL("document.css", N_("Enable CSS"),
33 "enable", 0, 1,
34 N_("Enable adding of CSS style info to documents.")),
36 INIT_OPT_BOOL("document.css", N_("Import external style sheets"),
37 "import", 0, 1,
38 N_("When enabled any external style sheets that are imported from\n"
39 "either CSS itself using the @import keyword or from the HTML using\n"
40 "<link> tags in the document header will also be downloaded.")),
42 INIT_OPT_STRING("document.css", N_("Default style sheet"),
43 "stylesheet", 0, "",
44 N_("The path to the file containing the default user defined\n"
45 "Cascading Style Sheet. It can be used to control the basic\n"
46 "layout of HTML documents. The path is assumed to be relative\n"
47 "to ELinks' home directory.\n"
48 "Leave as \"\" to use built-in document styling.")),
50 NULL_OPTION_INFO,
54 void
55 import_css(struct css_stylesheet *css, struct uri *uri)
57 /* Do we have it in the cache? (TODO: CSS cache) */
58 struct cache_entry *cached;
59 struct fragment *fragment;
61 if (!uri || css->import_level >= MAX_REDIRECTS)
62 return;
64 cached = get_redirected_cache_entry(uri);
65 if (!cached) return;
67 fragment = get_cache_fragment(cached);
68 if (fragment) {
69 unsigned char *end = fragment->data + fragment->length;
71 css->import_level++;
72 css_parse_stylesheet(css, uri, fragment->data, end);
73 css->import_level--;
78 static void
79 import_css_file(struct css_stylesheet *css, struct uri *base_uri,
80 unsigned char *url, int urllen)
82 struct string string, filename;
84 if (!*url
85 || css->import_level >= MAX_REDIRECTS
86 || !init_string(&filename))
87 return;
89 if (*url != '/' && elinks_home) {
90 add_to_string(&filename, elinks_home);
93 add_bytes_to_string(&filename, url, urllen);
95 if (read_encoded_file(&filename, &string) == S_OK) {
96 unsigned char *end = string.source + string.length;
98 css->import_level++;
99 css_parse_stylesheet(css, base_uri, string.source, end);
100 done_string(&string);
101 css->import_level--;
104 done_string(&filename);
107 struct css_stylesheet default_stylesheet = INIT_CSS_STYLESHEET(default_stylesheet, import_css_file);
109 static void
110 import_default_css(void)
112 unsigned char *url = get_opt_str("document.css.stylesheet");
114 if (!list_empty(default_stylesheet.selectors))
115 done_css_stylesheet(&default_stylesheet);
117 if (!*url) return;
119 import_css_file(&default_stylesheet, NULL, url, strlen(url));
122 static int
123 change_hook_css(struct session *ses, struct option *current, struct option *changed)
125 if (!strcmp(changed->name, "stylesheet")) {
126 /* TODO: We need to update all entries in format cache. --jonas */
127 import_default_css();
130 draw_formatted(ses, 1);
132 return 0;
135 static void
136 init_css(struct module *module)
138 struct change_hook_info css_change_hooks[] = {
139 { "document.css", change_hook_css },
140 { NULL, NULL },
143 register_change_hooks(css_change_hooks);
144 import_default_css();
147 void
148 done_css(struct module *module)
150 done_css_stylesheet(&default_stylesheet);
154 struct module css_module = struct_module(
155 /* name: */ N_("Cascading Style Sheets"),
156 /* options: */ css_options_info,
157 /* hooks: */ NULL,
158 /* submodules: */ NULL,
159 /* data: */ NULL,
160 /* init: */ init_css,
161 /* done: */ done_css