Gitweb links:
...log
http://git.netsurf-browser.org/libnslayout.git/shortlog/f18b7ad86fe25e32cac7d9bca1abe92c46437535
...commit
http://git.netsurf-browser.org/libnslayout.git/commit/f18b7ad86fe25e32cac7d9bca1abe92c46437535
...tree
http://git.netsurf-browser.org/libnslayout.git/tree/f18b7ad86fe25e32cac7d9bca1abe92c46437535
The branch, master has been updated
via f18b7ad86fe25e32cac7d9bca1abe92c46437535 (commit)
via 39f7357b388d2e00d307419ccd2d761180eb5c5c (commit)
via c58cc57253109efe1e7bc3b7b90b177e11f55fbb (commit)
via 1f2942faaf00b22b58747d51c7b186a65f041606 (commit)
via 5ab8be1d772f617f505d80e06910ebdd5fdc06ab (commit)
via f3983f3743eebf1006877527b9e787e1189ebed1 (commit)
via bba349d0729f12f1631c776e9581d6feb39d314d (commit)
from c8e49af1cbd7d17b37f579bdb8d02bfcc6d982bb (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=f18b7ad86fe25e32cac7d9bca1abe92c46437535
commit f18b7ad86fe25e32cac7d9bca1abe92c46437535
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
dom watcher: Add callback for dom mutation watching.
diff --git a/src/dom/watcher.c b/src/dom/watcher.c
index cb775c6..d515cf6 100644
--- a/src/dom/watcher.c
+++ b/src/dom/watcher.c
@@ -31,8 +31,10 @@
* A dom watcher object
*/
struct nsl_dom_watcher {
- dom_document *document;
- dom_event_listener *listener; /**< DOM event listener object */
+ dom_document *document; /**< DOM document */
+ dom_event_listener *listener; /**< DOM event listener object */
+ nsl_dom_watcher_cb watcher_cb; /**< Client callback */
+ void *pw; /**< Client data */
};
/**
@@ -44,17 +46,65 @@ struct nsl_dom_watcher {
static void nsl__dom_event_handler(struct dom_event *evt, void *pw)
{
const struct nsl_dom_watcher *watcher = pw;
-
- UNUSED(watcher);
+ enum nsl_dom_watcher_type type;
+ dom_event_target *node = NULL;
+ dom_string *evt_type = NULL;
+ dom_node_type node_type;
+ dom_exception exc;
nsl_dom_debug_dump_event(evt);
- /* 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.
- */
+ exc = dom_event_get_target(evt, &node);
+ if ((exc != DOM_NO_ERR) || (node == NULL)) {
+ printf("FAILED to get target node!\n");
+ goto fail;
+ }
+
+ exc = dom_node_get_node_type(node, &node_type);
+ if (exc != DOM_NO_ERR) {
+ printf("FAILED to get target node type!\n");
+ goto fail;
+ }
+
+ exc = dom_event_get_type(evt, &evt_type);
+ if ((exc != DOM_NO_ERR) || (evt_type == NULL)) {
+ printf("FAILED to get event type!\n");
+ goto fail;
+ }
+
+ if (dom_string_isequal(evt_type,
+ nsl_dom_str_node_inserted)) {
+ type = NSL_DOM_WATCHER_NODE_INSERTED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_node_removed)) {
+ type = NSL_DOM_WATCHER_NODE_REMOVED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_subtree_modified)) {
+ type = NSL_DOM_WATCHER_SUBTREE_MODIFIED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_attr_modified)) {
+ type = NSL_DOM_WATCHER_ATTR_MODIFIED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_characterdata_modified)) {
+ type = NSL_DOM_WATCHER_CHAR_DATA_MODIFIED;
+ } else {
+ printf("FAILED: unrecognised event type: '%s'",
+ dom_string_data(evt_type));
+ goto fail;
+ }
+
+ dom_string_unref(evt_type);
+
+ watcher->watcher_cb(type, node, node_type, watcher->pw);
+ return;
+
+fail:
+ if (evt_type != NULL) dom_string_unref(evt_type);
+ if (node != NULL) dom_node_unref(node);
}
@@ -144,7 +194,9 @@ error:
/* Exported function, documented in src/dom/watcher.h */
nslayout_error nsl_dom_watcher_create(
struct nsl_dom_watcher **watcher_out,
- dom_document *document)
+ dom_document *document,
+ nsl_dom_watcher_cb watcher_cb,
+ void *pw)
{
struct nsl_dom_watcher *watcher;
nslayout_error err;
@@ -155,6 +207,8 @@ nslayout_error nsl_dom_watcher_create(
}
watcher->document = document;
+ watcher->watcher_cb = watcher_cb;
+ watcher->pw = pw;
err = nsl__dom_listener_create(&watcher->listener, watcher);
if (err != NSLAYOUT_OK) {
diff --git a/src/dom/watcher.h b/src/dom/watcher.h
index a6f2568..0883fd2 100644
--- a/src/dom/watcher.h
+++ b/src/dom/watcher.h
@@ -15,15 +15,49 @@ struct dom_document;
struct nsl_dom_watcher;
/**
+ * DOM watcher's mutation types
+ */
+enum nsl_dom_watcher_type {
+ NSL_DOM_WATCHER_NODE_INSERTED,
+ NSL_DOM_WATCHER_NODE_REMOVED,
+ NSL_DOM_WATCHER_SUBTREE_MODIFIED,
+ NSL_DOM_WATCHER_ATTR_MODIFIED,
+ NSL_DOM_WATCHER_CHAR_DATA_MODIFIED,
+ NSL_DOM_WATCHER__COUNT,
+};
+
+
+/**
+ * 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 dom watcher owner's private data.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+typedef nslayout_error (*nsl_dom_watcher_cb)(
+ enum nsl_dom_watcher_type type,
+ dom_event_target *node,
+ dom_node_type node_type,
+ void *pw);
+
+
+/**
* Create DOM change watcher for a DOM document.
*
* \param[out] watcher_out Returns a dom watcher object for layout.
* \param[in] document DOM document to create watcher for.
+ * \param[in] watcher_cb Function to call when dom modification happens.
+ * \param[in] pw Private data passed back to `watcher_cb`.
* \return NSLAYOUT_OK on success, appropriate error otherwise.
*/
nslayout_error nsl_dom_watcher_create(
struct nsl_dom_watcher **watcher_out,
- dom_document *document);
+ dom_document *document,
+ nsl_dom_watcher_cb watcher_cb,
+ void *pw);
+
/**
* Destroy a document change DOM change watcher.
diff --git a/src/layout.c b/src/layout.c
index d894f13..e4824bf 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -14,6 +14,7 @@
#include "libnslayout/nslayout.h"
#include "layout.h"
+#include "util/util.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
@@ -46,6 +47,40 @@ nslayout_error nslayout_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 NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl_layout_dom_watcher_cb(
+ enum nsl_dom_watcher_type type,
+ dom_event_target *node,
+ dom_node_type node_type,
+ void *pw)
+{
+ nslayout_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 NSLAYOUT_OK;
+}
+
+
/* Publically exported function, documented in include/libnslayout/nslayout.h
*/
nslayout_error nslayout_layout_create(
dom_document *doc,
@@ -75,7 +110,8 @@ nslayout_error nslayout_layout_create(
l->cb = cb;
l->pw = pw;
- err = nsl_dom_watcher_create(&l->watcher, l->document);
+ err = nsl_dom_watcher_create(&l->watcher, l->document,
+ nsl_layout_dom_watcher_cb, l);
if (err != NSLAYOUT_OK) {
return err;
}
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=39f7357b388d2e00d307419ccd2d761180eb5c5c
commit 39f7357b388d2e00d307419ccd2d761180eb5c5c
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
Header #include cleanup.
diff --git a/src/dom/watcher.c b/src/dom/watcher.c
index c33702e..cb775c6 100644
--- a/src/dom/watcher.c
+++ b/src/dom/watcher.c
@@ -13,10 +13,13 @@
* gain a LibDOM-specific extension API.
*/
+#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
+#include <stdbool.h>
+
+#include "libnslayout/nslayout.h"
#include "dom/debug.h"
#include "dom/watcher.h"
diff --git a/src/dom/watcher.h b/src/dom/watcher.h
index 2894db6..a6f2568 100644
--- a/src/dom/watcher.h
+++ b/src/dom/watcher.h
@@ -11,8 +11,7 @@
#ifndef nslayout_dom_watcher_h_
#define nslayout_dom_watcher_h_
-#include <libnslayout/nslayout.h>
-
+struct dom_document;
struct nsl_dom_watcher;
/**
diff --git a/src/layout.c b/src/layout.c
index ee3d507..d894f13 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -11,7 +11,7 @@
#include <assert.h>
#include <stdlib.h>
-#include <libnslayout/nslayout.h>
+#include "libnslayout/nslayout.h"
#include "layout.h"
#include "dom/watcher.h"
diff --git a/src/util/dom-str.c b/src/util/dom-str.c
index 73bfe63..3c0865a 100644
--- a/src/util/dom-str.c
+++ b/src/util/dom-str.c
@@ -10,6 +10,8 @@
#include <stdio.h>
+#include "libnslayout/nslayout.h"
+
#include "util/dom-str.h"
#include "util/util.h"
diff --git a/src/util/dom-str.h b/src/util/dom-str.h
index 7c130c3..9ce7bb9 100644
--- a/src/util/dom-str.h
+++ b/src/util/dom-str.h
@@ -11,8 +11,6 @@
#ifndef nslayout_util_dom_str_h_
#define nslayout_util_dom_str_h_
-#include <libnslayout/nslayout.h>
-
extern dom_string *nsl_dom_str_node_inserted;
extern dom_string *nsl_dom_str_node_removed;
extern dom_string *nsl_dom_str_subtree_modified;
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=c58cc57253109efe1e7bc3b7b90b177e11f55fbb
commit c58cc57253109efe1e7bc3b7b90b177e11f55fbb
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
layout: Make the layout object opaque.
diff --git a/src/layout.c b/src/layout.c
index 6921a4d..ee3d507 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -11,11 +11,27 @@
#include <assert.h>
#include <stdlib.h>
+#include <libnslayout/nslayout.h>
+
#include "layout.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
+/**
+ * The layout object for a DOM document
+ */
+struct nslayout_layout {
+ dom_document *document;
+ css_select_ctx *css_ctx;
+ css_media_type *media;
+ nslayout_callback cb;
+ void *pw;
+
+ struct nsl_dom_watcher *watcher;
+};
+
+
/* Publically exported function, documented in include/libnslayout/nslayout.h
*/
nslayout_error nslayout_init(void)
{
diff --git a/src/layout.h b/src/layout.h
index 3bc9a30..d8fa3b2 100644
--- a/src/layout.h
+++ b/src/layout.h
@@ -11,19 +11,6 @@
#ifndef nslayout_layout_h_
#define nslayout_layout_h_
-#include <libnslayout/nslayout.h>
-
-/**
- * The layout object for a DOM document
- */
-struct nslayout_layout {
- dom_document *document;
- css_select_ctx *css_ctx;
- css_media_type *media;
- nslayout_callback cb;
- void *pw;
-
- struct nsl_dom_watcher *watcher;
-};
+struct nslayout_layout;
#endif
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=1f2942faaf00b22b58747d51c7b186a65f041606
commit 1f2942faaf00b22b58747d51c7b186a65f041606
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
dom watcher: Turn the dom watcher into an opaque object.
diff --git a/src/dom/watcher.c b/src/dom/watcher.c
index 7de4f31..c33702e 100644
--- a/src/dom/watcher.c
+++ b/src/dom/watcher.c
@@ -5,7 +5,12 @@
*/
/** \file src/dom/watcher.c
- * DOM mutation handling
+ * Implementation of DOM mutation watching.
+ *
+ * TODO: LibDOM mutation event listeners are really slow.
+ * Need to find a better way to get DOM change notifications.
+ * LibDOM probably needs to gain Mutation Observer support, or
+ * gain a LibDOM-specific extension API.
*/
#include <assert.h>
@@ -13,21 +18,31 @@
#include <stdio.h>
#include <string.h>
-#include "layout.h"
#include "dom/debug.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
#include "util/util.h"
+
+/**
+ * A dom watcher object
+ */
+struct nsl_dom_watcher {
+ dom_document *document;
+ dom_event_listener *listener; /**< DOM event listener object */
+};
+
/**
* LibDOM event handler
*
- * \param[in] evt The LibDOM event object
- * \param[in] pw Pointer to our nslayout_layout object
+ * \param[in] evt The LibDOM event object
+ * \param[in] pw Pointer to our dom watcher object
*/
static void nsl__dom_event_handler(struct dom_event *evt, void *pw)
{
- UNUSED(pw);
+ const struct nsl_dom_watcher *watcher = pw;
+
+ UNUSED(watcher);
nsl_dom_debug_dump_event(evt);
@@ -39,77 +54,121 @@ static void nsl__dom_event_handler(struct dom_event *evt,
void *pw)
*/
}
-/* Exported function, documented in src/dom/watcher.h */
-nslayout_error nsl_dom_watcher_add_for_layout(nslayout_layout *layout)
+
+/**
+ * Destroy a DOM document's event listener
+ *
+ * \param[in] listener The listener to destroy
+ * \param[in] document The document that the listener was registerd for.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl__dom_listener_destroy(
+ dom_event_listener *listener,
+ dom_document *document)
{
dom_exception exc;
- /* TODO: LibDOM mutation event listeners are really slow.
- * Need to find a better way to get DOM change notifications.
- * LibDOM probably needs to gain Mutation Observer support.
- */
+ /* Passing NULL as type, removes listener for all event types. */
+ exc = dom_event_target_remove_event_listener(
+ document, NULL, listener, false);
+ if (exc != DOM_NO_ERR) {
+ return NSL_DOM_ERR(exc);
+ }
+
+ dom_event_listener_unref(listener);
+
+ return NSLAYOUT_OK;
+}
- assert(layout->listener == NULL);
+/**
+ * Create a dom event listener.
+ *
+ * \param[out] listener_out Returns a dom listener for watcher's document.
+ * \param[in] watcher DOM watcher object that listener is used for.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl__dom_listener_create(
+ dom_event_listener **listener_out,
+ const struct nsl_dom_watcher *watcher)
+{
+ dom_event_listener *listener;
+ dom_exception exc;
+
+ /* Create listener */
exc = dom_event_listener_create(nsl__dom_event_handler,
- layout, &layout->listener);
+ (void *) watcher, &listener);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_node_inserted,
- layout->listener, false);
+ /* Set the event types it should listen to */
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_node_inserted, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_subtree_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_subtree_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_node_removed,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_node_removed, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_attr_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_attr_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_characterdata_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_characterdata_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
+ *listener_out = listener;
return NSLAYOUT_OK;
error:
- nsl_dom_watcher_remove_for_layout(layout);
+ nsl__dom_listener_destroy(listener, watcher->document);
return NSL_DOM_ERR(exc);
}
-/* Exported function, documented in src/dom/event.h */
-nslayout_error nsl_dom_watcher_remove_for_layout(nslayout_layout *layout)
+/* Exported function, documented in src/dom/watcher.h */
+nslayout_error nsl_dom_watcher_create(
+ struct nsl_dom_watcher **watcher_out,
+ dom_document *document)
{
- dom_exception exc;
+ struct nsl_dom_watcher *watcher;
+ nslayout_error err;
- /* Passing NULL as type, removes listener for all event types. */
- exc = dom_event_target_remove_event_listener(
- layout->doc, NULL, layout->listener, false);
- if (exc != DOM_NO_ERR) {
- return NSL_DOM_ERR(exc);
+ watcher = malloc(sizeof(*watcher));
+ if (watcher == NULL) {
+ return NSLAYOUT_NO_MEM;
+ }
+
+ watcher->document = document;
+
+ err = nsl__dom_listener_create(&watcher->listener, watcher);
+ if (err != NSLAYOUT_OK) {
+ free(watcher);
+ return err;
}
- dom_event_listener_unref(layout->listener);
- layout->listener = NULL;
+ *watcher_out = watcher;
+ return NSLAYOUT_OK;
+}
+
+/* Exported function, documented in src/dom/watcher.h */
+nslayout_error nsl_dom_watcher_destroy(
+ struct nsl_dom_watcher *watcher)
+{
+ nsl__dom_listener_destroy(watcher->listener, watcher->document);
+ free(watcher);
return NSLAYOUT_OK;
}
diff --git a/src/dom/watcher.h b/src/dom/watcher.h
index 87f0a1b..2894db6 100644
--- a/src/dom/watcher.h
+++ b/src/dom/watcher.h
@@ -5,7 +5,7 @@
*/
/** \file src/dom/watcher.h
- * Layout object handling
+ * Interface to DOM mutation watching.
*/
#ifndef nslayout_dom_watcher_h_
@@ -13,20 +13,26 @@
#include <libnslayout/nslayout.h>
+struct nsl_dom_watcher;
+
/**
- * Add DOM change watchers to the layout's document.'
+ * Create DOM change watcher for a DOM document.
*
- * \param[in] layout nslayout_layout object to set watchers for.
+ * \param[out] watcher_out Returns a dom watcher object for layout.
+ * \param[in] document DOM document to create watcher for.
* \return NSLAYOUT_OK on success, appropriate error otherwise.
*/
-nslayout_error nsl_dom_watcher_add_for_layout(nslayout_layout *layout);
+nslayout_error nsl_dom_watcher_create(
+ struct nsl_dom_watcher **watcher_out,
+ dom_document *document);
/**
- * Remove DOM change watchers from the layout's document.
+ * Destroy a document change DOM change watcher.
*
- * \param[in] layout nslayout_layout object remove watchers from.
+ * \param[in] watcher DOM change watcher to destroy.
* \return NSLAYOUT_OK on success, appropriate error otherwise.
*/
-nslayout_error nsl_dom_watcher_remove_for_layout(nslayout_layout *layout);
+nslayout_error nsl_dom_watcher_destroy(
+ struct nsl_dom_watcher *watcher);
#endif
diff --git a/src/layout.c b/src/layout.c
index c660c82..6921a4d 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -53,13 +53,13 @@ nslayout_error nslayout_layout_create(
}
/* TODO: Decide: ownership will probably be passed to libnslayout */
- l->doc = doc;
+ l->document = doc;
l->css_ctx = css_ctx;
l->media = media;
l->cb = cb;
l->pw = pw;
- err = nsl_dom_watcher_add_for_layout(l);
+ err = nsl_dom_watcher_create(&l->watcher, l->document);
if (err != NSLAYOUT_OK) {
return err;
}
@@ -78,7 +78,7 @@ nslayout_error nslayout_layout_destroy(
assert(layout != NULL);
/* TODO: free/unref the stuff we own in the layout */
- err = nsl_dom_watcher_remove_for_layout(layout);
+ err = nsl_dom_watcher_destroy(layout->watcher);
if (err != NSLAYOUT_OK) {
return err;
}
diff --git a/src/layout.h b/src/layout.h
index 3549174..3bc9a30 100644
--- a/src/layout.h
+++ b/src/layout.h
@@ -17,13 +17,13 @@
* The layout object for a DOM document
*/
struct nslayout_layout {
- dom_document *doc;
+ dom_document *document;
css_select_ctx *css_ctx;
css_media_type *media;
nslayout_callback cb;
void *pw;
- dom_event_listener *listener;
+ struct nsl_dom_watcher *watcher;
};
#endif
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=5ab8be1d772f617f505d80e06910ebdd5fdc06ab
commit 5ab8be1d772f617f505d80e06910ebdd5fdc06ab
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
layout: Remove debug.
diff --git a/src/layout.c b/src/layout.c
index 1f90951..c660c82 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -10,7 +10,6 @@
#include <assert.h>
#include <stdlib.h>
-#include <stdio.h>
#include "layout.h"
#include "dom/watcher.h"
@@ -48,8 +47,6 @@ nslayout_error nslayout_layout_create(
assert(media != NULL);
assert(cb != NULL);
- printf("Called layout_create\n");
-
l = calloc(1, sizeof(nslayout_layout));
if (l == NULL) {
return NSLAYOUT_NO_MEM;
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=f3983f3743eebf1006877527b9e787e1189ebed1
commit f3983f3743eebf1006877527b9e787e1189ebed1
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
dom debug: Split watcher event dump out into debug module.
diff --git a/src/dom/Makefile b/src/dom/Makefile
index 0baa497..0b345cb 100644
--- a/src/dom/Makefile
+++ b/src/dom/Makefile
@@ -6,6 +6,8 @@
# Released under the ISC License (see COPYING file)
# Sources
-DIR_SOURCES := watcher.c
+DIR_SOURCES := \
+ debug.c \
+ watcher.c
include $(NSBUILD)/Makefile.subdir
diff --git a/src/dom/debug.c b/src/dom/debug.c
new file mode 100644
index 0000000..468c2f7
--- /dev/null
+++ b/src/dom/debug.c
@@ -0,0 +1,103 @@
+/*
+ * This file is part of LibNSLayout
+ * Licensed under the ISC License, http://opensource.org/licenses/ISC
+ * Copyright 2015-2017 Michael Drake <[email protected]>
+ */
+
+/** \file src/dom/debug.c
+ * DOM debug
+ */
+
+#include <stdio.h>
+
+#include <dom/dom.h>
+
+#include "dom/debug.h"
+
+
+#ifdef NSL_DOM_TRACE
+
+/**
+ * Convert a dom node type to a string
+ *
+ * \param[in] type DOM node type
+ * \return appropriate string.
+ */
+static inline const char *nsl__dom_node_type_to_string(dom_node_type type)
+{
+ static const char *str[DOM_NODE_TYPE_COUNT] = {
+ [DOM_ELEMENT_NODE] = "ELEMENT_NODE",
+ [DOM_ATTRIBUTE_NODE] = "ATTRIBUTE_NODE",
+ [DOM_TEXT_NODE] = "TEXT_NODE",
+ [DOM_CDATA_SECTION_NODE] = "CDATA_SECTION_NODE",
+ [DOM_ENTITY_REFERENCE_NODE] = "ENTITY_REFERENCE_NODE",
+ [DOM_ENTITY_NODE] = "ENTITY_NODE",
+ [DOM_PROCESSING_INSTRUCTION_NODE] =
"PROCESSING_INSTRUCTION_NODE",
+ [DOM_COMMENT_NODE] = "COMMENT_NODE",
+ [DOM_DOCUMENT_NODE] = "DOCUMENT_NODE",
+ [DOM_DOCUMENT_TYPE_NODE] = "DOCUMENT_TYPE_NODE",
+ [DOM_DOCUMENT_FRAGMENT_NODE] = "DOCUMENT_FRAGMENT_NODE",
+ [DOM_NOTATION_NODE] = "NOTATION_NODE"
+ };
+
+ return str[type];
+}
+
+
+/* Exported function, documented in src/dom/debug.h */
+void nsl__dom_debug_dump_event(const struct dom_event *evt)
+{
+ dom_event_target *node = NULL;
+ dom_node_type node_type;
+ dom_string *name = NULL;
+ dom_string *type = NULL;
+ dom_exception exc;
+
+ printf(" DOM Event: ");
+
+ /* Ugly test to see what events come out */
+ exc = dom_event_get_target(evt, &node);
+ if ((exc != DOM_NO_ERR) || (node == NULL)) {
+ printf("FAILED to get target node!\n");
+ goto fail;
+ }
+
+ exc = dom_node_get_node_type(node, &node_type);
+ if (exc != DOM_NO_ERR) {
+ printf("FAILED to get target node type!\n");
+ goto fail;
+ }
+
+ if (node_type == DOM_ELEMENT_NODE) {
+ exc = dom_node_get_node_name(node, &name);
+ if ((exc != DOM_NO_ERR) || (name == NULL)) {
+ printf("FAILED to get target node name!\n");
+ goto fail;
+ }
+ }
+
+ exc = dom_event_get_type(evt, &type);
+ if ((exc != DOM_NO_ERR) || (type == NULL)) {
+ printf("FAILED to get event type!\n");
+ goto fail;
+ }
+
+ if (node_type == DOM_ELEMENT_NODE) {
+ printf("<%s> %s",
+ dom_string_data(name),
+ dom_string_data(type));
+ } else {
+ printf("%s %s",
+ nsl__dom_node_type_to_string(node_type),
+ dom_string_data(type));
+ }
+
+fail:
+ if (type != NULL) dom_string_unref(type);
+ if (name != NULL) dom_string_unref(name);
+ if (node != NULL) dom_node_unref(node);
+
+ printf("\n");
+}
+
+#endif
diff --git a/src/dom/debug.h b/src/dom/debug.h
new file mode 100644
index 0000000..7d6b186
--- /dev/null
+++ b/src/dom/debug.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of LibNSLayout
+ * Licensed under the ISC License, http://opensource.org/licenses/ISC
+ * Copyright 2015-2017 Michael Drake <[email protected]>
+ */
+
+/** \file src/dom/debug.h
+ * DOM debug
+ */
+
+#ifndef nslayout_dom_debug_h_
+#define nslayout_dom_debug_h_
+
+/** Define to enable DOM trace debug output */
+#undef NSL_DOM_TRACE
+#define NSL_DOM_TRACE
+
+#include "util/util.h"
+
+struct dom_event;
+
+/** Don't use directly */
+void nsl__dom_debug_dump_event(const struct dom_event *evt);
+
+
+/**
+ * Dump debug for dom event
+ *
+ * \param[in] evt Dump debug concerning a DOM event.
+ */
+static inline void nsl_dom_debug_dump_event(const struct dom_event *evt)
+{
+#ifdef NSL_DOM_TRACE
+ nsl__dom_debug_dump_event(evt);
+#else
+ UNUSED(evt);
+#endif
+}
+
+
+#endif
diff --git a/src/dom/watcher.c b/src/dom/watcher.c
index 0788354..7de4f31 100644
--- a/src/dom/watcher.c
+++ b/src/dom/watcher.c
@@ -14,37 +14,12 @@
#include <string.h>
#include "layout.h"
+#include "dom/debug.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
#include "util/util.h"
/**
- * Convert a dom node type to a string
- *
- * \param[in] type DOM node type
- * \return appropriate string.
- */
-static const char *nsl__dom_node_type_to_string(dom_node_type type)
-{
- static const char *str[DOM_NODE_TYPE_COUNT] = {
- [DOM_ELEMENT_NODE] = "ELEMENT_NODE",
- [DOM_ATTRIBUTE_NODE] = "ATTRIBUTE_NODE",
- [DOM_TEXT_NODE] = "TEXT_NODE",
- [DOM_CDATA_SECTION_NODE] = "CDATA_SECTION_NODE",
- [DOM_ENTITY_REFERENCE_NODE] = "ENTITY_REFERENCE_NODE",
- [DOM_ENTITY_NODE] = "ENTITY_NODE",
- [DOM_PROCESSING_INSTRUCTION_NODE] =
"PROCESSING_INSTRUCTION_NODE",
- [DOM_COMMENT_NODE] = "COMMENT_NODE",
- [DOM_DOCUMENT_NODE] = "DOCUMENT_NODE",
- [DOM_DOCUMENT_TYPE_NODE] = "DOCUMENT_TYPE_NODE",
- [DOM_DOCUMENT_FRAGMENT_NODE] = "DOCUMENT_FRAGMENT_NODE",
- [DOM_NOTATION_NODE] = "NOTATION_NODE"
- };
-
- return str[type];
-}
-
-/**
* LibDOM event handler
*
* \param[in] evt The LibDOM event object
@@ -52,52 +27,9 @@ static const char
*nsl__dom_node_type_to_string(dom_node_type type)
*/
static void nsl__dom_event_handler(struct dom_event *evt, void *pw)
{
- dom_event_target *node = NULL;
- dom_node_type node_type;
- dom_string *name = NULL;
- dom_string *type = NULL;
- dom_exception exc;
-
UNUSED(pw);
- printf(" DOM Event: ");
-
- /* Ugly test to see what events come out */
- exc = dom_event_get_target(evt, &node);
- if ((exc != DOM_NO_ERR) || (node == NULL)) {
- printf("FAILED to get target node!\n");
- goto fail;
- }
-
- exc = dom_node_get_node_type(node, &node_type);
- if (exc != DOM_NO_ERR) {
- printf("FAILED to get target node type!\n");
- goto fail;
- }
-
- if (node_type == DOM_ELEMENT_NODE) {
- exc = dom_node_get_node_name(node, &name);
- if ((exc != DOM_NO_ERR) || (name == NULL)) {
- printf("FAILED to get target node name!\n");
- goto fail;
- }
- }
-
- exc = dom_event_get_type(evt, &type);
- if ((exc != DOM_NO_ERR) || (type == NULL)) {
- printf("FAILED to get event type!\n");
- goto fail;
- }
-
- if (node_type == DOM_ELEMENT_NODE) {
- printf("<%s> %s",
- dom_string_data(name),
- dom_string_data(type));
- } else {
- printf("%s %s",
- nsl__dom_node_type_to_string(node_type),
- dom_string_data(type));
- }
+ nsl_dom_debug_dump_event(evt);
/* TODO: Based on event type:
* 1. call to do (re)selection:
@@ -105,16 +37,9 @@ static void nsl__dom_event_handler(struct dom_event *evt,
void *pw)
* b. just this node?
* 2. call to update layout, if needed.
*/
-
-fail:
- if (type != NULL) dom_string_unref(type);
- if (name != NULL) dom_string_unref(name);
- if (node != NULL) dom_node_unref(node);
-
- printf("\n");
}
-/* Exported function, documented in src/dom/event.h */
+/* Exported function, documented in src/dom/watcher.h */
nslayout_error nsl_dom_watcher_add_for_layout(nslayout_layout *layout)
{
dom_exception exc;
commitdiff
http://git.netsurf-browser.org/libnslayout.git/commit/?id=bba349d0729f12f1631c776e9581d6feb39d314d
commit bba349d0729f12f1631c776e9581d6feb39d314d
Author: Michael Drake <[email protected]>
Commit: Michael Drake <[email protected]>
docs: Add comment for `struct nslayout_layout`.
diff --git a/src/layout.h b/src/layout.h
index e71d5aa..3549174 100644
--- a/src/layout.h
+++ b/src/layout.h
@@ -13,6 +13,9 @@
#include <libnslayout/nslayout.h>
+/**
+ * The layout object for a DOM document
+ */
struct nslayout_layout {
dom_document *doc;
css_select_ctx *css_ctx;
-----------------------------------------------------------------------
Summary of changes:
src/dom/Makefile | 4 +-
src/dom/debug.c | 103 +++++++++++++++++++++++
src/dom/debug.h | 41 ++++++++++
src/dom/watcher.c | 231 +++++++++++++++++++++++++++++++---------------------
src/dom/watcher.h | 55 +++++++++++--
src/layout.c | 61 ++++++++++++--
src/layout.h | 12 +--
src/util/dom-str.c | 2 +
src/util/dom-str.h | 2 -
9 files changed, 388 insertions(+), 123 deletions(-)
create mode 100644 src/dom/debug.c
create mode 100644 src/dom/debug.h
diff --git a/src/dom/Makefile b/src/dom/Makefile
index 0baa497..0b345cb 100644
--- a/src/dom/Makefile
+++ b/src/dom/Makefile
@@ -6,6 +6,8 @@
# Released under the ISC License (see COPYING file)
# Sources
-DIR_SOURCES := watcher.c
+DIR_SOURCES := \
+ debug.c \
+ watcher.c
include $(NSBUILD)/Makefile.subdir
diff --git a/src/dom/debug.c b/src/dom/debug.c
new file mode 100644
index 0000000..468c2f7
--- /dev/null
+++ b/src/dom/debug.c
@@ -0,0 +1,103 @@
+/*
+ * This file is part of LibNSLayout
+ * Licensed under the ISC License, http://opensource.org/licenses/ISC
+ * Copyright 2015-2017 Michael Drake <[email protected]>
+ */
+
+/** \file src/dom/debug.c
+ * DOM debug
+ */
+
+#include <stdio.h>
+
+#include <dom/dom.h>
+
+#include "dom/debug.h"
+
+
+#ifdef NSL_DOM_TRACE
+
+/**
+ * Convert a dom node type to a string
+ *
+ * \param[in] type DOM node type
+ * \return appropriate string.
+ */
+static inline const char *nsl__dom_node_type_to_string(dom_node_type type)
+{
+ static const char *str[DOM_NODE_TYPE_COUNT] = {
+ [DOM_ELEMENT_NODE] = "ELEMENT_NODE",
+ [DOM_ATTRIBUTE_NODE] = "ATTRIBUTE_NODE",
+ [DOM_TEXT_NODE] = "TEXT_NODE",
+ [DOM_CDATA_SECTION_NODE] = "CDATA_SECTION_NODE",
+ [DOM_ENTITY_REFERENCE_NODE] = "ENTITY_REFERENCE_NODE",
+ [DOM_ENTITY_NODE] = "ENTITY_NODE",
+ [DOM_PROCESSING_INSTRUCTION_NODE] =
"PROCESSING_INSTRUCTION_NODE",
+ [DOM_COMMENT_NODE] = "COMMENT_NODE",
+ [DOM_DOCUMENT_NODE] = "DOCUMENT_NODE",
+ [DOM_DOCUMENT_TYPE_NODE] = "DOCUMENT_TYPE_NODE",
+ [DOM_DOCUMENT_FRAGMENT_NODE] = "DOCUMENT_FRAGMENT_NODE",
+ [DOM_NOTATION_NODE] = "NOTATION_NODE"
+ };
+
+ return str[type];
+}
+
+
+/* Exported function, documented in src/dom/debug.h */
+void nsl__dom_debug_dump_event(const struct dom_event *evt)
+{
+ dom_event_target *node = NULL;
+ dom_node_type node_type;
+ dom_string *name = NULL;
+ dom_string *type = NULL;
+ dom_exception exc;
+
+ printf(" DOM Event: ");
+
+ /* Ugly test to see what events come out */
+ exc = dom_event_get_target(evt, &node);
+ if ((exc != DOM_NO_ERR) || (node == NULL)) {
+ printf("FAILED to get target node!\n");
+ goto fail;
+ }
+
+ exc = dom_node_get_node_type(node, &node_type);
+ if (exc != DOM_NO_ERR) {
+ printf("FAILED to get target node type!\n");
+ goto fail;
+ }
+
+ if (node_type == DOM_ELEMENT_NODE) {
+ exc = dom_node_get_node_name(node, &name);
+ if ((exc != DOM_NO_ERR) || (name == NULL)) {
+ printf("FAILED to get target node name!\n");
+ goto fail;
+ }
+ }
+
+ exc = dom_event_get_type(evt, &type);
+ if ((exc != DOM_NO_ERR) || (type == NULL)) {
+ printf("FAILED to get event type!\n");
+ goto fail;
+ }
+
+ if (node_type == DOM_ELEMENT_NODE) {
+ printf("<%s> %s",
+ dom_string_data(name),
+ dom_string_data(type));
+ } else {
+ printf("%s %s",
+ nsl__dom_node_type_to_string(node_type),
+ dom_string_data(type));
+ }
+
+fail:
+ if (type != NULL) dom_string_unref(type);
+ if (name != NULL) dom_string_unref(name);
+ if (node != NULL) dom_node_unref(node);
+
+ printf("\n");
+}
+
+#endif
diff --git a/src/dom/debug.h b/src/dom/debug.h
new file mode 100644
index 0000000..7d6b186
--- /dev/null
+++ b/src/dom/debug.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of LibNSLayout
+ * Licensed under the ISC License, http://opensource.org/licenses/ISC
+ * Copyright 2015-2017 Michael Drake <[email protected]>
+ */
+
+/** \file src/dom/debug.h
+ * DOM debug
+ */
+
+#ifndef nslayout_dom_debug_h_
+#define nslayout_dom_debug_h_
+
+/** Define to enable DOM trace debug output */
+#undef NSL_DOM_TRACE
+#define NSL_DOM_TRACE
+
+#include "util/util.h"
+
+struct dom_event;
+
+/** Don't use directly */
+void nsl__dom_debug_dump_event(const struct dom_event *evt);
+
+
+/**
+ * Dump debug for dom event
+ *
+ * \param[in] evt Dump debug concerning a DOM event.
+ */
+static inline void nsl_dom_debug_dump_event(const struct dom_event *evt)
+{
+#ifdef NSL_DOM_TRACE
+ nsl__dom_debug_dump_event(evt);
+#else
+ UNUSED(evt);
+#endif
+}
+
+
+#endif
diff --git a/src/dom/watcher.c b/src/dom/watcher.c
index 0788354..d515cf6 100644
--- a/src/dom/watcher.c
+++ b/src/dom/watcher.c
@@ -5,64 +5,55 @@
*/
/** \file src/dom/watcher.c
- * DOM mutation handling
+ * Implementation of DOM mutation watching.
+ *
+ * TODO: LibDOM mutation event listeners are really slow.
+ * Need to find a better way to get DOM change notifications.
+ * LibDOM probably needs to gain Mutation Observer support, or
+ * gain a LibDOM-specific extension API.
*/
+#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
+#include <stdbool.h>
+
+#include "libnslayout/nslayout.h"
-#include "layout.h"
+#include "dom/debug.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
#include "util/util.h"
+
/**
- * Convert a dom node type to a string
- *
- * \param[in] type DOM node type
- * \return appropriate string.
+ * A dom watcher object
*/
-static const char *nsl__dom_node_type_to_string(dom_node_type type)
-{
- static const char *str[DOM_NODE_TYPE_COUNT] = {
- [DOM_ELEMENT_NODE] = "ELEMENT_NODE",
- [DOM_ATTRIBUTE_NODE] = "ATTRIBUTE_NODE",
- [DOM_TEXT_NODE] = "TEXT_NODE",
- [DOM_CDATA_SECTION_NODE] = "CDATA_SECTION_NODE",
- [DOM_ENTITY_REFERENCE_NODE] = "ENTITY_REFERENCE_NODE",
- [DOM_ENTITY_NODE] = "ENTITY_NODE",
- [DOM_PROCESSING_INSTRUCTION_NODE] =
"PROCESSING_INSTRUCTION_NODE",
- [DOM_COMMENT_NODE] = "COMMENT_NODE",
- [DOM_DOCUMENT_NODE] = "DOCUMENT_NODE",
- [DOM_DOCUMENT_TYPE_NODE] = "DOCUMENT_TYPE_NODE",
- [DOM_DOCUMENT_FRAGMENT_NODE] = "DOCUMENT_FRAGMENT_NODE",
- [DOM_NOTATION_NODE] = "NOTATION_NODE"
- };
-
- return str[type];
-}
+struct nsl_dom_watcher {
+ dom_document *document; /**< DOM document */
+ dom_event_listener *listener; /**< DOM event listener object */
+ nsl_dom_watcher_cb watcher_cb; /**< Client callback */
+ void *pw; /**< Client data */
+};
/**
* LibDOM event handler
*
- * \param[in] evt The LibDOM event object
- * \param[in] pw Pointer to our nslayout_layout object
+ * \param[in] evt The LibDOM event object
+ * \param[in] pw Pointer to our dom watcher object
*/
static void nsl__dom_event_handler(struct dom_event *evt, void *pw)
{
+ const struct nsl_dom_watcher *watcher = pw;
+ enum nsl_dom_watcher_type type;
dom_event_target *node = NULL;
+ dom_string *evt_type = NULL;
dom_node_type node_type;
- dom_string *name = NULL;
- dom_string *type = NULL;
dom_exception exc;
- UNUSED(pw);
-
- printf(" DOM Event: ");
+ nsl_dom_debug_dump_event(evt);
- /* Ugly test to see what events come out */
exc = dom_event_get_target(evt, &node);
if ((exc != DOM_NO_ERR) || (node == NULL)) {
printf("FAILED to get target node!\n");
@@ -75,116 +66,166 @@ static void nsl__dom_event_handler(struct dom_event *evt,
void *pw)
goto fail;
}
- if (node_type == DOM_ELEMENT_NODE) {
- exc = dom_node_get_node_name(node, &name);
- if ((exc != DOM_NO_ERR) || (name == NULL)) {
- printf("FAILED to get target node name!\n");
- goto fail;
- }
- }
-
- exc = dom_event_get_type(evt, &type);
- if ((exc != DOM_NO_ERR) || (type == NULL)) {
+ exc = dom_event_get_type(evt, &evt_type);
+ if ((exc != DOM_NO_ERR) || (evt_type == NULL)) {
printf("FAILED to get event type!\n");
goto fail;
}
- if (node_type == DOM_ELEMENT_NODE) {
- printf("<%s> %s",
- dom_string_data(name),
- dom_string_data(type));
+ if (dom_string_isequal(evt_type,
+ nsl_dom_str_node_inserted)) {
+ type = NSL_DOM_WATCHER_NODE_INSERTED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_node_removed)) {
+ type = NSL_DOM_WATCHER_NODE_REMOVED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_subtree_modified)) {
+ type = NSL_DOM_WATCHER_SUBTREE_MODIFIED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_attr_modified)) {
+ type = NSL_DOM_WATCHER_ATTR_MODIFIED;
+
+ } else if (dom_string_isequal(evt_type,
+ nsl_dom_str_characterdata_modified)) {
+ type = NSL_DOM_WATCHER_CHAR_DATA_MODIFIED;
} else {
- printf("%s %s",
- nsl__dom_node_type_to_string(node_type),
- dom_string_data(type));
+ printf("FAILED: unrecognised event type: '%s'",
+ dom_string_data(evt_type));
+ goto fail;
}
- /* 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_string_unref(evt_type);
+
+ watcher->watcher_cb(type, node, node_type, watcher->pw);
+ return;
fail:
- if (type != NULL) dom_string_unref(type);
- if (name != NULL) dom_string_unref(name);
+ if (evt_type != NULL) dom_string_unref(evt_type);
if (node != NULL) dom_node_unref(node);
-
- printf("\n");
}
-/* Exported function, documented in src/dom/event.h */
-nslayout_error nsl_dom_watcher_add_for_layout(nslayout_layout *layout)
+
+/**
+ * Destroy a DOM document's event listener
+ *
+ * \param[in] listener The listener to destroy
+ * \param[in] document The document that the listener was registerd for.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl__dom_listener_destroy(
+ dom_event_listener *listener,
+ dom_document *document)
{
dom_exception exc;
- /* TODO: LibDOM mutation event listeners are really slow.
- * Need to find a better way to get DOM change notifications.
- * LibDOM probably needs to gain Mutation Observer support.
- */
+ /* Passing NULL as type, removes listener for all event types. */
+ exc = dom_event_target_remove_event_listener(
+ document, NULL, listener, false);
+ if (exc != DOM_NO_ERR) {
+ return NSL_DOM_ERR(exc);
+ }
+
+ dom_event_listener_unref(listener);
+
+ return NSLAYOUT_OK;
+}
- assert(layout->listener == NULL);
+/**
+ * Create a dom event listener.
+ *
+ * \param[out] listener_out Returns a dom listener for watcher's document.
+ * \param[in] watcher DOM watcher object that listener is used for.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl__dom_listener_create(
+ dom_event_listener **listener_out,
+ const struct nsl_dom_watcher *watcher)
+{
+ dom_event_listener *listener;
+ dom_exception exc;
+
+ /* Create listener */
exc = dom_event_listener_create(nsl__dom_event_handler,
- layout, &layout->listener);
+ (void *) watcher, &listener);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_node_inserted,
- layout->listener, false);
+ /* Set the event types it should listen to */
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_node_inserted, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_subtree_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_subtree_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_node_removed,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_node_removed, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_attr_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_attr_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
- exc = dom_event_target_add_event_listener(
- layout->doc, nsl_dom_str_characterdata_modified,
- layout->listener, false);
+ exc = dom_event_target_add_event_listener(watcher->document,
+ nsl_dom_str_characterdata_modified, listener, false);
if (exc != DOM_NO_ERR) {
goto error;
}
+ *listener_out = listener;
return NSLAYOUT_OK;
error:
- nsl_dom_watcher_remove_for_layout(layout);
+ nsl__dom_listener_destroy(listener, watcher->document);
return NSL_DOM_ERR(exc);
}
-/* Exported function, documented in src/dom/event.h */
-nslayout_error nsl_dom_watcher_remove_for_layout(nslayout_layout *layout)
+/* Exported function, documented in src/dom/watcher.h */
+nslayout_error nsl_dom_watcher_create(
+ struct nsl_dom_watcher **watcher_out,
+ dom_document *document,
+ nsl_dom_watcher_cb watcher_cb,
+ void *pw)
{
- dom_exception exc;
+ struct nsl_dom_watcher *watcher;
+ nslayout_error err;
- /* Passing NULL as type, removes listener for all event types. */
- exc = dom_event_target_remove_event_listener(
- layout->doc, NULL, layout->listener, false);
- if (exc != DOM_NO_ERR) {
- return NSL_DOM_ERR(exc);
+ watcher = malloc(sizeof(*watcher));
+ if (watcher == NULL) {
+ return NSLAYOUT_NO_MEM;
+ }
+
+ watcher->document = document;
+ watcher->watcher_cb = watcher_cb;
+ watcher->pw = pw;
+
+ err = nsl__dom_listener_create(&watcher->listener, watcher);
+ if (err != NSLAYOUT_OK) {
+ free(watcher);
+ return err;
}
- dom_event_listener_unref(layout->listener);
- layout->listener = NULL;
+ *watcher_out = watcher;
+ return NSLAYOUT_OK;
+}
+
+/* Exported function, documented in src/dom/watcher.h */
+nslayout_error nsl_dom_watcher_destroy(
+ struct nsl_dom_watcher *watcher)
+{
+ nsl__dom_listener_destroy(watcher->listener, watcher->document);
+ free(watcher);
return NSLAYOUT_OK;
}
diff --git a/src/dom/watcher.h b/src/dom/watcher.h
index 87f0a1b..0883fd2 100644
--- a/src/dom/watcher.h
+++ b/src/dom/watcher.h
@@ -5,28 +5,67 @@
*/
/** \file src/dom/watcher.h
- * Layout object handling
+ * Interface to DOM mutation watching.
*/
#ifndef nslayout_dom_watcher_h_
#define nslayout_dom_watcher_h_
-#include <libnslayout/nslayout.h>
+struct dom_document;
+struct nsl_dom_watcher;
/**
- * Add DOM change watchers to the layout's document.'
+ * DOM watcher's mutation types
+ */
+enum nsl_dom_watcher_type {
+ NSL_DOM_WATCHER_NODE_INSERTED,
+ NSL_DOM_WATCHER_NODE_REMOVED,
+ NSL_DOM_WATCHER_SUBTREE_MODIFIED,
+ NSL_DOM_WATCHER_ATTR_MODIFIED,
+ NSL_DOM_WATCHER_CHAR_DATA_MODIFIED,
+ NSL_DOM_WATCHER__COUNT,
+};
+
+
+/**
+ * Callback function for dom modifications.
*
- * \param[in] layout nslayout_layout object to set watchers for.
+ * \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 dom watcher owner's private data.
* \return NSLAYOUT_OK on success, appropriate error otherwise.
*/
-nslayout_error nsl_dom_watcher_add_for_layout(nslayout_layout *layout);
+typedef nslayout_error (*nsl_dom_watcher_cb)(
+ enum nsl_dom_watcher_type type,
+ dom_event_target *node,
+ dom_node_type node_type,
+ void *pw);
+
+
+/**
+ * Create DOM change watcher for a DOM document.
+ *
+ * \param[out] watcher_out Returns a dom watcher object for layout.
+ * \param[in] document DOM document to create watcher for.
+ * \param[in] watcher_cb Function to call when dom modification happens.
+ * \param[in] pw Private data passed back to `watcher_cb`.
+ * \return NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+nslayout_error nsl_dom_watcher_create(
+ struct nsl_dom_watcher **watcher_out,
+ dom_document *document,
+ nsl_dom_watcher_cb watcher_cb,
+ void *pw);
+
/**
- * Remove DOM change watchers from the layout's document.
+ * Destroy a document change DOM change watcher.
*
- * \param[in] layout nslayout_layout object remove watchers from.
+ * \param[in] watcher DOM change watcher to destroy.
* \return NSLAYOUT_OK on success, appropriate error otherwise.
*/
-nslayout_error nsl_dom_watcher_remove_for_layout(nslayout_layout *layout);
+nslayout_error nsl_dom_watcher_destroy(
+ struct nsl_dom_watcher *watcher);
#endif
diff --git a/src/layout.c b/src/layout.c
index 1f90951..e4824bf 100644
--- a/src/layout.c
+++ b/src/layout.c
@@ -10,13 +10,29 @@
#include <assert.h>
#include <stdlib.h>
-#include <stdio.h>
+
+#include "libnslayout/nslayout.h"
#include "layout.h"
+#include "util/util.h"
#include "dom/watcher.h"
#include "util/dom-str.h"
+/**
+ * The layout object for a DOM document
+ */
+struct nslayout_layout {
+ dom_document *document;
+ css_select_ctx *css_ctx;
+ css_media_type *media;
+ nslayout_callback cb;
+ void *pw;
+
+ struct nsl_dom_watcher *watcher;
+};
+
+
/* Publically exported function, documented in include/libnslayout/nslayout.h
*/
nslayout_error nslayout_init(void)
{
@@ -31,6 +47,40 @@ nslayout_error nslayout_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 NSLAYOUT_OK on success, appropriate error otherwise.
+ */
+static nslayout_error nsl_layout_dom_watcher_cb(
+ enum nsl_dom_watcher_type type,
+ dom_event_target *node,
+ dom_node_type node_type,
+ void *pw)
+{
+ nslayout_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 NSLAYOUT_OK;
+}
+
+
/* Publically exported function, documented in include/libnslayout/nslayout.h
*/
nslayout_error nslayout_layout_create(
dom_document *doc,
@@ -48,21 +98,20 @@ nslayout_error nslayout_layout_create(
assert(media != NULL);
assert(cb != NULL);
- printf("Called layout_create\n");
-
l = calloc(1, sizeof(nslayout_layout));
if (l == NULL) {
return NSLAYOUT_NO_MEM;
}
/* TODO: Decide: ownership will probably be passed to libnslayout */
- l->doc = doc;
+ l->document = doc;
l->css_ctx = css_ctx;
l->media = media;
l->cb = cb;
l->pw = pw;
- err = nsl_dom_watcher_add_for_layout(l);
+ err = nsl_dom_watcher_create(&l->watcher, l->document,
+ nsl_layout_dom_watcher_cb, l);
if (err != NSLAYOUT_OK) {
return err;
}
@@ -81,7 +130,7 @@ nslayout_error nslayout_layout_destroy(
assert(layout != NULL);
/* TODO: free/unref the stuff we own in the layout */
- err = nsl_dom_watcher_remove_for_layout(layout);
+ err = nsl_dom_watcher_destroy(layout->watcher);
if (err != NSLAYOUT_OK) {
return err;
}
diff --git a/src/layout.h b/src/layout.h
index e71d5aa..d8fa3b2 100644
--- a/src/layout.h
+++ b/src/layout.h
@@ -11,16 +11,6 @@
#ifndef nslayout_layout_h_
#define nslayout_layout_h_
-#include <libnslayout/nslayout.h>
-
-struct nslayout_layout {
- dom_document *doc;
- css_select_ctx *css_ctx;
- css_media_type *media;
- nslayout_callback cb;
- void *pw;
-
- dom_event_listener *listener;
-};
+struct nslayout_layout;
#endif
diff --git a/src/util/dom-str.c b/src/util/dom-str.c
index 73bfe63..3c0865a 100644
--- a/src/util/dom-str.c
+++ b/src/util/dom-str.c
@@ -10,6 +10,8 @@
#include <stdio.h>
+#include "libnslayout/nslayout.h"
+
#include "util/dom-str.h"
#include "util/util.h"
diff --git a/src/util/dom-str.h b/src/util/dom-str.h
index 7c130c3..9ce7bb9 100644
--- a/src/util/dom-str.h
+++ b/src/util/dom-str.h
@@ -11,8 +11,6 @@
#ifndef nslayout_util_dom_str_h_
#define nslayout_util_dom_str_h_
-#include <libnslayout/nslayout.h>
-
extern dom_string *nsl_dom_str_node_inserted;
extern dom_string *nsl_dom_str_node_removed;
extern dom_string *nsl_dom_str_subtree_modified;
--
NetSurf Layout Engine
_______________________________________________
netsurf-commits mailing list
[email protected]
http://listmaster.pepperfish.net/cgi-bin/mailman/listinfo/netsurf-commits-netsurf-browser.org