change to parameterised parser binding creation

This commit is contained in:
Vincent Sanders 2012-12-06 18:04:43 +00:00
parent 61723e01c1
commit 646aed64ff
3 changed files with 130 additions and 102 deletions

View File

@ -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);
error = dom_hubbub_parser_create(&parse_params,
&html->parser,
&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;
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;
}

View File

@ -22,7 +22,6 @@
#include <assert.h>
#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>
#include "utils/config.h"
#include "utils/log.h"
@ -254,6 +253,70 @@ void libdom_iterate_child_elements(dom_node *parent,
dom_nodelist_unref(children);
}
/* exported interface documented in libdom.h */
nserror libdom_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_BADPARM:
/* Bad parameter passed to creation */
return NSERROR_BAD_PARAMETER;
case DOM_HUBBUB_DOM:
/* DOM call returned error */
return NSERROR_DOM;
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 ignore_dom_msg(uint32_t severity, void *ctx, const char *msg, ...)
{
}
@ -261,6 +324,7 @@ static void ignore_dom_msg(uint32_t severity, void *ctx, const char *msg, ...)
/* exported interface documented in libdom.h */
nserror libdom_parse_file(const char *filename, const char *encoding, dom_document **doc)
{
dom_hubbub_parser_params parse_params;
dom_hubbub_error error;
dom_hubbub_parser *parser;
dom_document *document;
@ -273,11 +337,17 @@ nserror libdom_parse_file(const char *filename, const char *encoding, dom_docume
return NSERROR_NOT_FOUND;
}
parser = dom_hubbub_parser_create(encoding, false, false,
ignore_dom_msg, NULL, NULL, &document);
if (parser == NULL) {
parse_params.enc = encoding;
parse_params.fix_enc = false;
parse_params.enable_script = false;
parse_params.msg = ignore_dom_msg;
parse_params.script = NULL;
parse_params.ctx = NULL;
error = dom_hubbub_parser_create(&parse_params, &parser, &document);
if (error != DOM_HUBBUB_OK) {
fclose(fp);
return NSERROR_DOM;
return libdom_hubbub_error_to_nserror(error);
}
while (feof(fp) == 0) {
@ -297,7 +367,7 @@ nserror libdom_parse_file(const char *filename, const char *encoding, dom_docume
dom_node_unref(document);
dom_hubbub_parser_destroy(parser);
fclose(fp);
return NSERROR_DOM;
return libdom_hubbub_error_to_nserror(error);
}
dom_hubbub_parser_destroy(parser);

View File

@ -28,7 +28,11 @@
#include <dom/dom.h>
/* depth-first walk the dom calling callback for each element
#include <dom/bindings/hubbub/parser.h>
#include <dom/bindings/hubbub/errors.h>
/**
* depth-first walk the dom calling callback for each element
*
* \param root the dom node to use as the root of the tree walk
* \return true if all nodes were examined, false if the callback terminated
@ -65,4 +69,12 @@ void libdom_iterate_child_elements(dom_node *parent,
nserror libdom_parse_file(const char *filename, const char *encoding,
dom_document **doc);
/**
* Convert libdom hubbub binding errors to nserrors.
*
* \param error The hubbub binding error to convert
* \return The appropriate nserror
*/
nserror libdom_hubbub_error_to_nserror(dom_hubbub_error error);
#endif