summaryrefslogtreecommitdiff
path: root/src/treebuilder/initial.c
blob: 1a9731a5230b6b957e4443472b2e4266ef95f8a8 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * This file is part of Hubbub.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
 * 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"
#include "utils/string.h"


#define S(s)	{ s, sizeof s }

struct {
	const char *name;
	size_t len;
} public_doctypes[] = {
	S("+//Silmaril//dtd html Pro v0r11 19970101//"),
	S("-//AdvaSoft Ltd//DTD HTML 3.0 asWedit + extensions//"),
	S("-//AS//DTD HTML 3.0 asWedit + extensions//"),
	S("-//IETF//DTD HTML 2.0 Level 1//"),
	S("-//IETF//DTD HTML 2.0 Level 2//"),
	S("-//IETF//DTD HTML 2.0 Strict Level 1//"),
	S("-//IETF//DTD HTML 2.0 Strict Level 2//"),
	S("-//IETF//DTD HTML 2.0 Strict//"),
	S("-//IETF//DTD HTML 2.0//"),
	S("-//IETF//DTD HTML 2.1E//"),
	S("-//IETF//DTD HTML 3.0//"),
	S("-//IETF//DTD HTML 3.2 Final//"),
	S("-//IETF//DTD HTML 3.2//"),
	S("-//IETF//DTD HTML 3//"),
	S("-//IETF//DTD HTML Level 0//"),
	S("-//IETF//DTD HTML Level 1//"),
	S("-//IETF//DTD HTML Level 2//"),
	S("-//IETF//DTD HTML Level 3//"),
	S("-//IETF//DTD HTML Strict Level 0//"),
	S("-//IETF//DTD HTML Strict Level 1//"),
	S("-//IETF//DTD HTML Strict Level 2//"),
	S("-//IETF//DTD HTML Strict Level 3//"),
	S("-//IETF//DTD HTML Strict//"),
	S("-//IETF//DTD HTML//"),
	S("-//Metrius//DTD Metrius Presentational//"),
	S("-//Microsoft//DTD Internet Explorer 2.0 HTML Strict//"),
	S("-//Microsoft//DTD Internet Explorer 2.0 HTML//"),
	S("-//Microsoft//DTD Internet Explorer 2.0 Tables//"),
	S("-//Microsoft//DTD Internet Explorer 3.0 HTML Strict//"),
	S("-//Microsoft//DTD Internet Explorer 3.0 HTML//"),
	S("-//Microsoft//DTD Internet Explorer 3.0 Tables//"),
	S("-//Netscape Comm. Corp.//DTD HTML//"),
	S("-//Netscape Comm. Corp.//DTD Strict HTML//"),
	S("-//O'Reilly and Associates//DTD HTML 2.0//"),
	S("-//O'Reilly and Associates//DTD HTML Extended 1.0//"),
	S("-//O'Reilly and Associates//DTD HTML Extended Relaxed 1.0//"),
	S("-//SoftQuad Software//DTD HoTMetaL PRO 6.0::19990601::extensions to HTML 4.0//"),
	S("-//SoftQuad//DTD HoTMetaL PRO 4.0::19971010::extensions to HTML 4.0//"),
	S("-//Spyglass//DTD HTML 2.0 Extended//"),
	S("-//SQ//DTD HTML 2.0 HoTMetaL + extensions//"),
	S("-//Sun Microsystems Corp.//DTD HotJava HTML//"),
	S("-//Sun Microsystems Corp.//DTD HotJava Strict HTML//"),
	S("-//W3C//DTD HTML 3 1995-03-24//"),
	S("-//W3C//DTD HTML 3.2 Draft//"),
	S("-//W3C//DTD HTML 3.2 Final//"),
	S("-//W3C//DTD HTML 3.2//"),
	S("-//W3C//DTD HTML 3.2S Draft//"),
	S("-//W3C//DTD HTML 4.0 Frameset//"),
	S("-//W3C//DTD HTML 4.0 Transitional//"),
	S("-//W3C//DTD HTML Experimental 19960712//"),
	S("-//W3C//DTD HTML Experimental 970421//"),
	S("-//W3C//DTD W3 HTML//"),
	S("-//W3O//DTD W3 HTML 3.0//"),
};

#undef S


/**
 * Check if one string starts with another.
 *
 * \param a	String to compare
 * \param a_len	Length of first string
 * \param b	String to compare
 * \param b_len	Length of second string
 */
static bool starts_with(const uint8_t *a, size_t a_len, const uint8_t *b,
		size_t b_len)
{
	uint8_t z1, z2;
	const uint8_t *s1, *s2;

	if (a_len < b_len)
		return false;

	for (s1 = a, s2 = b; b_len > 0; s1++, s2++, b_len--)
	{
		z1 = (*s1 & ~0x20);
		z2 = (*s2 & ~0x20);
		if (z1 != z2) return false;
		if (!z1) return true;
	}

	return true;
}


/**
 * Determine whether this doctype triggers full quirks mode
 *
 * \param treebuilder  Treebuilder instance
 * \param cdoc         The doctype to examine
 * \return True to trigger quirks, false otherwise
 */
