summaryrefslogtreecommitdiff
path: root/utils/libdom.h
blob: 306aa0f8bbbc751f704a7d0abb3b7b7a7179f5b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright 2011 John-Mark Bell <jmb@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/** \file
 * libdom utilities (implementation).
 */

#ifndef NETSURF_UTILS_LIBDOM_H_
#define NETSURF_UTILS_LIBDOM_H_

#include <stdbool.h>
#include <stdio.h>

#include <dom/dom.h>

#include <dom/bindings/hubbub/parser.h>
#include <dom/bindings/hubbub/errors.h>

/**
 * depth-first walk the dom calling callback for each element
 *
 * \param root the dom node to use as the root of the tree walk
 * \param callback The function called for each element
 * \param ctx The context passed to the callback.
 * \return true if all nodes were examined, false if the callback terminated
 *         the walk early.
 */
bool libdom_treewalk(dom_node *root,
		bool (*callback)(dom_node *node, dom_string *name, void *ctx),
		void *ctx);

/**
 * Search the descendants of a node for an element.
 *
 * \param  node		dom_node to search children of, or NULL
 * \param  element_name	name of element to find
 * \return  first child of node which is an element and matches name, or
 *          NULL if not found or parameter node is NULL
 */
dom_node *libdom_find_element(dom_node *node, lwc_string *element_name);

/**
 * Search children of a node for first named element 
 * \param  parent dom_node to search children of, or NULL
 * \param  element_name	name of element to find
 * \return  first child of node which is an element and matches name, or
 *          NULL if not found or parameter node is NULL
 */
dom_node *libdom_find_first_element(dom_node *parent, lwc_string *element_name);

typedef nserror (*libdom_iterate_cb)(dom_node *node, void *ctx);

nserror libdom_iterate_child_elements(dom_node *parent,
		libdom_iterate_cb cb, void *ctx);

nserror libdom_parse_file(const char *filename, const char *encoding,
		dom_document **doc);

/**
 * Convert libdom hubbub binding errors to nserrors.
 *
 * \param error The hubbub binding error to convert
 * \return The appropriate nserror
 */
nserror libdom_hubbub_error_to_nserror(dom_hubbub_error error);

/**
 * Walk though a DOM (sub)tree, in depth first order, printing DOM structure.
 *
 * \param node The root node to start from.
 * \param f The file to write output into.
 * \param depth The depth of 'node' in the (sub)tree.
 */
nserror libdom_dump_structure(dom_node *node, FILE *f, int depth);

#endif