2006-12-10 12:24:34 -05:00
|
|
|
/** The DOM stack interface.
|
|
|
|
*
|
|
|
|
* @file dom/stack.h
|
|
|
|
*
|
|
|
|
* The DOM stack interface is used by the parser when building a DOM
|
|
|
|
* tree and by the various DOM tree traversers. It allows for several
|
|
|
|
* stack contexts to be registered, each defining their own type
|
|
|
|
* specific node handlers or callbacks (#dom_stack_callback_T) which
|
|
|
|
* will be called upon a node being pushed onto or popped from the
|
|
|
|
* stack. As an example a example, by defining DOM_STACK_TRACE it is be
|
|
|
|
* possible to add a DOM stack tracer (using #add_dom_stack_tracer) to
|
|
|
|
* debug all push and pop operations.
|
|
|
|
*
|
|
|
|
* The stack interface provides a method for automatically having
|
|
|
|
* objects allocated when a new node is pushed onto the stack. This
|
|
|
|
* object can then be used for storing private state information
|
|
|
|
* belonging to the individual contexts. This is done by setting the
|
|
|
|
* #dom_stack_context_info.object_size member to a non-zero value,
|
|
|
|
* usually using sizeof().
|
|
|
|
*
|
|
|
|
* States on the stack can be marked immutable meaning that they will
|
|
|
|
* be "locked" down so that any operations to pop them will fail. This
|
|
|
|
* can be used when parsing a "subdocument", e.g. output from
|
|
|
|
* ECMAScripts "document.write" function, where badly formatted SGML
|
|
|
|
* should not be allowed to change the root document.
|
|
|
|
*
|
|
|
|
* In some situations, it may be desired to avoid building a complete
|
|
|
|
* DOM tree in memory when only one document traversal is required, for
|
|
|
|
* example when highlighting SGML code. This can be done by passing the
|
|
|
|
* #DOM_STACK_FLAG_FREE_NODES flag to #init_dom_stack. Nodes that are
|
|
|
|
* popped from the stack will immediately be deallocated. A pop node
|
|
|
|
* callback can also request that a node is deallocated and removed from
|
|
|
|
* the DOM tree by returning the #DOM_CODE_FREE_NODE return code. An
|
|
|
|
* example of this can be seen in the DOM configuration module where
|
|
|
|
* comment nodes may be pruned from the tree.
|
|
|
|
*/
|
|
|
|
/* TODO: Write about:
|
|
|
|
* - How to access stack states and context state objects.
|
|
|
|
* - Using search_dom_stack() and walk_dom_nodes().
|
|
|
|
*/
|
|
|
|
|
2005-12-28 08:05:14 -05:00
|
|
|
#ifndef EL_DOM_STACK_H
|
|
|
|
#define EL_DOM_STACK_H
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-31 16:38:06 -05:00
|
|
|
#include "dom/code.h"
|
2005-12-28 08:05:14 -05:00
|
|
|
#include "dom/node.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
|
2005-11-15 04:43:52 -05:00
|
|
|
struct dom_stack;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** DOM stack callback
|
|
|
|
*
|
2009-02-14 20:10:14 -05:00
|
|
|
* Used by contexts, for 'hooking' into the node traversing.
|
|
|
|
*
|
|
|
|
* This callback must not call done_dom_node() to free the node it
|
|
|
|
* gets as a parameter, because call_dom_node_callbacks() may be
|
|
|
|
* intending to call more callbacks for the same node. Instead, the
|
|
|
|
* callback can return ::DOM_CODE_FREE_NODE, and the node will then
|
|
|
|
* be freed after the callbacks of all contexts have been called. */
|
2006-01-31 16:01:35 -05:00
|
|
|
typedef enum dom_code
|
2006-01-15 18:55:58 -05:00
|
|
|
(*dom_stack_callback_T)(struct dom_stack *, struct dom_node *, void *);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2005-12-20 22:57:25 -05:00
|
|
|
#define DOM_STACK_MAX_DEPTH 4096
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** DOM stack state
|
|
|
|
*
|
|
|
|
* This state records what node and where it is placed. */
|
2005-12-20 22:57:25 -05:00
|
|
|
struct dom_stack_state {
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The node assiciated with the state */
|
2005-12-20 22:57:25 -05:00
|
|
|
struct dom_node *node;
|
2006-01-09 06:44:57 -05:00
|
|
|
/**
|
|
|
|
* The depth of the state in the stack. This is amongst other things
|
2005-12-20 22:57:25 -05:00
|
|
|
* used to get the state object data. */
|
|
|
|
unsigned int depth;
|
2007-07-26 07:14:27 -04:00
|
|
|
/** Whether this stack state can be popped with pop_dom_node(),
|
|
|
|
* pop_dom_nodes(), or pop_dom_state(). */
|
2005-12-20 22:57:25 -05:00
|
|
|
unsigned int immutable:1;
|
|
|
|
};
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** DOM stack context info
|
|
|
|
*
|
|
|
|
* To add a DOM stack context define this struct either statically or on the
|
|
|
|
* stack. */
|
2005-12-20 18:58:22 -05:00
|
|
|
struct dom_stack_context_info {
|
2006-01-09 06:44:57 -05:00
|
|
|
/**
|
2006-12-09 19:17:35 -05:00
|
|
|
* The number of bytes to allocate on the stack for each state's
|
|
|
|
* data member. Zero means no state data should be allocated.
|
|
|
|
* */
|
2005-12-20 22:38:04 -05:00
|
|
|
size_t object_size;
|
2005-12-21 07:46:28 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Callbacks to be called when pushing nodes. */
|
2005-12-20 13:20:04 -05:00
|
|
|
dom_stack_callback_T push[DOM_NODES];
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Callbacks to be called when popping nodes. */
|
2005-12-20 13:20:04 -05:00
|
|
|
dom_stack_callback_T pop[DOM_NODES];
|
|
|
|
};
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** DOM stack context
|
|
|
|
*
|
|
|
|
* This holds 'runtime' data for the stack context. */
|
2005-12-20 19:15:19 -05:00
|
|
|
struct dom_stack_context {
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Data specific to the context. */
|
2005-12-20 19:25:50 -05:00
|
|
|
void *data;
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/**
|
|
|
|
* This is one big array of context specific objects. For the SGML
|
|
|
|
* parser this holds DTD-oriented info about the node (recorded in
|
2006-12-09 19:17:35 -05:00
|
|
|
* struct #sgml_node_info). E.g. whether an element node is optional.
|
2006-01-09 06:44:57 -05:00
|
|
|
*/
|
2005-12-20 22:38:04 -05:00
|
|
|
unsigned char *state_objects;
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Info about node callbacks and such. */
|
2005-12-20 19:15:19 -05:00
|
|
|
struct dom_stack_context_info *info;
|
|
|
|
};
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Flags for controlling the DOM stack */
|
2005-12-20 22:48:50 -05:00
|
|
|
enum dom_stack_flag {
|
2006-01-09 06:44:57 -05:00
|
|
|
/** No flag needed. */
|
2006-01-03 14:35:32 -05:00
|
|
|
DOM_STACK_FLAG_NONE = 0,
|
|
|
|
|
2007-07-26 07:14:27 -04:00
|
|
|
/** Free nodes when popping by calling done_dom_node(). */
|
2006-01-03 14:35:32 -05:00
|
|
|
DOM_STACK_FLAG_FREE_NODES = 1,
|
2005-12-20 22:48:50 -05:00
|
|
|
};
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The DOM stack
|
|
|
|
*
|
|
|
|
* The DOM stack is a convenient way to traverse DOM trees. Also it
|
2005-09-15 09:58:31 -04:00
|
|
|
* maintains needed state info and is therefore also a holder of the current
|
2005-11-15 04:43:52 -05:00
|
|
|
* context since the stack is used to when the DOM tree is manipulated. */
|
|
|
|
struct dom_stack {
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The states currently on the stack. */
|
2005-11-15 04:43:52 -05:00
|
|
|
struct dom_stack_state *states;
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The depth of the stack. */
|
2005-09-15 09:58:31 -04:00
|
|
|
size_t depth;
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Flags given to ref:[init_dom_stack]. */
|
2005-12-20 22:48:50 -05:00
|
|
|
enum dom_stack_flag flags;
|
2005-12-07 21:02:27 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Contexts for the pushed and popped nodes. */
|
2005-12-26 23:59:12 -05:00
|
|
|
struct dom_stack_context **contexts;
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The number of active contexts. */
|
2005-12-20 19:15:19 -05:00
|
|
|
size_t contexts_size;
|
2005-12-20 22:57:25 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/**
|
|
|
|
* The current context. Only meaningful within
|
2006-12-09 19:17:35 -05:00
|
|
|
* #dom_stack_callback_T functions. */
|
2005-12-20 19:32:43 -05:00
|
|
|
struct dom_stack_context *current;
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Check whether stack is empty or not
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to check.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns Non-zero if stack is empty. */
|
2005-12-18 20:15:36 -05:00
|
|
|
#define dom_stack_is_empty(stack) \
|
|
|
|
(!(stack)->states || (stack)->depth == 0)
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Access state by offset from top
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to fetch the state from.
|
|
|
|
* @param top_offset The offset from the stack top, zero is the top.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns The requested state. */
|
2005-11-15 04:43:52 -05:00
|
|
|
static inline struct dom_stack_state *
|
|
|
|
get_dom_stack_state(struct dom_stack *stack, int top_offset)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2005-11-15 04:43:52 -05:00
|
|
|
assertm(stack->depth - 1 - top_offset >= 0,
|
2005-09-15 09:58:31 -04:00
|
|
|
"Attempting to access invalid state");
|
2005-11-15 04:43:52 -05:00
|
|
|
return &stack->states[stack->depth - 1 - top_offset];
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Access the stack top
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to get the top state from.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns The top state. */
|
2006-01-09 06:44:57 -05:00
|
|
|
#define get_dom_stack_top(stack) \
|
|
|
|
get_dom_stack_state(stack, 0)
|
|
|
|
|
|
|
|
/** Access context specific state data
|
|
|
|
*
|
|
|
|
* Similar to ref:[get_dom_stack_state], this will fetch the data
|
|
|
|
* associated with the state for the given context.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param context The context to get data from.
|
|
|
|
* @param state The stack state to get data from.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns The state data or NULL if
|
|
|
|
* #dom_stack_context_info.object_size was zero. */
|
2005-12-21 07:56:18 -05:00
|
|
|
static inline void *
|
|
|
|
get_dom_stack_state_data(struct dom_stack_context *context,
|
|
|
|
struct dom_stack_state *state)
|
|
|
|
{
|
|
|
|
size_t object_size = context->info->object_size;
|
|
|
|
|
|
|
|
if (!object_size) return NULL;
|
|
|
|
|
2007-03-11 06:22:02 -04:00
|
|
|
assert(context->state_objects);
|
2005-12-21 07:56:18 -05:00
|
|
|
|
|
|
|
return (void *) &context->state_objects[state->depth * object_size];
|
|
|
|
}
|
2005-12-05 05:11:06 -05:00
|
|
|
|
2005-12-21 21:55:55 -05:00
|
|
|
/*#define DOM_STACK_TRACE*/
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Get debug info from the DOM stack
|
|
|
|
*
|
|
|
|
* Define `DOM_STACK_TRACE` to have debug info about the nodes added printed to
|
|
|
|
* the log. It will define add_dom_stack_tracer() to not be a no-op.
|
2006-01-12 18:11:39 -05:00
|
|
|
*
|
2006-01-09 06:44:57 -05:00
|
|
|
* Run as:
|
2006-01-12 18:11:39 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* ELINKS_LOG=/tmp/dom-dump.txt ./elinks -no-connect URL
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
|
|
|
* to have the debug dumped into a file. */
|
2006-12-09 19:17:35 -05:00
|
|
|
#ifdef DOM_STACK_TRACE
|
2005-12-29 21:29:17 -05:00
|
|
|
#define add_dom_stack_tracer(stack, name) \
|
|
|
|
add_dom_stack_context(stack, name, &dom_stack_trace_context_info)
|
2006-12-09 19:17:35 -05:00
|
|
|
extern struct dom_stack_context_info dom_stack_trace_context_info;
|
2005-12-21 21:55:55 -05:00
|
|
|
#else
|
2005-12-29 21:29:17 -05:00
|
|
|
#define add_dom_stack_tracer(stack, name) /* Nada */
|
2005-12-21 21:55:55 -05:00
|
|
|
#endif
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** The state iterators
|
|
|
|
*
|
|
|
|
* To safely iterate through the stack state iterators. */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Iterate the stack from bottom to top. */
|
2005-12-18 20:51:32 -05:00
|
|
|
#define foreach_dom_stack_state(stack, state, pos) \
|
|
|
|
for ((pos) = 0; (pos) < (stack)->depth; (pos)++) \
|
2006-01-09 08:11:29 -05:00
|
|
|
if (((state) = &(stack)->states[(pos)]))
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Iterate the stack from top to bottom. */
|
2005-12-18 20:51:32 -05:00
|
|
|
#define foreachback_dom_stack_state(stack, state, pos) \
|
|
|
|
for ((pos) = (stack)->depth - 1; (pos) >= 0; (pos)--) \
|
2006-01-09 08:11:29 -05:00
|
|
|
if (((state) = &(stack)->states[(pos)]))
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Life cycle functions. */
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Initialise a DOM stack
|
2006-12-09 19:17:35 -05:00
|
|
|
*
|
|
|
|
* @param stack Pointer to a (preallocated) stack.
|
|
|
|
* @param flags Any flags needed for controlling the behaviour of the stack.
|
2006-01-09 06:44:57 -05:00
|
|
|
*/
|
2005-12-20 22:48:50 -05:00
|
|
|
void init_dom_stack(struct dom_stack *stack, enum dom_stack_flag flags);
|
2006-12-09 19:17:35 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Release a DOM stack
|
|
|
|
*
|
|
|
|
* Free all resources collected by the stack.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to release. */
|
2005-11-15 04:43:52 -05:00
|
|
|
void done_dom_stack(struct dom_stack *stack);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Add a context to the stack
|
|
|
|
*
|
|
|
|
* This is needed if either you want to have the stack allocated objects for
|
|
|
|
* created states and/or if you want to install callbacks for pushing or
|
|
|
|
* popping.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack where the context should be created.
|
|
|
|
* @param data Private data to be stored in ref:[dom_stack_context.data].
|
|
|
|
* @param context_info Information about state objects and node callbacks.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns A pointer to the newly created context or NULL. */
|
2005-12-26 23:59:12 -05:00
|
|
|
struct dom_stack_context *
|
|
|
|
add_dom_stack_context(struct dom_stack *stack, void *data,
|
|
|
|
struct dom_stack_context_info *context_info);
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Unregister a stack context
|
2006-12-09 19:17:35 -05:00
|
|
|
*
|
2006-01-09 06:44:57 -05:00
|
|
|
* This should be done especially for temporary stack contexts (without any
|
|
|
|
* callbacks) so that they do not increasing the memory usage. */
|
2005-12-26 23:59:12 -05:00
|
|
|
void done_dom_stack_context(struct dom_stack *stack, struct dom_stack_context *context);
|
2005-12-20 14:27:20 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Push a node onto the stack
|
|
|
|
*
|
|
|
|
* Makes the pushed node the new top of the stack.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to push onto.
|
|
|
|
* @param node The node to push onto the stack.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns If an error occurs the node is released with
|
|
|
|
* #done_dom_node and NULL is returned. Else the pushed
|
|
|
|
* node is returned. */
|
2006-01-31 16:01:35 -05:00
|
|
|
enum dom_code push_dom_node(struct dom_stack *stack, struct dom_node *node);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Pop the top stack state
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to pop from. */
|
2005-11-15 04:43:52 -05:00
|
|
|
void pop_dom_node(struct dom_stack *stack);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Conditionally pop the stack states
|
|
|
|
*
|
|
|
|
* Searches the stack (using ref:[search_dom_stack]) for a specific node and
|
|
|
|
* pops all states until that particular state is met.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @note The popping is stopped if an immutable state is
|
|
|
|
* encountered. */
|
2005-11-15 04:43:52 -05:00
|
|
|
void pop_dom_nodes(struct dom_stack *stack, enum dom_node_type type,
|
2005-12-10 15:42:49 -05:00
|
|
|
struct dom_string *string);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Pop all states until target state
|
|
|
|
*
|
|
|
|
* Pop all stack states until a specific state is reached. The target state
|
|
|
|
* is also popped.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to pop from.
|
|
|
|
* @param target The state to pop until and including. */
|
2005-12-15 16:05:30 -05:00
|
|
|
void pop_dom_state(struct dom_stack *stack, struct dom_stack_state *target);
|
2005-12-05 13:40:35 -05:00
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Search the stack states
|
|
|
|
*
|
|
|
|
* The string comparison is done against the ref:[dom_node.string] member of
|
|
|
|
* the of the state nodes.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to search in.
|
|
|
|
* @param type The type of node to match against.
|
|
|
|
* @param string The string to match against.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @returns A state that matched the type and string or NULL. */
|
2005-12-21 07:48:37 -05:00
|
|
|
struct dom_stack_state *
|
|
|
|
search_dom_stack(struct dom_stack *stack, enum dom_node_type type,
|
|
|
|
struct dom_string *string);
|
|
|
|
|
2006-01-09 06:44:57 -05:00
|
|
|
/** Walk all nodes reachable from a given node
|
|
|
|
*
|
|
|
|
* Visits each node in the DOM tree rooted at a given node, pre-order style.
|
|
|
|
*
|
2006-12-09 19:17:35 -05:00
|
|
|
* @param stack The stack to use for walking the nodes.
|
|
|
|
* @param root The root node to start from.
|
2006-01-09 06:44:57 -05:00
|
|
|
*
|
|
|
|
* It is assummed that the given stack has been initialised with
|
2006-12-09 19:17:35 -05:00
|
|
|
* #init_dom_stack and that the caller already added one or more context
|
|
|
|
* to the stack. */
|
2005-11-15 04:43:52 -05:00
|
|
|
void walk_dom_nodes(struct dom_stack *stack, struct dom_node *root);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#endif
|