diff --git a/src/document/dom/renderer.c b/src/document/dom/renderer.c index 10e034e92..0f04574d1 100644 --- a/src/document/dom/renderer.c +++ b/src/document/dom/renderer.c @@ -796,48 +796,17 @@ dom_rss_pop_element(struct dom_stack *stack, struct dom_node *node, void *data) return DOM_STACK_CODE_OK; } -static struct dom_string * -get_rss_node_text(struct dom_node *node) -{ - struct dom_node *child; - int index; - - if (!node->data.element.children) - return NULL; - - foreach_dom_node (node->data.element.children, child, index) { - if (child->type == DOM_NODE_TEXT) - return &child->string; - } - - return NULL; -} - -static struct dom_node * -get_rss_child(struct dom_node *parent, enum rss_element_type type) -{ - struct dom_node *node; - int index; - - if (!parent->data.element.children) - return NULL; - - foreach_dom_node (parent->data.element.children, node, index) { - if (node->type == DOM_NODE_ELEMENT - && type == node->data.element.type) - return node; - } - - return NULL; -} - static struct dom_string * get_rss_text(struct dom_node *node, enum rss_element_type type) { - node = get_rss_child(node, type); + node = get_dom_node_child(node, DOM_NODE_ELEMENT, type); - return node ? get_rss_node_text(node) : NULL; + if (!node) return NULL; + + node = get_dom_node_child(node, DOM_NODE_TEXT, 0); + + return node ? &node->string: NULL; } static void diff --git a/src/dom/node.c b/src/dom/node.c index 8d86a7a19..9995c2230 100644 --- a/src/dom/node.c +++ b/src/dom/node.c @@ -267,6 +267,48 @@ get_dom_node_prev(struct dom_node *node) return NULL; } +struct dom_node * +get_dom_node_child(struct dom_node *parent, enum dom_node_type type, + int16_t subtype) +{ + struct dom_node_list **list; + struct dom_node *node; + int index; + + list = get_dom_node_list_by_type(parent, type); + if (!list) return NULL; + + foreach_dom_node (*list, node, index) { + if (node->type != type) + continue; + + if (!subtype) return node; + + switch (type) { + case DOM_NODE_ELEMENT: + if (node->data.element.type == subtype) + return node; + break; + + case DOM_NODE_ATTRIBUTE: + if (node->data.attribute.type == subtype) + return node; + break; + + case DOM_NODE_PROCESSING_INSTRUCTION: + if (node->data.attribute.type == subtype) + return node; + break; + + default: + return node; + } + } + + return NULL; +} + + /* Nodes */ struct dom_node * diff --git a/src/dom/node.h b/src/dom/node.h index 3e5e94c8e..9f4d2cfbd 100644 --- a/src/dom/node.h +++ b/src/dom/node.h @@ -246,6 +246,11 @@ 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); +/* Returns first text node of the element or NULL. */ +struct dom_node * +get_dom_node_child(struct dom_node *node, enum dom_node_type child_type, + int16_t child_subtype); + /* 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 @@ -307,17 +312,17 @@ struct dom_string *get_dom_node_value(struct dom_node *node); /* Returns the name used for identifying the node type. */ struct dom_string *get_dom_node_type_name(enum dom_node_type type); -/* Based on the type of the parent and the node return a proper list +/* Based on the type of the parent and the node type return a proper list * or NULL. This is useful when adding a node to a parent node. */ static inline struct dom_node_list ** -get_dom_node_list(struct dom_node *parent, struct dom_node *node) +get_dom_node_list_by_type(struct dom_node *parent, enum dom_node_type type) { switch (parent->type) { case DOM_NODE_DOCUMENT: return &parent->data.document.children; case DOM_NODE_ELEMENT: - switch (node->type) { + switch (type) { case DOM_NODE_ATTRIBUTE: return &parent->data.element.map; @@ -326,7 +331,7 @@ get_dom_node_list(struct dom_node *parent, struct dom_node *node) } case DOM_NODE_DOCUMENT_TYPE: - switch (node->type) { + switch (type) { case DOM_NODE_ENTITY: return &parent->data.document_type.entities; @@ -338,7 +343,7 @@ get_dom_node_list(struct dom_node *parent, struct dom_node *node) } case DOM_NODE_PROCESSING_INSTRUCTION: - switch (node->type) { + switch (type) { case DOM_NODE_ATTRIBUTE: return &parent->data.proc_instruction.map; @@ -351,4 +356,7 @@ get_dom_node_list(struct dom_node *parent, struct dom_node *node) } } +#define get_dom_node_list(parent, node) \ + get_dom_node_list_by_type(parent, (node)->type) + #endif