/src/libxml2/HTMLparser.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HTMLparser.c : an HTML 4.0 non-verifying parser |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * daniel@veillard.com |
7 | | */ |
8 | | |
9 | | #define IN_LIBXML |
10 | | #include "libxml.h" |
11 | | #ifdef LIBXML_HTML_ENABLED |
12 | | |
13 | | #include <string.h> |
14 | | #include <ctype.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include <libxml/HTMLparser.h> |
18 | | #include <libxml/xmlmemory.h> |
19 | | #include <libxml/tree.h> |
20 | | #include <libxml/parser.h> |
21 | | #include <libxml/parserInternals.h> |
22 | | #include <libxml/xmlerror.h> |
23 | | #include <libxml/HTMLtree.h> |
24 | | #include <libxml/entities.h> |
25 | | #include <libxml/encoding.h> |
26 | | #include <libxml/xmlIO.h> |
27 | | #include <libxml/uri.h> |
28 | | |
29 | | #include "private/buf.h" |
30 | | #include "private/enc.h" |
31 | | #include "private/error.h" |
32 | | #include "private/html.h" |
33 | | #include "private/io.h" |
34 | | #include "private/parser.h" |
35 | | #include "private/tree.h" |
36 | | |
37 | | #define HTML_MAX_NAMELEN 1000 |
38 | 0 | #define HTML_PARSER_BIG_BUFFER_SIZE 1000 |
39 | 0 | #define HTML_PARSER_BUFFER_SIZE 100 |
40 | | |
41 | | static int htmlOmittedDefaultValue = 1; |
42 | | |
43 | | static void htmlParseComment(htmlParserCtxtPtr ctxt); |
44 | | |
45 | | /************************************************************************ |
46 | | * * |
47 | | * Some factorized error routines * |
48 | | * * |
49 | | ************************************************************************/ |
50 | | |
51 | | /** |
52 | | * htmlErrMemory: |
53 | | * @ctxt: an HTML parser context |
54 | | * @extra: extra information |
55 | | * |
56 | | * Handle a redefinition of attribute error |
57 | | */ |
58 | | static void |
59 | | htmlErrMemory(xmlParserCtxtPtr ctxt) |
60 | 0 | { |
61 | 0 | xmlCtxtErrMemory(ctxt); |
62 | 0 | } |
63 | | |
64 | | /** |
65 | | * htmlParseErr: |
66 | | * @ctxt: an HTML parser context |
67 | | * @error: the error number |
68 | | * @msg: the error message |
69 | | * @str1: string infor |
70 | | * @str2: string infor |
71 | | * |
72 | | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
73 | | */ |
74 | | static void LIBXML_ATTR_FORMAT(3,0) |
75 | | htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
76 | | const char *msg, const xmlChar *str1, const xmlChar *str2) |
77 | 0 | { |
78 | 0 | xmlCtxtErr(ctxt, NULL, XML_FROM_HTML, error, XML_ERR_ERROR, |
79 | 0 | str1, str2, NULL, 0, msg, str1, str2); |
80 | 0 | } |
81 | | |
82 | | /** |
83 | | * htmlParseErrInt: |
84 | | * @ctxt: an HTML parser context |
85 | | * @error: the error number |
86 | | * @msg: the error message |
87 | | * @val: integer info |
88 | | * |
89 | | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
90 | | */ |
91 | | static void LIBXML_ATTR_FORMAT(3,0) |
92 | | htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
93 | | const char *msg, int val) |
94 | 0 | { |
95 | 0 | xmlCtxtErr(ctxt, NULL, XML_FROM_HTML, error, XML_ERR_ERROR, |
96 | 0 | NULL, NULL, NULL, val, msg, val); |
97 | 0 | } |
98 | | |
99 | | /************************************************************************ |
100 | | * * |
101 | | * Parser stacks related functions and macros * |
102 | | * * |
103 | | ************************************************************************/ |
104 | | |
105 | | /** |
106 | | * htmlnamePush: |
107 | | * @ctxt: an HTML parser context |
108 | | * @value: the element name |
109 | | * |
110 | | * Pushes a new element name on top of the name stack |
111 | | * |
112 | | * Returns -1 in case of error, the index in the stack otherwise |
113 | | */ |
114 | | static int |
115 | | htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value) |
116 | 0 | { |
117 | 0 | if ((ctxt->html < 3) && (xmlStrEqual(value, BAD_CAST "head"))) |
118 | 0 | ctxt->html = 3; |
119 | 0 | if ((ctxt->html < 10) && (xmlStrEqual(value, BAD_CAST "body"))) |
120 | 0 | ctxt->html = 10; |
121 | 0 | if (ctxt->nameNr >= ctxt->nameMax) { |
122 | 0 | size_t newSize = ctxt->nameMax * 2; |
123 | 0 | const xmlChar **tmp; |
124 | |
|
125 | 0 | tmp = xmlRealloc((xmlChar **) ctxt->nameTab, |
126 | 0 | newSize * sizeof(ctxt->nameTab[0])); |
127 | 0 | if (tmp == NULL) { |
128 | 0 | htmlErrMemory(ctxt); |
129 | 0 | return (-1); |
130 | 0 | } |
131 | 0 | ctxt->nameTab = tmp; |
132 | 0 | ctxt->nameMax = newSize; |
133 | 0 | } |
134 | 0 | ctxt->nameTab[ctxt->nameNr] = value; |
135 | 0 | ctxt->name = value; |
136 | 0 | return (ctxt->nameNr++); |
137 | 0 | } |
138 | | /** |
139 | | * htmlnamePop: |
140 | | * @ctxt: an HTML parser context |
141 | | * |
142 | | * Pops the top element name from the name stack |
143 | | * |
144 | | * Returns the name just removed |
145 | | */ |
146 | | static const xmlChar * |
147 | | htmlnamePop(htmlParserCtxtPtr ctxt) |
148 | 0 | { |
149 | 0 | const xmlChar *ret; |
150 | |
|
151 | 0 | if (ctxt->nameNr <= 0) |
152 | 0 | return (NULL); |
153 | 0 | ctxt->nameNr--; |
154 | 0 | if (ctxt->nameNr < 0) |
155 | 0 | return (NULL); |
156 | 0 | if (ctxt->nameNr > 0) |
157 | 0 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
158 | 0 | else |
159 | 0 | ctxt->name = NULL; |
160 | 0 | ret = ctxt->nameTab[ctxt->nameNr]; |
161 | 0 | ctxt->nameTab[ctxt->nameNr] = NULL; |
162 | 0 | return (ret); |
163 | 0 | } |
164 | | |
165 | | /** |
166 | | * htmlNodeInfoPush: |
167 | | * @ctxt: an HTML parser context |
168 | | * @value: the node info |
169 | | * |
170 | | * Pushes a new element name on top of the node info stack |
171 | | * |
172 | | * Returns 0 in case of error, the index in the stack otherwise |
173 | | */ |
174 | | static int |
175 | | htmlNodeInfoPush(htmlParserCtxtPtr ctxt, htmlParserNodeInfo *value) |
176 | 0 | { |
177 | 0 | if (ctxt->nodeInfoNr >= ctxt->nodeInfoMax) { |
178 | 0 | if (ctxt->nodeInfoMax == 0) |
179 | 0 | ctxt->nodeInfoMax = 5; |
180 | 0 | ctxt->nodeInfoMax *= 2; |
181 | 0 | ctxt->nodeInfoTab = (htmlParserNodeInfo *) |
182 | 0 | xmlRealloc((htmlParserNodeInfo *)ctxt->nodeInfoTab, |
183 | 0 | ctxt->nodeInfoMax * |
184 | 0 | sizeof(ctxt->nodeInfoTab[0])); |
185 | 0 | if (ctxt->nodeInfoTab == NULL) { |
186 | 0 | htmlErrMemory(ctxt); |
187 | 0 | return (0); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | ctxt->nodeInfoTab[ctxt->nodeInfoNr] = *value; |
191 | 0 | ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr]; |
192 | 0 | return (ctxt->nodeInfoNr++); |
193 | 0 | } |
194 | | |
195 | | /** |
196 | | * htmlNodeInfoPop: |
197 | | * @ctxt: an HTML parser context |
198 | | * |
199 | | * Pops the top element name from the node info stack |
200 | | * |
201 | | * Returns 0 in case of error, the pointer to NodeInfo otherwise |
202 | | */ |
203 | | static htmlParserNodeInfo * |
204 | | htmlNodeInfoPop(htmlParserCtxtPtr ctxt) |
205 | 0 | { |
206 | 0 | if (ctxt->nodeInfoNr <= 0) |
207 | 0 | return (NULL); |
208 | 0 | ctxt->nodeInfoNr--; |
209 | 0 | if (ctxt->nodeInfoNr < 0) |
210 | 0 | return (NULL); |
211 | 0 | if (ctxt->nodeInfoNr > 0) |
212 | 0 | ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr - 1]; |
213 | 0 | else |
214 | 0 | ctxt->nodeInfo = NULL; |
215 | 0 | return &ctxt->nodeInfoTab[ctxt->nodeInfoNr]; |
216 | 0 | } |
217 | | |
218 | | /* |
219 | | * Macros for accessing the content. Those should be used only by the parser, |
220 | | * and not exported. |
221 | | * |
222 | | * Dirty macros, i.e. one need to make assumption on the context to use them |
223 | | * |
224 | | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
225 | | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
226 | | * in ISO-Latin or UTF-8, and the current 16 bit value if compiled |
227 | | * in UNICODE mode. This should be used internally by the parser |
228 | | * only to compare to ASCII values otherwise it would break when |
229 | | * running with UTF-8 encoding. |
230 | | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
231 | | * to compare on ASCII based substring. |
232 | | * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR |
233 | | * it should be used only to compare on ASCII based substring. |
234 | | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
235 | | * strings without newlines within the parser. |
236 | | * |
237 | | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
238 | | * |
239 | | * NEXT Skip to the next character, this does the proper decoding |
240 | | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
241 | | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
242 | | * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly |
243 | | */ |
244 | | |
245 | 0 | #define UPPER (toupper(*ctxt->input->cur)) |
246 | | |
247 | 0 | #define SKIP(val) ctxt->input->cur += (val),ctxt->input->col+=(val) |
248 | | |
249 | 0 | #define NXT(val) ctxt->input->cur[(val)] |
250 | | |
251 | 0 | #define UPP(val) (toupper(ctxt->input->cur[(val)])) |
252 | | |
253 | 0 | #define CUR_PTR ctxt->input->cur |
254 | 0 | #define BASE_PTR ctxt->input->base |
255 | | |
256 | | #define SHRINK \ |
257 | 0 | if ((!PARSER_PROGRESSIVE(ctxt)) && \ |
258 | 0 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
259 | 0 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
260 | 0 | xmlParserShrink(ctxt); |
261 | | |
262 | | #define GROW \ |
263 | 0 | if ((!PARSER_PROGRESSIVE(ctxt)) && \ |
264 | 0 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
265 | 0 | xmlParserGrow(ctxt); |
266 | | |
267 | 0 | #define SKIP_BLANKS htmlSkipBlankChars(ctxt) |
268 | | |
269 | | /* Imported from XML */ |
270 | | |
271 | 0 | #define CUR (*ctxt->input->cur) |
272 | 0 | #define NEXT xmlNextChar(ctxt) |
273 | | |
274 | 0 | #define RAW (*ctxt->input->cur) |
275 | | |
276 | | |
277 | 0 | #define NEXTL(l) do { \ |
278 | 0 | if (*(ctxt->input->cur) == '\n') { \ |
279 | 0 | ctxt->input->line++; ctxt->input->col = 1; \ |
280 | 0 | } else ctxt->input->col++; \ |
281 | 0 | ctxt->input->cur += l; \ |
282 | 0 | } while (0) |
283 | | |
284 | | /************ |
285 | | \ |
286 | | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
287 | | if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt); |
288 | | ************/ |
289 | | |
290 | 0 | #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l) |
291 | | |
292 | | #define COPY_BUF(b, i, v) \ |
293 | 0 | if (v < 0x80) b[i++] = v; \ |
294 | 0 | else i += xmlCopyCharMultiByte(&b[i],v) |
295 | | |
296 | | /** |
297 | | * htmlFindEncoding: |
298 | | * @the HTML parser context |
299 | | * |
300 | | * Ty to find and encoding in the current data available in the input |
301 | | * buffer this is needed to try to switch to the proper encoding when |
302 | | * one face a character error. |
303 | | * That's an heuristic, since it's operating outside of parsing it could |
304 | | * try to use a meta which had been commented out, that's the reason it |
305 | | * should only be used in case of error, not as a default. |
306 | | * |
307 | | * Returns an encoding string or NULL if not found, the string need to |
308 | | * be freed |
309 | | */ |
310 | | static xmlChar * |
311 | 0 | htmlFindEncoding(xmlParserCtxtPtr ctxt) { |
312 | 0 | const xmlChar *start, *cur, *end; |
313 | 0 | xmlChar *ret; |
314 | |
|
315 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL) || |
316 | 0 | (ctxt->input->flags & XML_INPUT_HAS_ENCODING)) |
317 | 0 | return(NULL); |
318 | 0 | if ((ctxt->input->cur == NULL) || (ctxt->input->end == NULL)) |
319 | 0 | return(NULL); |
320 | | |
321 | 0 | start = ctxt->input->cur; |
322 | 0 | end = ctxt->input->end; |
323 | | /* we also expect the input buffer to be zero terminated */ |
324 | 0 | if (*end != 0) |
325 | 0 | return(NULL); |
326 | | |
327 | 0 | cur = xmlStrcasestr(start, BAD_CAST "HTTP-EQUIV"); |
328 | 0 | if (cur == NULL) |
329 | 0 | return(NULL); |
330 | 0 | cur = xmlStrcasestr(cur, BAD_CAST "CONTENT"); |
331 | 0 | if (cur == NULL) |
332 | 0 | return(NULL); |
333 | 0 | cur = xmlStrcasestr(cur, BAD_CAST "CHARSET="); |
334 | 0 | if (cur == NULL) |
335 | 0 | return(NULL); |
336 | 0 | cur += 8; |
337 | 0 | start = cur; |
338 | 0 | while (((*cur >= 'A') && (*cur <= 'Z')) || |
339 | 0 | ((*cur >= 'a') && (*cur <= 'z')) || |
340 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
341 | 0 | (*cur == '-') || (*cur == '_') || (*cur == ':') || (*cur == '/')) |
342 | 0 | cur++; |
343 | 0 | if (cur == start) |
344 | 0 | return(NULL); |
345 | 0 | ret = xmlStrndup(start, cur - start); |
346 | 0 | if (ret == NULL) |
347 | 0 | htmlErrMemory(ctxt); |
348 | 0 | return(ret); |
349 | 0 | } |
350 | | |
351 | | /** |
352 | | * htmlCurrentChar: |
353 | | * @ctxt: the HTML parser context |
354 | | * @len: pointer to the length of the char read |
355 | | * |
356 | | * The current char value, if using UTF-8 this may actually span multiple |
357 | | * bytes in the input buffer. Implement the end of line normalization: |
358 | | * 2.11 End-of-Line Handling |
359 | | * If the encoding is unspecified, in the case we find an ISO-Latin-1 |
360 | | * char, then the encoding converter is plugged in automatically. |
361 | | * |
362 | | * Returns the current char value and its length |
363 | | */ |
364 | | |
365 | | static int |
366 | 0 | htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) { |
367 | 0 | const unsigned char *cur; |
368 | 0 | unsigned char c; |
369 | 0 | unsigned int val; |
370 | |
|
371 | 0 | if (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK) |
372 | 0 | xmlParserGrow(ctxt); |
373 | |
|
374 | 0 | if ((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) { |
375 | 0 | xmlChar * guess; |
376 | | |
377 | | /* |
378 | | * Assume it's a fixed length encoding (1) with |
379 | | * a compatible encoding for the ASCII set, since |
380 | | * HTML constructs only use < 128 chars |
381 | | */ |
382 | 0 | if (*ctxt->input->cur < 0x80) { |
383 | 0 | if (*ctxt->input->cur == 0) { |
384 | 0 | if (ctxt->input->cur < ctxt->input->end) { |
385 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
386 | 0 | "Char 0x%X out of allowed range\n", 0); |
387 | 0 | *len = 1; |
388 | 0 | return(' '); |
389 | 0 | } else { |
390 | 0 | *len = 0; |
391 | 0 | return(0); |
392 | 0 | } |
393 | 0 | } |
394 | 0 | *len = 1; |
395 | 0 | return(*ctxt->input->cur); |
396 | 0 | } |
397 | | |
398 | | /* |
399 | | * Humm this is bad, do an automatic flow conversion |
400 | | */ |
401 | 0 | guess = htmlFindEncoding(ctxt); |
402 | 0 | if (guess == NULL) { |
403 | 0 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1); |
404 | 0 | } else { |
405 | 0 | xmlSwitchEncodingName(ctxt, (const char *) guess); |
406 | 0 | xmlFree(guess); |
407 | 0 | } |
408 | 0 | ctxt->input->flags |= XML_INPUT_HAS_ENCODING; |
409 | 0 | } |
410 | | |
411 | | /* |
412 | | * We are supposed to handle UTF8, check it's valid |
413 | | * From rfc2044: encoding of the Unicode values on UTF-8: |
414 | | * |
415 | | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
416 | | * 0000 0000-0000 007F 0xxxxxxx |
417 | | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
418 | | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
419 | | * |
420 | | * Check for the 0x110000 limit too |
421 | | */ |
422 | 0 | cur = ctxt->input->cur; |
423 | 0 | c = *cur; |
424 | 0 | if (c & 0x80) { |
425 | 0 | size_t avail; |
426 | |
|
427 | 0 | if ((c & 0x40) == 0) |
428 | 0 | goto encoding_error; |
429 | | |
430 | 0 | avail = ctxt->input->end - ctxt->input->cur; |
431 | |
|
432 | 0 | if ((avail < 2) || ((cur[1] & 0xc0) != 0x80)) |
433 | 0 | goto encoding_error; |
434 | 0 | if ((c & 0xe0) == 0xe0) { |
435 | 0 | if ((avail < 3) || ((cur[2] & 0xc0) != 0x80)) |
436 | 0 | goto encoding_error; |
437 | 0 | if ((c & 0xf0) == 0xf0) { |
438 | 0 | if (((c & 0xf8) != 0xf0) || |
439 | 0 | (avail < 4) || ((cur[3] & 0xc0) != 0x80)) |
440 | 0 | goto encoding_error; |
441 | | /* 4-byte code */ |
442 | 0 | *len = 4; |
443 | 0 | val = (cur[0] & 0x7) << 18; |
444 | 0 | val |= (cur[1] & 0x3f) << 12; |
445 | 0 | val |= (cur[2] & 0x3f) << 6; |
446 | 0 | val |= cur[3] & 0x3f; |
447 | 0 | if (val < 0x10000) |
448 | 0 | goto encoding_error; |
449 | 0 | } else { |
450 | | /* 3-byte code */ |
451 | 0 | *len = 3; |
452 | 0 | val = (cur[0] & 0xf) << 12; |
453 | 0 | val |= (cur[1] & 0x3f) << 6; |
454 | 0 | val |= cur[2] & 0x3f; |
455 | 0 | if (val < 0x800) |
456 | 0 | goto encoding_error; |
457 | 0 | } |
458 | 0 | } else { |
459 | | /* 2-byte code */ |
460 | 0 | *len = 2; |
461 | 0 | val = (cur[0] & 0x1f) << 6; |
462 | 0 | val |= cur[1] & 0x3f; |
463 | 0 | if (val < 0x80) |
464 | 0 | goto encoding_error; |
465 | 0 | } |
466 | 0 | if (!IS_CHAR(val)) { |
467 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
468 | 0 | "Char 0x%X out of allowed range\n", val); |
469 | 0 | } |
470 | 0 | return(val); |
471 | 0 | } else { |
472 | 0 | if (*ctxt->input->cur == 0) { |
473 | 0 | if (ctxt->input->cur < ctxt->input->end) { |
474 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
475 | 0 | "Char 0x%X out of allowed range\n", 0); |
476 | 0 | *len = 1; |
477 | 0 | return(' '); |
478 | 0 | } else { |
479 | 0 | *len = 0; |
480 | 0 | return(0); |
481 | 0 | } |
482 | 0 | } |
483 | | /* 1-byte code */ |
484 | 0 | *len = 1; |
485 | 0 | return(*ctxt->input->cur); |
486 | 0 | } |
487 | | |
488 | 0 | encoding_error: |
489 | 0 | xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL); |
490 | |
|
491 | 0 | if ((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) |
492 | 0 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1); |
493 | 0 | *len = 1; |
494 | 0 | return(*ctxt->input->cur); |
495 | 0 | } |
496 | | |
497 | | /** |
498 | | * htmlSkipBlankChars: |
499 | | * @ctxt: the HTML parser context |
500 | | * |
501 | | * skip all blanks character found at that point in the input streams. |
502 | | * |
503 | | * Returns the number of space chars skipped |
504 | | */ |
505 | | |
506 | | static int |
507 | 0 | htmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
508 | 0 | int res = 0; |
509 | |
|
510 | 0 | while (IS_BLANK_CH(*(ctxt->input->cur))) { |
511 | 0 | if (*(ctxt->input->cur) == '\n') { |
512 | 0 | ctxt->input->line++; ctxt->input->col = 1; |
513 | 0 | } else ctxt->input->col++; |
514 | 0 | ctxt->input->cur++; |
515 | 0 | if (*ctxt->input->cur == 0) |
516 | 0 | xmlParserGrow(ctxt); |
517 | 0 | if (res < INT_MAX) |
518 | 0 | res++; |
519 | 0 | } |
520 | 0 | return(res); |
521 | 0 | } |
522 | | |
523 | | |
524 | | |
525 | | /************************************************************************ |
526 | | * * |
527 | | * The list of HTML elements and their properties * |
528 | | * * |
529 | | ************************************************************************/ |
530 | | |
531 | | /* |
532 | | * Start Tag: 1 means the start tag can be omitted |
533 | | * End Tag: 1 means the end tag can be omitted |
534 | | * 2 means it's forbidden (empty elements) |
535 | | * 3 means the tag is stylistic and should be closed easily |
536 | | * Depr: this element is deprecated |
537 | | * DTD: 1 means that this element is valid only in the Loose DTD |
538 | | * 2 means that this element is valid only in the Frameset DTD |
539 | | * |
540 | | * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description |
541 | | , subElements , impliedsubelt , Attributes, userdata |
542 | | */ |
543 | | |
544 | | /* Definitions and a couple of vars for HTML Elements */ |
545 | | |
546 | | #define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small" |
547 | | #define NB_FONTSTYLE 8 |
548 | | #define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym" |
549 | | #define NB_PHRASE 10 |
550 | | #define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe" |
551 | | #define NB_SPECIAL 16 |
552 | | #define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL |
553 | | #define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL |
554 | | #define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address" |
555 | | #define NB_BLOCK NB_HEADING + NB_LIST + 14 |
556 | | #define FORMCTRL "input", "select", "textarea", "label", "button" |
557 | | #define NB_FORMCTRL 5 |
558 | | #define PCDATA |
559 | | #define NB_PCDATA 0 |
560 | | #define HEADING "h1", "h2", "h3", "h4", "h5", "h6" |
561 | | #define NB_HEADING 6 |
562 | | #define LIST "ul", "ol", "dir", "menu" |
563 | | #define NB_LIST 4 |
564 | | #define MODIFIER |
565 | | #define NB_MODIFIER 0 |
566 | | #define FLOW BLOCK,INLINE |
567 | | #define NB_FLOW NB_BLOCK + NB_INLINE |
568 | | #define EMPTY NULL |
569 | | |
570 | | |
571 | | static const char* const html_flow[] = { FLOW, NULL } ; |
572 | | static const char* const html_inline[] = { INLINE, NULL } ; |
573 | | |
574 | | /* placeholders: elts with content but no subelements */ |
575 | | static const char* const html_pcdata[] = { NULL } ; |
576 | | #define html_cdata html_pcdata |
577 | | |
578 | | |
579 | | /* ... and for HTML Attributes */ |
580 | | |
581 | | #define COREATTRS "id", "class", "style", "title" |
582 | | #define NB_COREATTRS 4 |
583 | | #define I18N "lang", "dir" |
584 | | #define NB_I18N 2 |
585 | | #define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup" |
586 | | #define NB_EVENTS 9 |
587 | | #define ATTRS COREATTRS,I18N,EVENTS |
588 | | #define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS |
589 | | #define CELLHALIGN "align", "char", "charoff" |
590 | | #define NB_CELLHALIGN 3 |
591 | | #define CELLVALIGN "valign" |
592 | | #define NB_CELLVALIGN 1 |
593 | | |
594 | | static const char* const html_attrs[] = { ATTRS, NULL } ; |
595 | | static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ; |
596 | | static const char* const core_attrs[] = { COREATTRS, NULL } ; |
597 | | static const char* const i18n_attrs[] = { I18N, NULL } ; |
598 | | |
599 | | |
600 | | /* Other declarations that should go inline ... */ |
601 | | static const char* const a_attrs[] = { ATTRS, "charset", "type", "name", |
602 | | "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords", |
603 | | "tabindex", "onfocus", "onblur", NULL } ; |
604 | | static const char* const target_attr[] = { "target", NULL } ; |
605 | | static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ; |
606 | | static const char* const alt_attr[] = { "alt", NULL } ; |
607 | | static const char* const src_alt_attrs[] = { "src", "alt", NULL } ; |
608 | | static const char* const href_attrs[] = { "href", NULL } ; |
609 | | static const char* const clear_attrs[] = { "clear", NULL } ; |
610 | | static const char* const inline_p[] = { INLINE, "p", NULL } ; |
611 | | |
612 | | static const char* const flow_param[] = { FLOW, "param", NULL } ; |
613 | | static const char* const applet_attrs[] = { COREATTRS , "codebase", |
614 | | "archive", "alt", "name", "height", "width", "align", |
615 | | "hspace", "vspace", NULL } ; |
616 | | static const char* const area_attrs[] = { "shape", "coords", "href", "nohref", |
617 | | "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
618 | | static const char* const basefont_attrs[] = |
619 | | { "id", "size", "color", "face", NULL } ; |
620 | | static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ; |
621 | | static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ; |
622 | | static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ; |
623 | | static const char* const body_depr[] = { "background", "bgcolor", "text", |
624 | | "link", "vlink", "alink", NULL } ; |
625 | | static const char* const button_attrs[] = { ATTRS, "name", "value", "type", |
626 | | "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
627 | | |
628 | | |
629 | | static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ; |
630 | | static const char* const col_elt[] = { "col", NULL } ; |
631 | | static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ; |
632 | | static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ; |
633 | | static const char* const dl_contents[] = { "dt", "dd", NULL } ; |
634 | | static const char* const compact_attr[] = { "compact", NULL } ; |
635 | | static const char* const label_attr[] = { "label", NULL } ; |
636 | | static const char* const fieldset_contents[] = { FLOW, "legend" } ; |
637 | | static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ; |
638 | | static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ; |
639 | | static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ; |
640 | | static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ; |
641 | | static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ; |
642 | | static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ; |
643 | | static const char* const head_attrs[] = { I18N, "profile", NULL } ; |
644 | | static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ; |
645 | | static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ; |
646 | | static const char* const version_attr[] = { "version", NULL } ; |
647 | | static const char* const html_content[] = { "head", "body", "frameset", NULL } ; |
648 | | static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ; |
649 | | static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ; |
650 | | static const char* const embed_attrs[] = { COREATTRS, "align", "alt", "border", "code", "codebase", "frameborder", "height", "hidden", "hspace", "name", "palette", "pluginspace", "pluginurl", "src", "type", "units", "vspace", "width", NULL } ; |
651 | | static const char* const input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ; |
652 | | static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ; |
653 | | static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ; |
654 | | static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ; |
655 | | static const char* const align_attr[] = { "align", NULL } ; |
656 | | static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ; |
657 | | static const char* const map_contents[] = { BLOCK, "area", NULL } ; |
658 | | static const char* const name_attr[] = { "name", NULL } ; |
659 | | static const char* const action_attr[] = { "action", NULL } ; |
660 | | static const char* const blockli_elt[] = { BLOCK, "li", NULL } ; |
661 | | static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", "charset", NULL } ; |
662 | | static const char* const content_attr[] = { "content", NULL } ; |
663 | | static const char* const type_attr[] = { "type", NULL } ; |
664 | | static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ; |
665 | | static const char* const object_contents[] = { FLOW, "param", NULL } ; |
666 | | static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ; |
667 | | static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ; |
668 | | static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ; |
669 | | static const char* const option_elt[] = { "option", NULL } ; |
670 | | static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ; |
671 | | static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ; |
672 | | static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ; |
673 | | static const char* const width_attr[] = { "width", NULL } ; |
674 | | static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ; |
675 | | static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ; |
676 | | static const char* const language_attr[] = { "language", NULL } ; |
677 | | static const char* const select_content[] = { "optgroup", "option", NULL } ; |
678 | | static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ; |
679 | | static const char* const style_attrs[] = { I18N, "media", "title", NULL } ; |
680 | | static const char* const table_attrs[] = { ATTRS, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ; |
681 | | static const char* const table_depr[] = { "align", "bgcolor", NULL } ; |
682 | | static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ; |
683 | | static const char* const tr_elt[] = { "tr", NULL } ; |
684 | | static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ; |
685 | | static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ; |
686 | | static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ; |
687 | | static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ; |
688 | | static const char* const tr_contents[] = { "th", "td", NULL } ; |
689 | | static const char* const bgcolor_attr[] = { "bgcolor", NULL } ; |
690 | | static const char* const li_elt[] = { "li", NULL } ; |
691 | | static const char* const ul_depr[] = { "type", "compact", NULL} ; |
692 | | static const char* const dir_attr[] = { "dir", NULL} ; |
693 | | |
694 | | #define DECL (const char**) |
695 | | |
696 | | static const htmlElemDesc |
697 | | html40ElementTable[] = { |
698 | | { "a", 0, 0, 0, 0, 0, 0, 1, "anchor ", |
699 | | DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL |
700 | | }, |
701 | | { "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form", |
702 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
703 | | }, |
704 | | { "acronym", 0, 0, 0, 0, 0, 0, 1, "", |
705 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
706 | | }, |
707 | | { "address", 0, 0, 0, 0, 0, 0, 0, "information on author ", |
708 | | DECL inline_p , NULL , DECL html_attrs, NULL, NULL |
709 | | }, |
710 | | { "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ", |
711 | | DECL flow_param , NULL , NULL , DECL applet_attrs, NULL |
712 | | }, |
713 | | { "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ", |
714 | | EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr |
715 | | }, |
716 | | { "b", 0, 3, 0, 0, 0, 0, 1, "bold text style", |
717 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
718 | | }, |
719 | | { "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ", |
720 | | EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs |
721 | | }, |
722 | | { "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " , |
723 | | EMPTY , NULL , NULL, DECL basefont_attrs, NULL |
724 | | }, |
725 | | { "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ", |
726 | | DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr |
727 | | }, |
728 | | { "big", 0, 3, 0, 0, 0, 0, 1, "large text style", |
729 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
730 | | }, |
731 | | { "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ", |
732 | | DECL html_flow , NULL , DECL quote_attrs , NULL, NULL |
733 | | }, |
734 | | { "body", 1, 1, 0, 0, 0, 0, 0, "document body ", |
735 | | DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL |
736 | | }, |
737 | | { "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ", |
738 | | EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL |
739 | | }, |
740 | | { "button", 0, 0, 0, 0, 0, 0, 2, "push button ", |
741 | | DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL |
742 | | }, |
743 | | { "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ", |
744 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
745 | | }, |
746 | | { "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ", |
747 | | DECL html_flow , NULL , NULL, DECL html_attrs, NULL |
748 | | }, |
749 | | { "cite", 0, 0, 0, 0, 0, 0, 1, "citation", |
750 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
751 | | }, |
752 | | { "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment", |
753 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
754 | | }, |
755 | | { "col", 0, 2, 2, 1, 0, 0, 0, "table column ", |
756 | | EMPTY , NULL , DECL col_attrs , NULL, NULL |
757 | | }, |
758 | | { "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ", |
759 | | DECL col_elt , "col" , DECL col_attrs , NULL, NULL |
760 | | }, |
761 | | { "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ", |
762 | | DECL html_flow , NULL , DECL html_attrs, NULL, NULL |
763 | | }, |
764 | | { "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ", |
765 | | DECL html_flow , NULL , DECL edit_attrs , NULL, NULL |
766 | | }, |
767 | | { "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition", |
768 | | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
769 | | }, |
770 | | { "dir", 0, 0, 0, 0, 1, 1, 0, "directory list", |
771 | | DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL |
772 | | }, |
773 | | { "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container", |
774 | | DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL |
775 | | }, |
776 | | { "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ", |
777 | | DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL |
778 | | }, |
779 | | { "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ", |
780 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
781 | | }, |
782 | | { "em", 0, 3, 0, 0, 0, 0, 1, "emphasis", |
783 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
784 | | }, |
785 | | { "embed", 0, 1, 0, 0, 1, 1, 1, "generic embedded object ", |
786 | | EMPTY, NULL, DECL embed_attrs, NULL, NULL |
787 | | }, |
788 | | { "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ", |
789 | | DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL |
790 | | }, |
791 | | { "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ", |
792 | | DECL html_inline, NULL, NULL, DECL font_attrs, NULL |
793 | | }, |
794 | | { "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ", |
795 | | DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr |
796 | | }, |
797 | | { "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " , |
798 | | EMPTY, NULL, NULL, DECL frame_attrs, NULL |
799 | | }, |
800 | | { "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" , |
801 | | DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL |
802 | | }, |
803 | | { "h1", 0, 0, 0, 0, 0, 0, 0, "heading ", |
804 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
805 | | }, |
806 | | { "h2", 0, 0, 0, 0, 0, 0, 0, "heading ", |
807 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
808 | | }, |
809 | | { "h3", 0, 0, 0, 0, 0, 0, 0, "heading ", |
810 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
811 | | }, |
812 | | { "h4", 0, 0, 0, 0, 0, 0, 0, "heading ", |
813 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
814 | | }, |
815 | | { "h5", 0, 0, 0, 0, 0, 0, 0, "heading ", |
816 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
817 | | }, |
818 | | { "h6", 0, 0, 0, 0, 0, 0, 0, "heading ", |
819 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
820 | | }, |
821 | | { "head", 1, 1, 0, 0, 0, 0, 0, "document head ", |
822 | | DECL head_contents, NULL, DECL head_attrs, NULL, NULL |
823 | | }, |
824 | | { "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " , |
825 | | EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL |
826 | | }, |
827 | | { "html", 1, 1, 0, 0, 0, 0, 0, "document root element ", |
828 | | DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL |
829 | | }, |
830 | | { "i", 0, 3, 0, 0, 0, 0, 1, "italic text style", |
831 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
832 | | }, |
833 | | { "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ", |
834 | | DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL |
835 | | }, |
836 | | { "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ", |
837 | | EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs |
838 | | }, |
839 | | { "input", 0, 2, 2, 1, 0, 0, 1, "form control ", |
840 | | EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL |
841 | | }, |
842 | | { "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text", |
843 | | DECL html_flow, NULL, DECL edit_attrs, NULL, NULL |
844 | | }, |
845 | | { "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ", |
846 | | EMPTY, NULL, NULL, DECL prompt_attrs, NULL |
847 | | }, |
848 | | { "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user", |
849 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
850 | | }, |
851 | | { "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ", |
852 | | DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL |
853 | | }, |
854 | | { "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ", |
855 | | DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL |
856 | | }, |
857 | | { "li", 0, 1, 1, 0, 0, 0, 0, "list item ", |
858 | | DECL html_flow, NULL, DECL html_attrs, NULL, NULL |
859 | | }, |
860 | | { "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ", |
861 | | EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL |
862 | | }, |
863 | | { "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ", |
864 | | DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr |
865 | | }, |
866 | | { "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ", |
867 | | DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL |
868 | | }, |
869 | | { "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ", |
870 | | EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr |
871 | | }, |
872 | | { "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ", |
873 | | DECL noframes_content, "body" , DECL html_attrs, NULL, NULL |
874 | | }, |
875 | | { "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ", |
876 | | DECL html_flow, "div", DECL html_attrs, NULL, NULL |
877 | | }, |
878 | | { "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ", |
879 | | DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL |
880 | | }, |
881 | | { "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ", |
882 | | DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL |
883 | | }, |
884 | | { "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ", |
885 | | DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr |
886 | | }, |
887 | | { "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " , |
888 | | DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL |
889 | | }, |
890 | | { "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ", |
891 | | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
892 | | }, |
893 | | { "param", 0, 2, 2, 1, 0, 0, 0, "named property value ", |
894 | | EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr |
895 | | }, |
896 | | { "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ", |
897 | | DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL |
898 | | }, |
899 | | { "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ", |
900 | | DECL html_inline, NULL, DECL quote_attrs, NULL, NULL |
901 | | }, |
902 | | { "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style", |
903 | | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
904 | | }, |
905 | | { "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.", |
906 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
907 | | }, |
908 | | { "script", 0, 0, 0, 0, 0, 0, 2, "script statements ", |
909 | | DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr |
910 | | }, |
911 | | { "select", 0, 0, 0, 0, 0, 0, 1, "option selector ", |
912 | | DECL select_content, NULL, DECL select_attrs, NULL, NULL |
913 | | }, |
914 | | { "small", 0, 3, 0, 0, 0, 0, 1, "small text style", |
915 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
916 | | }, |
917 | | { "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ", |
918 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
919 | | }, |
920 | | { "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text", |
921 | | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
922 | | }, |
923 | | { "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis", |
924 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
925 | | }, |
926 | | { "style", 0, 0, 0, 0, 0, 0, 0, "style info ", |
927 | | DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr |
928 | | }, |
929 | | { "sub", 0, 3, 0, 0, 0, 0, 1, "subscript", |
930 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
931 | | }, |
932 | | { "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ", |
933 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
934 | | }, |
935 | | { "table", 0, 0, 0, 0, 0, 0, 0, "", |
936 | | DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL |
937 | | }, |
938 | | { "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ", |
939 | | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
940 | | }, |
941 | | { "td", 0, 0, 0, 0, 0, 0, 0, "table data cell", |
942 | | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
943 | | }, |
944 | | { "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ", |
945 | | DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr |
946 | | }, |
947 | | { "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ", |
948 | | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
949 | | }, |
950 | | { "th", 0, 1, 0, 0, 0, 0, 0, "table header cell", |
951 | | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
952 | | }, |
953 | | { "thead", 0, 1, 0, 0, 0, 0, 0, "table header ", |
954 | | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
955 | | }, |
956 | | { "title", 0, 0, 0, 0, 0, 0, 0, "document title ", |
957 | | DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL |
958 | | }, |
959 | | { "tr", 0, 0, 0, 0, 0, 0, 0, "table row ", |
960 | | DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL |
961 | | }, |
962 | | { "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style", |
963 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
964 | | }, |
965 | | { "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style", |
966 | | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
967 | | }, |
968 | | { "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ", |
969 | | DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL |
970 | | }, |
971 | | { "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument", |
972 | | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
973 | | } |
974 | | }; |
975 | | |
976 | | typedef struct { |
977 | | const char *oldTag; |
978 | | const char *newTag; |
979 | | } htmlStartCloseEntry; |
980 | | |
981 | | /* |
982 | | * start tags that imply the end of current element |
983 | | */ |
984 | | static const htmlStartCloseEntry htmlStartClose[] = { |
985 | | { "a", "a" }, |
986 | | { "a", "fieldset" }, |
987 | | { "a", "table" }, |
988 | | { "a", "td" }, |
989 | | { "a", "th" }, |
990 | | { "address", "dd" }, |
991 | | { "address", "dl" }, |
992 | | { "address", "dt" }, |
993 | | { "address", "form" }, |
994 | | { "address", "li" }, |
995 | | { "address", "ul" }, |
996 | | { "b", "center" }, |
997 | | { "b", "p" }, |
998 | | { "b", "td" }, |
999 | | { "b", "th" }, |
1000 | | { "big", "p" }, |
1001 | | { "caption", "col" }, |
1002 | | { "caption", "colgroup" }, |
1003 | | { "caption", "tbody" }, |
1004 | | { "caption", "tfoot" }, |
1005 | | { "caption", "thead" }, |
1006 | | { "caption", "tr" }, |
1007 | | { "col", "col" }, |
1008 | | { "col", "colgroup" }, |
1009 | | { "col", "tbody" }, |
1010 | | { "col", "tfoot" }, |
1011 | | { "col", "thead" }, |
1012 | | { "col", "tr" }, |
1013 | | { "colgroup", "colgroup" }, |
1014 | | { "colgroup", "tbody" }, |
1015 | | { "colgroup", "tfoot" }, |
1016 | | { "colgroup", "thead" }, |
1017 | | { "colgroup", "tr" }, |
1018 | | { "dd", "dt" }, |
1019 | | { "dir", "dd" }, |
1020 | | { "dir", "dl" }, |
1021 | | { "dir", "dt" }, |
1022 | | { "dir", "form" }, |
1023 | | { "dir", "ul" }, |
1024 | | { "dl", "form" }, |
1025 | | { "dl", "li" }, |
1026 | | { "dt", "dd" }, |
1027 | | { "dt", "dl" }, |
1028 | | { "font", "center" }, |
1029 | | { "font", "td" }, |
1030 | | { "font", "th" }, |
1031 | | { "form", "form" }, |
1032 | | { "h1", "fieldset" }, |
1033 | | { "h1", "form" }, |
1034 | | { "h1", "li" }, |
1035 | | { "h1", "p" }, |
1036 | | { "h1", "table" }, |
1037 | | { "h2", "fieldset" }, |
1038 | | { "h2", "form" }, |
1039 | | { "h2", "li" }, |
1040 | | { "h2", "p" }, |
1041 | | { "h2", "table" }, |
1042 | | { "h3", "fieldset" }, |
1043 | | { "h3", "form" }, |
1044 | | { "h3", "li" }, |
1045 | | { "h3", "p" }, |
1046 | | { "h3", "table" }, |
1047 | | { "h4", "fieldset" }, |
1048 | | { "h4", "form" }, |
1049 | | { "h4", "li" }, |
1050 | | { "h4", "p" }, |
1051 | | { "h4", "table" }, |
1052 | | { "h5", "fieldset" }, |
1053 | | { "h5", "form" }, |
1054 | | { "h5", "li" }, |
1055 | | { "h5", "p" }, |
1056 | | { "h5", "table" }, |
1057 | | { "h6", "fieldset" }, |
1058 | | { "h6", "form" }, |
1059 | | { "h6", "li" }, |
1060 | | { "h6", "p" }, |
1061 | | { "h6", "table" }, |
1062 | | { "head", "a" }, |
1063 | | { "head", "abbr" }, |
1064 | | { "head", "acronym" }, |
1065 | | { "head", "address" }, |
1066 | | { "head", "b" }, |
1067 | | { "head", "bdo" }, |
1068 | | { "head", "big" }, |
1069 | | { "head", "blockquote" }, |
1070 | | { "head", "body" }, |
1071 | | { "head", "br" }, |
1072 | | { "head", "center" }, |
1073 | | { "head", "cite" }, |
1074 | | { "head", "code" }, |
1075 | | { "head", "dd" }, |
1076 | | { "head", "dfn" }, |
1077 | | { "head", "dir" }, |
1078 | | { "head", "div" }, |
1079 | | { "head", "dl" }, |
1080 | | { "head", "dt" }, |
1081 | | { "head", "em" }, |
1082 | | { "head", "fieldset" }, |
1083 | | { "head", "font" }, |
1084 | | { "head", "form" }, |
1085 | | { "head", "frameset" }, |
1086 | | { "head", "h1" }, |
1087 | | { "head", "h2" }, |
1088 | | { "head", "h3" }, |
1089 | | { "head", "h4" }, |
1090 | | { "head", "h5" }, |
1091 | | { "head", "h6" }, |
1092 | | { "head", "hr" }, |
1093 | | { "head", "i" }, |
1094 | | { "head", "iframe" }, |
1095 | | { "head", "img" }, |
1096 | | { "head", "kbd" }, |
1097 | | { "head", "li" }, |
1098 | | { "head", "listing" }, |
1099 | | { "head", "map" }, |
1100 | | { "head", "menu" }, |
1101 | | { "head", "ol" }, |
1102 | | { "head", "p" }, |
1103 | | { "head", "pre" }, |
1104 | | { "head", "q" }, |
1105 | | { "head", "s" }, |
1106 | | { "head", "samp" }, |
1107 | | { "head", "small" }, |
1108 | | { "head", "span" }, |
1109 | | { "head", "strike" }, |
1110 | | { "head", "strong" }, |
1111 | | { "head", "sub" }, |
1112 | | { "head", "sup" }, |
1113 | | { "head", "table" }, |
1114 | | { "head", "tt" }, |
1115 | | { "head", "u" }, |
1116 | | { "head", "ul" }, |
1117 | | { "head", "var" }, |
1118 | | { "head", "xmp" }, |
1119 | | { "hr", "form" }, |
1120 | | { "i", "center" }, |
1121 | | { "i", "p" }, |
1122 | | { "i", "td" }, |
1123 | | { "i", "th" }, |
1124 | | { "legend", "fieldset" }, |
1125 | | { "li", "li" }, |
1126 | | { "link", "body" }, |
1127 | | { "link", "frameset" }, |
1128 | | { "listing", "dd" }, |
1129 | | { "listing", "dl" }, |
1130 | | { "listing", "dt" }, |
1131 | | { "listing", "fieldset" }, |
1132 | | { "listing", "form" }, |
1133 | | { "listing", "li" }, |
1134 | | { "listing", "table" }, |
1135 | | { "listing", "ul" }, |
1136 | | { "menu", "dd" }, |
1137 | | { "menu", "dl" }, |
1138 | | { "menu", "dt" }, |
1139 | | { "menu", "form" }, |
1140 | | { "menu", "ul" }, |
1141 | | { "ol", "form" }, |
1142 | | { "option", "optgroup" }, |
1143 | | { "option", "option" }, |
1144 | | { "p", "address" }, |
1145 | | { "p", "blockquote" }, |
1146 | | { "p", "body" }, |
1147 | | { "p", "caption" }, |
1148 | | { "p", "center" }, |
1149 | | { "p", "col" }, |
1150 | | { "p", "colgroup" }, |
1151 | | { "p", "dd" }, |
1152 | | { "p", "dir" }, |
1153 | | { "p", "div" }, |
1154 | | { "p", "dl" }, |
1155 | | { "p", "dt" }, |
1156 | | { "p", "fieldset" }, |
1157 | | { "p", "form" }, |
1158 | | { "p", "frameset" }, |
1159 | | { "p", "h1" }, |
1160 | | { "p", "h2" }, |
1161 | | { "p", "h3" }, |
1162 | | { "p", "h4" }, |
1163 | | { "p", "h5" }, |
1164 | | { "p", "h6" }, |
1165 | | { "p", "head" }, |
1166 | | { "p", "hr" }, |
1167 | | { "p", "li" }, |
1168 | | { "p", "listing" }, |
1169 | | { "p", "menu" }, |
1170 | | { "p", "ol" }, |
1171 | | { "p", "p" }, |
1172 | | { "p", "pre" }, |
1173 | | { "p", "table" }, |
1174 | | { "p", "tbody" }, |
1175 | | { "p", "td" }, |
1176 | | { "p", "tfoot" }, |
1177 | | { "p", "th" }, |
1178 | | { "p", "title" }, |
1179 | | { "p", "tr" }, |
1180 | | { "p", "ul" }, |
1181 | | { "p", "xmp" }, |
1182 | | { "pre", "dd" }, |
1183 | | { "pre", "dl" }, |
1184 | | { "pre", "dt" }, |
1185 | | { "pre", "fieldset" }, |
1186 | | { "pre", "form" }, |
1187 | | { "pre", "li" }, |
1188 | | { "pre", "table" }, |
1189 | | { "pre", "ul" }, |
1190 | | { "s", "p" }, |
1191 | | { "script", "noscript" }, |
1192 | | { "small", "p" }, |
1193 | | { "span", "td" }, |
1194 | | { "span", "th" }, |
1195 | | { "strike", "p" }, |
1196 | | { "style", "body" }, |
1197 | | { "style", "frameset" }, |
1198 | | { "tbody", "tbody" }, |
1199 | | { "tbody", "tfoot" }, |
1200 | | { "td", "tbody" }, |
1201 | | { "td", "td" }, |
1202 | | { "td", "tfoot" }, |
1203 | | { "td", "th" }, |
1204 | | { "td", "tr" }, |
1205 | | { "tfoot", "tbody" }, |
1206 | | { "th", "tbody" }, |
1207 | | { "th", "td" }, |
1208 | | { "th", "tfoot" }, |
1209 | | { "th", "th" }, |
1210 | | { "th", "tr" }, |
1211 | | { "thead", "tbody" }, |
1212 | | { "thead", "tfoot" }, |
1213 | | { "title", "body" }, |
1214 | | { "title", "frameset" }, |
1215 | | { "tr", "tbody" }, |
1216 | | { "tr", "tfoot" }, |
1217 | | { "tr", "tr" }, |
1218 | | { "tt", "p" }, |
1219 | | { "u", "p" }, |
1220 | | { "u", "td" }, |
1221 | | { "u", "th" }, |
1222 | | { "ul", "address" }, |
1223 | | { "ul", "form" }, |
1224 | | { "ul", "menu" }, |
1225 | | { "ul", "pre" }, |
1226 | | { "xmp", "dd" }, |
1227 | | { "xmp", "dl" }, |
1228 | | { "xmp", "dt" }, |
1229 | | { "xmp", "fieldset" }, |
1230 | | { "xmp", "form" }, |
1231 | | { "xmp", "li" }, |
1232 | | { "xmp", "table" }, |
1233 | | { "xmp", "ul" } |
1234 | | }; |
1235 | | |
1236 | | /* |
1237 | | * The list of HTML elements which are supposed not to have |
1238 | | * CDATA content and where a p element will be implied |
1239 | | * |
1240 | | * TODO: extend that list by reading the HTML SGML DTD on |
1241 | | * implied paragraph |
1242 | | */ |
1243 | | static const char *const htmlNoContentElements[] = { |
1244 | | "html", |
1245 | | "head", |
1246 | | NULL |
1247 | | }; |
1248 | | |
1249 | | /* |
1250 | | * The list of HTML attributes which are of content %Script; |
1251 | | * NOTE: when adding ones, check htmlIsScriptAttribute() since |
1252 | | * it assumes the name starts with 'on' |
1253 | | */ |
1254 | | static const char *const htmlScriptAttributes[] = { |
1255 | | "onclick", |
1256 | | "ondblclick", |
1257 | | "onmousedown", |
1258 | | "onmouseup", |
1259 | | "onmouseover", |
1260 | | "onmousemove", |
1261 | | "onmouseout", |
1262 | | "onkeypress", |
1263 | | "onkeydown", |
1264 | | "onkeyup", |
1265 | | "onload", |
1266 | | "onunload", |
1267 | | "onfocus", |
1268 | | "onblur", |
1269 | | "onsubmit", |
1270 | | "onreset", |
1271 | | "onchange", |
1272 | | "onselect" |
1273 | | }; |
1274 | | |
1275 | | /* |
1276 | | * This table is used by the htmlparser to know what to do with |
1277 | | * broken html pages. By assigning different priorities to different |
1278 | | * elements the parser can decide how to handle extra endtags. |
1279 | | * Endtags are only allowed to close elements with lower or equal |
1280 | | * priority. |
1281 | | */ |
1282 | | |
1283 | | typedef struct { |
1284 | | const char *name; |
1285 | | int priority; |
1286 | | } elementPriority; |
1287 | | |
1288 | | static const elementPriority htmlEndPriority[] = { |
1289 | | {"div", 150}, |
1290 | | {"td", 160}, |
1291 | | {"th", 160}, |
1292 | | {"tr", 170}, |
1293 | | {"thead", 180}, |
1294 | | {"tbody", 180}, |
1295 | | {"tfoot", 180}, |
1296 | | {"table", 190}, |
1297 | | {"head", 200}, |
1298 | | {"body", 200}, |
1299 | | {"html", 220}, |
1300 | | {NULL, 100} /* Default priority */ |
1301 | | }; |
1302 | | |
1303 | | /************************************************************************ |
1304 | | * * |
1305 | | * functions to handle HTML specific data * |
1306 | | * * |
1307 | | ************************************************************************/ |
1308 | | |
1309 | | /** |
1310 | | * htmlInitAutoClose: |
1311 | | * |
1312 | | * DEPRECATED: This is a no-op. |
1313 | | */ |
1314 | | void |
1315 | 0 | htmlInitAutoClose(void) { |
1316 | 0 | } |
1317 | | |
1318 | | static int |
1319 | 0 | htmlCompareTags(const void *key, const void *member) { |
1320 | 0 | const xmlChar *tag = (const xmlChar *) key; |
1321 | 0 | const htmlElemDesc *desc = (const htmlElemDesc *) member; |
1322 | |
|
1323 | 0 | return(xmlStrcasecmp(tag, BAD_CAST desc->name)); |
1324 | 0 | } |
1325 | | |
1326 | | /** |
1327 | | * htmlTagLookup: |
1328 | | * @tag: The tag name in lowercase |
1329 | | * |
1330 | | * Lookup the HTML tag in the ElementTable |
1331 | | * |
1332 | | * Returns the related htmlElemDescPtr or NULL if not found. |
1333 | | */ |
1334 | | const htmlElemDesc * |
1335 | 0 | htmlTagLookup(const xmlChar *tag) { |
1336 | 0 | if (tag == NULL) |
1337 | 0 | return(NULL); |
1338 | | |
1339 | 0 | return((const htmlElemDesc *) bsearch(tag, html40ElementTable, |
1340 | 0 | sizeof(html40ElementTable) / sizeof(htmlElemDesc), |
1341 | 0 | sizeof(htmlElemDesc), htmlCompareTags)); |
1342 | 0 | } |
1343 | | |
1344 | | /** |
1345 | | * htmlGetEndPriority: |
1346 | | * @name: The name of the element to look up the priority for. |
1347 | | * |
1348 | | * Return value: The "endtag" priority. |
1349 | | **/ |
1350 | | static int |
1351 | 0 | htmlGetEndPriority (const xmlChar *name) { |
1352 | 0 | int i = 0; |
1353 | |
|
1354 | 0 | while ((htmlEndPriority[i].name != NULL) && |
1355 | 0 | (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name))) |
1356 | 0 | i++; |
1357 | |
|
1358 | 0 | return(htmlEndPriority[i].priority); |
1359 | 0 | } |
1360 | | |
1361 | | |
1362 | | static int |
1363 | 0 | htmlCompareStartClose(const void *vkey, const void *member) { |
1364 | 0 | const htmlStartCloseEntry *key = (const htmlStartCloseEntry *) vkey; |
1365 | 0 | const htmlStartCloseEntry *entry = (const htmlStartCloseEntry *) member; |
1366 | 0 | int ret; |
1367 | |
|
1368 | 0 | ret = strcmp(key->oldTag, entry->oldTag); |
1369 | 0 | if (ret == 0) |
1370 | 0 | ret = strcmp(key->newTag, entry->newTag); |
1371 | |
|
1372 | 0 | return(ret); |
1373 | 0 | } |
1374 | | |
1375 | | /** |
1376 | | * htmlCheckAutoClose: |
1377 | | * @newtag: The new tag name |
1378 | | * @oldtag: The old tag name |
1379 | | * |
1380 | | * Checks whether the new tag is one of the registered valid tags for |
1381 | | * closing old. |
1382 | | * |
1383 | | * Returns 0 if no, 1 if yes. |
1384 | | */ |
1385 | | static int |
1386 | | htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag) |
1387 | 0 | { |
1388 | 0 | htmlStartCloseEntry key; |
1389 | 0 | void *res; |
1390 | |
|
1391 | 0 | key.oldTag = (const char *) oldtag; |
1392 | 0 | key.newTag = (const char *) newtag; |
1393 | 0 | res = bsearch(&key, htmlStartClose, |
1394 | 0 | sizeof(htmlStartClose) / sizeof(htmlStartCloseEntry), |
1395 | 0 | sizeof(htmlStartCloseEntry), htmlCompareStartClose); |
1396 | 0 | return(res != NULL); |
1397 | 0 | } |
1398 | | |
1399 | | /** |
1400 | | * htmlAutoCloseOnClose: |
1401 | | * @ctxt: an HTML parser context |
1402 | | * @newtag: The new tag name |
1403 | | * @force: force the tag closure |
1404 | | * |
1405 | | * The HTML DTD allows an ending tag to implicitly close other tags. |
1406 | | */ |
1407 | | static void |
1408 | | htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
1409 | 0 | { |
1410 | 0 | const htmlElemDesc *info; |
1411 | 0 | int i, priority; |
1412 | |
|
1413 | 0 | priority = htmlGetEndPriority(newtag); |
1414 | |
|
1415 | 0 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
1416 | |
|
1417 | 0 | if (xmlStrEqual(newtag, ctxt->nameTab[i])) |
1418 | 0 | break; |
1419 | | /* |
1420 | | * A misplaced endtag can only close elements with lower |
1421 | | * or equal priority, so if we find an element with higher |
1422 | | * priority before we find an element with |
1423 | | * matching name, we just ignore this endtag |
1424 | | */ |
1425 | 0 | if (htmlGetEndPriority(ctxt->nameTab[i]) > priority) |
1426 | 0 | return; |
1427 | 0 | } |
1428 | 0 | if (i < 0) |
1429 | 0 | return; |
1430 | | |
1431 | 0 | while (!xmlStrEqual(newtag, ctxt->name)) { |
1432 | 0 | info = htmlTagLookup(ctxt->name); |
1433 | 0 | if ((info != NULL) && (info->endTag == 3)) { |
1434 | 0 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
1435 | 0 | "Opening and ending tag mismatch: %s and %s\n", |
1436 | 0 | newtag, ctxt->name); |
1437 | 0 | } |
1438 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
1439 | 0 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
1440 | 0 | htmlnamePop(ctxt); |
1441 | 0 | } |
1442 | 0 | } |
1443 | | |
1444 | | /** |
1445 | | * htmlAutoCloseOnEnd: |
1446 | | * @ctxt: an HTML parser context |
1447 | | * |
1448 | | * Close all remaining tags at the end of the stream |
1449 | | */ |
1450 | | static void |
1451 | | htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt) |
1452 | 0 | { |
1453 | 0 | int i; |
1454 | |
|
1455 | 0 | if (ctxt->nameNr == 0) |
1456 | 0 | return; |
1457 | 0 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
1458 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
1459 | 0 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
1460 | 0 | htmlnamePop(ctxt); |
1461 | 0 | } |
1462 | 0 | } |
1463 | | |
1464 | | /** |
1465 | | * htmlAutoClose: |
1466 | | * @ctxt: an HTML parser context |
1467 | | * @newtag: The new tag name or NULL |
1468 | | * |
1469 | | * The HTML DTD allows a tag to implicitly close other tags. |
1470 | | * The list is kept in htmlStartClose array. This function is |
1471 | | * called when a new tag has been detected and generates the |
1472 | | * appropriates closes if possible/needed. |
1473 | | * If newtag is NULL this mean we are at the end of the resource |
1474 | | * and we should check |
1475 | | */ |
1476 | | static void |
1477 | | htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
1478 | 0 | { |
1479 | 0 | if (newtag == NULL) |
1480 | 0 | return; |
1481 | | |
1482 | 0 | while ((ctxt->name != NULL) && |
1483 | 0 | (htmlCheckAutoClose(newtag, ctxt->name))) { |
1484 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
1485 | 0 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
1486 | 0 | htmlnamePop(ctxt); |
1487 | 0 | } |
1488 | 0 | } |
1489 | | |
1490 | | /** |
1491 | | * htmlAutoCloseTag: |
1492 | | * @doc: the HTML document |
1493 | | * @name: The tag name |
1494 | | * @elem: the HTML element |
1495 | | * |
1496 | | * The HTML DTD allows a tag to implicitly close other tags. |
1497 | | * The list is kept in htmlStartClose array. This function checks |
1498 | | * if the element or one of it's children would autoclose the |
1499 | | * given tag. |
1500 | | * |
1501 | | * Returns 1 if autoclose, 0 otherwise |
1502 | | */ |
1503 | | int |
1504 | 0 | htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) { |
1505 | 0 | htmlNodePtr child; |
1506 | |
|
1507 | 0 | if (elem == NULL) return(1); |
1508 | 0 | if (xmlStrEqual(name, elem->name)) return(0); |
1509 | 0 | if (htmlCheckAutoClose(elem->name, name)) return(1); |
1510 | 0 | child = elem->children; |
1511 | 0 | while (child != NULL) { |
1512 | 0 | if (htmlAutoCloseTag(doc, name, child)) return(1); |
1513 | 0 | child = child->next; |
1514 | 0 | } |
1515 | 0 | return(0); |
1516 | 0 | } |
1517 | | |
1518 | | /** |
1519 | | * htmlIsAutoClosed: |
1520 | | * @doc: the HTML document |
1521 | | * @elem: the HTML element |
1522 | | * |
1523 | | * The HTML DTD allows a tag to implicitly close other tags. |
1524 | | * The list is kept in htmlStartClose array. This function checks |
1525 | | * if a tag is autoclosed by one of it's child |
1526 | | * |
1527 | | * Returns 1 if autoclosed, 0 otherwise |
1528 | | */ |
1529 | | int |
1530 | 0 | htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) { |
1531 | 0 | htmlNodePtr child; |
1532 | |
|
1533 | 0 | if (elem == NULL) return(1); |
1534 | 0 | child = elem->children; |
1535 | 0 | while (child != NULL) { |
1536 | 0 | if (htmlAutoCloseTag(doc, elem->name, child)) return(1); |
1537 | 0 | child = child->next; |
1538 | 0 | } |
1539 | 0 | return(0); |
1540 | 0 | } |
1541 | | |
1542 | | /** |
1543 | | * htmlCheckImplied: |
1544 | | * @ctxt: an HTML parser context |
1545 | | * @newtag: The new tag name |
1546 | | * |
1547 | | * The HTML DTD allows a tag to exists only implicitly |
1548 | | * called when a new tag has been detected and generates the |
1549 | | * appropriates implicit tags if missing |
1550 | | */ |
1551 | | static void |
1552 | 0 | htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) { |
1553 | 0 | int i; |
1554 | |
|
1555 | 0 | if (ctxt->options & HTML_PARSE_NOIMPLIED) |
1556 | 0 | return; |
1557 | 0 | if (!htmlOmittedDefaultValue) |
1558 | 0 | return; |
1559 | 0 | if (xmlStrEqual(newtag, BAD_CAST"html")) |
1560 | 0 | return; |
1561 | 0 | if (ctxt->nameNr <= 0) { |
1562 | 0 | htmlnamePush(ctxt, BAD_CAST"html"); |
1563 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
1564 | 0 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL); |
1565 | 0 | } |
1566 | 0 | if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head"))) |
1567 | 0 | return; |
1568 | 0 | if ((ctxt->nameNr <= 1) && |
1569 | 0 | ((xmlStrEqual(newtag, BAD_CAST"script")) || |
1570 | 0 | (xmlStrEqual(newtag, BAD_CAST"style")) || |
1571 | 0 | (xmlStrEqual(newtag, BAD_CAST"meta")) || |
1572 | 0 | (xmlStrEqual(newtag, BAD_CAST"link")) || |
1573 | 0 | (xmlStrEqual(newtag, BAD_CAST"title")) || |
1574 | 0 | (xmlStrEqual(newtag, BAD_CAST"base")))) { |
1575 | 0 | if (ctxt->html >= 3) { |
1576 | | /* we already saw or generated an <head> before */ |
1577 | 0 | return; |
1578 | 0 | } |
1579 | | /* |
1580 | | * dropped OBJECT ... i you put it first BODY will be |
1581 | | * assumed ! |
1582 | | */ |
1583 | 0 | htmlnamePush(ctxt, BAD_CAST"head"); |
1584 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
1585 | 0 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL); |
1586 | 0 | } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) && |
1587 | 0 | (!xmlStrEqual(newtag, BAD_CAST"frame")) && |
1588 | 0 | (!xmlStrEqual(newtag, BAD_CAST"frameset"))) { |
1589 | 0 | if (ctxt->html >= 10) { |
1590 | | /* we already saw or generated a <body> before */ |
1591 | 0 | return; |
1592 | 0 | } |
1593 | 0 | for (i = 0;i < ctxt->nameNr;i++) { |
1594 | 0 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) { |
1595 | 0 | return; |
1596 | 0 | } |
1597 | 0 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) { |
1598 | 0 | return; |
1599 | 0 | } |
1600 | 0 | } |
1601 | | |
1602 | 0 | htmlnamePush(ctxt, BAD_CAST"body"); |
1603 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
1604 | 0 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL); |
1605 | 0 | } |
1606 | 0 | } |
1607 | | |
1608 | | /** |
1609 | | * htmlCheckParagraph |
1610 | | * @ctxt: an HTML parser context |
1611 | | * |
1612 | | * Check whether a p element need to be implied before inserting |
1613 | | * characters in the current element. |
1614 | | * |
1615 | | * Returns 1 if a paragraph has been inserted, 0 if not and -1 |
1616 | | * in case of error. |
1617 | | */ |
1618 | | |
1619 | | static int |
1620 | 0 | htmlCheckParagraph(htmlParserCtxtPtr ctxt) { |
1621 | 0 | const xmlChar *tag; |
1622 | 0 | int i; |
1623 | |
|
1624 | 0 | if (ctxt == NULL) |
1625 | 0 | return(-1); |
1626 | 0 | tag = ctxt->name; |
1627 | 0 | if (tag == NULL) { |
1628 | 0 | htmlAutoClose(ctxt, BAD_CAST"p"); |
1629 | 0 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
1630 | 0 | htmlnamePush(ctxt, BAD_CAST"p"); |
1631 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
1632 | 0 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
1633 | 0 | return(1); |
1634 | 0 | } |
1635 | 0 | if (!htmlOmittedDefaultValue) |
1636 | 0 | return(0); |
1637 | 0 | for (i = 0; htmlNoContentElements[i] != NULL; i++) { |
1638 | 0 | if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) { |
1639 | 0 | htmlAutoClose(ctxt, BAD_CAST"p"); |
1640 | 0 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
1641 | 0 | htmlnamePush(ctxt, BAD_CAST"p"); |
1642 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
1643 | 0 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
1644 | 0 | return(1); |
1645 | 0 | } |
1646 | 0 | } |
1647 | 0 | return(0); |
1648 | 0 | } |
1649 | | |
1650 | | /** |
1651 | | * htmlIsScriptAttribute: |
1652 | | * @name: an attribute name |
1653 | | * |
1654 | | * Check if an attribute is of content type Script |
1655 | | * |
1656 | | * Returns 1 is the attribute is a script 0 otherwise |
1657 | | */ |
1658 | | int |
1659 | 0 | htmlIsScriptAttribute(const xmlChar *name) { |
1660 | 0 | unsigned int i; |
1661 | |
|
1662 | 0 | if (name == NULL) |
1663 | 0 | return(0); |
1664 | | /* |
1665 | | * all script attributes start with 'on' |
1666 | | */ |
1667 | 0 | if ((name[0] != 'o') || (name[1] != 'n')) |
1668 | 0 | return(0); |
1669 | 0 | for (i = 0; |
1670 | 0 | i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]); |
1671 | 0 | i++) { |
1672 | 0 | if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i])) |
1673 | 0 | return(1); |
1674 | 0 | } |
1675 | 0 | return(0); |
1676 | 0 | } |
1677 | | |
1678 | | /************************************************************************ |
1679 | | * * |
1680 | | * The list of HTML predefined entities * |
1681 | | * * |
1682 | | ************************************************************************/ |
1683 | | |
1684 | | |
1685 | | static const htmlEntityDesc html40EntitiesTable[] = { |
1686 | | /* |
1687 | | * the 4 absolute ones, plus apostrophe. |
1688 | | */ |
1689 | | { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" }, |
1690 | | { 38, "amp", "ampersand, U+0026 ISOnum" }, |
1691 | | { 39, "apos", "single quote" }, |
1692 | | { 60, "lt", "less-than sign, U+003C ISOnum" }, |
1693 | | { 62, "gt", "greater-than sign, U+003E ISOnum" }, |
1694 | | |
1695 | | /* |
1696 | | * A bunch still in the 128-255 range |
1697 | | * Replacing them depend really on the charset used. |
1698 | | */ |
1699 | | { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" }, |
1700 | | { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" }, |
1701 | | { 162, "cent", "cent sign, U+00A2 ISOnum" }, |
1702 | | { 163, "pound","pound sign, U+00A3 ISOnum" }, |
1703 | | { 164, "curren","currency sign, U+00A4 ISOnum" }, |
1704 | | { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" }, |
1705 | | { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" }, |
1706 | | { 167, "sect", "section sign, U+00A7 ISOnum" }, |
1707 | | { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" }, |
1708 | | { 169, "copy", "copyright sign, U+00A9 ISOnum" }, |
1709 | | { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" }, |
1710 | | { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" }, |
1711 | | { 172, "not", "not sign, U+00AC ISOnum" }, |
1712 | | { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" }, |
1713 | | { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" }, |
1714 | | { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" }, |
1715 | | { 176, "deg", "degree sign, U+00B0 ISOnum" }, |
1716 | | { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" }, |
1717 | | { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" }, |
1718 | | { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" }, |
1719 | | { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" }, |
1720 | | { 181, "micro","micro sign, U+00B5 ISOnum" }, |
1721 | | { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" }, |
1722 | | { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" }, |
1723 | | { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" }, |
1724 | | { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" }, |
1725 | | { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" }, |
1726 | | { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" }, |
1727 | | { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" }, |
1728 | | { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" }, |
1729 | | { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" }, |
1730 | | { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" }, |
1731 | | { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" }, |
1732 | | { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" }, |
1733 | | { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" }, |
1734 | | { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" }, |
1735 | | { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" }, |
1736 | | { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" }, |
1737 | | { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" }, |
1738 | | { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" }, |
1739 | | { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" }, |
1740 | | { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" }, |
1741 | | { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" }, |
1742 | | { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" }, |
1743 | | { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" }, |
1744 | | { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" }, |
1745 | | { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" }, |
1746 | | { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" }, |
1747 | | { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" }, |
1748 | | { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" }, |
1749 | | { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" }, |
1750 | | { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" }, |
1751 | | { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" }, |
1752 | | { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" }, |
1753 | | { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" }, |
1754 | | { 215, "times","multiplication sign, U+00D7 ISOnum" }, |
1755 | | { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" }, |
1756 | | { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" }, |
1757 | | { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" }, |
1758 | | { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" }, |
1759 | | { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" }, |
1760 | | { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" }, |
1761 | | { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" }, |
1762 | | { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" }, |
1763 | | { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" }, |
1764 | | { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" }, |
1765 | | { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" }, |
1766 | | { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" }, |
1767 | | { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" }, |
1768 | | { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" }, |
1769 | | { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" }, |
1770 | | { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" }, |
1771 | | { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" }, |
1772 | | { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" }, |
1773 | | { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" }, |
1774 | | { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" }, |
1775 | | { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" }, |
1776 | | { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" }, |
1777 | | { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" }, |
1778 | | { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" }, |
1779 | | { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" }, |
1780 | | { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" }, |
1781 | | { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" }, |
1782 | | { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" }, |
1783 | | { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" }, |
1784 | | { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" }, |
1785 | | { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" }, |
1786 | | { 247, "divide","division sign, U+00F7 ISOnum" }, |
1787 | | { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" }, |
1788 | | { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" }, |
1789 | | { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" }, |
1790 | | { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" }, |
1791 | | { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" }, |
1792 | | { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" }, |
1793 | | { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" }, |
1794 | | { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" }, |
1795 | | |
1796 | | { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" }, |
1797 | | { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" }, |
1798 | | { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" }, |
1799 | | { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" }, |
1800 | | { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" }, |
1801 | | |
1802 | | /* |
1803 | | * Anything below should really be kept as entities references |
1804 | | */ |
1805 | | { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" }, |
1806 | | |
1807 | | { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" }, |
1808 | | { 732, "tilde","small tilde, U+02DC ISOdia" }, |
1809 | | |
1810 | | { 913, "Alpha","greek capital letter alpha, U+0391" }, |
1811 | | { 914, "Beta", "greek capital letter beta, U+0392" }, |
1812 | | { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" }, |
1813 | | { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" }, |
1814 | | { 917, "Epsilon","greek capital letter epsilon, U+0395" }, |
1815 | | { 918, "Zeta", "greek capital letter zeta, U+0396" }, |
1816 | | { 919, "Eta", "greek capital letter eta, U+0397" }, |
1817 | | { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" }, |
1818 | | { 921, "Iota", "greek capital letter iota, U+0399" }, |
1819 | | { 922, "Kappa","greek capital letter kappa, U+039A" }, |
1820 | | { 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" }, |
1821 | | { 924, "Mu", "greek capital letter mu, U+039C" }, |
1822 | | { 925, "Nu", "greek capital letter nu, U+039D" }, |
1823 | | { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" }, |
1824 | | { 927, "Omicron","greek capital letter omicron, U+039F" }, |
1825 | | { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" }, |
1826 | | { 929, "Rho", "greek capital letter rho, U+03A1" }, |
1827 | | { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" }, |
1828 | | { 932, "Tau", "greek capital letter tau, U+03A4" }, |
1829 | | { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" }, |
1830 | | { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" }, |
1831 | | { 935, "Chi", "greek capital letter chi, U+03A7" }, |
1832 | | { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" }, |
1833 | | { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" }, |
1834 | | |
1835 | | { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" }, |
1836 | | { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" }, |
1837 | | { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" }, |
1838 | | { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" }, |
1839 | | { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" }, |
1840 | | { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" }, |
1841 | | { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" }, |
1842 | | { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" }, |
1843 | | { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" }, |
1844 | | { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" }, |
1845 | | { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" }, |
1846 | | { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" }, |
1847 | | { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" }, |
1848 | | { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" }, |
1849 | | { 959, "omicron","greek small letter omicron, U+03BF NEW" }, |
1850 | | { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" }, |
1851 | | { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" }, |
1852 | | { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" }, |
1853 | | { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" }, |
1854 | | { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" }, |
1855 | | { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" }, |
1856 | | { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" }, |
1857 | | { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" }, |
1858 | | { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" }, |
1859 | | { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" }, |
1860 | | { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" }, |
1861 | | { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" }, |
1862 | | { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" }, |
1863 | | |
1864 | | { 8194, "ensp", "en space, U+2002 ISOpub" }, |
1865 | | { 8195, "emsp", "em space, U+2003 ISOpub" }, |
1866 | | { 8201, "thinsp","thin space, U+2009 ISOpub" }, |
1867 | | { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" }, |
1868 | | { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" }, |
1869 | | { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" }, |
1870 | | { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" }, |
1871 | | { 8211, "ndash","en dash, U+2013 ISOpub" }, |
1872 | | { 8212, "mdash","em dash, U+2014 ISOpub" }, |
1873 | | { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" }, |
1874 | | { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" }, |
1875 | | { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" }, |
1876 | | { 8220, "ldquo","left double quotation mark, U+201C ISOnum" }, |
1877 | | { 8221, "rdquo","right double quotation mark, U+201D ISOnum" }, |
1878 | | { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" }, |
1879 | | { 8224, "dagger","dagger, U+2020 ISOpub" }, |
1880 | | { 8225, "Dagger","double dagger, U+2021 ISOpub" }, |
1881 | | |
1882 | | { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" }, |
1883 | | { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" }, |
1884 | | |
1885 | | { 8240, "permil","per mille sign, U+2030 ISOtech" }, |
1886 | | |
1887 | | { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" }, |
1888 | | { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" }, |
1889 | | |
1890 | | { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" }, |
1891 | | { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" }, |
1892 | | |
1893 | | { 8254, "oline","overline = spacing overscore, U+203E NEW" }, |
1894 | | { 8260, "frasl","fraction slash, U+2044 NEW" }, |
1895 | | |
1896 | | { 8364, "euro", "euro sign, U+20AC NEW" }, |
1897 | | |
1898 | | { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" }, |
1899 | | { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" }, |
1900 | | { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" }, |
1901 | | { 8482, "trade","trade mark sign, U+2122 ISOnum" }, |
1902 | | { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" }, |
1903 | | { 8592, "larr", "leftwards arrow, U+2190 ISOnum" }, |
1904 | | { 8593, "uarr", "upwards arrow, U+2191 ISOnum" }, |
1905 | | { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" }, |
1906 | | { 8595, "darr", "downwards arrow, U+2193 ISOnum" }, |
1907 | | { 8596, "harr", "left right arrow, U+2194 ISOamsa" }, |
1908 | | { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" }, |
1909 | | { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" }, |
1910 | | { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" }, |
1911 | | { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" }, |
1912 | | { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" }, |
1913 | | { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" }, |
1914 | | |
1915 | | { 8704, "forall","for all, U+2200 ISOtech" }, |
1916 | | { 8706, "part", "partial differential, U+2202 ISOtech" }, |
1917 | | { 8707, "exist","there exists, U+2203 ISOtech" }, |
1918 | | { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" }, |
1919 | | { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" }, |
1920 | | { 8712, "isin", "element of, U+2208 ISOtech" }, |
1921 | | { 8713, "notin","not an element of, U+2209 ISOtech" }, |
1922 | | { 8715, "ni", "contains as member, U+220B ISOtech" }, |
1923 | | { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" }, |
1924 | | { 8721, "sum", "n-ary summation, U+2211 ISOamsb" }, |
1925 | | { 8722, "minus","minus sign, U+2212 ISOtech" }, |
1926 | | { 8727, "lowast","asterisk operator, U+2217 ISOtech" }, |
1927 | | { 8730, "radic","square root = radical sign, U+221A ISOtech" }, |
1928 | | { 8733, "prop", "proportional to, U+221D ISOtech" }, |
1929 | | { 8734, "infin","infinity, U+221E ISOtech" }, |
1930 | | { 8736, "ang", "angle, U+2220 ISOamso" }, |
1931 | | { 8743, "and", "logical and = wedge, U+2227 ISOtech" }, |
1932 | | { 8744, "or", "logical or = vee, U+2228 ISOtech" }, |
1933 | | { 8745, "cap", "intersection = cap, U+2229 ISOtech" }, |
1934 | | { 8746, "cup", "union = cup, U+222A ISOtech" }, |
1935 | | { 8747, "int", "integral, U+222B ISOtech" }, |
1936 | | { 8756, "there4","therefore, U+2234 ISOtech" }, |
1937 | | { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" }, |
1938 | | { 8773, "cong", "approximately equal to, U+2245 ISOtech" }, |
1939 | | { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" }, |
1940 | | { 8800, "ne", "not equal to, U+2260 ISOtech" }, |
1941 | | { 8801, "equiv","identical to, U+2261 ISOtech" }, |
1942 | | { 8804, "le", "less-than or equal to, U+2264 ISOtech" }, |
1943 | | { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" }, |
1944 | | { 8834, "sub", "subset of, U+2282 ISOtech" }, |
1945 | | { 8835, "sup", "superset of, U+2283 ISOtech" }, |
1946 | | { 8836, "nsub", "not a subset of, U+2284 ISOamsn" }, |
1947 | | { 8838, "sube", "subset of or equal to, U+2286 ISOtech" }, |
1948 | | { 8839, "supe", "superset of or equal to, U+2287 ISOtech" }, |
1949 | | { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" }, |
1950 | | { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" }, |
1951 | | { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" }, |
1952 | | { 8901, "sdot", "dot operator, U+22C5 ISOamsb" }, |
1953 | | { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" }, |
1954 | | { 8969, "rceil","right ceiling, U+2309 ISOamsc" }, |
1955 | | { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" }, |
1956 | | { 8971, "rfloor","right floor, U+230B ISOamsc" }, |
1957 | | { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" }, |
1958 | | { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" }, |
1959 | | { 9674, "loz", "lozenge, U+25CA ISOpub" }, |
1960 | | |
1961 | | { 9824, "spades","black spade suit, U+2660 ISOpub" }, |
1962 | | { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" }, |
1963 | | { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" }, |
1964 | | { 9830, "diams","black diamond suit, U+2666 ISOpub" }, |
1965 | | |
1966 | | }; |
1967 | | |
1968 | | /************************************************************************ |
1969 | | * * |
1970 | | * Commodity functions to handle entities * |
1971 | | * * |
1972 | | ************************************************************************/ |
1973 | | |
1974 | | /* |
1975 | | * Macro used to grow the current buffer. |
1976 | | */ |
1977 | 0 | #define growBuffer(buffer) { \ |
1978 | 0 | xmlChar *tmp; \ |
1979 | 0 | buffer##_size *= 2; \ |
1980 | 0 | tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size); \ |
1981 | 0 | if (tmp == NULL) { \ |
1982 | 0 | htmlErrMemory(ctxt); \ |
1983 | 0 | xmlFree(buffer); \ |
1984 | 0 | return(NULL); \ |
1985 | 0 | } \ |
1986 | 0 | buffer = tmp; \ |
1987 | 0 | } |
1988 | | |
1989 | | /** |
1990 | | * htmlEntityLookup: |
1991 | | * @name: the entity name |
1992 | | * |
1993 | | * Lookup the given entity in EntitiesTable |
1994 | | * |
1995 | | * TODO: the linear scan is really ugly, an hash table is really needed. |
1996 | | * |
1997 | | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
1998 | | */ |
1999 | | const htmlEntityDesc * |
2000 | 0 | htmlEntityLookup(const xmlChar *name) { |
2001 | 0 | unsigned int i; |
2002 | |
|
2003 | 0 | for (i = 0;i < (sizeof(html40EntitiesTable)/ |
2004 | 0 | sizeof(html40EntitiesTable[0]));i++) { |
2005 | 0 | if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) { |
2006 | 0 | return((htmlEntityDescPtr) &html40EntitiesTable[i]); |
2007 | 0 | } |
2008 | 0 | } |
2009 | 0 | return(NULL); |
2010 | 0 | } |
2011 | | |
2012 | | static int |
2013 | 0 | htmlCompareEntityDesc(const void *vkey, const void *vdesc) { |
2014 | 0 | const unsigned *key = vkey; |
2015 | 0 | const htmlEntityDesc *desc = vdesc; |
2016 | |
|
2017 | 0 | return((int) *key - (int) desc->value); |
2018 | 0 | } |
2019 | | |
2020 | | /** |
2021 | | * htmlEntityValueLookup: |
2022 | | * @value: the entity's unicode value |
2023 | | * |
2024 | | * Lookup the given entity in EntitiesTable |
2025 | | * |
2026 | | * TODO: the linear scan is really ugly, an hash table is really needed. |
2027 | | * |
2028 | | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
2029 | | */ |
2030 | | const htmlEntityDesc * |
2031 | 0 | htmlEntityValueLookup(unsigned int value) { |
2032 | 0 | const htmlEntityDesc *desc; |
2033 | 0 | size_t nmemb; |
2034 | |
|
2035 | 0 | nmemb = sizeof(html40EntitiesTable) / sizeof(html40EntitiesTable[0]); |
2036 | 0 | desc = bsearch(&value, html40EntitiesTable, nmemb, sizeof(htmlEntityDesc), |
2037 | 0 | htmlCompareEntityDesc); |
2038 | |
|
2039 | 0 | return(desc); |
2040 | 0 | } |
2041 | | |
2042 | | /** |
2043 | | * UTF8ToHtml: |
2044 | | * @out: a pointer to an array of bytes to store the result |
2045 | | * @outlen: the length of @out |
2046 | | * @in: a pointer to an array of UTF-8 chars |
2047 | | * @inlen: the length of @in |
2048 | | * |
2049 | | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
2050 | | * plus HTML entities block of chars out. |
2051 | | * |
2052 | | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
2053 | | * The value of @inlen after return is the number of octets consumed |
2054 | | * as the return value is positive, else unpredictable. |
2055 | | * The value of @outlen after return is the number of octets consumed. |
2056 | | */ |
2057 | | int |
2058 | | UTF8ToHtml(unsigned char* out, int *outlen, |
2059 | 0 | const unsigned char* in, int *inlen) { |
2060 | 0 | const unsigned char* instart = in; |
2061 | 0 | const unsigned char* inend; |
2062 | 0 | unsigned char* outstart = out; |
2063 | 0 | unsigned char* outend; |
2064 | 0 | int ret = XML_ENC_ERR_SPACE; |
2065 | |
|
2066 | 0 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) |
2067 | 0 | return(XML_ENC_ERR_INTERNAL); |
2068 | | |
2069 | 0 | if (in == NULL) { |
2070 | | /* |
2071 | | * initialization nothing to do |
2072 | | */ |
2073 | 0 | *outlen = 0; |
2074 | 0 | *inlen = 0; |
2075 | 0 | return(XML_ENC_ERR_SUCCESS); |
2076 | 0 | } |
2077 | | |
2078 | 0 | inend = in + *inlen; |
2079 | 0 | outend = out + *outlen; |
2080 | 0 | while (in < inend) { |
2081 | 0 | const htmlEntityDesc *ent; |
2082 | 0 | const char *cp; |
2083 | 0 | char nbuf[16]; |
2084 | 0 | unsigned c, d; |
2085 | 0 | int seqlen, len, i; |
2086 | |
|
2087 | 0 | d = *in; |
2088 | |
|
2089 | 0 | if (d < 0x80) { |
2090 | 0 | if (out >= outend) |
2091 | 0 | goto done; |
2092 | 0 | *out++ = d; |
2093 | 0 | in += 1; |
2094 | 0 | continue; |
2095 | 0 | } |
2096 | | |
2097 | 0 | if (d < 0xE0) { c = d & 0x1F; seqlen = 2; } |
2098 | 0 | else if (d < 0xF0) { c = d & 0x0F; seqlen = 3; } |
2099 | 0 | else { c = d & 0x07; seqlen = 4; } |
2100 | |
|
2101 | 0 | if (inend - in < seqlen) |
2102 | 0 | break; |
2103 | | |
2104 | 0 | for (i = 1; i < seqlen; i++) { |
2105 | 0 | d = in[i]; |
2106 | 0 | c <<= 6; |
2107 | 0 | c |= d & 0x3F; |
2108 | 0 | } |
2109 | | |
2110 | | /* |
2111 | | * Try to lookup a predefined HTML entity for it |
2112 | | */ |
2113 | 0 | ent = htmlEntityValueLookup(c); |
2114 | |
|
2115 | 0 | if (ent == NULL) { |
2116 | 0 | snprintf(nbuf, sizeof(nbuf), "#%u", c); |
2117 | 0 | cp = nbuf; |
2118 | 0 | } else { |
2119 | 0 | cp = ent->name; |
2120 | 0 | } |
2121 | |
|
2122 | 0 | len = strlen(cp); |
2123 | 0 | if (outend - out < len + 2) |
2124 | 0 | goto done; |
2125 | | |
2126 | 0 | *out++ = '&'; |
2127 | 0 | memcpy(out, cp, len); |
2128 | 0 | out += len; |
2129 | 0 | *out++ = ';'; |
2130 | |
|
2131 | 0 | in += seqlen; |
2132 | 0 | } |
2133 | | |
2134 | 0 | ret = out - outstart; |
2135 | |
|
2136 | 0 | done: |
2137 | 0 | *outlen = out - outstart; |
2138 | 0 | *inlen = in - instart; |
2139 | 0 | return(ret); |
2140 | 0 | } |
2141 | | |
2142 | | /** |
2143 | | * htmlEncodeEntities: |
2144 | | * @out: a pointer to an array of bytes to store the result |
2145 | | * @outlen: the length of @out |
2146 | | * @in: a pointer to an array of UTF-8 chars |
2147 | | * @inlen: the length of @in |
2148 | | * @quoteChar: the quote character to escape (' or ") or zero. |
2149 | | * |
2150 | | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
2151 | | * plus HTML entities block of chars out. |
2152 | | * |
2153 | | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
2154 | | * The value of @inlen after return is the number of octets consumed |
2155 | | * as the return value is positive, else unpredictable. |
2156 | | * The value of @outlen after return is the number of octets consumed. |
2157 | | */ |
2158 | | int |
2159 | | htmlEncodeEntities(unsigned char* out, int *outlen, |
2160 | 0 | const unsigned char* in, int *inlen, int quoteChar) { |
2161 | 0 | const unsigned char* processed = in; |
2162 | 0 | const unsigned char* outend; |
2163 | 0 | const unsigned char* outstart = out; |
2164 | 0 | const unsigned char* instart = in; |
2165 | 0 | const unsigned char* inend; |
2166 | 0 | unsigned int c, d; |
2167 | 0 | int trailing; |
2168 | |
|
2169 | 0 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) |
2170 | 0 | return(-1); |
2171 | 0 | outend = out + (*outlen); |
2172 | 0 | inend = in + (*inlen); |
2173 | 0 | while (in < inend) { |
2174 | 0 | d = *in++; |
2175 | 0 | if (d < 0x80) { c= d; trailing= 0; } |
2176 | 0 | else if (d < 0xC0) { |
2177 | | /* trailing byte in leading position */ |
2178 | 0 | *outlen = out - outstart; |
2179 | 0 | *inlen = processed - instart; |
2180 | 0 | return(-2); |
2181 | 0 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
2182 | 0 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
2183 | 0 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
2184 | 0 | else { |
2185 | | /* no chance for this in Ascii */ |
2186 | 0 | *outlen = out - outstart; |
2187 | 0 | *inlen = processed - instart; |
2188 | 0 | return(-2); |
2189 | 0 | } |
2190 | | |
2191 | 0 | if (inend - in < trailing) |
2192 | 0 | break; |
2193 | | |
2194 | 0 | while (trailing--) { |
2195 | 0 | if (((d= *in++) & 0xC0) != 0x80) { |
2196 | 0 | *outlen = out - outstart; |
2197 | 0 | *inlen = processed - instart; |
2198 | 0 | return(-2); |
2199 | 0 | } |
2200 | 0 | c <<= 6; |
2201 | 0 | c |= d & 0x3F; |
2202 | 0 | } |
2203 | | |
2204 | | /* assertion: c is a single UTF-4 value */ |
2205 | 0 | if ((c < 0x80) && (c != (unsigned int) quoteChar) && |
2206 | 0 | (c != '&') && (c != '<') && (c != '>')) { |
2207 | 0 | if (out >= outend) |
2208 | 0 | break; |
2209 | 0 | *out++ = c; |
2210 | 0 | } else { |
2211 | 0 | const htmlEntityDesc * ent; |
2212 | 0 | const char *cp; |
2213 | 0 | char nbuf[16]; |
2214 | 0 | int len; |
2215 | | |
2216 | | /* |
2217 | | * Try to lookup a predefined HTML entity for it |
2218 | | */ |
2219 | 0 | ent = htmlEntityValueLookup(c); |
2220 | 0 | if (ent == NULL) { |
2221 | 0 | snprintf(nbuf, sizeof(nbuf), "#%u", c); |
2222 | 0 | cp = nbuf; |
2223 | 0 | } |
2224 | 0 | else |
2225 | 0 | cp = ent->name; |
2226 | 0 | len = strlen(cp); |
2227 | 0 | if (outend - out < len + 2) |
2228 | 0 | break; |
2229 | 0 | *out++ = '&'; |
2230 | 0 | memcpy(out, cp, len); |
2231 | 0 | out += len; |
2232 | 0 | *out++ = ';'; |
2233 | 0 | } |
2234 | 0 | processed = in; |
2235 | 0 | } |
2236 | 0 | *outlen = out - outstart; |
2237 | 0 | *inlen = processed - instart; |
2238 | 0 | return(0); |
2239 | 0 | } |
2240 | | |
2241 | | /************************************************************************ |
2242 | | * * |
2243 | | * Commodity functions, cleanup needed ? * |
2244 | | * * |
2245 | | ************************************************************************/ |
2246 | | /* |
2247 | | * all tags allowing pc data from the html 4.01 loose dtd |
2248 | | * NOTE: it might be more appropriate to integrate this information |
2249 | | * into the html40ElementTable array but I don't want to risk any |
2250 | | * binary incompatibility |
2251 | | */ |
2252 | | static const char *allowPCData[] = { |
2253 | | "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big", |
2254 | | "blockquote", "body", "button", "caption", "center", "cite", "code", |
2255 | | "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2", |
2256 | | "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend", |
2257 | | "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp", |
2258 | | "small", "span", "strike", "strong", "td", "th", "tt", "u", "var" |
2259 | | }; |
2260 | | |
2261 | | /** |
2262 | | * areBlanks: |
2263 | | * @ctxt: an HTML parser context |
2264 | | * @str: a xmlChar * |
2265 | | * @len: the size of @str |
2266 | | * |
2267 | | * Is this a sequence of blank chars that one can ignore ? |
2268 | | * |
2269 | | * Returns 1 if ignorable 0 otherwise. |
2270 | | */ |
2271 | | |
2272 | 0 | static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) { |
2273 | 0 | unsigned int i; |
2274 | 0 | int j; |
2275 | 0 | xmlNodePtr lastChild; |
2276 | 0 | xmlDtdPtr dtd; |
2277 | |
|
2278 | 0 | for (j = 0;j < len;j++) |
2279 | 0 | if (!(IS_BLANK_CH(str[j]))) return(0); |
2280 | | |
2281 | 0 | if (CUR == 0) return(1); |
2282 | 0 | if (CUR != '<') return(0); |
2283 | 0 | if (ctxt->name == NULL) |
2284 | 0 | return(1); |
2285 | 0 | if (xmlStrEqual(ctxt->name, BAD_CAST"html")) |
2286 | 0 | return(1); |
2287 | 0 | if (xmlStrEqual(ctxt->name, BAD_CAST"head")) |
2288 | 0 | return(1); |
2289 | | |
2290 | | /* Only strip CDATA children of the body tag for strict HTML DTDs */ |
2291 | 0 | if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) { |
2292 | 0 | dtd = xmlGetIntSubset(ctxt->myDoc); |
2293 | 0 | if (dtd != NULL && dtd->ExternalID != NULL) { |
2294 | 0 | if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") || |
2295 | 0 | !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN")) |
2296 | 0 | return(1); |
2297 | 0 | } |
2298 | 0 | } |
2299 | | |
2300 | 0 | if (ctxt->node == NULL) return(0); |
2301 | 0 | lastChild = xmlGetLastChild(ctxt->node); |
2302 | 0 | while ((lastChild) && (lastChild->type == XML_COMMENT_NODE)) |
2303 | 0 | lastChild = lastChild->prev; |
2304 | 0 | if (lastChild == NULL) { |
2305 | 0 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
2306 | 0 | (ctxt->node->content != NULL)) return(0); |
2307 | | /* keep ws in constructs like ...<b> </b>... |
2308 | | for all tags "b" allowing PCDATA */ |
2309 | 0 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
2310 | 0 | if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) { |
2311 | 0 | return(0); |
2312 | 0 | } |
2313 | 0 | } |
2314 | 0 | } else if (xmlNodeIsText(lastChild)) { |
2315 | 0 | return(0); |
2316 | 0 | } else { |
2317 | | /* keep ws in constructs like <p><b>xy</b> <i>z</i><p> |
2318 | | for all tags "p" allowing PCDATA */ |
2319 | 0 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
2320 | 0 | if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) { |
2321 | 0 | return(0); |
2322 | 0 | } |
2323 | 0 | } |
2324 | 0 | } |
2325 | 0 | return(1); |
2326 | 0 | } |
2327 | | |
2328 | | /** |
2329 | | * htmlNewDocNoDtD: |
2330 | | * @URI: URI for the dtd, or NULL |
2331 | | * @ExternalID: the external ID of the DTD, or NULL |
2332 | | * |
2333 | | * Creates a new HTML document without a DTD node if @URI and @ExternalID |
2334 | | * are NULL |
2335 | | * |
2336 | | * Returns a new document, do not initialize the DTD if not provided |
2337 | | */ |
2338 | | htmlDocPtr |
2339 | 0 | htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) { |
2340 | 0 | xmlDocPtr cur; |
2341 | | |
2342 | | /* |
2343 | | * Allocate a new document and fill the fields. |
2344 | | */ |
2345 | 0 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
2346 | 0 | if (cur == NULL) |
2347 | 0 | return(NULL); |
2348 | 0 | memset(cur, 0, sizeof(xmlDoc)); |
2349 | |
|
2350 | 0 | cur->type = XML_HTML_DOCUMENT_NODE; |
2351 | 0 | cur->version = NULL; |
2352 | 0 | cur->intSubset = NULL; |
2353 | 0 | cur->doc = cur; |
2354 | 0 | cur->name = NULL; |
2355 | 0 | cur->children = NULL; |
2356 | 0 | cur->extSubset = NULL; |
2357 | 0 | cur->oldNs = NULL; |
2358 | 0 | cur->encoding = NULL; |
2359 | 0 | cur->standalone = 1; |
2360 | 0 | cur->compression = 0; |
2361 | 0 | cur->ids = NULL; |
2362 | 0 | cur->refs = NULL; |
2363 | 0 | cur->_private = NULL; |
2364 | 0 | cur->charset = XML_CHAR_ENCODING_UTF8; |
2365 | 0 | cur->properties = XML_DOC_HTML | XML_DOC_USERBUILT; |
2366 | 0 | if ((ExternalID != NULL) || |
2367 | 0 | (URI != NULL)) { |
2368 | 0 | xmlDtdPtr intSubset; |
2369 | |
|
2370 | 0 | intSubset = xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI); |
2371 | 0 | if (intSubset == NULL) { |
2372 | 0 | xmlFree(cur); |
2373 | 0 | return(NULL); |
2374 | 0 | } |
2375 | 0 | } |
2376 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2377 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
2378 | 0 | return(cur); |
2379 | 0 | } |
2380 | | |
2381 | | /** |
2382 | | * htmlNewDoc: |
2383 | | * @URI: URI for the dtd, or NULL |
2384 | | * @ExternalID: the external ID of the DTD, or NULL |
2385 | | * |
2386 | | * Creates a new HTML document |
2387 | | * |
2388 | | * Returns a new document |
2389 | | */ |
2390 | | htmlDocPtr |
2391 | 0 | htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) { |
2392 | 0 | if ((URI == NULL) && (ExternalID == NULL)) |
2393 | 0 | return(htmlNewDocNoDtD( |
2394 | 0 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd", |
2395 | 0 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN")); |
2396 | | |
2397 | 0 | return(htmlNewDocNoDtD(URI, ExternalID)); |
2398 | 0 | } |
2399 | | |
2400 | | |
2401 | | /************************************************************************ |
2402 | | * * |
2403 | | * The parser itself * |
2404 | | * Relates to http://www.w3.org/TR/html40 * |
2405 | | * * |
2406 | | ************************************************************************/ |
2407 | | |
2408 | | /************************************************************************ |
2409 | | * * |
2410 | | * The parser itself * |
2411 | | * * |
2412 | | ************************************************************************/ |
2413 | | |
2414 | | static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt); |
2415 | | |
2416 | | static void |
2417 | 0 | htmlSkipBogusComment(htmlParserCtxtPtr ctxt) { |
2418 | 0 | int c; |
2419 | |
|
2420 | 0 | htmlParseErr(ctxt, XML_HTML_INCORRECTLY_OPENED_COMMENT, |
2421 | 0 | "Incorrectly opened comment\n", NULL, NULL); |
2422 | |
|
2423 | 0 | while (PARSER_STOPPED(ctxt) == 0) { |
2424 | 0 | c = CUR; |
2425 | 0 | if (c == 0) |
2426 | 0 | break; |
2427 | 0 | NEXT; |
2428 | 0 | if (c == '>') |
2429 | 0 | break; |
2430 | 0 | } |
2431 | 0 | } |
2432 | | |
2433 | | /** |
2434 | | * htmlParseHTMLName: |
2435 | | * @ctxt: an HTML parser context |
2436 | | * |
2437 | | * parse an HTML tag or attribute name, note that we convert it to lowercase |
2438 | | * since HTML names are not case-sensitive. |
2439 | | * |
2440 | | * Returns the Tag Name parsed or NULL |
2441 | | */ |
2442 | | |
2443 | | static const xmlChar * |
2444 | 0 | htmlParseHTMLName(htmlParserCtxtPtr ctxt) { |
2445 | 0 | const xmlChar *ret; |
2446 | 0 | int i = 0; |
2447 | 0 | xmlChar loc[HTML_PARSER_BUFFER_SIZE]; |
2448 | |
|
2449 | 0 | if (!IS_ASCII_LETTER(CUR) && (CUR != '_') && |
2450 | 0 | (CUR != ':') && (CUR != '.')) return(NULL); |
2451 | | |
2452 | 0 | while ((i < HTML_PARSER_BUFFER_SIZE) && |
2453 | 0 | ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) || |
2454 | 0 | (CUR == ':') || (CUR == '-') || (CUR == '_') || |
2455 | 0 | (CUR == '.'))) { |
2456 | 0 | if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20; |
2457 | 0 | else loc[i] = CUR; |
2458 | 0 | i++; |
2459 | |
|
2460 | 0 | NEXT; |
2461 | 0 | } |
2462 | |
|
2463 | 0 | ret = xmlDictLookup(ctxt->dict, loc, i); |
2464 | 0 | if (ret == NULL) |
2465 | 0 | htmlErrMemory(ctxt); |
2466 | |
|
2467 | 0 | return(ret); |
2468 | 0 | } |
2469 | | |
2470 | | |
2471 | | /** |
2472 | | * htmlParseHTMLName_nonInvasive: |
2473 | | * @ctxt: an HTML parser context |
2474 | | * |
2475 | | * parse an HTML tag or attribute name, note that we convert it to lowercase |
2476 | | * since HTML names are not case-sensitive, this doesn't consume the data |
2477 | | * from the stream, it's a look-ahead |
2478 | | * |
2479 | | * Returns the Tag Name parsed or NULL |
2480 | | */ |
2481 | | |
2482 | | static const xmlChar * |
2483 | 0 | htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) { |
2484 | 0 | int i = 0; |
2485 | 0 | xmlChar loc[HTML_PARSER_BUFFER_SIZE]; |
2486 | 0 | const xmlChar *ret; |
2487 | |
|
2488 | 0 | if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') && |
2489 | 0 | (NXT(1) != ':')) return(NULL); |
2490 | | |
2491 | 0 | while ((i < HTML_PARSER_BUFFER_SIZE) && |
2492 | 0 | ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) || |
2493 | 0 | (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) { |
2494 | 0 | if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20; |
2495 | 0 | else loc[i] = NXT(1+i); |
2496 | 0 | i++; |
2497 | 0 | } |
2498 | |
|
2499 | 0 | ret = xmlDictLookup(ctxt->dict, loc, i); |
2500 | 0 | if (ret == NULL) |
2501 | 0 | htmlErrMemory(ctxt); |
2502 | |
|
2503 | 0 | return(ret); |
2504 | 0 | } |
2505 | | |
2506 | | |
2507 | | /** |
2508 | | * htmlParseName: |
2509 | | * @ctxt: an HTML parser context |
2510 | | * |
2511 | | * parse an HTML name, this routine is case sensitive. |
2512 | | * |
2513 | | * Returns the Name parsed or NULL |
2514 | | */ |
2515 | | |
2516 | | static const xmlChar * |
2517 | 0 | htmlParseName(htmlParserCtxtPtr ctxt) { |
2518 | 0 | const xmlChar *in; |
2519 | 0 | const xmlChar *ret; |
2520 | 0 | int count = 0; |
2521 | |
|
2522 | 0 | GROW; |
2523 | | |
2524 | | /* |
2525 | | * Accelerator for simple ASCII names |
2526 | | */ |
2527 | 0 | in = ctxt->input->cur; |
2528 | 0 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
2529 | 0 | ((*in >= 0x41) && (*in <= 0x5A)) || |
2530 | 0 | (*in == '_') || (*in == ':')) { |
2531 | 0 | in++; |
2532 | 0 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
2533 | 0 | ((*in >= 0x41) && (*in <= 0x5A)) || |
2534 | 0 | ((*in >= 0x30) && (*in <= 0x39)) || |
2535 | 0 | (*in == '_') || (*in == '-') || |
2536 | 0 | (*in == ':') || (*in == '.')) |
2537 | 0 | in++; |
2538 | |
|
2539 | 0 | if (in == ctxt->input->end) |
2540 | 0 | return(NULL); |
2541 | | |
2542 | 0 | if ((*in > 0) && (*in < 0x80)) { |
2543 | 0 | count = in - ctxt->input->cur; |
2544 | 0 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
2545 | 0 | if (ret == NULL) |
2546 | 0 | htmlErrMemory(ctxt); |
2547 | 0 | ctxt->input->cur = in; |
2548 | 0 | ctxt->input->col += count; |
2549 | 0 | return(ret); |
2550 | 0 | } |
2551 | 0 | } |
2552 | 0 | return(htmlParseNameComplex(ctxt)); |
2553 | 0 | } |
2554 | | |
2555 | | static const xmlChar * |
2556 | 0 | htmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
2557 | 0 | int len = 0, l; |
2558 | 0 | int c; |
2559 | 0 | int maxLength = (ctxt->options & XML_PARSE_HUGE) ? |
2560 | 0 | XML_MAX_TEXT_LENGTH : |
2561 | 0 | XML_MAX_NAME_LENGTH; |
2562 | 0 | const xmlChar *base = ctxt->input->base; |
2563 | 0 | const xmlChar *ret; |
2564 | | |
2565 | | /* |
2566 | | * Handler for more complex cases |
2567 | | */ |
2568 | 0 | c = CUR_CHAR(l); |
2569 | 0 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
2570 | 0 | (!IS_LETTER(c) && (c != '_') && |
2571 | 0 | (c != ':'))) { |
2572 | 0 | return(NULL); |
2573 | 0 | } |
2574 | | |
2575 | 0 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
2576 | 0 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
2577 | 0 | (c == '.') || (c == '-') || |
2578 | 0 | (c == '_') || (c == ':') || |
2579 | 0 | (IS_COMBINING(c)) || |
2580 | 0 | (IS_EXTENDER(c)))) { |
2581 | 0 | len += l; |
2582 | 0 | if (len > maxLength) { |
2583 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_TOO_LONG, "name too long", NULL, NULL); |
2584 | 0 | return(NULL); |
2585 | 0 | } |
2586 | 0 | NEXTL(l); |
2587 | 0 | c = CUR_CHAR(l); |
2588 | 0 | if (ctxt->input->base != base) { |
2589 | | /* |
2590 | | * We changed encoding from an unknown encoding |
2591 | | * Input buffer changed location, so we better start again |
2592 | | */ |
2593 | 0 | return(htmlParseNameComplex(ctxt)); |
2594 | 0 | } |
2595 | 0 | } |
2596 | | |
2597 | 0 | if (ctxt->input->cur - ctxt->input->base < len) { |
2598 | | /* Sanity check */ |
2599 | 0 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
2600 | 0 | "unexpected change of input buffer", NULL, NULL); |
2601 | 0 | return (NULL); |
2602 | 0 | } |
2603 | | |
2604 | 0 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len); |
2605 | 0 | if (ret == NULL) |
2606 | 0 | htmlErrMemory(ctxt); |
2607 | |
|
2608 | 0 | return(ret); |
2609 | 0 | } |
2610 | | |
2611 | | |
2612 | | /** |
2613 | | * htmlParseHTMLAttribute: |
2614 | | * @ctxt: an HTML parser context |
2615 | | * @stop: a char stop value |
2616 | | * |
2617 | | * parse an HTML attribute value till the stop (quote), if |
2618 | | * stop is 0 then it stops at the first space |
2619 | | * |
2620 | | * Returns the attribute parsed or NULL |
2621 | | */ |
2622 | | |
2623 | | static xmlChar * |
2624 | 0 | htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) { |
2625 | 0 | xmlChar *buffer = NULL; |
2626 | 0 | int buffer_size = 0; |
2627 | 0 | int maxLength = (ctxt->options & XML_PARSE_HUGE) ? |
2628 | 0 | XML_MAX_HUGE_LENGTH : |
2629 | 0 | XML_MAX_TEXT_LENGTH; |
2630 | 0 | xmlChar *out = NULL; |
2631 | 0 | const xmlChar *name = NULL; |
2632 | 0 | const xmlChar *cur = NULL; |
2633 | 0 | const htmlEntityDesc * ent; |
2634 | | |
2635 | | /* |
2636 | | * allocate a translation buffer. |
2637 | | */ |
2638 | 0 | buffer_size = HTML_PARSER_BUFFER_SIZE; |
2639 | 0 | buffer = xmlMalloc(buffer_size); |
2640 | 0 | if (buffer == NULL) { |
2641 | 0 | htmlErrMemory(ctxt); |
2642 | 0 | return(NULL); |
2643 | 0 | } |
2644 | 0 | out = buffer; |
2645 | | |
2646 | | /* |
2647 | | * Ok loop until we reach one of the ending chars |
2648 | | */ |
2649 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
2650 | 0 | (CUR != 0) && (CUR != stop)) { |
2651 | 0 | if ((stop == 0) && (CUR == '>')) break; |
2652 | 0 | if ((stop == 0) && (IS_BLANK_CH(CUR))) break; |
2653 | 0 | if (CUR == '&') { |
2654 | 0 | if (NXT(1) == '#') { |
2655 | 0 | unsigned int c; |
2656 | 0 | int bits; |
2657 | |
|
2658 | 0 | c = htmlParseCharRef(ctxt); |
2659 | 0 | if (c < 0x80) |
2660 | 0 | { *out++ = c; bits= -6; } |
2661 | 0 | else if (c < 0x800) |
2662 | 0 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
2663 | 0 | else if (c < 0x10000) |
2664 | 0 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
2665 | 0 | else |
2666 | 0 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
2667 | |
|
2668 | 0 | for ( ; bits >= 0; bits-= 6) { |
2669 | 0 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
2670 | 0 | } |
2671 | |
|
2672 | 0 | if (out - buffer > buffer_size - 100) { |
2673 | 0 | int indx = out - buffer; |
2674 | |
|
2675 | 0 | growBuffer(buffer); |
2676 | 0 | out = &buffer[indx]; |
2677 | 0 | } |
2678 | 0 | } else { |
2679 | 0 | ent = htmlParseEntityRef(ctxt, &name); |
2680 | 0 | if (name == NULL) { |
2681 | 0 | *out++ = '&'; |
2682 | 0 | if (out - buffer > buffer_size - 100) { |
2683 | 0 | int indx = out - buffer; |
2684 | |
|
2685 | 0 | growBuffer(buffer); |
2686 | 0 | out = &buffer[indx]; |
2687 | 0 | } |
2688 | 0 | } else if (ent == NULL) { |
2689 | 0 | *out++ = '&'; |
2690 | 0 | cur = name; |
2691 | 0 | while (*cur != 0) { |
2692 | 0 | if (out - buffer > buffer_size - 100) { |
2693 | 0 | int indx = out - buffer; |
2694 | |
|
2695 | 0 | growBuffer(buffer); |
2696 | 0 | out = &buffer[indx]; |
2697 | 0 | } |
2698 | 0 | *out++ = *cur++; |
2699 | 0 | } |
2700 | 0 | } else { |
2701 | 0 | unsigned int c; |
2702 | 0 | int bits; |
2703 | |
|
2704 | 0 | if (out - buffer > buffer_size - 100) { |
2705 | 0 | int indx = out - buffer; |
2706 | |
|
2707 | 0 | growBuffer(buffer); |
2708 | 0 | out = &buffer[indx]; |
2709 | 0 | } |
2710 | 0 | c = ent->value; |
2711 | 0 | if (c < 0x80) |
2712 | 0 | { *out++ = c; bits= -6; } |
2713 | 0 | else if (c < 0x800) |
2714 | 0 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
2715 | 0 | else if (c < 0x10000) |
2716 | 0 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
2717 | 0 | else |
2718 | 0 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
2719 | |
|
2720 | 0 | for ( ; bits >= 0; bits-= 6) { |
2721 | 0 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
2722 | 0 | } |
2723 | 0 | } |
2724 | 0 | } |
2725 | 0 | } else { |
2726 | 0 | unsigned int c; |
2727 | 0 | int bits, l; |
2728 | |
|
2729 | 0 | if (out - buffer > buffer_size - 100) { |
2730 | 0 | int indx = out - buffer; |
2731 | |
|
2732 | 0 | growBuffer(buffer); |
2733 | 0 | out = &buffer[indx]; |
2734 | 0 | } |
2735 | 0 | c = CUR_CHAR(l); |
2736 | 0 | if (c < 0x80) |
2737 | 0 | { *out++ = c; bits= -6; } |
2738 | 0 | else if (c < 0x800) |
2739 | 0 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
2740 | 0 | else if (c < 0x10000) |
2741 | 0 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
2742 | 0 | else |
2743 | 0 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
2744 | |
|
2745 | 0 | for ( ; bits >= 0; bits-= 6) { |
2746 | 0 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
2747 | 0 | } |
2748 | 0 | NEXTL(l); |
2749 | 0 | } |
2750 | 0 | if (out - buffer > maxLength) { |
2751 | 0 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
2752 | 0 | "attribute value too long\n", NULL, NULL); |
2753 | 0 | xmlFree(buffer); |
2754 | 0 | return(NULL); |
2755 | 0 | } |
2756 | 0 | } |
2757 | 0 | *out = 0; |
2758 | 0 | return(buffer); |
2759 | 0 | } |
2760 | | |
2761 | | /** |
2762 | | * htmlParseEntityRef: |
2763 | | * @ctxt: an HTML parser context |
2764 | | * @str: location to store the entity name |
2765 | | * |
2766 | | * DEPRECATED: Internal function, don't use. |
2767 | | * |
2768 | | * parse an HTML ENTITY references |
2769 | | * |
2770 | | * [68] EntityRef ::= '&' Name ';' |
2771 | | * |
2772 | | * Returns the associated htmlEntityDescPtr if found, or NULL otherwise, |
2773 | | * if non-NULL *str will have to be freed by the caller. |
2774 | | */ |
2775 | | const htmlEntityDesc * |
2776 | 0 | htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) { |
2777 | 0 | const xmlChar *name; |
2778 | 0 | const htmlEntityDesc * ent = NULL; |
2779 | |
|
2780 | 0 | if (str != NULL) *str = NULL; |
2781 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
2782 | | |
2783 | 0 | if (CUR == '&') { |
2784 | 0 | NEXT; |
2785 | 0 | name = htmlParseName(ctxt); |
2786 | 0 | if (name == NULL) { |
2787 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
2788 | 0 | "htmlParseEntityRef: no name\n", NULL, NULL); |
2789 | 0 | } else { |
2790 | 0 | GROW; |
2791 | 0 | if (CUR == ';') { |
2792 | 0 | if (str != NULL) |
2793 | 0 | *str = name; |
2794 | | |
2795 | | /* |
2796 | | * Lookup the entity in the table. |
2797 | | */ |
2798 | 0 | ent = htmlEntityLookup(name); |
2799 | 0 | if (ent != NULL) /* OK that's ugly !!! */ |
2800 | 0 | NEXT; |
2801 | 0 | } else { |
2802 | 0 | htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, |
2803 | 0 | "htmlParseEntityRef: expecting ';'\n", |
2804 | 0 | NULL, NULL); |
2805 | 0 | if (str != NULL) |
2806 | 0 | *str = name; |
2807 | 0 | } |
2808 | 0 | } |
2809 | 0 | } |
2810 | 0 | return(ent); |
2811 | 0 | } |
2812 | | |
2813 | | /** |
2814 | | * htmlParseAttValue: |
2815 | | * @ctxt: an HTML parser context |
2816 | | * |
2817 | | * parse a value for an attribute |
2818 | | * Note: the parser won't do substitution of entities here, this |
2819 | | * will be handled later in xmlStringGetNodeList, unless it was |
2820 | | * asked for ctxt->replaceEntities != 0 |
2821 | | * |
2822 | | * Returns the AttValue parsed or NULL. |
2823 | | */ |
2824 | | |
2825 | | static xmlChar * |
2826 | 0 | htmlParseAttValue(htmlParserCtxtPtr ctxt) { |
2827 | 0 | xmlChar *ret = NULL; |
2828 | |
|
2829 | 0 | if (CUR == '"') { |
2830 | 0 | NEXT; |
2831 | 0 | ret = htmlParseHTMLAttribute(ctxt, '"'); |
2832 | 0 | if (CUR != '"') { |
2833 | 0 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
2834 | 0 | "AttValue: \" expected\n", NULL, NULL); |
2835 | 0 | } else |
2836 | 0 | NEXT; |
2837 | 0 | } else if (CUR == '\'') { |
2838 | 0 | NEXT; |
2839 | 0 | ret = htmlParseHTMLAttribute(ctxt, '\''); |
2840 | 0 | if (CUR != '\'') { |
2841 | 0 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
2842 | 0 | "AttValue: ' expected\n", NULL, NULL); |
2843 | 0 | } else |
2844 | 0 | NEXT; |
2845 | 0 | } else { |
2846 | | /* |
2847 | | * That's an HTMLism, the attribute value may not be quoted |
2848 | | */ |
2849 | 0 | ret = htmlParseHTMLAttribute(ctxt, 0); |
2850 | 0 | if (ret == NULL) { |
2851 | 0 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
2852 | 0 | "AttValue: no value found\n", NULL, NULL); |
2853 | 0 | } |
2854 | 0 | } |
2855 | 0 | return(ret); |
2856 | 0 | } |
2857 | | |
2858 | | /** |
2859 | | * htmlParseSystemLiteral: |
2860 | | * @ctxt: an HTML parser context |
2861 | | * |
2862 | | * parse an HTML Literal |
2863 | | * |
2864 | | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
2865 | | * |
2866 | | * Returns the SystemLiteral parsed or NULL |
2867 | | */ |
2868 | | |
2869 | | static xmlChar * |
2870 | 0 | htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) { |
2871 | 0 | size_t len = 0, startPosition = 0; |
2872 | 0 | int err = 0; |
2873 | 0 | int quote; |
2874 | 0 | xmlChar *ret = NULL; |
2875 | |
|
2876 | 0 | if ((CUR != '"') && (CUR != '\'')) { |
2877 | 0 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
2878 | 0 | "SystemLiteral \" or ' expected\n", NULL, NULL); |
2879 | 0 | return(NULL); |
2880 | 0 | } |
2881 | 0 | quote = CUR; |
2882 | 0 | NEXT; |
2883 | |
|
2884 | 0 | if (CUR_PTR < BASE_PTR) |
2885 | 0 | return(ret); |
2886 | 0 | startPosition = CUR_PTR - BASE_PTR; |
2887 | |
|
2888 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
2889 | 0 | (CUR != 0) && (CUR != quote)) { |
2890 | | /* TODO: Handle UTF-8 */ |
2891 | 0 | if (!IS_CHAR_CH(CUR)) { |
2892 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
2893 | 0 | "Invalid char in SystemLiteral 0x%X\n", CUR); |
2894 | 0 | err = 1; |
2895 | 0 | } |
2896 | 0 | NEXT; |
2897 | 0 | len++; |
2898 | 0 | } |
2899 | 0 | if (CUR != quote) { |
2900 | 0 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
2901 | 0 | "Unfinished SystemLiteral\n", NULL, NULL); |
2902 | 0 | } else { |
2903 | 0 | if (err == 0) { |
2904 | 0 | ret = xmlStrndup((BASE_PTR+startPosition), len); |
2905 | 0 | if (ret == NULL) { |
2906 | 0 | htmlErrMemory(ctxt); |
2907 | 0 | return(NULL); |
2908 | 0 | } |
2909 | 0 | } |
2910 | 0 | NEXT; |
2911 | 0 | } |
2912 | | |
2913 | 0 | return(ret); |
2914 | 0 | } |
2915 | | |
2916 | | /** |
2917 | | * htmlParsePubidLiteral: |
2918 | | * @ctxt: an HTML parser context |
2919 | | * |
2920 | | * parse an HTML public literal |
2921 | | * |
2922 | | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
2923 | | * |
2924 | | * Returns the PubidLiteral parsed or NULL. |
2925 | | */ |
2926 | | |
2927 | | static xmlChar * |
2928 | 0 | htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) { |
2929 | 0 | size_t len = 0, startPosition = 0; |
2930 | 0 | int err = 0; |
2931 | 0 | int quote; |
2932 | 0 | xmlChar *ret = NULL; |
2933 | |
|
2934 | 0 | if ((CUR != '"') && (CUR != '\'')) { |
2935 | 0 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
2936 | 0 | "PubidLiteral \" or ' expected\n", NULL, NULL); |
2937 | 0 | return(NULL); |
2938 | 0 | } |
2939 | 0 | quote = CUR; |
2940 | 0 | NEXT; |
2941 | | |
2942 | | /* |
2943 | | * Name ::= (Letter | '_') (NameChar)* |
2944 | | */ |
2945 | 0 | if (CUR_PTR < BASE_PTR) |
2946 | 0 | return(ret); |
2947 | 0 | startPosition = CUR_PTR - BASE_PTR; |
2948 | |
|
2949 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
2950 | 0 | (CUR != 0) && (CUR != quote)) { |
2951 | 0 | if (!IS_PUBIDCHAR_CH(CUR)) { |
2952 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
2953 | 0 | "Invalid char in PubidLiteral 0x%X\n", CUR); |
2954 | 0 | err = 1; |
2955 | 0 | } |
2956 | 0 | len++; |
2957 | 0 | NEXT; |
2958 | 0 | } |
2959 | |
|
2960 | 0 | if (CUR != quote) { |
2961 | 0 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
2962 | 0 | "Unfinished PubidLiteral\n", NULL, NULL); |
2963 | 0 | } else { |
2964 | 0 | if (err == 0) { |
2965 | 0 | ret = xmlStrndup((BASE_PTR + startPosition), len); |
2966 | 0 | if (ret == NULL) { |
2967 | 0 | htmlErrMemory(ctxt); |
2968 | 0 | return(NULL); |
2969 | 0 | } |
2970 | 0 | } |
2971 | 0 | NEXT; |
2972 | 0 | } |
2973 | | |
2974 | 0 | return(ret); |
2975 | 0 | } |
2976 | | |
2977 | | /** |
2978 | | * htmlParseScript: |
2979 | | * @ctxt: an HTML parser context |
2980 | | * |
2981 | | * parse the content of an HTML SCRIPT or STYLE element |
2982 | | * http://www.w3.org/TR/html4/sgml/dtd.html#Script |
2983 | | * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet |
2984 | | * http://www.w3.org/TR/html4/types.html#type-script |
2985 | | * http://www.w3.org/TR/html4/types.html#h-6.15 |
2986 | | * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1 |
2987 | | * |
2988 | | * Script data ( %Script; in the DTD) can be the content of the SCRIPT |
2989 | | * element and the value of intrinsic event attributes. User agents must |
2990 | | * not evaluate script data as HTML markup but instead must pass it on as |
2991 | | * data to a script engine. |
2992 | | * NOTES: |
2993 | | * - The content is passed like CDATA |
2994 | | * - the attributes for style and scripting "onXXX" are also described |
2995 | | * as CDATA but SGML allows entities references in attributes so their |
2996 | | * processing is identical as other attributes |
2997 | | */ |
2998 | | static void |
2999 | 0 | htmlParseScript(htmlParserCtxtPtr ctxt) { |
3000 | 0 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5]; |
3001 | 0 | int nbchar = 0; |
3002 | 0 | int cur,l; |
3003 | |
|
3004 | 0 | cur = CUR_CHAR(l); |
3005 | 0 | while (cur != 0) { |
3006 | 0 | if ((cur == '<') && (NXT(1) == '/')) { |
3007 | | /* |
3008 | | * One should break here, the specification is clear: |
3009 | | * Authors should therefore escape "</" within the content. |
3010 | | * Escape mechanisms are specific to each scripting or |
3011 | | * style sheet language. |
3012 | | * |
3013 | | * In recovery mode, only break if end tag match the |
3014 | | * current tag, effectively ignoring all tags inside the |
3015 | | * script/style block and treating the entire block as |
3016 | | * CDATA. |
3017 | | */ |
3018 | 0 | if (ctxt->recovery) { |
3019 | 0 | if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2, |
3020 | 0 | xmlStrlen(ctxt->name)) == 0) |
3021 | 0 | { |
3022 | 0 | break; /* while */ |
3023 | 0 | } else { |
3024 | 0 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
3025 | 0 | "Element %s embeds close tag\n", |
3026 | 0 | ctxt->name, NULL); |
3027 | 0 | } |
3028 | 0 | } else { |
3029 | 0 | if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) || |
3030 | 0 | ((NXT(2) >= 'a') && (NXT(2) <= 'z'))) |
3031 | 0 | { |
3032 | 0 | break; /* while */ |
3033 | 0 | } |
3034 | 0 | } |
3035 | 0 | } |
3036 | 0 | if (IS_CHAR(cur)) { |
3037 | 0 | COPY_BUF(buf,nbchar,cur); |
3038 | 0 | } else { |
3039 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
3040 | 0 | "Invalid char in CDATA 0x%X\n", cur); |
3041 | 0 | } |
3042 | 0 | NEXTL(l); |
3043 | 0 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
3044 | 0 | buf[nbchar] = 0; |
3045 | 0 | if (ctxt->sax->cdataBlock!= NULL) { |
3046 | | /* |
3047 | | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
3048 | | */ |
3049 | 0 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
3050 | 0 | } else if (ctxt->sax->characters != NULL) { |
3051 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3052 | 0 | } |
3053 | 0 | nbchar = 0; |
3054 | 0 | SHRINK; |
3055 | 0 | } |
3056 | 0 | cur = CUR_CHAR(l); |
3057 | 0 | } |
3058 | |
|
3059 | 0 | if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
3060 | 0 | buf[nbchar] = 0; |
3061 | 0 | if (ctxt->sax->cdataBlock!= NULL) { |
3062 | | /* |
3063 | | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
3064 | | */ |
3065 | 0 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
3066 | 0 | } else if (ctxt->sax->characters != NULL) { |
3067 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3068 | 0 | } |
3069 | 0 | } |
3070 | 0 | } |
3071 | | |
3072 | | |
3073 | | /** |
3074 | | * htmlParseCharDataInternal: |
3075 | | * @ctxt: an HTML parser context |
3076 | | * @readahead: optional read ahead character in ascii range |
3077 | | * |
3078 | | * parse a CharData section. |
3079 | | * if we are within a CDATA section ']]>' marks an end of section. |
3080 | | * |
3081 | | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
3082 | | */ |
3083 | | |
3084 | | static void |
3085 | 0 | htmlParseCharDataInternal(htmlParserCtxtPtr ctxt, int readahead) { |
3086 | 0 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 6]; |
3087 | 0 | int nbchar = 0; |
3088 | 0 | int cur, l; |
3089 | |
|
3090 | 0 | if (readahead) |
3091 | 0 | buf[nbchar++] = readahead; |
3092 | |
|
3093 | 0 | cur = CUR_CHAR(l); |
3094 | 0 | while ((cur != '<') && |
3095 | 0 | (cur != '&') && |
3096 | 0 | (cur != 0) && |
3097 | 0 | (!PARSER_STOPPED(ctxt))) { |
3098 | 0 | if (!(IS_CHAR(cur))) { |
3099 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
3100 | 0 | "Invalid char in CDATA 0x%X\n", cur); |
3101 | 0 | } else { |
3102 | 0 | COPY_BUF(buf,nbchar,cur); |
3103 | 0 | } |
3104 | 0 | NEXTL(l); |
3105 | 0 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
3106 | 0 | buf[nbchar] = 0; |
3107 | | |
3108 | | /* |
3109 | | * Ok the segment is to be consumed as chars. |
3110 | | */ |
3111 | 0 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
3112 | 0 | if (areBlanks(ctxt, buf, nbchar)) { |
3113 | 0 | if (ctxt->keepBlanks) { |
3114 | 0 | if (ctxt->sax->characters != NULL) |
3115 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3116 | 0 | } else { |
3117 | 0 | if (ctxt->sax->ignorableWhitespace != NULL) |
3118 | 0 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
3119 | 0 | buf, nbchar); |
3120 | 0 | } |
3121 | 0 | } else { |
3122 | 0 | htmlCheckParagraph(ctxt); |
3123 | 0 | if (ctxt->sax->characters != NULL) |
3124 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3125 | 0 | } |
3126 | 0 | } |
3127 | 0 | nbchar = 0; |
3128 | 0 | SHRINK; |
3129 | 0 | } |
3130 | 0 | cur = CUR_CHAR(l); |
3131 | 0 | } |
3132 | 0 | if (nbchar != 0) { |
3133 | 0 | buf[nbchar] = 0; |
3134 | | |
3135 | | /* |
3136 | | * Ok the segment is to be consumed as chars. |
3137 | | */ |
3138 | 0 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
3139 | 0 | if (areBlanks(ctxt, buf, nbchar)) { |
3140 | 0 | if (ctxt->keepBlanks) { |
3141 | 0 | if (ctxt->sax->characters != NULL) |
3142 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3143 | 0 | } else { |
3144 | 0 | if (ctxt->sax->ignorableWhitespace != NULL) |
3145 | 0 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
3146 | 0 | buf, nbchar); |
3147 | 0 | } |
3148 | 0 | } else { |
3149 | 0 | htmlCheckParagraph(ctxt); |
3150 | 0 | if (ctxt->sax->characters != NULL) |
3151 | 0 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
3152 | 0 | } |
3153 | 0 | } |
3154 | 0 | } |
3155 | 0 | } |
3156 | | |
3157 | | /** |
3158 | | * htmlParseCharData: |
3159 | | * @ctxt: an HTML parser context |
3160 | | * |
3161 | | * parse a CharData section. |
3162 | | * if we are within a CDATA section ']]>' marks an end of section. |
3163 | | * |
3164 | | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
3165 | | */ |
3166 | | |
3167 | | static void |
3168 | 0 | htmlParseCharData(htmlParserCtxtPtr ctxt) { |
3169 | 0 | htmlParseCharDataInternal(ctxt, 0); |
3170 | 0 | } |
3171 | | |
3172 | | /** |
3173 | | * htmlParseExternalID: |
3174 | | * @ctxt: an HTML parser context |
3175 | | * @publicID: a xmlChar** receiving PubidLiteral |
3176 | | * |
3177 | | * Parse an External ID or a Public ID |
3178 | | * |
3179 | | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
3180 | | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
3181 | | * |
3182 | | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
3183 | | * |
3184 | | * Returns the function returns SystemLiteral and in the second |
3185 | | * case publicID receives PubidLiteral, is strict is off |
3186 | | * it is possible to return NULL and have publicID set. |
3187 | | */ |
3188 | | |
3189 | | static xmlChar * |
3190 | 0 | htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) { |
3191 | 0 | xmlChar *URI = NULL; |
3192 | |
|
3193 | 0 | if ((UPPER == 'S') && (UPP(1) == 'Y') && |
3194 | 0 | (UPP(2) == 'S') && (UPP(3) == 'T') && |
3195 | 0 | (UPP(4) == 'E') && (UPP(5) == 'M')) { |
3196 | 0 | SKIP(6); |
3197 | 0 | if (!IS_BLANK_CH(CUR)) { |
3198 | 0 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
3199 | 0 | "Space required after 'SYSTEM'\n", NULL, NULL); |
3200 | 0 | } |
3201 | 0 | SKIP_BLANKS; |
3202 | 0 | URI = htmlParseSystemLiteral(ctxt); |
3203 | 0 | if (URI == NULL) { |
3204 | 0 | htmlParseErr(ctxt, XML_ERR_URI_REQUIRED, |
3205 | 0 | "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL); |
3206 | 0 | } |
3207 | 0 | } else if ((UPPER == 'P') && (UPP(1) == 'U') && |
3208 | 0 | (UPP(2) == 'B') && (UPP(3) == 'L') && |
3209 | 0 | (UPP(4) == 'I') && (UPP(5) == 'C')) { |
3210 | 0 | SKIP(6); |
3211 | 0 | if (!IS_BLANK_CH(CUR)) { |
3212 | 0 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
3213 | 0 | "Space required after 'PUBLIC'\n", NULL, NULL); |
3214 | 0 | } |
3215 | 0 | SKIP_BLANKS; |
3216 | 0 | *publicID = htmlParsePubidLiteral(ctxt); |
3217 | 0 | if (*publicID == NULL) { |
3218 | 0 | htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED, |
3219 | 0 | "htmlParseExternalID: PUBLIC, no Public Identifier\n", |
3220 | 0 | NULL, NULL); |
3221 | 0 | } |
3222 | 0 | SKIP_BLANKS; |
3223 | 0 | if ((CUR == '"') || (CUR == '\'')) { |
3224 | 0 | URI = htmlParseSystemLiteral(ctxt); |
3225 | 0 | } |
3226 | 0 | } |
3227 | 0 | return(URI); |
3228 | 0 | } |
3229 | | |
3230 | | /** |
3231 | | * htmlParsePI: |
3232 | | * @ctxt: an HTML parser context |
3233 | | * |
3234 | | * Parse an XML Processing Instruction. HTML5 doesn't allow processing |
3235 | | * instructions, so this will be removed at some point. |
3236 | | */ |
3237 | | static void |
3238 | 0 | htmlParsePI(htmlParserCtxtPtr ctxt) { |
3239 | 0 | xmlChar *buf = NULL; |
3240 | 0 | int len = 0; |
3241 | 0 | int size = HTML_PARSER_BUFFER_SIZE; |
3242 | 0 | int cur, l; |
3243 | 0 | int maxLength = (ctxt->options & XML_PARSE_HUGE) ? |
3244 | 0 | XML_MAX_HUGE_LENGTH : |
3245 | 0 | XML_MAX_TEXT_LENGTH; |
3246 | 0 | const xmlChar *target; |
3247 | 0 | xmlParserInputState state; |
3248 | |
|
3249 | 0 | if ((RAW == '<') && (NXT(1) == '?')) { |
3250 | 0 | state = ctxt->instate; |
3251 | 0 | ctxt->instate = XML_PARSER_PI; |
3252 | | /* |
3253 | | * this is a Processing Instruction. |
3254 | | */ |
3255 | 0 | SKIP(2); |
3256 | | |
3257 | | /* |
3258 | | * Parse the target name and check for special support like |
3259 | | * namespace. |
3260 | | */ |
3261 | 0 | target = htmlParseName(ctxt); |
3262 | 0 | if (target != NULL) { |
3263 | 0 | if (RAW == '>') { |
3264 | 0 | SKIP(1); |
3265 | | |
3266 | | /* |
3267 | | * SAX: PI detected. |
3268 | | */ |
3269 | 0 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
3270 | 0 | (ctxt->sax->processingInstruction != NULL)) |
3271 | 0 | ctxt->sax->processingInstruction(ctxt->userData, |
3272 | 0 | target, NULL); |
3273 | 0 | goto done; |
3274 | 0 | } |
3275 | 0 | buf = xmlMalloc(size); |
3276 | 0 | if (buf == NULL) { |
3277 | 0 | htmlErrMemory(ctxt); |
3278 | 0 | return; |
3279 | 0 | } |
3280 | 0 | cur = CUR; |
3281 | 0 | if (!IS_BLANK(cur)) { |
3282 | 0 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
3283 | 0 | "ParsePI: PI %s space expected\n", target, NULL); |
3284 | 0 | } |
3285 | 0 | SKIP_BLANKS; |
3286 | 0 | cur = CUR_CHAR(l); |
3287 | 0 | while ((cur != 0) && (cur != '>')) { |
3288 | 0 | if (len + 5 >= size) { |
3289 | 0 | xmlChar *tmp; |
3290 | |
|
3291 | 0 | size *= 2; |
3292 | 0 | tmp = (xmlChar *) xmlRealloc(buf, size); |
3293 | 0 | if (tmp == NULL) { |
3294 | 0 | htmlErrMemory(ctxt); |
3295 | 0 | xmlFree(buf); |
3296 | 0 | return; |
3297 | 0 | } |
3298 | 0 | buf = tmp; |
3299 | 0 | } |
3300 | 0 | if (IS_CHAR(cur)) { |
3301 | 0 | COPY_BUF(buf,len,cur); |
3302 | 0 | } else { |
3303 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
3304 | 0 | "Invalid char in processing instruction " |
3305 | 0 | "0x%X\n", cur); |
3306 | 0 | } |
3307 | 0 | if (len > maxLength) { |
3308 | 0 | htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED, |
3309 | 0 | "PI %s too long", target, NULL); |
3310 | 0 | xmlFree(buf); |
3311 | 0 | goto done; |
3312 | 0 | } |
3313 | 0 | NEXTL(l); |
3314 | 0 | cur = CUR_CHAR(l); |
3315 | 0 | } |
3316 | 0 | buf[len] = 0; |
3317 | 0 | if (cur != '>') { |
3318 | 0 | htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED, |
3319 | 0 | "ParsePI: PI %s never end ...\n", target, NULL); |
3320 | 0 | } else { |
3321 | 0 | SKIP(1); |
3322 | | |
3323 | | /* |
3324 | | * SAX: PI detected. |
3325 | | */ |
3326 | 0 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
3327 | 0 | (ctxt->sax->processingInstruction != NULL)) |
3328 | 0 | ctxt->sax->processingInstruction(ctxt->userData, |
3329 | 0 | target, buf); |
3330 | 0 | } |
3331 | 0 | xmlFree(buf); |
3332 | 0 | } else { |
3333 | 0 | htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED, |
3334 | 0 | "PI is not started correctly", NULL, NULL); |
3335 | 0 | } |
3336 | | |
3337 | 0 | done: |
3338 | 0 | ctxt->instate = state; |
3339 | 0 | } |
3340 | 0 | } |
3341 | | |
3342 | | /** |
3343 | | * htmlParseComment: |
3344 | | * @ctxt: an HTML parser context |
3345 | | * |
3346 | | * Parse an HTML comment |
3347 | | */ |
3348 | | static void |
3349 | 0 | htmlParseComment(htmlParserCtxtPtr ctxt) { |
3350 | 0 | xmlChar *buf = NULL; |
3351 | 0 | int len; |
3352 | 0 | int size = HTML_PARSER_BUFFER_SIZE; |
3353 | 0 | int q, ql; |
3354 | 0 | int r, rl; |
3355 | 0 | int cur, l; |
3356 | 0 | int next, nl; |
3357 | 0 | int maxLength = (ctxt->options & XML_PARSE_HUGE) ? |
3358 | 0 | XML_MAX_HUGE_LENGTH : |
3359 | 0 | XML_MAX_TEXT_LENGTH; |
3360 | 0 | xmlParserInputState state; |
3361 | | |
3362 | | /* |
3363 | | * Check that there is a comment right here. |
3364 | | */ |
3365 | 0 | if ((RAW != '<') || (NXT(1) != '!') || |
3366 | 0 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
3367 | | |
3368 | 0 | state = ctxt->instate; |
3369 | 0 | ctxt->instate = XML_PARSER_COMMENT; |
3370 | 0 | SKIP(4); |
3371 | 0 | buf = xmlMalloc(size); |
3372 | 0 | if (buf == NULL) { |
3373 | 0 | htmlErrMemory(ctxt); |
3374 | 0 | return; |
3375 | 0 | } |
3376 | 0 | len = 0; |
3377 | 0 | buf[len] = 0; |
3378 | 0 | q = CUR_CHAR(ql); |
3379 | 0 | if (q == 0) |
3380 | 0 | goto unfinished; |
3381 | 0 | if (q == '>') { |
3382 | 0 | htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL); |
3383 | 0 | cur = '>'; |
3384 | 0 | goto finished; |
3385 | 0 | } |
3386 | 0 | NEXTL(ql); |
3387 | 0 | r = CUR_CHAR(rl); |
3388 | 0 | if (r == 0) |
3389 | 0 | goto unfinished; |
3390 | 0 | if (q == '-' && r == '>') { |
3391 | 0 | htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL); |
3392 | 0 | cur = '>'; |
3393 | 0 | goto finished; |
3394 | 0 | } |
3395 | 0 | NEXTL(rl); |
3396 | 0 | cur = CUR_CHAR(l); |
3397 | 0 | while ((cur != 0) && |
3398 | 0 | ((cur != '>') || |
3399 | 0 | (r != '-') || (q != '-'))) { |
3400 | 0 | NEXTL(l); |
3401 | 0 | next = CUR_CHAR(nl); |
3402 | |
|
3403 | 0 | if ((q == '-') && (r == '-') && (cur == '!') && (next == '>')) { |
3404 | 0 | htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
3405 | 0 | "Comment incorrectly closed by '--!>'", NULL, NULL); |
3406 | 0 | cur = '>'; |
3407 | 0 | break; |
3408 | 0 | } |
3409 | | |
3410 | 0 | if (len + 5 >= size) { |
3411 | 0 | xmlChar *tmp; |
3412 | |
|
3413 | 0 | size *= 2; |
3414 | 0 | tmp = (xmlChar *) xmlRealloc(buf, size); |
3415 | 0 | if (tmp == NULL) { |
3416 | 0 | xmlFree(buf); |
3417 | 0 | htmlErrMemory(ctxt); |
3418 | 0 | return; |
3419 | 0 | } |
3420 | 0 | buf = tmp; |
3421 | 0 | } |
3422 | 0 | if (IS_CHAR(q)) { |
3423 | 0 | COPY_BUF(buf,len,q); |
3424 | 0 | } else { |
3425 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
3426 | 0 | "Invalid char in comment 0x%X\n", q); |
3427 | 0 | } |
3428 | 0 | if (len > maxLength) { |
3429 | 0 | htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
3430 | 0 | "comment too long", NULL, NULL); |
3431 | 0 | xmlFree(buf); |
3432 | 0 | ctxt->instate = state; |
3433 | 0 | return; |
3434 | 0 | } |
3435 | | |
3436 | 0 | q = r; |
3437 | 0 | ql = rl; |
3438 | 0 | r = cur; |
3439 | 0 | rl = l; |
3440 | 0 | cur = next; |
3441 | 0 | l = nl; |
3442 | 0 | } |
3443 | 0 | finished: |
3444 | 0 | buf[len] = 0; |
3445 | 0 | if (cur == '>') { |
3446 | 0 | NEXT; |
3447 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
3448 | 0 | (!ctxt->disableSAX)) |
3449 | 0 | ctxt->sax->comment(ctxt->userData, buf); |
3450 | 0 | xmlFree(buf); |
3451 | 0 | ctxt->instate = state; |
3452 | 0 | return; |
3453 | 0 | } |
3454 | | |
3455 | 0 | unfinished: |
3456 | 0 | htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
3457 | 0 | "Comment not terminated \n<!--%.50s\n", buf, NULL); |
3458 | 0 | xmlFree(buf); |
3459 | 0 | } |
3460 | | |
3461 | | /** |
3462 | | * htmlParseCharRef: |
3463 | | * @ctxt: an HTML parser context |
3464 | | * |
3465 | | * DEPRECATED: Internal function, don't use. |
3466 | | * |
3467 | | * parse Reference declarations |
3468 | | * |
3469 | | * [66] CharRef ::= '&#' [0-9]+ ';' | |
3470 | | * '&#x' [0-9a-fA-F]+ ';' |
3471 | | * |
3472 | | * Returns the value parsed (as an int) |
3473 | | */ |
3474 | | int |
3475 | 0 | htmlParseCharRef(htmlParserCtxtPtr ctxt) { |
3476 | 0 | int val = 0; |
3477 | |
|
3478 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
3479 | 0 | return(0); |
3480 | 0 | if ((CUR == '&') && (NXT(1) == '#') && |
3481 | 0 | ((NXT(2) == 'x') || NXT(2) == 'X')) { |
3482 | 0 | SKIP(3); |
3483 | 0 | while (CUR != ';') { |
3484 | 0 | if ((CUR >= '0') && (CUR <= '9')) { |
3485 | 0 | if (val < 0x110000) |
3486 | 0 | val = val * 16 + (CUR - '0'); |
3487 | 0 | } else if ((CUR >= 'a') && (CUR <= 'f')) { |
3488 | 0 | if (val < 0x110000) |
3489 | 0 | val = val * 16 + (CUR - 'a') + 10; |
3490 | 0 | } else if ((CUR >= 'A') && (CUR <= 'F')) { |
3491 | 0 | if (val < 0x110000) |
3492 | 0 | val = val * 16 + (CUR - 'A') + 10; |
3493 | 0 | } else { |
3494 | 0 | htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, |
3495 | 0 | "htmlParseCharRef: missing semicolon\n", |
3496 | 0 | NULL, NULL); |
3497 | 0 | break; |
3498 | 0 | } |
3499 | 0 | NEXT; |
3500 | 0 | } |
3501 | 0 | if (CUR == ';') |
3502 | 0 | NEXT; |
3503 | 0 | } else if ((CUR == '&') && (NXT(1) == '#')) { |
3504 | 0 | SKIP(2); |
3505 | 0 | while (CUR != ';') { |
3506 | 0 | if ((CUR >= '0') && (CUR <= '9')) { |
3507 | 0 | if (val < 0x110000) |
3508 | 0 | val = val * 10 + (CUR - '0'); |
3509 | 0 | } else { |
3510 | 0 | htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, |
3511 | 0 | "htmlParseCharRef: missing semicolon\n", |
3512 | 0 | NULL, NULL); |
3513 | 0 | break; |
3514 | 0 | } |
3515 | 0 | NEXT; |
3516 | 0 | } |
3517 | 0 | if (CUR == ';') |
3518 | 0 | NEXT; |
3519 | 0 | } else { |
3520 | 0 | htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF, |
3521 | 0 | "htmlParseCharRef: invalid value\n", NULL, NULL); |
3522 | 0 | } |
3523 | | /* |
3524 | | * Check the value IS_CHAR ... |
3525 | | */ |
3526 | 0 | if (IS_CHAR(val)) { |
3527 | 0 | return(val); |
3528 | 0 | } else if (val >= 0x110000) { |
3529 | 0 | htmlParseErr(ctxt, XML_ERR_INVALID_CHAR, |
3530 | 0 | "htmlParseCharRef: value too large\n", NULL, NULL); |
3531 | 0 | } else { |
3532 | 0 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
3533 | 0 | "htmlParseCharRef: invalid xmlChar value %d\n", |
3534 | 0 | val); |
3535 | 0 | } |
3536 | 0 | return(0); |
3537 | 0 | } |
3538 | | |
3539 | | |
3540 | | /** |
3541 | | * htmlParseDocTypeDecl: |
3542 | | * @ctxt: an HTML parser context |
3543 | | * |
3544 | | * parse a DOCTYPE declaration |
3545 | | * |
3546 | | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
3547 | | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
3548 | | */ |
3549 | | |
3550 | | static void |
3551 | 0 | htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) { |
3552 | 0 | const xmlChar *name; |
3553 | 0 | xmlChar *ExternalID = NULL; |
3554 | 0 | xmlChar *URI = NULL; |
3555 | | |
3556 | | /* |
3557 | | * We know that '<!DOCTYPE' has been detected. |
3558 | | */ |
3559 | 0 | SKIP(9); |
3560 | |
|
3561 | 0 | SKIP_BLANKS; |
3562 | | |
3563 | | /* |
3564 | | * Parse the DOCTYPE name. |
3565 | | */ |
3566 | 0 | name = htmlParseName(ctxt); |
3567 | 0 | if (name == NULL) { |
3568 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
3569 | 0 | "htmlParseDocTypeDecl : no DOCTYPE name !\n", |
3570 | 0 | NULL, NULL); |
3571 | 0 | } |
3572 | | /* |
3573 | | * Check that upper(name) == "HTML" !!!!!!!!!!!!! |
3574 | | */ |
3575 | |
|
3576 | 0 | SKIP_BLANKS; |
3577 | | |
3578 | | /* |
3579 | | * Check for SystemID and ExternalID |
3580 | | */ |
3581 | 0 | URI = htmlParseExternalID(ctxt, &ExternalID); |
3582 | 0 | SKIP_BLANKS; |
3583 | | |
3584 | | /* |
3585 | | * We should be at the end of the DOCTYPE declaration. |
3586 | | */ |
3587 | 0 | if (CUR != '>') { |
3588 | 0 | htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, |
3589 | 0 | "DOCTYPE improperly terminated\n", NULL, NULL); |
3590 | | /* Ignore bogus content */ |
3591 | 0 | while ((CUR != 0) && (CUR != '>') && |
3592 | 0 | (PARSER_STOPPED(ctxt) == 0)) |
3593 | 0 | NEXT; |
3594 | 0 | } |
3595 | 0 | if (CUR == '>') |
3596 | 0 | NEXT; |
3597 | | |
3598 | | /* |
3599 | | * Create or update the document accordingly to the DOCTYPE |
3600 | | */ |
3601 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
3602 | 0 | (!ctxt->disableSAX)) |
3603 | 0 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
3604 | | |
3605 | | /* |
3606 | | * Cleanup, since we don't use all those identifiers |
3607 | | */ |
3608 | 0 | if (URI != NULL) xmlFree(URI); |
3609 | 0 | if (ExternalID != NULL) xmlFree(ExternalID); |
3610 | 0 | } |
3611 | | |
3612 | | /** |
3613 | | * htmlParseAttribute: |
3614 | | * @ctxt: an HTML parser context |
3615 | | * @value: a xmlChar ** used to store the value of the attribute |
3616 | | * |
3617 | | * parse an attribute |
3618 | | * |
3619 | | * [41] Attribute ::= Name Eq AttValue |
3620 | | * |
3621 | | * [25] Eq ::= S? '=' S? |
3622 | | * |
3623 | | * With namespace: |
3624 | | * |
3625 | | * [NS 11] Attribute ::= QName Eq AttValue |
3626 | | * |
3627 | | * Also the case QName == xmlns:??? is handled independently as a namespace |
3628 | | * definition. |
3629 | | * |
3630 | | * Returns the attribute name, and the value in *value. |
3631 | | */ |
3632 | | |
3633 | | static const xmlChar * |
3634 | 0 | htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) { |
3635 | 0 | const xmlChar *name; |
3636 | 0 | xmlChar *val = NULL; |
3637 | |
|
3638 | 0 | *value = NULL; |
3639 | 0 | name = htmlParseHTMLName(ctxt); |
3640 | 0 | if (name == NULL) { |
3641 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
3642 | 0 | "error parsing attribute name\n", NULL, NULL); |
3643 | 0 | return(NULL); |
3644 | 0 | } |
3645 | | |
3646 | | /* |
3647 | | * read the value |
3648 | | */ |
3649 | 0 | SKIP_BLANKS; |
3650 | 0 | if (CUR == '=') { |
3651 | 0 | NEXT; |
3652 | 0 | SKIP_BLANKS; |
3653 | 0 | val = htmlParseAttValue(ctxt); |
3654 | 0 | } |
3655 | |
|
3656 | 0 | *value = val; |
3657 | 0 | return(name); |
3658 | 0 | } |
3659 | | |
3660 | | /** |
3661 | | * htmlCheckEncoding: |
3662 | | * @ctxt: an HTML parser context |
3663 | | * @attvalue: the attribute value |
3664 | | * |
3665 | | * Checks an http-equiv attribute from a Meta tag to detect |
3666 | | * the encoding |
3667 | | * If a new encoding is detected the parser is switched to decode |
3668 | | * it and pass UTF8 |
3669 | | */ |
3670 | | static void |
3671 | 0 | htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { |
3672 | 0 | const xmlChar *encoding; |
3673 | 0 | xmlChar *copy; |
3674 | |
|
3675 | 0 | if (!attvalue) |
3676 | 0 | return; |
3677 | | |
3678 | 0 | encoding = xmlStrcasestr(attvalue, BAD_CAST"charset"); |
3679 | 0 | if (encoding != NULL) { |
3680 | 0 | encoding += 7; |
3681 | 0 | } |
3682 | | /* |
3683 | | * skip blank |
3684 | | */ |
3685 | 0 | if (encoding && IS_BLANK_CH(*encoding)) |
3686 | 0 | encoding = xmlStrcasestr(attvalue, BAD_CAST"="); |
3687 | 0 | if (encoding && *encoding == '=') { |
3688 | 0 | encoding ++; |
3689 | 0 | copy = xmlStrdup(encoding); |
3690 | 0 | if (copy == NULL) |
3691 | 0 | htmlErrMemory(ctxt); |
3692 | 0 | xmlSetDeclaredEncoding(ctxt, copy); |
3693 | 0 | } |
3694 | 0 | } |
3695 | | |
3696 | | /** |
3697 | | * htmlCheckMeta: |
3698 | | * @ctxt: an HTML parser context |
3699 | | * @atts: the attributes values |
3700 | | * |
3701 | | * Checks an attributes from a Meta tag |
3702 | | */ |
3703 | | static void |
3704 | 0 | htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) { |
3705 | 0 | int i; |
3706 | 0 | const xmlChar *att, *value; |
3707 | 0 | int http = 0; |
3708 | 0 | const xmlChar *content = NULL; |
3709 | |
|
3710 | 0 | if ((ctxt == NULL) || (atts == NULL)) |
3711 | 0 | return; |
3712 | | |
3713 | 0 | i = 0; |
3714 | 0 | att = atts[i++]; |
3715 | 0 | while (att != NULL) { |
3716 | 0 | value = atts[i++]; |
3717 | 0 | if (value != NULL) { |
3718 | 0 | if ((!xmlStrcasecmp(att, BAD_CAST "http-equiv")) && |
3719 | 0 | (!xmlStrcasecmp(value, BAD_CAST "Content-Type"))) { |
3720 | 0 | http = 1; |
3721 | 0 | } else if (!xmlStrcasecmp(att, BAD_CAST "charset")) { |
3722 | 0 | xmlChar *copy; |
3723 | |
|
3724 | 0 | copy = xmlStrdup(value); |
3725 | 0 | if (copy == NULL) |
3726 | 0 | htmlErrMemory(ctxt); |
3727 | 0 | xmlSetDeclaredEncoding(ctxt, copy); |
3728 | 0 | } else if (!xmlStrcasecmp(att, BAD_CAST "content")) { |
3729 | 0 | content = value; |
3730 | 0 | } |
3731 | 0 | } |
3732 | 0 | att = atts[i++]; |
3733 | 0 | } |
3734 | 0 | if ((http) && (content != NULL)) |
3735 | 0 | htmlCheckEncoding(ctxt, content); |
3736 | |
|
3737 | 0 | } |
3738 | | |
3739 | | /** |
3740 | | * htmlParseStartTag: |
3741 | | * @ctxt: an HTML parser context |
3742 | | * |
3743 | | * parse a start of tag either for rule element or |
3744 | | * EmptyElement. In both case we don't parse the tag closing chars. |
3745 | | * |
3746 | | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
3747 | | * |
3748 | | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
3749 | | * |
3750 | | * With namespace: |
3751 | | * |
3752 | | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
3753 | | * |
3754 | | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
3755 | | * |
3756 | | * Returns 0 in case of success, -1 in case of error and 1 if discarded |
3757 | | */ |
3758 | | |
3759 | | static int |
3760 | 0 | htmlParseStartTag(htmlParserCtxtPtr ctxt) { |
3761 | 0 | const xmlChar *name; |
3762 | 0 | const xmlChar *attname; |
3763 | 0 | xmlChar *attvalue; |
3764 | 0 | const xmlChar **atts; |
3765 | 0 | int nbatts = 0; |
3766 | 0 | int maxatts; |
3767 | 0 | int meta = 0; |
3768 | 0 | int i; |
3769 | 0 | int discardtag = 0; |
3770 | |
|
3771 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
3772 | 0 | return -1; |
3773 | 0 | if (CUR != '<') return -1; |
3774 | 0 | NEXT; |
3775 | |
|
3776 | 0 | atts = ctxt->atts; |
3777 | 0 | maxatts = ctxt->maxatts; |
3778 | |
|
3779 | 0 | GROW; |
3780 | 0 | name = htmlParseHTMLName(ctxt); |
3781 | 0 | if (name == NULL) { |
3782 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
3783 | 0 | "htmlParseStartTag: invalid element name\n", |
3784 | 0 | NULL, NULL); |
3785 | | /* Dump the bogus tag like browsers do */ |
3786 | 0 | while ((CUR != 0) && (CUR != '>') && |
3787 | 0 | (PARSER_STOPPED(ctxt) == 0)) |
3788 | 0 | NEXT; |
3789 | 0 | return -1; |
3790 | 0 | } |
3791 | 0 | if (xmlStrEqual(name, BAD_CAST"meta")) |
3792 | 0 | meta = 1; |
3793 | | |
3794 | | /* |
3795 | | * Check for auto-closure of HTML elements. |
3796 | | */ |
3797 | 0 | htmlAutoClose(ctxt, name); |
3798 | | |
3799 | | /* |
3800 | | * Check for implied HTML elements. |
3801 | | */ |
3802 | 0 | htmlCheckImplied(ctxt, name); |
3803 | | |
3804 | | /* |
3805 | | * Avoid html at any level > 0, head at any level != 1 |
3806 | | * or any attempt to recurse body |
3807 | | */ |
3808 | 0 | if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) { |
3809 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
3810 | 0 | "htmlParseStartTag: misplaced <html> tag\n", |
3811 | 0 | name, NULL); |
3812 | 0 | discardtag = 1; |
3813 | 0 | ctxt->depth++; |
3814 | 0 | } |
3815 | 0 | if ((ctxt->nameNr != 1) && |
3816 | 0 | (xmlStrEqual(name, BAD_CAST"head"))) { |
3817 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
3818 | 0 | "htmlParseStartTag: misplaced <head> tag\n", |
3819 | 0 | name, NULL); |
3820 | 0 | discardtag = 1; |
3821 | 0 | ctxt->depth++; |
3822 | 0 | } |
3823 | 0 | if (xmlStrEqual(name, BAD_CAST"body")) { |
3824 | 0 | int indx; |
3825 | 0 | for (indx = 0;indx < ctxt->nameNr;indx++) { |
3826 | 0 | if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) { |
3827 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
3828 | 0 | "htmlParseStartTag: misplaced <body> tag\n", |
3829 | 0 | name, NULL); |
3830 | 0 | discardtag = 1; |
3831 | 0 | ctxt->depth++; |
3832 | 0 | } |
3833 | 0 | } |
3834 | 0 | } |
3835 | | |
3836 | | /* |
3837 | | * Now parse the attributes, it ends up with the ending |
3838 | | * |
3839 | | * (S Attribute)* S? |
3840 | | */ |
3841 | 0 | SKIP_BLANKS; |
3842 | 0 | while ((CUR != 0) && |
3843 | 0 | (CUR != '>') && |
3844 | 0 | ((CUR != '/') || (NXT(1) != '>')) && |
3845 | 0 | (PARSER_STOPPED(ctxt) == 0)) { |
3846 | 0 | GROW; |
3847 | 0 | attname = htmlParseAttribute(ctxt, &attvalue); |
3848 | 0 | if (attname != NULL) { |
3849 | | |
3850 | | /* |
3851 | | * Well formedness requires at most one declaration of an attribute |
3852 | | */ |
3853 | 0 | for (i = 0; i < nbatts;i += 2) { |
3854 | 0 | if (xmlStrEqual(atts[i], attname)) { |
3855 | 0 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED, |
3856 | 0 | "Attribute %s redefined\n", attname, NULL); |
3857 | 0 | if (attvalue != NULL) |
3858 | 0 | xmlFree(attvalue); |
3859 | 0 | goto failed; |
3860 | 0 | } |
3861 | 0 | } |
3862 | | |
3863 | | /* |
3864 | | * Add the pair to atts |
3865 | | */ |
3866 | 0 | if (atts == NULL) { |
3867 | 0 | maxatts = 22; /* allow for 10 attrs by default */ |
3868 | 0 | atts = (const xmlChar **) |
3869 | 0 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
3870 | 0 | if (atts == NULL) { |
3871 | 0 | htmlErrMemory(ctxt); |
3872 | 0 | if (attvalue != NULL) |
3873 | 0 | xmlFree(attvalue); |
3874 | 0 | goto failed; |
3875 | 0 | } |
3876 | 0 | ctxt->atts = atts; |
3877 | 0 | ctxt->maxatts = maxatts; |
3878 | 0 | } else if (nbatts + 4 > maxatts) { |
3879 | 0 | const xmlChar **n; |
3880 | |
|
3881 | 0 | maxatts *= 2; |
3882 | 0 | n = (const xmlChar **) xmlRealloc((void *) atts, |
3883 | 0 | maxatts * sizeof(const xmlChar *)); |
3884 | 0 | if (n == NULL) { |
3885 | 0 | htmlErrMemory(ctxt); |
3886 | 0 | if (attvalue != NULL) |
3887 | 0 | xmlFree(attvalue); |
3888 | 0 | goto failed; |
3889 | 0 | } |
3890 | 0 | atts = n; |
3891 | 0 | ctxt->atts = atts; |
3892 | 0 | ctxt->maxatts = maxatts; |
3893 | 0 | } |
3894 | 0 | atts[nbatts++] = attname; |
3895 | 0 | atts[nbatts++] = attvalue; |
3896 | 0 | atts[nbatts] = NULL; |
3897 | 0 | atts[nbatts + 1] = NULL; |
3898 | 0 | } |
3899 | 0 | else { |
3900 | 0 | if (attvalue != NULL) |
3901 | 0 | xmlFree(attvalue); |
3902 | | /* Dump the bogus attribute string up to the next blank or |
3903 | | * the end of the tag. */ |
3904 | 0 | while ((CUR != 0) && |
3905 | 0 | !(IS_BLANK_CH(CUR)) && (CUR != '>') && |
3906 | 0 | ((CUR != '/') || (NXT(1) != '>')) && |
3907 | 0 | (PARSER_STOPPED(ctxt) == 0)) |
3908 | 0 | NEXT; |
3909 | 0 | } |
3910 | | |
3911 | 0 | failed: |
3912 | 0 | SKIP_BLANKS; |
3913 | 0 | } |
3914 | | |
3915 | | /* |
3916 | | * Handle specific association to the META tag |
3917 | | */ |
3918 | 0 | if (meta && (nbatts != 0)) |
3919 | 0 | htmlCheckMeta(ctxt, atts); |
3920 | | |
3921 | | /* |
3922 | | * SAX: Start of Element ! |
3923 | | */ |
3924 | 0 | if (!discardtag) { |
3925 | 0 | htmlnamePush(ctxt, name); |
3926 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) { |
3927 | 0 | if (nbatts != 0) |
3928 | 0 | ctxt->sax->startElement(ctxt->userData, name, atts); |
3929 | 0 | else |
3930 | 0 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
3931 | 0 | } |
3932 | 0 | } |
3933 | |
|
3934 | 0 | if (atts != NULL) { |
3935 | 0 | for (i = 1;i < nbatts;i += 2) { |
3936 | 0 | if (atts[i] != NULL) |
3937 | 0 | xmlFree((xmlChar *) atts[i]); |
3938 | 0 | } |
3939 | 0 | } |
3940 | |
|
3941 | 0 | return(discardtag); |
3942 | 0 | } |
3943 | | |
3944 | | /** |
3945 | | * htmlParseEndTag: |
3946 | | * @ctxt: an HTML parser context |
3947 | | * |
3948 | | * parse an end of tag |
3949 | | * |
3950 | | * [42] ETag ::= '</' Name S? '>' |
3951 | | * |
3952 | | * With namespace |
3953 | | * |
3954 | | * [NS 9] ETag ::= '</' QName S? '>' |
3955 | | * |
3956 | | * Returns 1 if the current level should be closed. |
3957 | | */ |
3958 | | |
3959 | | static int |
3960 | | htmlParseEndTag(htmlParserCtxtPtr ctxt) |
3961 | 0 | { |
3962 | 0 | const xmlChar *name; |
3963 | 0 | const xmlChar *oldname; |
3964 | 0 | int i, ret; |
3965 | |
|
3966 | 0 | if ((CUR != '<') || (NXT(1) != '/')) { |
3967 | 0 | htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED, |
3968 | 0 | "htmlParseEndTag: '</' not found\n", NULL, NULL); |
3969 | 0 | return (0); |
3970 | 0 | } |
3971 | 0 | SKIP(2); |
3972 | |
|
3973 | 0 | name = htmlParseHTMLName(ctxt); |
3974 | 0 | if (name == NULL) |
3975 | 0 | return (0); |
3976 | | /* |
3977 | | * We should definitely be at the ending "S? '>'" part |
3978 | | */ |
3979 | 0 | SKIP_BLANKS; |
3980 | 0 | if (CUR != '>') { |
3981 | 0 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
3982 | 0 | "End tag : expected '>'\n", NULL, NULL); |
3983 | | /* Skip to next '>' */ |
3984 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
3985 | 0 | (CUR != 0) && (CUR != '>')) |
3986 | 0 | NEXT; |
3987 | 0 | } |
3988 | 0 | if (CUR == '>') |
3989 | 0 | NEXT; |
3990 | | |
3991 | | /* |
3992 | | * if we ignored misplaced tags in htmlParseStartTag don't pop them |
3993 | | * out now. |
3994 | | */ |
3995 | 0 | if ((ctxt->depth > 0) && |
3996 | 0 | (xmlStrEqual(name, BAD_CAST "html") || |
3997 | 0 | xmlStrEqual(name, BAD_CAST "body") || |
3998 | 0 | xmlStrEqual(name, BAD_CAST "head"))) { |
3999 | 0 | ctxt->depth--; |
4000 | 0 | return (0); |
4001 | 0 | } |
4002 | | |
4003 | | /* |
4004 | | * If the name read is not one of the element in the parsing stack |
4005 | | * then return, it's just an error. |
4006 | | */ |
4007 | 0 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
4008 | 0 | if (xmlStrEqual(name, ctxt->nameTab[i])) |
4009 | 0 | break; |
4010 | 0 | } |
4011 | 0 | if (i < 0) { |
4012 | 0 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
4013 | 0 | "Unexpected end tag : %s\n", name, NULL); |
4014 | 0 | return (0); |
4015 | 0 | } |
4016 | | |
4017 | | |
4018 | | /* |
4019 | | * Check for auto-closure of HTML elements. |
4020 | | */ |
4021 | | |
4022 | 0 | htmlAutoCloseOnClose(ctxt, name); |
4023 | | |
4024 | | /* |
4025 | | * Well formedness constraints, opening and closing must match. |
4026 | | * With the exception that the autoclose may have popped stuff out |
4027 | | * of the stack. |
4028 | | */ |
4029 | 0 | if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) { |
4030 | 0 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
4031 | 0 | "Opening and ending tag mismatch: %s and %s\n", |
4032 | 0 | name, ctxt->name); |
4033 | 0 | } |
4034 | | |
4035 | | /* |
4036 | | * SAX: End of Tag |
4037 | | */ |
4038 | 0 | oldname = ctxt->name; |
4039 | 0 | if ((oldname != NULL) && (xmlStrEqual(oldname, name))) { |
4040 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
4041 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
4042 | 0 | htmlNodeInfoPop(ctxt); |
4043 | 0 | htmlnamePop(ctxt); |
4044 | 0 | ret = 1; |
4045 | 0 | } else { |
4046 | 0 | ret = 0; |
4047 | 0 | } |
4048 | |
|
4049 | 0 | return (ret); |
4050 | 0 | } |
4051 | | |
4052 | | |
4053 | | /** |
4054 | | * htmlParseReference: |
4055 | | * @ctxt: an HTML parser context |
4056 | | * |
4057 | | * parse and handle entity references in content, |
4058 | | * this will end-up in a call to character() since this is either a |
4059 | | * CharRef, or a predefined entity. |
4060 | | */ |
4061 | | static void |
4062 | 0 | htmlParseReference(htmlParserCtxtPtr ctxt) { |
4063 | 0 | const htmlEntityDesc * ent; |
4064 | 0 | xmlChar out[6]; |
4065 | 0 | const xmlChar *name; |
4066 | 0 | if (CUR != '&') return; |
4067 | | |
4068 | 0 | if (NXT(1) == '#') { |
4069 | 0 | unsigned int c; |
4070 | 0 | int bits, i = 0; |
4071 | |
|
4072 | 0 | c = htmlParseCharRef(ctxt); |
4073 | 0 | if (c == 0) |
4074 | 0 | return; |
4075 | | |
4076 | 0 | if (c < 0x80) { out[i++]= c; bits= -6; } |
4077 | 0 | else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
4078 | 0 | else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
4079 | 0 | else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
4080 | |
|
4081 | 0 | for ( ; bits >= 0; bits-= 6) { |
4082 | 0 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
4083 | 0 | } |
4084 | 0 | out[i] = 0; |
4085 | |
|
4086 | 0 | htmlCheckParagraph(ctxt); |
4087 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
4088 | 0 | ctxt->sax->characters(ctxt->userData, out, i); |
4089 | 0 | } else { |
4090 | 0 | ent = htmlParseEntityRef(ctxt, &name); |
4091 | 0 | if (name == NULL) { |
4092 | 0 | htmlCheckParagraph(ctxt); |
4093 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
4094 | 0 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
4095 | 0 | return; |
4096 | 0 | } |
4097 | 0 | if ((ent == NULL) || !(ent->value > 0)) { |
4098 | 0 | htmlCheckParagraph(ctxt); |
4099 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) { |
4100 | 0 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
4101 | 0 | ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name)); |
4102 | | /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */ |
4103 | 0 | } |
4104 | 0 | } else { |
4105 | 0 | unsigned int c; |
4106 | 0 | int bits, i = 0; |
4107 | |
|
4108 | 0 | c = ent->value; |
4109 | 0 | if (c < 0x80) |
4110 | 0 | { out[i++]= c; bits= -6; } |
4111 | 0 | else if (c < 0x800) |
4112 | 0 | { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
4113 | 0 | else if (c < 0x10000) |
4114 | 0 | { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
4115 | 0 | else |
4116 | 0 | { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
4117 | |
|
4118 | 0 | for ( ; bits >= 0; bits-= 6) { |
4119 | 0 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
4120 | 0 | } |
4121 | 0 | out[i] = 0; |
4122 | |
|
4123 | 0 | htmlCheckParagraph(ctxt); |
4124 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
4125 | 0 | ctxt->sax->characters(ctxt->userData, out, i); |
4126 | 0 | } |
4127 | 0 | } |
4128 | 0 | } |
4129 | | |
4130 | | /** |
4131 | | * htmlParseContent: |
4132 | | * @ctxt: an HTML parser context |
4133 | | * |
4134 | | * Parse a content: comment, sub-element, reference or text. |
4135 | | * Kept for compatibility with old code |
4136 | | */ |
4137 | | |
4138 | | static void |
4139 | 0 | htmlParseContent(htmlParserCtxtPtr ctxt) { |
4140 | 0 | xmlChar *currentNode; |
4141 | 0 | int depth; |
4142 | 0 | const xmlChar *name; |
4143 | |
|
4144 | 0 | currentNode = xmlStrdup(ctxt->name); |
4145 | 0 | depth = ctxt->nameNr; |
4146 | 0 | while (!PARSER_STOPPED(ctxt)) { |
4147 | 0 | GROW; |
4148 | | |
4149 | | /* |
4150 | | * Our tag or one of it's parent or children is ending. |
4151 | | */ |
4152 | 0 | if ((CUR == '<') && (NXT(1) == '/')) { |
4153 | 0 | if (htmlParseEndTag(ctxt) && |
4154 | 0 | ((currentNode != NULL) || (ctxt->nameNr == 0))) { |
4155 | 0 | if (currentNode != NULL) |
4156 | 0 | xmlFree(currentNode); |
4157 | 0 | return; |
4158 | 0 | } |
4159 | 0 | continue; /* while */ |
4160 | 0 | } |
4161 | | |
4162 | 0 | else if ((CUR == '<') && |
4163 | 0 | ((IS_ASCII_LETTER(NXT(1))) || |
4164 | 0 | (NXT(1) == '_') || (NXT(1) == ':'))) { |
4165 | 0 | name = htmlParseHTMLName_nonInvasive(ctxt); |
4166 | 0 | if (name == NULL) { |
4167 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
4168 | 0 | "htmlParseStartTag: invalid element name\n", |
4169 | 0 | NULL, NULL); |
4170 | | /* Dump the bogus tag like browsers do */ |
4171 | 0 | while ((CUR != 0) && (CUR != '>')) |
4172 | 0 | NEXT; |
4173 | |
|
4174 | 0 | if (currentNode != NULL) |
4175 | 0 | xmlFree(currentNode); |
4176 | 0 | return; |
4177 | 0 | } |
4178 | | |
4179 | 0 | if (ctxt->name != NULL) { |
4180 | 0 | if (htmlCheckAutoClose(name, ctxt->name) == 1) { |
4181 | 0 | htmlAutoClose(ctxt, name); |
4182 | 0 | continue; |
4183 | 0 | } |
4184 | 0 | } |
4185 | 0 | } |
4186 | | |
4187 | | /* |
4188 | | * Has this node been popped out during parsing of |
4189 | | * the next element |
4190 | | */ |
4191 | 0 | if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) && |
4192 | 0 | (!xmlStrEqual(currentNode, ctxt->name))) |
4193 | 0 | { |
4194 | 0 | if (currentNode != NULL) xmlFree(currentNode); |
4195 | 0 | return; |
4196 | 0 | } |
4197 | | |
4198 | 0 | if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) || |
4199 | 0 | (xmlStrEqual(currentNode, BAD_CAST"style")))) { |
4200 | | /* |
4201 | | * Handle SCRIPT/STYLE separately |
4202 | | */ |
4203 | 0 | htmlParseScript(ctxt); |
4204 | 0 | } |
4205 | | |
4206 | 0 | else if ((CUR == '<') && (NXT(1) == '!')) { |
4207 | | /* |
4208 | | * Sometimes DOCTYPE arrives in the middle of the document |
4209 | | */ |
4210 | 0 | if ((UPP(2) == 'D') && (UPP(3) == 'O') && |
4211 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
4212 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
4213 | 0 | (UPP(8) == 'E')) { |
4214 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
4215 | 0 | "Misplaced DOCTYPE declaration\n", |
4216 | 0 | BAD_CAST "DOCTYPE" , NULL); |
4217 | 0 | htmlParseDocTypeDecl(ctxt); |
4218 | 0 | } |
4219 | | /* |
4220 | | * First case : a comment |
4221 | | */ |
4222 | 0 | else if ((NXT(2) == '-') && (NXT(3) == '-')) { |
4223 | 0 | htmlParseComment(ctxt); |
4224 | 0 | } |
4225 | 0 | else { |
4226 | 0 | htmlSkipBogusComment(ctxt); |
4227 | 0 | } |
4228 | 0 | } |
4229 | | |
4230 | | /* |
4231 | | * Second case : a Processing Instruction. |
4232 | | */ |
4233 | 0 | else if ((CUR == '<') && (NXT(1) == '?')) { |
4234 | 0 | htmlParsePI(ctxt); |
4235 | 0 | } |
4236 | | |
4237 | | /* |
4238 | | * Third case : a sub-element. |
4239 | | */ |
4240 | 0 | else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) { |
4241 | 0 | htmlParseElement(ctxt); |
4242 | 0 | } |
4243 | 0 | else if (CUR == '<') { |
4244 | 0 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
4245 | 0 | (ctxt->sax->characters != NULL)) |
4246 | 0 | ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1); |
4247 | 0 | NEXT; |
4248 | 0 | } |
4249 | | |
4250 | | /* |
4251 | | * Fourth case : a reference. If if has not been resolved, |
4252 | | * parsing returns it's Name, create the node |
4253 | | */ |
4254 | 0 | else if (CUR == '&') { |
4255 | 0 | htmlParseReference(ctxt); |
4256 | 0 | } |
4257 | | |
4258 | | /* |
4259 | | * Fifth case : end of the resource |
4260 | | */ |
4261 | 0 | else if (CUR == 0) { |
4262 | 0 | htmlAutoCloseOnEnd(ctxt); |
4263 | 0 | break; |
4264 | 0 | } |
4265 | | |
4266 | | /* |
4267 | | * Last case, text. Note that References are handled directly. |
4268 | | */ |
4269 | 0 | else { |
4270 | 0 | htmlParseCharData(ctxt); |
4271 | 0 | } |
4272 | | |
4273 | 0 | SHRINK; |
4274 | 0 | GROW; |
4275 | 0 | } |
4276 | 0 | if (currentNode != NULL) xmlFree(currentNode); |
4277 | 0 | } |
4278 | | |
4279 | | /** |
4280 | | * htmlParseElement: |
4281 | | * @ctxt: an HTML parser context |
4282 | | * |
4283 | | * DEPRECATED: Internal function, don't use. |
4284 | | * |
4285 | | * parse an HTML element, this is highly recursive |
4286 | | * this is kept for compatibility with previous code versions |
4287 | | * |
4288 | | * [39] element ::= EmptyElemTag | STag content ETag |
4289 | | * |
4290 | | * [41] Attribute ::= Name Eq AttValue |
4291 | | */ |
4292 | | |
4293 | | void |
4294 | 0 | htmlParseElement(htmlParserCtxtPtr ctxt) { |
4295 | 0 | const xmlChar *name; |
4296 | 0 | xmlChar *currentNode = NULL; |
4297 | 0 | const htmlElemDesc * info; |
4298 | 0 | htmlParserNodeInfo node_info; |
4299 | 0 | int failed; |
4300 | 0 | int depth; |
4301 | 0 | const xmlChar *oldptr; |
4302 | |
|
4303 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
4304 | 0 | return; |
4305 | | |
4306 | | /* Capture start position */ |
4307 | 0 | if (ctxt->record_info) { |
4308 | 0 | node_info.begin_pos = ctxt->input->consumed + |
4309 | 0 | (CUR_PTR - ctxt->input->base); |
4310 | 0 | node_info.begin_line = ctxt->input->line; |
4311 | 0 | } |
4312 | |
|
4313 | 0 | failed = htmlParseStartTag(ctxt); |
4314 | 0 | name = ctxt->name; |
4315 | 0 | if ((failed == -1) || (name == NULL)) { |
4316 | 0 | if (CUR == '>') |
4317 | 0 | NEXT; |
4318 | 0 | return; |
4319 | 0 | } |
4320 | | |
4321 | | /* |
4322 | | * Lookup the info for that element. |
4323 | | */ |
4324 | 0 | info = htmlTagLookup(name); |
4325 | 0 | if (info == NULL) { |
4326 | 0 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
4327 | 0 | "Tag %s invalid\n", name, NULL); |
4328 | 0 | } |
4329 | | |
4330 | | /* |
4331 | | * Check for an Empty Element labeled the XML/SGML way |
4332 | | */ |
4333 | 0 | if ((CUR == '/') && (NXT(1) == '>')) { |
4334 | 0 | SKIP(2); |
4335 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
4336 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
4337 | 0 | htmlnamePop(ctxt); |
4338 | 0 | return; |
4339 | 0 | } |
4340 | | |
4341 | 0 | if (CUR == '>') { |
4342 | 0 | NEXT; |
4343 | 0 | } else { |
4344 | 0 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
4345 | 0 | "Couldn't find end of Start Tag %s\n", name, NULL); |
4346 | | |
4347 | | /* |
4348 | | * end of parsing of this node. |
4349 | | */ |
4350 | 0 | if (xmlStrEqual(name, ctxt->name)) { |
4351 | 0 | nodePop(ctxt); |
4352 | 0 | htmlnamePop(ctxt); |
4353 | 0 | } |
4354 | | |
4355 | | /* |
4356 | | * Capture end position and add node |
4357 | | */ |
4358 | 0 | if (ctxt->record_info) { |
4359 | 0 | node_info.end_pos = ctxt->input->consumed + |
4360 | 0 | (CUR_PTR - ctxt->input->base); |
4361 | 0 | node_info.end_line = ctxt->input->line; |
4362 | 0 | node_info.node = ctxt->node; |
4363 | 0 | xmlParserAddNodeInfo(ctxt, &node_info); |
4364 | 0 | } |
4365 | 0 | return; |
4366 | 0 | } |
4367 | | |
4368 | | /* |
4369 | | * Check for an Empty Element from DTD definition |
4370 | | */ |
4371 | 0 | if ((info != NULL) && (info->empty)) { |
4372 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
4373 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
4374 | 0 | htmlnamePop(ctxt); |
4375 | 0 | return; |
4376 | 0 | } |
4377 | | |
4378 | | /* |
4379 | | * Parse the content of the element: |
4380 | | */ |
4381 | 0 | currentNode = xmlStrdup(ctxt->name); |
4382 | 0 | depth = ctxt->nameNr; |
4383 | 0 | while (CUR != 0) { |
4384 | 0 | oldptr = ctxt->input->cur; |
4385 | 0 | htmlParseContent(ctxt); |
4386 | 0 | if (oldptr==ctxt->input->cur) break; |
4387 | 0 | if (ctxt->nameNr < depth) break; |
4388 | 0 | } |
4389 | | |
4390 | | /* |
4391 | | * Capture end position and add node |
4392 | | */ |
4393 | 0 | if ( currentNode != NULL && ctxt->record_info ) { |
4394 | 0 | node_info.end_pos = ctxt->input->consumed + |
4395 | 0 | (CUR_PTR - ctxt->input->base); |
4396 | 0 | node_info.end_line = ctxt->input->line; |
4397 | 0 | node_info.node = ctxt->node; |
4398 | 0 | xmlParserAddNodeInfo(ctxt, &node_info); |
4399 | 0 | } |
4400 | 0 | if (CUR == 0) { |
4401 | 0 | htmlAutoCloseOnEnd(ctxt); |
4402 | 0 | } |
4403 | |
|
4404 | 0 | if (currentNode != NULL) |
4405 | 0 | xmlFree(currentNode); |
4406 | 0 | } |
4407 | | |
4408 | | static void |
4409 | 0 | htmlParserFinishElementParsing(htmlParserCtxtPtr ctxt) { |
4410 | | /* |
4411 | | * Capture end position and add node |
4412 | | */ |
4413 | 0 | if ( ctxt->node != NULL && ctxt->record_info ) { |
4414 | 0 | ctxt->nodeInfo->end_pos = ctxt->input->consumed + |
4415 | 0 | (CUR_PTR - ctxt->input->base); |
4416 | 0 | ctxt->nodeInfo->end_line = ctxt->input->line; |
4417 | 0 | ctxt->nodeInfo->node = ctxt->node; |
4418 | 0 | xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo); |
4419 | 0 | htmlNodeInfoPop(ctxt); |
4420 | 0 | } |
4421 | 0 | if (CUR == 0) { |
4422 | 0 | htmlAutoCloseOnEnd(ctxt); |
4423 | 0 | } |
4424 | 0 | } |
4425 | | |
4426 | | /** |
4427 | | * htmlParseElementInternal: |
4428 | | * @ctxt: an HTML parser context |
4429 | | * |
4430 | | * parse an HTML element, new version, non recursive |
4431 | | * |
4432 | | * [39] element ::= EmptyElemTag | STag content ETag |
4433 | | * |
4434 | | * [41] Attribute ::= Name Eq AttValue |
4435 | | */ |
4436 | | |
4437 | | static void |
4438 | 0 | htmlParseElementInternal(htmlParserCtxtPtr ctxt) { |
4439 | 0 | const xmlChar *name; |
4440 | 0 | const htmlElemDesc * info; |
4441 | 0 | htmlParserNodeInfo node_info = { NULL, 0, 0, 0, 0 }; |
4442 | 0 | int failed; |
4443 | |
|
4444 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
4445 | 0 | return; |
4446 | | |
4447 | | /* Capture start position */ |
4448 | 0 | if (ctxt->record_info) { |
4449 | 0 | node_info.begin_pos = ctxt->input->consumed + |
4450 | 0 | (CUR_PTR - ctxt->input->base); |
4451 | 0 | node_info.begin_line = ctxt->input->line; |
4452 | 0 | } |
4453 | |
|
4454 | 0 | failed = htmlParseStartTag(ctxt); |
4455 | 0 | name = ctxt->name; |
4456 | 0 | if ((failed == -1) || (name == NULL)) { |
4457 | 0 | if (CUR == '>') |
4458 | 0 | NEXT; |
4459 | 0 | return; |
4460 | 0 | } |
4461 | | |
4462 | | /* |
4463 | | * Lookup the info for that element. |
4464 | | */ |
4465 | 0 | info = htmlTagLookup(name); |
4466 | 0 | if (info == NULL) { |
4467 | 0 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
4468 | 0 | "Tag %s invalid\n", name, NULL); |
4469 | 0 | } |
4470 | | |
4471 | | /* |
4472 | | * Check for an Empty Element labeled the XML/SGML way |
4473 | | */ |
4474 | 0 | if ((CUR == '/') && (NXT(1) == '>')) { |
4475 | 0 | SKIP(2); |
4476 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
4477 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
4478 | 0 | htmlnamePop(ctxt); |
4479 | 0 | return; |
4480 | 0 | } |
4481 | | |
4482 | 0 | if (CUR == '>') { |
4483 | 0 | NEXT; |
4484 | 0 | } else { |
4485 | 0 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
4486 | 0 | "Couldn't find end of Start Tag %s\n", name, NULL); |
4487 | | |
4488 | | /* |
4489 | | * end of parsing of this node. |
4490 | | */ |
4491 | 0 | if (xmlStrEqual(name, ctxt->name)) { |
4492 | 0 | nodePop(ctxt); |
4493 | 0 | htmlnamePop(ctxt); |
4494 | 0 | } |
4495 | |
|
4496 | 0 | if (ctxt->record_info) |
4497 | 0 | htmlNodeInfoPush(ctxt, &node_info); |
4498 | 0 | htmlParserFinishElementParsing(ctxt); |
4499 | 0 | return; |
4500 | 0 | } |
4501 | | |
4502 | | /* |
4503 | | * Check for an Empty Element from DTD definition |
4504 | | */ |
4505 | 0 | if ((info != NULL) && (info->empty)) { |
4506 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
4507 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
4508 | 0 | htmlnamePop(ctxt); |
4509 | 0 | return; |
4510 | 0 | } |
4511 | | |
4512 | 0 | if (ctxt->record_info) |
4513 | 0 | htmlNodeInfoPush(ctxt, &node_info); |
4514 | 0 | } |
4515 | | |
4516 | | /** |
4517 | | * htmlParseContentInternal: |
4518 | | * @ctxt: an HTML parser context |
4519 | | * |
4520 | | * Parse a content: comment, sub-element, reference or text. |
4521 | | * New version for non recursive htmlParseElementInternal |
4522 | | */ |
4523 | | |
4524 | | static void |
4525 | 0 | htmlParseContentInternal(htmlParserCtxtPtr ctxt) { |
4526 | 0 | xmlChar *currentNode; |
4527 | 0 | int depth; |
4528 | 0 | const xmlChar *name; |
4529 | |
|
4530 | 0 | depth = ctxt->nameNr; |
4531 | 0 | if (depth <= 0) { |
4532 | 0 | currentNode = NULL; |
4533 | 0 | } else { |
4534 | 0 | currentNode = xmlStrdup(ctxt->name); |
4535 | 0 | if (currentNode == NULL) { |
4536 | 0 | htmlErrMemory(ctxt); |
4537 | 0 | return; |
4538 | 0 | } |
4539 | 0 | } |
4540 | 0 | while (PARSER_STOPPED(ctxt) == 0) { |
4541 | 0 | GROW; |
4542 | | |
4543 | | /* |
4544 | | * Our tag or one of it's parent or children is ending. |
4545 | | */ |
4546 | 0 | if ((CUR == '<') && (NXT(1) == '/')) { |
4547 | 0 | if (htmlParseEndTag(ctxt) && |
4548 | 0 | ((currentNode != NULL) || (ctxt->nameNr == 0))) { |
4549 | 0 | if (currentNode != NULL) |
4550 | 0 | xmlFree(currentNode); |
4551 | |
|
4552 | 0 | depth = ctxt->nameNr; |
4553 | 0 | if (depth <= 0) { |
4554 | 0 | currentNode = NULL; |
4555 | 0 | } else { |
4556 | 0 | currentNode = xmlStrdup(ctxt->name); |
4557 | 0 | if (currentNode == NULL) { |
4558 | 0 | htmlErrMemory(ctxt); |
4559 | 0 | break; |
4560 | 0 | } |
4561 | 0 | } |
4562 | 0 | } |
4563 | 0 | continue; /* while */ |
4564 | 0 | } |
4565 | | |
4566 | 0 | else if ((CUR == '<') && |
4567 | 0 | ((IS_ASCII_LETTER(NXT(1))) || |
4568 | 0 | (NXT(1) == '_') || (NXT(1) == ':'))) { |
4569 | 0 | name = htmlParseHTMLName_nonInvasive(ctxt); |
4570 | 0 | if (name == NULL) { |
4571 | 0 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
4572 | 0 | "htmlParseStartTag: invalid element name\n", |
4573 | 0 | NULL, NULL); |
4574 | | /* Dump the bogus tag like browsers do */ |
4575 | 0 | while ((CUR == 0) && (CUR != '>')) |
4576 | 0 | NEXT; |
4577 | |
|
4578 | 0 | htmlParserFinishElementParsing(ctxt); |
4579 | 0 | if (currentNode != NULL) |
4580 | 0 | xmlFree(currentNode); |
4581 | |
|
4582 | 0 | if (ctxt->name == NULL) { |
4583 | 0 | currentNode = NULL; |
4584 | 0 | } else { |
4585 | 0 | currentNode = xmlStrdup(ctxt->name); |
4586 | 0 | if (currentNode == NULL) { |
4587 | 0 | htmlErrMemory(ctxt); |
4588 | 0 | break; |
4589 | 0 | } |
4590 | 0 | } |
4591 | 0 | depth = ctxt->nameNr; |
4592 | 0 | continue; |
4593 | 0 | } |
4594 | | |
4595 | 0 | if (ctxt->name != NULL) { |
4596 | 0 | if (htmlCheckAutoClose(name, ctxt->name) == 1) { |
4597 | 0 | htmlAutoClose(ctxt, name); |
4598 | 0 | continue; |
4599 | 0 | } |
4600 | 0 | } |
4601 | 0 | } |
4602 | | |
4603 | | /* |
4604 | | * Has this node been popped out during parsing of |
4605 | | * the next element |
4606 | | */ |
4607 | 0 | if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) && |
4608 | 0 | (!xmlStrEqual(currentNode, ctxt->name))) |
4609 | 0 | { |
4610 | 0 | htmlParserFinishElementParsing(ctxt); |
4611 | 0 | if (currentNode != NULL) xmlFree(currentNode); |
4612 | |
|
4613 | 0 | if (ctxt->name == NULL) { |
4614 | 0 | currentNode = NULL; |
4615 | 0 | } else { |
4616 | 0 | currentNode = xmlStrdup(ctxt->name); |
4617 | 0 | if (currentNode == NULL) { |
4618 | 0 | htmlErrMemory(ctxt); |
4619 | 0 | break; |
4620 | 0 | } |
4621 | 0 | } |
4622 | 0 | depth = ctxt->nameNr; |
4623 | 0 | continue; |
4624 | 0 | } |
4625 | | |
4626 | 0 | if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) || |
4627 | 0 | (xmlStrEqual(currentNode, BAD_CAST"style")))) { |
4628 | | /* |
4629 | | * Handle SCRIPT/STYLE separately |
4630 | | */ |
4631 | 0 | htmlParseScript(ctxt); |
4632 | 0 | } |
4633 | | |
4634 | 0 | else if ((CUR == '<') && (NXT(1) == '!')) { |
4635 | | /* |
4636 | | * Sometimes DOCTYPE arrives in the middle of the document |
4637 | | */ |
4638 | 0 | if ((UPP(2) == 'D') && (UPP(3) == 'O') && |
4639 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
4640 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
4641 | 0 | (UPP(8) == 'E')) { |
4642 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
4643 | 0 | "Misplaced DOCTYPE declaration\n", |
4644 | 0 | BAD_CAST "DOCTYPE" , NULL); |
4645 | 0 | htmlParseDocTypeDecl(ctxt); |
4646 | 0 | } |
4647 | | /* |
4648 | | * First case : a comment |
4649 | | */ |
4650 | 0 | else if ((NXT(2) == '-') && (NXT(3) == '-')) { |
4651 | 0 | htmlParseComment(ctxt); |
4652 | 0 | } |
4653 | 0 | else { |
4654 | 0 | htmlSkipBogusComment(ctxt); |
4655 | 0 | } |
4656 | 0 | } |
4657 | | |
4658 | | /* |
4659 | | * Second case : a Processing Instruction. |
4660 | | */ |
4661 | 0 | else if ((CUR == '<') && (NXT(1) == '?')) { |
4662 | 0 | htmlParsePI(ctxt); |
4663 | 0 | } |
4664 | | |
4665 | | /* |
4666 | | * Third case : a sub-element. |
4667 | | */ |
4668 | 0 | else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) { |
4669 | 0 | htmlParseElementInternal(ctxt); |
4670 | 0 | if (currentNode != NULL) xmlFree(currentNode); |
4671 | |
|
4672 | 0 | if (ctxt->name == NULL) { |
4673 | 0 | currentNode = NULL; |
4674 | 0 | } else { |
4675 | 0 | currentNode = xmlStrdup(ctxt->name); |
4676 | 0 | if (currentNode == NULL) { |
4677 | 0 | htmlErrMemory(ctxt); |
4678 | 0 | break; |
4679 | 0 | } |
4680 | 0 | } |
4681 | 0 | depth = ctxt->nameNr; |
4682 | 0 | } |
4683 | 0 | else if (CUR == '<') { |
4684 | 0 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
4685 | 0 | (ctxt->sax->characters != NULL)) |
4686 | 0 | ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1); |
4687 | 0 | NEXT; |
4688 | 0 | } |
4689 | | |
4690 | | /* |
4691 | | * Fourth case : a reference. If if has not been resolved, |
4692 | | * parsing returns it's Name, create the node |
4693 | | */ |
4694 | 0 | else if (CUR == '&') { |
4695 | 0 | htmlParseReference(ctxt); |
4696 | 0 | } |
4697 | | |
4698 | | /* |
4699 | | * Fifth case : end of the resource |
4700 | | */ |
4701 | 0 | else if (CUR == 0) { |
4702 | 0 | htmlAutoCloseOnEnd(ctxt); |
4703 | 0 | break; |
4704 | 0 | } |
4705 | | |
4706 | | /* |
4707 | | * Last case, text. Note that References are handled directly. |
4708 | | */ |
4709 | 0 | else { |
4710 | 0 | htmlParseCharData(ctxt); |
4711 | 0 | } |
4712 | | |
4713 | 0 | SHRINK; |
4714 | 0 | GROW; |
4715 | 0 | } |
4716 | 0 | if (currentNode != NULL) xmlFree(currentNode); |
4717 | 0 | } |
4718 | | |
4719 | | xmlNodePtr |
4720 | 0 | htmlCtxtParseContentInternal(htmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
4721 | 0 | xmlNodePtr root; |
4722 | 0 | xmlNodePtr list = NULL; |
4723 | 0 | xmlChar *rootName = BAD_CAST "#root"; |
4724 | |
|
4725 | 0 | root = xmlNewDocNode(ctxt->myDoc, NULL, rootName, NULL); |
4726 | 0 | if (root == NULL) { |
4727 | 0 | htmlErrMemory(ctxt); |
4728 | 0 | return(NULL); |
4729 | 0 | } |
4730 | | |
4731 | 0 | if (xmlPushInput(ctxt, input) < 0) { |
4732 | 0 | xmlFreeNode(root); |
4733 | 0 | return(NULL); |
4734 | 0 | } |
4735 | | |
4736 | 0 | htmlnamePush(ctxt, rootName); |
4737 | 0 | nodePush(ctxt, root); |
4738 | |
|
4739 | 0 | htmlParseContentInternal(ctxt); |
4740 | | |
4741 | | /* TODO: Use xmlCtxtIsCatastrophicError */ |
4742 | 0 | if (ctxt->errNo != XML_ERR_NO_MEMORY) { |
4743 | 0 | xmlNodePtr cur; |
4744 | | |
4745 | | /* |
4746 | | * Unlink newly created node list. |
4747 | | */ |
4748 | 0 | list = root->children; |
4749 | 0 | root->children = NULL; |
4750 | 0 | root->last = NULL; |
4751 | 0 | for (cur = list; cur != NULL; cur = cur->next) |
4752 | 0 | cur->parent = NULL; |
4753 | 0 | } |
4754 | |
|
4755 | 0 | nodePop(ctxt); |
4756 | 0 | htmlnamePop(ctxt); |
4757 | | |
4758 | | /* xmlPopInput would free the stream */ |
4759 | 0 | inputPop(ctxt); |
4760 | |
|
4761 | 0 | xmlFreeNode(root); |
4762 | 0 | return(list); |
4763 | 0 | } |
4764 | | |
4765 | | /** |
4766 | | * htmlParseDocument: |
4767 | | * @ctxt: an HTML parser context |
4768 | | * |
4769 | | * Parse an HTML document and invoke the SAX handlers. This is useful |
4770 | | * if you're only interested in custom SAX callbacks. If you want a |
4771 | | * document tree, use htmlCtxtParseDocument. |
4772 | | * |
4773 | | * Returns 0, -1 in case of error. |
4774 | | */ |
4775 | | |
4776 | | int |
4777 | 0 | htmlParseDocument(htmlParserCtxtPtr ctxt) { |
4778 | 0 | xmlDtdPtr dtd; |
4779 | |
|
4780 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
4781 | 0 | return(-1); |
4782 | | |
4783 | 0 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) { |
4784 | 0 | ctxt->sax->setDocumentLocator(ctxt->userData, |
4785 | 0 | (xmlSAXLocator *) &xmlDefaultSAXLocator); |
4786 | 0 | } |
4787 | |
|
4788 | 0 | xmlDetectEncoding(ctxt); |
4789 | | |
4790 | | /* |
4791 | | * This is wrong but matches long-standing behavior. In most cases, |
4792 | | * a document starting with an XML declaration will specify UTF-8. |
4793 | | */ |
4794 | 0 | if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) && |
4795 | 0 | (xmlStrncmp(ctxt->input->cur, BAD_CAST "<?xm", 4) == 0)) |
4796 | 0 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_UTF8); |
4797 | | |
4798 | | /* |
4799 | | * Wipe out everything which is before the first '<' |
4800 | | */ |
4801 | 0 | SKIP_BLANKS; |
4802 | 0 | if (CUR == 0) { |
4803 | 0 | htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY, |
4804 | 0 | "Document is empty\n", NULL, NULL); |
4805 | 0 | } |
4806 | |
|
4807 | 0 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
4808 | 0 | ctxt->sax->startDocument(ctxt->userData); |
4809 | | |
4810 | | /* |
4811 | | * Parse possible comments and PIs before any content |
4812 | | */ |
4813 | 0 | while (((CUR == '<') && (NXT(1) == '!') && |
4814 | 0 | (NXT(2) == '-') && (NXT(3) == '-')) || |
4815 | 0 | ((CUR == '<') && (NXT(1) == '?'))) { |
4816 | 0 | htmlParseComment(ctxt); |
4817 | 0 | htmlParsePI(ctxt); |
4818 | 0 | SKIP_BLANKS; |
4819 | 0 | } |
4820 | | |
4821 | | |
4822 | | /* |
4823 | | * Then possibly doc type declaration(s) and more Misc |
4824 | | * (doctypedecl Misc*)? |
4825 | | */ |
4826 | 0 | if ((CUR == '<') && (NXT(1) == '!') && |
4827 | 0 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
4828 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
4829 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
4830 | 0 | (UPP(8) == 'E')) { |
4831 | 0 | htmlParseDocTypeDecl(ctxt); |
4832 | 0 | } |
4833 | 0 | SKIP_BLANKS; |
4834 | | |
4835 | | /* |
4836 | | * Parse possible comments and PIs before any content |
4837 | | */ |
4838 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
4839 | 0 | (((CUR == '<') && (NXT(1) == '!') && |
4840 | 0 | (NXT(2) == '-') && (NXT(3) == '-')) || |
4841 | 0 | ((CUR == '<') && (NXT(1) == '?')))) { |
4842 | 0 | htmlParseComment(ctxt); |
4843 | 0 | htmlParsePI(ctxt); |
4844 | 0 | SKIP_BLANKS; |
4845 | 0 | } |
4846 | | |
4847 | | /* |
4848 | | * Time to start parsing the tree itself |
4849 | | */ |
4850 | 0 | htmlParseContentInternal(ctxt); |
4851 | | |
4852 | | /* |
4853 | | * autoclose |
4854 | | */ |
4855 | 0 | if (CUR == 0) |
4856 | 0 | htmlAutoCloseOnEnd(ctxt); |
4857 | | |
4858 | | |
4859 | | /* |
4860 | | * SAX: end of the document processing. |
4861 | | */ |
4862 | 0 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
4863 | 0 | ctxt->sax->endDocument(ctxt->userData); |
4864 | |
|
4865 | 0 | if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL)) { |
4866 | 0 | dtd = xmlGetIntSubset(ctxt->myDoc); |
4867 | 0 | if (dtd == NULL) { |
4868 | 0 | ctxt->myDoc->intSubset = |
4869 | 0 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
4870 | 0 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
4871 | 0 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
4872 | 0 | if (ctxt->myDoc->intSubset == NULL) |
4873 | 0 | htmlErrMemory(ctxt); |
4874 | 0 | } |
4875 | 0 | } |
4876 | 0 | if (! ctxt->wellFormed) return(-1); |
4877 | 0 | return(0); |
4878 | 0 | } |
4879 | | |
4880 | | |
4881 | | /************************************************************************ |
4882 | | * * |
4883 | | * Parser contexts handling * |
4884 | | * * |
4885 | | ************************************************************************/ |
4886 | | |
4887 | | /** |
4888 | | * htmlInitParserCtxt: |
4889 | | * @ctxt: an HTML parser context |
4890 | | * @sax: SAX handler |
4891 | | * @userData: user data |
4892 | | * |
4893 | | * Initialize a parser context |
4894 | | * |
4895 | | * Returns 0 in case of success and -1 in case of error |
4896 | | */ |
4897 | | |
4898 | | static int |
4899 | | htmlInitParserCtxt(htmlParserCtxtPtr ctxt, const htmlSAXHandler *sax, |
4900 | | void *userData) |
4901 | 0 | { |
4902 | 0 | if (ctxt == NULL) return(-1); |
4903 | 0 | memset(ctxt, 0, sizeof(htmlParserCtxt)); |
4904 | |
|
4905 | 0 | ctxt->dict = xmlDictCreate(); |
4906 | 0 | if (ctxt->dict == NULL) |
4907 | 0 | return(-1); |
4908 | | |
4909 | 0 | if (ctxt->sax == NULL) |
4910 | 0 | ctxt->sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler)); |
4911 | 0 | if (ctxt->sax == NULL) |
4912 | 0 | return(-1); |
4913 | 0 | if (sax == NULL) { |
4914 | 0 | memset(ctxt->sax, 0, sizeof(htmlSAXHandler)); |
4915 | 0 | xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax); |
4916 | 0 | ctxt->userData = ctxt; |
4917 | 0 | } else { |
4918 | 0 | memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler)); |
4919 | 0 | ctxt->userData = userData ? userData : ctxt; |
4920 | 0 | } |
4921 | | |
4922 | | /* Allocate the Input stack */ |
4923 | 0 | ctxt->inputTab = (htmlParserInputPtr *) |
4924 | 0 | xmlMalloc(5 * sizeof(htmlParserInputPtr)); |
4925 | 0 | if (ctxt->inputTab == NULL) |
4926 | 0 | return(-1); |
4927 | 0 | ctxt->inputNr = 0; |
4928 | 0 | ctxt->inputMax = 5; |
4929 | 0 | ctxt->input = NULL; |
4930 | 0 | ctxt->version = NULL; |
4931 | 0 | ctxt->encoding = NULL; |
4932 | 0 | ctxt->standalone = -1; |
4933 | 0 | ctxt->instate = XML_PARSER_START; |
4934 | | |
4935 | | /* Allocate the Node stack */ |
4936 | 0 | ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr)); |
4937 | 0 | if (ctxt->nodeTab == NULL) |
4938 | 0 | return(-1); |
4939 | 0 | ctxt->nodeNr = 0; |
4940 | 0 | ctxt->nodeMax = 10; |
4941 | 0 | ctxt->node = NULL; |
4942 | | |
4943 | | /* Allocate the Name stack */ |
4944 | 0 | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
4945 | 0 | if (ctxt->nameTab == NULL) |
4946 | 0 | return(-1); |
4947 | 0 | ctxt->nameNr = 0; |
4948 | 0 | ctxt->nameMax = 10; |
4949 | 0 | ctxt->name = NULL; |
4950 | |
|
4951 | 0 | ctxt->nodeInfoTab = NULL; |
4952 | 0 | ctxt->nodeInfoNr = 0; |
4953 | 0 | ctxt->nodeInfoMax = 0; |
4954 | |
|
4955 | 0 | ctxt->myDoc = NULL; |
4956 | 0 | ctxt->wellFormed = 1; |
4957 | 0 | ctxt->replaceEntities = 0; |
4958 | 0 | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
4959 | 0 | ctxt->keepBlanks = xmlKeepBlanksDefaultValue; |
4960 | 0 | ctxt->html = 1; |
4961 | 0 | ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT; |
4962 | 0 | ctxt->vctxt.userData = ctxt; |
4963 | 0 | ctxt->vctxt.error = xmlParserValidityError; |
4964 | 0 | ctxt->vctxt.warning = xmlParserValidityWarning; |
4965 | 0 | ctxt->record_info = 0; |
4966 | 0 | ctxt->validate = 0; |
4967 | 0 | ctxt->checkIndex = 0; |
4968 | 0 | ctxt->catalogs = NULL; |
4969 | 0 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
4970 | 0 | return(0); |
4971 | 0 | } |
4972 | | |
4973 | | /** |
4974 | | * htmlFreeParserCtxt: |
4975 | | * @ctxt: an HTML parser context |
4976 | | * |
4977 | | * Free all the memory used by a parser context. However the parsed |
4978 | | * document in ctxt->myDoc is not freed. |
4979 | | */ |
4980 | | |
4981 | | void |
4982 | | htmlFreeParserCtxt(htmlParserCtxtPtr ctxt) |
4983 | 0 | { |
4984 | 0 | xmlFreeParserCtxt(ctxt); |
4985 | 0 | } |
4986 | | |
4987 | | /** |
4988 | | * htmlNewParserCtxt: |
4989 | | * |
4990 | | * Allocate and initialize a new HTML parser context. |
4991 | | * |
4992 | | * This can be used to parse HTML documents into DOM trees with |
4993 | | * functions like xmlCtxtReadFile or xmlCtxtReadMemory. |
4994 | | * |
4995 | | * See htmlCtxtUseOptions for parser options. |
4996 | | * |
4997 | | * See xmlCtxtSetErrorHandler for advanced error handling. |
4998 | | * |
4999 | | * See htmlNewSAXParserCtxt for custom SAX parsers. |
5000 | | * |
5001 | | * Returns the htmlParserCtxtPtr or NULL in case of allocation error |
5002 | | */ |
5003 | | |
5004 | | htmlParserCtxtPtr |
5005 | | htmlNewParserCtxt(void) |
5006 | 0 | { |
5007 | 0 | return(htmlNewSAXParserCtxt(NULL, NULL)); |
5008 | 0 | } |
5009 | | |
5010 | | /** |
5011 | | * htmlNewSAXParserCtxt: |
5012 | | * @sax: SAX handler |
5013 | | * @userData: user data |
5014 | | * |
5015 | | * Allocate and initialize a new HTML SAX parser context. If userData |
5016 | | * is NULL, the parser context will be passed as user data. |
5017 | | * |
5018 | | * Available since 2.11.0. If you want support older versions, |
5019 | | * it's best to invoke htmlNewParserCtxt and set ctxt->sax with |
5020 | | * struct assignment. |
5021 | | * |
5022 | | * Also see htmlNewParserCtxt. |
5023 | | * |
5024 | | * Returns the htmlParserCtxtPtr or NULL in case of allocation error |
5025 | | */ |
5026 | | |
5027 | | htmlParserCtxtPtr |
5028 | | htmlNewSAXParserCtxt(const htmlSAXHandler *sax, void *userData) |
5029 | 0 | { |
5030 | 0 | xmlParserCtxtPtr ctxt; |
5031 | |
|
5032 | 0 | xmlInitParser(); |
5033 | |
|
5034 | 0 | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
5035 | 0 | if (ctxt == NULL) |
5036 | 0 | return(NULL); |
5037 | 0 | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
5038 | 0 | if (htmlInitParserCtxt(ctxt, sax, userData) < 0) { |
5039 | 0 | htmlFreeParserCtxt(ctxt); |
5040 | 0 | return(NULL); |
5041 | 0 | } |
5042 | 0 | return(ctxt); |
5043 | 0 | } |
5044 | | |
5045 | | static htmlParserCtxtPtr |
5046 | | htmlCreateMemoryParserCtxtInternal(const char *url, |
5047 | | const char *buffer, size_t size, |
5048 | 0 | const char *encoding) { |
5049 | 0 | xmlParserCtxtPtr ctxt; |
5050 | 0 | xmlParserInputPtr input; |
5051 | |
|
5052 | 0 | if (buffer == NULL) |
5053 | 0 | return(NULL); |
5054 | | |
5055 | 0 | ctxt = htmlNewParserCtxt(); |
5056 | 0 | if (ctxt == NULL) |
5057 | 0 | return(NULL); |
5058 | | |
5059 | 0 | input = xmlCtxtNewInputFromMemory(ctxt, url, buffer, size, encoding, 0); |
5060 | 0 | if (input == NULL) { |
5061 | 0 | xmlFreeParserCtxt(ctxt); |
5062 | 0 | return(NULL); |
5063 | 0 | } |
5064 | | |
5065 | 0 | if (inputPush(ctxt, input) < 0) { |
5066 | 0 | xmlFreeInputStream(input); |
5067 | 0 | xmlFreeParserCtxt(ctxt); |
5068 | 0 | return(NULL); |
5069 | 0 | } |
5070 | | |
5071 | 0 | return(ctxt); |
5072 | 0 | } |
5073 | | |
5074 | | /** |
5075 | | * htmlCreateMemoryParserCtxt: |
5076 | | * @buffer: a pointer to a char array |
5077 | | * @size: the size of the array |
5078 | | * |
5079 | | * DEPRECATED: Use htmlNewParserCtxt and htmlCtxtReadMemory. |
5080 | | * |
5081 | | * Create a parser context for an HTML in-memory document. The input |
5082 | | * buffer must not contain any terminating null bytes. |
5083 | | * |
5084 | | * Returns the new parser context or NULL |
5085 | | */ |
5086 | | htmlParserCtxtPtr |
5087 | 0 | htmlCreateMemoryParserCtxt(const char *buffer, int size) { |
5088 | 0 | if (size <= 0) |
5089 | 0 | return(NULL); |
5090 | | |
5091 | 0 | return(htmlCreateMemoryParserCtxtInternal(NULL, buffer, size, NULL)); |
5092 | 0 | } |
5093 | | |
5094 | | /** |
5095 | | * htmlCreateDocParserCtxt: |
5096 | | * @str: a pointer to an array of xmlChar |
5097 | | * @encoding: encoding (optional) |
5098 | | * |
5099 | | * Create a parser context for a null-terminated string. |
5100 | | * |
5101 | | * Returns the new parser context or NULL if a memory allocation failed. |
5102 | | */ |
5103 | | static htmlParserCtxtPtr |
5104 | | htmlCreateDocParserCtxt(const xmlChar *str, const char *url, |
5105 | 0 | const char *encoding) { |
5106 | 0 | xmlParserCtxtPtr ctxt; |
5107 | 0 | xmlParserInputPtr input; |
5108 | |
|
5109 | 0 | if (str == NULL) |
5110 | 0 | return(NULL); |
5111 | | |
5112 | 0 | ctxt = htmlNewParserCtxt(); |
5113 | 0 | if (ctxt == NULL) |
5114 | 0 | return(NULL); |
5115 | | |
5116 | 0 | input = xmlCtxtNewInputFromString(ctxt, url, (const char *) str, |
5117 | 0 | encoding, 0); |
5118 | 0 | if (input == NULL) { |
5119 | 0 | xmlFreeParserCtxt(ctxt); |
5120 | 0 | return(NULL); |
5121 | 0 | } |
5122 | | |
5123 | 0 | if (inputPush(ctxt, input) < 0) { |
5124 | 0 | xmlFreeInputStream(input); |
5125 | 0 | xmlFreeParserCtxt(ctxt); |
5126 | 0 | return(NULL); |
5127 | 0 | } |
5128 | | |
5129 | 0 | return(ctxt); |
5130 | 0 | } |
5131 | | |
5132 | | #ifdef LIBXML_PUSH_ENABLED |
5133 | | /************************************************************************ |
5134 | | * * |
5135 | | * Progressive parsing interfaces * |
5136 | | * * |
5137 | | ************************************************************************/ |
5138 | | |
5139 | | /** |
5140 | | * htmlParseLookupSequence: |
5141 | | * @ctxt: an HTML parser context |
5142 | | * @first: the first char to lookup |
5143 | | * @next: the next char to lookup or zero |
5144 | | * @third: the next char to lookup or zero |
5145 | | * @ignoreattrval: skip over attribute values |
5146 | | * |
5147 | | * Try to find if a sequence (first, next, third) or just (first next) or |
5148 | | * (first) is available in the input stream. |
5149 | | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
5150 | | * to avoid rescanning sequences of bytes, it DOES change the state of the |
5151 | | * parser, do not use liberally. |
5152 | | * This is basically similar to xmlParseLookupSequence() |
5153 | | * |
5154 | | * Returns the index to the current parsing point if the full sequence |
5155 | | * is available, -1 otherwise. |
5156 | | */ |
5157 | | static int |
5158 | | htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first, |
5159 | | xmlChar next, xmlChar third, int ignoreattrval) |
5160 | 0 | { |
5161 | 0 | size_t base, len; |
5162 | 0 | htmlParserInputPtr in; |
5163 | 0 | const xmlChar *buf; |
5164 | 0 | int quote; |
5165 | |
|
5166 | 0 | in = ctxt->input; |
5167 | 0 | if (in == NULL) |
5168 | 0 | return (-1); |
5169 | | |
5170 | 0 | base = ctxt->checkIndex; |
5171 | 0 | quote = ctxt->endCheckState; |
5172 | |
|
5173 | 0 | buf = in->cur; |
5174 | 0 | len = in->end - in->cur; |
5175 | | |
5176 | | /* take into account the sequence length */ |
5177 | 0 | if (third) |
5178 | 0 | len -= 2; |
5179 | 0 | else if (next) |
5180 | 0 | len--; |
5181 | 0 | for (; base < len; base++) { |
5182 | 0 | if (base >= INT_MAX / 2) { |
5183 | 0 | ctxt->checkIndex = 0; |
5184 | 0 | ctxt->endCheckState = 0; |
5185 | 0 | return (base - 2); |
5186 | 0 | } |
5187 | 0 | if (ignoreattrval) { |
5188 | 0 | if (quote) { |
5189 | 0 | if (buf[base] == quote) |
5190 | 0 | quote = 0; |
5191 | 0 | continue; |
5192 | 0 | } |
5193 | 0 | if (buf[base] == '"' || buf[base] == '\'') { |
5194 | 0 | quote = buf[base]; |
5195 | 0 | continue; |
5196 | 0 | } |
5197 | 0 | } |
5198 | 0 | if (buf[base] == first) { |
5199 | 0 | if (third != 0) { |
5200 | 0 | if ((buf[base + 1] != next) || (buf[base + 2] != third)) |
5201 | 0 | continue; |
5202 | 0 | } else if (next != 0) { |
5203 | 0 | if (buf[base + 1] != next) |
5204 | 0 | continue; |
5205 | 0 | } |
5206 | 0 | ctxt->checkIndex = 0; |
5207 | 0 | ctxt->endCheckState = 0; |
5208 | 0 | return (base); |
5209 | 0 | } |
5210 | 0 | } |
5211 | 0 | ctxt->checkIndex = base; |
5212 | 0 | ctxt->endCheckState = quote; |
5213 | 0 | return (-1); |
5214 | 0 | } |
5215 | | |
5216 | | /** |
5217 | | * htmlParseLookupCommentEnd: |
5218 | | * @ctxt: an HTML parser context |
5219 | | * |
5220 | | * Try to find a comment end tag in the input stream |
5221 | | * The search includes "-->" as well as WHATWG-recommended incorrectly-closed tags. |
5222 | | * (See https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment) |
5223 | | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
5224 | | * to avoid rescanning sequences of bytes, it DOES change the state of the |
5225 | | * parser, do not use liberally. |
5226 | | * This wraps to htmlParseLookupSequence() |
5227 | | * |
5228 | | * Returns the index to the current parsing point if the full sequence is available, -1 otherwise. |
5229 | | */ |
5230 | | static int |
5231 | | htmlParseLookupCommentEnd(htmlParserCtxtPtr ctxt) |
5232 | 0 | { |
5233 | 0 | int mark = 0; |
5234 | 0 | int offset; |
5235 | |
|
5236 | 0 | while (1) { |
5237 | 0 | mark = htmlParseLookupSequence(ctxt, '-', '-', 0, 0); |
5238 | 0 | if (mark < 0) |
5239 | 0 | break; |
5240 | 0 | if ((NXT(mark+2) == '>') || |
5241 | 0 | ((NXT(mark+2) == '!') && (NXT(mark+3) == '>'))) { |
5242 | 0 | ctxt->checkIndex = 0; |
5243 | 0 | break; |
5244 | 0 | } |
5245 | 0 | offset = (NXT(mark+2) == '!') ? 3 : 2; |
5246 | 0 | if (mark + offset >= ctxt->input->end - ctxt->input->cur) { |
5247 | 0 | ctxt->checkIndex = mark; |
5248 | 0 | return(-1); |
5249 | 0 | } |
5250 | 0 | ctxt->checkIndex = mark + 1; |
5251 | 0 | } |
5252 | 0 | return mark; |
5253 | 0 | } |
5254 | | |
5255 | | |
5256 | | /** |
5257 | | * htmlParseTryOrFinish: |
5258 | | * @ctxt: an HTML parser context |
5259 | | * @terminate: last chunk indicator |
5260 | | * |
5261 | | * Try to progress on parsing |
5262 | | * |
5263 | | * Returns zero if no parsing was possible |
5264 | | */ |
5265 | | static int |
5266 | 0 | htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) { |
5267 | 0 | int ret = 0; |
5268 | 0 | htmlParserInputPtr in; |
5269 | 0 | ptrdiff_t avail = 0; |
5270 | 0 | xmlChar cur, next; |
5271 | |
|
5272 | 0 | htmlParserNodeInfo node_info; |
5273 | |
|
5274 | 0 | while (PARSER_STOPPED(ctxt) == 0) { |
5275 | |
|
5276 | 0 | in = ctxt->input; |
5277 | 0 | if (in == NULL) break; |
5278 | 0 | avail = in->end - in->cur; |
5279 | 0 | if ((avail == 0) && (terminate)) { |
5280 | 0 | htmlAutoCloseOnEnd(ctxt); |
5281 | 0 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
5282 | | /* |
5283 | | * SAX: end of the document processing. |
5284 | | */ |
5285 | 0 | ctxt->instate = XML_PARSER_EOF; |
5286 | 0 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
5287 | 0 | ctxt->sax->endDocument(ctxt->userData); |
5288 | 0 | } |
5289 | 0 | } |
5290 | 0 | if (avail < 1) |
5291 | 0 | goto done; |
5292 | | /* |
5293 | | * This is done to make progress and avoid an infinite loop |
5294 | | * if a parsing attempt was aborted by hitting a NUL byte. After |
5295 | | * changing htmlCurrentChar, this probably isn't necessary anymore. |
5296 | | * We should consider removing this check. |
5297 | | */ |
5298 | 0 | cur = in->cur[0]; |
5299 | 0 | if (cur == 0) { |
5300 | 0 | SKIP(1); |
5301 | 0 | continue; |
5302 | 0 | } |
5303 | | |
5304 | 0 | switch (ctxt->instate) { |
5305 | 0 | case XML_PARSER_EOF: |
5306 | | /* |
5307 | | * Document parsing is done ! |
5308 | | */ |
5309 | 0 | goto done; |
5310 | 0 | case XML_PARSER_START: |
5311 | | /* |
5312 | | * This is wrong but matches long-standing behavior. In most |
5313 | | * cases, a document starting with an XML declaration will |
5314 | | * specify UTF-8. |
5315 | | */ |
5316 | 0 | if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) && |
5317 | 0 | (xmlStrncmp(ctxt->input->cur, BAD_CAST "<?xm", 4) == 0)) { |
5318 | 0 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_UTF8); |
5319 | 0 | } |
5320 | | |
5321 | | /* |
5322 | | * Very first chars read from the document flow. |
5323 | | */ |
5324 | 0 | cur = in->cur[0]; |
5325 | 0 | if (IS_BLANK_CH(cur)) { |
5326 | 0 | SKIP_BLANKS; |
5327 | 0 | avail = in->end - in->cur; |
5328 | 0 | } |
5329 | 0 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) { |
5330 | 0 | ctxt->sax->setDocumentLocator(ctxt->userData, |
5331 | 0 | (xmlSAXLocator *) &xmlDefaultSAXLocator); |
5332 | 0 | } |
5333 | 0 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
5334 | 0 | (!ctxt->disableSAX)) |
5335 | 0 | ctxt->sax->startDocument(ctxt->userData); |
5336 | |
|
5337 | 0 | cur = in->cur[0]; |
5338 | 0 | next = in->cur[1]; |
5339 | 0 | if ((cur == '<') && (next == '!') && |
5340 | 0 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
5341 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
5342 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
5343 | 0 | (UPP(8) == 'E')) { |
5344 | 0 | if ((!terminate) && |
5345 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0)) |
5346 | 0 | goto done; |
5347 | 0 | htmlParseDocTypeDecl(ctxt); |
5348 | 0 | ctxt->instate = XML_PARSER_PROLOG; |
5349 | 0 | } else { |
5350 | 0 | ctxt->instate = XML_PARSER_MISC; |
5351 | 0 | } |
5352 | 0 | break; |
5353 | 0 | case XML_PARSER_MISC: |
5354 | 0 | SKIP_BLANKS; |
5355 | 0 | avail = in->end - in->cur; |
5356 | | /* |
5357 | | * no chars in buffer |
5358 | | */ |
5359 | 0 | if (avail < 1) |
5360 | 0 | goto done; |
5361 | | /* |
5362 | | * not enough chars in buffer |
5363 | | */ |
5364 | 0 | if (avail < 2) { |
5365 | 0 | if (!terminate) |
5366 | 0 | goto done; |
5367 | 0 | else |
5368 | 0 | next = ' '; |
5369 | 0 | } else { |
5370 | 0 | next = in->cur[1]; |
5371 | 0 | } |
5372 | 0 | cur = in->cur[0]; |
5373 | 0 | if ((cur == '<') && (next == '!') && |
5374 | 0 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
5375 | 0 | if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0)) |
5376 | 0 | goto done; |
5377 | 0 | htmlParseComment(ctxt); |
5378 | 0 | ctxt->instate = XML_PARSER_MISC; |
5379 | 0 | } else if ((cur == '<') && (next == '?')) { |
5380 | 0 | if ((!terminate) && |
5381 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5382 | 0 | goto done; |
5383 | 0 | htmlParsePI(ctxt); |
5384 | 0 | ctxt->instate = XML_PARSER_MISC; |
5385 | 0 | } else if ((cur == '<') && (next == '!') && |
5386 | 0 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
5387 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
5388 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
5389 | 0 | (UPP(8) == 'E')) { |
5390 | 0 | if ((!terminate) && |
5391 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0)) |
5392 | 0 | goto done; |
5393 | 0 | htmlParseDocTypeDecl(ctxt); |
5394 | 0 | ctxt->instate = XML_PARSER_PROLOG; |
5395 | 0 | } else if ((cur == '<') && (next == '!') && |
5396 | 0 | (avail < 9)) { |
5397 | 0 | goto done; |
5398 | 0 | } else { |
5399 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5400 | 0 | } |
5401 | 0 | break; |
5402 | 0 | case XML_PARSER_PROLOG: |
5403 | 0 | SKIP_BLANKS; |
5404 | 0 | avail = in->end - in->cur; |
5405 | 0 | if (avail < 2) |
5406 | 0 | goto done; |
5407 | 0 | cur = in->cur[0]; |
5408 | 0 | next = in->cur[1]; |
5409 | 0 | if ((cur == '<') && (next == '!') && |
5410 | 0 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
5411 | 0 | if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0)) |
5412 | 0 | goto done; |
5413 | 0 | htmlParseComment(ctxt); |
5414 | 0 | ctxt->instate = XML_PARSER_PROLOG; |
5415 | 0 | } else if ((cur == '<') && (next == '?')) { |
5416 | 0 | if ((!terminate) && |
5417 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5418 | 0 | goto done; |
5419 | 0 | htmlParsePI(ctxt); |
5420 | 0 | ctxt->instate = XML_PARSER_PROLOG; |
5421 | 0 | } else if ((cur == '<') && (next == '!') && |
5422 | 0 | (avail < 4)) { |
5423 | 0 | goto done; |
5424 | 0 | } else { |
5425 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5426 | 0 | } |
5427 | 0 | break; |
5428 | 0 | case XML_PARSER_EPILOG: |
5429 | 0 | avail = in->end - in->cur; |
5430 | 0 | if (avail < 1) |
5431 | 0 | goto done; |
5432 | 0 | cur = in->cur[0]; |
5433 | 0 | if (IS_BLANK_CH(cur)) { |
5434 | 0 | htmlParseCharData(ctxt); |
5435 | 0 | goto done; |
5436 | 0 | } |
5437 | 0 | if (avail < 2) |
5438 | 0 | goto done; |
5439 | 0 | next = in->cur[1]; |
5440 | 0 | if ((cur == '<') && (next == '!') && |
5441 | 0 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
5442 | 0 | if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0)) |
5443 | 0 | goto done; |
5444 | 0 | htmlParseComment(ctxt); |
5445 | 0 | ctxt->instate = XML_PARSER_EPILOG; |
5446 | 0 | } else if ((cur == '<') && (next == '?')) { |
5447 | 0 | if ((!terminate) && |
5448 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5449 | 0 | goto done; |
5450 | 0 | htmlParsePI(ctxt); |
5451 | 0 | ctxt->instate = XML_PARSER_EPILOG; |
5452 | 0 | } else if ((cur == '<') && (next == '!') && |
5453 | 0 | (avail < 4)) { |
5454 | 0 | goto done; |
5455 | 0 | } else { |
5456 | 0 | ctxt->errNo = XML_ERR_DOCUMENT_END; |
5457 | 0 | ctxt->wellFormed = 0; |
5458 | 0 | ctxt->instate = XML_PARSER_EOF; |
5459 | 0 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
5460 | 0 | ctxt->sax->endDocument(ctxt->userData); |
5461 | 0 | goto done; |
5462 | 0 | } |
5463 | 0 | break; |
5464 | 0 | case XML_PARSER_START_TAG: { |
5465 | 0 | const xmlChar *name; |
5466 | 0 | int failed; |
5467 | 0 | const htmlElemDesc * info; |
5468 | | |
5469 | | /* |
5470 | | * no chars in buffer |
5471 | | */ |
5472 | 0 | if (avail < 1) |
5473 | 0 | goto done; |
5474 | | /* |
5475 | | * not enough chars in buffer |
5476 | | */ |
5477 | 0 | if (avail < 2) { |
5478 | 0 | if (!terminate) |
5479 | 0 | goto done; |
5480 | 0 | else |
5481 | 0 | next = ' '; |
5482 | 0 | } else { |
5483 | 0 | next = in->cur[1]; |
5484 | 0 | } |
5485 | 0 | cur = in->cur[0]; |
5486 | 0 | if (cur != '<') { |
5487 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5488 | 0 | break; |
5489 | 0 | } |
5490 | 0 | if (next == '/') { |
5491 | 0 | ctxt->instate = XML_PARSER_END_TAG; |
5492 | 0 | ctxt->checkIndex = 0; |
5493 | 0 | break; |
5494 | 0 | } |
5495 | 0 | if ((!terminate) && |
5496 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0)) |
5497 | 0 | goto done; |
5498 | | |
5499 | | /* Capture start position */ |
5500 | 0 | if (ctxt->record_info) { |
5501 | 0 | node_info.begin_pos = ctxt->input->consumed + |
5502 | 0 | (CUR_PTR - ctxt->input->base); |
5503 | 0 | node_info.begin_line = ctxt->input->line; |
5504 | 0 | } |
5505 | | |
5506 | |
|
5507 | 0 | failed = htmlParseStartTag(ctxt); |
5508 | 0 | name = ctxt->name; |
5509 | 0 | if ((failed == -1) || |
5510 | 0 | (name == NULL)) { |
5511 | 0 | if (CUR == '>') |
5512 | 0 | NEXT; |
5513 | 0 | break; |
5514 | 0 | } |
5515 | | |
5516 | | /* |
5517 | | * Lookup the info for that element. |
5518 | | */ |
5519 | 0 | info = htmlTagLookup(name); |
5520 | 0 | if (info == NULL) { |
5521 | 0 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
5522 | 0 | "Tag %s invalid\n", name, NULL); |
5523 | 0 | } |
5524 | | |
5525 | | /* |
5526 | | * Check for an Empty Element labeled the XML/SGML way |
5527 | | */ |
5528 | 0 | if ((CUR == '/') && (NXT(1) == '>')) { |
5529 | 0 | SKIP(2); |
5530 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
5531 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
5532 | 0 | htmlnamePop(ctxt); |
5533 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5534 | 0 | break; |
5535 | 0 | } |
5536 | | |
5537 | 0 | if (CUR == '>') { |
5538 | 0 | NEXT; |
5539 | 0 | } else { |
5540 | 0 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
5541 | 0 | "Couldn't find end of Start Tag %s\n", |
5542 | 0 | name, NULL); |
5543 | | |
5544 | | /* |
5545 | | * end of parsing of this node. |
5546 | | */ |
5547 | 0 | if (xmlStrEqual(name, ctxt->name)) { |
5548 | 0 | nodePop(ctxt); |
5549 | 0 | htmlnamePop(ctxt); |
5550 | 0 | } |
5551 | |
|
5552 | 0 | if (ctxt->record_info) |
5553 | 0 | htmlNodeInfoPush(ctxt, &node_info); |
5554 | |
|
5555 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5556 | 0 | break; |
5557 | 0 | } |
5558 | | |
5559 | | /* |
5560 | | * Check for an Empty Element from DTD definition |
5561 | | */ |
5562 | 0 | if ((info != NULL) && (info->empty)) { |
5563 | 0 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
5564 | 0 | ctxt->sax->endElement(ctxt->userData, name); |
5565 | 0 | htmlnamePop(ctxt); |
5566 | 0 | } |
5567 | |
|
5568 | 0 | if (ctxt->record_info) |
5569 | 0 | htmlNodeInfoPush(ctxt, &node_info); |
5570 | |
|
5571 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5572 | 0 | break; |
5573 | 0 | } |
5574 | 0 | case XML_PARSER_CONTENT: { |
5575 | 0 | xmlChar chr[2] = { 0, 0 }; |
5576 | | |
5577 | | /* |
5578 | | * Handle preparsed entities and charRef |
5579 | | */ |
5580 | 0 | if ((avail == 1) && (terminate)) { |
5581 | 0 | cur = in->cur[0]; |
5582 | 0 | if ((cur != '<') && (cur != '&')) { |
5583 | 0 | if (ctxt->sax != NULL) { |
5584 | 0 | chr[0] = cur; |
5585 | 0 | if (IS_BLANK_CH(cur)) { |
5586 | 0 | if (ctxt->keepBlanks) { |
5587 | 0 | if (ctxt->sax->characters != NULL) |
5588 | 0 | ctxt->sax->characters( |
5589 | 0 | ctxt->userData, chr, 1); |
5590 | 0 | } else { |
5591 | 0 | if (ctxt->sax->ignorableWhitespace != NULL) |
5592 | 0 | ctxt->sax->ignorableWhitespace( |
5593 | 0 | ctxt->userData, chr, 1); |
5594 | 0 | } |
5595 | 0 | } else { |
5596 | 0 | htmlCheckParagraph(ctxt); |
5597 | 0 | if (ctxt->sax->characters != NULL) |
5598 | 0 | ctxt->sax->characters( |
5599 | 0 | ctxt->userData, chr, 1); |
5600 | 0 | } |
5601 | 0 | } |
5602 | 0 | ctxt->checkIndex = 0; |
5603 | 0 | in->cur++; |
5604 | 0 | break; |
5605 | 0 | } |
5606 | 0 | } |
5607 | 0 | if (avail < 2) |
5608 | 0 | goto done; |
5609 | 0 | cur = in->cur[0]; |
5610 | 0 | next = in->cur[1]; |
5611 | 0 | if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) || |
5612 | 0 | (xmlStrEqual(ctxt->name, BAD_CAST"style"))) { |
5613 | | /* |
5614 | | * Handle SCRIPT/STYLE separately |
5615 | | */ |
5616 | 0 | if (!terminate) { |
5617 | 0 | int idx; |
5618 | 0 | xmlChar val; |
5619 | |
|
5620 | 0 | idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0); |
5621 | 0 | if (idx < 0) |
5622 | 0 | goto done; |
5623 | 0 | val = in->cur[idx + 2]; |
5624 | 0 | if (val == 0) { /* bad cut of input */ |
5625 | | /* |
5626 | | * FIXME: htmlParseScript checks for additional |
5627 | | * characters after '</'. |
5628 | | */ |
5629 | 0 | ctxt->checkIndex = idx; |
5630 | 0 | goto done; |
5631 | 0 | } |
5632 | 0 | } |
5633 | 0 | htmlParseScript(ctxt); |
5634 | 0 | if ((cur == '<') && (next == '/')) { |
5635 | 0 | ctxt->instate = XML_PARSER_END_TAG; |
5636 | 0 | ctxt->checkIndex = 0; |
5637 | 0 | break; |
5638 | 0 | } |
5639 | 0 | } else if ((cur == '<') && (next == '!')) { |
5640 | 0 | if (avail < 4) |
5641 | 0 | goto done; |
5642 | | /* |
5643 | | * Sometimes DOCTYPE arrives in the middle of the document |
5644 | | */ |
5645 | 0 | if ((UPP(2) == 'D') && (UPP(3) == 'O') && |
5646 | 0 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
5647 | 0 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
5648 | 0 | (UPP(8) == 'E')) { |
5649 | 0 | if ((!terminate) && |
5650 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0)) |
5651 | 0 | goto done; |
5652 | 0 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
5653 | 0 | "Misplaced DOCTYPE declaration\n", |
5654 | 0 | BAD_CAST "DOCTYPE" , NULL); |
5655 | 0 | htmlParseDocTypeDecl(ctxt); |
5656 | 0 | } else if ((in->cur[2] == '-') && (in->cur[3] == '-')) { |
5657 | 0 | if ((!terminate) && |
5658 | 0 | (htmlParseLookupCommentEnd(ctxt) < 0)) |
5659 | 0 | goto done; |
5660 | 0 | htmlParseComment(ctxt); |
5661 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5662 | 0 | } else { |
5663 | 0 | if ((!terminate) && |
5664 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5665 | 0 | goto done; |
5666 | 0 | htmlSkipBogusComment(ctxt); |
5667 | 0 | } |
5668 | 0 | } else if ((cur == '<') && (next == '?')) { |
5669 | 0 | if ((!terminate) && |
5670 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5671 | 0 | goto done; |
5672 | 0 | htmlParsePI(ctxt); |
5673 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5674 | 0 | } else if ((cur == '<') && (next == '/')) { |
5675 | 0 | ctxt->instate = XML_PARSER_END_TAG; |
5676 | 0 | ctxt->checkIndex = 0; |
5677 | 0 | break; |
5678 | 0 | } else if ((cur == '<') && IS_ASCII_LETTER(next)) { |
5679 | 0 | if ((!terminate) && (next == 0)) |
5680 | 0 | goto done; |
5681 | 0 | ctxt->instate = XML_PARSER_START_TAG; |
5682 | 0 | ctxt->checkIndex = 0; |
5683 | 0 | break; |
5684 | 0 | } else if (cur == '<') { |
5685 | 0 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
5686 | 0 | (ctxt->sax->characters != NULL)) |
5687 | 0 | ctxt->sax->characters(ctxt->userData, |
5688 | 0 | BAD_CAST "<", 1); |
5689 | 0 | NEXT; |
5690 | 0 | } else { |
5691 | | /* |
5692 | | * check that the text sequence is complete |
5693 | | * before handing out the data to the parser |
5694 | | * to avoid problems with erroneous end of |
5695 | | * data detection. |
5696 | | */ |
5697 | 0 | if ((!terminate) && |
5698 | 0 | (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0)) |
5699 | 0 | goto done; |
5700 | 0 | ctxt->checkIndex = 0; |
5701 | 0 | while ((PARSER_STOPPED(ctxt) == 0) && |
5702 | 0 | (cur != '<') && (in->cur < in->end)) { |
5703 | 0 | if (cur == '&') { |
5704 | 0 | htmlParseReference(ctxt); |
5705 | 0 | } else { |
5706 | 0 | htmlParseCharData(ctxt); |
5707 | 0 | } |
5708 | 0 | cur = in->cur[0]; |
5709 | 0 | } |
5710 | 0 | } |
5711 | | |
5712 | 0 | break; |
5713 | 0 | } |
5714 | 0 | case XML_PARSER_END_TAG: |
5715 | 0 | if (avail < 2) |
5716 | 0 | goto done; |
5717 | 0 | if ((!terminate) && |
5718 | 0 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
5719 | 0 | goto done; |
5720 | 0 | htmlParseEndTag(ctxt); |
5721 | 0 | if (ctxt->nameNr == 0) { |
5722 | 0 | ctxt->instate = XML_PARSER_EPILOG; |
5723 | 0 | } else { |
5724 | 0 | ctxt->instate = XML_PARSER_CONTENT; |
5725 | 0 | } |
5726 | 0 | ctxt->checkIndex = 0; |
5727 | 0 | break; |
5728 | 0 | default: |
5729 | 0 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
5730 | 0 | "HPP: internal error\n", NULL, NULL); |
5731 | 0 | ctxt->instate = XML_PARSER_EOF; |
5732 | 0 | break; |
5733 | 0 | } |
5734 | 0 | } |
5735 | 0 | done: |
5736 | 0 | if ((avail == 0) && (terminate)) { |
5737 | 0 | htmlAutoCloseOnEnd(ctxt); |
5738 | 0 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
5739 | | /* |
5740 | | * SAX: end of the document processing. |
5741 | | */ |
5742 | 0 | ctxt->instate = XML_PARSER_EOF; |
5743 | 0 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
5744 | 0 | ctxt->sax->endDocument(ctxt->userData); |
5745 | 0 | } |
5746 | 0 | } |
5747 | 0 | if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL) && |
5748 | 0 | ((terminate) || (ctxt->instate == XML_PARSER_EOF) || |
5749 | 0 | (ctxt->instate == XML_PARSER_EPILOG))) { |
5750 | 0 | xmlDtdPtr dtd; |
5751 | 0 | dtd = xmlGetIntSubset(ctxt->myDoc); |
5752 | 0 | if (dtd == NULL) { |
5753 | 0 | ctxt->myDoc->intSubset = |
5754 | 0 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
5755 | 0 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
5756 | 0 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
5757 | 0 | if (ctxt->myDoc->intSubset == NULL) |
5758 | 0 | htmlErrMemory(ctxt); |
5759 | 0 | } |
5760 | 0 | } |
5761 | 0 | return(ret); |
5762 | 0 | } |
5763 | | |
5764 | | /** |
5765 | | * htmlParseChunk: |
5766 | | * @ctxt: an HTML parser context |
5767 | | * @chunk: chunk of memory |
5768 | | * @size: size of chunk in bytes |
5769 | | * @terminate: last chunk indicator |
5770 | | * |
5771 | | * Parse a chunk of memory in push parser mode. |
5772 | | * |
5773 | | * Assumes that the parser context was initialized with |
5774 | | * htmlCreatePushParserCtxt. |
5775 | | * |
5776 | | * The last chunk, which will often be empty, must be marked with |
5777 | | * the @terminate flag. With the default SAX callbacks, the resulting |
5778 | | * document will be available in ctxt->myDoc. This pointer will not |
5779 | | * be freed by the library. |
5780 | | * |
5781 | | * If the document isn't well-formed, ctxt->myDoc is set to NULL. |
5782 | | * |
5783 | | * Returns an xmlParserErrors code (0 on success). |
5784 | | */ |
5785 | | int |
5786 | | htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size, |
5787 | 0 | int terminate) { |
5788 | 0 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
5789 | 0 | return(XML_ERR_ARGUMENT); |
5790 | 0 | if (PARSER_STOPPED(ctxt) != 0) |
5791 | 0 | return(ctxt->errNo); |
5792 | 0 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
5793 | 0 | (ctxt->input->buf != NULL)) { |
5794 | 0 | size_t pos = ctxt->input->cur - ctxt->input->base; |
5795 | 0 | int res; |
5796 | |
|
5797 | 0 | res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
5798 | 0 | xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos); |
5799 | 0 | if (res < 0) { |
5800 | 0 | htmlParseErr(ctxt, ctxt->input->buf->error, |
5801 | 0 | "xmlParserInputBufferPush failed", NULL, NULL); |
5802 | 0 | xmlHaltParser(ctxt); |
5803 | 0 | return (ctxt->errNo); |
5804 | 0 | } |
5805 | 0 | } |
5806 | 0 | htmlParseTryOrFinish(ctxt, terminate); |
5807 | 0 | if (terminate) { |
5808 | 0 | if (ctxt->instate != XML_PARSER_EOF) { |
5809 | 0 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
5810 | 0 | ctxt->sax->endDocument(ctxt->userData); |
5811 | 0 | } |
5812 | 0 | ctxt->instate = XML_PARSER_EOF; |
5813 | 0 | } |
5814 | 0 | return((xmlParserErrors) ctxt->errNo); |
5815 | 0 | } |
5816 | | |
5817 | | /************************************************************************ |
5818 | | * * |
5819 | | * User entry points * |
5820 | | * * |
5821 | | ************************************************************************/ |
5822 | | |
5823 | | /** |
5824 | | * htmlCreatePushParserCtxt: |
5825 | | * @sax: a SAX handler (optional) |
5826 | | * @user_data: The user data returned on SAX callbacks (optional) |
5827 | | * @chunk: a pointer to an array of chars (optional) |
5828 | | * @size: number of chars in the array |
5829 | | * @filename: only used for error reporting (optional) |
5830 | | * @enc: encoding (deprecated, pass XML_CHAR_ENCODING_NONE) |
5831 | | * |
5832 | | * Create a parser context for using the HTML parser in push mode. |
5833 | | * |
5834 | | * Returns the new parser context or NULL if a memory allocation |
5835 | | * failed. |
5836 | | */ |
5837 | | htmlParserCtxtPtr |
5838 | | htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, |
5839 | | const char *chunk, int size, const char *filename, |
5840 | 0 | xmlCharEncoding enc) { |
5841 | 0 | htmlParserCtxtPtr ctxt; |
5842 | 0 | htmlParserInputPtr input; |
5843 | 0 | const char *encoding; |
5844 | |
|
5845 | 0 | ctxt = htmlNewSAXParserCtxt(sax, user_data); |
5846 | 0 | if (ctxt == NULL) |
5847 | 0 | return(NULL); |
5848 | | |
5849 | 0 | encoding = xmlGetCharEncodingName(enc); |
5850 | 0 | input = xmlNewPushInput(filename, chunk, size); |
5851 | 0 | if (input == NULL) { |
5852 | 0 | htmlFreeParserCtxt(ctxt); |
5853 | 0 | return(NULL); |
5854 | 0 | } |
5855 | | |
5856 | 0 | if (inputPush(ctxt, input) < 0) { |
5857 | 0 | xmlFreeInputStream(input); |
5858 | 0 | xmlFreeParserCtxt(ctxt); |
5859 | 0 | return(NULL); |
5860 | 0 | } |
5861 | | |
5862 | 0 | if (encoding != NULL) |
5863 | 0 | xmlSwitchEncodingName(ctxt, encoding); |
5864 | |
|
5865 | 0 | return(ctxt); |
5866 | 0 | } |
5867 | | #endif /* LIBXML_PUSH_ENABLED */ |
5868 | | |
5869 | | /** |
5870 | | * htmlSAXParseDoc: |
5871 | | * @cur: a pointer to an array of xmlChar |
5872 | | * @encoding: a free form C string describing the HTML document encoding, or NULL |
5873 | | * @sax: the SAX handler block |
5874 | | * @userData: if using SAX, this pointer will be provided on callbacks. |
5875 | | * |
5876 | | * DEPRECATED: Use htmlNewSAXParserCtxt and htmlCtxtReadDoc. |
5877 | | * |
5878 | | * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks |
5879 | | * to handle parse events. If sax is NULL, fallback to the default DOM |
5880 | | * behavior and return a tree. |
5881 | | * |
5882 | | * Returns the resulting document tree unless SAX is NULL or the document is |
5883 | | * not well formed. |
5884 | | */ |
5885 | | |
5886 | | htmlDocPtr |
5887 | | htmlSAXParseDoc(const xmlChar *cur, const char *encoding, |
5888 | 0 | htmlSAXHandlerPtr sax, void *userData) { |
5889 | 0 | htmlDocPtr ret; |
5890 | 0 | htmlParserCtxtPtr ctxt; |
5891 | |
|
5892 | 0 | if (cur == NULL) |
5893 | 0 | return(NULL); |
5894 | | |
5895 | 0 | ctxt = htmlCreateDocParserCtxt(cur, NULL, encoding); |
5896 | 0 | if (ctxt == NULL) |
5897 | 0 | return(NULL); |
5898 | | |
5899 | 0 | if (sax != NULL) { |
5900 | 0 | *ctxt->sax = *sax; |
5901 | 0 | ctxt->userData = userData; |
5902 | 0 | } |
5903 | |
|
5904 | 0 | htmlParseDocument(ctxt); |
5905 | 0 | ret = ctxt->myDoc; |
5906 | 0 | htmlFreeParserCtxt(ctxt); |
5907 | |
|
5908 | 0 | return(ret); |
5909 | 0 | } |
5910 | | |
5911 | | /** |
5912 | | * htmlParseDoc: |
5913 | | * @cur: a pointer to an array of xmlChar |
5914 | | * @encoding: the encoding (optional) |
5915 | | * |
5916 | | * DEPRECATED: Use htmlReadDoc. |
5917 | | * |
5918 | | * Parse an HTML in-memory document and build a tree. |
5919 | | * |
5920 | | * This function uses deprecated global parser options. |
5921 | | * |
5922 | | * Returns the resulting document tree |
5923 | | */ |
5924 | | |
5925 | | htmlDocPtr |
5926 | 0 | htmlParseDoc(const xmlChar *cur, const char *encoding) { |
5927 | 0 | return(htmlSAXParseDoc(cur, encoding, NULL, NULL)); |
5928 | 0 | } |
5929 | | |
5930 | | |
5931 | | /** |
5932 | | * htmlCreateFileParserCtxt: |
5933 | | * @filename: the filename |
5934 | | * @encoding: optional encoding |
5935 | | * |
5936 | | * DEPRECATED: Use htmlNewParserCtxt and htmlCtxtReadFile. |
5937 | | * |
5938 | | * Create a parser context to read from a file. |
5939 | | * |
5940 | | * A non-NULL encoding overrides encoding declarations in the document. |
5941 | | * |
5942 | | * Automatic support for ZLIB/Compress compressed document is provided |
5943 | | * by default if found at compile-time. |
5944 | | * |
5945 | | * Returns the new parser context or NULL if a memory allocation failed. |
5946 | | */ |
5947 | | htmlParserCtxtPtr |
5948 | | htmlCreateFileParserCtxt(const char *filename, const char *encoding) |
5949 | 0 | { |
5950 | 0 | htmlParserCtxtPtr ctxt; |
5951 | 0 | htmlParserInputPtr input; |
5952 | |
|
5953 | 0 | if (filename == NULL) |
5954 | 0 | return(NULL); |
5955 | | |
5956 | 0 | ctxt = htmlNewParserCtxt(); |
5957 | 0 | if (ctxt == NULL) { |
5958 | 0 | return(NULL); |
5959 | 0 | } |
5960 | | |
5961 | 0 | input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0); |
5962 | 0 | if (input == NULL) { |
5963 | 0 | xmlFreeParserCtxt(ctxt); |
5964 | 0 | return(NULL); |
5965 | 0 | } |
5966 | 0 | if (inputPush(ctxt, input) < 0) { |
5967 | 0 | xmlFreeInputStream(input); |
5968 | 0 | xmlFreeParserCtxt(ctxt); |
5969 | 0 | return(NULL); |
5970 | 0 | } |
5971 | | |
5972 | 0 | return(ctxt); |
5973 | 0 | } |
5974 | | |
5975 | | /** |
5976 | | * htmlSAXParseFile: |
5977 | | * @filename: the filename |
5978 | | * @encoding: encoding (optional) |
5979 | | * @sax: the SAX handler block |
5980 | | * @userData: if using SAX, this pointer will be provided on callbacks. |
5981 | | * |
5982 | | * DEPRECATED: Use htmlNewSAXParserCtxt and htmlCtxtReadFile. |
5983 | | * |
5984 | | * parse an HTML file and build a tree. Automatic support for ZLIB/Compress |
5985 | | * compressed document is provided by default if found at compile-time. |
5986 | | * It use the given SAX function block to handle the parsing callback. |
5987 | | * If sax is NULL, fallback to the default DOM tree building routines. |
5988 | | * |
5989 | | * Returns the resulting document tree unless SAX is NULL or the document is |
5990 | | * not well formed. |
5991 | | */ |
5992 | | |
5993 | | htmlDocPtr |
5994 | | htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, |
5995 | 0 | void *userData) { |
5996 | 0 | htmlDocPtr ret; |
5997 | 0 | htmlParserCtxtPtr ctxt; |
5998 | 0 | htmlSAXHandlerPtr oldsax = NULL; |
5999 | |
|
6000 | 0 | ctxt = htmlCreateFileParserCtxt(filename, encoding); |
6001 | 0 | if (ctxt == NULL) return(NULL); |
6002 | 0 | if (sax != NULL) { |
6003 | 0 | oldsax = ctxt->sax; |
6004 | 0 | ctxt->sax = sax; |
6005 | 0 | ctxt->userData = userData; |
6006 | 0 | } |
6007 | |
|
6008 | 0 | htmlParseDocument(ctxt); |
6009 | |
|
6010 | 0 | ret = ctxt->myDoc; |
6011 | 0 | if (sax != NULL) { |
6012 | 0 | ctxt->sax = oldsax; |
6013 | 0 | ctxt->userData = NULL; |
6014 | 0 | } |
6015 | 0 | htmlFreeParserCtxt(ctxt); |
6016 | |
|
6017 | 0 | return(ret); |
6018 | 0 | } |
6019 | | |
6020 | | /** |
6021 | | * htmlParseFile: |
6022 | | * @filename: the filename |
6023 | | * @encoding: encoding (optional) |
6024 | | * |
6025 | | * Parse an HTML file and build a tree. |
6026 | | * |
6027 | | * Returns the resulting document tree |
6028 | | */ |
6029 | | |
6030 | | htmlDocPtr |
6031 | 0 | htmlParseFile(const char *filename, const char *encoding) { |
6032 | 0 | return(htmlSAXParseFile(filename, encoding, NULL, NULL)); |
6033 | 0 | } |
6034 | | |
6035 | | /** |
6036 | | * htmlHandleOmittedElem: |
6037 | | * @val: int 0 or 1 |
6038 | | * |
6039 | | * DEPRECATED: Use HTML_PARSE_NOIMPLIED |
6040 | | * |
6041 | | * Set and return the previous value for handling HTML omitted tags. |
6042 | | * |
6043 | | * Returns the last value for 0 for no handling, 1 for auto insertion. |
6044 | | */ |
6045 | | |
6046 | | int |
6047 | 0 | htmlHandleOmittedElem(int val) { |
6048 | 0 | int old = htmlOmittedDefaultValue; |
6049 | |
|
6050 | 0 | htmlOmittedDefaultValue = val; |
6051 | 0 | return(old); |
6052 | 0 | } |
6053 | | |
6054 | | /** |
6055 | | * htmlElementAllowedHere: |
6056 | | * @parent: HTML parent element |
6057 | | * @elt: HTML element |
6058 | | * |
6059 | | * Checks whether an HTML element may be a direct child of a parent element. |
6060 | | * Note - doesn't check for deprecated elements |
6061 | | * |
6062 | | * Returns 1 if allowed; 0 otherwise. |
6063 | | */ |
6064 | | int |
6065 | 0 | htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) { |
6066 | 0 | const char** p ; |
6067 | |
|
6068 | 0 | if ( ! elt || ! parent || ! parent->subelts ) |
6069 | 0 | return 0 ; |
6070 | | |
6071 | 0 | for ( p = parent->subelts; *p; ++p ) |
6072 | 0 | if ( !xmlStrcmp((const xmlChar *)*p, elt) ) |
6073 | 0 | return 1 ; |
6074 | | |
6075 | 0 | return 0 ; |
6076 | 0 | } |
6077 | | /** |
6078 | | * htmlElementStatusHere: |
6079 | | * @parent: HTML parent element |
6080 | | * @elt: HTML element |
6081 | | * |
6082 | | * Checks whether an HTML element may be a direct child of a parent element. |
6083 | | * and if so whether it is valid or deprecated. |
6084 | | * |
6085 | | * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
6086 | | */ |
6087 | | htmlStatus |
6088 | 0 | htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) { |
6089 | 0 | if ( ! parent || ! elt ) |
6090 | 0 | return HTML_INVALID ; |
6091 | 0 | if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) ) |
6092 | 0 | return HTML_INVALID ; |
6093 | | |
6094 | 0 | return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ; |
6095 | 0 | } |
6096 | | /** |
6097 | | * htmlAttrAllowed: |
6098 | | * @elt: HTML element |
6099 | | * @attr: HTML attribute |
6100 | | * @legacy: whether to allow deprecated attributes |
6101 | | * |
6102 | | * Checks whether an attribute is valid for an element |
6103 | | * Has full knowledge of Required and Deprecated attributes |
6104 | | * |
6105 | | * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
6106 | | */ |
6107 | | htmlStatus |
6108 | 0 | htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) { |
6109 | 0 | const char** p ; |
6110 | |
|
6111 | 0 | if ( !elt || ! attr ) |
6112 | 0 | return HTML_INVALID ; |
6113 | | |
6114 | 0 | if ( elt->attrs_req ) |
6115 | 0 | for ( p = elt->attrs_req; *p; ++p) |
6116 | 0 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
6117 | 0 | return HTML_REQUIRED ; |
6118 | | |
6119 | 0 | if ( elt->attrs_opt ) |
6120 | 0 | for ( p = elt->attrs_opt; *p; ++p) |
6121 | 0 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
6122 | 0 | return HTML_VALID ; |
6123 | | |
6124 | 0 | if ( legacy && elt->attrs_depr ) |
6125 | 0 | for ( p = elt->attrs_depr; *p; ++p) |
6126 | 0 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
6127 | 0 | return HTML_DEPRECATED ; |
6128 | | |
6129 | 0 | return HTML_INVALID ; |
6130 | 0 | } |
6131 | | /** |
6132 | | * htmlNodeStatus: |
6133 | | * @node: an htmlNodePtr in a tree |
6134 | | * @legacy: whether to allow deprecated elements (YES is faster here |
6135 | | * for Element nodes) |
6136 | | * |
6137 | | * Checks whether the tree node is valid. Experimental (the author |
6138 | | * only uses the HTML enhancements in a SAX parser) |
6139 | | * |
6140 | | * Return: for Element nodes, a return from htmlElementAllowedHere (if |
6141 | | * legacy allowed) or htmlElementStatusHere (otherwise). |
6142 | | * for Attribute nodes, a return from htmlAttrAllowed |
6143 | | * for other nodes, HTML_NA (no checks performed) |
6144 | | */ |
6145 | | htmlStatus |
6146 | 0 | htmlNodeStatus(htmlNodePtr node, int legacy) { |
6147 | 0 | if ( ! node ) |
6148 | 0 | return HTML_INVALID ; |
6149 | | |
6150 | 0 | switch ( node->type ) { |
6151 | 0 | case XML_ELEMENT_NODE: |
6152 | 0 | return legacy |
6153 | 0 | ? ( htmlElementAllowedHere ( |
6154 | 0 | htmlTagLookup(node->parent->name) , node->name |
6155 | 0 | ) ? HTML_VALID : HTML_INVALID ) |
6156 | 0 | : htmlElementStatusHere( |
6157 | 0 | htmlTagLookup(node->parent->name) , |
6158 | 0 | htmlTagLookup(node->name) ) |
6159 | 0 | ; |
6160 | 0 | case XML_ATTRIBUTE_NODE: |
6161 | 0 | return htmlAttrAllowed( |
6162 | 0 | htmlTagLookup(node->parent->name) , node->name, legacy) ; |
6163 | 0 | default: return HTML_NA ; |
6164 | 0 | } |
6165 | 0 | } |
6166 | | /************************************************************************ |
6167 | | * * |
6168 | | * New set (2.6.0) of simpler and more flexible APIs * |
6169 | | * * |
6170 | | ************************************************************************/ |
6171 | | /** |
6172 | | * DICT_FREE: |
6173 | | * @str: a string |
6174 | | * |
6175 | | * Free a string if it is not owned by the "dict" dictionary in the |
6176 | | * current scope |
6177 | | */ |
6178 | | #define DICT_FREE(str) \ |
6179 | 0 | if ((str) && ((!dict) || \ |
6180 | 0 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
6181 | 0 | xmlFree((char *)(str)); |
6182 | | |
6183 | | /** |
6184 | | * htmlCtxtReset: |
6185 | | * @ctxt: an HTML parser context |
6186 | | * |
6187 | | * Reset a parser context |
6188 | | */ |
6189 | | void |
6190 | | htmlCtxtReset(htmlParserCtxtPtr ctxt) |
6191 | 0 | { |
6192 | 0 | xmlParserInputPtr input; |
6193 | 0 | xmlDictPtr dict; |
6194 | |
|
6195 | 0 | if (ctxt == NULL) |
6196 | 0 | return; |
6197 | | |
6198 | 0 | dict = ctxt->dict; |
6199 | |
|
6200 | 0 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
6201 | 0 | xmlFreeInputStream(input); |
6202 | 0 | } |
6203 | 0 | ctxt->inputNr = 0; |
6204 | 0 | ctxt->input = NULL; |
6205 | |
|
6206 | 0 | ctxt->spaceNr = 0; |
6207 | 0 | if (ctxt->spaceTab != NULL) { |
6208 | 0 | ctxt->spaceTab[0] = -1; |
6209 | 0 | ctxt->space = &ctxt->spaceTab[0]; |
6210 | 0 | } else { |
6211 | 0 | ctxt->space = NULL; |
6212 | 0 | } |
6213 | | |
6214 | |
|
6215 | 0 | ctxt->nodeNr = 0; |
6216 | 0 | ctxt->node = NULL; |
6217 | |
|
6218 | 0 | ctxt->nameNr = 0; |
6219 | 0 | ctxt->name = NULL; |
6220 | |
|
6221 | 0 | ctxt->nsNr = 0; |
6222 | |
|
6223 | 0 | DICT_FREE(ctxt->version); |
6224 | 0 | ctxt->version = NULL; |
6225 | 0 | DICT_FREE(ctxt->encoding); |
6226 | 0 | ctxt->encoding = NULL; |
6227 | 0 | DICT_FREE(ctxt->extSubURI); |
6228 | 0 | ctxt->extSubURI = NULL; |
6229 | 0 | DICT_FREE(ctxt->extSubSystem); |
6230 | 0 | ctxt->extSubSystem = NULL; |
6231 | |
|
6232 | 0 | if (ctxt->directory != NULL) { |
6233 | 0 | xmlFree(ctxt->directory); |
6234 | 0 | ctxt->directory = NULL; |
6235 | 0 | } |
6236 | |
|
6237 | 0 | if (ctxt->myDoc != NULL) |
6238 | 0 | xmlFreeDoc(ctxt->myDoc); |
6239 | 0 | ctxt->myDoc = NULL; |
6240 | |
|
6241 | 0 | ctxt->standalone = -1; |
6242 | 0 | ctxt->hasExternalSubset = 0; |
6243 | 0 | ctxt->hasPErefs = 0; |
6244 | 0 | ctxt->html = 1; |
6245 | 0 | ctxt->instate = XML_PARSER_START; |
6246 | |
|
6247 | 0 | ctxt->wellFormed = 1; |
6248 | 0 | ctxt->nsWellFormed = 1; |
6249 | 0 | ctxt->disableSAX = 0; |
6250 | 0 | ctxt->valid = 1; |
6251 | 0 | ctxt->vctxt.userData = ctxt; |
6252 | 0 | ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT; |
6253 | 0 | ctxt->vctxt.error = xmlParserValidityError; |
6254 | 0 | ctxt->vctxt.warning = xmlParserValidityWarning; |
6255 | 0 | ctxt->record_info = 0; |
6256 | 0 | ctxt->checkIndex = 0; |
6257 | 0 | ctxt->endCheckState = 0; |
6258 | 0 | ctxt->inSubset = 0; |
6259 | 0 | ctxt->errNo = XML_ERR_OK; |
6260 | 0 | ctxt->depth = 0; |
6261 | 0 | ctxt->catalogs = NULL; |
6262 | 0 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
6263 | |
|
6264 | 0 | if (ctxt->attsDefault != NULL) { |
6265 | 0 | xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator); |
6266 | 0 | ctxt->attsDefault = NULL; |
6267 | 0 | } |
6268 | 0 | if (ctxt->attsSpecial != NULL) { |
6269 | 0 | xmlHashFree(ctxt->attsSpecial, NULL); |
6270 | 0 | ctxt->attsSpecial = NULL; |
6271 | 0 | } |
6272 | |
|
6273 | 0 | ctxt->nbErrors = 0; |
6274 | 0 | ctxt->nbWarnings = 0; |
6275 | 0 | if (ctxt->lastError.code != XML_ERR_OK) |
6276 | 0 | xmlResetError(&ctxt->lastError); |
6277 | 0 | } |
6278 | | |
6279 | | /** |
6280 | | * htmlCtxtUseOptions: |
6281 | | * @ctxt: an HTML parser context |
6282 | | * @options: a combination of htmlParserOption(s) |
6283 | | * |
6284 | | * Applies the options to the parser context |
6285 | | * |
6286 | | * Returns 0 in case of success, the set of unknown or unimplemented options |
6287 | | * in case of error. |
6288 | | */ |
6289 | | int |
6290 | | htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options) |
6291 | 0 | { |
6292 | 0 | if (ctxt == NULL) |
6293 | 0 | return(-1); |
6294 | | |
6295 | 0 | if (options & HTML_PARSE_NOWARNING) { |
6296 | 0 | ctxt->sax->warning = NULL; |
6297 | 0 | ctxt->vctxt.warning = NULL; |
6298 | 0 | options -= XML_PARSE_NOWARNING; |
6299 | 0 | ctxt->options |= XML_PARSE_NOWARNING; |
6300 | 0 | } |
6301 | 0 | if (options & HTML_PARSE_NOERROR) { |
6302 | 0 | ctxt->sax->error = NULL; |
6303 | 0 | ctxt->vctxt.error = NULL; |
6304 | 0 | ctxt->sax->fatalError = NULL; |
6305 | 0 | options -= XML_PARSE_NOERROR; |
6306 | 0 | ctxt->options |= XML_PARSE_NOERROR; |
6307 | 0 | } |
6308 | 0 | if (options & HTML_PARSE_PEDANTIC) { |
6309 | 0 | ctxt->pedantic = 1; |
6310 | 0 | options -= XML_PARSE_PEDANTIC; |
6311 | 0 | ctxt->options |= XML_PARSE_PEDANTIC; |
6312 | 0 | } else |
6313 | 0 | ctxt->pedantic = 0; |
6314 | 0 | if (options & XML_PARSE_NOBLANKS) { |
6315 | 0 | ctxt->keepBlanks = 0; |
6316 | 0 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
6317 | 0 | options -= XML_PARSE_NOBLANKS; |
6318 | 0 | ctxt->options |= XML_PARSE_NOBLANKS; |
6319 | 0 | } else |
6320 | 0 | ctxt->keepBlanks = 1; |
6321 | 0 | if (options & HTML_PARSE_RECOVER) { |
6322 | 0 | ctxt->recovery = 1; |
6323 | 0 | options -= HTML_PARSE_RECOVER; |
6324 | 0 | } else |
6325 | 0 | ctxt->recovery = 0; |
6326 | 0 | if (options & HTML_PARSE_COMPACT) { |
6327 | 0 | ctxt->options |= HTML_PARSE_COMPACT; |
6328 | 0 | options -= HTML_PARSE_COMPACT; |
6329 | 0 | } |
6330 | 0 | if (options & XML_PARSE_HUGE) { |
6331 | 0 | ctxt->options |= XML_PARSE_HUGE; |
6332 | 0 | options -= XML_PARSE_HUGE; |
6333 | 0 | } |
6334 | 0 | if (options & HTML_PARSE_NODEFDTD) { |
6335 | 0 | ctxt->options |= HTML_PARSE_NODEFDTD; |
6336 | 0 | options -= HTML_PARSE_NODEFDTD; |
6337 | 0 | } |
6338 | 0 | if (options & HTML_PARSE_IGNORE_ENC) { |
6339 | 0 | ctxt->options |= HTML_PARSE_IGNORE_ENC; |
6340 | 0 | options -= HTML_PARSE_IGNORE_ENC; |
6341 | 0 | } |
6342 | 0 | if (options & HTML_PARSE_NOIMPLIED) { |
6343 | 0 | ctxt->options |= HTML_PARSE_NOIMPLIED; |
6344 | 0 | options -= HTML_PARSE_NOIMPLIED; |
6345 | 0 | } |
6346 | 0 | ctxt->dictNames = 0; |
6347 | 0 | ctxt->linenumbers = 1; |
6348 | 0 | return (options); |
6349 | 0 | } |
6350 | | |
6351 | | /** |
6352 | | * htmlCtxtParseDocument: |
6353 | | * @ctxt: an HTML parser context |
6354 | | * @input: parser input |
6355 | | * |
6356 | | * Parse an HTML document and return the resulting document tree. |
6357 | | * |
6358 | | * Available since 2.13.0. |
6359 | | * |
6360 | | * Returns the resulting document tree or NULL |
6361 | | */ |
6362 | | htmlDocPtr |
6363 | | htmlCtxtParseDocument(htmlParserCtxtPtr ctxt, xmlParserInputPtr input) |
6364 | 0 | { |
6365 | 0 | htmlDocPtr ret; |
6366 | |
|
6367 | 0 | if ((ctxt == NULL) || (input == NULL)) |
6368 | 0 | return(NULL); |
6369 | | |
6370 | | /* assert(ctxt->inputNr == 0); */ |
6371 | 0 | while (ctxt->inputNr > 0) |
6372 | 0 | xmlFreeInputStream(inputPop(ctxt)); |
6373 | |
|
6374 | 0 | if (inputPush(ctxt, input) < 0) { |
6375 | 0 | xmlFreeInputStream(input); |
6376 | 0 | return(NULL); |
6377 | 0 | } |
6378 | | |
6379 | 0 | ctxt->html = 1; |
6380 | 0 | htmlParseDocument(ctxt); |
6381 | |
|
6382 | 0 | if (ctxt->errNo != XML_ERR_NO_MEMORY) { |
6383 | 0 | ret = ctxt->myDoc; |
6384 | 0 | } else { |
6385 | 0 | ret = NULL; |
6386 | 0 | xmlFreeDoc(ctxt->myDoc); |
6387 | 0 | } |
6388 | 0 | ctxt->myDoc = NULL; |
6389 | | |
6390 | | /* assert(ctxt->inputNr == 1); */ |
6391 | 0 | while (ctxt->inputNr > 0) |
6392 | 0 | xmlFreeInputStream(inputPop(ctxt)); |
6393 | |
|
6394 | 0 | return(ret); |
6395 | 0 | } |
6396 | | |
6397 | | /** |
6398 | | * htmlReadDoc: |
6399 | | * @str: a pointer to a zero terminated string |
6400 | | * @url: only used for error reporting (optoinal) |
6401 | | * @encoding: the document encoding (optional) |
6402 | | * @options: a combination of htmlParserOptions |
6403 | | * |
6404 | | * Convenience function to parse an HTML document from a zero-terminated |
6405 | | * string. |
6406 | | * |
6407 | | * See htmlCtxtReadDoc for details. |
6408 | | * |
6409 | | * Returns the resulting document tree. |
6410 | | */ |
6411 | | htmlDocPtr |
6412 | | htmlReadDoc(const xmlChar *str, const char *url, const char *encoding, |
6413 | | int options) |
6414 | 0 | { |
6415 | 0 | htmlParserCtxtPtr ctxt; |
6416 | 0 | xmlParserInputPtr input; |
6417 | 0 | htmlDocPtr doc; |
6418 | |
|
6419 | 0 | ctxt = htmlNewParserCtxt(); |
6420 | 0 | if (ctxt == NULL) |
6421 | 0 | return(NULL); |
6422 | | |
6423 | 0 | htmlCtxtUseOptions(ctxt, options); |
6424 | |
|
6425 | 0 | input = xmlCtxtNewInputFromString(ctxt, url, (const char *) str, encoding, |
6426 | 0 | XML_INPUT_BUF_STATIC); |
6427 | |
|
6428 | 0 | doc = htmlCtxtParseDocument(ctxt, input); |
6429 | |
|
6430 | 0 | htmlFreeParserCtxt(ctxt); |
6431 | 0 | return(doc); |
6432 | 0 | } |
6433 | | |
6434 | | /** |
6435 | | * htmlReadFile: |
6436 | | * @filename: a file or URL |
6437 | | * @encoding: the document encoding (optional) |
6438 | | * @options: a combination of htmlParserOptions |
6439 | | * |
6440 | | * Convenience function to parse an HTML file from the filesystem, |
6441 | | * the network or a global user-defined resource loader. |
6442 | | * |
6443 | | * See htmlCtxtReadFile for details. |
6444 | | * |
6445 | | * Returns the resulting document tree. |
6446 | | */ |
6447 | | htmlDocPtr |
6448 | | htmlReadFile(const char *filename, const char *encoding, int options) |
6449 | 0 | { |
6450 | 0 | htmlParserCtxtPtr ctxt; |
6451 | 0 | xmlParserInputPtr input; |
6452 | 0 | htmlDocPtr doc; |
6453 | |
|
6454 | 0 | ctxt = htmlNewParserCtxt(); |
6455 | 0 | if (ctxt == NULL) |
6456 | 0 | return(NULL); |
6457 | | |
6458 | 0 | htmlCtxtUseOptions(ctxt, options); |
6459 | |
|
6460 | 0 | input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0); |
6461 | |
|
6462 | 0 | doc = htmlCtxtParseDocument(ctxt, input); |
6463 | |
|
6464 | 0 | htmlFreeParserCtxt(ctxt); |
6465 | 0 | return(doc); |
6466 | 0 | } |
6467 | | |
6468 | | /** |
6469 | | * htmlReadMemory: |
6470 | | * @buffer: a pointer to a char array |
6471 | | * @size: the size of the array |
6472 | | * @url: only used for error reporting (optional) |
6473 | | * @encoding: the document encoding, or NULL |
6474 | | * @options: a combination of htmlParserOption(s) |
6475 | | * |
6476 | | * Convenience function to parse an HTML document from memory. |
6477 | | * The input buffer must not contain any terminating null bytes. |
6478 | | * |
6479 | | * See htmlCtxtReadMemory for details. |
6480 | | * |
6481 | | * Returns the resulting document tree |
6482 | | */ |
6483 | | htmlDocPtr |
6484 | | htmlReadMemory(const char *buffer, int size, const char *url, |
6485 | | const char *encoding, int options) |
6486 | 0 | { |
6487 | 0 | htmlParserCtxtPtr ctxt; |
6488 | 0 | xmlParserInputPtr input; |
6489 | 0 | htmlDocPtr doc; |
6490 | |
|
6491 | 0 | if (size < 0) |
6492 | 0 | return(NULL); |
6493 | | |
6494 | 0 | ctxt = htmlNewParserCtxt(); |
6495 | 0 | if (ctxt == NULL) |
6496 | 0 | return(NULL); |
6497 | | |
6498 | 0 | htmlCtxtUseOptions(ctxt, options); |
6499 | |
|
6500 | 0 | input = xmlCtxtNewInputFromMemory(ctxt, url, buffer, size, encoding, |
6501 | 0 | XML_INPUT_BUF_STATIC); |
6502 | |
|
6503 | 0 | doc = htmlCtxtParseDocument(ctxt, input); |
6504 | |
|
6505 | 0 | htmlFreeParserCtxt(ctxt); |
6506 | 0 | return(doc); |
6507 | 0 | } |
6508 | | |
6509 | | /** |
6510 | | * htmlReadFd: |
6511 | | * @fd: an open file descriptor |
6512 | | * @url: only used for error reporting (optional) |
6513 | | * @encoding: the document encoding, or NULL |
6514 | | * @options: a combination of htmlParserOptions |
6515 | | * |
6516 | | * Convenience function to parse an HTML document from a |
6517 | | * file descriptor. |
6518 | | * |
6519 | | * NOTE that the file descriptor will not be closed when the |
6520 | | * context is freed or reset. |
6521 | | * |
6522 | | * See htmlCtxtReadFd for details. |
6523 | | * |
6524 | | * Returns the resulting document tree |
6525 | | */ |
6526 | | htmlDocPtr |
6527 | | htmlReadFd(int fd, const char *url, const char *encoding, int options) |
6528 | 0 | { |
6529 | 0 | htmlParserCtxtPtr ctxt; |
6530 | 0 | xmlParserInputPtr input; |
6531 | 0 | htmlDocPtr doc; |
6532 | |
|
6533 | 0 | ctxt = htmlNewParserCtxt(); |
6534 | 0 | if (ctxt == NULL) |
6535 | 0 | return(NULL); |
6536 | | |
6537 | 0 | htmlCtxtUseOptions(ctxt, options); |
6538 | |
|
6539 | 0 | input = xmlCtxtNewInputFromFd(ctxt, url, fd, encoding, 0); |
6540 | |
|
6541 | 0 | doc = htmlCtxtParseDocument(ctxt, input); |
6542 | |
|
6543 | 0 | htmlFreeParserCtxt(ctxt); |
6544 | 0 | return(doc); |
6545 | 0 | } |
6546 | | |
6547 | | /** |
6548 | | * htmlReadIO: |
6549 | | * @ioread: an I/O read function |
6550 | | * @ioclose: an I/O close function (optional) |
6551 | | * @ioctx: an I/O handler |
6552 | | * @url: only used for error reporting (optional) |
6553 | | * @encoding: the document encoding (optional) |
6554 | | * @options: a combination of htmlParserOption(s) |
6555 | | * |
6556 | | * Convenience function to parse an HTML document from I/O functions |
6557 | | * and context. |
6558 | | * |
6559 | | * See htmlCtxtReadIO for details. |
6560 | | * |
6561 | | * Returns the resulting document tree |
6562 | | */ |
6563 | | htmlDocPtr |
6564 | | htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
6565 | | void *ioctx, const char *url, const char *encoding, int options) |
6566 | 0 | { |
6567 | 0 | htmlParserCtxtPtr ctxt; |
6568 | 0 | xmlParserInputPtr input; |
6569 | 0 | htmlDocPtr doc; |
6570 | |
|
6571 | 0 | ctxt = htmlNewParserCtxt(); |
6572 | 0 | if (ctxt == NULL) |
6573 | 0 | return (NULL); |
6574 | | |
6575 | 0 | htmlCtxtUseOptions(ctxt, options); |
6576 | |
|
6577 | 0 | input = xmlCtxtNewInputFromIO(ctxt, url, ioread, ioclose, ioctx, |
6578 | 0 | encoding, 0); |
6579 | |
|
6580 | 0 | doc = htmlCtxtParseDocument(ctxt, input); |
6581 | |
|
6582 | 0 | htmlFreeParserCtxt(ctxt); |
6583 | 0 | return(doc); |
6584 | 0 | } |
6585 | | |
6586 | | /** |
6587 | | * htmlCtxtReadDoc: |
6588 | | * @ctxt: an HTML parser context |
6589 | | * @str: a pointer to a zero terminated string |
6590 | | * @URL: only used for error reporting (optional) |
6591 | | * @encoding: the document encoding (optional) |
6592 | | * @options: a combination of htmlParserOptions |
6593 | | * |
6594 | | * Parse an HTML in-memory document and build a tree. |
6595 | | * |
6596 | | * See htmlCtxtUseOptions for details. |
6597 | | * |
6598 | | * Returns the resulting document tree |
6599 | | */ |
6600 | | htmlDocPtr |
6601 | | htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar *str, |
6602 | | const char *URL, const char *encoding, int options) |
6603 | 0 | { |
6604 | 0 | xmlParserInputPtr input; |
6605 | |
|
6606 | 0 | if (ctxt == NULL) |
6607 | 0 | return (NULL); |
6608 | | |
6609 | 0 | htmlCtxtReset(ctxt); |
6610 | 0 | htmlCtxtUseOptions(ctxt, options); |
6611 | |
|
6612 | 0 | input = xmlCtxtNewInputFromString(ctxt, URL, (const char *) str, |
6613 | 0 | encoding, 0); |
6614 | |
|
6615 | 0 | return(htmlCtxtParseDocument(ctxt, input)); |
6616 | 0 | } |
6617 | | |
6618 | | /** |
6619 | | * htmlCtxtReadFile: |
6620 | | * @ctxt: an HTML parser context |
6621 | | * @filename: a file or URL |
6622 | | * @encoding: the document encoding (optional) |
6623 | | * @options: a combination of htmlParserOptions |
6624 | | * |
6625 | | * Parse an HTML file from the filesystem, the network or a |
6626 | | * user-defined resource loader. |
6627 | | * |
6628 | | * See htmlCtxtUseOptions for details. |
6629 | | * |
6630 | | * Returns the resulting document tree |
6631 | | */ |
6632 | | htmlDocPtr |
6633 | | htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename, |
6634 | | const char *encoding, int options) |
6635 | 0 | { |
6636 | 0 | xmlParserInputPtr input; |
6637 | |
|
6638 | 0 | if (ctxt == NULL) |
6639 | 0 | return (NULL); |
6640 | | |
6641 | 0 | htmlCtxtReset(ctxt); |
6642 | 0 | htmlCtxtUseOptions(ctxt, options); |
6643 | |
|
6644 | 0 | input = xmlCtxtNewInputFromUrl(ctxt, filename, NULL, encoding, 0); |
6645 | |
|
6646 | 0 | return(htmlCtxtParseDocument(ctxt, input)); |
6647 | 0 | } |
6648 | | |
6649 | | /** |
6650 | | * htmlCtxtReadMemory: |
6651 | | * @ctxt: an HTML parser context |
6652 | | * @buffer: a pointer to a char array |
6653 | | * @size: the size of the array |
6654 | | * @URL: only used for error reporting (optional) |
6655 | | * @encoding: the document encoding (optinal) |
6656 | | * @options: a combination of htmlParserOptions |
6657 | | * |
6658 | | * Parse an HTML in-memory document and build a tree. The input buffer must |
6659 | | * not contain any terminating null bytes. |
6660 | | * |
6661 | | * See htmlCtxtUseOptions for details. |
6662 | | * |
6663 | | * Returns the resulting document tree |
6664 | | */ |
6665 | | htmlDocPtr |
6666 | | htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size, |
6667 | | const char *URL, const char *encoding, int options) |
6668 | 0 | { |
6669 | 0 | xmlParserInputPtr input; |
6670 | |
|
6671 | 0 | if ((ctxt == NULL) || (size < 0)) |
6672 | 0 | return (NULL); |
6673 | | |
6674 | 0 | htmlCtxtReset(ctxt); |
6675 | 0 | htmlCtxtUseOptions(ctxt, options); |
6676 | |
|
6677 | 0 | input = xmlCtxtNewInputFromMemory(ctxt, URL, buffer, size, encoding, |
6678 | 0 | XML_INPUT_BUF_STATIC); |
6679 | |
|
6680 | 0 | return(htmlCtxtParseDocument(ctxt, input)); |
6681 | 0 | } |
6682 | | |
6683 | | /** |
6684 | | * htmlCtxtReadFd: |
6685 | | * @ctxt: an HTML parser context |
6686 | | * @fd: an open file descriptor |
6687 | | * @URL: only used for error reporting (optional) |
6688 | | * @encoding: the document encoding (optinal) |
6689 | | * @options: a combination of htmlParserOptions |
6690 | | * |
6691 | | * Parse an HTML from a file descriptor and build a tree. |
6692 | | * |
6693 | | * See htmlCtxtUseOptions for details. |
6694 | | * |
6695 | | * NOTE that the file descriptor will not be closed when the |
6696 | | * context is freed or reset. |
6697 | | * |
6698 | | * Returns the resulting document tree |
6699 | | */ |
6700 | | htmlDocPtr |
6701 | | htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd, |
6702 | | const char *URL, const char *encoding, int options) |
6703 | 0 | { |
6704 | 0 | xmlParserInputPtr input; |
6705 | |
|
6706 | 0 | if (ctxt == NULL) |
6707 | 0 | return(NULL); |
6708 | | |
6709 | 0 | htmlCtxtReset(ctxt); |
6710 | 0 | htmlCtxtUseOptions(ctxt, options); |
6711 | |
|
6712 | 0 | input = xmlCtxtNewInputFromFd(ctxt, URL, fd, encoding, 0); |
6713 | |
|
6714 | 0 | return(htmlCtxtParseDocument(ctxt, input)); |
6715 | 0 | } |
6716 | | |
6717 | | /** |
6718 | | * htmlCtxtReadIO: |
6719 | | * @ctxt: an HTML parser context |
6720 | | * @ioread: an I/O read function |
6721 | | * @ioclose: an I/O close function |
6722 | | * @ioctx: an I/O handler |
6723 | | * @URL: the base URL to use for the document |
6724 | | * @encoding: the document encoding, or NULL |
6725 | | * @options: a combination of htmlParserOption(s) |
6726 | | * |
6727 | | * Parse an HTML document from I/O functions and source and build a tree. |
6728 | | * |
6729 | | * See htmlCtxtUseOptions for details. |
6730 | | * |
6731 | | * Returns the resulting document tree |
6732 | | */ |
6733 | | htmlDocPtr |
6734 | | htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
6735 | | xmlInputCloseCallback ioclose, void *ioctx, |
6736 | | const char *URL, |
6737 | | const char *encoding, int options) |
6738 | 0 | { |
6739 | 0 | xmlParserInputPtr input; |
6740 | |
|
6741 | 0 | if (ctxt == NULL) |
6742 | 0 | return (NULL); |
6743 | | |
6744 | 0 | htmlCtxtReset(ctxt); |
6745 | 0 | htmlCtxtUseOptions(ctxt, options); |
6746 | |
|
6747 | 0 | input = xmlCtxtNewInputFromIO(ctxt, URL, ioread, ioclose, ioctx, |
6748 | 0 | encoding, 0); |
6749 | |
|
6750 | 0 | return(htmlCtxtParseDocument(ctxt, input)); |
6751 | 0 | } |
6752 | | |
6753 | | #endif /* LIBXML_HTML_ENABLED */ |