diff options
author | Michael Drake <michael.drake@codethink.co.uk> | 2018-08-15 15:05:54 +0100 |
---|---|---|
committer | Michael Drake <michael.drake@codethink.co.uk> | 2018-08-15 15:05:54 +0100 |
commit | b34c357bbd4883e409a8d617c528db92bf0963c0 (patch) | |
tree | b272a51d36733739b05053437eca8aeba94724d7 | |
parent | ecb247eb155ff5495a91e32b6e416fd0b6c6a53e (diff) | |
download | libnslayout-b34c357bbd4883e409a8d617c528db92bf0963c0.tar.gz libnslayout-b34c357bbd4883e409a8d617c528db92bf0963c0.tar.bz2 |
Library: DOM change watching is now the job of the client.
-rw-r--r-- | src/layout.c | 50 | ||||
-rw-r--r-- | test/dom/Makefile (renamed from src/dom/Makefile) | 0 | ||||
-rw-r--r-- | test/dom/debug.c (renamed from src/dom/debug.c) | 2 | ||||
-rw-r--r-- | test/dom/debug.h (renamed from src/dom/debug.h) | 0 | ||||
-rw-r--r-- | test/dom/watcher.c (renamed from src/dom/watcher.c) | 16 | ||||
-rw-r--r-- | test/dom/watcher.h (renamed from src/dom/watcher.h) | 6 | ||||
-rw-r--r-- | test/test-loader.c | 75 |
7 files changed, 65 insertions, 84 deletions
diff --git a/src/layout.c b/src/layout.c index 0c11660..b3b57ce 100644 --- a/src/layout.c +++ b/src/layout.c @@ -15,7 +15,6 @@ #include "layout.h" #include "util/util.h" -#include "dom/watcher.h" #include "util/dom-str.h" @@ -28,8 +27,6 @@ struct nsl_layout { css_media_type *media; nsl_callback cb; void *pw; - - struct nsl_dom_watcher *watcher; }; @@ -47,40 +44,6 @@ nsl_error nsl_fini(void) } -/** - * Callback function for dom modifications. - * - * \param[in] type The mutation type. - * \param[in] node The target node. (Caller yields ownership.) - * \param[in] node_type The type of node. - * \param[in] pw The layout object. - * \return NSL_OK on success, appropriate error otherwise. - */ -static nsl_error nsl_layout_dom_watcher_cb( - enum nsl_dom_watcher_type type, - dom_event_target *node, - dom_node_type node_type, - void *pw) -{ - nsl_layout *layout = pw; - - UNUSED(type); - UNUSED(layout); - UNUSED(node_type); - - /* TODO: Based on event type: - * 1. call to do (re)selection: - * a. all nodes? - * b. just this node? - * 2. call to update layout, if needed. - */ - - dom_node_unref(node); - - return NSL_OK; -} - - /* Publically exported function, documented in include/libnslayout/nslayout.h */ nsl_error nsl_layout_create( dom_document *doc, @@ -91,7 +54,6 @@ nsl_error nsl_layout_create( nsl_layout **layout) { nsl_layout *l = NULL; - nsl_error err; assert(doc != NULL); assert(css_ctx != NULL); @@ -110,12 +72,6 @@ nsl_error nsl_layout_create( l->cb = cb; l->pw = pw; - err = nsl_dom_watcher_create(&l->watcher, l->document, - nsl_layout_dom_watcher_cb, l); - if (err != NSL_OK) { - return err; - } - *layout = l; return NSL_OK; } @@ -125,15 +81,9 @@ nsl_error nsl_layout_create( nsl_error nsl_layout_destroy( nsl_layout *layout) { - nsl_error err; - assert(layout != NULL); /* TODO: free/unref the stuff we own in the layout */ - err = nsl_dom_watcher_destroy(layout->watcher); - if (err != NSL_OK) { - return err; - } free(layout); return NSL_OK; diff --git a/src/dom/Makefile b/test/dom/Makefile index 0b345cb..0b345cb 100644 --- a/src/dom/Makefile +++ b/test/dom/Makefile diff --git a/src/dom/debug.c b/test/dom/debug.c index 98a5e9b..9e3e0b0 100644 --- a/src/dom/debug.c +++ b/test/dom/debug.c @@ -12,7 +12,7 @@ #include <dom/dom.h> -#include "dom/debug.h" +#include "debug.h" #ifdef NSL_DOM_TRACE diff --git a/src/dom/debug.h b/test/dom/debug.h index cbe8559..cbe8559 100644 --- a/src/dom/debug.h +++ b/test/dom/debug.h diff --git a/src/dom/watcher.c b/test/dom/watcher.c index db1f8a9..312dbf2 100644 --- a/src/dom/watcher.c +++ b/test/dom/watcher.c @@ -21,8 +21,8 @@ #include "libnslayout/nslayout.h" -#include "dom/debug.h" -#include "dom/watcher.h" +#include "debug.h" +#include "watcher.h" #include "util/dom-str.h" #include "util/util.h" @@ -192,7 +192,7 @@ error: } /* Exported function, documented in src/dom/watcher.h */ -nsl_error nsl_dom_watcher_create( +bool nsl_dom_watcher_create( struct nsl_dom_watcher **watcher_out, dom_document *document, nsl_dom_watcher_cb watcher_cb, @@ -203,7 +203,7 @@ nsl_error nsl_dom_watcher_create( watcher = malloc(sizeof(*watcher)); if (watcher == NULL) { - return NSL_NO_MEM; + return false; } watcher->document = document; @@ -213,19 +213,19 @@ nsl_error nsl_dom_watcher_create( err = nsl__dom_listener_create(&watcher->listener, watcher); if (err != NSL_OK) { free(watcher); - return err; + return false; } *watcher_out = watcher; - return NSL_OK; + return true; } /* Exported function, documented in src/dom/watcher.h */ -nsl_error nsl_dom_watcher_destroy( +bool nsl_dom_watcher_destroy( struct nsl_dom_watcher *watcher) { nsl__dom_listener_destroy(watcher->listener, watcher->document); free(watcher); - return NSL_OK; + return true; } diff --git a/src/dom/watcher.h b/test/dom/watcher.h index 2cfa26e..cf26a56 100644 --- a/src/dom/watcher.h +++ b/test/dom/watcher.h @@ -36,7 +36,7 @@ enum nsl_dom_watcher_type { * \param[in] pw The dom watcher owner's private data. * \return NSL_OK on success, appropriate error otherwise. */ -typedef nsl_error (*nsl_dom_watcher_cb)( +typedef bool (*nsl_dom_watcher_cb)( enum nsl_dom_watcher_type type, dom_event_target *node, dom_node_type node_type, @@ -52,7 +52,7 @@ typedef nsl_error (*nsl_dom_watcher_cb)( * \param[in] pw Private data passed back to `watcher_cb`. * \return NSL_OK on success, appropriate error otherwise. */ -nsl_error nsl_dom_watcher_create( +bool nsl_dom_watcher_create( struct nsl_dom_watcher **watcher_out, dom_document *document, nsl_dom_watcher_cb watcher_cb, @@ -65,7 +65,7 @@ nsl_error nsl_dom_watcher_create( * \param[in] watcher DOM change watcher to destroy. * \return NSL_OK on success, appropriate error otherwise. */ -nsl_error nsl_dom_watcher_destroy( +bool nsl_dom_watcher_destroy( struct nsl_dom_watcher *watcher); #endif diff --git a/test/test-loader.c b/test/test-loader.c index cab08d6..5cbd788 100644 --- a/test/test-loader.c +++ b/test/test-loader.c @@ -15,6 +15,8 @@ #include <libnslayout/nslayout.h> +#include "dom/watcher.h" + #ifndef UNUSED #define UNUSED(x) (void)(x) #endif @@ -42,11 +44,46 @@ struct test_loader_ctx { dom_document *doc; css_select_ctx *css_ctx; css_stylesheet *css_sheet; + + struct nsl_dom_watcher *watcher; }; +/** + * Callback function for dom modifications. + * + * \param[in] type The mutation type. + * \param[in] node The target node. (Caller yields ownership.) + * \param[in] node_type The type of node. + * \param[in] pw The layout object. + * \return NSL_OK on success, appropriate error otherwise. + */ +static bool nsl_layout_dom_watcher_cb( + enum nsl_dom_watcher_type type, + dom_event_target *node, + dom_node_type node_type, + void *pw) +{ + nsl_layout *layout = pw; + + UNUSED(type); + UNUSED(layout); + UNUSED(node_type); + + /* TODO: Based on event type: + * 1. call to do (re)selection: + * a. all nodes? + * b. just this node? + * 2. call to update layout, if needed. + */ + + dom_node_unref(node); + + return true; +} + + static bool test_loader_doc_load_start( - size_t chunk_length, struct test_loader_ctx *load_ctx) { dom_hubbub_parser_params params; @@ -67,21 +104,6 @@ static bool test_loader_doc_load_start( return false; } - /* Find length of first chunk */ - if (chunk_length > (load_ctx->html->len - load_ctx->html->pos)) - chunk_length = load_ctx->html->len - load_ctx->html->pos; - - /* Load first chunk */ - error = dom_hubbub_parser_parse_chunk(load_ctx->parser, - load_ctx->html->buf + load_ctx->html->pos, - chunk_length); - load_ctx->html->pos += chunk_length; - if (error != DOM_HUBBUB_OK) { - dom_hubbub_parser_destroy(load_ctx->parser); - printf("Parsing errors occur\n"); - return false; - } - return true; } @@ -106,7 +128,6 @@ bool test_loader_doc_load_next( chunk_length); load_ctx->html->pos += chunk_length; if (error != DOM_HUBBUB_OK) { - dom_hubbub_parser_destroy(load_ctx->parser); printf("Parsing errors occur\n"); return false; } @@ -122,14 +143,10 @@ bool test_loader_doc_load_next( /* Done parsing file */ error = dom_hubbub_parser_completed(load_ctx->parser); if (error != DOM_HUBBUB_OK) { - dom_hubbub_parser_destroy(load_ctx->parser); printf("Parsing error when construct DOM\n"); return false; } - /* Finished with parser */ - dom_hubbub_parser_destroy(load_ctx->parser); - return true; } @@ -308,11 +325,18 @@ static bool test_loader( load_ctx.css_ctx = NULL; printf("Starting load\n"); - if (!test_loader_doc_load_start(chunk_size, &load_ctx)) { + if (!test_loader_doc_load_start(&load_ctx)) { printf("ERROR: doc_load_start\n"); goto fail; } + printf("Adding dom watcher\n"); + if (!nsl_dom_watcher_create(&load_ctx.watcher, load_ctx.doc, + nsl_layout_dom_watcher_cb, &load_ctx)) { + printf("ERROR: nsl_dom_watcher_create\n"); + goto fail; + } + printf("Creating style context\n"); if (!test_loader_css_init(&load_ctx)) { printf("ERROR: create_style_context\n"); @@ -349,8 +373,15 @@ fail: nsl_layout_destroy(layout); } test_loader_css_fini(&load_ctx); + + nsl_dom_watcher_destroy(load_ctx.watcher); + dom_node_unref(load_ctx.doc); + if (load_ctx.parser != NULL) { + dom_hubbub_parser_destroy(load_ctx.parser); + } + return ret; } |