2007-07-27 19:31:58 -04:00
|
|
|
/** CSS module management
|
|
|
|
* @file */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "cache/cache.h"
|
|
|
|
#include "config/home.h"
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "document/css/css.h"
|
|
|
|
#include "document/css/parser.h"
|
|
|
|
#include "document/css/stylesheet.h"
|
|
|
|
#include "encoding/encoding.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/module.h"
|
|
|
|
#include "network/connection.h"
|
|
|
|
#include "protocol/uri.h"
|
2007-01-27 15:33:02 -05:00
|
|
|
#include "session/session.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "viewer/text/draw.h"
|
|
|
|
|
|
|
|
|
bug 764: Initialize the right member of union option_value
INIT_OPTION used to initialize union option_value at compile time by
casting the default value to LIST_OF(struct option) *, which is the
type of the first member. On sparc64 and other big-endian systems
where sizeof(int) < sizeof(struct list_head *), this tended to leave
option->value.number as zero, thus messing up OPT_INT and OPT_BOOL
at least. OPT_LONG however tended to work right.
This would be easy to fix with C99 designated initializers,
but doc/hacking.txt says ELinks must be kept C89 compatible.
Another solution would be to make register_options() read the
value from option->value.tree (the first member), cast it back
to the right type, and write it to the appropriate member;
but that would still require somewhat dubious conversions
between integers, data pointers, and function pointers.
So here's a rather more invasive solution. Add struct option_init,
which is somewhat similar to struct option but has non-overlapping
members for different types of values, to ensure nothing is lost
in compile-time conversions. Move unsigned char *path from struct
option_info to struct option_init, and replace struct option_info
with a union that contains struct option_init and struct option.
Now, this union can be initialized with no portability problems,
and register_options() then moves the values from struct option_init
to their final places in struct option.
In my x86 ELinks build with plenty of options configured in, this
change bloated the text section by 340 bytes but compressed the data
section by 2784 bytes, presumably because union option_info is a
pointer smaller than struct option_info was.
(cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835)
Conflicts:
src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT.
src/protocol/smb/smb2.c: Ditto.
2009-08-15 15:39:07 -04:00
|
|
|
union option_info css_options_info[] = {
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_TREE("document", N_("Cascading Style Sheets"),
|
|
|
|
"css", OPT_SORT,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("Options concerning how to use CSS for styling "
|
|
|
|
"documents.")),
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_BOOL("document.css", N_("Enable CSS"),
|
|
|
|
"enable", OPT_ZERO, 1,
|
2005-09-15 09:58:31 -04:00
|
|
|
N_("Enable adding of CSS style info to documents.")),
|
|
|
|
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_BOOL("document.css", N_("Ignore \"display: none\""),
|
|
|
|
"ignore_display_none", OPT_ZERO, 1,
|
2009-03-12 02:46:02 -04:00
|
|
|
N_("When enabled, elements are rendered, even when their "
|
|
|
|
"display property has the value \"none\". Because ELinks's "
|
|
|
|
"CSS support is still very incomplete, this setting can "
|
|
|
|
"improve the way that some documents are rendered.")),
|
2007-12-21 22:20:55 -05:00
|
|
|
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_BOOL("document.css", N_("Import external style sheets"),
|
|
|
|
"import", OPT_ZERO, 1,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("When enabled any external style sheets that are imported "
|
|
|
|
"from either CSS itself using the @import keyword or from the "
|
|
|
|
"HTML using <link> tags in the document header will also be "
|
|
|
|
"downloaded.")),
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_STRING("document.css", N_("Default style sheet"),
|
|
|
|
"stylesheet", OPT_ZERO, "",
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
N_("The path to the file containing the default user defined "
|
|
|
|
"Cascading Style Sheet. It can be used to control the basic "
|
|
|
|
"layout of HTML documents. The path is assumed to be relative "
|
2005-09-15 09:58:31 -04:00
|
|
|
"to ELinks' home directory.\n"
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 13:48:38 -05:00
|
|
|
"\n"
|
2005-09-15 09:58:31 -04:00
|
|
|
"Leave as \"\" to use built-in document styling.")),
|
|
|
|
|
2022-03-02 12:30:25 -05:00
|
|
|
INIT_OPT_STRING("document.css", N_("Media types"),
|
|
|
|
"media", OPT_ZERO, "tty",
|
2009-03-12 02:46:02 -04:00
|
|
|
N_("CSS media types that ELinks claims to support, separated "
|
|
|
|
"with commas. The \"all\" type is implied. Currently, only "
|
|
|
|
"ASCII characters work reliably here. See CSS2 section 7: "
|
2007-12-21 22:18:06 -05:00
|
|
|
"http://www.w3.org/TR/1998/REC-CSS2-19980512/media.html")),
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
NULL_OPTION_INFO,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-12-21 22:18:06 -05:00
|
|
|
/** Check whether ELinks claims to support a specific CSS media type.
|
|
|
|
*
|
|
|
|
* @param optstr
|
|
|
|
* Null-terminated value of the document.css.media option.
|
|
|
|
* @param token
|
2008-01-12 03:08:57 -05:00
|
|
|
* A name parsed from a CSS file or from an HTML media attribute.
|
|
|
|
* Need not be null-terminated.
|
2007-12-21 22:18:06 -05:00
|
|
|
* @param token_length
|
|
|
|
* Length of @a token, in bytes.
|
|
|
|
*
|
|
|
|
* Both strings should be in the ASCII charset.
|
|
|
|
*
|
|
|
|
* @return nonzero if the media type is supported, 0 if not. */
|
|
|
|
int
|
2021-01-02 10:20:27 -05:00
|
|
|
supports_css_media_type(const char *optstr,
|
|
|
|
const char *token, size_t token_length)
|
2007-12-21 22:18:06 -05:00
|
|
|
{
|
|
|
|
/* Split @optstr into comma-delimited strings, strip leading
|
|
|
|
* and trailing spaces from each, and compare them to the
|
|
|
|
* token. */
|
|
|
|
while (*optstr != '\0') {
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *beg, *end;
|
2007-12-21 22:18:06 -05:00
|
|
|
|
|
|
|
while (*optstr == ' ')
|
|
|
|
++optstr;
|
|
|
|
|
|
|
|
beg = optstr;
|
|
|
|
optstr += strcspn(optstr, ",");
|
|
|
|
end = optstr;
|
|
|
|
while (end > beg && end[-1] == ' ')
|
|
|
|
--end;
|
|
|
|
|
2008-01-12 03:08:57 -05:00
|
|
|
/* W3C REC-html401-19991224 section 6.13:
|
|
|
|
* "3. A case-sensitive match is then made with
|
|
|
|
* the set of media types defined above."
|
|
|
|
* W3C REC-html401-19991224 section 14.2.3:
|
|
|
|
* "media = media-descriptors [CI]"
|
|
|
|
* where CI stands for case-insensitive.
|
|
|
|
* This mismatch has been reported to the
|
|
|
|
* www-html-editor@w3.org mailing list on 2000-11-16.
|
|
|
|
*
|
|
|
|
* W3C REC-CSS2-19980512 section 7.3:
|
|
|
|
* "Media type names are case-insensitive." */
|
2007-12-21 22:18:06 -05:00
|
|
|
if (!strlcasecmp(token, token_length, beg, end - beg))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
while (*optstr == ',')
|
|
|
|
++optstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An explicit "all" is probably rarer than e.g. "tty". */
|
|
|
|
if (!strlcasecmp(token, token_length, "all", 3))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
|
|
|
import_css(struct css_stylesheet *css, struct uri *uri)
|
|
|
|
{
|
|
|
|
/* Do we have it in the cache? (TODO: CSS cache) */
|
|
|
|
struct cache_entry *cached;
|
|
|
|
struct fragment *fragment;
|
|
|
|
|
|
|
|
if (!uri || css->import_level >= MAX_REDIRECTS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cached = get_redirected_cache_entry(uri);
|
|
|
|
if (!cached) return;
|
|
|
|
|
|
|
|
fragment = get_cache_fragment(cached);
|
|
|
|
if (fragment) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *end = fragment->data + fragment->length;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
css->import_level++;
|
|
|
|
css_parse_stylesheet(css, uri, fragment->data, end);
|
|
|
|
css->import_level--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
import_css_file(struct css_stylesheet *css, struct uri *base_uri,
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *url, int urllen)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string string, filename;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!*url
|
|
|
|
|| css->import_level >= MAX_REDIRECTS
|
|
|
|
|| !init_string(&filename))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*url != '/' && elinks_home) {
|
|
|
|
add_to_string(&filename, elinks_home);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_bytes_to_string(&filename, url, urllen);
|
|
|
|
|
2008-08-03 08:24:26 -04:00
|
|
|
if (is_in_state(read_encoded_file(&filename, &string), S_OK)) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *end = string.source + string.length;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
css->import_level++;
|
|
|
|
css_parse_stylesheet(css, base_uri, string.source, end);
|
|
|
|
done_string(&string);
|
|
|
|
css->import_level--;
|
|
|
|
}
|
|
|
|
|
|
|
|
done_string(&filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct css_stylesheet default_stylesheet = INIT_CSS_STYLESHEET(default_stylesheet, import_css_file);
|
|
|
|
|
|
|
|
static void
|
|
|
|
import_default_css(void)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *url = get_opt_str("document.css.stylesheet", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-07-15 13:04:39 -04:00
|
|
|
if (!css_selector_set_empty(&default_stylesheet.selectors))
|
2005-09-15 09:58:31 -04:00
|
|
|
done_css_stylesheet(&default_stylesheet);
|
|
|
|
|
|
|
|
if (!*url) return;
|
|
|
|
|
|
|
|
import_css_file(&default_stylesheet, NULL, url, strlen(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
change_hook_css(struct session *ses, struct option *current, struct option *changed)
|
|
|
|
{
|
2007-12-21 22:57:58 -05:00
|
|
|
int reload_css = 0;
|
|
|
|
|
|
|
|
if (!strcmp(changed->name, "stylesheet")) {
|
2007-07-27 19:31:58 -04:00
|
|
|
/** @todo TODO: We need to update all entries in
|
|
|
|
* format cache. --jonas */
|
2005-09-15 09:58:31 -04:00
|
|
|
import_default_css();
|
|
|
|
}
|
|
|
|
|
2007-12-21 22:57:58 -05:00
|
|
|
if (!strcmp(changed->name, "media"))
|
|
|
|
reload_css = 1;
|
|
|
|
|
2007-01-27 15:33:02 -05:00
|
|
|
/* Instead of using the value of the @ses parameter, iterate
|
|
|
|
* through the @sessions list. The parameter may be NULL and
|
|
|
|
* anyway we don't support session-specific options yet. */
|
2007-12-21 22:57:58 -05:00
|
|
|
foreach (ses, sessions) draw_formatted(ses, 1 + reload_css);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_css(struct module *module)
|
|
|
|
{
|
2007-01-27 12:00:47 -05:00
|
|
|
static const struct change_hook_info css_change_hooks[] = {
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "document.css", change_hook_css },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
register_change_hooks(css_change_hooks);
|
|
|
|
import_default_css();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
done_css(struct module *module)
|
|
|
|
{
|
|
|
|
done_css_stylesheet(&default_stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct module css_module = struct_module(
|
|
|
|
/* name: */ N_("Cascading Style Sheets"),
|
|
|
|
/* options: */ css_options_info,
|
|
|
|
/* hooks: */ NULL,
|
|
|
|
/* submodules: */ NULL,
|
|
|
|
/* data: */ NULL,
|
|
|
|
/* init: */ init_css,
|
|
|
|
/* done: */ done_css
|
|
|
|
);
|