summaryrefslogtreecommitdiff
path: root/src/treebuilder/in_foreign_content.c
blob: d53be1ff5344d92f846ea460cb14ac03bb9ff150 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * 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"
#include "utils/string.h"


/*** Attribute-correction stuff ***/

#define S(s)		s, SLEN(s)

/**
 * Mapping table for case changes
 */
typedef struct
{
	const char *attr;	/**< Lower case attribute name */
	size_t len;		/**< Length of name in bytes */
	const char *proper;	/**< Correctly cased version */
} case_changes;

static const case_changes svg_attributes[] = {
	{ S("attributename"),		"attributeName" },
	{ S("attributetype"),		"attributeType" },
	{ S("basefrequency"),		"baseFrequency" },
	{ S("baseprofile"),		"baseProfile" },
	{ S("calcmode"),		"calcMode" },
	{ S("clippathunits"),		"clipPathUnits" },
	{ S("contentscripttype"),	"contentScriptType" },
	{ S("contentstyletype"),	"contentStyleType" },
	{ S("diffuseconstant"),		"diffuseConstant" },
	{ S("edgemode"),		"edgeMode" },
	{ S("externalresourcesrequired"),	"externalResourcesRequired" },
	{ S("filterres"),		"filterRes" },
	{ S("filterunits"),		"filterUnits" },
	{ S("glyphref"),		"glyphRef" },
	{ S("gradienttransform"),	"gradientTransform" },
	{ S("gradientunits"),		"gradientUnits" },
	{ S("kernelmatrix"),		"kernelMatrix" },
	{ S("kernelunitlength"),	"kernelUnitLength" },
	{ S("keypoints"),		"keyPoints" },
	{ S("keysplines"),		"keySplines" },
	{ S("keytimes"),		"keyTimes" },
	{ S("lengthadjust"),		"lengthAdjust" },
	{ S("limitingconeangle"),	"limitingConeAngle" },
	{ S("markerheight"),		"markerHeight" },
	{ S("markerunits"),		"markerUnits" },
	{ S("markerwidth"),		"markerWidth" },
	{ S("maskcontentunits"),	"maskContentUnits" },
	{ S("maskunits"),		"maskUnits" },
	{ S("numoctaves"),		"numOctaves" },
	{ S("pathlength"),		"pathLength" },
	{ S("patterncontentunits"),	"patternContentUnits" },
	{ S("patterntransform"),	"patternTransform" },
	{ S("patternunits"),		"patternUnits" },
	{ S("pointsatx"),		"pointsAtX" },
	{ S("pointsaty"),		"pointsAtY" },
	{ S("pointsatz"),		"pointsAtZ" },
	{ S("preservealpha"),		"preserveAlpha" },
	{ S("preserveaspectratio"),	"preserveAspectRatio" },
	{ S("primitiveunits"),		"primitiveUnits" },
	{ S("refx"),			"refX" },
	{ S("refy"),			"refY" },
	{ S("repeatcount"),		"repeatCount" },
	{ S("repeatdur"),		"repeatDur" },
	{ S("requiredextensions"),	"requiredExtensions" },
	{ S("requiredfeatures"),	"requiredFeatures" },
	{ S("specularconstant"),	"specularConstant" },
	{ S("specularexponent"),	"specularExponent" },
	{ S("spreadmethod"),		"spreadMethod" },
	{ S("startoffset"),		"startOffset" },
	{ S("stddeviation"),		"stdDeviation" },
	{ S("stitchtiles"),		"stitchTiles" },
	{ S("surfacescale"),		"surfaceScale" },
	{ S("systemlanguage"),		"systemLanguage" },
	{ S("tablevalues"),		"tableValues" },
	{ S("targetx"),			"targetX" },
	{ S("targety"),			"targetY" },
	{ S("textlength"),		"textLength" },
	{ S("viewbox"),			"viewBox" },
	{ S("viewtarget"),		"viewTarget" },
	{ S("xchannelselector"),	"xChannelSelector" },
	{ S("ychannelselector"),	"yChannelSelector" },
	{ S("zoomandpan"),		"zoomAndPan" },
};

