From 646aed64ff07fa356f8aef034e77792cf634b053 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 6 Dec 2012 18:04:43 +0000 Subject: change to parameterised parser binding creation --- render/html.c | 138 ++++++++++++++++++---------------------------------------- 1 file changed, 42 insertions(+), 96 deletions(-) (limited to 'render/html.c') diff --git a/render/html.c b/render/html.c index 812c6909b..0b2c54365 100644 --- a/render/html.c +++ b/render/html.c @@ -112,60 +112,6 @@ static nserror css_error_to_nserror(css_error error) return NSERROR_CSS; } -static nserror -dom_hubbub_error_to_nserror(dom_hubbub_error error) -{ - switch (error) { - - /* HUBBUB_REPROCESS is not handled here because it can - * never occur outside the hubbub treebuilder - */ - - case DOM_HUBBUB_OK: - /* parsed ok */ - return NSERROR_OK; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_PAUSED): - /* hubbub input paused */ - return NSERROR_OK; - - case DOM_HUBBUB_NOMEM: - /* out of memory error from DOM */ - return NSERROR_NOMEM; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_ENCODINGCHANGE): - /* encoding changed */ - return NSERROR_ENCODING_CHANGE; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_NOMEM): - /* out of memory error from parser */ - return NSERROR_NOMEM; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_BADPARM): - return NSERROR_BAD_PARAMETER; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_INVALID): - return NSERROR_INVALID; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_FILENOTFOUND): - return NSERROR_NOT_FOUND; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_NEEDDATA): - return NSERROR_NEED_DATA; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_BADENCODING): - return NSERROR_BAD_ENCODING; - - case (DOM_HUBBUB_HUBBUB_ERR | HUBBUB_UNKNOWN): - /* currently only generated by the libdom hubbub binding */ - return NSERROR_DOM; - default: - /* unknown error */ - /** @todo better error handling and reporting */ - return NSERROR_UNKNOWN; - } - return NSERROR_UNKNOWN; -} static void html_destroy_objects(html_content *html) { @@ -371,6 +317,8 @@ html_create_html_data(html_content *c, const http_parameter *params) { lwc_string *charset; nserror nerror; + dom_hubbub_parser_params parse_params; + dom_hubbub_error error; c->parser = NULL; c->document = NULL; @@ -424,36 +372,37 @@ html_create_html_data(html_content *c, const http_parameter *params) } /* Create the parser binding */ - c->parser = dom_hubbub_parser_create(c->encoding, - true, - nsoption_bool(enable_javascript), - NULL, - html_process_script, - c, - &c->document); - if ((c->parser == NULL) && (c->encoding != NULL)) { + parse_params.enc = c->encoding; + parse_params.fix_enc = true; + parse_params.enable_script = nsoption_bool(enable_javascript); + parse_params.msg = NULL; + parse_params.script = html_process_script; + parse_params.ctx = c; + + error = dom_hubbub_parser_create(&parse_params, + &c->parser, + &c->document); + if ((error != DOM_HUBBUB_OK) && (c->encoding != NULL)) { /* Ok, we don't support the declared encoding. Bailing out * isn't exactly user-friendly, so fall back to autodetect */ free(c->encoding); c->encoding = NULL; - c->parser = dom_hubbub_parser_create(c->encoding, - true, - nsoption_bool(enable_javascript), - NULL, - html_process_script, - c, - &c->document); + parse_params.enc = c->encoding; + + error = dom_hubbub_parser_create(&parse_params, + &c->parser, + &c->document); } - if (c->parser == NULL) { + if (error != DOM_HUBBUB_OK) { nsurl_unref(c->base_url); c->base_url = NULL; lwc_string_unref(c->universal); c->universal = NULL; - return NSERROR_NOMEM; + return libdom_hubbub_error_to_nserror(error); } return NSERROR_OK; @@ -510,6 +459,7 @@ html_process_encoding_change(struct content *c, unsigned int size) { html_content *html = (html_content *) c; + dom_hubbub_parser_params parse_params; dom_hubbub_error error; const char *encoding; const char *source_data; @@ -539,15 +489,18 @@ html_process_encoding_change(struct content *c, dom_node_unref(html->document); } + parse_params.enc = html->encoding; + parse_params.fix_enc = true; + parse_params.enable_script = nsoption_bool(enable_javascript); + parse_params.msg = NULL; + parse_params.script = html_process_script; + parse_params.ctx = html; + /* Create new binding, using the new encoding */ - html->parser = dom_hubbub_parser_create(html->encoding, - true, - nsoption_bool(enable_javascript), - NULL, - html_process_script, - html, - &html->document); - if (html->parser == NULL) { + error = dom_hubbub_parser_create(&parse_params, + &html->parser, + &html->document); + if (error != DOM_HUBBUB_OK) { /* Ok, we don't support the declared encoding. Bailing out * isn't exactly user-friendly, so fall back to Windows-1252 */ free(html->encoding); @@ -555,21 +508,14 @@ html_process_encoding_change(struct content *c, if (html->encoding == NULL) { return NSERROR_NOMEM; } + parse_params.enc = html->encoding; - html->parser = dom_hubbub_parser_create(html->encoding, - true, - nsoption_bool(enable_javascript), - NULL, - html_process_script, - html, - &html->document); - - if (html->parser == NULL) { - /** @todo add a message callback function and pass the - * parser errors back instead of everything being - * OOM - */ - return NSERROR_NOMEM; + error = dom_hubbub_parser_create(&parse_params, + &html->parser, + &html->document); + + if (error != DOM_HUBBUB_OK) { + return libdom_hubbub_error_to_nserror(error); } } @@ -584,7 +530,7 @@ html_process_encoding_change(struct content *c, (const uint8_t *)source_data, source_size); - return dom_hubbub_error_to_nserror(error); + return libdom_hubbub_error_to_nserror(error); } @@ -603,7 +549,7 @@ html_process_data(struct content *c, const char *data, unsigned int size) (const uint8_t *) data, size); - err = dom_hubbub_error_to_nserror(dom_ret); + err = libdom_hubbub_error_to_nserror(dom_ret); /* deal with encoding change */ if (err == NSERROR_ENCODING_CHANGE) { @@ -1993,7 +1939,7 @@ html_begin_conversion(html_content *htmlc) LOG(("Parsing failed")); content_broadcast_errorcode(&htmlc->base, - dom_hubbub_error_to_nserror(error)); + libdom_hubbub_error_to_nserror(error)); return false; } -- cgit v1.2.3