1 /* CSS module management */
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"),
30 N_("Options concerning how to use CSS for styling documents.")),
32 INIT_OPT_BOOL("document.css", N_("Enable CSS"),
34 N_("Enable adding of CSS style info to documents.")),
36 INIT_OPT_BOOL("document.css", N_("Import external style sheets"),
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"),
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.")),
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
)
64 cached
= get_redirected_cache_entry(uri
);
67 fragment
= get_cache_fragment(cached
);
69 unsigned char *end
= fragment
->data
+ fragment
->length
;
72 css_parse_stylesheet(css
, uri
, fragment
->data
, end
);
79 import_css_file(struct css_stylesheet
*css
, struct uri
*base_uri
,
80 unsigned char *url
, int urllen
)
82 struct string string
, filename
;
85 || css
->import_level
>= MAX_REDIRECTS
86 || !init_string(&filename
))
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
;
99 css_parse_stylesheet(css
, base_uri
, string
.source
, end
);
100 done_string(&string
);
104 done_string(&filename
);
107 struct css_stylesheet default_stylesheet
= INIT_CSS_STYLESHEET(default_stylesheet
, import_css_file
);
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
);
119 import_css_file(&default_stylesheet
, NULL
, url
, strlen(url
));
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);
136 init_css(struct module
*module
)
138 struct change_hook_info css_change_hooks
[] = {
139 { "document.css", change_hook_css
},
143 register_change_hooks(css_change_hooks
);
144 import_default_css();
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
,
158 /* submodules: */ NULL
,
160 /* init: */ init_css
,