[project @ 2002-05-04 21:17:06 by bursa]

Added code to use style attribute of elements.

svn path=/import/netsurf/; revision=14
This commit is contained in:
James Bursa 2002-05-04 21:17:06 +00:00
parent 50d95fdf6f
commit 91300f840f
7 changed files with 51 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/** /**
* $Id: box.c,v 1.1 2002/05/04 19:57:18 bursa Exp $ * $Id: box.c,v 1.2 2002/05/04 21:17:06 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -60,6 +60,7 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
struct box * inline_container_c; struct box * inline_container_c;
struct css_style * style; struct css_style * style;
xmlNode * c; xmlNode * c;
xmlChar * s;
if (n->type == XML_ELEMENT_NODE) { if (n->type == XML_ELEMENT_NODE) {
/* work out the style for this element */ /* work out the style for this element */
@ -71,6 +72,15 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css
memcpy(style, parent_style, sizeof(struct css_style)); memcpy(style, parent_style, sizeof(struct css_style));
css_get_style(stylesheet, *selector, depth + 1, style); css_get_style(stylesheet, *selector, depth + 1, style);
if (s = xmlGetProp(n, "style")) {
struct css_style * astyle = xcalloc(1, sizeof(struct css_style));
memcpy(astyle, &css_empty_style, sizeof(struct css_style));
css_parse_property_list(astyle, s);
css_cascade(style, astyle);
free(astyle);
free(s);
}
switch (style->display) { switch (style->display) {
case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */ case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */
box = xcalloc(1, sizeof(struct box)); box = xcalloc(1, sizeof(struct box));

View File

@ -1,5 +1,5 @@
/** /**
* $Id: css.c,v 1.1.1.1 2002/04/22 09:24:34 bursa Exp $ * $Id: css.c,v 1.2 2002/05/04 21:17:06 bursa Exp $
*/ */
#include <string.h> #include <string.h>
@ -36,7 +36,6 @@ static void parse_float(struct css_style * const style, const char * const value
static void parse_font_size(struct css_style * const style, const char * const value); static void parse_font_size(struct css_style * const style, const char * const value);
static void parse_height(struct css_style * const style, const char * const value); static void parse_height(struct css_style * const style, const char * const value);
static void parse_width(struct css_style * const style, const char * const value); static void parse_width(struct css_style * const style, const char * const value);
static void parse_property_list(struct css_style * style, char * str);
static void parse_selector(struct css_selector * sel, char * const str); static void parse_selector(struct css_selector * sel, char * const str);
static unsigned int hash_str(const char * str); static unsigned int hash_str(const char * str);
static int seleq(const struct css_selector * const s1, const struct css_selector * const s2); static int seleq(const struct css_selector * const s1, const struct css_selector * const s2);
@ -51,6 +50,23 @@ static void dump_selector(const struct css_selector * const sel);
static void dump_rule(const struct rule * rule); static void dump_rule(const struct rule * rule);
static void css_dump_stylesheet(const struct css_stylesheet * stylesheet); static void css_dump_stylesheet(const struct css_stylesheet * stylesheet);
const struct css_style css_base_style = {
CSS_DISPLAY_BLOCK,
CSS_FLOAT_NONE,
{ CSS_FONT_SIZE_ABSOLUTE, 10.0 },
{ CSS_HEIGHT_AUTO },
{ CSS_WIDTH_AUTO }
};
const struct css_style css_empty_style = {
CSS_DISPLAY_INHERIT,
CSS_FLOAT_INHERIT,
{ CSS_FONT_SIZE_INHERIT },
{ CSS_HEIGHT_AUTO },
{ CSS_WIDTH_AUTO }
};
/** /**
* property parsers * property parsers
*/ */
@ -145,7 +161,7 @@ static struct property {
* parse a property list * parse a property list
*/ */
static void parse_property_list(struct css_style * style, char * str) void css_parse_property_list(struct css_style * style, char * str)
{ {
char * end; char * end;
for (; str != 0; str = end) { for (; str != 0; str = end) {
@ -158,7 +174,7 @@ static void parse_property_list(struct css_style * style, char * str)
*value = 0; value++; *value = 0; value++;
prop = strip(str); prop = strip(str);
value = strip(value); value = strip(value);
printf("css_parse: '%s' => '%s'\n", prop, value); /*printf("css_parse: '%s' => '%s'\n", prop, value);*/
for (i = 0; i < sizeof(property) / sizeof(struct property); i++) { for (i = 0; i < sizeof(property) / sizeof(struct property); i++) {
if (strcmp(prop, property[i].name) == 0) { if (strcmp(prop, property[i].name) == 0) {
@ -310,17 +326,18 @@ static void update_style(struct css_stylesheet * stylesheet, struct css_selector
struct rule * rule = find_rule(stylesheet, selector, selectors); struct rule * rule = find_rule(stylesheet, selector, selectors);
if (rule == 0) { if (rule == 0) {
unsigned int h = hash_str(selector[selectors - 1].element); unsigned int h = hash_str(selector[selectors - 1].element);
printf("update_style: not present - adding\n"); /*printf("update_style: not present - adding\n");*/
rule = xcalloc(1, sizeof(struct rule)); rule = xcalloc(1, sizeof(struct rule));
rule->selector = selector; rule->selector = selector;
rule->selectors = selectors; rule->selectors = selectors;
rule->style = xcalloc(1, sizeof(struct css_style)); rule->style = xcalloc(1, sizeof(struct css_style));
parse_property_list(rule->style, str); memcpy(rule->style, &css_empty_style, sizeof(struct css_style));
css_parse_property_list(rule->style, str);
rule->next = stylesheet->hash[h]; rule->next = stylesheet->hash[h];
stylesheet->hash[h] = rule; stylesheet->hash[h] = rule;
} else { } else {
printf("update_style: already present - updating\n"); /*printf("update_style: already present - updating\n");*/
parse_property_list(rule->style, str); css_parse_property_list(rule->style, str);
free(selector); free(selector);
} }
} }
@ -361,7 +378,7 @@ void css_parse_stylesheet(struct css_stylesheet * stylesheet, char * str)
if (comma != 0) *comma = 0; if (comma != 0) *comma = 0;
sel_str = strip(sels_str); sel_str = strip(sels_str);
printf("css_parse_stylesheet: %s\n", sel_str); /*printf("css_parse_stylesheet: %s\n", sel_str);*/
do { do {
space = strchr(sel_str, ' '); space = strchr(sel_str, ' ');
if (space != 0) *space = 0; if (space != 0) *space = 0;
@ -459,8 +476,8 @@ static void css_dump_stylesheet(const struct css_stylesheet * stylesheet)
void css_cascade(struct css_style * const style, const struct css_style * const apply) void css_cascade(struct css_style * const style, const struct css_style * const apply)
{ {
float f; float f;
style->display = apply->display; if (apply->display != CSS_DISPLAY_INHERIT) style->display = apply->display;
style->float_ = apply->float_; if (apply->float_ != CSS_FLOAT_INHERIT) style->float_ = apply->float_;
style->height = apply->height; style->height = apply->height;
style->width = apply->width; style->width = apply->width;

View File

@ -1,5 +1,5 @@
/** /**
* $Id: css.h,v 1.1.1.1 2002/04/22 09:24:34 bursa Exp $ * $Id: css.h,v 1.2 2002/05/04 21:17:06 bursa Exp $
*/ */
#include "css_enum.h" #include "css_enum.h"
@ -47,24 +47,6 @@ struct css_style {
float percent; float percent;
} value; } value;
} width; } width;
enum { BACKGROUND_SCROLL = 1, BACKGROUND_FIXED } background_attachment;
colour background_color;
/* char background_image[100]; */
/* background-position */
enum { BACKGROUND_REPEAT = 1, BACKGROUND_REPEAT_X,
BACKGROUND_REPEAT_Y, BACKGROUND_NO_REPEAT } background_repeat;
/* borders */
enum { CLEAR_NONE = 1, CLEAR_BOTH, CLEAR_LEFT, CLEAR_RIGHT } clear;
colour color;
/* font-family */
enum { FONT_STRAIGHT, FONT_OBLIQUE, FONT_ITALIC } font_style;
enum { FONT_NORMAL, FONT_SMALLCAPS } font_variant;
struct {
enum { WEIGHT_ABSOLUTE, WEIGHT_BOLDER, WEIGHT_LIGHTER } weight;
unsigned int value;
} font_weight;
}; };
struct css_stylesheet; struct css_stylesheet;
@ -75,6 +57,9 @@ struct css_selector {
char * id; char * id;
}; };
extern const struct css_style css_base_style;
extern const struct css_style css_empty_style;
/** /**
* interface * interface
*/ */
@ -85,3 +70,5 @@ void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * sel
void css_parse_stylesheet(struct css_stylesheet * stylesheet, char * str); void css_parse_stylesheet(struct css_stylesheet * stylesheet, char * str);
void css_dump_style(const struct css_style * const style); void css_dump_style(const struct css_style * const style);
void css_cascade(struct css_style * const style, const struct css_style * const apply); void css_cascade(struct css_style * const style, const struct css_style * const apply);
void css_parse_property_list(struct css_style * style, char * str);

View File

@ -5,8 +5,8 @@ css_background_repeat inherit repeat repeat_x repeat_y no_repeat
css_border_width inherit medium thin thick length css_border_width inherit medium thin thick length
css_border_style inherit none dashed dotted double groove inset outset ridge solid css_border_style inherit none dashed dotted double groove inset outset ridge solid
css_clear none both left right css_clear none both left right
css_display inline block list-item run-in compact marker table inline-table table-row-group table-header-group table-footer-group table-row table-column-group table-column table-cell table-caption none css_display inherit inline block list-item run-in compact marker table inline-table table-row-group table-header-group table-footer-group table-row table-column-group table-column table-cell table-caption none
css_float none left right css_float inherit none left right
css_font_style normal italic oblique css_font_style normal italic oblique
css_font_variant normal smallcaps css_font_variant normal smallcaps
css_font_weight normal bold bolder lighter 100 200 300 400 500 600 700 800 900 css_font_weight normal bold bolder lighter 100 200 300 400 500 600 700 800 900

View File

@ -1,5 +1,5 @@
/** /**
* $Id: render.c,v 1.7 2002/05/04 19:57:18 bursa Exp $ * $Id: render.c,v 1.8 2002/05/04 21:17:06 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -110,6 +110,7 @@ int main(int argc, char *argv[])
stylesheet = css_new_stylesheet(); stylesheet = css_new_stylesheet();
css_parse_stylesheet(stylesheet, load(argv[2])); css_parse_stylesheet(stylesheet, load(argv[2]));
memcpy(style, &css_base_style, sizeof(struct css_style));
doc_box->type = BOX_BLOCK; doc_box->type = BOX_BLOCK;
doc_box->node = c; doc_box->node = c;
xml_to_box(c, style, stylesheet, &selector, 0, doc_box, 0); xml_to_box(c, style, stylesheet, &selector, 0, doc_box, 0);

View File

@ -22,5 +22,3 @@ caption { display: table-caption }
div { float: left; width: 90%; } div { float: left; width: 90%; }
td { width: 40%; }

View File

@ -5,11 +5,11 @@
<body> <body>
<h1>Heading</h1> <h1 style="width: 80%">Heading</h1>
<p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque <b>habitant <i>Morbi</i></b> tristique Senectus et Metus et malesuada Fames ac turpis Egestas. (This paragraph has no closing tag.)<p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; <b>Pellentesque</b> habitant Morbi tristique Senectus et Metus et malesuada Fames ac turpis Egestas.</p> <p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque <b>habitant <i>Morbi</i></b> tristique Senectus et Metus et malesuada Fames ac turpis Egestas. (This paragraph has no closing tag.)<p>Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; <b>Pellentesque</b> habitant Morbi tristique Senectus et Metus et malesuada Fames ac turpis Egestas.</p>
<table><tr><td>Cell 1</td><td>Cell 2<p>More cell 2</p></td></tr> <table><tr><td style="width: 30%; height: 1em">Cell 1</td><td style="width: 50%;">Cell 2<p>More cell 2</p></td></tr>
<tr><td>Cell 3</td><td>Cell 4</td></tr></table> <tr><td>Cell 3</td><td>Cell 4</td></tr></table>
<p>Inline text <div>containing a block</div> and more text.</p> <p>Inline text <div>containing a block</div> and more text.</p>