summaryrefslogtreecommitdiff
path: root/src/treebuilder/in_foreign_content.c
blob: c400c1f3fab1854b1dc92d2052951d691af9bcef (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * This file is part of Hubbub.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 Andrew Sidwell <takkaria@netsurf-browser.org>
 */

#include <assert.h>
#include <string.h>

#include "treebuilder/modes.h"
#include "treebuilder/internal.h"
#include "treebuilder/treebuilder.h"
#include "utils/utils.h"


/**
 * Returns true iff there is an element in scope that has a namespace other
 * than the HTML namespace.
 */
static bool element_in_scope_in_non_html_ns(hubbub_treebuilder *treebuilder)
{
	element_context *stack = treebuilder->context.element_stack;
	uint32_t node;

	assert((signed) treebuilder->context.current_node >= 0);

	for (node = treebuilder->context.current_node; node > 0; node--) {
		element_type node_type = stack[node].type;

		/* The list of element types given in the spec here are the
		 * scoping elements excluding TABLE and HTML. TABLE is handled
		 * in the previous conditional and HTML should only occur
		 * as the first node in the stack, which is never processed
		 * in this loop. */
		if (node_type == TABLE || is_scoping_element(node_type))
			break;

		if (stack[node].ns != HUBBUB_NS_HTML)
			return true;
	}

	return false;
}

/**
 * Process a token as if in the secondary insertion mode.
 */
static void process_as_in_secondary(hubbub_treebuilder *treebuilder,
		const hubbub_token *token)
{
	/* Because we don't support calling insertion modes directly,
	 * instead we set the current mode to the secondary mode,
	 * call the token handler, and then reset the mode afterward
	 * as long as it's unchanged, as this has the same effect */

	treebuilder->context.mode = treebuilder->context.second_mode;

	hubbub_treebuilder_token_handler(token, treebuilder);

	if (treebuilder->context.mode == treebuilder->context.second_mode)
		treebuilder->context.mode = IN_FOREIGN_CONTENT;

	if (treebuilder->context.mode == IN_FOREIGN_CONTENT &&
			!element_in_scope_in_non_html_ns(treebuilder)) {
		treebuilder->context.mode =
				treebuilder->context.second_mode;
	}
}

/**
 * Break out of foreign content as a result of certain start tags or EOF.
 */
static void foreign_break_out(hubbub_treebuilder *treebuilder)
{
	element_context *stack = treebuilder->context.element_stack;

	/** \todo parse error */

	while (stack[treebuilder->context.current_node].ns !=
			HUBBUB_NS_HTML) {
		hubbub_ns ns;
		element_type type;
		void *node;

		element_stack_pop(treebuilder, &ns, &type, &node);

		treebuilder->tree_handler->unref_node(
				treebuilder->tree_handler->ctx,
				node);
	}

	treebuilder->context.mode = treebuilder->context.second_mode;
}

/**
 * Handle tokens in "in foreign content" insertion mode
 *
 * \param treebuilder  The treebuilder instance
 * \param token        The token to process
 * \return True to reprocess the token, false otherwise
 */
bool handle_in_foreign_content(hubbub_treebuilder *treebuilder,
		const hubbub_token *token)
{
	bool reprocess = false;

	switch (token->type) {
	case HUBBUB_TOKEN_CHARACTER:
		append_text(treebuilder, &token->data.character);
		break;
	case HUBBUB_TOKEN_COMMENT:
		process_comment_append(treebuilder, token,
				treebuilder->context.element_stack[
				treebuilder->context.current_node].node);
		break;
	case HUBBUB_TOKEN_DOCTYPE:
		/** \todo parse error */
		break;
	case HUBBUB_TOKEN_START_TAG:
	{
		hubbub_ns cur_node_ns = treebuilder->context.element_stack[
				treebuilder->context.current_node].ns;

		element_type cur_node = current_node(treebuilder);
		element_type type = element_type_from_name(treebuilder,
				&token->data.tag.name);

		if (cur_node_ns == HUBBUB_NS_HTML ||
				(cur_node_ns == HUBBUB_NS_MATHML &&
				(type != MGLYPH && type != MALIGNMARK) &&
				(cur_node == MI || cur_node == MO ||
				cur_node == MN || cur_node == MS ||
				cur_node == MTEXT))) {
			process_as_in_secondary(treebuilder, token);
		} else if (type == B || type ==  BIG || type == BLOCKQUOTE ||
				type == BODY || type == BR || type == CENTER ||
				type == CODE || type == DD || type == DIV ||
				type == DL || type == DT || type == EM ||
				type == EMBED || type == FONT || type == H1 ||
				type == H2 || type == H3 || type == H4 ||
				type == H5 || type == H6 || type == HEAD ||
				type == HR || type == I || type == IMG ||
				type == LI || type == LISTING ||
				type == MENU || type == META || type == NOBR ||
				type == OL || type == P || type == PRE ||
				type == RUBY || type == S || type == SMALL ||
				type == SPAN || type == STRONG ||
				type == STRIKE || type == SUB || type == SUP ||
				type == TABLE || type == TT || type == U ||
				type == UL || type == VAR) {
			foreign_break_out(treebuilder);
			reprocess = true;
		} else {
			hubbub_tag tag = token->data.tag;

			adjust_foreign_attributes(treebuilder, &tag);

			/* Set to the right namespace and insert */
			tag.ns = cur_node_ns;

			if (token->data.tag.self_closing) {
				insert_element_no_push(treebuilder, &tag);
				/** \todo ack sc flag */
			} else {
				insert_element(treebuilder, &tag);
			}
		}
	}
		break;
	case HUBBUB_TOKEN_END_TAG:
		process_as_in_secondary(treebuilder, token);
		break;
	case HUBBUB_TOKEN_EOF:
		foreign_break_out(treebuilder);
		reprocess = true;
		break;
	}

	return reprocess;
}