From 2253e38be8cbcf4243e43fc0d317cfda08bffeb2 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sat, 5 Apr 2003 21:38:06 +0000 Subject: [PATCH] [project @ 2003-04-05 21:38:06 by bursa] External stylesheets. svn path=/import/netsurf/; revision=115 --- content/content.c | 4 +- content/content.h | 5 +- css/css.c | 170 +++++++++++++++++++++++++++++----------------- css/css.h | 5 +- css/ruleset.c | 10 +-- desktop/browser.c | 55 +-------------- render/box.c | 49 ++++++++----- render/box.h | 5 +- render/html.c | 133 ++++++++++++++++++++++++++++-------- utils/utils.c | 58 +++++++++++++++- utils/utils.h | 3 +- 11 files changed, 324 insertions(+), 173 deletions(-) diff --git a/content/content.c b/content/content.c index 5812b9e5f..be528d787 100644 --- a/content/content.c +++ b/content/content.c @@ -1,5 +1,5 @@ /** - * $Id: content.c,v 1.4 2003/04/04 15:19:31 bursa Exp $ + * $Id: content.c,v 1.5 2003/04/05 21:38:06 bursa Exp $ */ #include @@ -41,7 +41,7 @@ static const struct handler_entry handler_map[] = { {textplain_create, textplain_process_data, textplain_convert, textplain_revive, textplain_reformat, textplain_destroy}, {jpeg_create, jpeg_process_data, jpeg_convert, jpeg_revive, jpeg_reformat, jpeg_destroy}, - {css_create, css_process_data, css_convert, css_revive, css_destroy}, + {css_create, css_process_data, css_convert, css_revive, css_reformat, css_destroy}, /* {png_create, png_process_data, png_convert, png_revive, png_destroy},*/ }; diff --git a/content/content.h b/content/content.h index 06210b790..b0711488d 100644 --- a/content/content.h +++ b/content/content.h @@ -1,5 +1,5 @@ /** - * $Id: content.h,v 1.3 2003/04/04 15:19:31 bursa Exp $ + * $Id: content.h,v 1.4 2003/04/05 21:38:06 bursa Exp $ */ #ifndef _NETSURF_DESKTOP_CONTENT_H_ @@ -54,6 +54,9 @@ struct content xmlDoc* document; xmlNode* markup; struct box* layout; + unsigned int stylesheet_count; + char **stylesheet_url; + struct content **stylesheet_content; struct css_stylesheet* stylesheet; struct css_style* style; struct { diff --git a/css/css.c b/css/css.c index 117292d93..b48d4a684 100644 --- a/css/css.c +++ b/css/css.c @@ -1,5 +1,5 @@ /** - * $Id: css.c,v 1.3 2003/04/05 16:24:43 bursa Exp $ + * $Id: css.c,v 1.4 2003/04/05 21:38:06 bursa Exp $ */ #include @@ -7,6 +7,7 @@ #include #include #define CSS_INTERNALS +#define NDEBUG #include "netsurf/content/content.h" #include "netsurf/css/css.h" #include "netsurf/css/parser.h" @@ -173,17 +174,24 @@ void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * sel unsigned int selectors, struct css_style * style) { struct node *r, *n, *m; - unsigned int hash, i; + unsigned int hash, i, done_empty = 0; LOG(("stylesheet %p, selectors %u", stylesheet, selectors)); hash = css_hash(selector[selectors - 1].element); - for (r = stylesheet->rule[hash]; r != 0; r = r->next) { + for (r = stylesheet->rule[hash]; ; r = r->next) { + if (r == 0 && !done_empty) { + r = stylesheet->rule[0]; + done_empty = 1; + } + if (r == 0) + return; i = selectors - 1; n = r; /* compare element */ - if (strcasecmp(selector[i].element, n->data) != 0) - goto not_matched; + if (n->data != 0) + if (strcasecmp(selector[i].element, n->data) != 0) + goto not_matched; LOG(("top element '%s' matched", selector[i].element)); while (1) { /* class and id */ @@ -194,6 +202,7 @@ void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * sel goto not_matched; } else if (m->type == NODE_CLASS) { /* TODO: check if case sensitive */ + LOG(("comparing class '%s' against '%s'", selector[i].class, m->data)); if (strcmp(selector[i].class, m->data) != 0) goto not_matched; } else { @@ -208,6 +217,8 @@ void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * sel /* search for ancestor */ assert(n->right != 0); n = n->right; + if (n->data == 0) + goto not_matched; /* TODO: handle this case */ LOG(("searching for ancestor '%s'", n->data)); while (i != 0 && strcasecmp(selector[i - 1].element, n->data) != 0) i--; @@ -224,7 +235,7 @@ void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * sel matched: /* TODO: sort by specificity */ LOG(("matched rule %p", r)); - css_cascade(style, r->style); + css_merge(style, r->style); not_matched: @@ -268,81 +279,84 @@ void css_parse_property_list(struct css_style * style, char * str) * dump a style */ -#define DUMP_LENGTH(pre, len, post) LOG((pre "%g%s" post, (len)->value, css_unit_name[(len)->unit])); +static void dump_length(const struct css_length * const length) +{ + fprintf(stderr, "%g%s", length->value, + css_unit_name[length->unit]); +} void css_dump_style(const struct css_style * const style) { - LOG(("{ ")); - LOG(("background-color: #%lx;", style->background_color)); - LOG(("clear: %s;", css_clear_name[style->clear])); - LOG(("color: #%lx;", style->color)); - LOG(("display: %s;", css_display_name[style->display])); - LOG(("float: %s;", css_float_name[style->float_])); + fprintf(stderr, "{ "); + fprintf(stderr, "background-color: #%lx; ", style->background_color); + fprintf(stderr, "clear: %s; ", css_clear_name[style->clear]); + fprintf(stderr, "color: #%lx; ", style->color); + fprintf(stderr, "display: %s; ", css_display_name[style->display]); + fprintf(stderr, "float: %s; ", css_float_name[style->float_]); + fprintf(stderr, "font-size: "); switch (style->font_size.size) { - case CSS_FONT_SIZE_ABSOLUTE: - LOG(("font-size: [%g];", style->font_size.value.absolute)); break; - case CSS_FONT_SIZE_LENGTH: - DUMP_LENGTH("font-size: ", &style->font_size.value.length, ";"); break; - case CSS_FONT_SIZE_PERCENT: - LOG(("font-size: %g%%;", style->font_size.value.percent)); break; - case CSS_FONT_SIZE_INHERIT: LOG(("font-size: inherit;")); break; - default: LOG(("font-size: UNKNOWN;")); break; + case CSS_FONT_SIZE_ABSOLUTE: fprintf(stderr, "[%g]", style->font_size.value.absolute); break; + case CSS_FONT_SIZE_LENGTH: dump_length(&style->font_size.value.length); break; + case CSS_FONT_SIZE_PERCENT: fprintf(stderr, "%g%%", style->font_size.value.percent); break; + case CSS_FONT_SIZE_INHERIT: fprintf(stderr, "inherit"); break; + default: fprintf(stderr, "UNKNOWN"); break; } + fprintf(stderr, "; "); + fprintf(stderr, "height: "); switch (style->height.height) { - case CSS_HEIGHT_AUTO: LOG(("height: auto;")); break; - case CSS_HEIGHT_LENGTH: DUMP_LENGTH("height: ", &style->height.length, ";"); break; - default: LOG(("height: UNKNOWN;")); break; + case CSS_HEIGHT_AUTO: fprintf(stderr, "auto"); break; + case CSS_HEIGHT_LENGTH: dump_length(&style->height.length); break; + default: fprintf(stderr, "UNKNOWN"); break; } + fprintf(stderr, "; "); + fprintf(stderr, "line-height: "); switch (style->line_height.size) { - case CSS_LINE_HEIGHT_ABSOLUTE: - LOG(("line-height: [%g];", style->line_height.value.absolute)); break; - case CSS_LINE_HEIGHT_LENGTH: - DUMP_LENGTH("line-height: ", &style->line_height.value.length, ";"); break; - case CSS_LINE_HEIGHT_PERCENT: - LOG(("line-height: %g%%;", style->line_height.value.percent)); break; - case CSS_LINE_HEIGHT_INHERIT: LOG(("line-height: inherit;")); break; - default: LOG(("line-height: UNKNOWN;")); break; + case CSS_LINE_HEIGHT_ABSOLUTE: fprintf(stderr, "[%g]", style->line_height.value.absolute); break; + case CSS_LINE_HEIGHT_LENGTH: dump_length(&style->line_height.value.length); break; + case CSS_LINE_HEIGHT_PERCENT: fprintf(stderr, "%g%%", style->line_height.value.percent); break; + case CSS_LINE_HEIGHT_INHERIT: fprintf(stderr, "inherit"); break; + default: fprintf(stderr, "UNKNOWN"); break; } - LOG(("text-align: %s;", css_text_align_name[style->text_align])); + fprintf(stderr, "; "); + fprintf(stderr, "text-align: %s; ", css_text_align_name[style->text_align]); + fprintf(stderr, "width: "); switch (style->width.width) { - case CSS_WIDTH_AUTO: LOG(("width: auto;")); break; - case CSS_WIDTH_LENGTH: - DUMP_LENGTH("width: ", &style->width.value.length, ";"); break; - case CSS_WIDTH_PERCENT: LOG(("width: %g%%;", style->width.value.percent)); break; - default: LOG(("width: UNKNOWN;")); break; + case CSS_WIDTH_AUTO: fprintf(stderr, "auto"); break; + case CSS_WIDTH_LENGTH: dump_length(&style->width.value.length); break; + case CSS_WIDTH_PERCENT: fprintf(stderr, "%g%%", style->width.value.percent); break; + default: fprintf(stderr, "UNKNOWN"); break; } - LOG(("}")); -} -#if 0 -static void dump_selector(const struct css_selector * const sel) -{ - if (sel->class != 0) - LOG(("'%s'.'%s' ", sel->element, sel->class); - else if (sel->id != 0) - LOG(("'%s'#'%s' ", sel->element, sel->id); - else - LOG(("'%s' ", sel->element); + fprintf(stderr, "; "); + fprintf(stderr, "}"); } -static void dump_rule(const struct rule * rule) -{ - unsigned int i; - for (i = 0; i < rule->selectors; i++) - dump_selector(&rule->selector[i]); - css_dump_style(rule->style); -} void css_dump_stylesheet(const struct css_stylesheet * stylesheet) { unsigned int i; - for (i = 0; i < HASH_SIZE; i++) { - struct rule * rule; - LOG(("hash %i:\n", i); - for (rule = stylesheet->hash[i]; rule != 0; rule = rule->next) - dump_rule(rule); + struct node *r, *n, *m; + for (i = 0; i != HASH_SIZE; i++) { + fprintf(stderr, "hash %i:\n", i); + for (r = stylesheet->rule[i]; r != 0; r = r->next) { + for (n = r; n != 0; n = n->right) { + if (n->data != 0) + fprintf(stderr, "%s", n->data); + for (m = n->left; m != 0; m = m->next) { + switch (m->type) { + case NODE_ID: fprintf(stderr, "%s", m->data); break; + case NODE_CLASS: fprintf(stderr, ".%s", m->data); break; + default: fprintf(stderr, "unexpected node"); + } + } + fprintf(stderr, " "); + } + css_dump_style(r->style); + fprintf(stderr, "\n"); + } } } -#endif + + /** * cascade styles */ @@ -412,12 +426,42 @@ void css_cascade(struct css_style * const style, const struct css_style * const } +void css_merge(struct css_style * const style, const struct css_style * const apply) +{ + if (apply->background_color != CSS_COLOR_INHERIT) + style->background_color = apply->background_color; + if (apply->clear != CSS_CLEAR_INHERIT) + style->clear = apply->clear; + if (apply->color != CSS_COLOR_INHERIT) + style->color = apply->color; + if (apply->display != CSS_DISPLAY_INHERIT) + style->display = apply->display; + if (apply->float_ != CSS_FLOAT_INHERIT) + style->float_ = apply->float_; + if (apply->height.height != CSS_HEIGHT_INHERIT) + style->height = apply->height; + if (apply->text_align != CSS_TEXT_ALIGN_INHERIT) + style->text_align = apply->text_align; + if (apply->width.width != CSS_WIDTH_INHERIT) + style->width = apply->width; + if (apply->font_weight != CSS_FONT_WEIGHT_INHERIT) + style->font_weight = apply->font_weight; + if (apply->font_style != CSS_FONT_STYLE_INHERIT) + style->font_style = apply->font_style; + if (apply->font_size.size != CSS_FONT_SIZE_INHERIT) + style->font_size = apply->font_size; +} + + + unsigned int css_hash(const char *s) { unsigned int z = 0; + if (s == 0) + return 0; for (; *s != 0; s++) z += *s; - return z % HASH_SIZE; + return (z % (HASH_SIZE - 1)) + 1; } diff --git a/css/css.h b/css/css.h index 78d0b8a56..a2ac5f4c7 100644 --- a/css/css.h +++ b/css/css.h @@ -1,5 +1,5 @@ /** - * $Id: css.h,v 1.3 2003/04/05 16:24:43 bursa Exp $ + * $Id: css.h,v 1.4 2003/04/05 21:38:06 bursa Exp $ */ #ifndef _NETSURF_CSS_CSS_H_ @@ -136,7 +136,7 @@ struct node { #include "netsurf/css/scanner.h" -#define HASH_SIZE 47 +#define HASH_SIZE (47 + 1) struct css_stylesheet { yyscan_t lexer; @@ -187,6 +187,7 @@ void css_parser_(void *yyp, int yymajor, char* yyminor, void css_get_style(struct css_stylesheet * stylesheet, struct css_selector * selector, unsigned int selectors, struct css_style * style); void css_cascade(struct css_style * const style, const struct css_style * const apply); +void css_merge(struct css_style * const style, const struct css_style * const apply); void css_parse_property_list(struct css_style * style, char * str); #endif diff --git a/css/ruleset.c b/css/ruleset.c index d9c713d87..6b16c96b7 100644 --- a/css/ruleset.c +++ b/css/ruleset.c @@ -1,5 +1,5 @@ /** - * $Id: ruleset.c,v 1.3 2003/04/05 16:24:43 bursa Exp $ + * $Id: ruleset.c,v 1.4 2003/04/05 21:38:06 bursa Exp $ */ #include @@ -117,13 +117,15 @@ void css_add_ruleset(struct css_stylesheet *stylesheet, break; if (n == 0) { /* not present: construct a new struct css_style */ + LOG(("constructing new style")); style = xcalloc(1, sizeof(*style)); - memcpy(style, &css_blank_style, sizeof(*style)); + memcpy(style, &css_empty_style, sizeof(*style)); sel->style = style; sel->next = stylesheet->rule[hash]; stylesheet->rule[hash] = sel; } else { /* already exists: augument existing style */ + LOG(("augumenting existing style")); style = n->style; sel->next = 0; css_free_node(sel); @@ -154,11 +156,11 @@ void css_add_declarations(struct css_style *style, struct node *declaration) int compare_selectors(struct node *n0, struct node *n1) { struct node *m0, *m1; - unsigned int count0, count1; + unsigned int count0 = 0, count1 = 0; /* compare element name */ if (!((n0->data == 0 && n1->data == 0) || - (n0->data != 0 && n1->data != 0 && strcasecmp(n0->data, n1->data) == 0))) + (n0->data != 0 && n1->data != 0 && strcmp(n0->data, n1->data) == 0))) return 0; if (n0->comb != n1->comb) diff --git a/desktop/browser.c b/desktop/browser.c index 1d94971fa..fdfdb3eae 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -1,5 +1,5 @@ /** - * $Id: browser.c,v 1.30 2003/03/04 11:59:35 bursa Exp $ + * $Id: browser.c,v 1.31 2003/04/05 21:38:06 bursa Exp $ */ #include "netsurf/content/cache.h" @@ -38,7 +38,6 @@ static void gui_redraw_gadget2(struct browser_window* bw, struct box* box, struc unsigned long x, unsigned long y); static void browser_window_gadget_select(struct browser_window* bw, struct gui_gadget* g, int item); static int browser_window_gadget_click(struct browser_window* bw, unsigned long click_x, unsigned long click_y); -static char *url_join(const char* new, const char* base); void browser_window_start_throbber(struct browser_window* bw) @@ -858,56 +857,4 @@ void browser_window_redraw_boxes(struct browser_window* bw, struct box_position* redraw_max_x, redraw_max_y); } -char *url_join(const char* new, const char* base) -{ - char* ret; - int i; - LOG(("new = %s, base = %s", new, base)); - - if (base == 0) - { - /* no base, so make an absolute URL */ - ret = xcalloc(strlen(new) + 10, sizeof(char)); - - /* check if a scheme is present */ - i = strspn(new, "abcdefghijklmnopqrstuvwxyz"); - if (new[i] == ':') - { - strcpy(ret, new); - i += 3; - } - else - { - strcpy(ret, "http://"); - strcat(ret, new); - i = 7; - } - - /* make server name lower case */ - for (; ret[i] != 0 && ret[i] != '/'; i++) - ret[i] = tolower(ret[i]); - - xmlNormalizeURIPath(ret + i); - - /* http://www.example.com -> http://www.example.com/ */ - if (ret[i] == 0) - { - ret[i] = '/'; - ret[i+1] = 0; - } - } - else - { - /* relative url */ - ret = xmlBuildURI(new, base); - } - - LOG(("ret = %s", ret)); - if (ret == NULL) - { - ret = xcalloc(strlen(new) + 10, sizeof(char)); - strcpy(ret, new); - } - return ret; -} diff --git a/render/box.c b/render/box.c index 11093c52b..b8ef34c9b 100644 --- a/render/box.c +++ b/render/box.c @@ -1,5 +1,5 @@ /** - * $Id: box.c,v 1.36 2003/04/04 15:19:31 bursa Exp $ + * $Id: box.c,v 1.37 2003/04/05 21:38:06 bursa Exp $ */ #include @@ -26,14 +26,15 @@ static struct box * box_create(xmlNode * node, box_type type, struct css_style * char *href); static char * tolat1(xmlChar * s); static struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, - struct css_stylesheet * stylesheet, + struct content ** stylesheet, unsigned int stylesheet_count, struct css_selector ** selector, unsigned int depth, struct box * parent, struct box * inline_container, char *href, struct font_set *fonts, struct gui_gadget* current_select, struct formoption* current_option, struct gui_gadget* current_textarea, struct form* current_form, struct page_elements* elements); -static struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style, +static struct css_style * box_get_style(struct content ** stylesheet, + unsigned int stylesheet_count, struct css_style * parent_style, xmlNode * n, struct css_selector * selector, unsigned int depth); static void box_normalise_block(struct box *block); static void box_normalise_table(struct box *table); @@ -144,7 +145,7 @@ char * tolat1(xmlChar * s) */ void xml_to_box(xmlNode * n, struct css_style * parent_style, - struct css_stylesheet * stylesheet, + struct content ** stylesheet, unsigned int stylesheet_count, struct css_selector ** selector, unsigned int depth, struct box * parent, struct box * inline_container, char *href, struct font_set *fonts, @@ -153,7 +154,7 @@ void xml_to_box(xmlNode * n, struct css_style * parent_style, struct page_elements* elements) { LOG(("node %p", n)); - convert_xml_to_box(n, parent_style, stylesheet, + convert_xml_to_box(n, parent_style, stylesheet, stylesheet_count, selector, depth, parent, inline_container, href, fonts, current_select, current_option, current_textarea, current_form, elements); LOG(("normalising")); @@ -161,7 +162,7 @@ void xml_to_box(xmlNode * n, struct css_style * parent_style, } struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, - struct css_stylesheet * stylesheet, + struct content ** stylesheet, unsigned int stylesheet_count, struct css_selector ** selector, unsigned int depth, struct box * parent, struct box * inline_container, char *href, struct font_set *fonts, @@ -188,9 +189,10 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, (*selector)[depth].class = (*selector)[depth].id = 0; if ((s = (char *) xmlGetProp(n, (const xmlChar *) "class"))) { (*selector)[depth].class = s; - free(s); + /*free(s);*/ } - style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1); + style = box_get_style(stylesheet, stylesheet_count, parent_style, n, + *selector, depth + 1); LOG(("display: %s", css_display_name[style->display])); if (style->display == CSS_DISPLAY_NONE) return inline_container; @@ -326,7 +328,8 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, box_add_child(parent, box); inline_container_c = 0; for (c = n->children; c != 0; c = c->next) - inline_container_c = convert_xml_to_box(c, style, stylesheet, + inline_container_c = convert_xml_to_box(c, style, + stylesheet, stylesheet_count, selector, depth + 1, box, inline_container_c, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -336,7 +339,8 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, assert(box == 0); /* special inline elements have already been added to the inline container above */ for (c = n->children; c != 0; c = c->next) - inline_container = convert_xml_to_box(c, style, stylesheet, + inline_container = convert_xml_to_box(c, style, + stylesheet, stylesheet_count, selector, depth + 1, parent, inline_container, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -345,7 +349,7 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, box = box_create(n, BOX_TABLE, style, href); box_add_child(parent, box); for (c = n->children; c != 0; c = c->next) - convert_xml_to_box(c, style, stylesheet, + convert_xml_to_box(c, style, stylesheet, stylesheet_count, selector, depth + 1, box, 0, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -358,7 +362,8 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, box_add_child(parent, box); inline_container_c = 0; for (c = n->children; c != 0; c = c->next) - inline_container_c = convert_xml_to_box(c, style, stylesheet, + inline_container_c = convert_xml_to_box(c, style, + stylesheet, stylesheet_count, selector, depth + 1, box, inline_container_c, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -368,7 +373,7 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, box = box_create(n, BOX_TABLE_ROW, style, href); box_add_child(parent, box); for (c = n->children; c != 0; c = c->next) - convert_xml_to_box(c, style, stylesheet, + convert_xml_to_box(c, style, stylesheet, stylesheet_count, selector, depth + 1, box, 0, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -384,7 +389,8 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, box_add_child(parent, box); inline_container_c = 0; for (c = n->children; c != 0; c = c->next) - inline_container_c = convert_xml_to_box(c, style, stylesheet, + inline_container_c = convert_xml_to_box(c, style, + stylesheet, stylesheet_count, selector, depth + 1, box, inline_container_c, href, fonts, current_select, current_option, current_textarea, current_form, elements); @@ -404,14 +410,25 @@ struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style, * get the style for an element */ -struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style, +struct css_style * box_get_style(struct content ** stylesheet, + unsigned int stylesheet_count, struct css_style * parent_style, xmlNode * n, struct css_selector * selector, unsigned int depth) { struct css_style * style = xcalloc(1, sizeof(struct css_style)); + struct css_style * style_new = xcalloc(1, sizeof(struct css_style)); char * s; + unsigned int i; memcpy(style, parent_style, sizeof(struct css_style)); - css_get_style(stylesheet, selector, depth, style); + memcpy(style_new, &css_blank_style, sizeof(struct css_style)); + for (i = 0; i != stylesheet_count; i++) { + if (stylesheet[i] != 0) { + assert(stylesheet[i]->type == CONTENT_CSS); + css_get_style(stylesheet[i]->data.css, selector, depth, style_new); + } + } + css_cascade(style, style_new); + free(style_new); if ((s = (char *) xmlGetProp(n, (const xmlChar *) "align"))) { if (strcmp((const char *) n->name, "table") == 0 || diff --git a/render/box.h b/render/box.h index 4477d24c1..21e80a096 100644 --- a/render/box.h +++ b/render/box.h @@ -1,5 +1,5 @@ /** - * $Id: box.h,v 1.21 2003/04/04 15:19:31 bursa Exp $ + * $Id: box.h,v 1.22 2003/04/05 21:38:06 bursa Exp $ */ #ifndef _NETSURF_RENDER_BOX_H_ @@ -133,7 +133,8 @@ struct page_elements * interface */ -void xml_to_box(xmlNode * n, struct css_style * parent_style, struct css_stylesheet * stylesheet, +void xml_to_box(xmlNode * n, struct css_style * parent_style, + struct content ** stylesheet, unsigned int stylesheet_count, struct css_selector ** selector, unsigned int depth, struct box * parent, struct box * inline_container, char *href, struct font_set *fonts, diff --git a/render/html.c b/render/html.c index 9b2508c65..63bab48c0 100644 --- a/render/html.c +++ b/render/html.c @@ -1,9 +1,10 @@ /** - * $Id: html.c,v 1.7 2003/04/04 15:19:31 bursa Exp $ + * $Id: html.c,v 1.8 2003/04/05 21:38:06 bursa Exp $ */ #include #include +#include #include #include "netsurf/content/fetch.h" #include "netsurf/content/fetchcache.h" @@ -14,9 +15,16 @@ #include "netsurf/utils/log.h" -static void html_convert_callback(fetchcache_msg msg, struct content *css, +struct fetch_data { + struct content *c; + unsigned int i; +}; + + +static void html_convert_css_callback(fetchcache_msg msg, struct content *css, void *p, const char *error); static void html_title(struct content *c); +static void html_find_stylesheets(struct content *c); void html_create(struct content *c) @@ -47,6 +55,8 @@ void html_process_data(struct content *c, char *data, unsigned long size) int html_convert(struct content *c, unsigned int width, unsigned int height) { struct css_selector* selector = xcalloc(1, sizeof(struct css_selector)); + struct fetch_data *fetch_data; + unsigned int i; htmlParseChunk(c->data.html.parser, "", 0, 1); c->data.html.document = c->data.html.parser->myDoc; @@ -66,19 +76,29 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) LOG(("No markup")); return 1; } - if (stricmp((const char *) c->data.html.markup->name, "html")) { + if (strcmp((const char *) c->data.html.markup->name, "html")) { LOG(("Not html")); return 1; } html_title(c); + /* get stylesheets */ + html_find_stylesheets(c); + c->data.html.stylesheet_content = xcalloc(c->data.html.stylesheet_count, + sizeof(*c->data.html.stylesheet_content)); + c->error = 0; c->active = 0; - fetchcache("file:///%3CNetSurf$Dir%3E/Resources/CSS", 0, - html_convert_callback, c, width, height); - c->active++; + for (i = 0; i != c->data.html.stylesheet_count; i++) { + fetch_data = xcalloc(1, sizeof(*fetch_data)); + fetch_data->c = c; + fetch_data->i = i; + c->active++; + fetchcache(c->data.html.stylesheet_url[i], c->url, html_convert_css_callback, + fetch_data, width, height); + } while (c->active != 0) { fetch_poll(); @@ -89,6 +109,8 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) /* TODO: clean up */ return 1; } + + c->data.html.stylesheet = c->data.html.stylesheet_content[0]->data.css; LOG(("Copying base style")); c->data.html.style = xcalloc(1, sizeof(struct css_style)); @@ -102,7 +124,8 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) c->data.html.fonts = font_new_set(); LOG(("XML to box")); - xml_to_box(c->data.html.markup, c->data.html.style, c->data.html.stylesheet, + xml_to_box(c->data.html.markup, c->data.html.style, + c->data.html.stylesheet_content, c->data.html.stylesheet_count, &selector, 0, c->data.html.layout, 0, 0, c->data.html.fonts, 0, 0, 0, 0, &c->data.html.elements); /*box_dump(c->data.html.layout->children, 0);*/ @@ -118,18 +141,24 @@ int html_convert(struct content *c, unsigned int width, unsigned int height) } -void html_convert_callback(fetchcache_msg msg, struct content *css, +void html_convert_css_callback(fetchcache_msg msg, struct content *css, void *p, const char *error) { - struct content *c = p; + struct fetch_data *data = p; + struct content *c = data->c; + unsigned int i = data->i; switch (msg) { case FETCHCACHE_OK: - /* TODO: store struct content *css somewhere in c */ - c->data.html.stylesheet = css->data.css; + free(data); + LOG(("got stylesheet '%s'", c->data.html.stylesheet_url[i])); + c->data.html.stylesheet_content[i] = css; + /*css_dump_stylesheet(css->data.css);*/ c->active--; break; case FETCHCACHE_BADTYPE: case FETCHCACHE_ERROR: + free(data); + c->data.html.stylesheet_content[i] = 0; c->active--; c->error = 1; break; @@ -145,30 +174,80 @@ void html_convert_callback(fetchcache_msg msg, struct content *css, void html_title(struct content *c) { - xmlNode *node = c->data.html.markup; + xmlNode *head = c->data.html.markup->children; + xmlNode *node; c->title = 0; - while (node != 0) { - if (node->type == XML_ELEMENT_NODE) { - if (stricmp(node->name, "html") == 0) { - node = node->children; - continue; - } - if (stricmp(node->name, "head") == 0) { - node = node->children; - continue; - } - if (stricmp(node->name, "title") == 0) { - c->title = xmlNodeGetContent(node); - return; - } + if (strcmp(head->name, "head") != 0) + return; + for (node = head->children; node != 0; node = node->next) { + if (strcmp(node->name, "title") == 0) { + c->title = xmlNodeGetContent(node); + return; } - node = node->next; } } +void html_find_stylesheets(struct content *c) +{ + xmlNode *head = c->data.html.markup->children; + xmlNode *node; + char *rel, *type, *media, *href; + unsigned int count = 1; + + c->data.html.stylesheet_url = xcalloc(1, sizeof(*c->data.html.stylesheet_url)); + c->data.html.stylesheet_url[0] = "file:///%3CNetSurf$Dir%3E/Resources/CSS"; + + if (strcmp(head->name, "head") != 0) + return; + for (node = head->children; node != 0; node = node->next) { + if (strcmp(node->name, "link") == 0) { + /* rel='stylesheet' */ + if (!(rel = (char *) xmlGetProp(node, (const xmlChar *) "rel"))) + continue; + if (strcasecmp(rel, "stylesheet") != 0) { + free(rel); + continue; + } + free(rel); + + /* type='text/css' */ + if (!(type = (char *) xmlGetProp(node, (const xmlChar *) "type"))) + continue; + if (strcmp(type, "text/css") != 0) { + free(type); + continue; + } + free(type); + + /* media='screen' or not present */ + if ((media = (char *) xmlGetProp(node, (const xmlChar *) "media"))) { + if (strcasecmp(media, "screen") != 0) { + free(media); + continue; + } + free(media); + } + + /* href='...' */ + if (!(href = (char *) xmlGetProp(node, (const xmlChar *) "href"))) + continue; + + count++; + c->data.html.stylesheet_url = xrealloc(c->data.html.stylesheet_url, + count * sizeof(*c->data.html.stylesheet_url)); + c->data.html.stylesheet_url[count - 1] = url_join(href, c->url); + LOG(("linked stylesheet '%s'", c->data.html.stylesheet_url[count - 1])); + free(href); + } + } + + c->data.html.stylesheet_count = count; +} + + void html_revive(struct content *c, unsigned int width, unsigned int height) { /* TODO: reload stylesheets and images and fix any pointers to them */ diff --git a/utils/utils.c b/utils/utils.c index 8604d9ed4..47c90a4c0 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -1,11 +1,13 @@ /** - * $Id: utils.c,v 1.6 2003/02/09 12:58:15 bursa Exp $ + * $Id: utils.c,v 1.7 2003/04/05 21:38:06 bursa Exp $ */ #include #include #include #include +#include "libxml/uri.h" +#include "netsurf/utils/log.h" #include "netsurf/utils/utils.h" void die(const char * const error) @@ -96,3 +98,57 @@ char * squash_whitespace(const char * s) return c; } +char *url_join(const char* new, const char* base) +{ + char* ret; + int i; + + LOG(("new = %s, base = %s", new, base)); + + if (base == 0) + { + /* no base, so make an absolute URL */ + ret = xcalloc(strlen(new) + 10, sizeof(char)); + + /* check if a scheme is present */ + i = strspn(new, "abcdefghijklmnopqrstuvwxyz"); + if (new[i] == ':') + { + strcpy(ret, new); + i += 3; + } + else + { + strcpy(ret, "http://"); + strcat(ret, new); + i = 7; + } + + /* make server name lower case */ + for (; ret[i] != 0 && ret[i] != '/'; i++) + ret[i] = tolower(ret[i]); + + xmlNormalizeURIPath(ret + i); + + /* http://www.example.com -> http://www.example.com/ */ + if (ret[i] == 0) + { + ret[i] = '/'; + ret[i+1] = 0; + } + } + else + { + /* relative url */ + ret = xmlBuildURI(new, base); + } + + LOG(("ret = %s", ret)); + if (ret == NULL) + { + ret = xcalloc(strlen(new) + 10, sizeof(char)); + strcpy(ret, new); + } + return ret; +} + diff --git a/utils/utils.h b/utils/utils.h index 5ae167eb9..c55ab5b69 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -1,5 +1,5 @@ /** - * $Id: utils.h,v 1.5 2003/02/09 12:58:15 bursa Exp $ + * $Id: utils.h,v 1.6 2003/04/05 21:38:06 bursa Exp $ */ #ifndef _NETSURF_UTILS_UTILS_H_ @@ -16,5 +16,6 @@ void xfree(void* p); char * xstrdup(const char * const s); char * load(const char * const path); char * squash_whitespace(const char * s); +char *url_join(const char* new, const char* base); #endif