diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2011-05-06 20:40:09 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2011-05-06 20:40:09 +0000 |
commit | e71691bae890040b83cfd54a2d9a1097d5026866 (patch) | |
tree | 96b2680dc6559ca0ab88fa0b6a533c13b7c9487e /render | |
parent | e77b1a29550e4753f771848705975295a6ebe99e (diff) | |
download | netsurf-e71691bae890040b83cfd54a2d9a1097d5026866.tar.gz netsurf-e71691bae890040b83cfd54a2d9a1097d5026866.tar.bz2 |
Merge branches/jmb/content-factory to trunk
svn path=/trunk/netsurf/; revision=12283
Diffstat (limited to 'render')
-rw-r--r-- | render/box.c | 16 | ||||
-rw-r--r-- | render/box.h | 6 | ||||
-rw-r--r-- | render/box_construct.c | 189 | ||||
-rw-r--r-- | render/box_normalise.c | 73 | ||||
-rw-r--r-- | render/favicon.c | 275 | ||||
-rw-r--r-- | render/favicon.h | 28 | ||||
-rw-r--r-- | render/form.c | 1 | ||||
-rw-r--r-- | render/html.c | 822 | ||||
-rw-r--r-- | render/html.h | 105 | ||||
-rw-r--r-- | render/html_interaction.c | 2 | ||||
-rw-r--r-- | render/html_internal.h | 112 | ||||
-rw-r--r-- | render/html_redraw.c | 9 | ||||
-rw-r--r-- | render/imagemap.c | 53 | ||||
-rw-r--r-- | render/imagemap.h | 8 | ||||
-rw-r--r-- | render/layout.c | 45 | ||||
-rw-r--r-- | render/layout.h | 5 | ||||
-rw-r--r-- | render/textplain.c | 325 | ||||
-rw-r--r-- | render/textplain.h | 32 |
18 files changed, 984 insertions, 1122 deletions
diff --git a/render/box.c b/render/box.c index e54ca2fb7..537ea3674 100644 --- a/render/box.c +++ b/render/box.c @@ -35,7 +35,7 @@ #include "desktop/options.h" #include "render/box.h" #include "render/form.h" -#include "render/html.h" +#include "render/html_internal.h" #include "utils/log.h" #include "utils/talloc.h" #include "utils/utils.h" @@ -563,16 +563,12 @@ bool box_contains_point(struct box *box, int x, int y, bool *physically) struct box *box_object_at_point(hlcache_handle *h, int x, int y) { - struct content *c = hlcache_handle_get_content(h); struct box *box; int box_x = 0, box_y = 0; hlcache_handle *content = h; struct box *object_box = 0; - assert(c != NULL); - assert(c->type == CONTENT_HTML); - - box = c->data.html.layout; + box = html_get_box_tree(h); while ((box = box_at_point(box, x, y, &box_x, &box_y, &content))) { if (box->style && css_computed_visibility(box->style) == @@ -597,16 +593,12 @@ struct box *box_object_at_point(hlcache_handle *h, int x, int y) struct box *box_href_at_point(hlcache_handle *h, int x, int y) { - struct content *c = hlcache_handle_get_content(h); struct box *box; int box_x = 0, box_y = 0; hlcache_handle *content = h; struct box *href_box = 0; - assert(c != NULL); - assert(c->type == CONTENT_HTML); - - box = c->data.html.layout; + box = html_get_box_tree(h); while ((box = box_at_point(box, x, y, &box_x, &box_y, &content))) { if (box->style && css_computed_visibility(box->style) == @@ -804,7 +796,7 @@ struct box *box_pick_text_box(hlcache_handle *h, { struct box *text_box = NULL; - if (h && content_get_type(h) == CONTENT_HTML) { + if (h != NULL && content_get_type(h) == CONTENT_HTML) { struct box *box = html_get_box_tree(h); int nr_xd, nr_yd; int bx = box->margin[LEFT]; diff --git a/render/box.h b/render/box.h index e7c19ad6d..b5092728f 100644 --- a/render/box.h +++ b/render/box.h @@ -99,7 +99,7 @@ struct box; struct column; struct object_params; struct object_param; - +struct html_content; #define UNKNOWN_WIDTH INT_MAX #define UNKNOWN_MAX_WIDTH INT_MAX @@ -332,8 +332,8 @@ bool box_handle_scrollbars(struct browser_window *bw, struct box *box, bool box_vscrollbar_present(const struct box *box); bool box_hscrollbar_present(const struct box *box); -bool xml_to_box(xmlNode *n, struct content *c); +bool xml_to_box(xmlNode *n, struct html_content *c); -bool box_normalise_block(struct box *block, struct content *c); +bool box_normalise_block(struct box *block, struct html_content *c); #endif diff --git a/render/box_construct.c b/render/box_construct.c index f780d0714..f73a8322c 100644 --- a/render/box_construct.c +++ b/render/box_construct.c @@ -42,7 +42,7 @@ #include "desktop/options.h" #include "render/box.h" #include "render/form.h" -#include "render/html.h" +#include "render/html_internal.h" #include "desktop/gui.h" #include "utils/locale.h" #include "utils/log.h" @@ -52,45 +52,7 @@ #include "utils/utils.h" -static const content_type image_types[] = { -#ifdef WITH_JPEG - CONTENT_JPEG, -#endif -#ifdef WITH_GIF - CONTENT_GIF, -#endif -#ifdef WITH_BMP - CONTENT_BMP, -#endif -#if defined(WITH_MNG) || defined(WITH_PNG) - CONTENT_PNG, -#endif -#ifdef WITH_MNG - CONTENT_JNG, - CONTENT_MNG, -#endif -#if defined(WITH_NS_SVG) || defined(WITH_RSVG) - CONTENT_SVG, -#endif -#if defined(WITH_SPRITE) || defined(WITH_NSSPRITE) - CONTENT_SPRITE, -#endif -#ifdef WITH_DRAW - CONTENT_DRAW, -#endif -#ifdef WITH_ARTWORKS - CONTENT_ARTWORKS, -#endif -#ifdef WITH_WEBP - CONTENT_WEBP, -#endif -#ifdef WITH_AMIGA_ICON - CONTENT_AMIGA_ICON, -#endif -#ifdef WITH_APPLE_IMAGE - CONTENT_APPLE_IMAGE, -#endif - CONTENT_UNKNOWN }; +static const content_type image_types = CONTENT_IMAGE; /* the strings are not important, since we just compare the pointers */ const char *TARGET_SELF = "_self"; @@ -98,25 +60,25 @@ const char *TARGET_PARENT = "_parent"; const char *TARGET_TOP = "_top"; const char *TARGET_BLANK = "_blank"; -static bool convert_xml_to_box(xmlNode *n, struct content *content, +static bool convert_xml_to_box(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title); -bool box_construct_element(xmlNode *n, struct content *content, +bool box_construct_element(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title); -void box_construct_generate(xmlNode *n, struct content *content, +void box_construct_generate(xmlNode *n, html_content *content, struct box *box, const css_computed_style *style); -bool box_construct_text(xmlNode *n, struct content *content, +bool box_construct_text(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title); -static css_select_results * box_get_style(struct content *c, +static css_select_results * box_get_style(html_content *c, const css_computed_style *parent_style, xmlNode *n); static void box_text_transform(char *s, unsigned int len, enum css_text_transform_e tt); -#define BOX_SPECIAL_PARAMS xmlNode *n, struct content *content, \ +#define BOX_SPECIAL_PARAMS xmlNode *n, html_content *content, \ struct box *box, bool *convert_children static bool box_a(BOX_SPECIAL_PARAMS); static bool box_body(BOX_SPECIAL_PARAMS); @@ -129,7 +91,7 @@ static bool box_input_text(BOX_SPECIAL_PARAMS, bool password); static bool box_button(BOX_SPECIAL_PARAMS); static bool box_frameset(BOX_SPECIAL_PARAMS); static bool box_create_frameset(struct content_html_frames *f, xmlNode *n, - struct content *content); + html_content *content); static bool box_select_add_option(struct form_control *control, xmlNode *n); static bool box_object(BOX_SPECIAL_PARAMS); static bool box_embed(BOX_SPECIAL_PARAMS); @@ -173,13 +135,11 @@ static const struct element_entry element_table[] = { * \return true on success, false on memory exhaustion */ -bool xml_to_box(xmlNode *n, struct content *c) +bool xml_to_box(xmlNode *n, html_content *c) { struct box root; struct box *inline_container = NULL; - assert(c->type == CONTENT_HTML); - root.type = BOX_BLOCK; root.style = NULL; root.next = NULL; @@ -198,8 +158,8 @@ bool xml_to_box(xmlNode *n, struct content *c) if (!box_normalise_block(&root, c)) return false; - c->data.html.layout = root.children; - c->data.html.layout->parent = NULL; + c->layout = root.children; + c->layout->parent = NULL; return true; } @@ -243,7 +203,7 @@ static const box_type box_map[] = { * \return true on success, false on memory exhaustion */ -bool convert_xml_to_box(xmlNode *n, struct content *content, +bool convert_xml_to_box(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title) @@ -277,7 +237,7 @@ bool convert_xml_to_box(xmlNode *n, struct content *content, * \return true on success, false on memory exhaustion */ -bool box_construct_element(xmlNode *n, struct content *content, +bool box_construct_element(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title) @@ -543,7 +503,7 @@ bool box_construct_element(xmlNode *n, struct content *content, if (!html_fetch_object(content, lwc_string_data(image_uri), marker, image_types, - content->available_width, + content->base.available_width, 1000, false)) return false; } @@ -607,7 +567,7 @@ bool box_construct_element(xmlNode *n, struct content *content, bgimage_uri != NULL) { if (!html_fetch_object(content, lwc_string_data(bgimage_uri), - box, image_types, content->available_width, + box, image_types, content->base.available_width, 1000, true)) return false; } @@ -645,7 +605,7 @@ bool box_construct_element(xmlNode *n, struct content *content, * We don't actually support generated content yet. */ -void box_construct_generate(xmlNode *n, struct content *content, +void box_construct_generate(xmlNode *n, html_content *content, struct box *box, const css_computed_style *style) { struct box *gen = NULL; @@ -694,7 +654,7 @@ void box_construct_generate(xmlNode *n, struct content *content, * \return true on success, false on memory exhaustion */ -bool box_construct_text(xmlNode *n, struct content *content, +bool box_construct_text(xmlNode *n, html_content *content, const css_computed_style *parent_style, struct box *parent, struct box **inline_container, char *href, const char *target, char *title) @@ -915,7 +875,7 @@ bool box_construct_text(xmlNode *n, struct content *content, * \param n node in xml tree * \return the new style, or NULL on memory exhaustion */ -css_select_results *box_get_style(struct content *c, +css_select_results *box_get_style(html_content *c, const css_computed_style *parent_style, xmlNode *n) { char *s; @@ -923,13 +883,14 @@ css_select_results *box_get_style(struct content *c, css_error error; css_stylesheet *inline_style = NULL; css_select_results *styles; + nscss_select_ctx ctx; /* Firstly, construct inline stylesheet, if any */ if ((s = (char *) xmlGetProp(n, (const xmlChar *) "style"))) { inline_style = nscss_create_inline_style( (uint8_t *) s, strlen(s), - c->data.html.encoding, content__get_url(c), - c->data.html.quirks != BINDING_QUIRKS_MODE_NONE, + c->encoding, content__get_url(&c->base), + c->quirks != BINDING_QUIRKS_MODE_NONE, box_style_alloc, NULL); xmlFree(s); @@ -938,8 +899,13 @@ css_select_results *box_get_style(struct content *c, return NULL; } + /* Populate selection context */ + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + /* Select partial style for element */ - styles = nscss_get_style(c, n, CSS_MEDIA_SCREEN, inline_style, + styles = nscss_get_style(&ctx, n, CSS_MEDIA_SCREEN, inline_style, box_style_alloc, NULL); /* No longer need inline style */ @@ -1066,9 +1032,9 @@ bool box_body(BOX_SPECIAL_PARAMS) css_computed_background_color(box->style, &color); if (nscss_color_is_transparent(color)) - content->data.html.background_colour = NS_TRANSPARENT; + content->background_colour = NS_TRANSPARENT; else - content->data.html.background_colour = nscss_color_to_ns(color); + content->background_colour = nscss_color_to_ns(color); return true; } @@ -1106,7 +1072,7 @@ bool box_a(BOX_SPECIAL_PARAMS) if ((s = xmlGetProp(n, (const xmlChar *) "href"))) { ok = box_extract_link((const char *) s, - content->data.html.base_url, &url); + content->base_url, &url); xmlFree(s); if (!ok) return false; @@ -1191,7 +1157,7 @@ bool box_image(BOX_SPECIAL_PARAMS) /* get image URL */ if (!(src = xmlGetProp(n, (const xmlChar *) "src"))) return true; - if (!box_extract_link((char *) src, content->data.html.base_url, &url)) + if (!box_extract_link((char *) src, content->base_url, &url)) return false; xmlFree(src); if (!url) @@ -1199,7 +1165,7 @@ bool box_image(BOX_SPECIAL_PARAMS) /* start fetch */ ok = html_fetch_object(content, url, box, image_types, - content->available_width, 1000, false); + content->base.available_width, 1000, false); free(url); wtype = css_computed_width(box->style, &value, &wunit); @@ -1250,13 +1216,13 @@ bool box_object(BOX_SPECIAL_PARAMS) * (codebase is the base for the other two) */ if ((codebase = xmlGetProp(n, (const xmlChar *) "codebase"))) { if (!box_extract_link((char *) codebase, - content->data.html.base_url, + content->base_url, ¶ms->codebase)) return false; xmlFree(codebase); } if (!params->codebase) - params->codebase = content->data.html.base_url; + params->codebase = content->base_url; if ((classid = xmlGetProp(n, (const xmlChar *) "classid"))) { if (!box_extract_link((char *) classid, params->codebase, @@ -1277,14 +1243,10 @@ bool box_object(BOX_SPECIAL_PARAMS) return true; /* Don't include ourself */ - if (params->classid && - strcmp(content->data.html.base_url, - params->classid) == 0) + if (params->classid && strcmp(content->base_url, params->classid) == 0) return true; - if (params->data && - strcmp(content->data.html.base_url, - params->data) == 0) + if (params->data && strcmp(content->base_url, params->data) == 0) return true; /* codetype and type are MIME types */ @@ -1298,12 +1260,14 @@ bool box_object(BOX_SPECIAL_PARAMS) * !classid && !data => invalid; ignored */ if (params->classid && !params->data && params->codetype && - content_lookup(params->codetype) == CONTENT_OTHER) + content_factory_type_from_mime_type(params->codetype) == + CONTENT_NONE) /* can't handle this MIME type */ return true; if (params->data && params->type && - content_lookup(params->type) == CONTENT_OTHER) + content_factory_type_from_mime_type(params->type) == + CONTENT_NONE) /* can't handle this MIME type */ return true; @@ -1349,7 +1313,8 @@ bool box_object(BOX_SPECIAL_PARAMS) /* start fetch (MIME type is ok or not specified) */ if (!html_fetch_object(content, params->data ? params->data : params->classid, - box, 0, content->available_width, 1000, false)) + box, CONTENT_ANY, content->base.available_width, 1000, + false)) return false; *convert_children = false; @@ -1503,7 +1468,7 @@ bool box_frameset(BOX_SPECIAL_PARAMS) { bool ok; - if (content->data.html.frameset) { + if (content->frameset) { LOG(("Error: multiple framesets in document.")); /* Don't convert children */ if (convert_children) @@ -1513,12 +1478,11 @@ bool box_frameset(BOX_SPECIAL_PARAMS) return true; } - content->data.html.frameset = talloc_zero(content, - struct content_html_frames); - if (!content->data.html.frameset) + content->frameset = talloc_zero(content, struct content_html_frames); + if (!content->frameset) return false; - ok = box_create_frameset(content->data.html.frameset, n, content); + ok = box_create_frameset(content->frameset, n, content); if (ok) box->type = BOX_NONE; @@ -1528,7 +1492,7 @@ bool box_frameset(BOX_SPECIAL_PARAMS) } bool box_create_frameset(struct content_html_frames *f, xmlNode *n, - struct content *content) { + html_content *content) { unsigned int row, col, index, i; unsigned int rows = 1, cols = 1; char *s, *url; @@ -1645,15 +1609,14 @@ bool box_create_frameset(struct content_html_frames *f, xmlNode *n, url = NULL; if ((s = (char *) xmlGetProp(c, (const xmlChar *) "src"))) { - box_extract_link(s, content->data.html.base_url, - &url); + box_extract_link(s, content->base_url, &url); xmlFree(s); } /* copy url */ if (url) { /* no self-references */ - if (strcmp(content->data.html.base_url, url)) + if (strcmp(content->base_url, url)) frame->url = talloc_strdup(content, url); free(url); @@ -1738,7 +1701,7 @@ bool box_iframe(BOX_SPECIAL_PARAMS) if (!(s = (char *) xmlGetProp(n, (const xmlChar *) "src"))) return true; - if (!box_extract_link(s, content->data.html.base_url, &url)) { + if (!box_extract_link(s, content->base_url, &url)) { xmlFree(s); return false; } @@ -1747,7 +1710,7 @@ bool box_iframe(BOX_SPECIAL_PARAMS) return true; /* don't include ourself */ - if (strcmp(content->data.html.base_url, url) == 0) { + if (strcmp(content->base_url, url) == 0) { free(url); return true; } @@ -1767,17 +1730,15 @@ bool box_iframe(BOX_SPECIAL_PARAMS) iframe->border = true; /* Add this iframe to the linked list of iframes */ - iframe->next = content->data.html.iframe; - content->data.html.iframe = iframe; + iframe->next = content->iframe; + content->iframe = iframe; /* fill in specified values */ - if ((s = (char *) xmlGetProp(n, - (const xmlChar *) "name"))) { + if ((s = (char *) xmlGetProp(n, (const xmlChar *) "name"))) { iframe->name = talloc_strdup(content, s); xmlFree(s); } - if ((s = (char *) xmlGetProp(n, - (const xmlChar *) "frameborder"))) { + if ((s = (char *) xmlGetProp(n, (const xmlChar *) "frameborder"))) { i = atoi(s); iframe->border = (i != 0); xmlFree(s); @@ -1790,21 +1751,18 @@ bool box_iframe(BOX_SPECIAL_PARAMS) xmlFree(s); } - if ((s = (char *) xmlGetProp(n, - (const xmlChar *) "scrolling"))) { + if ((s = (char *) xmlGetProp(n, (const xmlChar *) "scrolling"))) { if (!strcasecmp(s, "yes")) iframe->scrolling = SCROLLING_YES; else if (!strcasecmp(s, "no")) iframe->scrolling = SCROLLING_NO; xmlFree(s); } - if ((s = (char *) xmlGetProp(n, - (const xmlChar *) "marginwidth"))) { + if ((s = (char *) xmlGetProp(n, (const xmlChar *) "marginwidth"))) { iframe->margin_width = atoi(s); xmlFree(s); } - if ((s = (char *) xmlGetProp(n, - (const xmlChar *) "marginheight"))) { + if ((s = (char *) xmlGetProp(n, (const xmlChar *) "marginheight"))) { iframe->margin_height = atoi(s); xmlFree(s); } @@ -1835,8 +1793,7 @@ bool box_input(BOX_SPECIAL_PARAMS) type = (char *) xmlGetProp(n, (const xmlChar *) "type"); - gadget = binding_get_control_for_node(content->data.html.parser_binding, - n); + gadget = binding_get_control_for_node(content->parser_binding, n); if (!gadget) goto no_memory; box->gadget = gadget; @@ -1901,8 +1858,7 @@ bool box_input(BOX_SPECIAL_PARAMS) n->parent == NULL) != CSS_DISPLAY_NONE) { if ((s = (char *) xmlGetProp(n, (const xmlChar*) "src"))) { - res = url_join(s, - content->data.html.base_url, &url); + res = url_join(s, content->base_url, &url); xmlFree(s); /* if url is equivalent to the parent's url, * we've got infinite inclusion. stop it here @@ -1910,11 +1866,10 @@ bool box_input(BOX_SPECIAL_PARAMS) */ if (res == URL_FUNC_OK && strcasecmp(url, - content->data. - html.base_url) != 0) { + content->base_url) != 0) { if (!html_fetch_object(content, url, box, image_types, - content-> + content->base. available_width, 1000, false)) { free(url); @@ -1997,8 +1952,7 @@ bool box_button(BOX_SPECIAL_PARAMS) { struct form_control *gadget; - gadget = binding_get_control_for_node(content->data.html.parser_binding, - n); + gadget = binding_get_control_for_node(content->parser_binding, n); if (!gadget) return false; @@ -2024,8 +1978,7 @@ bool box_select(BOX_SPECIAL_PARAMS) struct form_control *gadget; xmlNode *c, *c2; - gadget = binding_get_control_for_node(content->data.html.parser_binding, - n); + gadget = binding_get_control_for_node(content->parser_binding, n); if (!gadget) return false; @@ -2171,8 +2124,7 @@ bool box_textarea(BOX_SPECIAL_PARAMS) size_t len; box->type = BOX_INLINE_BLOCK; - box->gadget = binding_get_control_for_node( - content->data.html.parser_binding, n); + box->gadget = binding_get_control_for_node(content->parser_binding, n); if (!box->gadget) return false; box->gadget->box = box; @@ -2301,15 +2253,14 @@ bool box_embed(BOX_SPECIAL_PARAMS) /* src is a URL */ if (!(src = xmlGetProp(n, (const xmlChar *) "src"))) return true; - if (!box_extract_link((char *) src, content->data.html.base_url, - ¶ms->data)) + if (!box_extract_link((char *) src, content->base_url, ¶ms->data)) return false; xmlFree(src); if (!params->data) return true; /* Don't include ourself */ - if (strcmp(content->data.html.base_url, params->data) == 0) + if (strcmp(content->base_url, params->data) == 0) return true; /* add attributes as parameters to linked list */ @@ -2339,8 +2290,8 @@ bool box_embed(BOX_SPECIAL_PARAMS) box->object_params = params; /* start fetch */ - return html_fetch_object(content, params->data, box, 0, - content->available_width, 1000, false); + return html_fetch_object(content, params->data, box, CONTENT_ANY, + content->base.available_width, 1000, false); } /** diff --git a/render/box_normalise.c b/render/box_normalise.c index 21ede6dd4..f7154c5f3 100644 --- a/render/box_normalise.c +++ b/render/box_normalise.c @@ -28,6 +28,7 @@ #include "css/css.h" #include "css/select.h" #include "render/box.h" +#include "render/html_internal.h" #include "render/table.h" #include "desktop/gui.h" #include "utils/log.h" @@ -61,19 +62,19 @@ struct columns { }; -static bool box_normalise_table(struct box *table, struct content *c); +static bool box_normalise_table(struct box *table, html_content *c); static bool box_normalise_table_spans(struct box *table, - struct span_info *spans, struct content *c); + struct span_info *spans, html_content *c); static bool box_normalise_table_row_group(struct box *row_group, struct columns *col_info, - struct content *c); + html_content *c); static bool box_normalise_table_row(struct box *row, struct columns *col_info, - struct content *c); + html_content *c); static bool calculate_table_row(struct columns *col_info, unsigned int col_span, unsigned int row_span, unsigned int *start_column); -static bool box_normalise_inline_container(struct box *cont, struct content *c); +static bool box_normalise_inline_container(struct box *cont, html_content *c); /** * Ensure the box tree is correctly nested by adding and removing nodes. @@ -96,12 +97,13 @@ static bool box_normalise_inline_container(struct box *cont, struct content *c); * \endcode */ -bool box_normalise_block(struct box *block, struct content *c) +bool box_normalise_block(struct box *block, html_content *c) { struct box *child; struct box *next_child; struct box *table; css_computed_style *style; + nscss_select_ctx ctx; assert(block != NULL); @@ -152,7 +154,11 @@ bool box_normalise_block(struct box *block, struct content *c) /* insert implied table */ assert(block->style != NULL); - style = nscss_get_blank_style(c, block->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, block->style, box_style_alloc, NULL); if (style == NULL) return false; @@ -203,13 +209,14 @@ bool box_normalise_block(struct box *block, struct content *c) } -bool box_normalise_table(struct box *table, struct content * c) +bool box_normalise_table(struct box *table, html_content * c) { struct box *child; struct box *next_child; struct box *row_group; css_computed_style *style; struct columns col_info; + nscss_select_ctx ctx; assert(table != NULL); assert(table->type == BOX_TABLE); @@ -248,7 +255,11 @@ bool box_normalise_table(struct box *table, struct content * c) /* insert implied table row group */ assert(table->style != NULL); - style = nscss_get_blank_style(c, table->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, table->style, box_style_alloc, NULL); if (style == NULL) { free(col_info.spans); @@ -330,7 +341,11 @@ bool box_normalise_table(struct box *table, struct content * c) assert(table->style != NULL); - style = nscss_get_blank_style(c, table->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, table->style, box_style_alloc, NULL); if (style == NULL) { free(col_info.spans); @@ -346,7 +361,7 @@ bool box_normalise_table(struct box *table, struct content * c) } row_group->type = BOX_TABLE_ROW_GROUP; - style = nscss_get_blank_style(c, row_group->style, + style = nscss_get_blank_style(&ctx, row_group->style, box_style_alloc, NULL); if (style == NULL) { box_free(row_group); @@ -402,13 +417,14 @@ bool box_normalise_table(struct box *table, struct content * c) */ bool box_normalise_table_spans(struct box *table, struct span_info *spans, - struct content *c) + html_content *c) { struct box *table_row_group; struct box *table_row; struct box *table_cell; unsigned int rows_left = table->rows; unsigned int col; + nscss_select_ctx ctx; /* Clear span data */ memset(spans, 0, table->columns * sizeof(struct span_info)); @@ -459,7 +475,12 @@ bool box_normalise_table_spans(struct box *table, struct span_info *spans, col++; } - style = nscss_get_blank_style(c, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == + BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, table_row->style, box_style_alloc, NULL); if (style == NULL) @@ -531,12 +552,13 @@ bool box_normalise_table_spans(struct box *table, struct span_info *spans, bool box_normalise_table_row_group(struct box *row_group, struct columns *col_info, - struct content * c) + html_content * c) { struct box *child; struct box *next_child; struct box *row; css_computed_style *style; + nscss_select_ctx ctx; assert(row_group != 0); assert(row_group->type == BOX_TABLE_ROW_GROUP); @@ -563,7 +585,11 @@ bool box_normalise_table_row_group(struct box *row_group, /* insert implied table row */ assert(row_group->style != NULL); - style = nscss_get_blank_style(c, row_group->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, row_group->style, box_style_alloc, NULL); if (style == NULL) return false; @@ -633,7 +659,11 @@ bool box_normalise_table_row_group(struct box *row_group, assert(row_group->style != NULL); - style = nscss_get_blank_style(c, row_group->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, row_group->style, box_style_alloc, NULL); if (style == NULL) { return false; @@ -664,13 +694,14 @@ bool box_normalise_table_row_group(struct box *row_group, bool box_normalise_table_row(struct box *row, struct columns *col_info, - struct content * c) + html_content * c) { struct box *child; struct box *next_child; struct box *cell = NULL; css_computed_style *style; unsigned int i; + nscss_select_ctx ctx; assert(row != NULL); assert(row->type == BOX_TABLE_ROW); @@ -697,7 +728,11 @@ bool box_normalise_table_row(struct box *row, /* insert implied table cell */ assert(row->style != NULL); - style = nscss_get_blank_style(c, row->style, + ctx.ctx = c->select_ctx; + ctx.quirks = (c->quirks == BINDING_QUIRKS_MODE_FULL); + ctx.base_url = c->base_url; + + style = nscss_get_blank_style(&ctx, row->style, box_style_alloc, NULL); if (style == NULL) return false; @@ -856,7 +891,7 @@ bool calculate_table_row(struct columns *col_info, } -bool box_normalise_inline_container(struct box *cont, struct content * c) +bool box_normalise_inline_container(struct box *cont, html_content * c) { struct box *child; struct box *next_child; diff --git a/render/favicon.c b/render/favicon.c deleted file mode 100644 index 6bc654e29..000000000 --- a/render/favicon.c +++ /dev/null @@ -1,275 +0,0 @@ -/* - * Copyright 2007 James Bursa <bursa@users.sourceforge.net> - * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net> - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <string.h> -#include "content/content_protected.h" -#include "content/hlcache.h" -#include "desktop/shape.h" -#include "render/favicon.h" -#include "render/html.h" -#include "utils/log.h" -#include "utils/messages.h" -#include "utils/talloc.h" -#include "utils/url.h" -#include "utils/utils.h" - -static char *favicon_get_icon_ref(struct content *c, xmlNode *html); -static nserror favicon_callback(hlcache_handle *icon, - const hlcache_event *event, void *pw); - -/** - * retrieve 1 url reference to 1 favicon - * \param html xml node of html element - * \return pointer to url; NULL for no icon; caller owns returned pointer - */ -char *favicon_get_icon_ref(struct content *c, xmlNode *html) -{ - xmlNode *node = html; - char *rel, *href, *url, *url2 = NULL; - url_func_result res; - - while (node) { - if (node->children != NULL) { /* children */ - node = node->children; - } else if (node->next != NULL) { /* siblings */ - node = node->next; - } else { /* ancestor siblings */ - while (node != NULL && node->next == NULL) - node = node->parent; - - if (node == NULL) - break; - - node = node->next; - } - - assert(node != NULL); - - if (node->type != XML_ELEMENT_NODE) - continue; - - if (strcmp((const char *) node->name, "link") == 0) { - /* rel=<space separated list, including 'icon'> */ - if ((rel = (char *) xmlGetProp(node, - (const xmlChar *) "rel")) == NULL) - continue; - - if (strcasestr(rel, "icon") == 0) { - xmlFree(rel); - continue; - } - - if (strcasecmp(rel, "apple-touch-icon") == 0) { - xmlFree(rel); - continue; - } - - xmlFree(rel); - - if ((href = (char *) xmlGetProp(node, - (const xmlChar *) "href")) == NULL) - continue; - - res = url_join(href, c->data.html.base_url, &url); - - xmlFree(href); - - if (res != URL_FUNC_OK) - continue; - - if (url2 != NULL) { - free(url2); - url2 = NULL; - } - - res = url_normalize(url, &url2); - - free(url); - - if (res != URL_FUNC_OK) { - url2 = NULL; - - if (res == URL_FUNC_NOMEM) - return NULL; - - continue; - } - - } - } - - if (url2 == NULL) { - char *scheme; - - /* There was no icon link defined in the HTML source data. - * If the HTML document's base URL uses either the HTTP or - * HTTPS schemes, then try using "<scheme>://host/favicon.ico" - */ - if (url_scheme(c->data.html.base_url, &scheme) != URL_FUNC_OK) - return NULL; - - if (strcasecmp(scheme, "http") != 0 && - strcasecmp(scheme, "https") != 0) { - free(scheme); - return NULL; - } - - free(scheme); - - if (url_join("/favicon.ico", c->data.html.base_url, &url2) - != URL_FUNC_OK) - return NULL; - } - - return url2; -} - -/** - * retrieve 1 favicon - * \param c content structure - * \param html xml node of html element - * \return true for success, false for error - */ - -bool favicon_get_icon(struct content *c, xmlNode *html) -{ - static const content_type permitted_types[] = { -#ifdef WITH_BMP - CONTENT_ICO, -#endif -#if defined(WITH_MNG) || defined(WITH_PNG) - CONTENT_PNG, -#endif -#ifdef WITH_GIF - CONTENT_GIF, -#endif - CONTENT_UNKNOWN - }; - char *url; - nserror error; - - url = favicon_get_icon_ref(c, html); - if (url == NULL) - return false; - - error = hlcache_handle_retrieve(url, LLCACHE_RETRIEVE_NO_ERROR_PAGES, - content__get_url(c), NULL, favicon_callback, c, NULL, - permitted_types, &c->data.html.favicon); - - if (error == NSERROR_OK) { - c->active += 1; - } - - free(url); - - return error == NSERROR_OK; -} - -/** - * Callback for fetchcache() for linked favicon - */ - -nserror favicon_callback(hlcache_handle *icon, - const hlcache_event *event, void *pw) -{ - struct content *c = pw; - bool consider_done = false, consider_redraw = false; - - switch (event->type) { - case CONTENT_MSG_LOADING: - /* check that the favicon is really a correct image type */ - if (content_get_type(icon) == CONTENT_UNKNOWN) { - union content_msg_data msg_data; - - LOG(("%s is not a favicon", content_get_url(icon))); - - hlcache_handle_abort(icon); - hlcache_handle_release(icon); - c->data.html.favicon = NULL; - c->active -= 1; - consider_done = true; - - content_add_error(c, "NotFavIco", 0); - - msg_data.error = messages_get("NotFavIco"); - content_broadcast(c, CONTENT_MSG_STATUS, msg_data); - } - break; - - case CONTENT_MSG_READY: - break; - case CONTENT_MSG_DONE: - c->active -= 1; - consider_done = true; - break; - - case CONTENT_MSG_ERROR: - LOG(("favicon %s failed: %s", - content_get_url(icon), event->data.error)); - hlcache_handle_release(c->data.html.favicon); - c->data.html.favicon = NULL; - - content_add_error(c, "?", 0); - - c->active -= 1; - consider_done = true; - break; - - case CONTENT_MSG_STATUS: - content_broadcast(c, CONTENT_MSG_STATUS, event->data); - break; - - case CONTENT_MSG_REDRAW: - /* Fall through */ - case CONTENT_MSG_REFRESH: - /* Fall through */ - case CONTENT_MSG_REFORMAT: - consider_redraw = true; - break; - - default: - assert(0); - } - -#ifdef WITH_GIF - if (consider_redraw && (c->data.html.favicon != NULL) && - (content_get_type(c->data.html.favicon) == - CONTENT_GIF)) { - union content_msg_data msg_data; - struct rect clip; - /* This is needed in order to cause animated GIFs to update - * their bitmap */ - clip.x0 = clip.y0 = 0; - clip.x1 = clip.y1 = 0; - content_redraw(c->data.html.favicon, 0, 0, -1, -1, &clip, - 1.0, 0); - content_broadcast(c, CONTENT_MSG_FAVICON_REFRESH, msg_data); - } -#endif - - if (consider_done && (c->active == 0)) { - /* all objects have arrived */ - content__reformat(c, c->available_width, c->height); - html_set_status(c, ""); - content_set_done(c); - } - - - return NSERROR_OK; -} diff --git a/render/favicon.h b/render/favicon.h deleted file mode 100644 index 428655ecd..000000000 --- a/render/favicon.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net> - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef _NETSURF_RENDER_FAVICON_H_ -#define _NETSURF_RENDER_FAVICON_H_ - -#include <libxml/tree.h> - -struct content; - -bool favicon_get_icon(struct content *c, xmlNode *html); - -#endif diff --git a/render/form.c b/render/form.c index 8a52a4fdb..955c208e8 100644 --- a/render/form.c +++ b/render/form.c @@ -1466,7 +1466,6 @@ void form_submit(hlcache_handle *h, struct browser_window *target, url_func_result res; assert(form != NULL); - assert(content_get_type(h) == CONTENT_HTML); if (form_successful_controls(form, submit_button, &success) == false) { warn_user("NoMemory", 0); diff --git a/render/html.c b/render/html.c index cf9fdea81..aa905802e 100644 --- a/render/html.c +++ b/render/html.c @@ -36,10 +36,9 @@ #include "desktop/options.h" #include "image/bitmap.h" #include "render/box.h" -#include "render/favicon.h" #include "render/font.h" #include "render/form.h" -#include "render/html.h" +#include "render/html_internal.h" #include "render/imagemap.h" #include "render/layout.h" #include "utils/http.h" @@ -58,13 +57,32 @@ #define ALWAYS_DUMP_FRAMESET 0 #define ALWAYS_DUMP_BOX 0 -static void html_finish_conversion(struct content *c); +static nserror html_create(const content_handler *handler, + lwc_string *imime_type, const http_parameter *params, + llcache_handle *llcache, const char *fallback_charset, + bool quirks, struct content **c); +static nserror html_create_html_data(html_content *c, + const http_parameter *params); +static bool html_process_data(struct content *c, const char *data, + unsigned int size); +static bool html_convert(struct content *c); +static void html_reformat(struct content *c, int width, int height); +static void html_destroy(struct content *c); +static void html_stop(struct content *c); +static void html_open(struct content *c, struct browser_window *bw, + struct content *page, struct box *box, + struct object_params *params); +static void html_close(struct content *c); +static nserror html_clone(const struct content *old, struct content **newc); +static content_type html_content_type(lwc_string *mime_type); + +static void html_finish_conversion(html_content *c); static nserror html_convert_css_callback(hlcache_handle *css, const hlcache_event *event, void *pw); -static bool html_meta_refresh(struct content *c, xmlNode *head); -static bool html_head(struct content *c, xmlNode *head); -static bool html_find_stylesheets(struct content *c, xmlNode *html); -static bool html_process_style_element(struct content *c, unsigned int *index, +static bool html_meta_refresh(html_content *c, xmlNode *head); +static bool html_head(html_content *c, xmlNode *head); +static bool html_find_stylesheets(html_content *c, xmlNode *html); +static bool html_process_style_element(html_content *c, unsigned int *index, xmlNode *style); static void html_inline_style_done(struct content_css_data *css, void *pw); static bool html_replace_object(struct content_html_object *object, @@ -73,10 +91,8 @@ static nserror html_object_callback(hlcache_handle *object, const hlcache_event *event, void *pw); static void html_object_done(struct box *box, hlcache_handle *object, bool background); -static void html_object_failed(struct box *box, struct content *content, +static void html_object_failed(struct box *box, html_content *content, bool background); -static bool html_object_type_permitted(const content_type type, - const content_type *permitted_types); static void html_object_refresh(void *p); static void html_destroy_frameset(struct content_html_frames *frameset); static void html_destroy_iframe(struct content_html_iframe *iframe); @@ -85,6 +101,25 @@ static void html_dump_frameset(struct content_html_frames *frame, unsigned int depth); #endif +static const content_handler html_content_handler = { + html_create, + html_process_data, + html_convert, + html_reformat, + html_destroy, + html_stop, + html_mouse_track, + html_mouse_action, + html_redraw, + NULL, + html_open, + html_close, + html_clone, + NULL, + html_content_type, + true +}; + static const char empty_document[] = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"" " \"http://www.w3.org/TR/html4/strict.dtd\">" @@ -98,6 +133,52 @@ static const char empty_document[] = "</body>" "</html>"; +static const char *html_types[] = { + "application/xhtml+xml", + "text/html" +}; + +static lwc_string *html_mime_types[NOF_ELEMENTS(html_types)]; + +nserror html_init(void) +{ + uint32_t i; + lwc_error lerror; + nserror error; + + for (i = 0; i < NOF_ELEMENTS(html_mime_types); i++) { + lerror = lwc_intern_string(html_types[i], + strlen(html_types[i]), + &html_mime_types[i]); + if (lerror != lwc_error_ok) { + error = NSERROR_NOMEM; + goto error; + } + + error = content_factory_register_handler(html_mime_types[i], + &html_content_handler); + if (error != NSERROR_OK) + goto error; + } + + return NSERROR_OK; + +error: + html_fini(); + + return error; +} + +void html_fini(void) +{ + uint32_t i; + + for (i = 0; i < NOF_ELEMENTS(html_mime_types); i++) { + if (html_mime_types[i] != NULL) + lwc_string_unref(html_mime_types[i]); + } +} + /** * Create a CONTENT_HTML. * @@ -105,72 +186,104 @@ static const char empty_document[] = * created. */ -bool html_create(struct content *c, const http_parameter *params) +nserror html_create(const content_handler *handler, + lwc_string *imime_type, const http_parameter *params, + llcache_handle *llcache, const char *fallback_charset, + bool quirks, struct content **c) +{ + html_content *html; + nserror error; + + html = talloc_zero(0, html_content); + if (html == NULL) + return NSERROR_NOMEM; + + error = content__init(&html->base, handler, imime_type, params, + llcache, fallback_charset, quirks); + if (error != NSERROR_OK) { + talloc_free(html); + return error; + } + + error = html_create_html_data(html, params); + if (error != NSERROR_OK) { + talloc_free(html); + return error; + } + + *c = (struct content *) html; + + return NSERROR_OK; +} + +nserror html_create_html_data(html_content *c, const http_parameter *params) { - struct content_html_data *html = &c->data.html; const char *charset; union content_msg_data msg_data; binding_error error; nserror nerror; - html->parser_binding = NULL; - html->document = NULL; - html->quirks = BINDING_QUIRKS_MODE_NONE; - html->encoding = NULL; - html->base_url = (char *) content__get_url(c); - html->base_target = NULL; - html->layout = NULL; - html->background_colour = NS_TRANSPARENT; - html->stylesheet_count = 0; - html->stylesheets = NULL; - html->select_ctx = NULL; - html->num_objects = 0; - html->object_list = NULL; - html->forms = NULL; - html->imagemaps = NULL; - html->bw = NULL; - html->frameset = NULL; - html->iframe = NULL; - html->page = NULL; - html->box = NULL; - html->font_func = &nsfont; + c->parser_binding = NULL; + c->document = NULL; + c->quirks = BINDING_QUIRKS_MODE_NONE; + c->encoding = NULL; + c->base_url = (char *) content__get_url(&c->base); + c->base_target = NULL; + c->layout = NULL; + c->background_colour = NS_TRANSPARENT; + c->stylesheet_count = 0; + c->stylesheets = NULL; + c->select_ctx = NULL; + c->num_objects = 0; + c->object_list = NULL; + c->forms = NULL; + c->imagemaps = NULL; + c->bw = NULL; + c->frameset = NULL; + c->iframe = NULL; + c->page = NULL; + c->box = NULL; + c->font_func = &nsfont; nerror = http_parameter_list_find_item(params, "charset", &charset); if (nerror == NSERROR_OK) { - html->encoding = talloc_strdup(c, charset); - if (!html->encoding) { + c->encoding = talloc_strdup(c, charset); + if (!c->encoding) { error = BINDING_NOMEM; goto error; } - html->encoding_source = ENCODING_SOURCE_HEADER; + c->encoding_source = ENCODING_SOURCE_HEADER; } /* Create the parser binding */ - error = binding_create_tree(c, html->encoding, &html->parser_binding); - if (error == BINDING_BADENCODING && html->encoding != NULL) { + error = binding_create_tree(c, c->encoding, &c->parser_binding); + if (error == BINDING_BADENCODING && c->encoding != NULL) { /* Ok, we don't support the declared encoding. Bailing out * isn't exactly user-friendly, so fall back to autodetect */ - talloc_free(html->encoding); - html->encoding = NULL; + talloc_free(c->encoding); + c->encoding = NULL; - error = binding_create_tree(c, html->encoding, - &html->parser_binding); + error = binding_create_tree(c, c->encoding, &c->parser_binding); } if (error != BINDING_OK) goto error; - return true; + return NSERROR_OK; error: if (error == BINDING_BADENCODING) { - LOG(("Bad encoding: %s", html->encoding ? html->encoding : "")); + LOG(("Bad encoding: %s", c->encoding ? c->encoding : "")); msg_data.error = messages_get("ParsingFail"); - } else + nerror = NSERROR_BAD_ENCODING; + } else { msg_data.error = messages_get("NoMemory"); + nerror = NSERROR_NOMEM; + } - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - return false; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + + return nerror; } @@ -182,12 +295,13 @@ error: bool html_process_data(struct content *c, const char *data, unsigned int size) { + html_content *html = (html_content *) c; unsigned long x; binding_error err; const char *encoding; for (x = 0; x + CHUNK <= size; x += CHUNK) { - err = binding_parse_chunk(c->data.html.parser_binding, + err = binding_parse_chunk(html->parser_binding, (const uint8_t *) data + x, CHUNK); if (err == BINDING_ENCODINGCHANGE) { goto encoding_change; @@ -203,7 +317,7 @@ bool html_process_data(struct content *c, const char *data, unsigned int size) gui_multitask(); } - err = binding_parse_chunk(c->data.html.parser_binding, + err = binding_parse_chunk(html->parser_binding, (const uint8_t *) data + x, (size - x)); if (err == BINDING_ENCODINGCHANGE) { goto encoding_change; @@ -222,14 +336,14 @@ encoding_change: /* Retrieve new encoding */ encoding = binding_get_encoding( - c->data.html.parser_binding, - &c->data.html.encoding_source); + html->parser_binding, + &html->encoding_source); - if (c->data.html.encoding != NULL) - talloc_free(c->data.html.encoding); + if (html->encoding != NULL) + talloc_free(html->encoding); - c->data.html.encoding = talloc_strdup(c, encoding); - if (c->data.html.encoding == NULL) { + html->encoding = talloc_strdup(c, encoding); + if (html->encoding == NULL) { union content_msg_data msg_data; msg_data.error = messages_get("NoMemory"); @@ -238,17 +352,16 @@ encoding_change: } /* Destroy binding */ - binding_destroy_tree(c->data.html.parser_binding); + binding_destroy_tree(html->parser_binding); /* Create new binding, using the new encoding */ - err = binding_create_tree(c, c->data.html.encoding, - &c->data.html.parser_binding); + err = binding_create_tree(html, html->encoding, &html->parser_binding); if (err == BINDING_BADENCODING) { /* Ok, we don't support the declared encoding. Bailing out * isn't exactly user-friendly, so fall back to Windows-1252 */ - talloc_free(c->data.html.encoding); - c->data.html.encoding = talloc_strdup(c, "Windows-1252"); - if (c->data.html.encoding == NULL) { + talloc_free(html->encoding); + html->encoding = talloc_strdup(c, "Windows-1252"); + if (html->encoding == NULL) { union content_msg_data msg_data; msg_data.error = messages_get("NoMemory"); @@ -256,16 +369,16 @@ encoding_change: return false; } - err = binding_create_tree(c, c->data.html.encoding, - &c->data.html.parser_binding); + err = binding_create_tree(html, html->encoding, + &html->parser_binding); } if (err != BINDING_OK) { union content_msg_data msg_data; if (err == BINDING_BADENCODING) { - LOG(("Bad encoding: %s", c->data.html.encoding - ? c->data.html.encoding : "")); + LOG(("Bad encoding: %s", html->encoding + ? html->encoding : "")); msg_data.error = messages_get("ParsingFail"); } else msg_data.error = messages_get("NoMemory"); @@ -293,7 +406,6 @@ encoding_change: * * - parsing to an XML tree is completed * - stylesheets are fetched - * - favicon is retrieved * - the XML tree is converted to a box tree and object fetches are started * * On exit, the content status will be either CONTENT_STATUS_DONE if the @@ -303,6 +415,7 @@ encoding_change: bool html_convert(struct content *c) { + html_content *htmlc = (html_content *) c; binding_error err; xmlNode *html, *head; union content_msg_data msg_data; @@ -313,23 +426,22 @@ bool html_convert(struct content *c) content__get_source_data(c, &size); if (size == 0) { /* Destroy current binding */ - binding_destroy_tree(c->data.html.parser_binding); + binding_destroy_tree(htmlc->parser_binding); /* Also, any existing encoding information, * as it's not guaranteed to match the error page. */ - talloc_free(c->data.html.encoding); - c->data.html.encoding = NULL; + talloc_free(htmlc->encoding); + htmlc->encoding = NULL; /* Create new binding, using default charset */ - err = binding_create_tree(c, NULL, - &c->data.html.parser_binding); + err = binding_create_tree(c, NULL, &htmlc->parser_binding); if (err != BINDING_OK) { union content_msg_data msg_data; if (err == BINDING_BADENCODING) { - LOG(("Bad encoding: %s", c->data.html.encoding - ? c->data.html.encoding : "")); + LOG(("Bad encoding: %s", htmlc->encoding + ? htmlc->encoding : "")); msg_data.error = messages_get("ParsingFail"); } else msg_data.error = messages_get("NoMemory"); @@ -343,7 +455,7 @@ bool html_convert(struct content *c) return false; } - err = binding_parse_completed(c->data.html.parser_binding); + err = binding_parse_completed(htmlc->parser_binding); if (err != BINDING_OK) { union content_msg_data msg_data; @@ -353,25 +465,24 @@ bool html_convert(struct content *c) return false; } - c->data.html.document = - binding_get_document(c->data.html.parser_binding, - &c->data.html.quirks); - /*xmlDebugDumpDocument(stderr, c->data.html.document);*/ + htmlc->document =binding_get_document(htmlc->parser_binding, + &htmlc->quirks); + /*xmlDebugDumpDocument(stderr, htmlc->document);*/ - if (!c->data.html.document) { + if (!htmlc->document) { LOG(("Parsing failed")); msg_data.error = messages_get("ParsingFail"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } - if (c->data.html.encoding == NULL) { + if (htmlc->encoding == NULL) { const char *encoding = binding_get_encoding( - c->data.html.parser_binding, - &c->data.html.encoding_source); + htmlc->parser_binding, + &htmlc->encoding_source); - c->data.html.encoding = talloc_strdup(c, encoding); - if (c->data.html.encoding == NULL) { + htmlc->encoding = talloc_strdup(c, encoding); + if (htmlc->encoding == NULL) { msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; @@ -379,7 +490,7 @@ bool html_convert(struct content *c) } /* locate html and head elements */ - html = xmlDocGetRootElement(c->data.html.document); + html = xmlDocGetRootElement(htmlc->document); if (html == 0 || strcmp((const char *) html->name, "html") != 0) { LOG(("html element not found")); msg_data.error = messages_get("ParsingFail"); @@ -396,20 +507,20 @@ bool html_convert(struct content *c) } if (head) { - if (!html_head(c, head)) { + if (!html_head(htmlc, head)) { msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, msg_data); return false; } /* handle meta refresh */ - if (!html_meta_refresh(c, head)) + if (!html_meta_refresh(htmlc, head)) return false; } /* Retrieve forms from parser */ - c->data.html.forms = binding_get_forms(c->data.html.parser_binding); - for (f = c->data.html.forms; f != NULL; f = f->prev) { + htmlc->forms = binding_get_forms(htmlc->parser_binding); + for (f = htmlc->forms; f != NULL; f = f->prev) { char *action; url_func_result res; @@ -417,10 +528,9 @@ bool html_convert(struct content *c) if (f->action == NULL || f->action[0] == '\0') { /* HTML5 4.10.22.3 step 11 */ res = url_join(content__get_url(c), - c->data.html.base_url, &action); + htmlc->base_url, &action); } else { - res = url_join(f->action, - c->data.html.base_url, &action); + res = url_join(f->action, htmlc->base_url, &action); } if (res != URL_FUNC_OK) { @@ -434,7 +544,7 @@ bool html_convert(struct content *c) /* Ensure each form has a document encoding */ if (f->document_charset == NULL) { - f->document_charset = strdup(c->data.html.encoding); + f->document_charset = strdup(htmlc->encoding); if (f->document_charset == NULL) { msg_data.error = messages_get("NoMemory"); content_broadcast(c, CONTENT_MSG_ERROR, @@ -445,7 +555,7 @@ bool html_convert(struct content *c) } /* get stylesheets */ - if (!html_find_stylesheets(c, html)) + if (!html_find_stylesheets(htmlc, html)) return false; return true; @@ -456,37 +566,36 @@ bool html_convert(struct content *c) * * \param c Content to convert */ -void html_finish_conversion(struct content *c) +void html_finish_conversion(html_content *c) { union content_msg_data msg_data; xmlNode *html; uint32_t i; css_error error; - html = xmlDocGetRootElement(c->data.html.document); + html = xmlDocGetRootElement(c->document); assert(html != NULL); /* check that the base stylesheet loaded; layout fails without it */ - if (c->data.html.stylesheets[STYLESHEET_BASE].data.external == NULL) { + if (c->stylesheets[STYLESHEET_BASE].data.external == NULL) { msg_data.error = "Base stylesheet failed to load"; - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - c->status = CONTENT_STATUS_ERROR; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + c->base.status = CONTENT_STATUS_ERROR; return; } /* Create selection context */ - error = css_select_ctx_create(ns_realloc, c, &c->data.html.select_ctx); + error = css_select_ctx_create(ns_realloc, c, &c->select_ctx); if (error != CSS_OK) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - c->status = CONTENT_MSG_ERROR; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + c->base.status = CONTENT_MSG_ERROR; return; } /* Add sheets to it */ - for (i = STYLESHEET_BASE; i != c->data.html.stylesheet_count; i++) { - const struct html_stylesheet *hsheet = - &c->data.html.stylesheets[i]; + for (i = STYLESHEET_BASE; i != c->stylesheet_count; i++) { + const struct html_stylesheet *hsheet = &c->stylesheets[i]; css_stylesheet *sheet; css_origin origin = CSS_ORIGIN_AUTHOR; @@ -495,10 +604,7 @@ void html_finish_conversion(struct content *c) if (hsheet->type == HTML_STYLESHEET_EXTERNAL && hsheet->data.external != NULL) { - struct content *s = hlcache_handle_get_content( - hsheet->data.external); - - sheet = s->data.css.sheet; + sheet = nscss_get_stylesheet(hsheet->data.external); } else if (hsheet->type == HTML_STYLESHEET_INTERNAL) { sheet = hsheet->data.internal->sheet; } else { @@ -507,59 +613,54 @@ void html_finish_conversion(struct content *c) if (sheet != NULL) { error = css_select_ctx_append_sheet( - c->data.html.select_ctx, sheet, + c->select_ctx, sheet, origin, CSS_MEDIA_SCREEN); if (error != CSS_OK) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); - c->status = CONTENT_STATUS_ERROR; + c->base.status = CONTENT_STATUS_ERROR; return; } } } -#ifdef FAVICONS_ARE_NOT_A_PERFORMANCE_HOLE - /* get icon */ - favicon_get_icon(c, html); -#endif - /* convert xml tree to box tree */ LOG(("XML to box")); - content_set_status(c, messages_get("Processing")); - content_broadcast(c, CONTENT_MSG_STATUS, msg_data); + content_set_status(&c->base, messages_get("Processing")); + content_broadcast(&c->base, CONTENT_MSG_STATUS, msg_data); if (!xml_to_box(html, c)) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - c->status = CONTENT_STATUS_ERROR; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + c->base.status = CONTENT_STATUS_ERROR; return; } #if ALWAYS_DUMP_BOX - box_dump(stderr, c->data.html.layout->children, 0); + box_dump(stderr, c->layout->children, 0); #endif #if ALWAYS_DUMP_FRAMESET - if (c->data.html.frameset) - html_dump_frameset(c->data.html.frameset, 0); + if (c->frameset) + html_dump_frameset(c->frameset, 0); #endif /* extract image maps - can't do this sensibly in xml_to_box */ if (!imagemap_extract(html, c)) { LOG(("imagemap extraction failed")); msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - c->status = CONTENT_STATUS_ERROR; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + c->base.status = CONTENT_STATUS_ERROR; return; } /*imagemap_dump(c);*/ /* Destroy the parser binding */ - binding_destroy_tree(c->data.html.parser_binding); - c->data.html.parser_binding = NULL; + binding_destroy_tree(c->parser_binding); + c->parser_binding = NULL; - content_set_ready(c); + content_set_ready(&c->base); - if (c->active == 0) - content_set_done(c); + if (c->base.active == 0) + content_set_done(&c->base); html_set_status(c, ""); } @@ -575,7 +676,7 @@ void html_finish_conversion(struct content *c) * The title and base href are extracted if present. */ -bool html_head(struct content *c, xmlNode *head) +bool html_head(html_content *c, xmlNode *head) { xmlNode *node; xmlChar *s; @@ -584,7 +685,7 @@ bool html_head(struct content *c, xmlNode *head) if (node->type != XML_ELEMENT_NODE) continue; - if (c->title == NULL && strcmp((const char *) node->name, + if (c->base.title == NULL && strcmp((const char *) node->name, "title") == 0) { xmlChar *title = xmlNodeGetContent(node); char *title2; @@ -594,7 +695,7 @@ bool html_head(struct content *c, xmlNode *head) xmlFree(title); if (!title2) return false; - if (content__set_title(c, title2) == false) { + if (content__set_title(&c->base, title2) == false) { free(title2); return false; } @@ -609,8 +710,7 @@ bool html_head(struct content *c, xmlNode *head) url_func_result res; res = url_normalize(href, &url); if (res == URL_FUNC_OK) { - c->data.html.base_url = - talloc_strdup(c, url); + c->base_url = talloc_strdup(c, url); free(url); } xmlFree(href); @@ -626,10 +726,9 @@ bool html_head(struct content *c, xmlNode *head) "_self")) || ('a' <= s[0] && s[0] <= 'z') || ('A' <= s[0] && s[0] <= 'Z')) { /* [6.16] */ - c->data.html.base_target = - talloc_strdup(c, + c->base_target = talloc_strdup(c, (const char *) s); - if (!c->data.html.base_target) { + if (!c->base_target) { xmlFree(s); return false; } @@ -652,7 +751,7 @@ bool html_head(struct content *c, xmlNode *head) * \return true on success, false otherwise (error reported) */ -bool html_meta_refresh(struct content *c, xmlNode *head) +bool html_meta_refresh(html_content *c, xmlNode *head) { xmlNode *n; xmlChar *equiv, *content; @@ -669,7 +768,7 @@ bool html_meta_refresh(struct content *c, xmlNode *head) if (!html_meta_refresh(c, n)) { /* Some error occurred */ return false; - } else if (c->refresh) { + } else if (c->base.refresh) { /* Meta refresh found - stop */ return true; } @@ -739,15 +838,17 @@ bool html_meta_refresh(struct content *c, xmlNode *head) /* Just delay specified, so refresh current page */ xmlFree(content); - c->refresh = talloc_strdup(c, content__get_url(c)); - if (!c->refresh) { + c->base.refresh = talloc_strdup(c, + content__get_url(&c->base)); + if (!c->base.refresh) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); return false; } - content_broadcast(c, CONTENT_MSG_REFRESH, msg_data); + content_broadcast(&c->base, CONTENT_MSG_REFRESH, + msg_data); break; } @@ -810,30 +911,32 @@ bool html_meta_refresh(struct content *c, xmlNode *head) if (url < end) *url = '\0'; - res = url_join(refresh, c->data.html.base_url, &refresh); + res = url_join(refresh, c->base_url, &refresh); xmlFree(content); if (res == URL_FUNC_NOMEM) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + content_broadcast(&c->base, CONTENT_MSG_ERROR, + msg_data); return false; } else if (res == URL_FUNC_FAILED) { /* This isn't fatal so carry on looking */ continue; } - c->refresh = talloc_strdup(c, refresh); + c->base.refresh = talloc_strdup(c, refresh); free(refresh); - if (!c->refresh) { + if (!c->base.refresh) { msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + content_broadcast(&c->base, CONTENT_MSG_ERROR, + msg_data); return false; } - content_broadcast(c, CONTENT_MSG_REFRESH, msg_data); + content_broadcast(&c->base, CONTENT_MSG_REFRESH, msg_data); } return true; @@ -850,9 +953,9 @@ bool html_meta_refresh(struct content *c, xmlNode *head) * \return true on success, false if an error occurred */ -bool html_find_stylesheets(struct content *c, xmlNode *html) +bool html_find_stylesheets(html_content *c, xmlNode *html) { - static const content_type accept[] = { CONTENT_CSS, CONTENT_UNKNOWN }; + content_type accept = CONTENT_CSS; xmlNode *node; char *rel, *type, *media, *href, *url, *url2; unsigned int i = STYLESHEET_START; @@ -862,61 +965,57 @@ bool html_find_stylesheets(struct content *c, xmlNode *html) hlcache_child_context child; nserror ns_error; - child.charset = c->data.html.encoding; - child.quirks = c->quirks; + child.charset = c->encoding; + child.quirks = c->base.quirks; /* stylesheet 0 is the base style sheet, * stylesheet 1 is the quirks mode style sheet, * stylesheet 2 is the adblocking stylesheet */ - c->data.html.stylesheets = talloc_array(c, struct html_stylesheet, + c->stylesheets = talloc_array(c, struct html_stylesheet, STYLESHEET_START); - if (c->data.html.stylesheets == NULL) + if (c->stylesheets == NULL) goto no_memory; - c->data.html.stylesheets[STYLESHEET_BASE].type = - HTML_STYLESHEET_EXTERNAL; - c->data.html.stylesheets[STYLESHEET_BASE].data.external = NULL; - c->data.html.stylesheets[STYLESHEET_QUIRKS].type = - HTML_STYLESHEET_EXTERNAL; - c->data.html.stylesheets[STYLESHEET_QUIRKS].data.external = NULL; - c->data.html.stylesheets[STYLESHEET_ADBLOCK].type = - HTML_STYLESHEET_EXTERNAL; - c->data.html.stylesheets[STYLESHEET_ADBLOCK].data.external = NULL; - c->data.html.stylesheet_count = STYLESHEET_START; - - c->active = 0; + c->stylesheets[STYLESHEET_BASE].type = HTML_STYLESHEET_EXTERNAL; + c->stylesheets[STYLESHEET_BASE].data.external = NULL; + c->stylesheets[STYLESHEET_QUIRKS].type = HTML_STYLESHEET_EXTERNAL; + c->stylesheets[STYLESHEET_QUIRKS].data.external = NULL; + c->stylesheets[STYLESHEET_ADBLOCK].type = HTML_STYLESHEET_EXTERNAL; + c->stylesheets[STYLESHEET_ADBLOCK].data.external = NULL; + c->stylesheet_count = STYLESHEET_START; + + c->base.active = 0; ns_error = hlcache_handle_retrieve(default_stylesheet_url, 0, - content__get_url(c), NULL, + content__get_url(&c->base), NULL, html_convert_css_callback, c, &child, accept, - &c->data.html.stylesheets[ - STYLESHEET_BASE].data.external); + &c->stylesheets[STYLESHEET_BASE].data.external); if (ns_error != NSERROR_OK) goto no_memory; - c->active++; + c->base.active++; - if (c->data.html.quirks == BINDING_QUIRKS_MODE_FULL) { + if (c->quirks == BINDING_QUIRKS_MODE_FULL) { ns_error = hlcache_handle_retrieve(quirks_stylesheet_url, 0, - content__get_url(c), NULL, + content__get_url(&c->base), NULL, html_convert_css_callback, c, &child, accept, - &c->data.html.stylesheets[ - STYLESHEET_QUIRKS].data.external); + &c->stylesheets[STYLESHEET_QUIRKS]. + data.external); if (ns_error != NSERROR_OK) goto no_memory; - c->active++; + c->base.active++; } if (option_block_ads) { ns_error = hlcache_handle_retrieve(adblock_stylesheet_url, 0, - content__get_url(c), NULL, + content__get_url(&c->base), NULL, html_convert_css_callback, c, &child, accept, - &c->data.html.stylesheets[ - STYLESHEET_ADBLOCK].data.external); + &c->stylesheets[STYLESHEET_ADBLOCK]. + data.external); if (ns_error != NSERROR_OK) goto no_memory; - c->active++; + c->base.active++; } node = html; @@ -986,7 +1085,7 @@ bool html_find_stylesheets(struct content *c, xmlNode *html) * those with a title attribute) should be loaded * (see HTML4 14.3) */ - res = url_join(href, c->data.html.base_url, &url); + res = url_join(href, c->base_url, &url); xmlFree(href); if (res != URL_FUNC_OK) continue; @@ -1005,30 +1104,28 @@ bool html_find_stylesheets(struct content *c, xmlNode *html) /* start fetch */ stylesheets = talloc_realloc(c, - c->data.html.stylesheets, + c->stylesheets, struct html_stylesheet, i + 1); if (stylesheets == NULL) { free(url2); goto no_memory; } - c->data.html.stylesheets = stylesheets; - c->data.html.stylesheet_count++; - c->data.html.stylesheets[i].type = - HTML_STYLESHEET_EXTERNAL; + c->stylesheets = stylesheets; + c->stylesheet_count++; + c->stylesheets[i].type = HTML_STYLESHEET_EXTERNAL; ns_error = hlcache_handle_retrieve(url2, 0, - content__get_url(c), NULL, + content__get_url(&c->base), NULL, html_convert_css_callback, c, &child, accept, - &c->data.html.stylesheets[i]. - data.external); + &c->stylesheets[i].data.external); free(url2); if (ns_error != NSERROR_OK) goto no_memory; - c->active++; + c->base.active++; i++; } else if (strcmp((const char *) node->name, "style") == 0) { @@ -1037,13 +1134,13 @@ bool html_find_stylesheets(struct content *c, xmlNode *html) } } - assert(c->data.html.stylesheet_count == i); + assert(c->stylesheet_count == i); return true; no_memory: msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); return false; } @@ -1058,7 +1155,7 @@ no_memory: * \return true on success, false if an error occurred */ -bool html_process_style_element(struct content *c, unsigned int *index, +bool html_process_style_element(html_content *c, unsigned int *index, xmlNode *style) { xmlNode *child; @@ -1088,29 +1185,29 @@ bool html_process_style_element(struct content *c, unsigned int *index, } /* Extend array */ - stylesheets = talloc_realloc(c, c->data.html.stylesheets, + stylesheets = talloc_realloc(c, c->stylesheets, struct html_stylesheet, *index + 1); if (stylesheets == NULL) goto no_memory; - c->data.html.stylesheets = stylesheets; - c->data.html.stylesheet_count++; + c->stylesheets = stylesheets; + c->stylesheet_count++; - c->data.html.stylesheets[(*index)].type = HTML_STYLESHEET_INTERNAL; - c->data.html.stylesheets[(*index)].data.internal = NULL; + c->stylesheets[(*index)].type = HTML_STYLESHEET_INTERNAL; + c->stylesheets[(*index)].data.internal = NULL; /* create stylesheet */ sheet = talloc(c, struct content_css_data); if (sheet == NULL) { - c->data.html.stylesheet_count--; + c->stylesheet_count--; goto no_memory; } error = nscss_create_css_data(sheet, - c->data.html.base_url, NULL, c->data.html.quirks, + c->base_url, NULL, c->quirks, html_inline_style_done, c); if (error != NSERROR_OK) { - c->data.html.stylesheet_count--; + c->stylesheet_count--; goto no_memory; } @@ -1124,7 +1221,7 @@ bool html_process_style_element(struct content *c, unsigned int *index, xmlFree(data); nscss_destroy_css_data(sheet); talloc_free(sheet); - c->data.html.stylesheet_count--; + c->stylesheet_count--; /** \todo not necessarily caused by * memory exhaustion */ goto no_memory; @@ -1132,26 +1229,26 @@ bool html_process_style_element(struct content *c, unsigned int *index, xmlFree(data); } - c->active++; + c->base.active++; /* Convert the content -- manually, as we want the result */ if (nscss_convert_css_data(sheet) != CSS_OK) { /* conversion failed */ - c->active--; + c->base.active--; nscss_destroy_css_data(sheet); talloc_free(sheet); sheet = NULL; } /* Update index */ - c->data.html.stylesheets[(*index)].data.internal = sheet; + c->stylesheets[(*index)].data.internal = sheet; (*index)++; return true; no_memory: msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); return false; } @@ -1163,9 +1260,9 @@ no_memory: */ void html_inline_style_done(struct content_css_data *css, void *pw) { - struct content *html = pw; + html_content *html = pw; - if (--html->active == 0) + if (--html->base.active == 0) html_finish_conversion(html); } @@ -1176,26 +1273,22 @@ void html_inline_style_done(struct content_css_data *css, void *pw) nserror html_convert_css_callback(hlcache_handle *css, const hlcache_event *event, void *pw) { - struct content *parent = pw; + html_content *parent = pw; unsigned int i; struct html_stylesheet *s; /* Find sheet */ - for (i = 0, s = parent->data.html.stylesheets; - i != parent->data.html.stylesheet_count; i++, s++) { + for (i = 0, s = parent->stylesheets; + i != parent->stylesheet_count; i++, s++) { if (s->type == HTML_STYLESHEET_EXTERNAL && s->data.external == css) break; } - assert(i != parent->data.html.stylesheet_count); + assert(i != parent->stylesheet_count); switch (event->type) { case CONTENT_MSG_LOADING: - /* check that the stylesheet is really CSS */ - if (content_get_type(css) != CONTENT_CSS) { - assert(0 && "Non-CSS type unexpected"); - } break; case CONTENT_MSG_READY: @@ -1203,7 +1296,7 @@ nserror html_convert_css_callback(hlcache_handle *css, case CONTENT_MSG_DONE: LOG(("got stylesheet '%s'", content_get_url(css))); - parent->active--; + parent->base.active--; break; case CONTENT_MSG_ERROR: @@ -1211,20 +1304,21 @@ nserror html_convert_css_callback(hlcache_handle *css, content_get_url(css), event->data.error)); hlcache_handle_release(css); s->data.external = NULL; - parent->active--; - content_add_error(parent, "?", 0); + parent->base.active--; + content_add_error(&parent->base, "?", 0); break; case CONTENT_MSG_STATUS: html_set_status(parent, content_get_status_message(css)); - content_broadcast(parent, CONTENT_MSG_STATUS, event->data); + content_broadcast(&parent->base, CONTENT_MSG_STATUS, + event->data); break; default: assert(0); } - if (parent->active == 0) + if (parent->base.active == 0) html_finish_conversion(parent); return NSERROR_OK; @@ -1237,16 +1331,15 @@ nserror html_convert_css_callback(hlcache_handle *css, * \param c content of type CONTENT_HTML * \param url URL of object to fetch (copied) * \param box box that will contain the object - * \param permitted_types array of types, terminated by CONTENT_UNKNOWN, - * or 0 if all types except OTHER and UNKNOWN acceptable + * \param permitted_types bitmap of acceptable types * \param available_width estimate of width of object * \param available_height estimate of height of object * \param background this is a background image * \return true on success, false on memory exhaustion */ -bool html_fetch_object(struct content *c, const char *url, struct box *box, - const content_type *permitted_types, +bool html_fetch_object(html_content *c, const char *url, struct box *box, + content_type permitted_types, int available_width, int available_height, bool background) { @@ -1256,8 +1349,8 @@ bool html_fetch_object(struct content *c, const char *url, struct box *box, url_func_result res; nserror error; - child.charset = c->data.html.encoding; - child.quirks = c->quirks; + child.charset = c->encoding; + child.quirks = c->base.quirks; /* Normalize the URL */ res = url_normalize(url, &url2); @@ -1272,16 +1365,16 @@ bool html_fetch_object(struct content *c, const char *url, struct box *box, return false; } - object->parent = c; + object->parent = (struct content *) c; object->next = NULL; object->content = NULL; object->box = box; object->permitted_types = permitted_types; object->background = background; - error = hlcache_handle_retrieve(url2, 0, content__get_url(c), NULL, - html_object_callback, object, &child, permitted_types, - &object->content); + error = hlcache_handle_retrieve(url2, 0, content__get_url(&c->base), + NULL, html_object_callback, object, &child, + permitted_types, &object->content); /* No longer need normalized url */ free(url2); @@ -1292,11 +1385,11 @@ bool html_fetch_object(struct content *c, const char *url, struct box *box, } /* add to object list */ - object->next = c->data.html.object_list; - c->data.html.object_list = object; + object->next = c->object_list; + c->object_list = object; - c->data.html.num_objects++; - c->active++; + c->num_objects++; + c->base.active++; return error != NSERROR_NOMEM; } @@ -1312,24 +1405,24 @@ bool html_fetch_object(struct content *c, const char *url, struct box *box, bool html_replace_object(struct content_html_object *object, const char *url) { - struct content *c; + html_content *c; hlcache_child_context child; - struct content *page; + html_content *page; char *url2; url_func_result res; nserror error; assert(object != NULL); - c = object->parent; + c = (html_content *) object->parent; - child.charset = c->data.html.encoding; - child.quirks = c->quirks; + child.charset = c->encoding; + child.quirks = c->base.quirks; if (object->content != NULL) { /* remove existing object */ if (content_get_status(object->content) != CONTENT_STATUS_DONE) - c->active--; + c->base.active--; hlcache_handle_release(object->content); object->content = NULL; @@ -1342,8 +1435,8 @@ bool html_replace_object(struct content_html_object *object, const char *url) return res != URL_FUNC_NOMEM; /* initialise fetch */ - error = hlcache_handle_retrieve(url2, 0, content__get_url(c), NULL, - html_object_callback, object, &child, + error = hlcache_handle_retrieve(url2, 0, content__get_url(&c->base), + NULL, html_object_callback, object, &child, object->permitted_types, &object->content); @@ -1352,10 +1445,9 @@ bool html_replace_object(struct content_html_object *object, const char *url) if (error != NSERROR_OK) return false; - for (page = c; page; page = page->data.html.page) { - assert(page->type == CONTENT_HTML); - page->active++; - page->status = CONTENT_STATUS_READY; + for (page = c; page != NULL; page = page->page) { + page->base.active++; + page->base.status = CONTENT_STATUS_READY; } return true; @@ -1370,7 +1462,7 @@ nserror html_object_callback(hlcache_handle *object, const hlcache_event *event, void *pw) { struct content_html_object *o = pw; - struct content *c = o->parent; + html_content *c = (html_content *) o->parent; int x, y; struct box *box; @@ -1378,50 +1470,31 @@ nserror html_object_callback(hlcache_handle *object, switch (event->type) { case CONTENT_MSG_LOADING: - /* check if the type is acceptable for this object */ - if (html_object_type_permitted(content_get_type(object), - o->permitted_types)) { - if (c->data.html.bw != NULL) - content_open(object, - c->data.html.bw, c, - box, - box->object_params); - break; - } - - /* not acceptable */ - hlcache_handle_release(object); - - o->content = NULL; - - c->active--; - - content_add_error(c, "?", 0); - html_set_status(c, messages_get("BadObject")); - content_broadcast(c, CONTENT_MSG_STATUS, event->data); - - html_object_failed(box, c, o->background); + if (c->bw != NULL) + content_open(object, + c->bw, &c->base, + box, + box->object_params); break; case CONTENT_MSG_READY: if (content_get_type(object) == CONTENT_HTML) { html_object_done(box, object, o->background); - if (c->status == CONTENT_STATUS_READY || - c->status == CONTENT_STATUS_DONE) - content__reformat(c, - c->available_width, - c->height); + if (c->base.status == CONTENT_STATUS_READY || + c->base.status == CONTENT_STATUS_DONE) + content__reformat(&c->base, + c->base.available_width, + c->base.height); } break; case CONTENT_MSG_DONE: + c->base.active--; + html_object_done(box, object, o->background); if (box->flags & REPLACE_DIM) { union content_msg_data data; - c->active--; - html_object_done(box, object, o->background); - if (!box_visible(box)) break; @@ -1433,10 +1506,7 @@ nserror html_object_callback(hlcache_handle *object, data.redraw.height = box->height; data.redraw.full_redraw = true; - content_broadcast(c, CONTENT_MSG_REDRAW, data); - } else { - c->active--; - html_object_done(box, object, o->background); + content_broadcast(&c->base, CONTENT_MSG_REDRAW, data); } break; @@ -1445,17 +1515,17 @@ nserror html_object_callback(hlcache_handle *object, o->content = NULL; - c->active--; + c->base.active--; - content_add_error(c, "?", 0); + content_add_error(&c->base, "?", 0); html_set_status(c, event->data.error); - content_broadcast(c, CONTENT_MSG_STATUS, event->data); + content_broadcast(&c->base, CONTENT_MSG_STATUS, event->data); html_object_failed(box, c, o->background); break; case CONTENT_MSG_STATUS: html_set_status(c, content_get_status_message(object)); - /* content_broadcast(c, CONTENT_MSG_STATUS, 0); */ + /* content_broadcast(&c->base, CONTENT_MSG_STATUS, 0); */ break; case CONTENT_MSG_REFORMAT: @@ -1491,7 +1561,7 @@ nserror html_object_callback(hlcache_handle *object, data.redraw.object_x += x + box->padding[LEFT]; data.redraw.object_y += y + box->padding[TOP]; - content_broadcast(c, CONTENT_MSG_REDRAW, data); + content_broadcast(&c->base, CONTENT_MSG_REDRAW, data); } break; @@ -1508,14 +1578,15 @@ nserror html_object_callback(hlcache_handle *object, assert(0); } - if (c->status == CONTENT_STATUS_READY && c->active == 0 && + if (c->base.status == CONTENT_STATUS_READY && c->base.active == 0 && (event->type == CONTENT_MSG_LOADING || event->type == CONTENT_MSG_DONE || event->type == CONTENT_MSG_ERROR)) { /* all objects have arrived */ - content__reformat(c, c->available_width, c->height); + content__reformat(&c->base, c->base.available_width, + c->base.height); html_set_status(c, ""); - content_set_done(c); + content_set_done(&c->base); } /* If 1) the configuration option to reflow pages while objects are @@ -1529,10 +1600,11 @@ nserror html_object_callback(hlcache_handle *object, else if (option_incremental_reflow && event->type == CONTENT_MSG_DONE && !(box->flags & REPLACE_DIM) && - (c->status == CONTENT_STATUS_READY || - c->status == CONTENT_STATUS_DONE) && - (wallclock() > c->reformat_time)) { - content__reformat(c, c->available_width, c->height); + (c->base.status == CONTENT_STATUS_READY || + c->base.status == CONTENT_STATUS_DONE) && + (wallclock() > c->base.reformat_time)) { + content__reformat(&c->base, c->base.available_width, + c->base.height); } return NSERROR_OK; @@ -1577,7 +1649,7 @@ void html_object_done(struct box *box, hlcache_handle *object, * \param background the object was the background image for the box */ -void html_object_failed(struct box *box, struct content *content, +void html_object_failed(struct box *box, html_content *content, bool background) { /* Nothing to do */ @@ -1586,29 +1658,6 @@ void html_object_failed(struct box *box, struct content *content, /** - * Check if a type is in a list. - * - * \param type the content_type to search for - * \param permitted_types array of types, terminated by CONTENT_UNKNOWN, - * or 0 if all types except OTHER and UNKNOWN acceptable - * \return the type is in the list or acceptable - */ - -bool html_object_type_permitted(const content_type type, - const content_type *permitted_types) -{ - if (permitted_types) { - for (; *permitted_types != CONTENT_UNKNOWN; permitted_types++) - if (*permitted_types == type) - return true; - } else if (type < CONTENT_OTHER) { - return true; - } - return false; -} - - -/** * schedule() callback for object refresh */ @@ -1639,11 +1688,12 @@ void html_object_refresh(void *p) void html_stop(struct content *c) { + html_content *htmlc = (html_content *) c; struct content_html_object *object; assert(c->status == CONTENT_STATUS_READY); - for (object = c->data.html.object_list; object != NULL; + for (object = htmlc->object_list; object != NULL; object = object->next) { if (object->content == NULL) continue; @@ -1668,13 +1718,14 @@ void html_stop(struct content *c) void html_reformat(struct content *c, int width, int height) { + html_content *htmlc = (html_content *) c; struct box *layout; unsigned int time_before, time_taken; time_before = wallclock(); - layout_document(c, width, height); - layout = c->data.html.layout; + layout_document(htmlc, width, height); + layout = htmlc->layout; /* width and height are at least margin box of document */ c->width = layout->x + layout->padding[LEFT] + layout->width + @@ -1722,14 +1773,12 @@ void html_redraw_a_box(hlcache_handle *h, struct box *box) void html_destroy(struct content *c) { + html_content *html = (html_content *) c; unsigned int i; struct form *f, *g; - struct content_html_data *html; LOG(("content %p", c)); - html = &c->data.html; - /* Destroy forms */ for (f = html->forms; f != NULL; f = g) { g = f->prev; @@ -1737,12 +1786,7 @@ void html_destroy(struct content *c) form_free(f); } - if (html->favicon != NULL) { - hlcache_handle_release(html->favicon); - html->favicon = NULL; - } - - imagemap_destroy(c); + imagemap_destroy(html); if (html->parser_binding != NULL) binding_destroy_tree(html->parser_binding); @@ -1849,7 +1893,7 @@ void html_destroy_iframe(struct content_html_iframe *iframe) { } } -bool html_clone(const struct content *old, struct content *new_content) +nserror html_clone(const struct content *old, struct content **newc) { /** \todo Clone HTML specifics */ @@ -1865,22 +1909,21 @@ bool html_clone(const struct content *old, struct content *new_content) * Set the content status. */ -void html_set_status(struct content *c, const char *extra) +void html_set_status(html_content *c, const char *extra) { unsigned int stylesheets = 0, objects = 0; - if (c->data.html.num_objects == 0) - stylesheets = c->data.html.stylesheet_count - c->active; + if (c->num_objects == 0) + stylesheets = c->stylesheet_count - c->base.active; else { - stylesheets = c->data.html.stylesheet_count; - objects = c->data.html.num_objects - c->active; + stylesheets = c->stylesheet_count; + objects = c->num_objects - c->base.active; } - content_set_status(c, "%u/%u %s %u/%u %s %s", - stylesheets, c->data.html.stylesheet_count, - messages_get((c->data.html.stylesheet_count == 1) ? + content_set_status(&c->base, "%u/%u %s %u/%u %s %s", + stylesheets, c->stylesheet_count, + messages_get((c->stylesheet_count == 1) ? "styl" : "styls"), - objects, c->data.html.num_objects, - messages_get((c->data.html.num_objects == 1) ? - "obj" : "objs"), + objects, c->num_objects, + messages_get((c->num_objects == 1) ? "obj" : "objs"), extra); } @@ -1893,19 +1936,20 @@ void html_open(struct content *c, struct browser_window *bw, struct content *page, struct box *box, struct object_params *params) { + html_content *html = (html_content *) c; struct content_html_object *object, *next; - c->data.html.bw = bw; - c->data.html.page = page; - c->data.html.box = box; + html->bw = bw; + html->page = (html_content *) page; + html->box = box; - for (object = c->data.html.object_list; object != NULL; object = next) { + for (object = html->object_list; object != NULL; object = next) { next = object->next; if (object->content == NULL) continue; - if (content_get_type(object->content) == CONTENT_UNKNOWN) + if (content_get_type(object->content) == CONTENT_NONE) continue; content_open(object->content, @@ -1922,17 +1966,18 @@ void html_open(struct content *c, struct browser_window *bw, void html_close(struct content *c) { + html_content *html = (html_content *) c; struct content_html_object *object, *next; - c->data.html.bw = 0; + html->bw = NULL; - for (object = c->data.html.object_list; object != NULL; object = next) { + for (object = html->object_list; object != NULL; object = next) { next = object->next; if (object->content == NULL) continue; - if (content_get_type(object->content) == CONTENT_UNKNOWN) + if (content_get_type(object->content) == CONTENT_NONE) continue; if (content_get_type(object->content) == CONTENT_HTML) @@ -2004,12 +2049,11 @@ void html_dump_frameset(struct content_html_frames *frame, */ xmlDoc *html_get_document(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.document; + return c->document; } /** @@ -2022,12 +2066,11 @@ xmlDoc *html_get_document(hlcache_handle *h) */ struct box *html_get_box_tree(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.layout; + return c->layout; } /** @@ -2038,12 +2081,11 @@ struct box *html_get_box_tree(hlcache_handle *h) */ const char *html_get_encoding(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.encoding; + return c->encoding; } /** @@ -2054,12 +2096,11 @@ const char *html_get_encoding(hlcache_handle *h) */ binding_encoding_source html_get_encoding_source(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.encoding_source; + return c->encoding_source; } /** @@ -2070,12 +2111,11 @@ binding_encoding_source html_get_encoding_source(hlcache_handle *h) */ struct content_html_frames *html_get_frameset(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.frameset; + return c->frameset; } /** @@ -2086,12 +2126,11 @@ struct content_html_frames *html_get_frameset(hlcache_handle *h) */ struct content_html_iframe *html_get_iframe(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.iframe; + return c->iframe; } /** @@ -2102,12 +2141,11 @@ struct content_html_iframe *html_get_iframe(hlcache_handle *h) */ const char *html_get_base_url(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.base_url; + return c->base_url; } /** @@ -2118,12 +2156,11 @@ const char *html_get_base_url(hlcache_handle *h) */ const char *html_get_base_target(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.base_target; + return c->base_target; } /** @@ -2135,15 +2172,14 @@ const char *html_get_base_target(hlcache_handle *h) */ struct html_stylesheet *html_get_stylesheets(hlcache_handle *h, unsigned int *n) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); assert(n != NULL); - *n = c->data.html.stylesheet_count; + *n = c->stylesheet_count; - return c->data.html.stylesheets; + return c->stylesheets; } /** @@ -2155,15 +2191,14 @@ struct html_stylesheet *html_get_stylesheets(hlcache_handle *h, unsigned int *n) */ struct content_html_object *html_get_objects(hlcache_handle *h, unsigned int *n) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); assert(n != NULL); - *n = c->data.html.num_objects; + *n = c->num_objects; - return c->data.html.object_list; + return c->object_list; } /** @@ -2174,12 +2209,11 @@ struct content_html_object *html_get_objects(hlcache_handle *h, unsigned int *n) */ hlcache_handle *html_get_favicon(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); assert(c != NULL); - assert(c->type == CONTENT_HTML); - return c->data.html.favicon; + return NULL; } @@ -2208,3 +2242,15 @@ bool html_get_id_offset(hlcache_handle *h, const char *frag_id, int *x, int *y) } return false; } + +/** + * Compute the type of a content + * + * \param c Content to consider + * \return CONTENT_HTML + */ +content_type html_content_type(lwc_string *mime_type) +{ + return CONTENT_HTML; +} + diff --git a/render/html.h b/render/html.h index 8789f30e6..a321f5d48 100644 --- a/render/html.h +++ b/render/html.h @@ -45,16 +45,6 @@ struct plotters; struct scroll; struct scroll_msg_data; -/* entries in stylesheet_content */ -#define STYLESHEET_BASE 0 /* base style sheet */ -#define STYLESHEET_QUIRKS 1 /* quirks mode stylesheet */ -#define STYLESHEET_ADBLOCK 2 /* adblocking stylesheet */ -#define STYLESHEET_START 3 /* start of document stylesheets */ - -extern char *default_stylesheet_url; -extern char *adblock_stylesheet_url; -extern char *quirks_stylesheet_url; - /** * Container for stylesheets used by an HTML document */ @@ -90,9 +80,8 @@ struct content_html_object { struct hlcache_handle *content; /**< Content, or 0. */ struct box *box; /**< Node in box tree containing it. */ - /** Pointer to array of permitted content_type, terminated by - * CONTENT_UNKNOWN, or 0 if any type is acceptable. */ - const content_type *permitted_types; + /** Bitmap of acceptable content types */ + content_type permitted_types; bool background; /**< This object is a background image. */ }; @@ -134,92 +123,24 @@ struct content_html_iframe { struct content_html_iframe *next; }; -/** Data specific to CONTENT_HTML. */ -struct content_html_data { - void *parser_binding; - xmlDoc *document; - binding_quirks_mode quirks; /**< Quirkyness of document */ - - char *encoding; /**< Encoding of source, 0 if unknown. */ - binding_encoding_source encoding_source; - /**< Source of encoding information. */ - - char *base_url; /**< Base URL (may be a copy of content->url). */ - char *base_target; /**< Base target */ - - struct box *layout; /**< Box tree, or 0. */ - colour background_colour; /**< Document background colour. */ - const struct font_functions *font_func; - - struct hlcache_handle *favicon; /**< the favicon for the page */ - - /** Number of entries in stylesheet_content. */ - unsigned int stylesheet_count; - /** Stylesheets. Each may be 0. */ - struct html_stylesheet *stylesheets; - /**< Style selection context */ - css_select_ctx *select_ctx; - - /** Number of entries in object_list. */ - unsigned int num_objects; - /** List of objects. */ - struct content_html_object *object_list; - /** Forms, in reverse order to document. */ - struct form *forms; - /** Hash table of imagemaps. */ - struct imagemap **imagemaps; - - /** Browser window containing this document, or 0 if not open. */ - struct browser_window *bw; - - /** Frameset information */ - struct content_html_frames *frameset; - - /** Inline frame information */ - struct content_html_iframe *iframe; - - /** Content of type CONTENT_HTML containing this, or 0 if not an object - * within a page. */ - struct content *page; - /** Box containing this, or 0 if not an object. */ - struct box *box; -}; +/* entries in stylesheet_content */ +#define STYLESHEET_BASE 0 /* base style sheet */ +#define STYLESHEET_QUIRKS 1 /* quirks mode stylesheet */ +#define STYLESHEET_ADBLOCK 2 /* adblocking stylesheet */ +#define STYLESHEET_START 3 /* start of document stylesheets */ + +extern char *default_stylesheet_url; +extern char *adblock_stylesheet_url; +extern char *quirks_stylesheet_url; /** Render padding and margin box outlines in html_redraw(). */ extern bool html_redraw_debug; - -bool html_create(struct content *c, const struct http_parameter *params); -bool html_process_data(struct content *c, const char *data, unsigned int size); -bool html_convert(struct content *c); -void html_reformat(struct content *c, int width, int height); -void html_destroy(struct content *c); -bool html_clone(const struct content *old, struct content *new_content); -bool html_fetch_object(struct content *c, const char *url, struct box *box, - const content_type *permitted_types, - int available_width, int available_height, - bool background); -void html_stop(struct content *c); -void html_open(struct content *c, struct browser_window *bw, - struct content *page, struct box *box, - struct object_params *params); -void html_close(struct content *c); -void html_set_status(struct content *c, const char *extra); +nserror html_init(void); +void html_fini(void); void html_redraw_a_box(struct hlcache_handle *h, struct box *box); -/* in render/html_redraw.c */ -bool html_redraw(struct content *c, int x, int y, - int width, int height, const struct rect *clip, - float scale, colour background_colour); - -/* in render/html_interaction.c */ -void html_mouse_track(struct content *c, struct browser_window *bw, - browser_mouse_state mouse, int x, int y); -void html_mouse_action(struct content *c, struct browser_window *bw, - browser_mouse_state mouse, int x, int y); -void html_overflow_scroll_callback(void *client_data, - struct scroll_msg_data *scroll_data); void html_overflow_scroll_drag_end(struct scroll *scroll, browser_mouse_state mouse, int x, int y); size_t html_selection_drag_end(struct hlcache_handle *h, diff --git a/render/html_interaction.c b/render/html_interaction.c index 8cbd2c143..6670a1a90 100644 --- a/render/html_interaction.c +++ b/render/html_interaction.c @@ -37,7 +37,7 @@ #include "render/box.h" #include "render/font.h" #include "render/form.h" -#include "render/html.h" +#include "render/html_internal.h" #include "render/imagemap.h" #include "utils/messages.h" #include "utils/utils.h" diff --git a/render/html_internal.h b/render/html_internal.h new file mode 100644 index 000000000..ae1851e1e --- /dev/null +++ b/render/html_internal.h @@ -0,0 +1,112 @@ +/* + * Copyright 2004 James Bursa <bursa@users.sourceforge.net> + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** \file + * Content for text/html (private data). + */ + +#ifndef NETSURF_RENDER_HTML_INTERNAL_H_ +#define NETSURF_RENDER_HTML_INTERNAL_H_ + +#include "content/content_protected.h" +#include "render/html.h" + +/** Data specific to CONTENT_HTML. */ +typedef struct html_content { + struct content base; + + /** Parser object handle */ + void *parser_binding; + /** Document tree */ + xmlDoc *document; + /** Quirkyness of document */ + binding_quirks_mode quirks; + + /** Encoding of source, NULL if unknown. */ + char *encoding; + /** Source of encoding information. */ + binding_encoding_source encoding_source; + + /** Base URL (may be a copy of content->url). */ + char *base_url; + /** Base target */ + char *base_target; + + /** Box tree, or NULL. */ + struct box *layout; + /** Document background colour. */ + colour background_colour; + /** Font callback table */ + const struct font_functions *font_func; + + /** Number of entries in stylesheet_content. */ + unsigned int stylesheet_count; + /** Stylesheets. Each may be NULL. */ + struct html_stylesheet *stylesheets; + /**< Style selection context */ + css_select_ctx *select_ctx; + + /** Number of entries in object_list. */ + unsigned int num_objects; + /** List of objects. */ + struct content_html_object *object_list; + /** Forms, in reverse order to document. */ + struct form *forms; + /** Hash table of imagemaps. */ + struct imagemap **imagemaps; + + /** Browser window containing this document, or NULL if not open. */ + struct browser_window *bw; + + /** Frameset information */ + struct content_html_frames *frameset; + + /** Inline frame information */ + struct content_html_iframe *iframe; + + /** Content of type CONTENT_HTML containing this, or NULL if not an + * object within a page. */ + struct html_content *page; + /** Box containing this, or NULL if not an object. */ + struct box *box; +} html_content; + + +bool html_fetch_object(html_content *c, const char *url, struct box *box, + content_type permitted_types, + int available_width, int available_height, + bool background); + +void html_set_status(html_content *c, const char *extra); + +/* in render/html_redraw.c */ +bool html_redraw(struct content *c, int x, int y, + int width, int height, const struct rect *clip, + float scale, colour background_colour); + +/* in render/html_interaction.c */ +void html_mouse_track(struct content *c, struct browser_window *bw, + browser_mouse_state mouse, int x, int y); +void html_mouse_action(struct content *c, struct browser_window *bw, + browser_mouse_state mouse, int x, int y); +void html_overflow_scroll_callback(void *client_data, + struct scroll_msg_data *scroll_data); + +#endif + + diff --git a/render/html_redraw.c b/render/html_redraw.c index c5d20b26f..fbe09f744 100644 --- a/render/html_redraw.c +++ b/render/html_redraw.c @@ -47,6 +47,7 @@ #include "render/box.h" #include "render/font.h" #include "render/form.h" +#include "render/html_internal.h" #include "render/layout.h" #include "utils/log.h" #include "utils/messages.h" @@ -114,6 +115,7 @@ bool html_redraw(struct content *c, int x, int y, int width, int height, const struct rect *clip, float scale, colour background_colour) { + html_content *html = (html_content *) c; struct box *box; bool result = true; bool select, select_only; @@ -122,7 +124,7 @@ bool html_redraw(struct content *c, int x, int y, .fill_colour = background_colour, }; - box = c->data.html.layout; + box = html->layout; assert(box); /* The select menu needs special treating because, when opened, it @@ -145,9 +147,8 @@ bool html_redraw(struct content *c, int x, int y, /* clear to background colour */ result = plot.clip(clip); - if (c->data.html.background_colour != NS_TRANSPARENT) - pstyle_fill_bg.fill_colour = - c->data.html.background_colour; + if (html->background_colour != NS_TRANSPARENT) + pstyle_fill_bg.fill_colour = html->background_colour; result &= plot.rectangle(clip->x0, clip->y0, clip->x1, clip->y1, &pstyle_fill_bg); diff --git a/render/imagemap.c b/render/imagemap.c index 07a46d404..e8d61384f 100644 --- a/render/imagemap.c +++ b/render/imagemap.c @@ -27,6 +27,7 @@ #include "content/content_protected.h" #include "content/hlcache.h" #include "render/box.h" +#include "render/html_internal.h" #include "render/imagemap.h" #include "utils/log.h" #include "utils/utils.h" @@ -71,10 +72,10 @@ struct imagemap { struct imagemap *next; /**< next entry in this hash chain */ }; -static bool imagemap_add(struct content *c, const char *key, +static bool imagemap_add(html_content *c, const char *key, struct mapentry *list); -static bool imagemap_create(struct content *c); -static bool imagemap_extract_map(xmlNode *node, struct content *c, +static bool imagemap_create(html_content *c); +static bool imagemap_extract_map(xmlNode *node, html_content *c, struct mapentry **entry); static bool imagemap_addtolist(xmlNode *n, char *base_url, struct mapentry **entry); @@ -92,13 +93,12 @@ static int imagemap_point_in_poly(int num, float *xpt, float *ypt, * \param list List of map regions * \return true on succes, false otherwise */ -bool imagemap_add(struct content *c, const char *key, struct mapentry *list) +bool imagemap_add(html_content *c, const char *key, struct mapentry *list) { struct imagemap *map; unsigned int slot; assert(c != NULL); - assert(c->type == CONTENT_HTML); assert(key != NULL); assert(list != NULL); @@ -119,8 +119,8 @@ bool imagemap_add(struct content *c, const char *key, struct mapentry *list) slot = imagemap_hash(key); - map->next = c->data.html.imagemaps[slot]; - c->data.html.imagemaps[slot] = map; + map->next = c->imagemaps[slot]; + c->imagemaps[slot] = map; return true; } @@ -131,15 +131,13 @@ bool imagemap_add(struct content *c, const char *key, struct mapentry *list) * \param c The containing content * \return true on success, false otherwise */ -bool imagemap_create(struct content *c) +bool imagemap_create(html_content *c) { assert(c != NULL); - assert(c->type == CONTENT_HTML); - if (c->data.html.imagemaps == NULL) { - c->data.html.imagemaps = calloc(HASH_SIZE, - sizeof(struct imagemap)); - if (c->data.html.imagemaps == NULL) { + if (c->imagemaps == NULL) { + c->imagemaps = calloc(HASH_SIZE, sizeof(struct imagemap)); + if (c->imagemaps == NULL) { return false; } } @@ -152,21 +150,20 @@ bool imagemap_create(struct content *c) * * \param c The containing content */ -void imagemap_destroy(struct content *c) +void imagemap_destroy(html_content *c) { unsigned int i; assert(c != NULL); - assert(c->type == CONTENT_HTML); /* no imagemaps -> return */ - if (c->data.html.imagemaps == NULL) + if (c->imagemaps == NULL) return; for (i = 0; i != HASH_SIZE; i++) { struct imagemap *map, *next; - map = c->data.html.imagemaps[i]; + map = c->imagemaps[i]; while (map != NULL) { next = map->next; imagemap_freelist(map->list); @@ -176,7 +173,7 @@ void imagemap_destroy(struct content *c) } } - free(c->data.html.imagemaps); + free(c->imagemaps); } /** @@ -184,23 +181,22 @@ void imagemap_destroy(struct content *c) * * \param c The containing content */ -void imagemap_dump(struct content *c) +void imagemap_dump(html_content *c) { unsigned int i; int j; assert(c != NULL); - assert(c->type == CONTENT_HTML); - if (c->data.html.imagemaps == NULL) + if (c->imagemaps == NULL) return; for (i = 0; i != HASH_SIZE; i++) { struct imagemap *map; struct mapentry *entry; - map = c->data.html.imagemaps[i]; + map = c->imagemaps[i]; while (map != NULL) { LOG(("Imagemap: %s", map->key)); @@ -248,7 +244,7 @@ void imagemap_dump(struct content *c) * \param c The containing content * \return false on memory exhaustion, true otherwise */ -bool imagemap_extract(xmlNode *node, struct content *c) +bool imagemap_extract(xmlNode *node, html_content *c) { xmlNode *this_node; struct mapentry *entry = NULL; @@ -306,7 +302,7 @@ bool imagemap_extract(xmlNode *node, struct content *c) * \param entry List of map entries * \return false on memory exhaustion, true otherwise */ -bool imagemap_extract_map(xmlNode *node, struct content *c, +bool imagemap_extract_map(xmlNode *node, html_content *c, struct mapentry **entry) { xmlNode *this_node; @@ -320,7 +316,7 @@ bool imagemap_extract_map(xmlNode *node, struct content *c, */ if (strcmp((const char *) node->name, "area") == 0 || strcmp((const char *) node->name, "a") == 0) { - if (imagemap_addtolist(node, c->data.html.base_url, + if (imagemap_addtolist(node, c->base_url, entry) == false) return false; } @@ -641,24 +637,23 @@ const char *imagemap_get(hlcache_handle *h, const char *key, unsigned long click_x, unsigned long click_y, const char **target) { - struct content *c = hlcache_handle_get_content(h); + html_content *c = (html_content *) hlcache_handle_get_content(h); unsigned int slot = 0; struct imagemap *map; struct mapentry *entry; unsigned long cx, cy; assert(c != NULL); - assert(c->type == CONTENT_HTML); if (key == NULL) return NULL; - if (c->data.html.imagemaps == NULL) + if (c->imagemaps == NULL) return NULL; slot = imagemap_hash(key); - for (map = c->data.html.imagemaps[slot]; map != NULL; map = map->next) { + for (map = c->imagemaps[slot]; map != NULL; map = map->next) { if (map->key != NULL && strcasecmp(map->key, key) == 0) break; } diff --git a/render/imagemap.h b/render/imagemap.h index 87a84c3dd..60d73653c 100644 --- a/render/imagemap.h +++ b/render/imagemap.h @@ -21,12 +21,12 @@ #include <libxml/HTMLtree.h> -struct content; +struct html_content; struct hlcache_handle; -void imagemap_destroy(struct content *c); -void imagemap_dump(struct content *c); -bool imagemap_extract(xmlNode *node, struct content *c); +void imagemap_destroy(struct html_content *c); +void imagemap_dump(struct html_content *c); +bool imagemap_extract(xmlNode *node, struct html_content *c); const char *imagemap_get(struct hlcache_handle *h, const char *key, unsigned long x, unsigned long y, diff --git a/render/layout.c b/render/layout.c index ca3f84853..b46930b21 100644 --- a/render/layout.c +++ b/render/layout.c @@ -49,6 +49,7 @@ #include "render/box.h" #include "render/font.h" #include "render/form.h" +#include "render/html_internal.h" #include "render/layout.h" #include "render/table.h" #include "utils/log.h" @@ -66,7 +67,7 @@ static bool layout_block_context(struct box *block, int viewport_height, - struct content *content); + html_content *content); static void layout_minmax_block(struct box *block, const struct font_functions *font_func); static struct box* layout_next_margin_block(struct box *box, struct box *block, @@ -99,16 +100,16 @@ static int line_height(const css_computed_style *style); static bool layout_line(struct box *first, int *width, int *y, int cx, int cy, struct box *cont, bool indent, bool has_text_children, - struct content *content, struct box **next_box); + html_content *content, struct box **next_box); static struct box *layout_minmax_line(struct box *first, int *min, int *max, bool first_line, bool *line_has_height, const struct font_functions *font_func); static int layout_text_indent(const css_computed_style *style, int width); -static bool layout_float(struct box *b, int width, struct content *content); +static bool layout_float(struct box *b, int width, html_content *content); static void place_float_below(struct box *c, int width, int cx, int y, struct box *cont); static bool layout_table(struct box *box, int available_width, - struct content *content); + html_content *content); static void layout_move_children(struct box *box, int x, int y); static void calculate_mbp_width(const css_computed_style *style, unsigned int side, bool margin, bool border, bool padding, @@ -121,10 +122,10 @@ static void layout_compute_relative_offset(struct box *box, int *x, int *y); static bool layout_position_absolute(struct box *box, struct box *containing_block, int cx, int cy, - struct content *content); + html_content *content); static bool layout_absolute(struct box *box, struct box *containing_block, int cx, int cy, - struct content *content); + html_content *content); static void layout_compute_offsets(struct box *box, struct box *containing_block, int *top, int *right, int *bottom, int *left); @@ -139,13 +140,11 @@ static void layout_compute_offsets(struct box *box, * \return true on success, false on memory exhaustion */ -bool layout_document(struct content *content, int width, int height) +bool layout_document(html_content *content, int width, int height) { bool ret; - struct box *doc = content->data.html.layout; - const struct font_functions *font_func = content->data.html.font_func; - - assert(content->type == CONTENT_HTML); + struct box *doc = content->layout; + const struct font_functions *font_func = content->font_func; layout_minmax_block(doc, font_func); @@ -202,7 +201,7 @@ bool layout_document(struct content *content, int width, int height) */ bool layout_block_context(struct box *block, int viewport_height, - struct content *content) + html_content *content) { struct box *box; int cx, cy; /**< current coordinates */ @@ -1264,8 +1263,8 @@ void layout_block_add_scrollbar(struct box *box, int which) overflow = css_computed_overflow(box->style); if (overflow == CSS_OVERFLOW_SCROLL || overflow == CSS_OVERFLOW_AUTO || - (box->object && - content_get_type(box->object) == CONTENT_HTML)) { + (box->object && content_get_type(box->object) == + CONTENT_HTML)) { /* make space for scrollbars, unless height/width are AUTO */ if (which == BOTTOM && box->height != AUTO && (overflow == CSS_OVERFLOW_SCROLL || @@ -1987,7 +1986,7 @@ void find_sides(struct box *fl, int y0, int y1, */ bool layout_inline_container(struct box *inline_container, int width, - struct box *cont, int cx, int cy, struct content *content) + struct box *cont, int cx, int cy, html_content *content) { bool first_line = true; bool has_text_children; @@ -2141,13 +2140,13 @@ int line_height(const css_computed_style *style) * containing the text after new_length excluding the initial space character. */ -static bool layout_text_box_split(struct content *content, +static bool layout_text_box_split(html_content *content, plot_font_style_t *fstyle, struct box *split_box, size_t new_length, int new_width) { int space_width = split_box->space; struct box *c2; - const struct font_functions *font_func = content->data.html.font_func; + const struct font_functions *font_func = content->font_func; if (space_width == 0) { /* Currently split_box has no space. */ @@ -2226,7 +2225,7 @@ static bool layout_text_box_split(struct content *content, bool layout_line(struct box *first, int *width, int *y, int cx, int cy, struct box *cont, bool indent, bool has_text_children, - struct content *content, struct box **next_box) + html_content *content, struct box **next_box) { int height, used_height; int x0 = 0; @@ -2244,7 +2243,7 @@ bool layout_line(struct box *first, int *width, int *y, int space_before = 0, space_after = 0; unsigned int inline_count = 0; unsigned int i; - const struct font_functions *font_func = content->data.html.font_func; + const struct font_functions *font_func = content->font_func; plot_font_style_t fstyle; #ifdef LAYOUT_DEBUG @@ -3233,7 +3232,7 @@ int layout_text_indent(const css_computed_style *style, int width) * \return true on success, false on memory exhaustion */ -bool layout_float(struct box *b, int width, struct content *content) +bool layout_float(struct box *b, int width, html_content *content) { assert(b->type == BOX_TABLE || b->type == BOX_BLOCK || b->type == BOX_INLINE_BLOCK); @@ -3313,7 +3312,7 @@ void place_float_below(struct box *c, int width, int cx, int y, */ bool layout_table(struct box *table, int available_width, - struct content *content) + html_content *content) { unsigned int columns = table->columns; /* total columns */ unsigned int i; @@ -4363,7 +4362,7 @@ void layout_compute_relative_offset(struct box *box, int *x, int *y) bool layout_position_absolute(struct box *box, struct box *containing_block, int cx, int cy, - struct content *content) + html_content *content) { struct box *c; @@ -4430,7 +4429,7 @@ bool layout_position_absolute(struct box *box, bool layout_absolute(struct box *box, struct box *containing_block, int cx, int cy, - struct content *content) + html_content *content) { int static_left, static_top; /* static position */ int top, right, bottom, left; diff --git a/render/layout.h b/render/layout.h index 30d9757f6..f4117e286 100644 --- a/render/layout.h +++ b/render/layout.h @@ -28,10 +28,11 @@ #define _NETSURF_RENDER_LAYOUT_H_ struct box; +struct html_content; -bool layout_document(struct content *content, int width, int height); +bool layout_document(struct html_content *content, int width, int height); bool layout_inline_container(struct box *box, int width, - struct box *cont, int cx, int cy, struct content *content); + struct box *cont, int cx, int cy, struct html_content *content); void layout_calculate_descendant_bboxes(struct box *box); void layout_minmax_table(struct box *table, const struct font_functions *font_func); diff --git a/render/textplain.c b/render/textplain.c index 9601e25ab..a394b8251 100644 --- a/render/textplain.c +++ b/render/textplain.c @@ -50,6 +50,24 @@ #include "utils/utils.h" #include "utils/utf8.h" +struct textplain_line { + size_t start; + size_t length; +}; + +typedef struct textplain_content { + struct content base; + + char *encoding; + void *inputstream; + char *utf8_data; + size_t utf8_data_size; + size_t utf8_data_allocated; + unsigned long physical_line_count; + struct textplain_line *physical_line; + int formatted_width; +} textplain_content; + #define CHUNK 32768 /* Must be a power of 2 */ #define MARGIN 4 @@ -69,35 +87,126 @@ static plot_font_style_t textplain_style = { static int textplain_tab_width = 256; /* try for a sensible default */ -static bool textplain_create_internal(struct content *c, const char *encoding); +static nserror textplain_create(const content_handler *handler, + lwc_string *imime_type, const http_parameter *params, + llcache_handle *llcache, const char *fallback_charset, + bool quirks, struct content **c); +static nserror textplain_create_internal(textplain_content *c, + const char *charset); +static bool textplain_process_data(struct content *c, + const char *data, unsigned int size); +static bool textplain_convert(struct content *c); +static void textplain_mouse_track(struct content *c, struct browser_window *bw, + browser_mouse_state mouse, int x, int y); +static void textplain_mouse_action(struct content *c, struct browser_window *bw, + browser_mouse_state mouse, int x, int y); +static void textplain_reformat(struct content *c, int width, int height); +static void textplain_destroy(struct content *c); +static bool textplain_redraw(struct content *c, int x, int y, + int width, int height, const struct rect *clip, + float scale, colour background_colour); +static nserror textplain_clone(const struct content *old, + struct content **newc); +static content_type textplain_content_type(lwc_string *mime_type); + static parserutils_error textplain_charset_hack(const uint8_t *data, size_t len, uint16_t *mibenum, uint32_t *source); -static bool textplain_drain_input(struct content *c, +static bool textplain_drain_input(textplain_content *c, parserutils_inputstream *stream, parserutils_error terminator); -static bool textplain_copy_utf8_data(struct content *c, +static bool textplain_copy_utf8_data(textplain_content *c, const uint8_t *buf, size_t len); static int textplain_coord_from_offset(const char *text, size_t offset, size_t length); static float textplain_line_height(void); +static const content_handler textplain_content_handler = { + textplain_create, + textplain_process_data, + textplain_convert, + textplain_reformat, + textplain_destroy, + NULL, + textplain_mouse_track, + textplain_mouse_action, + textplain_redraw, + NULL, + NULL, + NULL, + textplain_clone, + NULL, + textplain_content_type, + true +}; + +static lwc_string *textplain_mime_type; + +/** + * Initialise the text content handler + */ +nserror textplain_init(void) +{ + lwc_error lerror; + nserror error; + + lerror = lwc_intern_string("text/plain", SLEN("text/plain"), + &textplain_mime_type); + if (lerror != lwc_error_ok) + return NSERROR_NOMEM; + + error = content_factory_register_handler(textplain_mime_type, + &textplain_content_handler); + if (error != NSERROR_OK) + lwc_string_unref(textplain_mime_type); + + return error; +} + +/** + * Clean up after the text content handler + */ +void textplain_fini(void) +{ + lwc_string_unref(textplain_mime_type); +} /** * Create a CONTENT_TEXTPLAIN. */ -bool textplain_create(struct content *c, const http_parameter *params) +nserror textplain_create(const content_handler *handler, + lwc_string *imime_type, const http_parameter *params, + llcache_handle *llcache, const char *fallback_charset, + bool quirks, struct content **c) { - const char *encoding; + textplain_content *text; nserror error; + const char *encoding; - textplain_style.size = (option_font_size * FONT_SIZE_SCALE) / 10; + text = talloc_zero(0, textplain_content); + if (text == NULL) + return NSERROR_NOMEM; + + error = content__init(&text->base, handler, imime_type, params, + llcache, fallback_charset, quirks); + if (error != NSERROR_OK) { + talloc_free(text); + return error; + } error = http_parameter_list_find_item(params, "charset", &encoding); if (error != NSERROR_OK) { encoding = "Windows-1252"; } - return textplain_create_internal(c, encoding); + error = textplain_create_internal(text, encoding); + if (error != NSERROR_OK) { + talloc_free(text); + return error; + } + + *c = (struct content *) text; + + return NSERROR_OK; } /* @@ -116,13 +225,15 @@ parserutils_error textplain_charset_hack(const uint8_t *data, size_t len, return PARSERUTILS_OK; } -bool textplain_create_internal(struct content *c, const char *encoding) +nserror textplain_create_internal(textplain_content *c, const char *encoding) { char *utf8_data; parserutils_inputstream *stream; parserutils_error error; union content_msg_data msg_data; + textplain_style.size = (option_font_size * FONT_SIZE_SCALE) / 10; + utf8_data = talloc_array(c, char, CHUNK); if (utf8_data == NULL) goto no_memory; @@ -140,30 +251,31 @@ bool textplain_create_internal(struct content *c, const char *encoding) goto no_memory; } - c->data.textplain.encoding = strdup(encoding); - if (c->data.textplain.encoding == NULL) { + c->encoding = strdup(encoding); + if (c->encoding == NULL) { talloc_free(utf8_data); parserutils_inputstream_destroy(stream); goto no_memory; } - c->data.textplain.inputstream = stream; - c->data.textplain.utf8_data = utf8_data; - c->data.textplain.utf8_data_size = 0; - c->data.textplain.utf8_data_allocated = CHUNK; - c->data.textplain.physical_line = 0; - c->data.textplain.physical_line_count = 0; - c->data.textplain.formatted_width = 0; + c->inputstream = stream; + c->utf8_data = utf8_data; + c->utf8_data_size = 0; + c->utf8_data_allocated = CHUNK; + c->physical_line = 0; + c->physical_line_count = 0; + c->formatted_width = 0; - return true; + return NSERROR_OK; no_memory: msg_data.error = messages_get("NoMemory"); - content_broadcast(c, CONTENT_MSG_ERROR, msg_data); - return false; + content_broadcast(&c->base, CONTENT_MSG_ERROR, msg_data); + return NSERROR_NOMEM; } -bool textplain_drain_input(struct content *c, parserutils_inputstream *stream, +bool textplain_drain_input(textplain_content *c, + parserutils_inputstream *stream, parserutils_error terminator) { static const uint8_t *u_fffd = (const uint8_t *) "\xef\xbf\xfd"; @@ -228,26 +340,25 @@ bool textplain_drain_input(struct content *c, parserutils_inputstream *stream, return true; } -bool textplain_copy_utf8_data(struct content *c, const uint8_t *buf, size_t len) +bool textplain_copy_utf8_data(textplain_content *c, + const uint8_t *buf, size_t len) { - if (c->data.textplain.utf8_data_size + len >= - c->data.textplain.utf8_data_allocated) { + if (c->utf8_data_size + len >= c->utf8_data_allocated) { /* Compute next multiple of chunk above the required space */ - size_t allocated = (c->data.textplain.utf8_data_size + len + + size_t allocated = (c->utf8_data_size + len + CHUNK - 1) & ~(CHUNK - 1); char *utf8_data = talloc_realloc(c, - c->data.textplain.utf8_data, + c->utf8_data, char, allocated); if (utf8_data == NULL) return false; - c->data.textplain.utf8_data = utf8_data; - c->data.textplain.utf8_data_allocated = allocated; + c->utf8_data = utf8_data; + c->utf8_data_allocated = allocated; } - memcpy(c->data.textplain.utf8_data + - c->data.textplain.utf8_data_size, buf, len); - c->data.textplain.utf8_data_size += len; + memcpy(c->utf8_data + c->utf8_data_size, buf, len); + c->utf8_data_size += len; return true; } @@ -260,7 +371,8 @@ bool textplain_copy_utf8_data(struct content *c, const uint8_t *buf, size_t len) bool textplain_process_data(struct content *c, const char *data, unsigned int size) { - parserutils_inputstream *stream = c->data.textplain.inputstream; + textplain_content *text = (textplain_content *) c; + parserutils_inputstream *stream = text->inputstream; union content_msg_data msg_data; parserutils_error error; @@ -270,7 +382,7 @@ bool textplain_process_data(struct content *c, goto no_memory; } - if (textplain_drain_input(c, stream, PARSERUTILS_NEEDDATA) == false) + if (textplain_drain_input(text, stream, PARSERUTILS_NEEDDATA) == false) goto no_memory; return true; @@ -288,7 +400,8 @@ no_memory: bool textplain_convert(struct content *c) { - parserutils_inputstream *stream = c->data.textplain.inputstream; + textplain_content *text = (textplain_content *) c; + parserutils_inputstream *stream = text->inputstream; parserutils_error error; error = parserutils_inputstream_append(stream, NULL, 0); @@ -296,11 +409,11 @@ bool textplain_convert(struct content *c) return false; } - if (textplain_drain_input(c, stream, PARSERUTILS_EOF) == false) + if (textplain_drain_input(text, stream, PARSERUTILS_EOF) == false) return false; parserutils_inputstream_destroy(stream); - c->data.textplain.inputstream = NULL; + text->inputstream = NULL; content_set_ready(c); content_set_done(c); @@ -316,10 +429,11 @@ bool textplain_convert(struct content *c) void textplain_reformat(struct content *c, int width, int height) { - char *utf8_data = c->data.textplain.utf8_data; - size_t utf8_data_size = c->data.textplain.utf8_data_size; + textplain_content *text = (textplain_content *) c; + char *utf8_data = text->utf8_data; + size_t utf8_data_size = text->utf8_data_size; unsigned long line_count = 0; - struct textplain_line *line = c->data.textplain.physical_line; + struct textplain_line *line = text->physical_line; struct textplain_line *line1; size_t i, space, col; size_t columns = 80; @@ -333,12 +447,12 @@ void textplain_reformat(struct content *c, int width, int height) columns = (width - MARGIN - MARGIN) * 8 / character_width; textplain_tab_width = (TAB_WIDTH * character_width) / 8; - c->data.textplain.formatted_width = width; + text->formatted_width = width; - c->data.textplain.physical_line_count = 0; + text->physical_line_count = 0; if (!line) { - c->data.textplain.physical_line = line = + text->physical_line = line = talloc_array(c, struct textplain_line, 1024 + 3); if (!line) goto no_memory; @@ -359,8 +473,7 @@ void textplain_reformat(struct content *c, int width, int height) struct textplain_line, line_count + 1024 + 3); if (!line1) goto no_memory; - c->data.textplain.physical_line = - line = line1; + text->physical_line = line = line1; } if (term) { line[line_count-1].length = i - line_start; @@ -392,7 +505,7 @@ void textplain_reformat(struct content *c, int width, int height) line[line_count-1].length = i - line[line_count-1].start; line[line_count].start = utf8_data_size; - c->data.textplain.physical_line_count = line_count; + text->physical_line_count = line_count; c->width = width; c->height = line_count * textplain_line_height() + MARGIN + MARGIN; @@ -410,39 +523,64 @@ no_memory: void textplain_destroy(struct content *c) { - if (c->data.textplain.encoding != NULL) - free(c->data.textplain.encoding); + textplain_content *text = (textplain_content *) c; - if (c->data.textplain.inputstream != NULL) - parserutils_inputstream_destroy(c->data.textplain.inputstream); + if (text->encoding != NULL) + free(text->encoding); + + if (text->inputstream != NULL) + parserutils_inputstream_destroy(text->inputstream); } -bool textplain_clone(const struct content *old, struct content *new_content) +nserror textplain_clone(const struct content *old, struct content **newc) { + const textplain_content *old_text = (textplain_content *) old; + textplain_content *text; + nserror error; const char *data; unsigned long size; + text = talloc_zero(0, textplain_content); + if (text == NULL) + return NSERROR_NOMEM; + + error = content__clone(old, &text->base); + if (error != NSERROR_OK) { + content_destroy(&text->base); + return error; + } + /* Simply replay create/process/convert */ - if (textplain_create_internal(new_content, - old->data.textplain.encoding) == false) - return false; + error = textplain_create_internal(text, old_text->encoding); + if (error != NSERROR_OK) { + content_destroy(&text->base); + return error; + } - data = content__get_source_data(new_content, &size); + data = content__get_source_data(&text->base, &size); if (size > 0) { - if (textplain_process_data(new_content, data, size) == false) - return false; + if (textplain_process_data(&text->base, data, size) == false) { + content_destroy(&text->base); + return NSERROR_NOMEM; + } } if (old->status == CONTENT_STATUS_READY || old->status == CONTENT_STATUS_DONE) { - if (textplain_convert(new_content) == false) - return false; + if (textplain_convert(&text->base) == false) { + content_destroy(&text->base); + return NSERROR_CLONE_FAILED; + } } - return true; + return NSERROR_OK; } +content_type textplain_content_type(lwc_string *mime_type) +{ + return CONTENT_TEXTPLAIN; +} /** * Handle mouse tracking (including drags) in a TEXTPLAIN content window. @@ -551,15 +689,16 @@ bool textplain_redraw(struct content *c, int x, int y, int width, int height, const struct rect *clip, float scale, colour background_colour) { + textplain_content *text = (textplain_content *) c; struct browser_window *bw = current_redraw_browser; - char *utf8_data = c->data.textplain.utf8_data; + char *utf8_data = text->utf8_data; long lineno; - unsigned long line_count = c->data.textplain.physical_line_count; + unsigned long line_count = text->physical_line_count; float line_height = textplain_line_height(); float scaled_line_height = line_height * scale; long line0 = (clip->y0 - y * scale) / scaled_line_height - 1; long line1 = (clip->y1 - y * scale) / scaled_line_height + 1; - struct textplain_line *line = c->data.textplain.physical_line; + struct textplain_line *line = text->physical_line; size_t length; plot_style_t *plot_style_highlight; @@ -682,11 +821,12 @@ bool textplain_redraw(struct content *c, int x, int y, */ unsigned long textplain_line_count(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); assert(c != NULL); - return c->data.textplain.physical_line_count; + return c->physical_line_count; } /** @@ -697,11 +837,12 @@ unsigned long textplain_line_count(hlcache_handle *h) */ size_t textplain_size(hlcache_handle *h) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); assert(c != NULL); - return c->data.textplain.utf8_data_size; + return c->utf8_data_size; } /** @@ -719,7 +860,8 @@ size_t textplain_size(hlcache_handle *h) size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); float line_height = textplain_line_height(); struct textplain_line *line; const char *text; @@ -728,12 +870,11 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir) int idx; assert(c != NULL); - assert(c->type == CONTENT_TEXTPLAIN); y = (int)((float)(y - MARGIN) / line_height); x -= MARGIN; - nlines = c->data.textplain.physical_line_count; + nlines = c->physical_line_count; if (!nlines) return 0; @@ -741,8 +882,8 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir) else if ((unsigned)y >= nlines) y = nlines - 1; - line = &c->data.textplain.physical_line[y]; - text = c->data.textplain.utf8_data + line->start; + line = &c->physical_line[y]; + text = c->utf8_data + line->start; length = line->length; idx = 0; @@ -798,18 +939,18 @@ size_t textplain_offset_from_coords(hlcache_handle *h, int x, int y, int dir) int textplain_find_line(hlcache_handle *h, unsigned offset) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); struct textplain_line *line; int nlines; int lineno = 0; assert(c != NULL); - assert(c->type == CONTENT_TEXTPLAIN); - line = c->data.textplain.physical_line; - nlines = c->data.textplain.physical_line_count; + line = c->physical_line; + nlines = c->physical_line_count; - if (offset > c->data.textplain.utf8_data_size) + if (offset > c->utf8_data_size) return -1; /* \todo - implement binary search here */ @@ -875,7 +1016,8 @@ int textplain_coord_from_offset(const char *text, size_t offset, size_t length) void textplain_coords_from_range(hlcache_handle *h, unsigned start, unsigned end, struct rect *r) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); float line_height = textplain_line_height(); char *utf8_data; struct textplain_line *line; @@ -883,13 +1025,12 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start, unsigned nlines; assert(c != NULL); - assert(c->type == CONTENT_TEXTPLAIN); assert(start <= end); - assert(end <= c->data.textplain.utf8_data_size); + assert(end <= c->utf8_data_size); - utf8_data = c->data.textplain.utf8_data; - nlines = c->data.textplain.physical_line_count; - line = c->data.textplain.physical_line; + utf8_data = c->utf8_data; + nlines = c->physical_line_count; + line = c->physical_line; /* find start */ lineno = textplain_find_line(h, start); @@ -904,7 +1045,7 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start, lineno = textplain_find_line(h, end); r->x0 = 0; - r->x1 = c->data.textplain.formatted_width; + r->x1 = c->formatted_width; } else { /* single line */ @@ -934,19 +1075,19 @@ void textplain_coords_from_range(hlcache_handle *h, unsigned start, char *textplain_get_line(hlcache_handle *h, unsigned lineno, size_t *poffset, size_t *plen) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); struct textplain_line *line; assert(c != NULL); - assert(c->type == CONTENT_TEXTPLAIN); - if (lineno >= c->data.textplain.physical_line_count) + if (lineno >= c->physical_line_count) return NULL; - line = &c->data.textplain.physical_line[lineno]; + line = &c->physical_line[lineno]; *poffset = line->start; *plen = line->length; - return c->data.textplain.utf8_data + line->start; + return c->utf8_data + line->start; } @@ -965,13 +1106,13 @@ char *textplain_get_line(hlcache_handle *h, unsigned lineno, char *textplain_get_raw_data(hlcache_handle *h, unsigned start, unsigned end, size_t *plen) { - struct content *c = hlcache_handle_get_content(h); + textplain_content *c = + (textplain_content *) hlcache_handle_get_content(h); size_t utf8_size; assert(c != NULL); - assert(c->type == CONTENT_TEXTPLAIN); - utf8_size = c->data.textplain.utf8_data_size; + utf8_size = c->utf8_data_size; /* any text at all? */ if (!utf8_size) return NULL; @@ -982,7 +1123,7 @@ char *textplain_get_raw_data(hlcache_handle *h, unsigned start, unsigned end, *plen = end - start; - return c->data.textplain.utf8_data + start; + return c->utf8_data + start; } /** diff --git a/render/textplain.h b/render/textplain.h index c338220d1..66e4e83ee 100644 --- a/render/textplain.h +++ b/render/textplain.h @@ -32,36 +32,8 @@ struct hlcache_handle; struct http_parameter; struct rect; -struct textplain_line { - size_t start; - size_t length; -}; - -struct content_textplain_data { - char *encoding; - void *inputstream; - char *utf8_data; - size_t utf8_data_size; - size_t utf8_data_allocated; - unsigned long physical_line_count; - struct textplain_line *physical_line; - int formatted_width; -}; - -bool textplain_create(struct content *c, const struct http_parameter *params); -bool textplain_process_data(struct content *c, - const char *data, unsigned int size); -bool textplain_convert(struct content *c); -void textplain_mouse_track(struct content *c, struct browser_window *bw, - browser_mouse_state mouse, int x, int y); -void textplain_mouse_action(struct content *c, struct browser_window *bw, - browser_mouse_state mouse, int x, int y); -void textplain_reformat(struct content *c, int width, int height); -void textplain_destroy(struct content *c); -bool textplain_redraw(struct content *c, int x, int y, - int width, int height, const struct rect *clip, - float scale, colour background_colour); -bool textplain_clone(const struct content *old, struct content *new_content); +nserror textplain_init(void); +void textplain_fini(void); /* access to lines for text selection and searching */ unsigned long textplain_line_count(struct hlcache_handle *h); |