diff --git a/src/ecmascript/ecmascript-c.c b/src/ecmascript/ecmascript-c.c index 965027c9c..e42e83273 100644 --- a/src/ecmascript/ecmascript-c.c +++ b/src/ecmascript/ecmascript-c.c @@ -8,6 +8,7 @@ #include "dialogs/status.h" #include "document/document.h" +#include "document/libdom/doc.h" #include "document/libdom/mapa.h" #include "document/view.h" #include "ecmascript/ecmascript.h" @@ -481,3 +482,59 @@ void ecmascript_moved_form_state(struct form_state *fs) spidermonkey_moved_form_state(fs); #endif } + +void * +walk_tree_query(dom_node *node, char *selector, int depth) +{ + dom_exception exc; + dom_node *child; + void *res = NULL; + dom_node_type typ; + + /* Only interested in element nodes */ + exc = dom_node_get_node_type(node, &typ); + if (typ != DOM_ELEMENT_NODE) { + return NULL; + } + + if (res = el_match_selector(selector, node)) { + /* There was an error; return */ + return res; + } + /* Get the node's first child */ + exc = dom_node_get_first_child(node, &child); + + if (exc != DOM_NO_ERR) { + fprintf(stderr, "Exception raised for node_get_first_child\n"); + return NULL; + } else if (child != NULL) { + /* node has children; decend to children's depth */ + depth++; + + /* Loop though all node's children */ + do { + dom_node *next_child; + + /* Visit node's descendents */ + res = walk_tree_query(child, selector, depth); + /* There was an error; return */ + if (res) { + dom_node_unref(child); + return res; + } + + /* Go to next sibling */ + exc = dom_node_get_next_sibling(child, &next_child); + if (exc != DOM_NO_ERR) { + fprintf(stderr, "Exception raised for " + "node_get_next_sibling\n"); + dom_node_unref(child); + return NULL; + } + + dom_node_unref(child); + child = next_child; + } while (child != NULL); /* No more children */ + } + return NULL; +} diff --git a/src/ecmascript/ecmascript-c.h b/src/ecmascript/ecmascript-c.h index 1207c4fd9..939032bae 100644 --- a/src/ecmascript/ecmascript-c.h +++ b/src/ecmascript/ecmascript-c.h @@ -40,6 +40,8 @@ void ecmascript_detach_form_view(struct form_view *fv); void ecmascript_detach_form_state(struct form_state *fs); void ecmascript_moved_form_state(struct form_state *fs); +void *walk_tree_query(dom_node *node, char *selector, int depth); + extern struct module ecmascript_module; #ifdef __cplusplus diff --git a/src/ecmascript/spidermonkey/document.cpp b/src/ecmascript/spidermonkey/document.cpp index 5c379727d..9f5b9088c 100644 --- a/src/ecmascript/spidermonkey/document.cpp +++ b/src/ecmascript/spidermonkey/document.cpp @@ -28,6 +28,7 @@ #include "document/view.h" #include "ecmascript/css2xpath.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/ecmascript-c.h" #include "ecmascript/libdom/parse.h" #include "ecmascript/spidermonkey/collection.h" #include "ecmascript/spidermonkey/form.h" @@ -1819,62 +1820,6 @@ document_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } -void * -walk_tree_query(dom_node *node, char *selector, int depth) -{ - dom_exception exc; - dom_node *child; - void *res = NULL; - dom_node_type typ; - - /* Only interested in element nodes */ - exc = dom_node_get_node_type(node, &typ); - if (typ != DOM_ELEMENT_NODE) { - return NULL; - } - - if (res = el_match_selector(selector, node)) { - /* There was an error; return */ - return res; - } - /* Get the node's first child */ - exc = dom_node_get_first_child(node, &child); - - if (exc != DOM_NO_ERR) { - fprintf(stderr, "Exception raised for node_get_first_child\n"); - return NULL; - } else if (child != NULL) { - /* node has children; decend to children's depth */ - depth++; - - /* Loop though all node's children */ - do { - dom_node *next_child; - - /* Visit node's descendents */ - res = walk_tree_query(child, selector, depth); - /* There was an error; return */ - if (res) { - dom_node_unref(child); - return res; - } - - /* Go to next sibling */ - exc = dom_node_get_next_sibling(child, &next_child); - if (exc != DOM_NO_ERR) { - fprintf(stderr, "Exception raised for " - "node_get_next_sibling\n"); - dom_node_unref(child); - return NULL; - } - - dom_node_unref(child); - child = next_child; - } while (child != NULL); /* No more children */ - } - return NULL; -} - static bool document_querySelector(JSContext *ctx, unsigned int argc, JS::Value *vp) {