1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-29 03:17:53 -04:00

document/dom: Move styles initialization out of init_dom_renderer()

Now, CSS is initialized separately for each of the renderers, so that
also RSS doesn't just choose styles of random DOM node types.

init_template_by_style() is introduced as the common backend for
loading CSS properties.
This commit is contained in:
Petr Baudis 2007-08-28 19:38:13 +02:00 committed by Petr Baudis
parent 494e4a1019
commit afb9b6daa5
5 changed files with 166 additions and 91 deletions

View File

@ -13,10 +13,6 @@
#include "elinks.h"
#include "cache/cache.h"
#include "document/css/css.h"
#include "document/css/parser.h"
#include "document/css/property.h"
#include "document/css/stylesheet.h"
#include "document/document.h"
#include "document/dom/renderer.h"
#include "document/dom/rss.h"
@ -32,7 +28,6 @@
#include "dom/stack.h"
#include "intl/charsets.h"
#include "protocol/uri.h"
#include "terminal/draw.h"
#include "util/error.h"
#include "util/memory.h"
#include "util/string.h"
@ -42,14 +37,10 @@
#define URL_REGFLAGS (REG_ICASE | REG_EXTENDED)
/* Checks the user CSS for properties for each DOM node type name */
static inline void
init_dom_renderer(struct dom_renderer *renderer, struct document *document,
struct string *buffer, struct conv_table *convert_table)
{
enum dom_node_type type;
struct css_stylesheet *css = &default_stylesheet;
memset(renderer, 0, sizeof(*renderer));
renderer->document = document;
@ -69,81 +60,6 @@ init_dom_renderer(struct dom_renderer *renderer, struct document *document,
}
}
#endif
for (type = 0; type < DOM_NODES; type++) {
struct screen_char *template = &renderer->styles[type];
color_T background = document->options.default_bg;
color_T foreground = document->options.default_fg;
enum screen_char_attr attr = 0;
static int i_want_struct_module_for_dom;
struct dom_string *name = get_dom_node_type_name(type);
struct css_selector *selector = NULL;
if (!i_want_struct_module_for_dom) {
static const unsigned char default_colors[] =
"document { color: yellow } "
"element { color: lightgreen } "
"entity-reference { color: red } "
"proc-instruction { color: red } "
"attribute { color: magenta } "
"comment { color: aqua } "
"cdata-section { color: orange2 } ";
unsigned char *styles = (unsigned char *) default_colors;
i_want_struct_module_for_dom = 1;
/* When someone will get here earlier than at 4am,
* this will be done in some init function, perhaps
* not overriding the user's default stylesheet. */
css_parse_stylesheet(css, NULL, styles, styles + sizeof(default_colors));
}
if (name)
if (is_dom_string_set(name))
selector = find_css_selector(&css->selectors,
CST_ELEMENT, CSR_ROOT,
name->string, name->length);
if (selector) {
struct css_property *property;
foreach (property, selector->properties) {
switch (property->type) {
case CSS_PT_BACKGROUND_COLOR:
case CSS_PT_BACKGROUND:
if (property->value_type == CSS_VT_COLOR)
background = property->value.color;
break;
case CSS_PT_COLOR:
foreground = property->value.color;
break;
case CSS_PT_FONT_WEIGHT:
if (property->value.font_attribute.add & AT_BOLD)
attr |= SCREEN_ATTR_BOLD;
break;
case CSS_PT_FONT_STYLE:
if (property->value.font_attribute.add & AT_UNDERLINE)
attr |= SCREEN_ATTR_UNDERLINE;
if (property->value.font_attribute.add & AT_ITALIC)
attr |= SCREEN_ATTR_ITALIC;
break;
case CSS_PT_TEXT_DECORATION:
if (property->value.font_attribute.add & AT_UNDERLINE)
attr |= SCREEN_ATTR_UNDERLINE;
break;
case CSS_PT_DISPLAY:
case CSS_PT_NONE:
case CSS_PT_TEXT_ALIGN:
case CSS_PT_WHITE_SPACE:
case CSS_PT_LAST:
break;
}
}
}
init_template(template, &document->options, background, foreground, attr);
}
}
static inline void

View File