static bool lookup_full_quirks(hubbub_treebuilder *treebuilder,
		const hubbub_doctype *cdoc)
{
	size_t i;

	const uint8_t *name = cdoc->name.ptr;
	size_t name_len = cdoc->name.len;

	const uint8_t *public_id = cdoc->public_id.ptr;
	size_t public_id_len = cdoc->public_id.len;

	const uint8_t *system_id = cdoc->system_id.ptr;
	size_t system_id_len = cdoc->system_id.len;

	UNUSED(treebuilder);

#define S(s)	(uint8_t *) s, sizeof s

	/* Check the name is "HTML" (case-insensitively) */
	if (!hubbub_string_match_ci(name, name_len, S("HTML")))
		return true;

	/* No public id means not-quirks */
	if (cdoc->public_missing) return false;

	for (i = 0; i < sizeof public_doctypes / sizeof public_doctypes[0]; i++)
	{
		if (starts_with(public_id, public_id_len,
				(uint8_t *) public_doctypes[i].name,
				public_doctypes[i].len)) {
			return true;
		}
	}

	if (hubbub_string_match_ci(public_id, public_id_len,
					S("-//W3O//DTD W3 HTML Strict 3.0//EN//")) ||
			hubbub_string_match_ci(public_id, public_id_len,
					S("-/W3C/DTD HTML 4.0 Transitional/EN")) ||
			hubbub_string_match_ci(public_id, public_id_len,
					S("HTML")) ||
			hubbub_string_match_ci(system_id, system_id_len,
					S("http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"))) {
		return true;
	}

	if (cdoc->system_missing == true &&
			(starts_with(public_id, public_id_len,
					S("-//W3C//DTD HTML 4.01 Frameset//")) ||
			starts_with(public_id, public_id_len,
					S("-//W3C//DTD HTML 4.01 Transitional//")))) {
		return true;
	}

#undef S

	return false;
}


/**
 * Determine whether this doctype triggers limited quirks mode
 *
 * \param treebuilder  Treebuilder instance
 * \param cdoc         The doctype to examine
 * \return True to trigger quirks, false otherwise
 */
static bool lookup_limited_quirks(hubbub_treebuilder *treebuilder,
		const hubbub_doctype *cdoc)
{
	const uint8_t *public_id = cdoc->public_id.ptr;
	size_t public_id_len = cdoc->public_id.len;

	UNUSED(treebuilder);

#define S(s)	(uint8_t *) s, sizeof s

	if (starts_with(public_id, public_id_len,
					S("-//W3C//DTD XHTML 1.0 Frameset//")) ||
			starts_with(public_id, public_id_len,
					S("-//W3C//DTD XHTML 1.0 Transitional//"))) {
		return true;
	}

	if (cdoc->system_missing == false &&
			(starts_with(public_id, public_id_len,
					S("-//W3C//DTD HTML 4.01 Frameset//")) ||
			starts_with(public_id, public_id_len,
					S("-//W3C//DTD HTML 4.01 Transitional//")))) {
		return true;
	}

#undef S

	return false;
}


/**
 * Handle token in initial insertion mode
 *
 * \param treebuilder  The treebuilder instance
 * \param token        The token to handle
 * \return True to reprocess token, false otherwise
 */
hubbub_error handle_initial(hubbub_treebuilder *treebuilder, const hubbub_token *token)
{
	hubbub_error err = HUBBUB_OK;

	switch (token->type) {
	case HUBBUB_TOKEN_CHARACTER:
		if (process_characters_expect_whitespace(treebuilder, token,
				false)) {
			/** \todo parse error */

			treebuilder->tree_handler->set_quirks_mode(
					treebuilder->tree_handler->ctx,
					HUBBUB_QUIRKS_MODE_FULL);
			treebuilder->context.mode = BEFORE_HTML;
			err = HUBBUB_REPROCESS;
		}
		break;
	case HUBBUB_TOKEN_COMMENT:
		process_comment_append(treebuilder, token,
				treebuilder->context.document);
		break;
	case HUBBUB_TOKEN_DOCTYPE:
	{
		int success;
		void *doctype, *appended;
		const hubbub_doctype *cdoc;

		/** \todo parse error */

		/** \todo need public and system ids from tokeniser */
		success = treebuilder->tree_handler->create_doctype(
				treebuilder->tree_handler->ctx,
				&token->data.doctype,
				&doctype);
		if (success != 0) {
			/** \todo errors */
		}

		/* Append to Document node */
		success = treebuilder->tree_handler->append_child(
				treebuilder->tree_handler->ctx,
				treebuilder->context.document,
				doctype, &appended);
		if (success != 0) {
			/** \todo errors */
			treebuilder->tree_handler->unref_node(
					treebuilder->tree_handler->ctx,
					doctype);
		}

		treebuilder->tree_handler->unref_node(
				treebuilder->tree_handler->ctx, appended);
		treebuilder->tree_handler->unref_node(
				treebuilder->tree_handler->ctx, doctype);

		cdoc = &token->data.doctype;

		/* Work out whether we need quirks mode or not */
		if (cdoc->force_quirks == true ||
				lookup_full_quirks(treebuilder, cdoc)) {
			treebuilder->tree_handler->set_quirks_mode(
					treebuilder->tree_handler->ctx,
					HUBBUB_QUIRKS_MODE_FULL);
		} else if (lookup_limited_quirks(treebuilder, cdoc)) {
			treebuilder->tree_handler->set_quirks_mode(
					treebuilder->tree_handler->ctx,
					HUBBUB_QUIRKS_MODE_LIMITED);
		}

		treebuilder->context.mode = BEFORE_HTML;
	}
		break;
	case HUBBUB_TOKEN_START_TAG:
	case HUBBUB_TOKEN_END_TAG:
	case HUBBUB_TOKEN_EOF:
		/** \todo parse error */
		treebuilder->tree_handler->set_quirks_mode(
				treebuilder->tree_handler->ctx,
				HUBBUB_QUIRKS_MODE_FULL);
		err = HUBBUB_REPROCESS;
		break;
	}

	if (err == HUBBUB_REPROCESS) {
		treebuilder->context.mode = BEFORE_HTML;
	}

	return err;
}