static const case_changes svg_tagnames[] = {
	{ S("altglyph"),		"altGlyph" },
	{ S("altglyphdef"),		"altGlyphDef" },
	{ S("altglyphitem"),		"altGlyphItem" },
	{ S("animatecolor"),		"animateColor" },
	{ S("animatemotion"),		"animateMotion" },
	{ S("animatetransform"),	"animateTransform" },
	{ S("clippath"),		"clipPath" },
	{ S("feblend"),			"feBlend" },
	{ S("fecolormatrix"),		"feColorMatrix" },
	{ S("fecomponenttransfer"),	"feComponentTransfer" },
	{ S("fecomposite"),		"feComposite" },
	{ S("feconvolvematrix"),	"feConvolveMatrix" },
	{ S("fediffuselighting"),	"feDiffuseLighting" },
	{ S("fedisplacementmap"),	"feDisplacementMap" },
	{ S("fedistantlight"),		"feDistantLight" },
	{ S("feflood"),			"feFlood" },
	{ S("fefunca"),			"feFuncA" },
	{ S("fefuncb"),			"feFuncB" },
	{ S("fefuncg"),			"feFuncG" },
	{ S("fefuncr"),			"feFuncR" },
	{ S("fegaussianblur"),		"feGaussianBlur" },
	{ S("feimage"),			"feImage" },
	{ S("femerge"),			"feMerge" },
	{ S("femergenode"),		"feMergeNode" },
	{ S("femorphology"),		"feMorphology" },
	{ S("feoffset"),		"feOffset" },
	{ S("fepointlight"),		"fePointLight" },
	{ S("fespecularlighting"),	"feSpecularLighting" },
	{ S("fespotlight"),		"feSpotLight" },
	{ S("fetile"),			"feTile" },
	{ S("feturbulence"),		"feTurbulence" },
	{ S("foreignobject"),		"foreignObject" },
	{ S("glyphref"),		"glyphRef" },
	{ S("lineargradient"),		"linearGradient" },
	{ S("radialgradient"),		"radialGradient" },
	{ S("textpath"),		"textPath" },
};

#undef S

/**
 * Adjust MathML attributes
 *
 * \param treebuilder  Treebuilder instance
 * \param tag          Tag to adjust the attributes of
 */
void adjust_mathml_attributes(hubbub_treebuilder *treebuilder,
		hubbub_tag *tag)
{
	size_t i;
	UNUSED(treebuilder);

	for (i = 0; i < tag->n_attributes; i++) {
		hubbub_attribute *attr = &tag->attributes[i];
		const uint8_t *name = attr->name.ptr;
		size_t len = attr->name.len;

		if (hubbub_string_match(name, len, 
				(const uint8_t *) "definitionurl", 
				SLEN("definitionurl"))) {
			attr->name.ptr = (uint8_t *) "definitionURL";
		}
	}
}

/**
 * Adjust SVG attributes.
 *
 * \param treebuilder	Treebuilder instance
 * \param tag		Tag to adjust the attributes of
 */
void adjust_svg_attributes(hubbub_treebuilder *treebuilder,
		hubbub_tag *tag)
{
	size_t i;
	UNUSED(treebuilder);

	for (i = 0; i < tag->n_attributes; i++) {
		hubbub_attribute *attr = &tag->attributes[i];

		const uint8_t *name = attr->name.ptr;
		size_t len = attr->name.len;
		size_t j;

		for (j = 0; j < N_ELEMENTS(svg_attributes); j++) {
			if (hubbub_string_match(name, len,
					(uint8_t *) svg_attributes[j].attr,
					svg_attributes[j].len)) {
				attr->name.ptr =
					(uint8_t *) svg_attributes[j].proper;
			}
		}
	}
}

/**
 * Adjust SVG tagnmes.
 *
 * \param treebuilder	Treebuilder instance
 * \param tag		Tag to adjust the name of
 */
void adjust_svg_tagname(hubbub_treebuilder *treebuilder,
		hubbub_tag *tag)
{
	const uint8_t *name = tag->name.ptr;
	size_t len = tag->name.len;
	size_t i;

	UNUSED(treebuilder);

	for (i = 0; i < N_ELEMENTS(svg_tagnames); i++) {
		if (hubbub_string_match(name, len,
				(uint8_t *) svg_tagnames[i].attr,
				svg_tagnames[i].len)) {
			tag->name.ptr =	(uint8_t *) svg_tagnames[i].proper;
		}
	}
}



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

/**
 * Adjust foreign attributes.
 *
 * \param treebuilder	Treebuilder instance
 * \param tag		Tag to adjust the attributes of
 */
