From 768f97c38e3c583372ad8f5642e88a0b2ebee04d Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Mon, 16 Jan 2006 05:10:22 +0100 Subject: [PATCH] Add get_dom_node_prev() which gets the previous sibling of a DOM node --- src/dom/node.c | 35 ++++++++++++++++++++++++++++++----- src/dom/node.h | 3 +++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/dom/node.c b/src/dom/node.c index 7ac7e1b9d..8d86a7a19 100644 --- a/src/dom/node.c +++ b/src/dom/node.c @@ -225,16 +225,15 @@ get_dom_node_map_entry(struct dom_node_list *list, enum dom_node_type type, return dom_node_list_bsearch(&search, list); } -int -get_dom_node_list_index(struct dom_node *parent, struct dom_node *node) +static int +get_dom_node_list_pos(struct dom_node_list *list, struct dom_node *node) { - struct dom_node_list **list = get_dom_node_list(parent, node); struct dom_node *entry; int i; - if (!list) return -1; + assert(list); - foreach_dom_node (*list, entry, i) { + foreach_dom_node (list, entry, i) { if (entry == node) return i; } @@ -242,6 +241,32 @@ get_dom_node_list_index(struct dom_node *parent, struct dom_node *node) return -1; } +int +get_dom_node_list_index(struct dom_node *parent, struct dom_node *node) +{ + struct dom_node_list **list = get_dom_node_list(parent, node); + + return list ? get_dom_node_list_pos(*list, node) : -1; +} + +struct dom_node * +get_dom_node_prev(struct dom_node *node) +{ + struct dom_node_list **list; + int index; + + assert(node->parent); + + list = get_dom_node_list(node->parent, node); + if (!list) return NULL; + + index = get_dom_node_list_pos(*list, node); + if (index > 0) + return (*list)->entries[index - 1]; + + return NULL; +} + /* Nodes */ struct dom_node * diff --git a/src/dom/node.h b/src/dom/node.h index 542b89301..3e5e94c8e 100644 --- a/src/dom/node.h +++ b/src/dom/node.h @@ -243,6 +243,9 @@ int get_dom_node_list_index(struct dom_node *parent, struct dom_node *node); * @list is already sorted properly. */ int get_dom_node_map_index(struct dom_node_list *list, struct dom_node *node); +/* Returns the previous sibling to the node. */ +struct dom_node *get_dom_node_prev(struct dom_node *node); + /* Looks up the @node_map for a node matching the requested type and name. * The @subtype maybe be 0 indication unknown subtype and only name should be * tested else it will indicate either the element or attribute private