2007-07-27 19:31:58 -04:00
|
|
|
/** CSS style applier
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @todo TODO: A way to disable CSS completely, PLUS a way to stop
|
|
|
|
* various property groups from taking effect. (Ie. way to turn out
|
|
|
|
* effect of 'display: none' or aligning or colors but keeping all the
|
|
|
|
* others.) --pasky */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "document/css/apply.h"
|
|
|
|
#include "document/css/css.h"
|
|
|
|
#include "document/css/parser.h"
|
|
|
|
#include "document/css/property.h"
|
|
|
|
#include "document/css/scanner.h"
|
|
|
|
#include "document/css/stylesheet.h"
|
2007-08-28 15:05:00 -04:00
|
|
|
#include "document/format.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "document/html/parser/parse.h"
|
|
|
|
#include "document/options.h"
|
|
|
|
#include "util/align.h"
|
|
|
|
#include "util/color.h"
|
|
|
|
#include "util/lists.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
/* XXX: Some strange dependency makes it necessary to this include last. */
|
|
|
|
#include "document/html/internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef void (*css_applier_T)(struct html_context *html_context,
|
|
|
|
struct html_element *element,
|
|
|
|
struct css_property *prop);
|
|
|
|
|
|
|
|
static void
|
|
|
|
css_apply_color(struct html_context *html_context, struct html_element *element,
|
|
|
|
struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_COLOR);
|
|
|
|
|
|
|
|
if (use_document_fg_colors(html_context->options))
|
2007-10-12 10:41:06 -04:00
|
|
|
element->attr.style.color.foreground = prop->value.color;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
css_apply_background_color(struct html_context *html_context,
|
|
|
|
struct html_element *element,
|
|
|
|
struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_COLOR);
|
|
|
|
|
|
|
|
if (use_document_bg_colors(html_context->options))
|
2007-10-12 10:41:06 -04:00
|
|
|
element->attr.style.color.background = prop->value.color;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2005-09-22 16:07:33 -04:00
|
|
|
static void
|
|
|
|
css_apply_display(struct html_context *html_context, struct html_element *element,
|
|
|
|
struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_DISPLAY);
|
|
|
|
|
|
|
|
switch (prop->value.display) {
|
|
|
|
case CSS_DISP_INLINE:
|
|
|
|
element->linebreak = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISP_BLOCK:
|
|
|
|
/* 1 or 2, that is the question. I went for 2 since it
|
|
|
|
* gives a more "blocky" feel and it's more common.
|
|
|
|
* YMMV. */
|
|
|
|
element->linebreak = 2;
|
|
|
|
break;
|
2007-12-21 22:20:55 -05:00
|
|
|
case CSS_DISP_NONE:
|
2008-05-22 22:44:11 -04:00
|
|
|
if (!html_context->options->css_ignore_display_none)
|
2008-05-22 22:51:30 -04:00
|
|
|
element->invisible = 2;
|
2007-12-21 22:20:55 -05:00
|
|
|
break;
|
2005-09-22 16:07:33 -04:00
|
|
|
default:
|
|
|
|
INTERNAL("Bad prop->value.display %d", prop->value.display);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
|
|
|
css_apply_font_attribute(struct html_context *html_context,
|
|
|
|
struct html_element *element, struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_FONT_ATTRIBUTE);
|
|
|
|
element->attr.style.attr |= prop->value.font_attribute.add;
|
|
|
|
element->attr.style.attr &= ~prop->value.font_attribute.rem;
|
|
|
|
}
|
|
|
|
|
2011-10-27 17:15:53 -04:00
|
|
|
static void
|
|
|
|
css_apply_list_style(struct html_context *html_context,
|
|
|
|
struct html_element *element, struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_LIST_STYLE);
|
|
|
|
|
|
|
|
element->parattr.list_number = (prop->value.list_style > CSS_LIST_ORDINAL);
|
|
|
|
switch (prop->value.list_style) {
|
|
|
|
case CSS_LIST_NONE: element->parattr.flags = P_NO_BULLET; break;
|
2013-04-02 14:51:55 -04:00
|
|
|
case CSS_LIST_DISC: element->parattr.flags = P_DISC; break;
|
2011-10-27 17:15:53 -04:00
|
|
|
case CSS_LIST_CIRCLE: element->parattr.flags = P_O; break;
|
2013-04-02 14:51:55 -04:00
|
|
|
case CSS_LIST_SQUARE: element->parattr.flags = P_SQUARE; break;
|
2011-10-27 17:15:53 -04:00
|
|
|
case CSS_LIST_DECIMAL: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_DECIMAL_LEADING_ZERO: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_LOWER_ROMAN: element->parattr.flags = P_roman; break;
|
|
|
|
case CSS_LIST_UPPER_ROMAN: element->parattr.flags = P_ROMAN; break;
|
|
|
|
case CSS_LIST_LOWER_ALPHA: element->parattr.flags = P_alpha; break;
|
|
|
|
case CSS_LIST_UPPER_ALPHA: element->parattr.flags = P_ALPHA; break;
|
|
|
|
case CSS_LIST_LOWER_GREEK: element->parattr.flags = P_roman; break;
|
|
|
|
case CSS_LIST_LOWER_LATIN: element->parattr.flags = P_alpha; break;
|
|
|
|
case CSS_LIST_UPPER_LATIN: element->parattr.flags = P_ALPHA; break;
|
|
|
|
case CSS_LIST_HEBREW: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_ARMENIAN: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_GEORGIAN: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_CJK_IDEOGRAPHIC: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_HIRAGANA: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_KATAKANA: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_HIRAGANA_IROHA: element->parattr.flags = P_NUMBER; break;
|
|
|
|
case CSS_LIST_KATAKANA_IROHA: element->parattr.flags = P_NUMBER; break;
|
2011-11-16 11:58:08 -05:00
|
|
|
case CSS_LIST_ORDINAL: assert(0); break;
|
2011-10-27 17:15:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-27 19:31:58 -04:00
|
|
|
/** @bug FIXME: Because the current CSS doesn't provide reasonable
|
|
|
|
* defaults for each HTML element this applier will cause bad
|
|
|
|
* rendering of @<pre> tags. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
|
|
|
css_apply_text_align(struct html_context *html_context,
|
|
|
|
struct html_element *element, struct css_property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->value_type == CSS_VT_TEXT_ALIGN);
|
|
|
|
element->parattr.align = prop->value.text_align;
|
|
|
|
}
|
|
|
|
|
2007-07-27 19:31:58 -04:00
|
|
|
/*! XXX: Sort like the css_property_type */
|
2007-07-27 19:38:11 -04:00
|
|
|
static const css_applier_T css_appliers[CSS_PT_LAST] = {
|
2005-09-15 09:58:31 -04:00
|
|
|
/* CSS_PT_NONE */ NULL,
|
|
|
|
/* CSS_PT_BACKGROUND */ css_apply_background_color,
|
|
|
|
/* CSS_PT_BACKGROUND_COLOR */ css_apply_background_color,
|
|
|
|
/* CSS_PT_COLOR */ css_apply_color,
|
2005-09-22 16:07:33 -04:00
|
|
|
/* CSS_PT_DISPLAY */ css_apply_display,
|
2005-09-15 09:58:31 -04:00
|
|
|
/* CSS_PT_FONT_STYLE */ css_apply_font_attribute,
|
|
|
|
/* CSS_PT_FONT_WEIGHT */ css_apply_font_attribute,
|
2011-10-27 17:15:53 -04:00
|
|
|
/* CSS_PT_LIST_STYLE */ css_apply_list_style,
|
2013-04-02 14:51:56 -04:00
|
|
|
/* CSS_PT_LIST_STYLE_TYPE */ css_apply_list_style,
|
2005-09-15 09:58:31 -04:00
|
|
|
/* CSS_PT_TEXT_ALIGN */ css_apply_text_align,
|
|
|
|
/* CSS_PT_TEXT_DECORATION */ css_apply_font_attribute,
|
|
|
|
/* CSS_PT_WHITE_SPACE */ css_apply_font_attribute,
|
|
|
|
};
|
|
|
|
|
2007-07-27 19:31:58 -04:00
|
|
|
/** This looks for a match in list of selectors. */
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
|
|
|
examine_element(struct html_context *html_context, struct css_selector *base,
|
2022-01-28 09:30:59 -05:00
|
|
|
css_selector_type_T seltype, enum css_selector_relation rel,
|
2007-07-27 20:22:29 -04:00
|
|
|
struct css_selector_set *selectors,
|
2007-07-26 15:39:08 -04:00
|
|
|
struct html_element *element)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
#ifdef DEBUG_CSS
|
2007-07-15 08:52:44 -04:00
|
|
|
/* Cannot use list_empty() inside the arglist of DBG() because
|
|
|
|
* GCC 4.1 "warning: operation on `errfile' may be undefined"
|
|
|
|
* breaks the build with -Werror. */
|
|
|
|
int dbg_has_leaves, dbg_has_properties;
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
DBG("examine_element(%p, %s, %d, %d, %p, %.*s);", html_context, base->name, seltype, rel, selectors, element->namelen, element->name);
|
|
|
|
#define dbginfo(sel, type_, base) \
|
2007-07-15 13:04:39 -04:00
|
|
|
dbg_has_leaves = !css_selector_set_empty(&sel->leaves), \
|
2007-07-15 08:52:44 -04:00
|
|
|
dbg_has_properties = !list_empty(sel->properties), \
|
|
|
|
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, dbg_has_leaves, dbg_has_properties)
|
2005-09-15 09:58:31 -04:00
|
|
|
#else
|
|
|
|
#define dbginfo(sel, type, base)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define process_found_selector(sel, type, base) \
|
|
|
|
if (selector) { \
|
|
|
|
dbginfo(sel, type, base); \
|
|
|
|
merge_css_selectors(base, sel); \
|
|
|
|
/* Ancestor matches? */ \
|
2007-07-15 18:42:15 -04:00
|
|
|
if (sel->leaves.may_contain_rel_ancestor_or_parent \
|
2007-07-27 20:22:29 -04:00
|
|
|
&& (LIST_OF(struct html_element) *) element->next \
|
2006-07-21 07:10:54 -04:00
|
|
|
!= &html_context->stack) { \
|
2005-09-15 09:58:31 -04:00
|
|
|
struct html_element *ancestor; \
|
|
|
|
/* This is less effective than doing reverse iterations,
|
|
|
|
* first over sel->leaves and then over the HTML stack,
|
|
|
|
* which shines in the most common case where there are
|
|
|
|
* no CSR_ANCESTOR selector leaves. However we would
|
|
|
|
* have to duplicate the whole examine_element(), so if
|
|
|
|
* profiles won't show it really costs... */ \
|
|
|
|
for (ancestor = element->next; \
|
2007-07-26 15:39:08 -04:00
|
|
|
(LIST_OF(struct html_element) *) ancestor \
|
2006-07-21 07:10:54 -04:00
|
|
|
!= &html_context->stack;\
|
2005-09-15 09:58:31 -04:00
|
|
|
ancestor = ancestor->next) \
|
|
|
|
examine_element(html_context, base, \
|
|
|
|
CST_ELEMENT, CSR_ANCESTOR, \
|
2006-07-21 07:10:54 -04:00
|
|
|
&sel->leaves, ancestor); \
|
2005-09-15 09:58:31 -04:00
|
|
|
examine_element(html_context, base, \
|
|
|
|
CST_ELEMENT, CSR_PARENT, \
|
2006-07-21 07:10:54 -04:00
|
|
|
&sel->leaves, element->next); \
|
2005-09-15 09:58:31 -04:00
|
|
|
} \
|
|
|
|
/* More specific matches? */ \
|
|
|
|
examine_element(html_context, base, type + 1, \
|
|
|
|
CSR_SPECIFITY, \
|
2006-07-21 07:10:54 -04:00
|
|
|
&sel->leaves, element); \
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (seltype <= CST_ELEMENT && element->namelen) {
|
|
|
|
selector = find_css_selector(selectors, CST_ELEMENT, rel,
|
|
|
|
"*", 1);
|
|
|
|
process_found_selector(selector, CST_ELEMENT, base);
|
|
|
|
|
|
|
|
selector = find_css_selector(selectors, CST_ELEMENT, rel,
|
|
|
|
element->name, element->namelen);
|
|
|
|
process_found_selector(selector, CST_ELEMENT, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element->options)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* TODO: More pseudo-classess. --pasky */
|
|
|
|
if (element->pseudo_class & ELEMENT_LINK) {
|
|
|
|
selector = find_css_selector(selectors, CST_PSEUDO, rel, "link", -1);
|
|
|
|
process_found_selector(selector, CST_PSEUDO, base);
|
|
|
|
}
|
|
|
|
if (element->pseudo_class & ELEMENT_VISITED) {
|
|
|
|
selector = find_css_selector(selectors, CST_PSEUDO, rel, "visited", -1);
|
|
|
|
process_found_selector(selector, CST_PSEUDO, base);
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:18:27 -04:00
|
|
|
if (element->attr.class_ && seltype <= CST_CLASS) {
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *class_ = element->attr.class_;
|
2006-06-06 09:21:00 -04:00
|
|
|
|
2007-07-15 15:19:30 -04:00
|
|
|
for (;;) {
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *begin;
|
2006-06-06 09:21:00 -04:00
|
|
|
|
2016-04-20 12:18:27 -04:00
|
|
|
while (*class_ == ' ') ++class_;
|
|
|
|
if (*class_ == '\0') break;
|
|
|
|
begin = class_;
|
|
|
|
while (*class_ != ' ' && *class_ != '\0') ++class_;
|
2006-06-06 09:21:00 -04:00
|
|
|
|
2007-07-15 15:19:30 -04:00
|
|
|
selector = find_css_selector(selectors, CST_CLASS, rel,
|
2016-04-20 12:18:27 -04:00
|
|
|
begin, class_ - begin);
|
2006-06-06 09:21:00 -04:00
|
|
|
process_found_selector(selector, CST_CLASS, base);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2007-07-15 15:19:30 -04:00
|
|
|
if (element->attr.id && seltype <= CST_ID) {
|
|
|
|
selector = find_css_selector(selectors, CST_ID, rel, element->attr.id, -1);
|
2005-09-15 09:58:31 -04:00
|
|
|
process_found_selector(selector, CST_ID, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef process_found_selector
|
|
|
|
#undef dbginfo
|
|
|
|
}
|
|
|
|
|
|
|
|
struct css_selector *
|
|
|
|
get_css_selector_for_element(struct html_context *html_context,
|
|
|
|
struct html_element *element,
|
|
|
|
struct css_stylesheet *css,
|
2007-07-26 15:39:08 -04:00
|
|
|
LIST_OF(struct html_element) *html_stack)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *code;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
assert(element && element->options && css);
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
selector = init_css_selector(NULL, CST_ELEMENT, CSR_ROOT, NULL, 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!selector)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#ifdef DEBUG_CSS
|
|
|
|
DBG("Applying to element %.*s...", element->namelen, element->name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
examine_element(html_context, selector, CST_ELEMENT, CSR_ROOT,
|
2006-07-21 07:10:54 -04:00
|
|
|
&css->selectors, element);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_CSS
|
|
|
|
DBG("Element %.*s applied.", element->namelen, element->name);
|
|
|
|
#endif
|
|
|
|
|
2007-04-29 17:56:39 -04:00
|
|
|
code = get_attr_val(element->options, "style", html_context->doc_cp);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (code) {
|
|
|
|
struct css_selector *stylesel;
|
|
|
|
struct scanner scanner;
|
|
|
|
|
2007-07-15 14:53:35 -04:00
|
|
|
stylesel = init_css_selector(NULL, CST_ELEMENT, CSR_ROOT, NULL, 0);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (stylesel) {
|
|
|
|
init_scanner(&scanner, &css_scanner_info, code, NULL);
|
|
|
|
css_parse_properties(&stylesel->properties, &scanner);
|
|
|
|
merge_css_selectors(selector, stylesel);
|
|
|
|
done_css_selector(stylesel);
|
|
|
|
}
|
|
|
|
mem_free(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
apply_css_selector_style(struct html_context *html_context,
|
|
|
|
struct html_element *element,
|
|
|
|
struct css_selector *selector)
|
|
|
|
{
|
|
|
|
struct css_property *property;
|
|
|
|
|
|
|
|
foreach (property, selector->properties) {
|
|
|
|
assert(property->type < CSS_PT_LAST);
|
|
|
|
/* We don't assert general prop->value_type here because I
|
|
|
|
* don't want hinder properties' ability to potentially make
|
|
|
|
* use of multiple value types. */
|
|
|
|
assert(css_appliers[property->type]);
|
|
|
|
css_appliers[property->type](html_context, element, property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
css_apply(struct html_context *html_context, struct html_element *element,
|
2007-07-26 15:39:08 -04:00
|
|
|
struct css_stylesheet *css, LIST_OF(struct html_element) *html_stack)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct css_selector *selector;
|
|
|
|
|
|
|
|
selector = get_css_selector_for_element(html_context, element, css,
|
|
|
|
html_stack);
|
|
|
|
if (!selector) return;
|
|
|
|
|
|
|
|
apply_css_selector_style(html_context, element, selector);
|
|
|
|
|
|
|
|
done_css_selector(selector);
|
|
|
|
}
|