void adjust_foreign_attributes(hubbub_treebuilder *treebuilder,
		hubbub_tag *tag)
{
	size_t i;
	UNUSED(treebuilder);

	for (i = 0; i < tag->n_attributes; i++) {
		hubbub_attribute *attr = &tag->attributes[i];
		const uint8_t *name = attr->name.ptr;

		/* 10 == strlen("xlink:href") */
		if (attr->name.len >= 10 &&
				strncmp((char *) name, "xlink:", 
						SLEN("xlink:")) == 0) {
			size_t len = attr->name.len - 6;
			name += 6;

			if (hubbub_string_match(name, len, S("actuate")) ||
					hubbub_string_match(name, len,
							S("arcrole")) ||
					hubbub_string_match(name, len,
							S("href")) ||
					hubbub_string_match(name, len,
							S("role")) ||
					hubbub_string_match(name, len,
							S("show")) ||
					hubbub_string_match(name, len,
							S("title")) ||
					hubbub_string_match(name, len,
							S("type"))) {
				attr->ns = HUBBUB_NS_XLINK;
				attr->name.ptr += 6;
				attr->name.len -= 6;
			}
		/* 8 == strlen("xml:base") */
		} else if (attr->name.len >= 8 &&
				strncmp((char *) name, "xml:", 
						SLEN("xml:")) == 0) {
			size_t len = attr->name.len - 4;
			name += 4;

			if (hubbub_string_match(name, len, S("base")) ||
					hubbub_string_match(name, len,
							S("lang")) ||
					hubbub_string_match(name, len,
							S("space"))) {
				attr->ns = HUBBUB_NS_XML;
				attr->name.ptr += 4;
				attr->name.len -= 4;
			}
		} else if (hubbub_string_match(name, attr->name.len,
						S("xmlns"))) {
			attr->ns = HUBBUB_NS_XMLNS;
		} else if (hubbub_string_match(name, attr->name.len,
						S("xmlns:xlink"))) {
			attr->ns = HUBBUB_NS_XMLNS;
			attr->name.ptr += 6;
			attr->name.len -= 6;
		}

	}
}

#undef S



/*** Foreign content insertion mode ***/


/**
 * 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 hubbub_error process_as_in_secondary(hubbub_treebuilder *treebuilder,
		const hubbub_token *token)
{
	hubbub_error err;

	/* 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;

	err = hubbub_treebuilder_token_handler(token, treebuilder);
	if (err != HUBBUB_OK) {
		treebuilder->context.mode = IN_FOREIGN_CONTENT;
		return err;
	}

	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;
	}

	return HUBBUB_OK;
}

/**
 * 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
 */
hubbub_error handle_in_foreign_content(hubbub_treebuilder *treebuilder,
		const hubbub_token *token)
{
	hubbub_error err = HUBBUB_OK;

	switch (token->type) {
	case HUBBUB_TOKEN_CHARACTER:
		err = append_text(treebuilder, &token->data.character);
		break;
	case HUBBUB_TOKEN_COMMENT:
		err = 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)) ||
			(type == SVG && (cur_node_ns == HUBBUB_NS_MATHML &&
				cur_node == ANNOTATION_XML)) ||
			(cur_node_ns == HUBBUB_NS_SVG &&
				(cur_node == FOREIGNOBJECT ||
				cur_node == DESC ||
				cur_node == TITLE))) {
			err = 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 == 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);
			err = HUBBUB_REPROCESS;
		} else if (type == FONT) {
			const hubbub_tag *tag = &token->data.tag;
			size_t i;

			for (i = 0; i < tag->n_attributes; i++) {
				hubbub_attribute *attr = &tag->attributes[i];
				const uint8_t *name = attr->name.ptr;
				size_t len = attr->name.len;

				if (hubbub_string_match(name, len, 
						(const uint8_t *) "color", 
						SLEN("color")) ||
						hubbub_string_match(name, len,
						(const uint8_t *) "face",
						SLEN("face")) ||
						hubbub_string_match(name, len,
						(const uint8_t *) "size",
						SLEN("size")))
					break;
			}

			if (i != tag->n_attributes) {
				foreign_break_out(treebuilder);
				err = HUBBUB_REPROCESS;
			}
		} else {
			hubbub_tag tag = token->data.tag;

			adjust_foreign_attributes(treebuilder, &tag);

			if (cur_node_ns == HUBBUB_NS_SVG) {
				adjust_svg_tagname(treebuilder, &tag);
				adjust_svg_attributes(treebuilder, &tag);
			}

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

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

	return err;
}