@ -6,6 +6,9 @@
#include "elinks.h"
#include "document/css/css.h"
#include "document/css/parser.h"
#include "document/css/stylesheet.h"
#include "document/document.h"
#include "document/dom/util.h"
#include "document/dom/rss.h"
@ -17,7 +20,11 @@
#include "util/memory.h"
/* DOM RSS Renderer */
enum rss_style {
RSS_STYLE_TITLE,
RSS_STYLE_AUX,
RSS_STYLES,
};
static enum dom_code
@ -142,7 +149,7 @@ render_rss_item(struct dom_renderer *renderer, struct dom_node *item)
if (str)
renderer->document->title = str;
}
render_dom_text(renderer, &renderer->styles[DOM_NODE_ELEMENT],
render_dom_text(renderer, &renderer->styles[RSS_STYLE_TITLE],
title->string, title->length);
}
@ -156,17 +163,17 @@ render_rss_item(struct dom_renderer *renderer, struct dom_node *item)
X(renderer) = 0;
if (author && is_dom_string_set(author)) {
render_dom_text(renderer, &renderer->styles[DOM_NODE_COMMENT],
render_dom_text(renderer, &renderer->styles[RSS_STYLE_AUX],
author->string, author->length);
}
if (date && is_dom_string_set(date)) {
if (author && is_dom_string_set(author)) {
render_dom_text(renderer, &renderer->styles[DOM_NODE_COMMENT],
render_dom_text(renderer, &renderer->styles[RSS_STYLE_AUX],
" - ", 3);
}
render_dom_text(renderer, &renderer->styles[DOM_NODE_COMMENT],
render_dom_text(renderer, &renderer->styles[RSS_STYLE_AUX],
date->string, date->length);
}
@ -178,6 +185,50 @@ render_rss_item(struct dom_renderer *renderer, struct dom_node *item)
}
}
static enum dom_code
dom_rss_push_document(struct dom_stack *stack, struct dom_node *root, void *data)
{
struct dom_renderer *renderer = stack->current->data;
struct css_stylesheet *css = &default_stylesheet;
struct document *document = renderer->document;
enum rss_style type;
/* Initialize styles. */
for (type = 0; type < RSS_STYLES; type++) {
struct screen_char *template = &renderer->styles[type];
color_T background = document->options.default_bg;
color_T foreground = document->options.default_fg;
enum screen_char_attr attr = 0;
static int i_want_struct_module_for_dom;
static unsigned char *names[RSS_STYLES] = { "title", "aux" };
struct css_selector *selector = NULL;
if (!i_want_struct_module_for_dom) {
static const unsigned char default_colors[] =
"title { color: lightgreen } "
"aux { color: aquA} // author, title ";
unsigned char *styles = (unsigned char *) default_colors;
i_want_struct_module_for_dom = 1;
/* When someone will get here earlier than at 4am,
* this will be done in some init function, perhaps
* not overriding the user's default stylesheet. */
css_parse_stylesheet(css, NULL, styles, styles + sizeof(default_colors));
}
selector = find_css_selector(&css->selectors,
CST_ELEMENT, CSR_ROOT,
names[type], strlen(names[type]));
init_template_by_style(template, &document->options, background, foreground, attr,
selector ? &selector->properties : NULL);
}
return DOM_CODE_OK;
}
static enum dom_code
dom_rss_pop_document(struct dom_stack *stack, struct dom_node *root, void *data)
{
@ -222,7 +273,7 @@ struct dom_stack_context_info dom_rss_renderer_context_info = {
/* DOM_NODE_ENTITY */ NULL,
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
/* DOM_NODE_COMMENT */ NULL,
/* DOM_NODE_DOCUMENT */ NULL,
/* DOM_NODE_DOCUMENT */ dom_rss_push_document,
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
/* DOM_NODE_NOTATION */ NULL,

View File

@ -303,6 +303,57 @@ render_dom_cdata_source(struct dom_stack *stack, struct dom_node *node, void *da
return DOM_CODE_OK;
}
static enum dom_code
render_dom_document_start(struct dom_stack *stack, struct dom_node *node, void *data)
{
struct dom_renderer *renderer = stack->current->data;
struct css_stylesheet *css = &default_stylesheet;
struct document *document = renderer->document;
enum dom_node_type type;
/* Initialize styles for all the DOM node types. */
for (type = 0; type < DOM_NODES; type++) {
struct screen_char *template = &renderer->styles[type];
color_T background = document->options.default_bg;
color_T foreground = document->options.default_fg;
enum screen_char_attr attr = 0;
static int i_want_struct_module_for_dom;
struct dom_string *name = get_dom_node_type_name(type);
struct css_selector *selector = NULL;
if (!i_want_struct_module_for_dom) {
static const unsigned char default_colors[] =
"document { color: yellow } "
"element { color: lightgreen } "
"entity-reference { color: red } "
"proc-instruction { color: red } "
"attribute { color: magenta } "
"comment { color: aqua } "
"cdata-section { color: orange2 } ";
unsigned char *styles = (unsigned char *) default_colors;
i_want_struct_module_for_dom = 1;
/* When someone will get here earlier than at 4am,
* this will be done in some init function, perhaps
* not overriding the user's default stylesheet. */
css_parse_stylesheet(css, NULL, styles, styles + sizeof(default_colors));
}
if (name)
if (is_dom_string_set(name))
selector = find_css_selector(&css->selectors,
CST_ELEMENT, CSR_ROOT,
name->string, name->length);
init_template_by_style(template, &document->options, background, foreground, attr,
selector ? &selector->properties : NULL);
}
return DOM_CODE_OK;
}
static enum dom_code
render_dom_document_end(struct dom_stack *stack, struct dom_node *node, void *data)
{
@ -318,6 +369,7 @@ render_dom_document_end(struct dom_stack *stack, struct dom_node *node, void *da
return DOM_CODE_OK;
}
struct dom_stack_context_info dom_source_renderer_context_info = {
/* Object size: */ 0,
/* Push: */
@ -331,7 +383,7 @@ struct dom_stack_context_info dom_source_renderer_context_info = {
/* DOM_NODE_ENTITY */ render_dom_node_source,
/* DOM_NODE_PROC_INSTRUCTION */ render_dom_element_source,
/* DOM_NODE_COMMENT */ render_dom_node_source,
/* DOM_NODE_DOCUMENT */ NULL,
/* DOM_NODE_DOCUMENT */ render_dom_document_start,
/* DOM_NODE_DOCUMENT_TYPE */ render_dom_node_source,
/* DOM_NODE_DOCUMENT_FRAGMENT */ render_dom_node_source,
/* DOM_NODE_NOTATION */ render_dom_node_source,

View File

@ -13,6 +13,7 @@
#include "elinks.h"
#include "bookmarks/bookmarks.h" /* get_bookmark() */
#include "document/css/property.h"
#include "document/docdata.h"
#include "document/document.h"
#include "document/dom/util.h"
@ -38,6 +39,53 @@ init_template(struct screen_char *template, struct document_options *options,
}
inline void
init_template_by_style(struct screen_char *template, struct document_options *options,
color_T background, color_T foreground, enum screen_char_attr attr,
LIST_OF(struct css_property) *properties)
{
struct css_property *property;
if (properties) {
foreach (property, *properties) {
switch (property->type) {
case CSS_PT_BACKGROUND_COLOR:
case CSS_PT_BACKGROUND:
if (property->value_type == CSS_VT_COLOR)
background = property->value.color;
break;
case CSS_PT_COLOR:
foreground = property->value.color;
break;
case CSS_PT_FONT_WEIGHT:
if (property->value.font_attribute.add & AT_BOLD)
attr |= SCREEN_ATTR_BOLD;
break;
case CSS_PT_FONT_STYLE:
if (property->value.font_attribute.add & AT_UNDERLINE)
attr |= SCREEN_ATTR_UNDERLINE;
if (property->value.font_attribute.add & AT_ITALIC)
attr |= SCREEN_ATTR_ITALIC;
break;
case CSS_PT_TEXT_DECORATION:
if (property->value.font_attribute.add & AT_UNDERLINE)
attr |= SCREEN_ATTR_UNDERLINE;
break;
case CSS_PT_DISPLAY:
case CSS_PT_NONE:
case CSS_PT_TEXT_ALIGN:
case CSS_PT_WHITE_SPACE:
case CSS_PT_LAST:
break;
}
}
}
init_template(template, options, background, foreground, attr);
}
static struct screen_char *
realloc_line(struct document *document, int x, int y)
{

View File

@ -38,6 +38,8 @@ struct dom_renderer {
regex_t url_regex;
unsigned int find_url:1;
#endif
/* DOM_NODES is just an arbitrary size to fit everything for all the
* renderers. */
struct screen_char styles[DOM_NODES];
/* RSS renderer variables */
@ -56,6 +58,12 @@ void init_template(struct screen_char *template,
struct document_options *options,
color_T background, color_T foreground,
enum screen_char_attr attr);
void init_template_by_style(struct screen_char *template,
struct document_options *options,
color_T background, color_T foreground,
enum screen_char_attr attr,
LIST_OF(struct css_property) *properties);
void render_dom_text(struct dom_renderer *renderer, struct screen_char *template,
unsigned char *string, int length);
struct link *add_dom_link(struct dom_renderer *renderer, unsigned char *string,