blob: a3fbb7f26ef025b8bfba8fd52a02f1de3cd7ecf8 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * HTMLparser.c : an HTML 4.0 non-verifying parser
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
Daniel Veillard34ce8be2002-03-18 19:37:11 +00009#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011#ifdef LIBXML_HTML_ENABLED
Bjorn Reese70a9da52001-04-21 16:57:29 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14#ifdef HAVE_CTYPE_H
15#include <ctype.h>
16#endif
17#ifdef HAVE_STDLIB_H
18#include <stdlib.h>
19#endif
20#ifdef HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#ifdef HAVE_FCNTL_H
24#include <fcntl.h>
25#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29#ifdef HAVE_ZLIB_H
30#include <zlib.h>
31#endif
32
33#include <libxml/xmlmemory.h>
34#include <libxml/tree.h>
35#include <libxml/parser.h>
36#include <libxml/parserInternals.h>
37#include <libxml/xmlerror.h>
38#include <libxml/HTMLparser.h>
Daniel Veillard56a4cb82001-03-24 17:00:36 +000039#include <libxml/HTMLtree.h>
Owen Taylor3473f882001-02-23 17:55:21 +000040#include <libxml/entities.h>
41#include <libxml/encoding.h>
42#include <libxml/valid.h>
43#include <libxml/xmlIO.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000044#include <libxml/globals.h>
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000045#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000046
47#define HTML_MAX_NAMELEN 1000
48#define HTML_PARSER_BIG_BUFFER_SIZE 1000
49#define HTML_PARSER_BUFFER_SIZE 100
50
51/* #define DEBUG */
52/* #define DEBUG_PUSH */
53
Daniel Veillard22090732001-07-16 00:06:07 +000054static int htmlOmittedDefaultValue = 1;
Owen Taylor3473f882001-02-23 17:55:21 +000055
Daniel Veillard56a4cb82001-03-24 17:00:36 +000056xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
57 xmlChar end, xmlChar end2, xmlChar end3);
Daniel Veillardc1f78342001-11-10 11:43:05 +000058static void htmlParseComment(htmlParserCtxtPtr ctxt);
Daniel Veillard56a4cb82001-03-24 17:00:36 +000059
60/************************************************************************
61 * *
Daniel Veillarde77db162009-08-22 11:32:38 +020062 * Some factorized error routines *
Daniel Veillardf403d292003-10-05 13:51:35 +000063 * *
64 ************************************************************************/
65
66/**
William M. Brackedb65a72004-02-06 07:36:04 +000067 * htmlErrMemory:
Daniel Veillardf403d292003-10-05 13:51:35 +000068 * @ctxt: an HTML parser context
69 * @extra: extra informations
70 *
71 * Handle a redefinition of attribute error
72 */
73static void
74htmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
75{
Daniel Veillard157fee02003-10-31 10:36:03 +000076 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
77 (ctxt->instate == XML_PARSER_EOF))
78 return;
Daniel Veillardf403d292003-10-05 13:51:35 +000079 if (ctxt != NULL) {
80 ctxt->errNo = XML_ERR_NO_MEMORY;
81 ctxt->instate = XML_PARSER_EOF;
82 ctxt->disableSAX = 1;
83 }
84 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +000085 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardf403d292003-10-05 13:51:35 +000086 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
87 NULL, NULL, 0, 0,
88 "Memory allocation failed : %s\n", extra);
89 else
Daniel Veillard659e71e2003-10-10 14:10:40 +000090 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardf403d292003-10-05 13:51:35 +000091 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
92 NULL, NULL, 0, 0, "Memory allocation failed\n");
93}
94
95/**
96 * htmlParseErr:
97 * @ctxt: an HTML parser context
98 * @error: the error number
99 * @msg: the error message
100 * @str1: string infor
101 * @str2: string infor
102 *
103 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
104 */
105static void
106htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
107 const char *msg, const xmlChar *str1, const xmlChar *str2)
108{
Daniel Veillard157fee02003-10-31 10:36:03 +0000109 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
110 (ctxt->instate == XML_PARSER_EOF))
111 return;
Daniel Veillarda03e3652004-11-02 18:45:30 +0000112 if (ctxt != NULL)
113 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000114 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
Daniel Veillardf403d292003-10-05 13:51:35 +0000115 XML_ERR_ERROR, NULL, 0,
116 (const char *) str1, (const char *) str2,
117 NULL, 0, 0,
118 msg, str1, str2);
Daniel Veillarda03e3652004-11-02 18:45:30 +0000119 if (ctxt != NULL)
120 ctxt->wellFormed = 0;
Daniel Veillardf403d292003-10-05 13:51:35 +0000121}
122
123/**
124 * htmlParseErrInt:
125 * @ctxt: an HTML parser context
126 * @error: the error number
127 * @msg: the error message
128 * @val: integer info
129 *
130 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
131 */
132static void
133htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
134 const char *msg, int val)
135{
Daniel Veillard157fee02003-10-31 10:36:03 +0000136 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
137 (ctxt->instate == XML_PARSER_EOF))
138 return;
Daniel Veillarda03e3652004-11-02 18:45:30 +0000139 if (ctxt != NULL)
140 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000141 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
Daniel Veillardf403d292003-10-05 13:51:35 +0000142 XML_ERR_ERROR, NULL, 0, NULL, NULL,
143 NULL, val, 0, msg, val);
Daniel Veillarda03e3652004-11-02 18:45:30 +0000144 if (ctxt != NULL)
145 ctxt->wellFormed = 0;
Daniel Veillardf403d292003-10-05 13:51:35 +0000146}
147
148/************************************************************************
149 * *
Daniel Veillarde77db162009-08-22 11:32:38 +0200150 * Parser stacks related functions and macros *
Owen Taylor3473f882001-02-23 17:55:21 +0000151 * *
152 ************************************************************************/
153
Daniel Veillard1c732d22002-11-30 11:22:59 +0000154/**
155 * htmlnamePush:
156 * @ctxt: an HTML parser context
157 * @value: the element name
158 *
159 * Pushes a new element name on top of the name stack
160 *
161 * Returns 0 in case of error, the index in the stack otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000162 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000163static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000164htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +0000165{
Daniel Veillard029a04d2009-08-24 12:50:23 +0200166 if ((ctxt->html < 3) && (xmlStrEqual(value, BAD_CAST "head")))
167 ctxt->html = 3;
168 if ((ctxt->html < 10) && (xmlStrEqual(value, BAD_CAST "body")))
169 ctxt->html = 10;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000170 if (ctxt->nameNr >= ctxt->nameMax) {
171 ctxt->nameMax *= 2;
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000172 ctxt->nameTab = (const xmlChar * *)
Igor Zlatkovicd37c1392003-08-28 10:34:33 +0000173 xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +0000174 ctxt->nameMax *
175 sizeof(ctxt->nameTab[0]));
176 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000177 htmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000178 return (0);
179 }
180 }
181 ctxt->nameTab[ctxt->nameNr] = value;
182 ctxt->name = value;
183 return (ctxt->nameNr++);
184}
185/**
186 * htmlnamePop:
187 * @ctxt: an HTML parser context
188 *
189 * Pops the top element name from the name stack
190 *
191 * Returns the name just removed
192 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000193static const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000194htmlnamePop(htmlParserCtxtPtr ctxt)
195{
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000196 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000197
Daniel Veillard1c732d22002-11-30 11:22:59 +0000198 if (ctxt->nameNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +0000199 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000200 ctxt->nameNr--;
201 if (ctxt->nameNr < 0)
Daniel Veillard24505b02005-07-28 23:49:35 +0000202 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000203 if (ctxt->nameNr > 0)
204 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
205 else
206 ctxt->name = NULL;
207 ret = ctxt->nameTab[ctxt->nameNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000208 ctxt->nameTab[ctxt->nameNr] = NULL;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000209 return (ret);
210}
Owen Taylor3473f882001-02-23 17:55:21 +0000211
Eugene Pimenov615904f2010-03-15 15:16:02 +0100212/**
213 * htmlNodeInfoPush:
214 * @ctxt: an HTML parser context
215 * @value: the node info
216 *
217 * Pushes a new element name on top of the node info stack
218 *
219 * Returns 0 in case of error, the index in the stack otherwise
220 */
221static int
222htmlNodeInfoPush(htmlParserCtxtPtr ctxt, htmlParserNodeInfo *value)
223{
224 if (ctxt->nodeInfoNr >= ctxt->nodeInfoMax) {
225 if (ctxt->nodeInfoMax == 0)
226 ctxt->nodeInfoMax = 5;
227 ctxt->nodeInfoMax *= 2;
228 ctxt->nodeInfoTab = (htmlParserNodeInfo *)
229 xmlRealloc((htmlParserNodeInfo *)ctxt->nodeInfoTab,
230 ctxt->nodeInfoMax *
231 sizeof(ctxt->nodeInfoTab[0]));
232 if (ctxt->nodeInfoTab == NULL) {
233 htmlErrMemory(ctxt, NULL);
234 return (0);
235 }
236 }
237 ctxt->nodeInfoTab[ctxt->nodeInfoNr] = *value;
238 ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
239 return (ctxt->nodeInfoNr++);
240}
241
242/**
243 * htmlNodeInfoPop:
244 * @ctxt: an HTML parser context
245 *
246 * Pops the top element name from the node info stack
247 *
248 * Returns 0 in case of error, the pointer to NodeInfo otherwise
249 */
250static htmlParserNodeInfo *
251htmlNodeInfoPop(htmlParserCtxtPtr ctxt)
252{
253 if (ctxt->nodeInfoNr <= 0)
254 return (NULL);
255 ctxt->nodeInfoNr--;
256 if (ctxt->nodeInfoNr < 0)
257 return (NULL);
258 if (ctxt->nodeInfoNr > 0)
259 ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr - 1];
260 else
261 ctxt->nodeInfo = NULL;
262 return &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
263}
264
Owen Taylor3473f882001-02-23 17:55:21 +0000265/*
266 * Macros for accessing the content. Those should be used only by the parser,
267 * and not exported.
268 *
269 * Dirty macros, i.e. one need to make assumption on the context to use them
270 *
271 * CUR_PTR return the current pointer to the xmlChar to be parsed.
272 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
273 * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
274 * in UNICODE mode. This should be used internally by the parser
275 * only to compare to ASCII values otherwise it would break when
276 * running with UTF-8 encoding.
277 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
278 * to compare on ASCII based substring.
279 * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
280 * it should be used only to compare on ASCII based substring.
281 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +0000282 * strings without newlines within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +0000283 *
284 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
285 *
286 * CURRENT Returns the current char value, with the full decoding of
287 * UTF-8 if we are using this mode. It returns an int.
288 * NEXT Skip to the next character, this does the proper decoding
289 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +0000290 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +0000291 * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
292 */
293
294#define UPPER (toupper(*ctxt->input->cur))
295
Daniel Veillard77a90a72003-03-22 00:04:05 +0000296#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val)
Owen Taylor3473f882001-02-23 17:55:21 +0000297
298#define NXT(val) ctxt->input->cur[(val)]
299
300#define UPP(val) (toupper(ctxt->input->cur[(val)]))
301
302#define CUR_PTR ctxt->input->cur
303
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000304#define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
305 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
306 xmlParserInputShrink(ctxt->input)
Owen Taylor3473f882001-02-23 17:55:21 +0000307
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000308#define GROW if ((ctxt->progressive == 0) && \
309 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
310 xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
Owen Taylor3473f882001-02-23 17:55:21 +0000311
312#define CURRENT ((int) (*ctxt->input->cur))
313
314#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
315
316/* Inported from XML */
317
Daniel Veillard561b7f82002-03-20 21:55:57 +0000318/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
319#define CUR ((int) (*ctxt->input->cur))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000320#define NEXT xmlNextChar(ctxt)
Owen Taylor3473f882001-02-23 17:55:21 +0000321
Daniel Veillard561b7f82002-03-20 21:55:57 +0000322#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
Owen Taylor3473f882001-02-23 17:55:21 +0000323
324
325#define NEXTL(l) do { \
326 if (*(ctxt->input->cur) == '\n') { \
327 ctxt->input->line++; ctxt->input->col = 1; \
328 } else ctxt->input->col++; \
329 ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \
330 } while (0)
Daniel Veillarde77db162009-08-22 11:32:38 +0200331
Owen Taylor3473f882001-02-23 17:55:21 +0000332/************
333 \
334 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
335 if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
336 ************/
337
338#define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
339#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
340
341#define COPY_BUF(l,b,i,v) \
342 if (l == 1) b[i++] = (xmlChar) v; \
343 else i += xmlCopyChar(l,&b[i],v)
344
345/**
Daniel Veillard533ec0e2009-08-12 20:13:38 +0200346 * htmlFindEncoding:
347 * @the HTML parser context
348 *
349 * Ty to find and encoding in the current data available in the input
350 * buffer this is needed to try to switch to the proper encoding when
351 * one face a character error.
352 * That's an heuristic, since it's operating outside of parsing it could
353 * try to use a meta which had been commented out, that's the reason it
354 * should only be used in case of error, not as a default.
355 *
356 * Returns an encoding string or NULL if not found, the string need to
357 * be freed
358 */
359static xmlChar *
360htmlFindEncoding(xmlParserCtxtPtr ctxt) {
361 const xmlChar *start, *cur, *end;
362
363 if ((ctxt == NULL) || (ctxt->input == NULL) ||
364 (ctxt->input->encoding != NULL) || (ctxt->input->buf == NULL) ||
365 (ctxt->input->buf->encoder != NULL))
366 return(NULL);
367 if ((ctxt->input->cur == NULL) || (ctxt->input->end == NULL))
368 return(NULL);
369
370 start = ctxt->input->cur;
371 end = ctxt->input->end;
372 /* we also expect the input buffer to be zero terminated */
373 if (*end != 0)
374 return(NULL);
375
376 cur = xmlStrcasestr(start, BAD_CAST "HTTP-EQUIV");
377 if (cur == NULL)
378 return(NULL);
379 cur = xmlStrcasestr(cur, BAD_CAST "CONTENT");
380 if (cur == NULL)
381 return(NULL);
382 cur = xmlStrcasestr(cur, BAD_CAST "CHARSET=");
383 if (cur == NULL)
384 return(NULL);
385 cur += 8;
386 start = cur;
387 while (((*cur >= 'A') && (*cur <= 'Z')) ||
388 ((*cur >= 'a') && (*cur <= 'z')) ||
389 ((*cur >= '0') && (*cur <= '9')) ||
390 (*cur == '-') || (*cur == '_') || (*cur == ':') || (*cur == '/'))
391 cur++;
392 if (cur == start)
393 return(NULL);
394 return(xmlStrndup(start, cur - start));
395}
396
397/**
Owen Taylor3473f882001-02-23 17:55:21 +0000398 * htmlCurrentChar:
399 * @ctxt: the HTML parser context
400 * @len: pointer to the length of the char read
401 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000402 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000403 * bytes in the input buffer. Implement the end of line normalization:
404 * 2.11 End-of-Line Handling
405 * If the encoding is unspecified, in the case we find an ISO-Latin-1
406 * char, then the encoding converter is plugged in automatically.
407 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000408 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000409 */
410
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000411static int
Owen Taylor3473f882001-02-23 17:55:21 +0000412htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
413 if (ctxt->instate == XML_PARSER_EOF)
414 return(0);
415
416 if (ctxt->token != 0) {
417 *len = 0;
418 return(ctxt->token);
Daniel Veillarde77db162009-08-22 11:32:38 +0200419 }
Owen Taylor3473f882001-02-23 17:55:21 +0000420 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
421 /*
422 * We are supposed to handle UTF8, check it's valid
423 * From rfc2044: encoding of the Unicode values on UTF-8:
424 *
425 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
426 * 0000 0000-0000 007F 0xxxxxxx
427 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
Daniel Veillarde77db162009-08-22 11:32:38 +0200428 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
Owen Taylor3473f882001-02-23 17:55:21 +0000429 *
430 * Check for the 0x110000 limit too
431 */
432 const unsigned char *cur = ctxt->input->cur;
433 unsigned char c;
434 unsigned int val;
435
436 c = *cur;
437 if (c & 0x80) {
Adiel Mittmann8a103792009-08-25 11:27:13 +0200438 if (cur[1] == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000439 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Adiel Mittmann8a103792009-08-25 11:27:13 +0200440 cur = ctxt->input->cur;
441 }
Owen Taylor3473f882001-02-23 17:55:21 +0000442 if ((cur[1] & 0xc0) != 0x80)
443 goto encoding_error;
444 if ((c & 0xe0) == 0xe0) {
445
Adiel Mittmann8a103792009-08-25 11:27:13 +0200446 if (cur[2] == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000447 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Adiel Mittmann8a103792009-08-25 11:27:13 +0200448 cur = ctxt->input->cur;
449 }
Owen Taylor3473f882001-02-23 17:55:21 +0000450 if ((cur[2] & 0xc0) != 0x80)
451 goto encoding_error;
452 if ((c & 0xf0) == 0xf0) {
Adiel Mittmann8a103792009-08-25 11:27:13 +0200453 if (cur[3] == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000454 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Adiel Mittmann8a103792009-08-25 11:27:13 +0200455 cur = ctxt->input->cur;
456 }
Owen Taylor3473f882001-02-23 17:55:21 +0000457 if (((c & 0xf8) != 0xf0) ||
458 ((cur[3] & 0xc0) != 0x80))
459 goto encoding_error;
460 /* 4-byte code */
461 *len = 4;
462 val = (cur[0] & 0x7) << 18;
463 val |= (cur[1] & 0x3f) << 12;
464 val |= (cur[2] & 0x3f) << 6;
465 val |= cur[3] & 0x3f;
466 } else {
467 /* 3-byte code */
468 *len = 3;
469 val = (cur[0] & 0xf) << 12;
470 val |= (cur[1] & 0x3f) << 6;
471 val |= cur[2] & 0x3f;
472 }
473 } else {
474 /* 2-byte code */
475 *len = 2;
476 val = (cur[0] & 0x1f) << 6;
477 val |= cur[1] & 0x3f;
478 }
479 if (!IS_CHAR(val)) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000480 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
481 "Char 0x%X out of allowed range\n", val);
Daniel Veillarde77db162009-08-22 11:32:38 +0200482 }
Owen Taylor3473f882001-02-23 17:55:21 +0000483 return(val);
484 } else {
Daniel Veillard856c6682009-08-24 18:16:56 +0200485 if ((*ctxt->input->cur == 0) &&
486 (ctxt->input->cur < ctxt->input->end)) {
487 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
488 "Char 0x%X out of allowed range\n", 0);
489 *len = 1;
490 return(' ');
491 }
Owen Taylor3473f882001-02-23 17:55:21 +0000492 /* 1-byte code */
493 *len = 1;
494 return((int) *ctxt->input->cur);
495 }
496 }
497 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000498 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000499 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000500 * XML constructs only use < 128 chars
501 */
502 *len = 1;
503 if ((int) *ctxt->input->cur < 0x80)
504 return((int) *ctxt->input->cur);
505
506 /*
507 * Humm this is bad, do an automatic flow conversion
508 */
Daniel Veillard533ec0e2009-08-12 20:13:38 +0200509 {
510 xmlChar * guess;
511 xmlCharEncodingHandlerPtr handler;
512
513 guess = htmlFindEncoding(ctxt);
514 if (guess == NULL) {
515 xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
516 } else {
517 if (ctxt->input->encoding != NULL)
518 xmlFree((xmlChar *) ctxt->input->encoding);
519 ctxt->input->encoding = guess;
520 handler = xmlFindCharEncodingHandler((const char *) guess);
521 if (handler != NULL) {
522 xmlSwitchToEncoding(ctxt, handler);
523 } else {
524 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
525 "Unsupported encoding %s", guess, NULL);
526 }
527 }
528 ctxt->charset = XML_CHAR_ENCODING_UTF8;
529 }
530
Owen Taylor3473f882001-02-23 17:55:21 +0000531 return(xmlCurrentChar(ctxt, len));
532
533encoding_error:
534 /*
535 * If we detect an UTF8 error that probably mean that the
536 * input encoding didn't get properly advertized in the
537 * declaration header. Report the error and switch the encoding
538 * to ISO-Latin-1 (if you don't like this policy, just declare the
539 * encoding !)
540 */
Daniel Veillarda03e3652004-11-02 18:45:30 +0000541 {
542 char buffer[150];
543
Daniel Veillard861101d2007-06-12 08:38:57 +0000544 if (ctxt->input->end - ctxt->input->cur >= 4) {
545 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
546 ctxt->input->cur[0], ctxt->input->cur[1],
547 ctxt->input->cur[2], ctxt->input->cur[3]);
548 } else {
549 snprintf(buffer, 149, "Bytes: 0x%02X\n", ctxt->input->cur[0]);
550 }
Daniel Veillarda03e3652004-11-02 18:45:30 +0000551 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
552 "Input is not proper UTF-8, indicate encoding !\n",
553 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000554 }
555
Daniel Veillarde77db162009-08-22 11:32:38 +0200556 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Owen Taylor3473f882001-02-23 17:55:21 +0000557 *len = 1;
558 return((int) *ctxt->input->cur);
559}
560
561/**
Owen Taylor3473f882001-02-23 17:55:21 +0000562 * htmlSkipBlankChars:
563 * @ctxt: the HTML parser context
564 *
565 * skip all blanks character found at that point in the input streams.
566 *
567 * Returns the number of space chars skipped
568 */
569
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000570static int
Owen Taylor3473f882001-02-23 17:55:21 +0000571htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
572 int res = 0;
573
William M. Brack76e95df2003-10-18 16:20:14 +0000574 while (IS_BLANK_CH(*(ctxt->input->cur))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000575 if ((*ctxt->input->cur == 0) &&
576 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
577 xmlPopInput(ctxt);
578 } else {
579 if (*(ctxt->input->cur) == '\n') {
580 ctxt->input->line++; ctxt->input->col = 1;
581 } else ctxt->input->col++;
582 ctxt->input->cur++;
583 ctxt->nbChars++;
584 if (*ctxt->input->cur == 0)
585 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
586 }
587 res++;
588 }
589 return(res);
590}
591
592
593
594/************************************************************************
595 * *
Daniel Veillarde77db162009-08-22 11:32:38 +0200596 * The list of HTML elements and their properties *
Owen Taylor3473f882001-02-23 17:55:21 +0000597 * *
598 ************************************************************************/
599
600/*
601 * Start Tag: 1 means the start tag can be ommited
602 * End Tag: 1 means the end tag can be ommited
603 * 2 means it's forbidden (empty elements)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000604 * 3 means the tag is stylistic and should be closed easily
Owen Taylor3473f882001-02-23 17:55:21 +0000605 * Depr: this element is deprecated
606 * DTD: 1 means that this element is valid only in the Loose DTD
607 * 2 means that this element is valid only in the Frameset DTD
608 *
Daniel Veillard02bb1702001-06-13 21:11:59 +0000609 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
Daniel Veillard930dfb62003-02-05 10:17:38 +0000610 , subElements , impliedsubelt , Attributes, userdata
Owen Taylor3473f882001-02-23 17:55:21 +0000611 */
Daniel Veillard930dfb62003-02-05 10:17:38 +0000612
613/* Definitions and a couple of vars for HTML Elements */
614
615#define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000616#define NB_FONTSTYLE 8
Daniel Veillard930dfb62003-02-05 10:17:38 +0000617#define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000618#define NB_PHRASE 10
Daniel Veillard491e58e2007-05-02 16:15:18 +0000619#define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
620#define NB_SPECIAL 16
Eugene Pimenov4b41f152010-01-20 14:25:59 +0100621#define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000622#define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL
Eugene Pimenov4b41f152010-01-20 14:25:59 +0100623#define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000624#define NB_BLOCK NB_HEADING + NB_LIST + 14
Daniel Veillard930dfb62003-02-05 10:17:38 +0000625#define FORMCTRL "input", "select", "textarea", "label", "button"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000626#define NB_FORMCTRL 5
Daniel Veillard930dfb62003-02-05 10:17:38 +0000627#define PCDATA
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000628#define NB_PCDATA 0
Daniel Veillard930dfb62003-02-05 10:17:38 +0000629#define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000630#define NB_HEADING 6
Daniel Veillard930dfb62003-02-05 10:17:38 +0000631#define LIST "ul", "ol", "dir", "menu"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000632#define NB_LIST 4
Daniel Veillard930dfb62003-02-05 10:17:38 +0000633#define MODIFIER
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000634#define NB_MODIFIER 0
Daniel Veillard930dfb62003-02-05 10:17:38 +0000635#define FLOW BLOCK,INLINE
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000636#define NB_FLOW NB_BLOCK + NB_INLINE
Daniel Veillard930dfb62003-02-05 10:17:38 +0000637#define EMPTY NULL
638
639
Daniel Veillard065abe82006-07-03 08:55:04 +0000640static const char* const html_flow[] = { FLOW, NULL } ;
641static const char* const html_inline[] = { INLINE, NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000642
643/* placeholders: elts with content but no subelements */
Daniel Veillard065abe82006-07-03 08:55:04 +0000644static const char* const html_pcdata[] = { NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000645#define html_cdata html_pcdata
646
647
648/* ... and for HTML Attributes */
649
650#define COREATTRS "id", "class", "style", "title"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000651#define NB_COREATTRS 4
Daniel Veillard930dfb62003-02-05 10:17:38 +0000652#define I18N "lang", "dir"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000653#define NB_I18N 2
Daniel Veillard930dfb62003-02-05 10:17:38 +0000654#define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000655#define NB_EVENTS 9
Daniel Veillard930dfb62003-02-05 10:17:38 +0000656#define ATTRS COREATTRS,I18N,EVENTS
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000657#define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS
Daniel Veillard930dfb62003-02-05 10:17:38 +0000658#define CELLHALIGN "align", "char", "charoff"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000659#define NB_CELLHALIGN 3
Daniel Veillard930dfb62003-02-05 10:17:38 +0000660#define CELLVALIGN "valign"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000661#define NB_CELLVALIGN 1
Daniel Veillard930dfb62003-02-05 10:17:38 +0000662
Daniel Veillard065abe82006-07-03 08:55:04 +0000663static const char* const html_attrs[] = { ATTRS, NULL } ;
664static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
665static const char* const core_attrs[] = { COREATTRS, NULL } ;
666static const char* const i18n_attrs[] = { I18N, NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000667
668
669/* Other declarations that should go inline ... */
Daniel Veillard065abe82006-07-03 08:55:04 +0000670static const char* const a_attrs[] = { ATTRS, "charset", "type", "name",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000671 "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
672 "tabindex", "onfocus", "onblur", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000673static const char* const target_attr[] = { "target", NULL } ;
674static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ;
675static const char* const alt_attr[] = { "alt", NULL } ;
676static const char* const src_alt_attrs[] = { "src", "alt", NULL } ;
677static const char* const href_attrs[] = { "href", NULL } ;
678static const char* const clear_attrs[] = { "clear", NULL } ;
679static const char* const inline_p[] = { INLINE, "p", NULL } ;
680
681static const char* const flow_param[] = { FLOW, "param", NULL } ;
682static const char* const applet_attrs[] = { COREATTRS , "codebase",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000683 "archive", "alt", "name", "height", "width", "align",
684 "hspace", "vspace", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000685static const char* const area_attrs[] = { "shape", "coords", "href", "nohref",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000686 "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000687static const char* const basefont_attrs[] =
Daniel Veillard930dfb62003-02-05 10:17:38 +0000688 { "id", "size", "color", "face", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000689static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ;
690static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ;
691static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
692static const char* const body_depr[] = { "background", "bgcolor", "text",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000693 "link", "vlink", "alink", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000694static const char* const button_attrs[] = { ATTRS, "name", "value", "type",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000695 "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
696
697
Daniel Veillard065abe82006-07-03 08:55:04 +0000698static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
699static const char* const col_elt[] = { "col", NULL } ;
700static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
701static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ;
702static const char* const dl_contents[] = { "dt", "dd", NULL } ;
703static const char* const compact_attr[] = { "compact", NULL } ;
704static const char* const label_attr[] = { "label", NULL } ;
705static const char* const fieldset_contents[] = { FLOW, "legend" } ;
706static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
707static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
708static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
709static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
710static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
711static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
712static const char* const head_attrs[] = { I18N, "profile", NULL } ;
713static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
714static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
715static const char* const version_attr[] = { "version", NULL } ;
716static const char* const html_content[] = { "head", "body", "frameset", NULL } ;
717static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
718static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
Daniel Veillard491e58e2007-05-02 16:15:18 +0000719static 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 } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000720static 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 } ;
721static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
722static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
723static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ;
724static const char* const align_attr[] = { "align", NULL } ;
725static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
726static const char* const map_contents[] = { BLOCK, "area", NULL } ;
727static const char* const name_attr[] = { "name", NULL } ;
728static const char* const action_attr[] = { "action", NULL } ;
729static const char* const blockli_elt[] = { BLOCK, "li", NULL } ;
730static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ;
731static const char* const content_attr[] = { "content", NULL } ;
732static const char* const type_attr[] = { "type", NULL } ;
733static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
734static const char* const object_contents[] = { FLOW, "param", NULL } ;
735static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
736static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
737static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ;
738static const char* const option_elt[] = { "option", NULL } ;
739static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
740static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
741static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
742static const char* const width_attr[] = { "width", NULL } ;
743static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
744static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
745static const char* const language_attr[] = { "language", NULL } ;
746static const char* const select_content[] = { "optgroup", "option", NULL } ;
747static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
748static const char* const style_attrs[] = { I18N, "media", "title", NULL } ;
Roland Steiner04f8eef2009-05-12 09:16:16 +0200749static const char* const table_attrs[] = { ATTRS, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000750static const char* const table_depr[] = { "align", "bgcolor", NULL } ;
751static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
752static const char* const tr_elt[] = { "tr", NULL } ;
753static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
754static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
755static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
756static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
757static const char* const tr_contents[] = { "th", "td", NULL } ;
758static const char* const bgcolor_attr[] = { "bgcolor", NULL } ;
759static const char* const li_elt[] = { "li", NULL } ;
760static const char* const ul_depr[] = { "type", "compact", NULL} ;
761static const char* const dir_attr[] = { "dir", NULL} ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000762
763#define DECL (const char**)
764
Daniel Veillard22090732001-07-16 00:06:07 +0000765static const htmlElemDesc
766html40ElementTable[] = {
Daniel Veillard930dfb62003-02-05 10:17:38 +0000767{ "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
768 DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
769},
770{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
771 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
772},
773{ "acronym", 0, 0, 0, 0, 0, 0, 1, "",
774 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
775},
776{ "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
777 DECL inline_p , NULL , DECL html_attrs, NULL, NULL
778},
779{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
780 DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
781},
782{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
783 EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
784},
785{ "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
786 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
787},
788{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
789 EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
790},
791{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
792 EMPTY , NULL , NULL, DECL basefont_attrs, NULL
793},
794{ "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
795 DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
796},
797{ "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
798 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
799},
800{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
801 DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
802},
803{ "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
804 DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
805},
806{ "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
807 EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
808},
809{ "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
810 DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
811},
812{ "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
813 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
814},
815{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
816 DECL html_flow , NULL , NULL, DECL html_attrs, NULL
817},
818{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
819 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
820},
821{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
822 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
823},
824{ "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
825 EMPTY , NULL , DECL col_attrs , NULL, NULL
826},
827{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
828 DECL col_elt , "col" , DECL col_attrs , NULL, NULL
829},
830{ "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
831 DECL html_flow , NULL , DECL html_attrs, NULL, NULL
832},
833{ "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
834 DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
835},
836{ "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
837 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
838},
839{ "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
840 DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
841},
842{ "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
843 DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
844},
845{ "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
William M. Bracke978ae22007-03-21 06:16:02 +0000846 DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL
Daniel Veillard930dfb62003-02-05 10:17:38 +0000847},
848{ "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
849 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
850},
851{ "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
852 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
853},
Daniel Veillard640f89e2008-01-11 06:24:09 +0000854{ "embed", 0, 1, 0, 0, 1, 1, 1, "generic embedded object ",
Daniel Veillard491e58e2007-05-02 16:15:18 +0000855 EMPTY, NULL, DECL embed_attrs, NULL, NULL
856},
Daniel Veillard930dfb62003-02-05 10:17:38 +0000857{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
858 DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
859},
860{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
861 DECL html_inline, NULL, NULL, DECL font_attrs, NULL
862},
863{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
864 DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
865},
866{ "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
867 EMPTY, NULL, NULL, DECL frame_attrs, NULL
868},
869{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
870 DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
871},
872{ "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
873 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
874},
875{ "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
876 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
877},
878{ "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
879 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
880},
881{ "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
882 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
883},
884{ "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
885 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
886},
887{ "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
888 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
889},
890{ "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
891 DECL head_contents, NULL, DECL head_attrs, NULL, NULL
892},
893{ "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
894 EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
895},
896{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
897 DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
898},
899{ "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
900 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
901},
902{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
903 DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
904},
905{ "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
William M. Bracke978ae22007-03-21 06:16:02 +0000906 EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs
Daniel Veillard930dfb62003-02-05 10:17:38 +0000907},
908{ "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
909 EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
910},
911{ "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
912 DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
913},
914{ "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
915 EMPTY, NULL, NULL, DECL prompt_attrs, NULL
916},
917{ "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
918 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
919},
920{ "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
921 DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
922},
923{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
924 DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
925},
926{ "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
927 DECL html_flow, NULL, DECL html_attrs, NULL, NULL
928},
929{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
930 EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
931},
932{ "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
William M. Bracke978ae22007-03-21 06:16:02 +0000933 DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000934},
935{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
936 DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
937},
938{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
939 EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
940},
941{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
942 DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
943},
944{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
945 DECL html_flow, "div", DECL html_attrs, NULL, NULL
946},
947{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
948 DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
949},
950{ "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
951 DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
952},
953{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
William M. Bracke978ae22007-03-21 06:16:02 +0000954 DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000955},
956{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
957 DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
958},
959{ "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
960 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
961},
962{ "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
William M. Bracke978ae22007-03-21 06:16:02 +0000963 EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000964},
965{ "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
966 DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
967},
968{ "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
969 DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
970},
971{ "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
972 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
973},
974{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
975 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
976},
977{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
978 DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
979},
980{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
981 DECL select_content, NULL, DECL select_attrs, NULL, NULL
982},
983{ "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
984 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
985},
986{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
987 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
988},
989{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
990 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
991},
992{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
993 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
994},
995{ "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
996 DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
997},
998{ "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
999 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1000},
1001{ "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
1002 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1003},
1004{ "table", 0, 0, 0, 0, 0, 0, 0, "",
1005 DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
1006},
1007{ "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
1008 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1009},
1010{ "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
1011 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
1012},
1013{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
1014 DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
1015},
1016{ "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
1017 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1018},
1019{ "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
1020 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
1021},
1022{ "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
1023 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1024},
1025{ "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
1026 DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
1027},
1028{ "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
1029 DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
1030},
1031{ "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
1032 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1033},
1034{ "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
1035 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
1036},
1037{ "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
1038 DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
1039},
1040{ "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
1041 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1042}
Owen Taylor3473f882001-02-23 17:55:21 +00001043};
1044
1045/*
Owen Taylor3473f882001-02-23 17:55:21 +00001046 * start tags that imply the end of current element
1047 */
Daniel Veillard065abe82006-07-03 08:55:04 +00001048static const char * const htmlStartClose[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001049"form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
1050 "dl", "ul", "ol", "menu", "dir", "address", "pre",
1051 "listing", "xmp", "head", NULL,
1052"head", "p", NULL,
1053"title", "p", NULL,
1054"body", "head", "style", "link", "title", "p", NULL,
Daniel Veillard25d5d9a2004-04-05 07:08:42 +00001055"frameset", "head", "style", "link", "title", "p", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001056"li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
1057 "pre", "listing", "xmp", "head", "li", NULL,
1058"hr", "p", "head", NULL,
1059"h1", "p", "head", NULL,
1060"h2", "p", "head", NULL,
1061"h3", "p", "head", NULL,
1062"h4", "p", "head", NULL,
1063"h5", "p", "head", NULL,
1064"h6", "p", "head", NULL,
1065"dir", "p", "head", NULL,
1066"address", "p", "head", "ul", NULL,
1067"pre", "p", "head", "ul", NULL,
1068"listing", "p", "head", NULL,
1069"xmp", "p", "head", NULL,
1070"blockquote", "p", "head", NULL,
1071"dl", "p", "dt", "menu", "dir", "address", "pre", "listing",
1072 "xmp", "head", NULL,
1073"dt", "p", "menu", "dir", "address", "pre", "listing", "xmp",
1074 "head", "dd", NULL,
1075"dd", "p", "menu", "dir", "address", "pre", "listing", "xmp",
1076 "head", "dt", NULL,
1077"ul", "p", "head", "ol", "menu", "dir", "address", "pre",
1078 "listing", "xmp", NULL,
1079"ol", "p", "head", "ul", NULL,
1080"menu", "p", "head", "ul", NULL,
Daniel Veillard6339c1a2009-08-24 11:59:51 +02001081"p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", FONTSTYLE, NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001082"div", "p", "head", NULL,
1083"noscript", "p", "head", NULL,
1084"center", "font", "b", "i", "p", "head", NULL,
1085"a", "a", NULL,
1086"caption", "p", NULL,
1087"colgroup", "caption", "colgroup", "col", "p", NULL,
1088"col", "caption", "col", "p", NULL,
1089"table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
1090 "listing", "xmp", "a", NULL,
Daniel Veillard43dadeb2001-04-24 11:23:35 +00001091"th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Daniel Veillarde77db162009-08-22 11:32:38 +02001092"td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00001093"tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
1094"thead", "caption", "col", "colgroup", NULL,
1095"tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead",
1096 "tbody", "p", NULL,
1097"tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead",
1098 "tfoot", "tbody", "p", NULL,
1099"optgroup", "option", NULL,
1100"option", "option", NULL,
1101"fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
1102 "pre", "listing", "xmp", "a", NULL,
1103NULL
1104};
1105
1106/*
1107 * The list of HTML elements which are supposed not to have
1108 * CDATA content and where a p element will be implied
1109 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001110 * TODO: extend that list by reading the HTML SGML DTD on
Owen Taylor3473f882001-02-23 17:55:21 +00001111 * implied paragraph
1112 */
Daniel Veillard065abe82006-07-03 08:55:04 +00001113static const char *const htmlNoContentElements[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001114 "html",
1115 "head",
Owen Taylor3473f882001-02-23 17:55:21 +00001116 NULL
1117};
1118
1119/*
1120 * The list of HTML attributes which are of content %Script;
1121 * NOTE: when adding ones, check htmlIsScriptAttribute() since
1122 * it assumes the name starts with 'on'
1123 */
Daniel Veillard065abe82006-07-03 08:55:04 +00001124static const char *const htmlScriptAttributes[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001125 "onclick",
1126 "ondblclick",
1127 "onmousedown",
1128 "onmouseup",
1129 "onmouseover",
1130 "onmousemove",
1131 "onmouseout",
1132 "onkeypress",
1133 "onkeydown",
1134 "onkeyup",
1135 "onload",
1136 "onunload",
1137 "onfocus",
1138 "onblur",
1139 "onsubmit",
1140 "onrest",
1141 "onchange",
1142 "onselect"
1143};
1144
Daniel Veillarda2bc3682001-05-03 08:27:20 +00001145/*
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001146 * This table is used by the htmlparser to know what to do with
1147 * broken html pages. By assigning different priorities to different
1148 * elements the parser can decide how to handle extra endtags.
1149 * Endtags are only allowed to close elements with lower or equal
1150 * priority.
Daniel Veillarde77db162009-08-22 11:32:38 +02001151 */
Daniel Veillarda2bc3682001-05-03 08:27:20 +00001152
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001153typedef struct {
1154 const char *name;
1155 int priority;
1156} elementPriority;
1157
Daniel Veillard22090732001-07-16 00:06:07 +00001158static const elementPriority htmlEndPriority[] = {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001159 {"div", 150},
1160 {"td", 160},
1161 {"th", 160},
1162 {"tr", 170},
1163 {"thead", 180},
1164 {"tbody", 180},
1165 {"tfoot", 180},
1166 {"table", 190},
1167 {"head", 200},
1168 {"body", 200},
1169 {"html", 220},
1170 {NULL, 100} /* Default priority */
1171};
Owen Taylor3473f882001-02-23 17:55:21 +00001172
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001173static const char** htmlStartCloseIndex[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001174static int htmlStartCloseIndexinitialized = 0;
1175
1176/************************************************************************
1177 * *
Daniel Veillarde77db162009-08-22 11:32:38 +02001178 * functions to handle HTML specific data *
Owen Taylor3473f882001-02-23 17:55:21 +00001179 * *
1180 ************************************************************************/
1181
1182/**
1183 * htmlInitAutoClose:
1184 *
1185 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1186 * This is not reentrant. Call xmlInitParser() once before processing in
1187 * case of use in multithreaded programs.
1188 */
1189void
1190htmlInitAutoClose(void) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001191 int indx, i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001192
1193 if (htmlStartCloseIndexinitialized) return;
1194
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001195 for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL;
1196 indx = 0;
1197 while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) {
Daniel Veillard28aac0b2006-10-16 08:31:18 +00001198 htmlStartCloseIndex[indx++] = (const char**) &htmlStartClose[i];
Owen Taylor3473f882001-02-23 17:55:21 +00001199 while (htmlStartClose[i] != NULL) i++;
1200 i++;
1201 }
1202 htmlStartCloseIndexinitialized = 1;
1203}
1204
1205/**
1206 * htmlTagLookup:
1207 * @tag: The tag name in lowercase
1208 *
1209 * Lookup the HTML tag in the ElementTable
1210 *
1211 * Returns the related htmlElemDescPtr or NULL if not found.
1212 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001213const htmlElemDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001214htmlTagLookup(const xmlChar *tag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001215 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001216
1217 for (i = 0; i < (sizeof(html40ElementTable) /
1218 sizeof(html40ElementTable[0]));i++) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +00001219 if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
William M. Brack78637da2003-07-31 14:47:38 +00001220 return((htmlElemDescPtr) &html40ElementTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001221 }
1222 return(NULL);
1223}
1224
1225/**
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001226 * htmlGetEndPriority:
1227 * @name: The name of the element to look up the priority for.
Daniel Veillarde77db162009-08-22 11:32:38 +02001228 *
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001229 * Return value: The "endtag" priority.
1230 **/
1231static int
1232htmlGetEndPriority (const xmlChar *name) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001233 int i = 0;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001234
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001235 while ((htmlEndPriority[i].name != NULL) &&
1236 (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
1237 i++;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001238
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001239 return(htmlEndPriority[i].priority);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001240}
1241
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001242
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001243/**
Owen Taylor3473f882001-02-23 17:55:21 +00001244 * htmlCheckAutoClose:
1245 * @newtag: The new tag name
1246 * @oldtag: The old tag name
1247 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001248 * Checks whether the new tag is one of the registered valid tags for
1249 * closing old.
Owen Taylor3473f882001-02-23 17:55:21 +00001250 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1251 *
1252 * Returns 0 if no, 1 if yes.
1253 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001254static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001255htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
1256{
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001257 int i, indx;
1258 const char **closed = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001259
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001260 if (htmlStartCloseIndexinitialized == 0)
1261 htmlInitAutoClose();
Owen Taylor3473f882001-02-23 17:55:21 +00001262
1263 /* inefficient, but not a big deal */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001264 for (indx = 0; indx < 100; indx++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001265 closed = htmlStartCloseIndex[indx];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001266 if (closed == NULL)
1267 return (0);
1268 if (xmlStrEqual(BAD_CAST * closed, newtag))
1269 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001270 }
1271
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001272 i = closed - htmlStartClose;
Owen Taylor3473f882001-02-23 17:55:21 +00001273 i++;
1274 while (htmlStartClose[i] != NULL) {
1275 if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001276 return (1);
1277 }
1278 i++;
Owen Taylor3473f882001-02-23 17:55:21 +00001279 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001280 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001281}
1282
1283/**
1284 * htmlAutoCloseOnClose:
1285 * @ctxt: an HTML parser context
1286 * @newtag: The new tag name
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001287 * @force: force the tag closure
Owen Taylor3473f882001-02-23 17:55:21 +00001288 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001289 * The HTML DTD allows an ending tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001290 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001291static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001292htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1293{
1294 const htmlElemDesc *info;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001295 int i, priority;
Owen Taylor3473f882001-02-23 17:55:21 +00001296
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001297 priority = htmlGetEndPriority(newtag);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001298
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001299 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001300
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001301 if (xmlStrEqual(newtag, ctxt->nameTab[i]))
1302 break;
1303 /*
1304 * A missplaced endtag can only close elements with lower
1305 * or equal priority, so if we find an element with higher
1306 * priority before we find an element with
Daniel Veillarde77db162009-08-22 11:32:38 +02001307 * matching name, we just ignore this endtag
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001308 */
1309 if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
1310 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001311 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001312 if (i < 0)
1313 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001314
1315 while (!xmlStrEqual(newtag, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001316 info = htmlTagLookup(ctxt->name);
Daniel Veillardf403d292003-10-05 13:51:35 +00001317 if ((info != NULL) && (info->endTag == 3)) {
1318 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
1319 "Opening and ending tag mismatch: %s and %s\n",
Daniel Veillard05bcb7e2003-10-19 14:26:34 +00001320 newtag, ctxt->name);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001321 }
1322 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1323 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001324 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001325 }
1326}
1327
1328/**
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001329 * htmlAutoCloseOnEnd:
1330 * @ctxt: an HTML parser context
1331 *
1332 * Close all remaining tags at the end of the stream
1333 */
1334static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001335htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
1336{
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001337 int i;
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001338
William M. Brack899e64a2003-09-26 18:03:42 +00001339 if (ctxt->nameNr == 0)
1340 return;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001341 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001342 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1343 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001344 htmlnamePop(ctxt);
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001345 }
1346}
1347
1348/**
Owen Taylor3473f882001-02-23 17:55:21 +00001349 * htmlAutoClose:
1350 * @ctxt: an HTML parser context
1351 * @newtag: The new tag name or NULL
1352 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001353 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001354 * The list is kept in htmlStartClose array. This function is
1355 * called when a new tag has been detected and generates the
1356 * appropriates closes if possible/needed.
1357 * If newtag is NULL this mean we are at the end of the resource
Daniel Veillarde77db162009-08-22 11:32:38 +02001358 * and we should check
Owen Taylor3473f882001-02-23 17:55:21 +00001359 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001360static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001361htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1362{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001363 while ((newtag != NULL) && (ctxt->name != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001364 (htmlCheckAutoClose(newtag, ctxt->name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001365 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1366 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001367 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001368 }
1369 if (newtag == NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001370 htmlAutoCloseOnEnd(ctxt);
1371 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001372 }
1373 while ((newtag == NULL) && (ctxt->name != NULL) &&
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001374 ((xmlStrEqual(ctxt->name, BAD_CAST "head")) ||
1375 (xmlStrEqual(ctxt->name, BAD_CAST "body")) ||
1376 (xmlStrEqual(ctxt->name, BAD_CAST "html")))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001377 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1378 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001379 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001380 }
Owen Taylor3473f882001-02-23 17:55:21 +00001381}
1382
1383/**
1384 * htmlAutoCloseTag:
1385 * @doc: the HTML document
1386 * @name: The tag name
1387 * @elem: the HTML element
1388 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001389 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001390 * The list is kept in htmlStartClose array. This function checks
1391 * if the element or one of it's children would autoclose the
1392 * given tag.
1393 *
1394 * Returns 1 if autoclose, 0 otherwise
1395 */
1396int
1397htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
1398 htmlNodePtr child;
1399
1400 if (elem == NULL) return(1);
1401 if (xmlStrEqual(name, elem->name)) return(0);
1402 if (htmlCheckAutoClose(elem->name, name)) return(1);
1403 child = elem->children;
1404 while (child != NULL) {
1405 if (htmlAutoCloseTag(doc, name, child)) return(1);
1406 child = child->next;
1407 }
1408 return(0);
1409}
1410
1411/**
1412 * htmlIsAutoClosed:
1413 * @doc: the HTML document
1414 * @elem: the HTML element
1415 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001416 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001417 * The list is kept in htmlStartClose array. This function checks
1418 * if a tag is autoclosed by one of it's child
1419 *
1420 * Returns 1 if autoclosed, 0 otherwise
1421 */
1422int
1423htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
1424 htmlNodePtr child;
1425
1426 if (elem == NULL) return(1);
1427 child = elem->children;
1428 while (child != NULL) {
1429 if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
1430 child = child->next;
1431 }
1432 return(0);
1433}
1434
1435/**
1436 * htmlCheckImplied:
1437 * @ctxt: an HTML parser context
1438 * @newtag: The new tag name
1439 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001440 * The HTML DTD allows a tag to exists only implicitly
Owen Taylor3473f882001-02-23 17:55:21 +00001441 * called when a new tag has been detected and generates the
1442 * appropriates implicit tags if missing
1443 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001444static void
Owen Taylor3473f882001-02-23 17:55:21 +00001445htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
Daniel Veillardb468f742009-08-24 18:45:33 +02001446 int i;
1447
Daniel Veillarde20fb5a2010-01-29 20:47:08 +01001448 if (ctxt->options & HTML_PARSE_NOIMPLIED)
1449 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001450 if (!htmlOmittedDefaultValue)
1451 return;
1452 if (xmlStrEqual(newtag, BAD_CAST"html"))
1453 return;
1454 if (ctxt->nameNr <= 0) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001455 htmlnamePush(ctxt, BAD_CAST"html");
Owen Taylor3473f882001-02-23 17:55:21 +00001456 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1457 ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
1458 }
1459 if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1460 return;
Daniel Veillarde77db162009-08-22 11:32:38 +02001461 if ((ctxt->nameNr <= 1) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001462 ((xmlStrEqual(newtag, BAD_CAST"script")) ||
1463 (xmlStrEqual(newtag, BAD_CAST"style")) ||
1464 (xmlStrEqual(newtag, BAD_CAST"meta")) ||
1465 (xmlStrEqual(newtag, BAD_CAST"link")) ||
1466 (xmlStrEqual(newtag, BAD_CAST"title")) ||
1467 (xmlStrEqual(newtag, BAD_CAST"base")))) {
Daniel Veillard029a04d2009-08-24 12:50:23 +02001468 if (ctxt->html >= 3) {
1469 /* we already saw or generated an <head> before */
1470 return;
1471 }
1472 /*
1473 * dropped OBJECT ... i you put it first BODY will be
1474 * assumed !
1475 */
1476 htmlnamePush(ctxt, BAD_CAST"head");
1477 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1478 ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001479 } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
1480 (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
1481 (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
Daniel Veillard029a04d2009-08-24 12:50:23 +02001482 if (ctxt->html >= 10) {
1483 /* we already saw or generated a <body> before */
1484 return;
1485 }
Owen Taylor3473f882001-02-23 17:55:21 +00001486 for (i = 0;i < ctxt->nameNr;i++) {
1487 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
1488 return;
1489 }
1490 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
1491 return;
1492 }
1493 }
Daniel Veillarde77db162009-08-22 11:32:38 +02001494
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001495 htmlnamePush(ctxt, BAD_CAST"body");
Owen Taylor3473f882001-02-23 17:55:21 +00001496 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1497 ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
1498 }
1499}
1500
1501/**
1502 * htmlCheckParagraph
1503 * @ctxt: an HTML parser context
1504 *
1505 * Check whether a p element need to be implied before inserting
1506 * characters in the current element.
1507 *
1508 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1509 * in case of error.
1510 */
1511
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001512static int
Owen Taylor3473f882001-02-23 17:55:21 +00001513htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
1514 const xmlChar *tag;
1515 int i;
1516
1517 if (ctxt == NULL)
1518 return(-1);
1519 tag = ctxt->name;
1520 if (tag == NULL) {
1521 htmlAutoClose(ctxt, BAD_CAST"p");
1522 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001523 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001524 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1525 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1526 return(1);
1527 }
1528 if (!htmlOmittedDefaultValue)
1529 return(0);
1530 for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1531 if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
Owen Taylor3473f882001-02-23 17:55:21 +00001532 htmlAutoClose(ctxt, BAD_CAST"p");
1533 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001534 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001535 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1536 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1537 return(1);
1538 }
1539 }
1540 return(0);
1541}
1542
1543/**
1544 * htmlIsScriptAttribute:
1545 * @name: an attribute name
1546 *
1547 * Check if an attribute is of content type Script
1548 *
1549 * Returns 1 is the attribute is a script 0 otherwise
1550 */
1551int
1552htmlIsScriptAttribute(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001553 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001554
1555 if (name == NULL)
Daniel Veillarde77db162009-08-22 11:32:38 +02001556 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 /*
1558 * all script attributes start with 'on'
1559 */
1560 if ((name[0] != 'o') || (name[1] != 'n'))
Daniel Veillarde77db162009-08-22 11:32:38 +02001561 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001562 for (i = 0;
1563 i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1564 i++) {
1565 if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1566 return(1);
1567 }
1568 return(0);
1569}
1570
1571/************************************************************************
1572 * *
Daniel Veillarde77db162009-08-22 11:32:38 +02001573 * The list of HTML predefined entities *
Owen Taylor3473f882001-02-23 17:55:21 +00001574 * *
1575 ************************************************************************/
1576
1577
Daniel Veillard22090732001-07-16 00:06:07 +00001578static const htmlEntityDesc html40EntitiesTable[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001579/*
1580 * the 4 absolute ones, plus apostrophe.
1581 */
1582{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1583{ 38, "amp", "ampersand, U+0026 ISOnum" },
1584{ 39, "apos", "single quote" },
1585{ 60, "lt", "less-than sign, U+003C ISOnum" },
1586{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1587
1588/*
1589 * A bunch still in the 128-255 range
1590 * Replacing them depend really on the charset used.
1591 */
1592{ 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1593{ 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1594{ 162, "cent", "cent sign, U+00A2 ISOnum" },
1595{ 163, "pound","pound sign, U+00A3 ISOnum" },
1596{ 164, "curren","currency sign, U+00A4 ISOnum" },
1597{ 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1598{ 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1599{ 167, "sect", "section sign, U+00A7 ISOnum" },
1600{ 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1601{ 169, "copy", "copyright sign, U+00A9 ISOnum" },
1602{ 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1603{ 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1604{ 172, "not", "not sign, U+00AC ISOnum" },
1605{ 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1606{ 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1607{ 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1608{ 176, "deg", "degree sign, U+00B0 ISOnum" },
1609{ 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1610{ 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1611{ 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1612{ 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1613{ 181, "micro","micro sign, U+00B5 ISOnum" },
1614{ 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1615{ 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1616{ 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1617{ 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1618{ 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1619{ 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1620{ 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1621{ 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1622{ 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1623{ 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1624{ 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1625{ 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1626{ 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1627{ 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1628{ 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1629{ 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1630{ 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1631{ 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1632{ 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1633{ 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1634{ 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1635{ 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1636{ 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1637{ 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1638{ 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1639{ 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1640{ 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1641{ 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1642{ 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1643{ 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1644{ 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1645{ 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1646{ 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1647{ 215, "times","multiplication sign, U+00D7 ISOnum" },
1648{ 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1649{ 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1650{ 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1651{ 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1652{ 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1653{ 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1654{ 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1655{ 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1656{ 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1657{ 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1658{ 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1659{ 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1660{ 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1661{ 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1662{ 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1663{ 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1664{ 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1665{ 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1666{ 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1667{ 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1668{ 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1669{ 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1670{ 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1671{ 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1672{ 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1673{ 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1674{ 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1675{ 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1676{ 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1677{ 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1678{ 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1679{ 247, "divide","division sign, U+00F7 ISOnum" },
1680{ 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1681{ 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1682{ 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1683{ 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1684{ 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1685{ 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1686{ 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1687{ 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1688
1689{ 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1690{ 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1691{ 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1692{ 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1693{ 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1694
1695/*
1696 * Anything below should really be kept as entities references
1697 */
1698{ 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1699
1700{ 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1701{ 732, "tilde","small tilde, U+02DC ISOdia" },
1702
1703{ 913, "Alpha","greek capital letter alpha, U+0391" },
1704{ 914, "Beta", "greek capital letter beta, U+0392" },
1705{ 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1706{ 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1707{ 917, "Epsilon","greek capital letter epsilon, U+0395" },
1708{ 918, "Zeta", "greek capital letter zeta, U+0396" },
1709{ 919, "Eta", "greek capital letter eta, U+0397" },
1710{ 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1711{ 921, "Iota", "greek capital letter iota, U+0399" },
1712{ 922, "Kappa","greek capital letter kappa, U+039A" },
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001713{ 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
Owen Taylor3473f882001-02-23 17:55:21 +00001714{ 924, "Mu", "greek capital letter mu, U+039C" },
1715{ 925, "Nu", "greek capital letter nu, U+039D" },
1716{ 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1717{ 927, "Omicron","greek capital letter omicron, U+039F" },
1718{ 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1719{ 929, "Rho", "greek capital letter rho, U+03A1" },
1720{ 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1721{ 932, "Tau", "greek capital letter tau, U+03A4" },
1722{ 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1723{ 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1724{ 935, "Chi", "greek capital letter chi, U+03A7" },
1725{ 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1726{ 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1727
1728{ 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1729{ 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1730{ 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1731{ 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1732{ 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1733{ 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1734{ 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1735{ 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1736{ 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1737{ 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1738{ 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1739{ 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1740{ 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1741{ 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1742{ 959, "omicron","greek small letter omicron, U+03BF NEW" },
1743{ 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1744{ 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1745{ 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1746{ 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1747{ 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1748{ 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1749{ 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1750{ 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1751{ 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1752{ 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1753{ 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1754{ 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1755{ 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1756
1757{ 8194, "ensp", "en space, U+2002 ISOpub" },
1758{ 8195, "emsp", "em space, U+2003 ISOpub" },
1759{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1760{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1761{ 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1762{ 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1763{ 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1764{ 8211, "ndash","en dash, U+2013 ISOpub" },
1765{ 8212, "mdash","em dash, U+2014 ISOpub" },
1766{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1767{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1768{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1769{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1770{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1771{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1772{ 8224, "dagger","dagger, U+2020 ISOpub" },
1773{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1774
1775{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1776{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1777
1778{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1779
1780{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1781{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1782
1783{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1784{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1785
1786{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1787{ 8260, "frasl","fraction slash, U+2044 NEW" },
1788
1789{ 8364, "euro", "euro sign, U+20AC NEW" },
1790
1791{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1792{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1793{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1794{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
1795{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
1796{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
1797{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
1798{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
1799{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
1800{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
1801{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
1802{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
1803{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
1804{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
1805{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
1806{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
1807
1808{ 8704, "forall","for all, U+2200 ISOtech" },
1809{ 8706, "part", "partial differential, U+2202 ISOtech" },
1810{ 8707, "exist","there exists, U+2203 ISOtech" },
1811{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
1812{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
1813{ 8712, "isin", "element of, U+2208 ISOtech" },
1814{ 8713, "notin","not an element of, U+2209 ISOtech" },
1815{ 8715, "ni", "contains as member, U+220B ISOtech" },
1816{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001817{ 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
Owen Taylor3473f882001-02-23 17:55:21 +00001818{ 8722, "minus","minus sign, U+2212 ISOtech" },
1819{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
1820{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
1821{ 8733, "prop", "proportional to, U+221D ISOtech" },
1822{ 8734, "infin","infinity, U+221E ISOtech" },
1823{ 8736, "ang", "angle, U+2220 ISOamso" },
1824{ 8743, "and", "logical and = wedge, U+2227 ISOtech" },
1825{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
1826{ 8745, "cap", "intersection = cap, U+2229 ISOtech" },
1827{ 8746, "cup", "union = cup, U+222A ISOtech" },
1828{ 8747, "int", "integral, U+222B ISOtech" },
1829{ 8756, "there4","therefore, U+2234 ISOtech" },
1830{ 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
1831{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
1832{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
1833{ 8800, "ne", "not equal to, U+2260 ISOtech" },
1834{ 8801, "equiv","identical to, U+2261 ISOtech" },
1835{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
1836{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
1837{ 8834, "sub", "subset of, U+2282 ISOtech" },
1838{ 8835, "sup", "superset of, U+2283 ISOtech" },
1839{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
1840{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
1841{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
1842{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
1843{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
1844{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
1845{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
1846{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
1847{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
1848{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
1849{ 8971, "rfloor","right floor, U+230B ISOamsc" },
1850{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
1851{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
1852{ 9674, "loz", "lozenge, U+25CA ISOpub" },
1853
1854{ 9824, "spades","black spade suit, U+2660 ISOpub" },
1855{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
1856{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
1857{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
1858
1859};
1860
1861/************************************************************************
1862 * *
1863 * Commodity functions to handle entities *
1864 * *
1865 ************************************************************************/
1866
1867/*
1868 * Macro used to grow the current buffer.
1869 */
1870#define growBuffer(buffer) { \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001871 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001872 buffer##_size *= 2; \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001873 tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
1874 if (tmp == NULL) { \
Daniel Veillardf403d292003-10-05 13:51:35 +00001875 htmlErrMemory(ctxt, "growing buffer\n"); \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001876 xmlFree(buffer); \
Owen Taylor3473f882001-02-23 17:55:21 +00001877 return(NULL); \
1878 } \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001879 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001880}
1881
1882/**
1883 * htmlEntityLookup:
1884 * @name: the entity name
1885 *
1886 * Lookup the given entity in EntitiesTable
1887 *
1888 * TODO: the linear scan is really ugly, an hash table is really needed.
1889 *
1890 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1891 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001892const htmlEntityDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001893htmlEntityLookup(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001894 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001895
1896 for (i = 0;i < (sizeof(html40EntitiesTable)/
1897 sizeof(html40EntitiesTable[0]));i++) {
1898 if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
William M. Brack78637da2003-07-31 14:47:38 +00001899 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001900 }
1901 }
1902 return(NULL);
1903}
1904
1905/**
1906 * htmlEntityValueLookup:
1907 * @value: the entity's unicode value
1908 *
1909 * Lookup the given entity in EntitiesTable
1910 *
1911 * TODO: the linear scan is really ugly, an hash table is really needed.
1912 *
1913 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1914 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001915const htmlEntityDesc *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001916htmlEntityValueLookup(unsigned int value) {
1917 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001918
1919 for (i = 0;i < (sizeof(html40EntitiesTable)/
1920 sizeof(html40EntitiesTable[0]));i++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001921 if (html40EntitiesTable[i].value >= value) {
1922 if (html40EntitiesTable[i].value > value)
Owen Taylor3473f882001-02-23 17:55:21 +00001923 break;
William M. Brack78637da2003-07-31 14:47:38 +00001924 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001925 }
Owen Taylor3473f882001-02-23 17:55:21 +00001926 }
1927 return(NULL);
1928}
1929
1930/**
1931 * UTF8ToHtml:
1932 * @out: a pointer to an array of bytes to store the result
1933 * @outlen: the length of @out
1934 * @in: a pointer to an array of UTF-8 chars
1935 * @inlen: the length of @in
1936 *
1937 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1938 * plus HTML entities block of chars out.
1939 *
1940 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1941 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001942 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001943 * The value of @outlen after return is the number of octets consumed.
1944 */
1945int
1946UTF8ToHtml(unsigned char* out, int *outlen,
1947 const unsigned char* in, int *inlen) {
1948 const unsigned char* processed = in;
1949 const unsigned char* outend;
1950 const unsigned char* outstart = out;
1951 const unsigned char* instart = in;
1952 const unsigned char* inend;
1953 unsigned int c, d;
1954 int trailing;
1955
Daniel Veillardce682bc2004-11-05 17:22:25 +00001956 if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001957 if (in == NULL) {
1958 /*
1959 * initialization nothing to do
1960 */
1961 *outlen = 0;
1962 *inlen = 0;
1963 return(0);
1964 }
1965 inend = in + (*inlen);
1966 outend = out + (*outlen);
1967 while (in < inend) {
1968 d = *in++;
1969 if (d < 0x80) { c= d; trailing= 0; }
1970 else if (d < 0xC0) {
1971 /* trailing byte in leading position */
1972 *outlen = out - outstart;
1973 *inlen = processed - instart;
1974 return(-2);
1975 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1976 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1977 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1978 else {
1979 /* no chance for this in Ascii */
1980 *outlen = out - outstart;
1981 *inlen = processed - instart;
1982 return(-2);
1983 }
1984
1985 if (inend - in < trailing) {
1986 break;
Daniel Veillarde77db162009-08-22 11:32:38 +02001987 }
Owen Taylor3473f882001-02-23 17:55:21 +00001988
1989 for ( ; trailing; trailing--) {
1990 if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
1991 break;
1992 c <<= 6;
1993 c |= d & 0x3F;
1994 }
1995
1996 /* assertion: c is a single UTF-4 value */
1997 if (c < 0x80) {
1998 if (out + 1 >= outend)
1999 break;
2000 *out++ = c;
2001 } else {
2002 int len;
Daniel Veillardbb371292001-08-16 23:26:59 +00002003 const htmlEntityDesc * ent;
Daniel Veillard1032ac42006-11-23 16:18:30 +00002004 const char *cp;
2005 char nbuf[16];
Owen Taylor3473f882001-02-23 17:55:21 +00002006
2007 /*
2008 * Try to lookup a predefined HTML entity for it
2009 */
2010
2011 ent = htmlEntityValueLookup(c);
2012 if (ent == NULL) {
Daniel Veillard1032ac42006-11-23 16:18:30 +00002013 snprintf(nbuf, sizeof(nbuf), "#%u", c);
2014 cp = nbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00002015 }
Daniel Veillard1032ac42006-11-23 16:18:30 +00002016 else
2017 cp = ent->name;
2018 len = strlen(cp);
Owen Taylor3473f882001-02-23 17:55:21 +00002019 if (out + 2 + len >= outend)
2020 break;
2021 *out++ = '&';
Daniel Veillard1032ac42006-11-23 16:18:30 +00002022 memcpy(out, cp, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002023 out += len;
2024 *out++ = ';';
2025 }
2026 processed = in;
2027 }
2028 *outlen = out - outstart;
2029 *inlen = processed - instart;
2030 return(0);
2031}
2032
2033/**
2034 * htmlEncodeEntities:
2035 * @out: a pointer to an array of bytes to store the result
2036 * @outlen: the length of @out
2037 * @in: a pointer to an array of UTF-8 chars
2038 * @inlen: the length of @in
2039 * @quoteChar: the quote character to escape (' or ") or zero.
2040 *
2041 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2042 * plus HTML entities block of chars out.
2043 *
2044 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2045 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002046 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00002047 * The value of @outlen after return is the number of octets consumed.
2048 */
2049int
2050htmlEncodeEntities(unsigned char* out, int *outlen,
2051 const unsigned char* in, int *inlen, int quoteChar) {
2052 const unsigned char* processed = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00002053 const unsigned char* outend;
Owen Taylor3473f882001-02-23 17:55:21 +00002054 const unsigned char* outstart = out;
2055 const unsigned char* instart = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00002056 const unsigned char* inend;
Owen Taylor3473f882001-02-23 17:55:21 +00002057 unsigned int c, d;
2058 int trailing;
2059
Daniel Veillardce682bc2004-11-05 17:22:25 +00002060 if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL))
2061 return(-1);
2062 outend = out + (*outlen);
2063 inend = in + (*inlen);
Owen Taylor3473f882001-02-23 17:55:21 +00002064 while (in < inend) {
2065 d = *in++;
2066 if (d < 0x80) { c= d; trailing= 0; }
2067 else if (d < 0xC0) {
2068 /* trailing byte in leading position */
2069 *outlen = out - outstart;
2070 *inlen = processed - instart;
2071 return(-2);
2072 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
2073 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
2074 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
2075 else {
2076 /* no chance for this in Ascii */
2077 *outlen = out - outstart;
2078 *inlen = processed - instart;
2079 return(-2);
2080 }
2081
2082 if (inend - in < trailing)
2083 break;
2084
2085 while (trailing--) {
2086 if (((d= *in++) & 0xC0) != 0x80) {
2087 *outlen = out - outstart;
2088 *inlen = processed - instart;
2089 return(-2);
2090 }
2091 c <<= 6;
2092 c |= d & 0x3F;
2093 }
2094
2095 /* assertion: c is a single UTF-4 value */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002096 if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
2097 (c != '&') && (c != '<') && (c != '>')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002098 if (out >= outend)
2099 break;
2100 *out++ = c;
2101 } else {
Daniel Veillardbb371292001-08-16 23:26:59 +00002102 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00002103 const char *cp;
2104 char nbuf[16];
2105 int len;
2106
2107 /*
2108 * Try to lookup a predefined HTML entity for it
2109 */
2110 ent = htmlEntityValueLookup(c);
2111 if (ent == NULL) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002112 snprintf(nbuf, sizeof(nbuf), "#%u", c);
Owen Taylor3473f882001-02-23 17:55:21 +00002113 cp = nbuf;
2114 }
2115 else
2116 cp = ent->name;
2117 len = strlen(cp);
2118 if (out + 2 + len > outend)
2119 break;
2120 *out++ = '&';
2121 memcpy(out, cp, len);
2122 out += len;
2123 *out++ = ';';
2124 }
2125 processed = in;
2126 }
2127 *outlen = out - outstart;
2128 *inlen = processed - instart;
2129 return(0);
2130}
2131
Owen Taylor3473f882001-02-23 17:55:21 +00002132/************************************************************************
2133 * *
2134 * Commodity functions to handle streams *
2135 * *
2136 ************************************************************************/
2137
2138/**
Owen Taylor3473f882001-02-23 17:55:21 +00002139 * htmlNewInputStream:
2140 * @ctxt: an HTML parser context
2141 *
2142 * Create a new input stream structure
2143 * Returns the new input stream or NULL
2144 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002145static htmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00002146htmlNewInputStream(htmlParserCtxtPtr ctxt) {
2147 htmlParserInputPtr input;
2148
2149 input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
2150 if (input == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002151 htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002152 return(NULL);
2153 }
2154 memset(input, 0, sizeof(htmlParserInput));
2155 input->filename = NULL;
2156 input->directory = NULL;
2157 input->base = NULL;
2158 input->cur = NULL;
2159 input->buf = NULL;
2160 input->line = 1;
2161 input->col = 1;
2162 input->buf = NULL;
2163 input->free = NULL;
2164 input->version = NULL;
2165 input->consumed = 0;
2166 input->length = 0;
2167 return(input);
2168}
2169
2170
2171/************************************************************************
2172 * *
2173 * Commodity functions, cleanup needed ? *
2174 * *
2175 ************************************************************************/
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002176/*
Daniel Veillarde77db162009-08-22 11:32:38 +02002177 * all tags allowing pc data from the html 4.01 loose dtd
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002178 * NOTE: it might be more apropriate to integrate this information
2179 * into the html40ElementTable array but I don't want to risk any
2180 * binary incomptibility
2181 */
2182static const char *allowPCData[] = {
2183 "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
2184 "blockquote", "body", "button", "caption", "center", "cite", "code",
2185 "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
2186 "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
2187 "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
2188 "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
2189};
Owen Taylor3473f882001-02-23 17:55:21 +00002190
2191/**
2192 * areBlanks:
2193 * @ctxt: an HTML parser context
2194 * @str: a xmlChar *
2195 * @len: the size of @str
2196 *
2197 * Is this a sequence of blank chars that one can ignore ?
2198 *
2199 * Returns 1 if ignorable 0 otherwise.
2200 */
2201
2202static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002203 unsigned int i;
2204 int j;
Owen Taylor3473f882001-02-23 17:55:21 +00002205 xmlNodePtr lastChild;
Daniel Veillard36d73402005-09-01 09:52:30 +00002206 xmlDtdPtr dtd;
Owen Taylor3473f882001-02-23 17:55:21 +00002207
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002208 for (j = 0;j < len;j++)
William M. Brack76e95df2003-10-18 16:20:14 +00002209 if (!(IS_BLANK_CH(str[j]))) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002210
2211 if (CUR == 0) return(1);
2212 if (CUR != '<') return(0);
2213 if (ctxt->name == NULL)
2214 return(1);
2215 if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
2216 return(1);
2217 if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
2218 return(1);
Daniel Veillard36d73402005-09-01 09:52:30 +00002219
2220 /* Only strip CDATA children of the body tag for strict HTML DTDs */
2221 if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) {
2222 dtd = xmlGetIntSubset(ctxt->myDoc);
2223 if (dtd != NULL && dtd->ExternalID != NULL) {
2224 if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") ||
2225 !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN"))
2226 return(1);
2227 }
2228 }
2229
Owen Taylor3473f882001-02-23 17:55:21 +00002230 if (ctxt->node == NULL) return(0);
2231 lastChild = xmlGetLastChild(ctxt->node);
Daniel Veillard18a65092004-05-11 15:57:42 +00002232 while ((lastChild) && (lastChild->type == XML_COMMENT_NODE))
2233 lastChild = lastChild->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00002234 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002235 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2236 (ctxt->node->content != NULL)) return(0);
Daniel Veillarde77db162009-08-22 11:32:38 +02002237 /* keep ws in constructs like ...<b> </b>...
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002238 for all tags "b" allowing PCDATA */
2239 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2240 if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
2241 return(0);
2242 }
2243 }
Owen Taylor3473f882001-02-23 17:55:21 +00002244 } else if (xmlNodeIsText(lastChild)) {
2245 return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002246 } else {
Daniel Veillarde77db162009-08-22 11:32:38 +02002247 /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002248 for all tags "p" allowing PCDATA */
2249 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2250 if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
2251 return(0);
2252 }
2253 }
Owen Taylor3473f882001-02-23 17:55:21 +00002254 }
2255 return(1);
2256}
2257
2258/**
Owen Taylor3473f882001-02-23 17:55:21 +00002259 * htmlNewDocNoDtD:
2260 * @URI: URI for the dtd, or NULL
2261 * @ExternalID: the external ID of the DTD, or NULL
2262 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002263 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2264 * are NULL
2265 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002266 * Returns a new document, do not initialize the DTD if not provided
Owen Taylor3473f882001-02-23 17:55:21 +00002267 */
2268htmlDocPtr
2269htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
2270 xmlDocPtr cur;
2271
2272 /*
2273 * Allocate a new document and fill the fields.
2274 */
2275 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
2276 if (cur == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002277 htmlErrMemory(NULL, "HTML document creation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002278 return(NULL);
2279 }
2280 memset(cur, 0, sizeof(xmlDoc));
2281
2282 cur->type = XML_HTML_DOCUMENT_NODE;
2283 cur->version = NULL;
2284 cur->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002285 cur->doc = cur;
2286 cur->name = NULL;
Daniel Veillarde77db162009-08-22 11:32:38 +02002287 cur->children = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002288 cur->extSubset = NULL;
2289 cur->oldNs = NULL;
2290 cur->encoding = NULL;
2291 cur->standalone = 1;
2292 cur->compression = 0;
2293 cur->ids = NULL;
2294 cur->refs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002295 cur->_private = NULL;
Daniel Veillard7cc23572004-07-29 11:20:30 +00002296 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillardae0765b2008-07-31 19:54:59 +00002297 cur->properties = XML_DOC_HTML | XML_DOC_USERBUILT;
Daniel Veillardb6b0fd82001-10-22 12:31:11 +00002298 if ((ExternalID != NULL) ||
2299 (URI != NULL))
Daniel Veillard40412cd2003-09-03 13:28:32 +00002300 xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002301 return(cur);
2302}
2303
2304/**
2305 * htmlNewDoc:
2306 * @URI: URI for the dtd, or NULL
2307 * @ExternalID: the external ID of the DTD, or NULL
2308 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002309 * Creates a new HTML document
2310 *
Owen Taylor3473f882001-02-23 17:55:21 +00002311 * Returns a new document
2312 */
2313htmlDocPtr
2314htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
2315 if ((URI == NULL) && (ExternalID == NULL))
2316 return(htmlNewDocNoDtD(
Daniel Veillard64269352001-05-04 17:52:34 +00002317 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
2318 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
Owen Taylor3473f882001-02-23 17:55:21 +00002319
2320 return(htmlNewDocNoDtD(URI, ExternalID));
2321}
2322
2323
2324/************************************************************************
2325 * *
2326 * The parser itself *
2327 * Relates to http://www.w3.org/TR/html40 *
2328 * *
2329 ************************************************************************/
2330
2331/************************************************************************
2332 * *
2333 * The parser itself *
2334 * *
2335 ************************************************************************/
2336
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002337static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002338
Owen Taylor3473f882001-02-23 17:55:21 +00002339/**
2340 * htmlParseHTMLName:
2341 * @ctxt: an HTML parser context
2342 *
2343 * parse an HTML tag or attribute name, note that we convert it to lowercase
2344 * since HTML names are not case-sensitive.
2345 *
2346 * Returns the Tag Name parsed or NULL
2347 */
2348
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002349static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002350htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002351 int i = 0;
2352 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2353
William M. Brackd1757ab2004-10-02 22:07:48 +00002354 if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
Daniel Veillard7459c592009-08-13 10:10:29 +02002355 (CUR != ':') && (CUR != '.')) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002356
2357 while ((i < HTML_PARSER_BUFFER_SIZE) &&
William M. Brackd1757ab2004-10-02 22:07:48 +00002358 ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) ||
Daniel Veillard7459c592009-08-13 10:10:29 +02002359 (CUR == ':') || (CUR == '-') || (CUR == '_') ||
2360 (CUR == '.'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002361 if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2362 else loc[i] = CUR;
2363 i++;
Daniel Veillarde77db162009-08-22 11:32:38 +02002364
Owen Taylor3473f882001-02-23 17:55:21 +00002365 NEXT;
2366 }
Daniel Veillarde77db162009-08-22 11:32:38 +02002367
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002368 return(xmlDictLookup(ctxt->dict, loc, i));
Owen Taylor3473f882001-02-23 17:55:21 +00002369}
2370
Daniel Veillard890fd9f2006-10-27 12:53:28 +00002371
2372/**
2373 * htmlParseHTMLName_nonInvasive:
2374 * @ctxt: an HTML parser context
2375 *
2376 * parse an HTML tag or attribute name, note that we convert it to lowercase
2377 * since HTML names are not case-sensitive, this doesn't consume the data
2378 * from the stream, it's a look-ahead
2379 *
2380 * Returns the Tag Name parsed or NULL
2381 */
2382
2383static const xmlChar *
2384htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) {
2385 int i = 0;
2386 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2387
2388 if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') &&
2389 (NXT(1) != ':')) return(NULL);
Daniel Veillarde77db162009-08-22 11:32:38 +02002390
Daniel Veillard890fd9f2006-10-27 12:53:28 +00002391 while ((i < HTML_PARSER_BUFFER_SIZE) &&
2392 ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) ||
2393 (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) {
2394 if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20;
2395 else loc[i] = NXT(1+i);
2396 i++;
2397 }
Daniel Veillarde77db162009-08-22 11:32:38 +02002398
Daniel Veillard890fd9f2006-10-27 12:53:28 +00002399 return(xmlDictLookup(ctxt->dict, loc, i));
2400}
2401
2402
Owen Taylor3473f882001-02-23 17:55:21 +00002403/**
2404 * htmlParseName:
2405 * @ctxt: an HTML parser context
2406 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002407 * parse an HTML name, this routine is case sensitive.
Owen Taylor3473f882001-02-23 17:55:21 +00002408 *
2409 * Returns the Name parsed or NULL
2410 */
2411
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002412static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002413htmlParseName(htmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002414 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002415 const xmlChar *ret;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002416 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002417
2418 GROW;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002419
2420 /*
2421 * Accelerator for simple ASCII names
2422 */
2423 in = ctxt->input->cur;
2424 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2425 ((*in >= 0x41) && (*in <= 0x5A)) ||
2426 (*in == '_') || (*in == ':')) {
2427 in++;
2428 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2429 ((*in >= 0x41) && (*in <= 0x5A)) ||
2430 ((*in >= 0x30) && (*in <= 0x39)) ||
2431 (*in == '_') || (*in == '-') ||
2432 (*in == ':') || (*in == '.'))
2433 in++;
2434 if ((*in > 0) && (*in < 0x80)) {
2435 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002436 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002437 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002438 ctxt->nbChars += count;
2439 ctxt->input->col += count;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002440 return(ret);
2441 }
2442 }
2443 return(htmlParseNameComplex(ctxt));
2444}
2445
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002446static const xmlChar *
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002447htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002448 int len = 0, l;
2449 int c;
2450 int count = 0;
2451
2452 /*
2453 * Handler for more complex cases
2454 */
2455 GROW;
2456 c = CUR_CHAR(l);
2457 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2458 (!IS_LETTER(c) && (c != '_') &&
2459 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002460 return(NULL);
2461 }
2462
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002463 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
2464 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2465 (c == '.') || (c == '-') ||
Daniel Veillarde77db162009-08-22 11:32:38 +02002466 (c == '_') || (c == ':') ||
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002467 (IS_COMBINING(c)) ||
2468 (IS_EXTENDER(c)))) {
2469 if (count++ > 100) {
2470 count = 0;
2471 GROW;
2472 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002473 len += l;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002474 NEXTL(l);
2475 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002476 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002477 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002478}
2479
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002480
Owen Taylor3473f882001-02-23 17:55:21 +00002481/**
2482 * htmlParseHTMLAttribute:
2483 * @ctxt: an HTML parser context
2484 * @stop: a char stop value
Daniel Veillarde77db162009-08-22 11:32:38 +02002485 *
Owen Taylor3473f882001-02-23 17:55:21 +00002486 * parse an HTML attribute value till the stop (quote), if
2487 * stop is 0 then it stops at the first space
2488 *
2489 * Returns the attribute parsed or NULL
2490 */
2491
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002492static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002493htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
2494 xmlChar *buffer = NULL;
2495 int buffer_size = 0;
2496 xmlChar *out = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002497 const xmlChar *name = NULL;
2498 const xmlChar *cur = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00002499 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00002500
2501 /*
2502 * allocate a translation buffer.
2503 */
2504 buffer_size = HTML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002505 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002506 if (buffer == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002507 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002508 return(NULL);
2509 }
2510 out = buffer;
2511
2512 /*
2513 * Ok loop until we reach one of the ending chars
2514 */
Daniel Veillard957fdcf2001-11-06 22:50:19 +00002515 while ((CUR != 0) && (CUR != stop)) {
2516 if ((stop == 0) && (CUR == '>')) break;
William M. Brack76e95df2003-10-18 16:20:14 +00002517 if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
Owen Taylor3473f882001-02-23 17:55:21 +00002518 if (CUR == '&') {
2519 if (NXT(1) == '#') {
2520 unsigned int c;
2521 int bits;
2522
2523 c = htmlParseCharRef(ctxt);
2524 if (c < 0x80)
2525 { *out++ = c; bits= -6; }
2526 else if (c < 0x800)
2527 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2528 else if (c < 0x10000)
2529 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002530 else
Owen Taylor3473f882001-02-23 17:55:21 +00002531 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002532
Owen Taylor3473f882001-02-23 17:55:21 +00002533 for ( ; bits >= 0; bits-= 6) {
2534 *out++ = ((c >> bits) & 0x3F) | 0x80;
2535 }
Daniel Veillarde77db162009-08-22 11:32:38 +02002536
Daniel Veillardce02dbc2002-10-22 19:14:58 +00002537 if (out - buffer > buffer_size - 100) {
2538 int indx = out - buffer;
2539
2540 growBuffer(buffer);
2541 out = &buffer[indx];
2542 }
Owen Taylor3473f882001-02-23 17:55:21 +00002543 } else {
2544 ent = htmlParseEntityRef(ctxt, &name);
2545 if (name == NULL) {
2546 *out++ = '&';
2547 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002548 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002549
2550 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002551 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002552 }
2553 } else if (ent == NULL) {
2554 *out++ = '&';
2555 cur = name;
2556 while (*cur != 0) {
2557 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002558 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002559
2560 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002561 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002562 }
2563 *out++ = *cur++;
2564 }
Owen Taylor3473f882001-02-23 17:55:21 +00002565 } else {
2566 unsigned int c;
2567 int bits;
2568
2569 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002570 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002571
2572 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002573 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002574 }
Daniel Veillard48519092006-10-17 15:56:35 +00002575 c = ent->value;
Owen Taylor3473f882001-02-23 17:55:21 +00002576 if (c < 0x80)
2577 { *out++ = c; bits= -6; }
2578 else if (c < 0x800)
2579 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2580 else if (c < 0x10000)
2581 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002582 else
Owen Taylor3473f882001-02-23 17:55:21 +00002583 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002584
Owen Taylor3473f882001-02-23 17:55:21 +00002585 for ( ; bits >= 0; bits-= 6) {
2586 *out++ = ((c >> bits) & 0x3F) | 0x80;
2587 }
Owen Taylor3473f882001-02-23 17:55:21 +00002588 }
2589 }
2590 } else {
2591 unsigned int c;
2592 int bits, l;
2593
2594 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002595 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002596
2597 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002598 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002599 }
2600 c = CUR_CHAR(l);
2601 if (c < 0x80)
2602 { *out++ = c; bits= -6; }
2603 else if (c < 0x800)
2604 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2605 else if (c < 0x10000)
2606 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002607 else
Owen Taylor3473f882001-02-23 17:55:21 +00002608 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
Daniel Veillarde77db162009-08-22 11:32:38 +02002609
Owen Taylor3473f882001-02-23 17:55:21 +00002610 for ( ; bits >= 0; bits-= 6) {
2611 *out++ = ((c >> bits) & 0x3F) | 0x80;
2612 }
2613 NEXT;
2614 }
2615 }
Daniel Veillard13cee4e2009-09-05 14:52:55 +02002616 *out = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002617 return(buffer);
2618}
2619
2620/**
Owen Taylor3473f882001-02-23 17:55:21 +00002621 * htmlParseEntityRef:
2622 * @ctxt: an HTML parser context
2623 * @str: location to store the entity name
2624 *
2625 * parse an HTML ENTITY references
2626 *
2627 * [68] EntityRef ::= '&' Name ';'
2628 *
2629 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2630 * if non-NULL *str will have to be freed by the caller.
2631 */
Daniel Veillardbb371292001-08-16 23:26:59 +00002632const htmlEntityDesc *
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002633htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
2634 const xmlChar *name;
Daniel Veillardbb371292001-08-16 23:26:59 +00002635 const htmlEntityDesc * ent = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00002636
2637 if (str != NULL) *str = NULL;
2638 if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002639
2640 if (CUR == '&') {
2641 NEXT;
2642 name = htmlParseName(ctxt);
2643 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002644 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2645 "htmlParseEntityRef: no name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002646 } else {
2647 GROW;
2648 if (CUR == ';') {
Daniel Veillard42595322004-11-08 10:52:06 +00002649 if (str != NULL)
2650 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002651
2652 /*
2653 * Lookup the entity in the table.
2654 */
2655 ent = htmlEntityLookup(name);
2656 if (ent != NULL) /* OK that's ugly !!! */
2657 NEXT;
2658 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002659 htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
2660 "htmlParseEntityRef: expecting ';'\n",
2661 NULL, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002662 if (str != NULL)
2663 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002664 }
2665 }
2666 }
2667 return(ent);
2668}
2669
2670/**
2671 * htmlParseAttValue:
2672 * @ctxt: an HTML parser context
2673 *
2674 * parse a value for an attribute
2675 * Note: the parser won't do substitution of entities here, this
2676 * will be handled later in xmlStringGetNodeList, unless it was
Daniel Veillarde77db162009-08-22 11:32:38 +02002677 * asked for ctxt->replaceEntities != 0
Owen Taylor3473f882001-02-23 17:55:21 +00002678 *
2679 * Returns the AttValue parsed or NULL.
2680 */
2681
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002682static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002683htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2684 xmlChar *ret = NULL;
2685
2686 if (CUR == '"') {
2687 NEXT;
2688 ret = htmlParseHTMLAttribute(ctxt, '"');
2689 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002690 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2691 "AttValue: \" expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002692 } else
2693 NEXT;
2694 } else if (CUR == '\'') {
2695 NEXT;
2696 ret = htmlParseHTMLAttribute(ctxt, '\'');
2697 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002698 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2699 "AttValue: ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002700 } else
2701 NEXT;
2702 } else {
2703 /*
2704 * That's an HTMLism, the attribute value may not be quoted
2705 */
2706 ret = htmlParseHTMLAttribute(ctxt, 0);
2707 if (ret == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002708 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
2709 "AttValue: no value found\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002710 }
2711 }
2712 return(ret);
2713}
2714
2715/**
2716 * htmlParseSystemLiteral:
2717 * @ctxt: an HTML parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02002718 *
Owen Taylor3473f882001-02-23 17:55:21 +00002719 * parse an HTML Literal
2720 *
2721 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2722 *
2723 * Returns the SystemLiteral parsed or NULL
2724 */
2725
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002726static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002727htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2728 const xmlChar *q;
2729 xmlChar *ret = NULL;
2730
2731 if (CUR == '"') {
2732 NEXT;
2733 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002734 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00002735 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002736 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002737 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2738 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002739 } else {
2740 ret = xmlStrndup(q, CUR_PTR - q);
2741 NEXT;
2742 }
2743 } else if (CUR == '\'') {
2744 NEXT;
2745 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002746 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002747 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002748 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002749 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2750 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002751 } else {
2752 ret = xmlStrndup(q, CUR_PTR - q);
2753 NEXT;
2754 }
2755 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002756 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2757 " or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002758 }
Daniel Veillarde77db162009-08-22 11:32:38 +02002759
Owen Taylor3473f882001-02-23 17:55:21 +00002760 return(ret);
2761}
2762
2763/**
2764 * htmlParsePubidLiteral:
2765 * @ctxt: an HTML parser context
2766 *
2767 * parse an HTML public literal
2768 *
2769 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2770 *
2771 * Returns the PubidLiteral parsed or NULL.
2772 */
2773
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002774static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002775htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
2776 const xmlChar *q;
2777 xmlChar *ret = NULL;
2778 /*
2779 * Name ::= (Letter | '_') (NameChar)*
2780 */
2781 if (CUR == '"') {
2782 NEXT;
2783 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002784 while (IS_PUBIDCHAR_CH(CUR)) NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00002785 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002786 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2787 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002788 } else {
2789 ret = xmlStrndup(q, CUR_PTR - q);
2790 NEXT;
2791 }
2792 } else if (CUR == '\'') {
2793 NEXT;
2794 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002795 while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002796 NEXT;
Daniel Veillard6560a422003-03-27 21:25:38 +00002797 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002798 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2799 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002800 } else {
2801 ret = xmlStrndup(q, CUR_PTR - q);
2802 NEXT;
2803 }
2804 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002805 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2806 "PubidLiteral \" or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002807 }
Daniel Veillarde77db162009-08-22 11:32:38 +02002808
Owen Taylor3473f882001-02-23 17:55:21 +00002809 return(ret);
2810}
2811
2812/**
2813 * htmlParseScript:
2814 * @ctxt: an HTML parser context
2815 *
2816 * parse the content of an HTML SCRIPT or STYLE element
2817 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
2818 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
2819 * http://www.w3.org/TR/html4/types.html#type-script
2820 * http://www.w3.org/TR/html4/types.html#h-6.15
2821 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
2822 *
2823 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
2824 * element and the value of intrinsic event attributes. User agents must
2825 * not evaluate script data as HTML markup but instead must pass it on as
2826 * data to a script engine.
2827 * NOTES:
2828 * - The content is passed like CDATA
2829 * - the attributes for style and scripting "onXXX" are also described
2830 * as CDATA but SGML allows entities references in attributes so their
2831 * processing is identical as other attributes
2832 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002833static void
Owen Taylor3473f882001-02-23 17:55:21 +00002834htmlParseScript(htmlParserCtxtPtr ctxt) {
Daniel Veillard7d2b3232005-07-14 08:57:39 +00002835 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
Owen Taylor3473f882001-02-23 17:55:21 +00002836 int nbchar = 0;
Daniel Veillard358fef42005-07-13 16:37:38 +00002837 int cur,l;
Owen Taylor3473f882001-02-23 17:55:21 +00002838
2839 SHRINK;
Daniel Veillard358fef42005-07-13 16:37:38 +00002840 cur = CUR_CHAR(l);
William M. Brack76e95df2003-10-18 16:20:14 +00002841 while (IS_CHAR_CH(cur)) {
Daniel Veillard42720242007-04-16 07:02:31 +00002842 if ((cur == '<') && (NXT(1) == '/')) {
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002843 /*
2844 * One should break here, the specification is clear:
2845 * Authors should therefore escape "</" within the content.
2846 * Escape mechanisms are specific to each scripting or
2847 * style sheet language.
2848 *
2849 * In recovery mode, only break if end tag match the
2850 * current tag, effectively ignoring all tags inside the
2851 * script/style block and treating the entire block as
2852 * CDATA.
2853 */
2854 if (ctxt->recovery) {
Daniel Veillarde77db162009-08-22 11:32:38 +02002855 if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2,
2856 xmlStrlen(ctxt->name)) == 0)
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002857 {
2858 break; /* while */
2859 } else {
2860 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard2cf36a12005-10-25 12:21:29 +00002861 "Element %s embeds close tag\n",
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002862 ctxt->name, NULL);
2863 }
2864 } else {
2865 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
Daniel Veillarde77db162009-08-22 11:32:38 +02002866 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002867 {
2868 break; /* while */
2869 }
2870 }
Owen Taylor3473f882001-02-23 17:55:21 +00002871 }
Daniel Veillard358fef42005-07-13 16:37:38 +00002872 COPY_BUF(l,buf,nbchar,cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002873 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2874 if (ctxt->sax->cdataBlock!= NULL) {
2875 /*
2876 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2877 */
2878 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002879 } else if (ctxt->sax->characters != NULL) {
2880 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002881 }
2882 nbchar = 0;
2883 }
Daniel Veillardb9900082005-10-25 12:36:29 +00002884 GROW;
Daniel Veillard358fef42005-07-13 16:37:38 +00002885 NEXTL(l);
2886 cur = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002887 }
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002888
Daniel Veillard68716a72006-10-16 09:32:17 +00002889 if ((!(IS_CHAR_CH(cur))) && (!((cur == 0) && (ctxt->progressive)))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002890 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2891 "Invalid char in CDATA 0x%X\n", cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002892 NEXT;
2893 }
2894
2895 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2896 if (ctxt->sax->cdataBlock!= NULL) {
2897 /*
2898 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2899 */
2900 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002901 } else if (ctxt->sax->characters != NULL) {
2902 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002903 }
2904 }
2905}
2906
2907
2908/**
2909 * htmlParseCharData:
2910 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002911 *
2912 * parse a CharData section.
2913 * if we are within a CDATA section ']]>' marks an end of section.
2914 *
2915 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2916 */
2917
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002918static void
2919htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002920 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2921 int nbchar = 0;
2922 int cur, l;
Daniel Veillarda57ba4c2008-09-25 16:06:18 +00002923 int chunk = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002924
2925 SHRINK;
2926 cur = CUR_CHAR(l);
2927 while (((cur != '<') || (ctxt->token == '<')) &&
Daniel Veillarde77db162009-08-22 11:32:38 +02002928 ((cur != '&') || (ctxt->token == '&')) &&
Daniel Veillardc5b43cc2008-01-11 07:41:39 +00002929 (cur != 0)) {
2930 if (!(IS_CHAR(cur))) {
2931 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2932 "Invalid char in CDATA 0x%X\n", cur);
2933 } else {
2934 COPY_BUF(l,buf,nbchar,cur);
2935 }
Owen Taylor3473f882001-02-23 17:55:21 +00002936 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2937 /*
2938 * Ok the segment is to be consumed as chars.
2939 */
2940 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2941 if (areBlanks(ctxt, buf, nbchar)) {
2942 if (ctxt->sax->ignorableWhitespace != NULL)
2943 ctxt->sax->ignorableWhitespace(ctxt->userData,
2944 buf, nbchar);
2945 } else {
2946 htmlCheckParagraph(ctxt);
2947 if (ctxt->sax->characters != NULL)
2948 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2949 }
2950 }
2951 nbchar = 0;
2952 }
2953 NEXTL(l);
Daniel Veillarda57ba4c2008-09-25 16:06:18 +00002954 chunk++;
2955 if (chunk > HTML_PARSER_BUFFER_SIZE) {
2956 chunk = 0;
2957 SHRINK;
2958 GROW;
2959 }
Owen Taylor3473f882001-02-23 17:55:21 +00002960 cur = CUR_CHAR(l);
Daniel Veillard358a9892003-02-04 15:22:32 +00002961 if (cur == 0) {
2962 SHRINK;
2963 GROW;
2964 cur = CUR_CHAR(l);
2965 }
Owen Taylor3473f882001-02-23 17:55:21 +00002966 }
2967 if (nbchar != 0) {
Daniel Veillardd2755a82005-08-07 23:42:39 +00002968 buf[nbchar] = 0;
2969
Owen Taylor3473f882001-02-23 17:55:21 +00002970 /*
2971 * Ok the segment is to be consumed as chars.
2972 */
2973 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2974 if (areBlanks(ctxt, buf, nbchar)) {
2975 if (ctxt->sax->ignorableWhitespace != NULL)
2976 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2977 } else {
2978 htmlCheckParagraph(ctxt);
2979 if (ctxt->sax->characters != NULL)
2980 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2981 }
2982 }
Daniel Veillard7cc95c02001-10-17 15:45:12 +00002983 } else {
2984 /*
2985 * Loop detection
2986 */
2987 if (cur == 0)
2988 ctxt->instate = XML_PARSER_EOF;
Owen Taylor3473f882001-02-23 17:55:21 +00002989 }
2990}
2991
2992/**
2993 * htmlParseExternalID:
2994 * @ctxt: an HTML parser context
2995 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002996 *
2997 * Parse an External ID or a Public ID
2998 *
Owen Taylor3473f882001-02-23 17:55:21 +00002999 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3000 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3001 *
3002 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3003 *
3004 * Returns the function returns SystemLiteral and in the second
3005 * case publicID receives PubidLiteral, is strict is off
3006 * it is possible to return NULL and have publicID set.
3007 */
3008
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003009static xmlChar *
3010htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00003011 xmlChar *URI = NULL;
3012
3013 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
3014 (UPP(2) == 'S') && (UPP(3) == 'T') &&
3015 (UPP(4) == 'E') && (UPP(5) == 'M')) {
3016 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003017 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003018 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3019 "Space required after 'SYSTEM'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003020 }
3021 SKIP_BLANKS;
3022 URI = htmlParseSystemLiteral(ctxt);
3023 if (URI == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003024 htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
3025 "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003026 }
3027 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
3028 (UPP(2) == 'B') && (UPP(3) == 'L') &&
3029 (UPP(4) == 'I') && (UPP(5) == 'C')) {
3030 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003031 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003032 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3033 "Space required after 'PUBLIC'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003034 }
3035 SKIP_BLANKS;
3036 *publicID = htmlParsePubidLiteral(ctxt);
3037 if (*publicID == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003038 htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
3039 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
3040 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003041 }
3042 SKIP_BLANKS;
3043 if ((CUR == '"') || (CUR == '\'')) {
3044 URI = htmlParseSystemLiteral(ctxt);
3045 }
3046 }
3047 return(URI);
3048}
3049
3050/**
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003051 * xmlParsePI:
3052 * @ctxt: an XML parser context
3053 *
3054 * parse an XML Processing Instruction.
3055 *
3056 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3057 */
3058static void
3059htmlParsePI(htmlParserCtxtPtr ctxt) {
3060 xmlChar *buf = NULL;
3061 int len = 0;
3062 int size = HTML_PARSER_BUFFER_SIZE;
3063 int cur, l;
3064 const xmlChar *target;
3065 xmlParserInputState state;
3066 int count = 0;
3067
3068 if ((RAW == '<') && (NXT(1) == '?')) {
3069 state = ctxt->instate;
3070 ctxt->instate = XML_PARSER_PI;
3071 /*
3072 * this is a Processing Instruction.
3073 */
3074 SKIP(2);
3075 SHRINK;
3076
3077 /*
3078 * Parse the target name and check for special support like
3079 * namespace.
3080 */
3081 target = htmlParseName(ctxt);
3082 if (target != NULL) {
3083 if (RAW == '>') {
3084 SKIP(1);
3085
3086 /*
3087 * SAX: PI detected.
3088 */
3089 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3090 (ctxt->sax->processingInstruction != NULL))
3091 ctxt->sax->processingInstruction(ctxt->userData,
3092 target, NULL);
3093 ctxt->instate = state;
3094 return;
3095 }
3096 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
3097 if (buf == NULL) {
3098 htmlErrMemory(ctxt, NULL);
3099 ctxt->instate = state;
3100 return;
3101 }
3102 cur = CUR;
3103 if (!IS_BLANK(cur)) {
3104 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3105 "ParsePI: PI %s space expected\n", target, NULL);
3106 }
3107 SKIP_BLANKS;
3108 cur = CUR_CHAR(l);
3109 while (IS_CHAR(cur) && (cur != '>')) {
3110 if (len + 5 >= size) {
3111 xmlChar *tmp;
3112
3113 size *= 2;
3114 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3115 if (tmp == NULL) {
3116 htmlErrMemory(ctxt, NULL);
3117 xmlFree(buf);
3118 ctxt->instate = state;
3119 return;
3120 }
3121 buf = tmp;
3122 }
3123 count++;
3124 if (count > 50) {
3125 GROW;
3126 count = 0;
3127 }
3128 COPY_BUF(l,buf,len,cur);
3129 NEXTL(l);
3130 cur = CUR_CHAR(l);
3131 if (cur == 0) {
3132 SHRINK;
3133 GROW;
3134 cur = CUR_CHAR(l);
3135 }
3136 }
3137 buf[len] = 0;
3138 if (cur != '>') {
3139 htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
3140 "ParsePI: PI %s never end ...\n", target, NULL);
3141 } else {
3142 SKIP(1);
3143
3144 /*
3145 * SAX: PI detected.
3146 */
3147 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3148 (ctxt->sax->processingInstruction != NULL))
3149 ctxt->sax->processingInstruction(ctxt->userData,
3150 target, buf);
3151 }
3152 xmlFree(buf);
3153 } else {
Daniel Veillarde77db162009-08-22 11:32:38 +02003154 htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003155 "PI is not started correctly", NULL, NULL);
3156 }
3157 ctxt->instate = state;
3158 }
3159}
3160
3161/**
Owen Taylor3473f882001-02-23 17:55:21 +00003162 * htmlParseComment:
3163 * @ctxt: an HTML parser context
3164 *
3165 * Parse an XML (SGML) comment <!-- .... -->
3166 *
3167 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3168 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003169static void
Owen Taylor3473f882001-02-23 17:55:21 +00003170htmlParseComment(htmlParserCtxtPtr ctxt) {
3171 xmlChar *buf = NULL;
3172 int len;
3173 int size = HTML_PARSER_BUFFER_SIZE;
3174 int q, ql;
3175 int r, rl;
3176 int cur, l;
3177 xmlParserInputState state;
3178
3179 /*
3180 * Check that there is a comment right here.
3181 */
3182 if ((RAW != '<') || (NXT(1) != '!') ||
3183 (NXT(2) != '-') || (NXT(3) != '-')) return;
3184
3185 state = ctxt->instate;
3186 ctxt->instate = XML_PARSER_COMMENT;
3187 SHRINK;
3188 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003189 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003190 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003191 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003192 ctxt->instate = state;
3193 return;
3194 }
3195 q = CUR_CHAR(ql);
3196 NEXTL(ql);
3197 r = CUR_CHAR(rl);
3198 NEXTL(rl);
3199 cur = CUR_CHAR(l);
3200 len = 0;
3201 while (IS_CHAR(cur) &&
3202 ((cur != '>') ||
3203 (r != '-') || (q != '-'))) {
3204 if (len + 5 >= size) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003205 xmlChar *tmp;
3206
Owen Taylor3473f882001-02-23 17:55:21 +00003207 size *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003208 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3209 if (tmp == NULL) {
3210 xmlFree(buf);
Daniel Veillardf403d292003-10-05 13:51:35 +00003211 htmlErrMemory(ctxt, "growing buffer failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003212 ctxt->instate = state;
3213 return;
3214 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003215 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003216 }
3217 COPY_BUF(ql,buf,len,q);
3218 q = r;
3219 ql = rl;
3220 r = cur;
3221 rl = l;
3222 NEXTL(l);
3223 cur = CUR_CHAR(l);
3224 if (cur == 0) {
3225 SHRINK;
3226 GROW;
3227 cur = CUR_CHAR(l);
3228 }
3229 }
3230 buf[len] = 0;
3231 if (!IS_CHAR(cur)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003232 htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3233 "Comment not terminated \n<!--%.50s\n", buf, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003234 xmlFree(buf);
3235 } else {
3236 NEXT;
3237 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3238 (!ctxt->disableSAX))
3239 ctxt->sax->comment(ctxt->userData, buf);
3240 xmlFree(buf);
3241 }
3242 ctxt->instate = state;
3243}
3244
3245/**
3246 * htmlParseCharRef:
3247 * @ctxt: an HTML parser context
3248 *
3249 * parse Reference declarations
3250 *
3251 * [66] CharRef ::= '&#' [0-9]+ ';' |
3252 * '&#x' [0-9a-fA-F]+ ';'
3253 *
3254 * Returns the value parsed (as an int)
3255 */
3256int
3257htmlParseCharRef(htmlParserCtxtPtr ctxt) {
3258 int val = 0;
3259
Daniel Veillarda03e3652004-11-02 18:45:30 +00003260 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3261 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3262 "htmlParseCharRef: context error\n",
3263 NULL, NULL);
3264 return(0);
3265 }
Owen Taylor3473f882001-02-23 17:55:21 +00003266 if ((CUR == '&') && (NXT(1) == '#') &&
Daniel Veillardc59d8262003-11-20 21:59:12 +00003267 ((NXT(2) == 'x') || NXT(2) == 'X')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003268 SKIP(3);
3269 while (CUR != ';') {
Daniel Veillarde77db162009-08-22 11:32:38 +02003270 if ((CUR >= '0') && (CUR <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00003271 val = val * 16 + (CUR - '0');
3272 else if ((CUR >= 'a') && (CUR <= 'f'))
3273 val = val * 16 + (CUR - 'a') + 10;
3274 else if ((CUR >= 'A') && (CUR <= 'F'))
3275 val = val * 16 + (CUR - 'A') + 10;
3276 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003277 htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
Daniel Veillard36de63e2008-04-03 09:05:05 +00003278 "htmlParseCharRef: missing semicolumn\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00003279 NULL, NULL);
Daniel Veillard36de63e2008-04-03 09:05:05 +00003280 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003281 }
3282 NEXT;
3283 }
3284 if (CUR == ';')
3285 NEXT;
3286 } else if ((CUR == '&') && (NXT(1) == '#')) {
3287 SKIP(2);
3288 while (CUR != ';') {
Daniel Veillarde77db162009-08-22 11:32:38 +02003289 if ((CUR >= '0') && (CUR <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00003290 val = val * 10 + (CUR - '0');
3291 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003292 htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
Daniel Veillard36de63e2008-04-03 09:05:05 +00003293 "htmlParseCharRef: missing semicolumn\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00003294 NULL, NULL);
Daniel Veillard36de63e2008-04-03 09:05:05 +00003295 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003296 }
3297 NEXT;
3298 }
3299 if (CUR == ';')
3300 NEXT;
3301 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003302 htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
3303 "htmlParseCharRef: invalid value\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003304 }
3305 /*
3306 * Check the value IS_CHAR ...
3307 */
3308 if (IS_CHAR(val)) {
3309 return(val);
3310 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003311 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3312 "htmlParseCharRef: invalid xmlChar value %d\n",
3313 val);
Owen Taylor3473f882001-02-23 17:55:21 +00003314 }
3315 return(0);
3316}
3317
3318
3319/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003320 * htmlParseDocTypeDecl:
Owen Taylor3473f882001-02-23 17:55:21 +00003321 * @ctxt: an HTML parser context
3322 *
3323 * parse a DOCTYPE declaration
3324 *
Daniel Veillarde77db162009-08-22 11:32:38 +02003325 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
Owen Taylor3473f882001-02-23 17:55:21 +00003326 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3327 */
3328
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003329static void
Owen Taylor3473f882001-02-23 17:55:21 +00003330htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003331 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003332 xmlChar *ExternalID = NULL;
3333 xmlChar *URI = NULL;
3334
3335 /*
3336 * We know that '<!DOCTYPE' has been detected.
3337 */
3338 SKIP(9);
3339
3340 SKIP_BLANKS;
3341
3342 /*
3343 * Parse the DOCTYPE name.
3344 */
3345 name = htmlParseName(ctxt);
3346 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003347 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3348 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3349 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003350 }
3351 /*
3352 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3353 */
3354
3355 SKIP_BLANKS;
3356
3357 /*
3358 * Check for SystemID and ExternalID
3359 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003360 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003361 SKIP_BLANKS;
3362
3363 /*
3364 * We should be at the end of the DOCTYPE declaration.
3365 */
3366 if (CUR != '>') {
Daniel Veillardf403d292003-10-05 13:51:35 +00003367 htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
3368 "DOCTYPE improperly terminated\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003369 /* We shouldn't try to resynchronize ... */
3370 }
3371 NEXT;
3372
3373 /*
3374 * Create or update the document accordingly to the DOCTYPE
3375 */
3376 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3377 (!ctxt->disableSAX))
3378 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3379
3380 /*
3381 * Cleanup, since we don't use all those identifiers
3382 */
3383 if (URI != NULL) xmlFree(URI);
3384 if (ExternalID != NULL) xmlFree(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003385}
3386
3387/**
3388 * htmlParseAttribute:
3389 * @ctxt: an HTML parser context
3390 * @value: a xmlChar ** used to store the value of the attribute
3391 *
3392 * parse an attribute
3393 *
3394 * [41] Attribute ::= Name Eq AttValue
3395 *
3396 * [25] Eq ::= S? '=' S?
3397 *
3398 * With namespace:
3399 *
3400 * [NS 11] Attribute ::= QName Eq AttValue
3401 *
3402 * Also the case QName == xmlns:??? is handled independently as a namespace
3403 * definition.
3404 *
3405 * Returns the attribute name, and the value in *value.
3406 */
3407
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003408static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003409htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003410 const xmlChar *name;
3411 xmlChar *val = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003412
3413 *value = NULL;
3414 name = htmlParseHTMLName(ctxt);
3415 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003416 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3417 "error parsing attribute name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003418 return(NULL);
3419 }
3420
3421 /*
3422 * read the value
3423 */
3424 SKIP_BLANKS;
3425 if (CUR == '=') {
3426 NEXT;
3427 SKIP_BLANKS;
3428 val = htmlParseAttValue(ctxt);
Daniel Veillardc47d2632006-10-17 16:13:27 +00003429 } else if (htmlIsBooleanAttr(name)) {
3430 /*
3431 * assume a minimized attribute
3432 */
3433 val = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00003434 }
3435
3436 *value = val;
3437 return(name);
3438}
3439
3440/**
3441 * htmlCheckEncoding:
3442 * @ctxt: an HTML parser context
3443 * @attvalue: the attribute value
3444 *
3445 * Checks an http-equiv attribute from a Meta tag to detect
3446 * the encoding
3447 * If a new encoding is detected the parser is switched to decode
3448 * it and pass UTF8
3449 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003450static void
Owen Taylor3473f882001-02-23 17:55:21 +00003451htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3452 const xmlChar *encoding;
3453
3454 if ((ctxt == NULL) || (attvalue == NULL))
3455 return;
3456
Daniel Veillarde77db162009-08-22 11:32:38 +02003457 /* do not change encoding */
Owen Taylor3473f882001-02-23 17:55:21 +00003458 if (ctxt->input->encoding != NULL)
3459 return;
3460
3461 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
3462 if (encoding != NULL) {
3463 encoding += 8;
3464 } else {
3465 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
3466 if (encoding != NULL)
3467 encoding += 9;
3468 }
3469 if (encoding != NULL) {
3470 xmlCharEncoding enc;
3471 xmlCharEncodingHandlerPtr handler;
3472
3473 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3474
3475 if (ctxt->input->encoding != NULL)
3476 xmlFree((xmlChar *) ctxt->input->encoding);
3477 ctxt->input->encoding = xmlStrdup(encoding);
3478
3479 enc = xmlParseCharEncoding((const char *) encoding);
3480 /*
3481 * registered set of known encodings
3482 */
3483 if (enc != XML_CHAR_ENCODING_ERROR) {
Daniel Veillarde77db162009-08-22 11:32:38 +02003484 if (((enc == XML_CHAR_ENCODING_UTF16LE) ||
Daniel Veillard7e303562006-10-16 13:14:55 +00003485 (enc == XML_CHAR_ENCODING_UTF16BE) ||
3486 (enc == XML_CHAR_ENCODING_UCS4LE) ||
3487 (enc == XML_CHAR_ENCODING_UCS4BE)) &&
3488 (ctxt->input->buf != NULL) &&
3489 (ctxt->input->buf->encoder == NULL)) {
3490 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3491 "htmlCheckEncoding: wrong encoding meta\n",
3492 NULL, NULL);
3493 } else {
3494 xmlSwitchEncoding(ctxt, enc);
3495 }
Owen Taylor3473f882001-02-23 17:55:21 +00003496 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3497 } else {
3498 /*
3499 * fallback for unknown encodings
3500 */
3501 handler = xmlFindCharEncodingHandler((const char *) encoding);
3502 if (handler != NULL) {
3503 xmlSwitchToEncoding(ctxt, handler);
3504 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3505 } else {
3506 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3507 }
3508 }
3509
3510 if ((ctxt->input->buf != NULL) &&
3511 (ctxt->input->buf->encoder != NULL) &&
3512 (ctxt->input->buf->raw != NULL) &&
3513 (ctxt->input->buf->buffer != NULL)) {
3514 int nbchars;
3515 int processed;
3516
3517 /*
3518 * convert as much as possible to the parser reading buffer.
3519 */
3520 processed = ctxt->input->cur - ctxt->input->base;
3521 xmlBufferShrink(ctxt->input->buf->buffer, processed);
3522 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
3523 ctxt->input->buf->buffer,
3524 ctxt->input->buf->raw);
3525 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003526 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3527 "htmlCheckEncoding: encoder error\n",
3528 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003529 }
3530 ctxt->input->base =
3531 ctxt->input->cur = ctxt->input->buf->buffer->content;
Eugene Pimenov1e60fbc2010-03-10 18:10:49 +01003532 ctxt->input->end =
3533 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00003534 }
3535 }
3536}
3537
3538/**
3539 * htmlCheckMeta:
3540 * @ctxt: an HTML parser context
3541 * @atts: the attributes values
3542 *
3543 * Checks an attributes from a Meta tag
3544 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003545static void
Owen Taylor3473f882001-02-23 17:55:21 +00003546htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3547 int i;
3548 const xmlChar *att, *value;
3549 int http = 0;
3550 const xmlChar *content = NULL;
3551
3552 if ((ctxt == NULL) || (atts == NULL))
3553 return;
3554
3555 i = 0;
3556 att = atts[i++];
3557 while (att != NULL) {
3558 value = atts[i++];
3559 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3560 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3561 http = 1;
3562 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3563 content = value;
3564 att = atts[i++];
3565 }
3566 if ((http) && (content != NULL))
3567 htmlCheckEncoding(ctxt, content);
3568
3569}
3570
3571/**
3572 * htmlParseStartTag:
3573 * @ctxt: an HTML parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02003574 *
Owen Taylor3473f882001-02-23 17:55:21 +00003575 * parse a start of tag either for rule element or
3576 * EmptyElement. In both case we don't parse the tag closing chars.
3577 *
3578 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3579 *
3580 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3581 *
3582 * With namespace:
3583 *
3584 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3585 *
3586 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3587 *
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003588 * Returns 0 in case of success, -1 in case of error and 1 if discarded
Owen Taylor3473f882001-02-23 17:55:21 +00003589 */
3590
Daniel Veillard597f1c12005-07-03 23:00:18 +00003591static int
Owen Taylor3473f882001-02-23 17:55:21 +00003592htmlParseStartTag(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003593 const xmlChar *name;
3594 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00003595 xmlChar *attvalue;
Daniel Veillard30e76072006-03-09 14:13:55 +00003596 const xmlChar **atts;
Owen Taylor3473f882001-02-23 17:55:21 +00003597 int nbatts = 0;
Daniel Veillard30e76072006-03-09 14:13:55 +00003598 int maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003599 int meta = 0;
3600 int i;
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003601 int discardtag = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003602
Daniel Veillarde77db162009-08-22 11:32:38 +02003603 if (ctxt->instate == XML_PARSER_EOF)
3604 return(-1);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003605 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3606 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3607 "htmlParseStartTag: context error\n", NULL, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003608 return -1;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003609 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003610 if (CUR != '<') return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003611 NEXT;
3612
Daniel Veillard30e76072006-03-09 14:13:55 +00003613 atts = ctxt->atts;
3614 maxatts = ctxt->maxatts;
3615
Owen Taylor3473f882001-02-23 17:55:21 +00003616 GROW;
3617 name = htmlParseHTMLName(ctxt);
3618 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003619 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3620 "htmlParseStartTag: invalid element name\n",
3621 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003622 /* Dump the bogus tag like browsers do */
Daniel Veillarde77db162009-08-22 11:32:38 +02003623 while ((IS_CHAR_CH(CUR)) && (CUR != '>') &&
3624 (ctxt->instate != XML_PARSER_EOF))
Owen Taylor3473f882001-02-23 17:55:21 +00003625 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003626 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003627 }
3628 if (xmlStrEqual(name, BAD_CAST"meta"))
3629 meta = 1;
3630
3631 /*
3632 * Check for auto-closure of HTML elements.
3633 */
3634 htmlAutoClose(ctxt, name);
3635
3636 /*
3637 * Check for implied HTML elements.
3638 */
3639 htmlCheckImplied(ctxt, name);
3640
3641 /*
3642 * Avoid html at any level > 0, head at any level != 1
3643 * or any attempt to recurse body
3644 */
3645 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003646 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3647 "htmlParseStartTag: misplaced <html> tag\n",
3648 name, NULL);
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003649 discardtag = 1;
Daniel Veillarded86dc22008-04-24 11:58:41 +00003650 ctxt->depth++;
Owen Taylor3473f882001-02-23 17:55:21 +00003651 }
Daniel Veillarde77db162009-08-22 11:32:38 +02003652 if ((ctxt->nameNr != 1) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003653 (xmlStrEqual(name, BAD_CAST"head"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003654 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3655 "htmlParseStartTag: misplaced <head> tag\n",
3656 name, NULL);
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003657 discardtag = 1;
Daniel Veillarded86dc22008-04-24 11:58:41 +00003658 ctxt->depth++;
Owen Taylor3473f882001-02-23 17:55:21 +00003659 }
3660 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003661 int indx;
3662 for (indx = 0;indx < ctxt->nameNr;indx++) {
3663 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003664 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3665 "htmlParseStartTag: misplaced <body> tag\n",
3666 name, NULL);
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003667 discardtag = 1;
Daniel Veillarded86dc22008-04-24 11:58:41 +00003668 ctxt->depth++;
Owen Taylor3473f882001-02-23 17:55:21 +00003669 }
3670 }
3671 }
3672
3673 /*
3674 * Now parse the attributes, it ends up with the ending
3675 *
3676 * (S Attribute)* S?
3677 */
3678 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003679 while ((IS_CHAR_CH(CUR)) &&
Daniel Veillarde77db162009-08-22 11:32:38 +02003680 (CUR != '>') &&
Owen Taylor3473f882001-02-23 17:55:21 +00003681 ((CUR != '/') || (NXT(1) != '>'))) {
3682 long cons = ctxt->nbChars;
3683
3684 GROW;
3685 attname = htmlParseAttribute(ctxt, &attvalue);
3686 if (attname != NULL) {
3687
3688 /*
3689 * Well formedness requires at most one declaration of an attribute
3690 */
3691 for (i = 0; i < nbatts;i += 2) {
3692 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003693 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
3694 "Attribute %s redefined\n", attname, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003695 if (attvalue != NULL)
3696 xmlFree(attvalue);
3697 goto failed;
3698 }
3699 }
3700
3701 /*
3702 * Add the pair to atts
3703 */
3704 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003705 maxatts = 22; /* allow for 10 attrs by default */
3706 atts = (const xmlChar **)
3707 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003708 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003709 htmlErrMemory(ctxt, NULL);
3710 if (attvalue != NULL)
3711 xmlFree(attvalue);
3712 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003713 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003714 ctxt->atts = atts;
3715 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003716 } else if (nbatts + 4 > maxatts) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003717 const xmlChar **n;
3718
Owen Taylor3473f882001-02-23 17:55:21 +00003719 maxatts *= 2;
Daniel Veillardf403d292003-10-05 13:51:35 +00003720 n = (const xmlChar **) xmlRealloc((void *) atts,
3721 maxatts * sizeof(const xmlChar *));
3722 if (n == NULL) {
3723 htmlErrMemory(ctxt, NULL);
3724 if (attvalue != NULL)
3725 xmlFree(attvalue);
3726 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003727 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003728 atts = n;
3729 ctxt->atts = atts;
3730 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003731 }
3732 atts[nbatts++] = attname;
3733 atts[nbatts++] = attvalue;
3734 atts[nbatts] = NULL;
3735 atts[nbatts + 1] = NULL;
3736 }
3737 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003738 if (attvalue != NULL)
3739 xmlFree(attvalue);
Owen Taylor3473f882001-02-23 17:55:21 +00003740 /* Dump the bogus attribute string up to the next blank or
3741 * the end of the tag. */
William M. Brack76e95df2003-10-18 16:20:14 +00003742 while ((IS_CHAR_CH(CUR)) &&
3743 !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
Daniel Veillard34ba3872003-07-15 13:34:05 +00003744 ((CUR != '/') || (NXT(1) != '>')))
Owen Taylor3473f882001-02-23 17:55:21 +00003745 NEXT;
3746 }
3747
3748failed:
3749 SKIP_BLANKS;
3750 if (cons == ctxt->nbChars) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003751 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3752 "htmlParseStartTag: problem parsing attributes\n",
3753 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003754 break;
3755 }
3756 }
3757
3758 /*
3759 * Handle specific association to the META tag
3760 */
William M. Bracke978ae22007-03-21 06:16:02 +00003761 if (meta && (nbatts != 0))
Owen Taylor3473f882001-02-23 17:55:21 +00003762 htmlCheckMeta(ctxt, atts);
3763
3764 /*
3765 * SAX: Start of Element !
3766 */
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003767 if (!discardtag) {
3768 htmlnamePush(ctxt, name);
3769 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
3770 if (nbatts != 0)
3771 ctxt->sax->startElement(ctxt->userData, name, atts);
3772 else
3773 ctxt->sax->startElement(ctxt->userData, name, NULL);
3774 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003775 }
Owen Taylor3473f882001-02-23 17:55:21 +00003776
3777 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003778 for (i = 1;i < nbatts;i += 2) {
Owen Taylor3473f882001-02-23 17:55:21 +00003779 if (atts[i] != NULL)
3780 xmlFree((xmlChar *) atts[i]);
3781 }
Owen Taylor3473f882001-02-23 17:55:21 +00003782 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003783
Daniel Veillard35fcbb82008-03-12 21:43:39 +00003784 return(discardtag);
Owen Taylor3473f882001-02-23 17:55:21 +00003785}
3786
3787/**
3788 * htmlParseEndTag:
3789 * @ctxt: an HTML parser context
3790 *
3791 * parse an end of tag
3792 *
3793 * [42] ETag ::= '</' Name S? '>'
3794 *
3795 * With namespace
3796 *
3797 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003798 *
3799 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003800 */
3801
Daniel Veillardf420ac52001-07-04 16:04:09 +00003802static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003803htmlParseEndTag(htmlParserCtxtPtr ctxt)
3804{
3805 const xmlChar *name;
3806 const xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003807 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003808
3809 if ((CUR != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003810 htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
3811 "htmlParseEndTag: '</' not found\n", NULL, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003812 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003813 }
3814 SKIP(2);
3815
3816 name = htmlParseHTMLName(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003817 if (name == NULL)
3818 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003819 /*
3820 * We should definitely be at the ending "S? '>'" part
3821 */
3822 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003823 if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003824 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3825 "End tag : expected '>'\n", NULL, NULL);
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00003826 if (ctxt->recovery) {
3827 /*
3828 * We're not at the ending > !!
3829 * Error, unless in recover mode where we search forwards
3830 * until we find a >
3831 */
3832 while (CUR != '\0' && CUR != '>') NEXT;
3833 NEXT;
3834 }
Owen Taylor3473f882001-02-23 17:55:21 +00003835 } else
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003836 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003837
3838 /*
Daniel Veillarded86dc22008-04-24 11:58:41 +00003839 * if we ignored misplaced tags in htmlParseStartTag don't pop them
3840 * out now.
3841 */
3842 if ((ctxt->depth > 0) &&
3843 (xmlStrEqual(name, BAD_CAST "html") ||
3844 xmlStrEqual(name, BAD_CAST "body") ||
3845 xmlStrEqual(name, BAD_CAST "head"))) {
3846 ctxt->depth--;
3847 return (0);
3848 }
3849
3850 /*
Owen Taylor3473f882001-02-23 17:55:21 +00003851 * If the name read is not one of the element in the parsing stack
3852 * then return, it's just an error.
3853 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003854 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
3855 if (xmlStrEqual(name, ctxt->nameTab[i]))
3856 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003857 }
3858 if (i < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003859 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3860 "Unexpected end tag : %s\n", name, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003861 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003862 }
3863
3864
3865 /*
3866 * Check for auto-closure of HTML elements.
3867 */
3868
3869 htmlAutoCloseOnClose(ctxt, name);
3870
3871 /*
3872 * Well formedness constraints, opening and closing must match.
3873 * With the exception that the autoclose may have popped stuff out
3874 * of the stack.
3875 */
3876 if (!xmlStrEqual(name, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003877 if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003878 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3879 "Opening and ending tag mismatch: %s and %s\n",
3880 name, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003881 }
3882 }
3883
3884 /*
3885 * SAX: End of Tag
3886 */
3887 oldname = ctxt->name;
3888 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003889 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3890 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003891 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003892 ret = 1;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003893 } else {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003894 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003895 }
3896
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003897 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003898}
3899
3900
3901/**
3902 * htmlParseReference:
3903 * @ctxt: an HTML parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02003904 *
Owen Taylor3473f882001-02-23 17:55:21 +00003905 * parse and handle entity references in content,
3906 * this will end-up in a call to character() since this is either a
3907 * CharRef, or a predefined entity.
3908 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003909static void
Owen Taylor3473f882001-02-23 17:55:21 +00003910htmlParseReference(htmlParserCtxtPtr ctxt) {
Daniel Veillardbb371292001-08-16 23:26:59 +00003911 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00003912 xmlChar out[6];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003913 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003914 if (CUR != '&') return;
3915
3916 if (NXT(1) == '#') {
3917 unsigned int c;
3918 int bits, i = 0;
3919
3920 c = htmlParseCharRef(ctxt);
3921 if (c == 0)
3922 return;
3923
3924 if (c < 0x80) { out[i++]= c; bits= -6; }
3925 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3926 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3927 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
Daniel Veillarde77db162009-08-22 11:32:38 +02003928
Owen Taylor3473f882001-02-23 17:55:21 +00003929 for ( ; bits >= 0; bits-= 6) {
3930 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3931 }
3932 out[i] = 0;
3933
3934 htmlCheckParagraph(ctxt);
3935 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3936 ctxt->sax->characters(ctxt->userData, out, i);
3937 } else {
3938 ent = htmlParseEntityRef(ctxt, &name);
3939 if (name == NULL) {
3940 htmlCheckParagraph(ctxt);
3941 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3942 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3943 return;
3944 }
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003945 if ((ent == NULL) || !(ent->value > 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003946 htmlCheckParagraph(ctxt);
3947 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3948 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3949 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3950 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3951 }
3952 } else {
3953 unsigned int c;
3954 int bits, i = 0;
3955
3956 c = ent->value;
3957 if (c < 0x80)
3958 { out[i++]= c; bits= -6; }
3959 else if (c < 0x800)
3960 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3961 else if (c < 0x10000)
3962 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
Daniel Veillarde77db162009-08-22 11:32:38 +02003963 else
Owen Taylor3473f882001-02-23 17:55:21 +00003964 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
Daniel Veillarde77db162009-08-22 11:32:38 +02003965
Owen Taylor3473f882001-02-23 17:55:21 +00003966 for ( ; bits >= 0; bits-= 6) {
3967 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3968 }
3969 out[i] = 0;
3970
3971 htmlCheckParagraph(ctxt);
3972 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3973 ctxt->sax->characters(ctxt->userData, out, i);
3974 }
Owen Taylor3473f882001-02-23 17:55:21 +00003975 }
3976}
3977
3978/**
3979 * htmlParseContent:
3980 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00003981 *
3982 * Parse a content: comment, sub-element, reference or text.
Eugene Pimenov615904f2010-03-15 15:16:02 +01003983 * Kept for compatibility with old code
Owen Taylor3473f882001-02-23 17:55:21 +00003984 */
3985
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003986static void
Owen Taylor3473f882001-02-23 17:55:21 +00003987htmlParseContent(htmlParserCtxtPtr ctxt) {
3988 xmlChar *currentNode;
3989 int depth;
Daniel Veillard890fd9f2006-10-27 12:53:28 +00003990 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003991
3992 currentNode = xmlStrdup(ctxt->name);
3993 depth = ctxt->nameNr;
3994 while (1) {
3995 long cons = ctxt->nbChars;
3996
3997 GROW;
Daniel Veillarde77db162009-08-22 11:32:38 +02003998
3999 if (ctxt->instate == XML_PARSER_EOF)
4000 break;
4001
Owen Taylor3473f882001-02-23 17:55:21 +00004002 /*
4003 * Our tag or one of it's parent or children is ending.
4004 */
4005 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00004006 if (htmlParseEndTag(ctxt) &&
4007 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
4008 if (currentNode != NULL)
4009 xmlFree(currentNode);
4010 return;
4011 }
4012 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00004013 }
4014
Daniel Veillard890fd9f2006-10-27 12:53:28 +00004015 else if ((CUR == '<') &&
4016 ((IS_ASCII_LETTER(NXT(1))) ||
4017 (NXT(1) == '_') || (NXT(1) == ':'))) {
4018 name = htmlParseHTMLName_nonInvasive(ctxt);
4019 if (name == NULL) {
4020 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
4021 "htmlParseStartTag: invalid element name\n",
4022 NULL, NULL);
4023 /* Dump the bogus tag like browsers do */
Daniel Veillarde77db162009-08-22 11:32:38 +02004024 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
Daniel Veillard890fd9f2006-10-27 12:53:28 +00004025 NEXT;
4026
4027 if (currentNode != NULL)
4028 xmlFree(currentNode);
4029 return;
4030 }
4031
4032 if (ctxt->name != NULL) {
4033 if (htmlCheckAutoClose(name, ctxt->name) == 1) {
4034 htmlAutoClose(ctxt, name);
4035 continue;
4036 }
Daniel Veillarde77db162009-08-22 11:32:38 +02004037 }
Daniel Veillard890fd9f2006-10-27 12:53:28 +00004038 }
4039
Owen Taylor3473f882001-02-23 17:55:21 +00004040 /*
4041 * Has this node been popped out during parsing of
4042 * the next element
4043 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00004044 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
4045 (!xmlStrEqual(currentNode, ctxt->name)))
4046 {
Owen Taylor3473f882001-02-23 17:55:21 +00004047 if (currentNode != NULL) xmlFree(currentNode);
4048 return;
4049 }
4050
Daniel Veillardf9533d12001-03-03 10:04:57 +00004051 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
4052 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004053 /*
4054 * Handle SCRIPT/STYLE separately
4055 */
4056 htmlParseScript(ctxt);
4057 } else {
4058 /*
4059 * Sometimes DOCTYPE arrives in the middle of the document
4060 */
4061 if ((CUR == '<') && (NXT(1) == '!') &&
4062 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4063 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4064 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4065 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004066 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4067 "Misplaced DOCTYPE declaration\n",
4068 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004069 htmlParseDocTypeDecl(ctxt);
4070 }
4071
4072 /*
4073 * First case : a comment
4074 */
4075 if ((CUR == '<') && (NXT(1) == '!') &&
4076 (NXT(2) == '-') && (NXT(3) == '-')) {
4077 htmlParseComment(ctxt);
4078 }
4079
4080 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004081 * Second case : a Processing Instruction.
4082 */
4083 else if ((CUR == '<') && (NXT(1) == '?')) {
4084 htmlParsePI(ctxt);
4085 }
4086
4087 /*
4088 * Third case : a sub-element.
Owen Taylor3473f882001-02-23 17:55:21 +00004089 */
4090 else if (CUR == '<') {
4091 htmlParseElement(ctxt);
4092 }
4093
4094 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004095 * Fourth case : a reference. If if has not been resolved,
Daniel Veillarde77db162009-08-22 11:32:38 +02004096 * parsing returns it's Name, create the node
Owen Taylor3473f882001-02-23 17:55:21 +00004097 */
4098 else if (CUR == '&') {
4099 htmlParseReference(ctxt);
4100 }
4101
4102 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004103 * Fifth case : end of the resource
Owen Taylor3473f882001-02-23 17:55:21 +00004104 */
4105 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004106 htmlAutoCloseOnEnd(ctxt);
4107 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004108 }
4109
4110 /*
4111 * Last case, text. Note that References are handled directly.
4112 */
4113 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004114 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004115 }
4116
4117 if (cons == ctxt->nbChars) {
4118 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004119 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4120 "detected an error in element content\n",
4121 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004122 }
4123 break;
4124 }
4125 }
4126 GROW;
4127 }
4128 if (currentNode != NULL) xmlFree(currentNode);
4129}
4130
4131/**
4132 * htmlParseElement:
4133 * @ctxt: an HTML parser context
4134 *
4135 * parse an HTML element, this is highly recursive
Eugene Pimenov615904f2010-03-15 15:16:02 +01004136 * this is kept for compatibility with previous code versions
Owen Taylor3473f882001-02-23 17:55:21 +00004137 *
4138 * [39] element ::= EmptyElemTag | STag content ETag
4139 *
4140 * [41] Attribute ::= Name Eq AttValue
4141 */
4142
4143void
4144htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004145 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004146 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00004147 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004148 htmlParserNodeInfo node_info;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004149 int failed;
Daniel Veillarda03e3652004-11-02 18:45:30 +00004150 int depth;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00004151 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00004152
Daniel Veillarda03e3652004-11-02 18:45:30 +00004153 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4154 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard597f1c12005-07-03 23:00:18 +00004155 "htmlParseElement: context error\n", NULL, NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00004156 return;
4157 }
Daniel Veillarddb4ac222009-08-22 17:58:31 +02004158
4159 if (ctxt->instate == XML_PARSER_EOF)
4160 return;
4161
Owen Taylor3473f882001-02-23 17:55:21 +00004162 /* Capture start position */
4163 if (ctxt->record_info) {
4164 node_info.begin_pos = ctxt->input->consumed +
4165 (CUR_PTR - ctxt->input->base);
4166 node_info.begin_line = ctxt->input->line;
4167 }
4168
Daniel Veillard597f1c12005-07-03 23:00:18 +00004169 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004170 name = ctxt->name;
Daniel Veillard35fcbb82008-03-12 21:43:39 +00004171 if ((failed == -1) || (name == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004172 if (CUR == '>')
4173 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004174 return;
4175 }
Owen Taylor3473f882001-02-23 17:55:21 +00004176
4177 /*
4178 * Lookup the info for that element.
4179 */
4180 info = htmlTagLookup(name);
4181 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004182 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4183 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004184 }
4185
4186 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004187 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004188 */
4189 if ((CUR == '/') && (NXT(1) == '>')) {
4190 SKIP(2);
4191 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4192 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00004193 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004194 return;
4195 }
4196
4197 if (CUR == '>') {
4198 NEXT;
4199 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004200 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4201 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004202
4203 /*
4204 * end of parsing of this node.
4205 */
Daniel Veillarde77db162009-08-22 11:32:38 +02004206 if (xmlStrEqual(name, ctxt->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004207 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00004208 htmlnamePop(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02004209 }
Owen Taylor3473f882001-02-23 17:55:21 +00004210
4211 /*
4212 * Capture end position and add node
4213 */
Daniel Veillard30e76072006-03-09 14:13:55 +00004214 if (ctxt->record_info) {
Owen Taylor3473f882001-02-23 17:55:21 +00004215 node_info.end_pos = ctxt->input->consumed +
4216 (CUR_PTR - ctxt->input->base);
4217 node_info.end_line = ctxt->input->line;
4218 node_info.node = ctxt->node;
4219 xmlParserAddNodeInfo(ctxt, &node_info);
4220 }
4221 return;
4222 }
4223
4224 /*
4225 * Check for an Empty Element from DTD definition
4226 */
4227 if ((info != NULL) && (info->empty)) {
4228 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4229 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00004230 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004231 return;
4232 }
4233
4234 /*
4235 * Parse the content of the element:
4236 */
4237 currentNode = xmlStrdup(ctxt->name);
4238 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00004239 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00004240 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004241 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00004242 if (oldptr==ctxt->input->cur) break;
Daniel Veillarde77db162009-08-22 11:32:38 +02004243 if (ctxt->nameNr < depth) break;
4244 }
Owen Taylor3473f882001-02-23 17:55:21 +00004245
Owen Taylor3473f882001-02-23 17:55:21 +00004246 /*
4247 * Capture end position and add node
4248 */
4249 if ( currentNode != NULL && ctxt->record_info ) {
4250 node_info.end_pos = ctxt->input->consumed +
4251 (CUR_PTR - ctxt->input->base);
4252 node_info.end_line = ctxt->input->line;
4253 node_info.node = ctxt->node;
4254 xmlParserAddNodeInfo(ctxt, &node_info);
4255 }
William M. Brack76e95df2003-10-18 16:20:14 +00004256 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004257 htmlAutoCloseOnEnd(ctxt);
4258 }
4259
Owen Taylor3473f882001-02-23 17:55:21 +00004260 if (currentNode != NULL)
4261 xmlFree(currentNode);
4262}
4263
Eugene Pimenov615904f2010-03-15 15:16:02 +01004264static void
4265htmlParserFinishElementParsing(htmlParserCtxtPtr ctxt) {
4266 /*
4267 * Capture end position and add node
4268 */
4269 if ( ctxt->node != NULL && ctxt->record_info ) {
4270 ctxt->nodeInfo->end_pos = ctxt->input->consumed +
4271 (CUR_PTR - ctxt->input->base);
4272 ctxt->nodeInfo->end_line = ctxt->input->line;
4273 ctxt->nodeInfo->node = ctxt->node;
4274 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
4275 htmlNodeInfoPop(ctxt);
4276 }
4277 if (!IS_CHAR_CH(CUR)) {
4278 htmlAutoCloseOnEnd(ctxt);
4279 }
4280}
4281
4282/**
4283 * htmlParseElementInternal:
4284 * @ctxt: an HTML parser context
4285 *
4286 * parse an HTML element, new version, non recursive
4287 *
4288 * [39] element ::= EmptyElemTag | STag content ETag
4289 *
4290 * [41] Attribute ::= Name Eq AttValue
4291 */
4292
4293static void
4294htmlParseElementInternal(htmlParserCtxtPtr ctxt) {
4295 const xmlChar *name;
4296 const htmlElemDesc * info;
4297 htmlParserNodeInfo node_info;
4298 int failed;
4299 int depth;
4300 const xmlChar *oldptr;
4301
4302 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4303 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4304 "htmlParseElementInternal: context error\n", NULL, NULL);
4305 return;
4306 }
4307
4308 if (ctxt->instate == XML_PARSER_EOF)
4309 return;
4310
4311 /* Capture start position */
4312 if (ctxt->record_info) {
4313 node_info.begin_pos = ctxt->input->consumed +
4314 (CUR_PTR - ctxt->input->base);
4315 node_info.begin_line = ctxt->input->line;
4316 }
4317
4318 failed = htmlParseStartTag(ctxt);
4319 name = ctxt->name;
4320 if ((failed == -1) || (name == NULL)) {
4321 if (CUR == '>')
4322 NEXT;
4323 return;
4324 }
4325
4326 /*
4327 * Lookup the info for that element.
4328 */
4329 info = htmlTagLookup(name);
4330 if (info == NULL) {
4331 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4332 "Tag %s invalid\n", name, NULL);
4333 }
4334
4335 /*
4336 * Check for an Empty Element labeled the XML/SGML way
4337 */
4338 if ((CUR == '/') && (NXT(1) == '>')) {
4339 SKIP(2);
4340 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4341 ctxt->sax->endElement(ctxt->userData, name);
4342 htmlnamePop(ctxt);
4343 return;
4344 }
4345
4346 if (CUR == '>') {
4347 NEXT;
4348 } else {
4349 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4350 "Couldn't find end of Start Tag %s\n", name, NULL);
4351
4352 /*
4353 * end of parsing of this node.
4354 */
4355 if (xmlStrEqual(name, ctxt->name)) {
4356 nodePop(ctxt);
4357 htmlnamePop(ctxt);
4358 }
4359
4360 if (ctxt->record_info)
4361 htmlNodeInfoPush(ctxt, &node_info);
4362 htmlParserFinishElementParsing(ctxt);
4363 return;
4364 }
4365
4366 /*
4367 * Check for an Empty Element from DTD definition
4368 */
4369 if ((info != NULL) && (info->empty)) {
4370 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4371 ctxt->sax->endElement(ctxt->userData, name);
4372 htmlnamePop(ctxt);
4373 return;
4374 }
4375
4376 if (ctxt->record_info)
4377 htmlNodeInfoPush(ctxt, &node_info);
4378}
4379
4380/**
4381 * htmlParseContentInternal:
4382 * @ctxt: an HTML parser context
4383 *
4384 * Parse a content: comment, sub-element, reference or text.
4385 * New version for non recursive htmlParseElementInternal
4386 */
4387
4388static void
4389htmlParseContentInternal(htmlParserCtxtPtr ctxt) {
4390 xmlChar *currentNode;
4391 int depth;
4392 const xmlChar *name;
4393
4394 currentNode = xmlStrdup(ctxt->name);
4395 depth = ctxt->nameNr;
4396 while (1) {
4397 long cons = ctxt->nbChars;
4398
4399 GROW;
4400
4401 if (ctxt->instate == XML_PARSER_EOF)
4402 break;
4403
4404 /*
4405 * Our tag or one of it's parent or children is ending.
4406 */
4407 if ((CUR == '<') && (NXT(1) == '/')) {
4408 if (htmlParseEndTag(ctxt) &&
4409 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
4410 if (currentNode != NULL)
4411 xmlFree(currentNode);
4412
4413 currentNode = xmlStrdup(ctxt->name);
4414 depth = ctxt->nameNr;
4415 }
4416 continue; /* while */
4417 }
4418
4419 else if ((CUR == '<') &&
4420 ((IS_ASCII_LETTER(NXT(1))) ||
4421 (NXT(1) == '_') || (NXT(1) == ':'))) {
4422 name = htmlParseHTMLName_nonInvasive(ctxt);
4423 if (name == NULL) {
4424 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
4425 "htmlParseStartTag: invalid element name\n",
4426 NULL, NULL);
4427 /* Dump the bogus tag like browsers do */
4428 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
4429 NEXT;
4430
4431 htmlParserFinishElementParsing(ctxt);
4432 if (currentNode != NULL)
4433 xmlFree(currentNode);
4434
4435 currentNode = xmlStrdup(ctxt->name);
4436 depth = ctxt->nameNr;
4437 continue;
4438 }
4439
4440 if (ctxt->name != NULL) {
4441 if (htmlCheckAutoClose(name, ctxt->name) == 1) {
4442 htmlAutoClose(ctxt, name);
4443 continue;
4444 }
4445 }
4446 }
4447
4448 /*
4449 * Has this node been popped out during parsing of
4450 * the next element
4451 */
4452 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
4453 (!xmlStrEqual(currentNode, ctxt->name)))
4454 {
4455 htmlParserFinishElementParsing(ctxt);
4456 if (currentNode != NULL) xmlFree(currentNode);
4457
4458 currentNode = xmlStrdup(ctxt->name);
4459 depth = ctxt->nameNr;
4460 continue;
4461 }
4462
4463 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
4464 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
4465 /*
4466 * Handle SCRIPT/STYLE separately
4467 */
4468 htmlParseScript(ctxt);
4469 } else {
4470 /*
4471 * Sometimes DOCTYPE arrives in the middle of the document
4472 */
4473 if ((CUR == '<') && (NXT(1) == '!') &&
4474 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4475 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4476 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4477 (UPP(8) == 'E')) {
4478 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4479 "Misplaced DOCTYPE declaration\n",
4480 BAD_CAST "DOCTYPE" , NULL);
4481 htmlParseDocTypeDecl(ctxt);
4482 }
4483
4484 /*
4485 * First case : a comment
4486 */
4487 if ((CUR == '<') && (NXT(1) == '!') &&
4488 (NXT(2) == '-') && (NXT(3) == '-')) {
4489 htmlParseComment(ctxt);
4490 }
4491
4492 /*
4493 * Second case : a Processing Instruction.
4494 */
4495 else if ((CUR == '<') && (NXT(1) == '?')) {
4496 htmlParsePI(ctxt);
4497 }
4498
4499 /*
4500 * Third case : a sub-element.
4501 */
4502 else if (CUR == '<') {
4503 htmlParseElementInternal(ctxt);
4504 if (currentNode != NULL) xmlFree(currentNode);
4505
4506 currentNode = xmlStrdup(ctxt->name);
4507 depth = ctxt->nameNr;
4508 }
4509
4510 /*
4511 * Fourth case : a reference. If if has not been resolved,
4512 * parsing returns it's Name, create the node
4513 */
4514 else if (CUR == '&') {
4515 htmlParseReference(ctxt);
4516 }
4517
4518 /*
4519 * Fifth case : end of the resource
4520 */
4521 else if (CUR == 0) {
4522 htmlAutoCloseOnEnd(ctxt);
4523 break;
4524 }
4525
4526 /*
4527 * Last case, text. Note that References are handled directly.
4528 */
4529 else {
4530 htmlParseCharData(ctxt);
4531 }
4532
4533 if (cons == ctxt->nbChars) {
4534 if (ctxt->node != NULL) {
4535 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4536 "detected an error in element content\n",
4537 NULL, NULL);
4538 }
4539 break;
4540 }
4541 }
4542 GROW;
4543 }
4544 if (currentNode != NULL) xmlFree(currentNode);
4545}
4546
4547/**
4548 * htmlParseContent:
4549 * @ctxt: an HTML parser context
4550 *
4551 * Parse a content: comment, sub-element, reference or text.
4552 * This is the entry point when called from parser.c
4553 */
4554
4555void
4556__htmlParseContent(void *ctxt) {
4557 if (ctxt != NULL)
4558 htmlParseContentInternal((htmlParserCtxtPtr) ctxt);
4559}
4560
Owen Taylor3473f882001-02-23 17:55:21 +00004561/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004562 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00004563 * @ctxt: an HTML parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02004564 *
Owen Taylor3473f882001-02-23 17:55:21 +00004565 * parse an HTML document (and build a tree if using the standard SAX
4566 * interface).
4567 *
4568 * Returns 0, -1 in case of error. the parser context is augmented
4569 * as a result of the parsing.
4570 */
4571
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00004572int
Owen Taylor3473f882001-02-23 17:55:21 +00004573htmlParseDocument(htmlParserCtxtPtr ctxt) {
Daniel Veillard7f4547c2008-10-03 07:58:23 +00004574 xmlChar start[4];
4575 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +00004576 xmlDtdPtr dtd;
4577
Daniel Veillardd0463562001-10-13 09:15:48 +00004578 xmlInitParser();
4579
Owen Taylor3473f882001-02-23 17:55:21 +00004580 htmlDefaultSAXHandlerInit();
Owen Taylor3473f882001-02-23 17:55:21 +00004581
Daniel Veillarda03e3652004-11-02 18:45:30 +00004582 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4583 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4584 "htmlParseDocument: context error\n", NULL, NULL);
4585 return(XML_ERR_INTERNAL_ERROR);
4586 }
4587 ctxt->html = 1;
Daniel Veillard4d3e2da2009-05-15 17:55:45 +02004588 ctxt->linenumbers = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004589 GROW;
4590 /*
4591 * SAX: beginning of the document processing.
4592 */
4593 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4594 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4595
Daniel Veillard7f4547c2008-10-03 07:58:23 +00004596 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
4597 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
4598 /*
4599 * Get the 4 first bytes and decode the charset
4600 * if enc != XML_CHAR_ENCODING_NONE
4601 * plug some encoding conversion routines.
4602 */
4603 start[0] = RAW;
4604 start[1] = NXT(1);
4605 start[2] = NXT(2);
4606 start[3] = NXT(3);
4607 enc = xmlDetectCharEncoding(&start[0], 4);
4608 if (enc != XML_CHAR_ENCODING_NONE) {
4609 xmlSwitchEncoding(ctxt, enc);
4610 }
4611 }
4612
Owen Taylor3473f882001-02-23 17:55:21 +00004613 /*
4614 * Wipe out everything which is before the first '<'
4615 */
4616 SKIP_BLANKS;
4617 if (CUR == 0) {
Daniel Veillarde77db162009-08-22 11:32:38 +02004618 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
Daniel Veillardf403d292003-10-05 13:51:35 +00004619 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004620 }
4621
4622 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4623 ctxt->sax->startDocument(ctxt->userData);
4624
4625
4626 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004627 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004628 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004629 while (((CUR == '<') && (NXT(1) == '!') &&
4630 (NXT(2) == '-') && (NXT(3) == '-')) ||
4631 ((CUR == '<') && (NXT(1) == '?'))) {
Daniel Veillard7f4547c2008-10-03 07:58:23 +00004632 htmlParseComment(ctxt);
4633 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004634 SKIP_BLANKS;
Daniel Veillard7f4547c2008-10-03 07:58:23 +00004635 }
Owen Taylor3473f882001-02-23 17:55:21 +00004636
4637
4638 /*
4639 * Then possibly doc type declaration(s) and more Misc
4640 * (doctypedecl Misc*)?
4641 */
4642 if ((CUR == '<') && (NXT(1) == '!') &&
4643 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4644 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4645 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4646 (UPP(8) == 'E')) {
4647 htmlParseDocTypeDecl(ctxt);
4648 }
4649 SKIP_BLANKS;
4650
4651 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004652 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004653 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004654 while (((CUR == '<') && (NXT(1) == '!') &&
4655 (NXT(2) == '-') && (NXT(3) == '-')) ||
4656 ((CUR == '<') && (NXT(1) == '?'))) {
Daniel Veillarde77db162009-08-22 11:32:38 +02004657 htmlParseComment(ctxt);
4658 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004659 SKIP_BLANKS;
Daniel Veillarde77db162009-08-22 11:32:38 +02004660 }
Owen Taylor3473f882001-02-23 17:55:21 +00004661
4662 /*
4663 * Time to start parsing the tree itself
4664 */
Eugene Pimenov615904f2010-03-15 15:16:02 +01004665 htmlParseContentInternal(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004666
4667 /*
4668 * autoclose
4669 */
4670 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004671 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004672
4673
4674 /*
4675 * SAX: end of the document processing.
4676 */
4677 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4678 ctxt->sax->endDocument(ctxt->userData);
4679
4680 if (ctxt->myDoc != NULL) {
4681 dtd = xmlGetIntSubset(ctxt->myDoc);
4682 if (dtd == NULL)
Daniel Veillarde77db162009-08-22 11:32:38 +02004683 ctxt->myDoc->intSubset =
4684 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004685 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4686 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4687 }
4688 if (! ctxt->wellFormed) return(-1);
4689 return(0);
4690}
4691
4692
4693/************************************************************************
4694 * *
4695 * Parser contexts handling *
4696 * *
4697 ************************************************************************/
4698
4699/**
William M. Brackedb65a72004-02-06 07:36:04 +00004700 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004701 * @ctxt: an HTML parser context
4702 *
4703 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00004704 *
4705 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00004706 */
4707
Daniel Veillardf403d292003-10-05 13:51:35 +00004708static int
Owen Taylor3473f882001-02-23 17:55:21 +00004709htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
4710{
4711 htmlSAXHandler *sax;
4712
Daniel Veillardf403d292003-10-05 13:51:35 +00004713 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004714 memset(ctxt, 0, sizeof(htmlParserCtxt));
4715
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004716 ctxt->dict = xmlDictCreate();
4717 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004718 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4719 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004720 }
Owen Taylor3473f882001-02-23 17:55:21 +00004721 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
4722 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004723 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4724 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004725 }
4726 else
4727 memset(sax, 0, sizeof(htmlSAXHandler));
4728
4729 /* Allocate the Input stack */
Daniel Veillarde77db162009-08-22 11:32:38 +02004730 ctxt->inputTab = (htmlParserInputPtr *)
Owen Taylor3473f882001-02-23 17:55:21 +00004731 xmlMalloc(5 * sizeof(htmlParserInputPtr));
4732 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004733 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004734 ctxt->inputNr = 0;
4735 ctxt->inputMax = 0;
4736 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004737 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004738 }
4739 ctxt->inputNr = 0;
4740 ctxt->inputMax = 5;
4741 ctxt->input = NULL;
4742 ctxt->version = NULL;
4743 ctxt->encoding = NULL;
4744 ctxt->standalone = -1;
4745 ctxt->instate = XML_PARSER_START;
4746
4747 /* Allocate the Node stack */
4748 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
4749 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004750 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004751 ctxt->nodeNr = 0;
4752 ctxt->nodeMax = 0;
4753 ctxt->node = NULL;
4754 ctxt->inputNr = 0;
4755 ctxt->inputMax = 0;
4756 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004757 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004758 }
4759 ctxt->nodeNr = 0;
4760 ctxt->nodeMax = 10;
4761 ctxt->node = NULL;
4762
4763 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004764 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00004765 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004766 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004767 ctxt->nameNr = 0;
Eugene Pimenovef9c6362010-03-15 11:37:48 +01004768 ctxt->nameMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004769 ctxt->name = NULL;
4770 ctxt->nodeNr = 0;
4771 ctxt->nodeMax = 0;
4772 ctxt->node = NULL;
4773 ctxt->inputNr = 0;
4774 ctxt->inputMax = 0;
4775 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004776 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004777 }
4778 ctxt->nameNr = 0;
4779 ctxt->nameMax = 10;
4780 ctxt->name = NULL;
4781
Eugene Pimenov615904f2010-03-15 15:16:02 +01004782 ctxt->nodeInfoTab = NULL;
4783 ctxt->nodeInfoNr = 0;
4784 ctxt->nodeInfoMax = 0;
4785
Daniel Veillard092643b2003-09-25 14:29:29 +00004786 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00004787 else {
4788 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00004789 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00004790 }
4791 ctxt->userData = ctxt;
4792 ctxt->myDoc = NULL;
4793 ctxt->wellFormed = 1;
4794 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00004795 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00004796 ctxt->html = 1;
Daniel Veillardeff45a92004-10-29 12:10:55 +00004797 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
William M. Brackedb65a72004-02-06 07:36:04 +00004798 ctxt->vctxt.userData = ctxt;
4799 ctxt->vctxt.error = xmlParserValidityError;
4800 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00004801 ctxt->record_info = 0;
4802 ctxt->validate = 0;
4803 ctxt->nbChars = 0;
4804 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00004805 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004806 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00004807 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004808}
4809
4810/**
4811 * htmlFreeParserCtxt:
4812 * @ctxt: an HTML parser context
4813 *
4814 * Free all the memory used by a parser context. However the parsed
4815 * document in ctxt->myDoc is not freed.
4816 */
4817
4818void
4819htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4820{
4821 xmlFreeParserCtxt(ctxt);
4822}
4823
4824/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004825 * htmlNewParserCtxt:
4826 *
4827 * Allocate and initialize a new parser context.
4828 *
Daniel Veillard34c647c2006-09-21 06:53:59 +00004829 * Returns the htmlParserCtxtPtr or NULL in case of allocation error
Daniel Veillard1d995272002-07-22 16:43:32 +00004830 */
4831
Daniel Veillard34c647c2006-09-21 06:53:59 +00004832htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004833htmlNewParserCtxt(void)
4834{
4835 xmlParserCtxtPtr ctxt;
4836
4837 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4838 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004839 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004840 return(NULL);
4841 }
4842 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004843 if (htmlInitParserCtxt(ctxt) < 0) {
4844 htmlFreeParserCtxt(ctxt);
4845 return(NULL);
4846 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004847 return(ctxt);
4848}
4849
4850/**
4851 * htmlCreateMemoryParserCtxt:
4852 * @buffer: a pointer to a char array
4853 * @size: the size of the array
4854 *
4855 * Create a parser context for an HTML in-memory document.
4856 *
4857 * Returns the new parser context or NULL
4858 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004859htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004860htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4861 xmlParserCtxtPtr ctxt;
4862 xmlParserInputPtr input;
4863 xmlParserInputBufferPtr buf;
4864
4865 if (buffer == NULL)
4866 return(NULL);
4867 if (size <= 0)
4868 return(NULL);
4869
4870 ctxt = htmlNewParserCtxt();
4871 if (ctxt == NULL)
4872 return(NULL);
4873
4874 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4875 if (buf == NULL) return(NULL);
4876
4877 input = xmlNewInputStream(ctxt);
4878 if (input == NULL) {
4879 xmlFreeParserCtxt(ctxt);
4880 return(NULL);
4881 }
4882
4883 input->filename = NULL;
4884 input->buf = buf;
4885 input->base = input->buf->buffer->content;
4886 input->cur = input->buf->buffer->content;
4887 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4888
4889 inputPush(ctxt, input);
4890 return(ctxt);
4891}
4892
4893/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004894 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004895 * @cur: a pointer to an array of xmlChar
4896 * @encoding: a free form C string describing the HTML document encoding, or NULL
4897 *
4898 * Create a parser context for an HTML document.
4899 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004900 * TODO: check the need to add encoding handling there
4901 *
Owen Taylor3473f882001-02-23 17:55:21 +00004902 * Returns the new parser context or NULL
4903 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004904static htmlParserCtxtPtr
Daniel Veillard4d1320f2007-04-26 08:55:33 +00004905htmlCreateDocParserCtxt(const xmlChar *cur, const char *encoding) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004906 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004907 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004908
Daniel Veillard1d995272002-07-22 16:43:32 +00004909 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004910 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004911 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004912 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
Daniel Veillard739e9d02007-04-27 09:33:58 +00004913 if (ctxt == NULL)
4914 return(NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004915
4916 if (encoding != NULL) {
4917 xmlCharEncoding enc;
4918 xmlCharEncodingHandlerPtr handler;
4919
4920 if (ctxt->input->encoding != NULL)
4921 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004922 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004923
4924 enc = xmlParseCharEncoding(encoding);
4925 /*
4926 * registered set of known encodings
4927 */
4928 if (enc != XML_CHAR_ENCODING_ERROR) {
4929 xmlSwitchEncoding(ctxt, enc);
4930 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004931 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarde77db162009-08-22 11:32:38 +02004932 "Unsupported encoding %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00004933 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004934 }
4935 } else {
4936 /*
4937 * fallback for unknown encodings
4938 */
4939 handler = xmlFindCharEncodingHandler((const char *) encoding);
4940 if (handler != NULL) {
4941 xmlSwitchToEncoding(ctxt, handler);
4942 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004943 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4944 "Unsupported encoding %s\n",
4945 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004946 }
4947 }
4948 }
4949 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004950}
4951
Daniel Veillard73b013f2003-09-30 12:36:01 +00004952#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004953/************************************************************************
4954 * *
Daniel Veillarde77db162009-08-22 11:32:38 +02004955 * Progressive parsing interfaces *
Owen Taylor3473f882001-02-23 17:55:21 +00004956 * *
4957 ************************************************************************/
4958
4959/**
4960 * htmlParseLookupSequence:
4961 * @ctxt: an HTML parser context
4962 * @first: the first char to lookup
4963 * @next: the next char to lookup or zero
4964 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004965 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004966 *
4967 * Try to find if a sequence (first, next, third) or just (first next) or
4968 * (first) is available in the input stream.
4969 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4970 * to avoid rescanning sequences of bytes, it DOES change the state of the
4971 * parser, do not use liberally.
4972 * This is basically similar to xmlParseLookupSequence()
4973 *
4974 * Returns the index to the current parsing point if the full sequence
4975 * is available, -1 otherwise.
4976 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004977static int
Owen Taylor3473f882001-02-23 17:55:21 +00004978htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
Jiri Netolicky446e1262009-08-07 17:05:36 +02004979 xmlChar next, xmlChar third, int iscomment,
Daniel Veillardeeb99322009-08-25 14:42:16 +02004980 int ignoreattrval)
4981{
Owen Taylor3473f882001-02-23 17:55:21 +00004982 int base, len;
4983 htmlParserInputPtr in;
4984 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004985 int incomment = 0;
Jiri Netolicky446e1262009-08-07 17:05:36 +02004986 int invalue = 0;
4987 char valdellim = 0x0;
Owen Taylor3473f882001-02-23 17:55:21 +00004988
4989 in = ctxt->input;
Daniel Veillardeeb99322009-08-25 14:42:16 +02004990 if (in == NULL)
4991 return (-1);
4992
Owen Taylor3473f882001-02-23 17:55:21 +00004993 base = in->cur - in->base;
Daniel Veillardeeb99322009-08-25 14:42:16 +02004994 if (base < 0)
4995 return (-1);
4996
Owen Taylor3473f882001-02-23 17:55:21 +00004997 if (ctxt->checkIndex > base)
4998 base = ctxt->checkIndex;
Daniel Veillardeeb99322009-08-25 14:42:16 +02004999
Owen Taylor3473f882001-02-23 17:55:21 +00005000 if (in->buf == NULL) {
Daniel Veillardeeb99322009-08-25 14:42:16 +02005001 buf = in->base;
5002 len = in->length;
Owen Taylor3473f882001-02-23 17:55:21 +00005003 } else {
Daniel Veillardeeb99322009-08-25 14:42:16 +02005004 buf = in->buf->buffer->content;
5005 len = in->buf->buffer->use;
Owen Taylor3473f882001-02-23 17:55:21 +00005006 }
Daniel Veillardeeb99322009-08-25 14:42:16 +02005007
Owen Taylor3473f882001-02-23 17:55:21 +00005008 /* take into account the sequence length */
Daniel Veillardeeb99322009-08-25 14:42:16 +02005009 if (third)
5010 len -= 2;
5011 else if (next)
5012 len--;
5013 for (; base < len; base++) {
5014 if ((!incomment) && (base + 4 < len) && (!iscomment)) {
5015 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
5016 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
5017 incomment = 1;
5018 /* do not increment past <! - some people use <!--> */
5019 base += 2;
5020 }
5021 }
5022 if (ignoreattrval) {
5023 if (buf[base] == '"' || buf[base] == '\'') {
5024 if (invalue) {
5025 if (buf[base] == valdellim) {
5026 invalue = 0;
5027 continue;
5028 }
5029 } else {
5030 valdellim = buf[base];
5031 invalue = 1;
5032 continue;
5033 }
5034 } else if (invalue) {
5035 continue;
5036 }
5037 }
5038 if (incomment) {
5039 if (base + 3 > len)
5040 return (-1);
5041 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
5042 (buf[base + 2] == '>')) {
5043 incomment = 0;
5044 base += 2;
5045 }
5046 continue;
5047 }
Owen Taylor3473f882001-02-23 17:55:21 +00005048 if (buf[base] == first) {
Daniel Veillardeeb99322009-08-25 14:42:16 +02005049 if (third != 0) {
5050 if ((buf[base + 1] != next) || (buf[base + 2] != third))
5051 continue;
5052 } else if (next != 0) {
5053 if (buf[base + 1] != next)
5054 continue;
5055 }
5056 ctxt->checkIndex = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005057#ifdef DEBUG_PUSH
Daniel Veillardeeb99322009-08-25 14:42:16 +02005058 if (next == 0)
5059 xmlGenericError(xmlGenericErrorContext,
5060 "HPP: lookup '%c' found at %d\n",
5061 first, base);
5062 else if (third == 0)
5063 xmlGenericError(xmlGenericErrorContext,
5064 "HPP: lookup '%c%c' found at %d\n",
5065 first, next, base);
5066 else
5067 xmlGenericError(xmlGenericErrorContext,
5068 "HPP: lookup '%c%c%c' found at %d\n",
5069 first, next, third, base);
Owen Taylor3473f882001-02-23 17:55:21 +00005070#endif
Daniel Veillardeeb99322009-08-25 14:42:16 +02005071 return (base - (in->cur - in->base));
5072 }
Owen Taylor3473f882001-02-23 17:55:21 +00005073 }
Daniel Veillardeeb99322009-08-25 14:42:16 +02005074 if ((!incomment) && (!invalue))
5075 ctxt->checkIndex = base;
Owen Taylor3473f882001-02-23 17:55:21 +00005076#ifdef DEBUG_PUSH
5077 if (next == 0)
Daniel Veillardeeb99322009-08-25 14:42:16 +02005078 xmlGenericError(xmlGenericErrorContext,
5079 "HPP: lookup '%c' failed\n", first);
Owen Taylor3473f882001-02-23 17:55:21 +00005080 else if (third == 0)
Daniel Veillardeeb99322009-08-25 14:42:16 +02005081 xmlGenericError(xmlGenericErrorContext,
5082 "HPP: lookup '%c%c' failed\n", first, next);
Daniel Veillarde77db162009-08-22 11:32:38 +02005083 else
Daniel Veillardeeb99322009-08-25 14:42:16 +02005084 xmlGenericError(xmlGenericErrorContext,
5085 "HPP: lookup '%c%c%c' failed\n", first, next,
5086 third);
Owen Taylor3473f882001-02-23 17:55:21 +00005087#endif
Daniel Veillardeeb99322009-08-25 14:42:16 +02005088 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00005089}
5090
5091/**
Markus Kull56a03032009-08-24 19:00:23 +02005092 * htmlParseLookupChars:
5093 * @ctxt: an HTML parser context
5094 * @stop: Array of chars, which stop the lookup.
5095 * @stopLen: Length of stop-Array
5096 *
5097 * Try to find if any char of the stop-Array is available in the input
5098 * stream.
5099 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5100 * to avoid rescanning sequences of bytes, it DOES change the state of the
5101 * parser, do not use liberally.
5102 *
5103 * Returns the index to the current parsing point if a stopChar
5104 * is available, -1 otherwise.
5105 */
5106static int
5107htmlParseLookupChars(htmlParserCtxtPtr ctxt, const xmlChar * stop,
5108 int stopLen)
5109{
5110 int base, len;
5111 htmlParserInputPtr in;
5112 const xmlChar *buf;
5113 int incomment = 0;
5114 int i;
5115
5116 in = ctxt->input;
5117 if (in == NULL)
5118 return (-1);
5119
5120 base = in->cur - in->base;
5121 if (base < 0)
5122 return (-1);
5123
5124 if (ctxt->checkIndex > base)
5125 base = ctxt->checkIndex;
5126
5127 if (in->buf == NULL) {
5128 buf = in->base;
5129 len = in->length;
5130 } else {
5131 buf = in->buf->buffer->content;
5132 len = in->buf->buffer->use;
5133 }
5134
5135 for (; base < len; base++) {
5136 if (!incomment && (base + 4 < len)) {
5137 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
5138 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
5139 incomment = 1;
5140 /* do not increment past <! - some people use <!--> */
5141 base += 2;
5142 }
5143 }
5144 if (incomment) {
5145 if (base + 3 > len)
5146 return (-1);
5147 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
5148 (buf[base + 2] == '>')) {
5149 incomment = 0;
5150 base += 2;
5151 }
5152 continue;
5153 }
5154 for (i = 0; i < stopLen; ++i) {
5155 if (buf[base] == stop[i]) {
5156 ctxt->checkIndex = 0;
5157 return (base - (in->cur - in->base));
5158 }
5159 }
5160 }
5161 ctxt->checkIndex = base;
5162 return (-1);
5163}
5164
5165/**
Owen Taylor3473f882001-02-23 17:55:21 +00005166 * htmlParseTryOrFinish:
5167 * @ctxt: an HTML parser context
5168 * @terminate: last chunk indicator
5169 *
5170 * Try to progress on parsing
5171 *
5172 * Returns zero if no parsing was possible
5173 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005174static int
Owen Taylor3473f882001-02-23 17:55:21 +00005175htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
5176 int ret = 0;
5177 htmlParserInputPtr in;
5178 int avail = 0;
5179 xmlChar cur, next;
5180
5181#ifdef DEBUG_PUSH
5182 switch (ctxt->instate) {
5183 case XML_PARSER_EOF:
5184 xmlGenericError(xmlGenericErrorContext,
5185 "HPP: try EOF\n"); break;
5186 case XML_PARSER_START:
5187 xmlGenericError(xmlGenericErrorContext,
5188 "HPP: try START\n"); break;
5189 case XML_PARSER_MISC:
5190 xmlGenericError(xmlGenericErrorContext,
5191 "HPP: try MISC\n");break;
5192 case XML_PARSER_COMMENT:
5193 xmlGenericError(xmlGenericErrorContext,
5194 "HPP: try COMMENT\n");break;
5195 case XML_PARSER_PROLOG:
5196 xmlGenericError(xmlGenericErrorContext,
5197 "HPP: try PROLOG\n");break;
5198 case XML_PARSER_START_TAG:
5199 xmlGenericError(xmlGenericErrorContext,
5200 "HPP: try START_TAG\n");break;
5201 case XML_PARSER_CONTENT:
5202 xmlGenericError(xmlGenericErrorContext,
5203 "HPP: try CONTENT\n");break;
5204 case XML_PARSER_CDATA_SECTION:
5205 xmlGenericError(xmlGenericErrorContext,
5206 "HPP: try CDATA_SECTION\n");break;
5207 case XML_PARSER_END_TAG:
5208 xmlGenericError(xmlGenericErrorContext,
5209 "HPP: try END_TAG\n");break;
5210 case XML_PARSER_ENTITY_DECL:
5211 xmlGenericError(xmlGenericErrorContext,
5212 "HPP: try ENTITY_DECL\n");break;
5213 case XML_PARSER_ENTITY_VALUE:
5214 xmlGenericError(xmlGenericErrorContext,
5215 "HPP: try ENTITY_VALUE\n");break;
5216 case XML_PARSER_ATTRIBUTE_VALUE:
5217 xmlGenericError(xmlGenericErrorContext,
5218 "HPP: try ATTRIBUTE_VALUE\n");break;
5219 case XML_PARSER_DTD:
5220 xmlGenericError(xmlGenericErrorContext,
5221 "HPP: try DTD\n");break;
5222 case XML_PARSER_EPILOG:
5223 xmlGenericError(xmlGenericErrorContext,
5224 "HPP: try EPILOG\n");break;
5225 case XML_PARSER_PI:
5226 xmlGenericError(xmlGenericErrorContext,
5227 "HPP: try PI\n");break;
5228 case XML_PARSER_SYSTEM_LITERAL:
5229 xmlGenericError(xmlGenericErrorContext,
5230 "HPP: try SYSTEM_LITERAL\n");break;
5231 }
5232#endif
5233
5234 while (1) {
5235
5236 in = ctxt->input;
5237 if (in == NULL) break;
5238 if (in->buf == NULL)
5239 avail = in->length - (in->cur - in->base);
5240 else
5241 avail = in->buf->buffer->use - (in->cur - in->base);
5242 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005243 htmlAutoCloseOnEnd(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02005244 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005245 /*
5246 * SAX: end of the document processing.
5247 */
5248 ctxt->instate = XML_PARSER_EOF;
5249 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5250 ctxt->sax->endDocument(ctxt->userData);
5251 }
5252 }
5253 if (avail < 1)
5254 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00005255 cur = in->cur[0];
5256 if (cur == 0) {
5257 SKIP(1);
5258 continue;
5259 }
5260
Owen Taylor3473f882001-02-23 17:55:21 +00005261 switch (ctxt->instate) {
5262 case XML_PARSER_EOF:
5263 /*
5264 * Document parsing is done !
5265 */
5266 goto done;
5267 case XML_PARSER_START:
5268 /*
5269 * Very first chars read from the document flow.
5270 */
5271 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00005272 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005273 SKIP_BLANKS;
5274 if (in->buf == NULL)
5275 avail = in->length - (in->cur - in->base);
5276 else
5277 avail = in->buf->buffer->use - (in->cur - in->base);
5278 }
5279 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
5280 ctxt->sax->setDocumentLocator(ctxt->userData,
5281 &xmlDefaultSAXLocator);
5282 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
5283 (!ctxt->disableSAX))
5284 ctxt->sax->startDocument(ctxt->userData);
5285
5286 cur = in->cur[0];
5287 next = in->cur[1];
5288 if ((cur == '<') && (next == '!') &&
5289 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5290 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5291 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5292 (UPP(8) == 'E')) {
5293 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005294 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005295 goto done;
5296#ifdef DEBUG_PUSH
5297 xmlGenericError(xmlGenericErrorContext,
5298 "HPP: Parsing internal subset\n");
5299#endif
5300 htmlParseDocTypeDecl(ctxt);
5301 ctxt->instate = XML_PARSER_PROLOG;
5302#ifdef DEBUG_PUSH
5303 xmlGenericError(xmlGenericErrorContext,
5304 "HPP: entering PROLOG\n");
5305#endif
5306 } else {
5307 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00005308#ifdef DEBUG_PUSH
Daniel Veillard597f1c12005-07-03 23:00:18 +00005309 xmlGenericError(xmlGenericErrorContext,
5310 "HPP: entering MISC\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005311#endif
Daniel Veillard597f1c12005-07-03 23:00:18 +00005312 }
Owen Taylor3473f882001-02-23 17:55:21 +00005313 break;
5314 case XML_PARSER_MISC:
5315 SKIP_BLANKS;
5316 if (in->buf == NULL)
5317 avail = in->length - (in->cur - in->base);
5318 else
5319 avail = in->buf->buffer->use - (in->cur - in->base);
5320 if (avail < 2)
5321 goto done;
5322 cur = in->cur[0];
5323 next = in->cur[1];
5324 if ((cur == '<') && (next == '!') &&
5325 (in->cur[2] == '-') && (in->cur[3] == '-')) {
5326 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005327 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005328 goto done;
5329#ifdef DEBUG_PUSH
5330 xmlGenericError(xmlGenericErrorContext,
5331 "HPP: Parsing Comment\n");
5332#endif
5333 htmlParseComment(ctxt);
5334 ctxt->instate = XML_PARSER_MISC;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005335 } else if ((cur == '<') && (next == '?')) {
5336 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005337 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005338 goto done;
5339#ifdef DEBUG_PUSH
5340 xmlGenericError(xmlGenericErrorContext,
5341 "HPP: Parsing PI\n");
5342#endif
5343 htmlParsePI(ctxt);
5344 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00005345 } else if ((cur == '<') && (next == '!') &&
5346 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5347 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5348 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5349 (UPP(8) == 'E')) {
5350 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005351 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005352 goto done;
5353#ifdef DEBUG_PUSH
5354 xmlGenericError(xmlGenericErrorContext,
5355 "HPP: Parsing internal subset\n");
5356#endif
5357 htmlParseDocTypeDecl(ctxt);
5358 ctxt->instate = XML_PARSER_PROLOG;
5359#ifdef DEBUG_PUSH
5360 xmlGenericError(xmlGenericErrorContext,
5361 "HPP: entering PROLOG\n");
5362#endif
5363 } else if ((cur == '<') && (next == '!') &&
5364 (avail < 9)) {
5365 goto done;
5366 } else {
5367 ctxt->instate = XML_PARSER_START_TAG;
5368#ifdef DEBUG_PUSH
5369 xmlGenericError(xmlGenericErrorContext,
5370 "HPP: entering START_TAG\n");
5371#endif
5372 }
5373 break;
5374 case XML_PARSER_PROLOG:
5375 SKIP_BLANKS;
5376 if (in->buf == NULL)
5377 avail = in->length - (in->cur - in->base);
5378 else
5379 avail = in->buf->buffer->use - (in->cur - in->base);
Daniel Veillarde77db162009-08-22 11:32:38 +02005380 if (avail < 2)
Owen Taylor3473f882001-02-23 17:55:21 +00005381 goto done;
5382 cur = in->cur[0];
5383 next = in->cur[1];
5384 if ((cur == '<') && (next == '!') &&
5385 (in->cur[2] == '-') && (in->cur[3] == '-')) {
5386 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005387 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005388 goto done;
5389#ifdef DEBUG_PUSH
5390 xmlGenericError(xmlGenericErrorContext,
5391 "HPP: Parsing Comment\n");
5392#endif
5393 htmlParseComment(ctxt);
5394 ctxt->instate = XML_PARSER_PROLOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005395 } else if ((cur == '<') && (next == '?')) {
5396 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005397 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005398 goto done;
5399#ifdef DEBUG_PUSH
5400 xmlGenericError(xmlGenericErrorContext,
5401 "HPP: Parsing PI\n");
5402#endif
5403 htmlParsePI(ctxt);
5404 ctxt->instate = XML_PARSER_PROLOG;
Owen Taylor3473f882001-02-23 17:55:21 +00005405 } else if ((cur == '<') && (next == '!') &&
5406 (avail < 4)) {
5407 goto done;
5408 } else {
5409 ctxt->instate = XML_PARSER_START_TAG;
5410#ifdef DEBUG_PUSH
5411 xmlGenericError(xmlGenericErrorContext,
5412 "HPP: entering START_TAG\n");
5413#endif
5414 }
5415 break;
5416 case XML_PARSER_EPILOG:
5417 if (in->buf == NULL)
5418 avail = in->length - (in->cur - in->base);
5419 else
5420 avail = in->buf->buffer->use - (in->cur - in->base);
5421 if (avail < 1)
5422 goto done;
5423 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00005424 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005425 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005426 goto done;
5427 }
5428 if (avail < 2)
5429 goto done;
5430 next = in->cur[1];
5431 if ((cur == '<') && (next == '!') &&
5432 (in->cur[2] == '-') && (in->cur[3] == '-')) {
5433 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005434 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005435 goto done;
5436#ifdef DEBUG_PUSH
5437 xmlGenericError(xmlGenericErrorContext,
5438 "HPP: Parsing Comment\n");
5439#endif
5440 htmlParseComment(ctxt);
5441 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005442 } else if ((cur == '<') && (next == '?')) {
5443 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005444 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005445 goto done;
5446#ifdef DEBUG_PUSH
5447 xmlGenericError(xmlGenericErrorContext,
5448 "HPP: Parsing PI\n");
5449#endif
5450 htmlParsePI(ctxt);
5451 ctxt->instate = XML_PARSER_EPILOG;
Owen Taylor3473f882001-02-23 17:55:21 +00005452 } else if ((cur == '<') && (next == '!') &&
5453 (avail < 4)) {
5454 goto done;
5455 } else {
5456 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005457 ctxt->wellFormed = 0;
5458 ctxt->instate = XML_PARSER_EOF;
5459#ifdef DEBUG_PUSH
5460 xmlGenericError(xmlGenericErrorContext,
5461 "HPP: entering EOF\n");
5462#endif
5463 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5464 ctxt->sax->endDocument(ctxt->userData);
5465 goto done;
5466 }
5467 break;
5468 case XML_PARSER_START_TAG: {
Daniel Veillard6a0baa02005-12-10 11:11:12 +00005469 const xmlChar *name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00005470 int failed;
Daniel Veillardbb371292001-08-16 23:26:59 +00005471 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00005472
5473 if (avail < 2)
5474 goto done;
5475 cur = in->cur[0];
5476 if (cur != '<') {
5477 ctxt->instate = XML_PARSER_CONTENT;
5478#ifdef DEBUG_PUSH
5479 xmlGenericError(xmlGenericErrorContext,
5480 "HPP: entering CONTENT\n");
5481#endif
5482 break;
5483 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00005484 if (in->cur[1] == '/') {
5485 ctxt->instate = XML_PARSER_END_TAG;
5486 ctxt->checkIndex = 0;
5487#ifdef DEBUG_PUSH
5488 xmlGenericError(xmlGenericErrorContext,
5489 "HPP: entering END_TAG\n");
5490#endif
5491 break;
5492 }
Owen Taylor3473f882001-02-23 17:55:21 +00005493 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005494 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005495 goto done;
5496
Daniel Veillard597f1c12005-07-03 23:00:18 +00005497 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005498 name = ctxt->name;
Daniel Veillard35fcbb82008-03-12 21:43:39 +00005499 if ((failed == -1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00005500 (name == NULL)) {
5501 if (CUR == '>')
5502 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00005503 break;
5504 }
Owen Taylor3473f882001-02-23 17:55:21 +00005505
5506 /*
5507 * Lookup the info for that element.
5508 */
5509 info = htmlTagLookup(name);
5510 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005511 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
5512 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005513 }
5514
5515 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005516 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00005517 */
5518 if ((CUR == '/') && (NXT(1) == '>')) {
5519 SKIP(2);
5520 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5521 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00005522 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005523 ctxt->instate = XML_PARSER_CONTENT;
5524#ifdef DEBUG_PUSH
5525 xmlGenericError(xmlGenericErrorContext,
5526 "HPP: entering CONTENT\n");
5527#endif
5528 break;
5529 }
5530
5531 if (CUR == '>') {
5532 NEXT;
5533 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005534 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
5535 "Couldn't find end of Start Tag %s\n",
5536 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005537
5538 /*
5539 * end of parsing of this node.
5540 */
Daniel Veillarde77db162009-08-22 11:32:38 +02005541 if (xmlStrEqual(name, ctxt->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005542 nodePop(ctxt);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00005543 htmlnamePop(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02005544 }
Owen Taylor3473f882001-02-23 17:55:21 +00005545
5546 ctxt->instate = XML_PARSER_CONTENT;
5547#ifdef DEBUG_PUSH
5548 xmlGenericError(xmlGenericErrorContext,
5549 "HPP: entering CONTENT\n");
5550#endif
5551 break;
5552 }
5553
5554 /*
5555 * Check for an Empty Element from DTD definition
5556 */
5557 if ((info != NULL) && (info->empty)) {
5558 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5559 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00005560 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005561 }
5562 ctxt->instate = XML_PARSER_CONTENT;
5563#ifdef DEBUG_PUSH
5564 xmlGenericError(xmlGenericErrorContext,
5565 "HPP: entering CONTENT\n");
5566#endif
5567 break;
5568 }
5569 case XML_PARSER_CONTENT: {
5570 long cons;
5571 /*
5572 * Handle preparsed entities and charRef
5573 */
5574 if (ctxt->token != 0) {
5575 xmlChar chr[2] = { 0 , 0 } ;
5576
5577 chr[0] = (xmlChar) ctxt->token;
5578 htmlCheckParagraph(ctxt);
5579 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
5580 ctxt->sax->characters(ctxt->userData, chr, 1);
5581 ctxt->token = 0;
5582 ctxt->checkIndex = 0;
5583 }
5584 if ((avail == 1) && (terminate)) {
5585 cur = in->cur[0];
5586 if ((cur != '<') && (cur != '&')) {
5587 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00005588 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005589 if (ctxt->sax->ignorableWhitespace != NULL)
5590 ctxt->sax->ignorableWhitespace(
5591 ctxt->userData, &cur, 1);
5592 } else {
5593 htmlCheckParagraph(ctxt);
5594 if (ctxt->sax->characters != NULL)
5595 ctxt->sax->characters(
5596 ctxt->userData, &cur, 1);
5597 }
5598 }
5599 ctxt->token = 0;
5600 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00005601 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00005602 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005603 }
Owen Taylor3473f882001-02-23 17:55:21 +00005604 }
5605 if (avail < 2)
5606 goto done;
5607 cur = in->cur[0];
5608 next = in->cur[1];
5609 cons = ctxt->nbChars;
5610 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
5611 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
5612 /*
5613 * Handle SCRIPT/STYLE separately
5614 */
Daniel Veillard68716a72006-10-16 09:32:17 +00005615 if (!terminate) {
5616 int idx;
5617 xmlChar val;
5618
Jiri Netolicky446e1262009-08-07 17:05:36 +02005619 idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0, 1);
Daniel Veillard68716a72006-10-16 09:32:17 +00005620 if (idx < 0)
5621 goto done;
5622 val = in->cur[idx + 2];
5623 if (val == 0) /* bad cut of input */
5624 goto done;
5625 }
Owen Taylor3473f882001-02-23 17:55:21 +00005626 htmlParseScript(ctxt);
5627 if ((cur == '<') && (next == '/')) {
5628 ctxt->instate = XML_PARSER_END_TAG;
5629 ctxt->checkIndex = 0;
5630#ifdef DEBUG_PUSH
5631 xmlGenericError(xmlGenericErrorContext,
5632 "HPP: entering END_TAG\n");
5633#endif
5634 break;
5635 }
5636 } else {
5637 /*
5638 * Sometimes DOCTYPE arrives in the middle of the document
5639 */
5640 if ((cur == '<') && (next == '!') &&
5641 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5642 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5643 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5644 (UPP(8) == 'E')) {
5645 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005646 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005647 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00005648 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
5649 "Misplaced DOCTYPE declaration\n",
5650 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005651 htmlParseDocTypeDecl(ctxt);
5652 } else if ((cur == '<') && (next == '!') &&
5653 (in->cur[2] == '-') && (in->cur[3] == '-')) {
5654 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005655 (htmlParseLookupSequence(
Daniel Veillarde77db162009-08-22 11:32:38 +02005656 ctxt, '-', '-', '>', 1, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005657 goto done;
5658#ifdef DEBUG_PUSH
5659 xmlGenericError(xmlGenericErrorContext,
5660 "HPP: Parsing Comment\n");
5661#endif
5662 htmlParseComment(ctxt);
5663 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005664 } else if ((cur == '<') && (next == '?')) {
5665 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005666 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005667 goto done;
5668#ifdef DEBUG_PUSH
5669 xmlGenericError(xmlGenericErrorContext,
5670 "HPP: Parsing PI\n");
5671#endif
5672 htmlParsePI(ctxt);
5673 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +00005674 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
5675 goto done;
5676 } else if ((cur == '<') && (next == '/')) {
5677 ctxt->instate = XML_PARSER_END_TAG;
5678 ctxt->checkIndex = 0;
5679#ifdef DEBUG_PUSH
5680 xmlGenericError(xmlGenericErrorContext,
5681 "HPP: entering END_TAG\n");
5682#endif
5683 break;
5684 } else if (cur == '<') {
5685 ctxt->instate = XML_PARSER_START_TAG;
5686 ctxt->checkIndex = 0;
5687#ifdef DEBUG_PUSH
5688 xmlGenericError(xmlGenericErrorContext,
5689 "HPP: entering START_TAG\n");
5690#endif
5691 break;
5692 } else if (cur == '&') {
5693 if ((!terminate) &&
Markus Kull56a03032009-08-24 19:00:23 +02005694 (htmlParseLookupChars(ctxt,
5695 BAD_CAST "; >/", 4) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005696 goto done;
5697#ifdef DEBUG_PUSH
5698 xmlGenericError(xmlGenericErrorContext,
5699 "HPP: Parsing Reference\n");
5700#endif
5701 /* TODO: check generation of subtrees if noent !!! */
5702 htmlParseReference(ctxt);
5703 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005704 /*
5705 * check that the text sequence is complete
5706 * before handing out the data to the parser
5707 * to avoid problems with erroneous end of
5708 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00005709 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00005710 if ((!terminate) &&
Markus Kull56a03032009-08-24 19:00:23 +02005711 (htmlParseLookupChars(ctxt, BAD_CAST "<&", 2) < 0))
Daniel Veillard14f752c2003-08-09 11:44:50 +00005712 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00005713 ctxt->checkIndex = 0;
5714#ifdef DEBUG_PUSH
5715 xmlGenericError(xmlGenericErrorContext,
5716 "HPP: Parsing char data\n");
5717#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005718 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005719 }
5720 }
5721 if (cons == ctxt->nbChars) {
5722 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005723 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5724 "detected an error in element content\n",
5725 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005726 }
5727 NEXT;
5728 break;
5729 }
5730
5731 break;
5732 }
5733 case XML_PARSER_END_TAG:
5734 if (avail < 2)
5735 goto done;
5736 if ((!terminate) &&
Jiri Netolicky446e1262009-08-07 17:05:36 +02005737 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005738 goto done;
5739 htmlParseEndTag(ctxt);
5740 if (ctxt->nameNr == 0) {
5741 ctxt->instate = XML_PARSER_EPILOG;
5742 } else {
5743 ctxt->instate = XML_PARSER_CONTENT;
5744 }
5745 ctxt->checkIndex = 0;
5746#ifdef DEBUG_PUSH
5747 xmlGenericError(xmlGenericErrorContext,
5748 "HPP: entering CONTENT\n");
5749#endif
5750 break;
5751 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00005752 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5753 "HPP: internal error, state == CDATA\n",
5754 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005755 ctxt->instate = XML_PARSER_CONTENT;
5756 ctxt->checkIndex = 0;
5757#ifdef DEBUG_PUSH
5758 xmlGenericError(xmlGenericErrorContext,
5759 "HPP: entering CONTENT\n");
5760#endif
5761 break;
5762 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00005763 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5764 "HPP: internal error, state == DTD\n",
5765 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005766 ctxt->instate = XML_PARSER_CONTENT;
5767 ctxt->checkIndex = 0;
5768#ifdef DEBUG_PUSH
5769 xmlGenericError(xmlGenericErrorContext,
5770 "HPP: entering CONTENT\n");
5771#endif
5772 break;
5773 case XML_PARSER_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00005774 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5775 "HPP: internal error, state == COMMENT\n",
5776 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005777 ctxt->instate = XML_PARSER_CONTENT;
5778 ctxt->checkIndex = 0;
5779#ifdef DEBUG_PUSH
5780 xmlGenericError(xmlGenericErrorContext,
5781 "HPP: entering CONTENT\n");
5782#endif
5783 break;
5784 case XML_PARSER_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00005785 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5786 "HPP: internal error, state == PI\n",
5787 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005788 ctxt->instate = XML_PARSER_CONTENT;
5789 ctxt->checkIndex = 0;
5790#ifdef DEBUG_PUSH
5791 xmlGenericError(xmlGenericErrorContext,
5792 "HPP: entering CONTENT\n");
5793#endif
5794 break;
5795 case XML_PARSER_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005796 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5797 "HPP: internal error, state == ENTITY_DECL\n",
5798 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005799 ctxt->instate = XML_PARSER_CONTENT;
5800 ctxt->checkIndex = 0;
5801#ifdef DEBUG_PUSH
5802 xmlGenericError(xmlGenericErrorContext,
5803 "HPP: entering CONTENT\n");
5804#endif
5805 break;
5806 case XML_PARSER_ENTITY_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005807 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5808 "HPP: internal error, state == ENTITY_VALUE\n",
5809 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005810 ctxt->instate = XML_PARSER_CONTENT;
5811 ctxt->checkIndex = 0;
5812#ifdef DEBUG_PUSH
5813 xmlGenericError(xmlGenericErrorContext,
5814 "HPP: entering DTD\n");
5815#endif
5816 break;
5817 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005818 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5819 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
5820 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005821 ctxt->instate = XML_PARSER_START_TAG;
5822 ctxt->checkIndex = 0;
5823#ifdef DEBUG_PUSH
5824 xmlGenericError(xmlGenericErrorContext,
5825 "HPP: entering START_TAG\n");
5826#endif
5827 break;
5828 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005829 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5830 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
5831 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005832 ctxt->instate = XML_PARSER_CONTENT;
5833 ctxt->checkIndex = 0;
5834#ifdef DEBUG_PUSH
5835 xmlGenericError(xmlGenericErrorContext,
5836 "HPP: entering CONTENT\n");
5837#endif
5838 break;
5839 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005840 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5841 "HPP: internal error, state == XML_PARSER_IGNORE\n",
5842 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005843 ctxt->instate = XML_PARSER_CONTENT;
5844 ctxt->checkIndex = 0;
5845#ifdef DEBUG_PUSH
5846 xmlGenericError(xmlGenericErrorContext,
5847 "HPP: entering CONTENT\n");
5848#endif
5849 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005850 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005851 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5852 "HPP: internal error, state == XML_PARSER_LITERAL\n",
5853 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005854 ctxt->instate = XML_PARSER_CONTENT;
5855 ctxt->checkIndex = 0;
5856#ifdef DEBUG_PUSH
5857 xmlGenericError(xmlGenericErrorContext,
5858 "HPP: entering CONTENT\n");
5859#endif
5860 break;
5861
Owen Taylor3473f882001-02-23 17:55:21 +00005862 }
5863 }
Daniel Veillarde77db162009-08-22 11:32:38 +02005864done:
Owen Taylor3473f882001-02-23 17:55:21 +00005865 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005866 htmlAutoCloseOnEnd(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02005867 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005868 /*
5869 * SAX: end of the document processing.
5870 */
5871 ctxt->instate = XML_PARSER_EOF;
5872 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5873 ctxt->sax->endDocument(ctxt->userData);
5874 }
5875 }
5876 if ((ctxt->myDoc != NULL) &&
5877 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5878 (ctxt->instate == XML_PARSER_EPILOG))) {
5879 xmlDtdPtr dtd;
5880 dtd = xmlGetIntSubset(ctxt->myDoc);
5881 if (dtd == NULL)
Daniel Veillarde77db162009-08-22 11:32:38 +02005882 ctxt->myDoc->intSubset =
5883 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00005884 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5885 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5886 }
5887#ifdef DEBUG_PUSH
5888 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5889#endif
5890 return(ret);
5891}
5892
5893/**
Owen Taylor3473f882001-02-23 17:55:21 +00005894 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00005895 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00005896 * @chunk: an char array
5897 * @size: the size in byte of the chunk
5898 * @terminate: last chunk indicator
5899 *
5900 * Parse a Chunk of memory
5901 *
5902 * Returns zero if no error, the xmlParserErrors otherwise.
5903 */
5904int
5905htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
5906 int terminate) {
Daniel Veillarda03e3652004-11-02 18:45:30 +00005907 if ((ctxt == NULL) || (ctxt->input == NULL)) {
5908 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5909 "htmlParseChunk: context error\n", NULL, NULL);
5910 return(XML_ERR_INTERNAL_ERROR);
5911 }
Owen Taylor3473f882001-02-23 17:55:21 +00005912 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5913 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
5914 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5915 int cur = ctxt->input->cur - ctxt->input->base;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005916 int res;
Daniel Veillarde77db162009-08-22 11:32:38 +02005917
5918 res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardd2755a82005-08-07 23:42:39 +00005919 if (res < 0) {
5920 ctxt->errNo = XML_PARSER_EOF;
5921 ctxt->disableSAX = 1;
5922 return (XML_PARSER_EOF);
5923 }
Owen Taylor3473f882001-02-23 17:55:21 +00005924 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5925 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005926 ctxt->input->end =
5927 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005928#ifdef DEBUG_PUSH
5929 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5930#endif
5931
Daniel Veillard14f752c2003-08-09 11:44:50 +00005932#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00005933 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5934 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005935#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005936 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005937 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
5938 xmlParserInputBufferPtr in = ctxt->input->buf;
5939 if ((in->encoder != NULL) && (in->buffer != NULL) &&
5940 (in->raw != NULL)) {
5941 int nbchars;
Daniel Veillarde77db162009-08-22 11:32:38 +02005942
Daniel Veillard14f752c2003-08-09 11:44:50 +00005943 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
5944 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005945 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
5946 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005947 return(XML_ERR_INVALID_ENCODING);
5948 }
5949 }
5950 }
Owen Taylor3473f882001-02-23 17:55:21 +00005951 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00005952 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00005953 if (terminate) {
5954 if ((ctxt->instate != XML_PARSER_EOF) &&
5955 (ctxt->instate != XML_PARSER_EPILOG) &&
5956 (ctxt->instate != XML_PARSER_MISC)) {
5957 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005958 ctxt->wellFormed = 0;
Daniel Veillarde77db162009-08-22 11:32:38 +02005959 }
Owen Taylor3473f882001-02-23 17:55:21 +00005960 if (ctxt->instate != XML_PARSER_EOF) {
5961 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5962 ctxt->sax->endDocument(ctxt->userData);
5963 }
5964 ctxt->instate = XML_PARSER_EOF;
5965 }
Daniel Veillarde77db162009-08-22 11:32:38 +02005966 return((xmlParserErrors) ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +00005967}
5968
5969/************************************************************************
5970 * *
5971 * User entry points *
5972 * *
5973 ************************************************************************/
5974
5975/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005976 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005977 * @sax: a SAX handler
5978 * @user_data: The user data returned on SAX callbacks
5979 * @chunk: a pointer to an array of chars
5980 * @size: number of chars in the array
5981 * @filename: an optional file name or URI
5982 * @enc: an optional encoding
5983 *
5984 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005985 * The value of @filename is used for fetching external entities
5986 * and error/warning reports.
5987 *
5988 * Returns the new parser context or NULL
5989 */
5990htmlParserCtxtPtr
Daniel Veillarde77db162009-08-22 11:32:38 +02005991htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
Owen Taylor3473f882001-02-23 17:55:21 +00005992 const char *chunk, int size, const char *filename,
5993 xmlCharEncoding enc) {
5994 htmlParserCtxtPtr ctxt;
5995 htmlParserInputPtr inputStream;
5996 xmlParserInputBufferPtr buf;
5997
Daniel Veillardd0463562001-10-13 09:15:48 +00005998 xmlInitParser();
5999
Owen Taylor3473f882001-02-23 17:55:21 +00006000 buf = xmlAllocParserInputBuffer(enc);
6001 if (buf == NULL) return(NULL);
6002
Daniel Veillardf403d292003-10-05 13:51:35 +00006003 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00006004 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006005 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00006006 return(NULL);
6007 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00006008 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
6009 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00006010 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00006011 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00006012 xmlFree(ctxt->sax);
6013 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
6014 if (ctxt->sax == NULL) {
6015 xmlFree(buf);
6016 xmlFree(ctxt);
6017 return(NULL);
6018 }
6019 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
6020 if (user_data != NULL)
6021 ctxt->userData = user_data;
Daniel Veillarde77db162009-08-22 11:32:38 +02006022 }
Owen Taylor3473f882001-02-23 17:55:21 +00006023 if (filename == NULL) {
6024 ctxt->directory = NULL;
6025 } else {
6026 ctxt->directory = xmlParserGetDirectory(filename);
6027 }
6028
6029 inputStream = htmlNewInputStream(ctxt);
6030 if (inputStream == NULL) {
6031 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00006032 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00006033 return(NULL);
6034 }
6035
6036 if (filename == NULL)
6037 inputStream->filename = NULL;
6038 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00006039 inputStream->filename = (char *)
6040 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00006041 inputStream->buf = buf;
6042 inputStream->base = inputStream->buf->buffer->content;
6043 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillarde77db162009-08-22 11:32:38 +02006044 inputStream->end =
Daniel Veillard5f704af2003-03-05 10:01:43 +00006045 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00006046
6047 inputPush(ctxt, inputStream);
6048
6049 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
Daniel Veillarde77db162009-08-22 11:32:38 +02006050 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00006051 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
6052 int cur = ctxt->input->cur - ctxt->input->base;
6053
Daniel Veillarde77db162009-08-22 11:32:38 +02006054 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00006055
6056 ctxt->input->base = ctxt->input->buf->buffer->content + base;
6057 ctxt->input->cur = ctxt->input->base + cur;
6058 ctxt->input->end =
6059 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00006060#ifdef DEBUG_PUSH
6061 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
6062#endif
6063 }
Daniel Veillard68716a72006-10-16 09:32:17 +00006064 ctxt->progressive = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006065
6066 return(ctxt);
6067}
William M. Brack21e4ef22005-01-02 09:53:13 +00006068#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00006069
6070/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006071 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00006072 * @cur: a pointer to an array of xmlChar
6073 * @encoding: a free form C string describing the HTML document encoding, or NULL
6074 * @sax: the SAX handler block
Daniel Veillarde77db162009-08-22 11:32:38 +02006075 * @userData: if using SAX, this pointer will be provided on callbacks.
Owen Taylor3473f882001-02-23 17:55:21 +00006076 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00006077 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
6078 * to handle parse events. If sax is NULL, fallback to the default DOM
6079 * behavior and return a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006080 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00006081 * Returns the resulting document tree unless SAX is NULL or the document is
6082 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00006083 */
6084
6085htmlDocPtr
6086htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
6087 htmlDocPtr ret;
6088 htmlParserCtxtPtr ctxt;
6089
Daniel Veillardd0463562001-10-13 09:15:48 +00006090 xmlInitParser();
6091
Owen Taylor3473f882001-02-23 17:55:21 +00006092 if (cur == NULL) return(NULL);
6093
6094
6095 ctxt = htmlCreateDocParserCtxt(cur, encoding);
6096 if (ctxt == NULL) return(NULL);
Daniel Veillarde77db162009-08-22 11:32:38 +02006097 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00006098 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00006099 ctxt->sax = sax;
6100 ctxt->userData = userData;
6101 }
6102
6103 htmlParseDocument(ctxt);
6104 ret = ctxt->myDoc;
6105 if (sax != NULL) {
6106 ctxt->sax = NULL;
6107 ctxt->userData = NULL;
6108 }
6109 htmlFreeParserCtxt(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02006110
Owen Taylor3473f882001-02-23 17:55:21 +00006111 return(ret);
6112}
6113
6114/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006115 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00006116 * @cur: a pointer to an array of xmlChar
6117 * @encoding: a free form C string describing the HTML document encoding, or NULL
6118 *
6119 * parse an HTML in-memory document and build a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006120 *
Owen Taylor3473f882001-02-23 17:55:21 +00006121 * Returns the resulting document tree
6122 */
6123
6124htmlDocPtr
6125htmlParseDoc(xmlChar *cur, const char *encoding) {
6126 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
6127}
6128
6129
6130/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006131 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00006132 * @filename: the filename
6133 * @encoding: a free form C string describing the HTML document encoding, or NULL
6134 *
Daniel Veillarde77db162009-08-22 11:32:38 +02006135 * Create a parser context for a file content.
Owen Taylor3473f882001-02-23 17:55:21 +00006136 * Automatic support for ZLIB/Compress compressed document is provided
6137 * by default if found at compile-time.
6138 *
6139 * Returns the new parser context or NULL
6140 */
6141htmlParserCtxtPtr
6142htmlCreateFileParserCtxt(const char *filename, const char *encoding)
6143{
6144 htmlParserCtxtPtr ctxt;
6145 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00006146 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00006147 /* htmlCharEncoding enc; */
6148 xmlChar *content, *content_line = (xmlChar *) "charset=";
6149
Daniel Veillarda03e3652004-11-02 18:45:30 +00006150 if (filename == NULL)
6151 return(NULL);
6152
Daniel Veillardf403d292003-10-05 13:51:35 +00006153 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00006154 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006155 return(NULL);
6156 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00006157 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
6158 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00006159#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00006160 if (xmlDefaultSAXHandler.error != NULL) {
6161 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
6162 }
Daniel Veillard87247e82004-01-13 20:42:02 +00006163#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00006164 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006165 return(NULL);
6166 }
Daniel Veillarde77db162009-08-22 11:32:38 +02006167
Daniel Veillarde8b09e42003-05-13 22:14:13 +00006168 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
6169 xmlFree(canonicFilename);
6170 if (inputStream == NULL) {
6171 xmlFreeParserCtxt(ctxt);
6172 return(NULL);
6173 }
Owen Taylor3473f882001-02-23 17:55:21 +00006174
6175 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00006176
Owen Taylor3473f882001-02-23 17:55:21 +00006177 /* set encoding */
6178 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006179 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Daniel Veillarde77db162009-08-22 11:32:38 +02006180 if (content) {
Owen Taylor3473f882001-02-23 17:55:21 +00006181 strcpy ((char *)content, (char *)content_line);
6182 strcat ((char *)content, (char *)encoding);
6183 htmlCheckEncoding (ctxt, content);
6184 xmlFree (content);
6185 }
6186 }
Daniel Veillarde77db162009-08-22 11:32:38 +02006187
Owen Taylor3473f882001-02-23 17:55:21 +00006188 return(ctxt);
6189}
6190
6191/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006192 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006193 * @filename: the filename
6194 * @encoding: a free form C string describing the HTML document encoding, or NULL
6195 * @sax: the SAX handler block
Daniel Veillarde77db162009-08-22 11:32:38 +02006196 * @userData: if using SAX, this pointer will be provided on callbacks.
Owen Taylor3473f882001-02-23 17:55:21 +00006197 *
6198 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6199 * compressed document is provided by default if found at compile-time.
6200 * It use the given SAX function block to handle the parsing callback.
6201 * If sax is NULL, fallback to the default DOM tree building routines.
6202 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00006203 * Returns the resulting document tree unless SAX is NULL or the document is
6204 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00006205 */
6206
6207htmlDocPtr
Daniel Veillarde77db162009-08-22 11:32:38 +02006208htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
Owen Taylor3473f882001-02-23 17:55:21 +00006209 void *userData) {
6210 htmlDocPtr ret;
6211 htmlParserCtxtPtr ctxt;
6212 htmlSAXHandlerPtr oldsax = NULL;
6213
Daniel Veillardd0463562001-10-13 09:15:48 +00006214 xmlInitParser();
6215
Owen Taylor3473f882001-02-23 17:55:21 +00006216 ctxt = htmlCreateFileParserCtxt(filename, encoding);
6217 if (ctxt == NULL) return(NULL);
6218 if (sax != NULL) {
6219 oldsax = ctxt->sax;
6220 ctxt->sax = sax;
6221 ctxt->userData = userData;
6222 }
6223
6224 htmlParseDocument(ctxt);
6225
6226 ret = ctxt->myDoc;
6227 if (sax != NULL) {
6228 ctxt->sax = oldsax;
6229 ctxt->userData = NULL;
6230 }
6231 htmlFreeParserCtxt(ctxt);
Daniel Veillarde77db162009-08-22 11:32:38 +02006232
Owen Taylor3473f882001-02-23 17:55:21 +00006233 return(ret);
6234}
6235
6236/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00006237 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00006238 * @filename: the filename
6239 * @encoding: a free form C string describing the HTML document encoding, or NULL
6240 *
6241 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6242 * compressed document is provided by default if found at compile-time.
6243 *
6244 * Returns the resulting document tree
6245 */
6246
6247htmlDocPtr
6248htmlParseFile(const char *filename, const char *encoding) {
6249 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
6250}
6251
6252/**
6253 * htmlHandleOmittedElem:
Daniel Veillarde77db162009-08-22 11:32:38 +02006254 * @val: int 0 or 1
Owen Taylor3473f882001-02-23 17:55:21 +00006255 *
6256 * Set and return the previous value for handling HTML omitted tags.
6257 *
6258 * Returns the last value for 0 for no handling, 1 for auto insertion.
6259 */
6260
6261int
6262htmlHandleOmittedElem(int val) {
6263 int old = htmlOmittedDefaultValue;
6264
6265 htmlOmittedDefaultValue = val;
6266 return(old);
6267}
6268
Daniel Veillard930dfb62003-02-05 10:17:38 +00006269/**
6270 * htmlElementAllowedHere:
6271 * @parent: HTML parent element
6272 * @elt: HTML element
6273 *
6274 * Checks whether an HTML element may be a direct child of a parent element.
6275 * Note - doesn't check for deprecated elements
6276 *
6277 * Returns 1 if allowed; 0 otherwise.
6278 */
6279int
6280htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
6281 const char** p ;
6282
6283 if ( ! elt || ! parent || ! parent->subelts )
6284 return 0 ;
6285
6286 for ( p = parent->subelts; *p; ++p )
6287 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
6288 return 1 ;
6289
6290 return 0 ;
6291}
6292/**
6293 * htmlElementStatusHere:
6294 * @parent: HTML parent element
6295 * @elt: HTML element
6296 *
6297 * Checks whether an HTML element may be a direct child of a parent element.
6298 * and if so whether it is valid or deprecated.
6299 *
6300 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6301 */
6302htmlStatus
6303htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
6304 if ( ! parent || ! elt )
6305 return HTML_INVALID ;
6306 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
6307 return HTML_INVALID ;
6308
6309 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
6310}
6311/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006312 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00006313 * @elt: HTML element
6314 * @attr: HTML attribute
6315 * @legacy: whether to allow deprecated attributes
6316 *
6317 * Checks whether an attribute is valid for an element
6318 * Has full knowledge of Required and Deprecated attributes
6319 *
6320 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6321 */
6322htmlStatus
6323htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
6324 const char** p ;
6325
6326 if ( !elt || ! attr )
6327 return HTML_INVALID ;
6328
6329 if ( elt->attrs_req )
6330 for ( p = elt->attrs_req; *p; ++p)
6331 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6332 return HTML_REQUIRED ;
6333
6334 if ( elt->attrs_opt )
6335 for ( p = elt->attrs_opt; *p; ++p)
6336 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6337 return HTML_VALID ;
6338
6339 if ( legacy && elt->attrs_depr )
6340 for ( p = elt->attrs_depr; *p; ++p)
6341 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6342 return HTML_DEPRECATED ;
6343
6344 return HTML_INVALID ;
6345}
6346/**
Daniel Veillard71531f32003-02-05 13:19:53 +00006347 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006348 * @node: an htmlNodePtr in a tree
6349 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00006350 * for Element nodes)
6351 *
6352 * Checks whether the tree node is valid. Experimental (the author
6353 * only uses the HTML enhancements in a SAX parser)
6354 *
6355 * Return: for Element nodes, a return from htmlElementAllowedHere (if
6356 * legacy allowed) or htmlElementStatusHere (otherwise).
6357 * for Attribute nodes, a return from htmlAttrAllowed
6358 * for other nodes, HTML_NA (no checks performed)
6359 */
6360htmlStatus
6361htmlNodeStatus(const htmlNodePtr node, int legacy) {
6362 if ( ! node )
6363 return HTML_INVALID ;
6364
6365 switch ( node->type ) {
6366 case XML_ELEMENT_NODE:
6367 return legacy
6368 ? ( htmlElementAllowedHere (
6369 htmlTagLookup(node->parent->name) , node->name
6370 ) ? HTML_VALID : HTML_INVALID )
6371 : htmlElementStatusHere(
6372 htmlTagLookup(node->parent->name) ,
6373 htmlTagLookup(node->name) )
6374 ;
6375 case XML_ATTRIBUTE_NODE:
6376 return htmlAttrAllowed(
6377 htmlTagLookup(node->parent->name) , node->name, legacy) ;
6378 default: return HTML_NA ;
6379 }
6380}
Daniel Veillard9475a352003-09-26 12:47:50 +00006381/************************************************************************
6382 * *
6383 * New set (2.6.0) of simpler and more flexible APIs *
6384 * *
6385 ************************************************************************/
6386/**
6387 * DICT_FREE:
6388 * @str: a string
6389 *
6390 * Free a string if it is not owned by the "dict" dictionnary in the
6391 * current scope
6392 */
6393#define DICT_FREE(str) \
Daniel Veillarde77db162009-08-22 11:32:38 +02006394 if ((str) && ((!dict) || \
Daniel Veillard9475a352003-09-26 12:47:50 +00006395 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
6396 xmlFree((char *)(str));
6397
6398/**
6399 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00006400 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00006401 *
6402 * Reset a parser context
6403 */
6404void
6405htmlCtxtReset(htmlParserCtxtPtr ctxt)
6406{
6407 xmlParserInputPtr input;
Daniel Veillarda03e3652004-11-02 18:45:30 +00006408 xmlDictPtr dict;
Daniel Veillarde77db162009-08-22 11:32:38 +02006409
Daniel Veillarda03e3652004-11-02 18:45:30 +00006410 if (ctxt == NULL)
6411 return;
6412
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006413 xmlInitParser();
Daniel Veillarda03e3652004-11-02 18:45:30 +00006414 dict = ctxt->dict;
Daniel Veillard9475a352003-09-26 12:47:50 +00006415
6416 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
6417 xmlFreeInputStream(input);
6418 }
6419 ctxt->inputNr = 0;
6420 ctxt->input = NULL;
6421
6422 ctxt->spaceNr = 0;
Daniel Veillarda521d282004-11-09 14:59:59 +00006423 if (ctxt->spaceTab != NULL) {
6424 ctxt->spaceTab[0] = -1;
6425 ctxt->space = &ctxt->spaceTab[0];
6426 } else {
6427 ctxt->space = NULL;
6428 }
Daniel Veillard9475a352003-09-26 12:47:50 +00006429
6430
6431 ctxt->nodeNr = 0;
6432 ctxt->node = NULL;
6433
6434 ctxt->nameNr = 0;
6435 ctxt->name = NULL;
6436
6437 DICT_FREE(ctxt->version);
6438 ctxt->version = NULL;
6439 DICT_FREE(ctxt->encoding);
6440 ctxt->encoding = NULL;
6441 DICT_FREE(ctxt->directory);
6442 ctxt->directory = NULL;
6443 DICT_FREE(ctxt->extSubURI);
6444 ctxt->extSubURI = NULL;
6445 DICT_FREE(ctxt->extSubSystem);
6446 ctxt->extSubSystem = NULL;
6447 if (ctxt->myDoc != NULL)
6448 xmlFreeDoc(ctxt->myDoc);
6449 ctxt->myDoc = NULL;
6450
6451 ctxt->standalone = -1;
6452 ctxt->hasExternalSubset = 0;
6453 ctxt->hasPErefs = 0;
6454 ctxt->html = 1;
6455 ctxt->external = 0;
6456 ctxt->instate = XML_PARSER_START;
6457 ctxt->token = 0;
6458
6459 ctxt->wellFormed = 1;
6460 ctxt->nsWellFormed = 1;
6461 ctxt->valid = 1;
6462 ctxt->vctxt.userData = ctxt;
6463 ctxt->vctxt.error = xmlParserValidityError;
6464 ctxt->vctxt.warning = xmlParserValidityWarning;
6465 ctxt->record_info = 0;
6466 ctxt->nbChars = 0;
6467 ctxt->checkIndex = 0;
6468 ctxt->inSubset = 0;
6469 ctxt->errNo = XML_ERR_OK;
6470 ctxt->depth = 0;
Daniel Veillard772869f2006-11-08 09:16:56 +00006471 ctxt->charset = XML_CHAR_ENCODING_NONE;
Daniel Veillard9475a352003-09-26 12:47:50 +00006472 ctxt->catalogs = NULL;
6473 xmlInitNodeInfoSeq(&ctxt->node_seq);
6474
6475 if (ctxt->attsDefault != NULL) {
6476 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
6477 ctxt->attsDefault = NULL;
6478 }
6479 if (ctxt->attsSpecial != NULL) {
6480 xmlHashFree(ctxt->attsSpecial, NULL);
6481 ctxt->attsSpecial = NULL;
6482 }
6483}
6484
6485/**
6486 * htmlCtxtUseOptions:
6487 * @ctxt: an HTML parser context
6488 * @options: a combination of htmlParserOption(s)
6489 *
6490 * Applies the options to the parser context
6491 *
6492 * Returns 0 in case of success, the set of unknown or unimplemented options
6493 * in case of error.
6494 */
6495int
6496htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
6497{
Daniel Veillarda03e3652004-11-02 18:45:30 +00006498 if (ctxt == NULL)
6499 return(-1);
6500
Daniel Veillard9475a352003-09-26 12:47:50 +00006501 if (options & HTML_PARSE_NOWARNING) {
6502 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006503 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00006504 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006505 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00006506 }
6507 if (options & HTML_PARSE_NOERROR) {
6508 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006509 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00006510 ctxt->sax->fatalError = NULL;
6511 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006512 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00006513 }
6514 if (options & HTML_PARSE_PEDANTIC) {
6515 ctxt->pedantic = 1;
6516 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006517 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00006518 } else
6519 ctxt->pedantic = 0;
6520 if (options & XML_PARSE_NOBLANKS) {
6521 ctxt->keepBlanks = 0;
6522 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
6523 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00006524 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00006525 } else
6526 ctxt->keepBlanks = 1;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00006527 if (options & HTML_PARSE_RECOVER) {
6528 ctxt->recovery = 1;
Daniel Veillardaf616a72006-10-17 20:18:39 +00006529 options -= HTML_PARSE_RECOVER;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00006530 } else
6531 ctxt->recovery = 0;
Daniel Veillard8874b942005-08-25 13:19:21 +00006532 if (options & HTML_PARSE_COMPACT) {
6533 ctxt->options |= HTML_PARSE_COMPACT;
6534 options -= HTML_PARSE_COMPACT;
6535 }
Daniel Veillarde77db162009-08-22 11:32:38 +02006536 if (options & XML_PARSE_HUGE) {
6537 ctxt->options |= XML_PARSE_HUGE;
6538 options -= XML_PARSE_HUGE;
6539 }
Daniel Veillard9475a352003-09-26 12:47:50 +00006540 ctxt->dictNames = 0;
6541 return (options);
6542}
6543
6544/**
6545 * htmlDoRead:
6546 * @ctxt: an HTML parser context
6547 * @URL: the base URL to use for the document
6548 * @encoding: the document encoding, or NULL
6549 * @options: a combination of htmlParserOption(s)
6550 * @reuse: keep the context for reuse
6551 *
6552 * Common front-end for the htmlRead functions
Daniel Veillarde77db162009-08-22 11:32:38 +02006553 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006554 * Returns the resulting document tree or NULL
6555 */
6556static htmlDocPtr
6557htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
6558 int options, int reuse)
6559{
6560 htmlDocPtr ret;
Daniel Veillarde77db162009-08-22 11:32:38 +02006561
Daniel Veillard9475a352003-09-26 12:47:50 +00006562 htmlCtxtUseOptions(ctxt, options);
6563 ctxt->html = 1;
6564 if (encoding != NULL) {
6565 xmlCharEncodingHandlerPtr hdlr;
6566
6567 hdlr = xmlFindCharEncodingHandler(encoding);
Daniel Veillard4cc67bb2008-08-29 19:58:23 +00006568 if (hdlr != NULL) {
Daniel Veillard9475a352003-09-26 12:47:50 +00006569 xmlSwitchToEncoding(ctxt, hdlr);
Daniel Veillard4cc67bb2008-08-29 19:58:23 +00006570 if (ctxt->input->encoding != NULL)
6571 xmlFree((xmlChar *) ctxt->input->encoding);
6572 ctxt->input->encoding = xmlStrdup((xmlChar *)encoding);
6573 }
Daniel Veillard9475a352003-09-26 12:47:50 +00006574 }
6575 if ((URL != NULL) && (ctxt->input != NULL) &&
6576 (ctxt->input->filename == NULL))
6577 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
6578 htmlParseDocument(ctxt);
6579 ret = ctxt->myDoc;
6580 ctxt->myDoc = NULL;
6581 if (!reuse) {
6582 if ((ctxt->dictNames) &&
6583 (ret != NULL) &&
6584 (ret->dict == ctxt->dict))
6585 ctxt->dict = NULL;
6586 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006587 }
6588 return (ret);
6589}
6590
6591/**
6592 * htmlReadDoc:
6593 * @cur: a pointer to a zero terminated string
6594 * @URL: the base URL to use for the document
6595 * @encoding: the document encoding, or NULL
6596 * @options: a combination of htmlParserOption(s)
6597 *
6598 * parse an XML in-memory document and build a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006599 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006600 * Returns the resulting document tree
6601 */
6602htmlDocPtr
6603htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
6604{
6605 htmlParserCtxtPtr ctxt;
6606
6607 if (cur == NULL)
6608 return (NULL);
6609
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006610 xmlInitParser();
Daniel Veillard8a82ae12006-10-17 20:04:10 +00006611 ctxt = htmlCreateDocParserCtxt(cur, NULL);
Daniel Veillard9475a352003-09-26 12:47:50 +00006612 if (ctxt == NULL)
6613 return (NULL);
6614 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6615}
6616
6617/**
6618 * htmlReadFile:
6619 * @filename: a file or URL
6620 * @encoding: the document encoding, or NULL
6621 * @options: a combination of htmlParserOption(s)
6622 *
6623 * parse an XML file from the filesystem or the network.
Daniel Veillarde77db162009-08-22 11:32:38 +02006624 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006625 * Returns the resulting document tree
6626 */
6627htmlDocPtr
6628htmlReadFile(const char *filename, const char *encoding, int options)
6629{
6630 htmlParserCtxtPtr ctxt;
6631
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006632 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006633 ctxt = htmlCreateFileParserCtxt(filename, encoding);
6634 if (ctxt == NULL)
6635 return (NULL);
6636 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
6637}
6638
6639/**
6640 * htmlReadMemory:
6641 * @buffer: a pointer to a char array
6642 * @size: the size of the array
6643 * @URL: the base URL to use for the document
6644 * @encoding: the document encoding, or NULL
6645 * @options: a combination of htmlParserOption(s)
6646 *
6647 * parse an XML in-memory document and build a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006648 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006649 * Returns the resulting document tree
6650 */
6651htmlDocPtr
6652htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
6653{
6654 htmlParserCtxtPtr ctxt;
6655
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006656 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006657 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
6658 if (ctxt == NULL)
6659 return (NULL);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006660 htmlDefaultSAXHandlerInit();
William M. Brackd43cdcd2004-08-03 15:13:29 +00006661 if (ctxt->sax != NULL)
6662 memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Daniel Veillard9475a352003-09-26 12:47:50 +00006663 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6664}
6665
6666/**
6667 * htmlReadFd:
6668 * @fd: an open file descriptor
6669 * @URL: the base URL to use for the document
6670 * @encoding: the document encoding, or NULL
6671 * @options: a combination of htmlParserOption(s)
6672 *
6673 * parse an XML from a file descriptor and build a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006674 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006675 * Returns the resulting document tree
6676 */
6677htmlDocPtr
6678htmlReadFd(int fd, const char *URL, const char *encoding, int options)
6679{
6680 htmlParserCtxtPtr ctxt;
6681 xmlParserInputBufferPtr input;
6682 xmlParserInputPtr stream;
6683
6684 if (fd < 0)
6685 return (NULL);
6686
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006687 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006688 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6689 if (input == NULL)
6690 return (NULL);
6691 ctxt = xmlNewParserCtxt();
6692 if (ctxt == NULL) {
6693 xmlFreeParserInputBuffer(input);
6694 return (NULL);
6695 }
6696 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6697 if (stream == NULL) {
6698 xmlFreeParserInputBuffer(input);
6699 xmlFreeParserCtxt(ctxt);
6700 return (NULL);
6701 }
6702 inputPush(ctxt, stream);
6703 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6704}
6705
6706/**
6707 * htmlReadIO:
6708 * @ioread: an I/O read function
6709 * @ioclose: an I/O close function
6710 * @ioctx: an I/O handler
6711 * @URL: the base URL to use for the document
6712 * @encoding: the document encoding, or NULL
6713 * @options: a combination of htmlParserOption(s)
6714 *
6715 * parse an HTML document from I/O functions and source and build a tree.
Daniel Veillarde77db162009-08-22 11:32:38 +02006716 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006717 * Returns the resulting document tree
6718 */
6719htmlDocPtr
6720htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
6721 void *ioctx, const char *URL, const char *encoding, int options)
6722{
6723 htmlParserCtxtPtr ctxt;
6724 xmlParserInputBufferPtr input;
6725 xmlParserInputPtr stream;
6726
6727 if (ioread == NULL)
6728 return (NULL);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006729 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006730
6731 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6732 XML_CHAR_ENCODING_NONE);
6733 if (input == NULL)
6734 return (NULL);
Daniel Veillard8a82ae12006-10-17 20:04:10 +00006735 ctxt = htmlNewParserCtxt();
Daniel Veillard9475a352003-09-26 12:47:50 +00006736 if (ctxt == NULL) {
6737 xmlFreeParserInputBuffer(input);
6738 return (NULL);
6739 }
6740 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6741 if (stream == NULL) {
6742 xmlFreeParserInputBuffer(input);
6743 xmlFreeParserCtxt(ctxt);
6744 return (NULL);
6745 }
6746 inputPush(ctxt, stream);
6747 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6748}
6749
6750/**
6751 * htmlCtxtReadDoc:
6752 * @ctxt: an HTML parser context
6753 * @cur: a pointer to a zero terminated string
6754 * @URL: the base URL to use for the document
6755 * @encoding: the document encoding, or NULL
6756 * @options: a combination of htmlParserOption(s)
6757 *
6758 * parse an XML in-memory document and build a tree.
6759 * This reuses the existing @ctxt parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02006760 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006761 * Returns the resulting document tree
6762 */
6763htmlDocPtr
6764htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
6765 const char *URL, const char *encoding, int options)
6766{
6767 xmlParserInputPtr stream;
6768
6769 if (cur == NULL)
6770 return (NULL);
6771 if (ctxt == NULL)
6772 return (NULL);
6773
6774 htmlCtxtReset(ctxt);
6775
6776 stream = xmlNewStringInputStream(ctxt, cur);
6777 if (stream == NULL) {
6778 return (NULL);
6779 }
6780 inputPush(ctxt, stream);
6781 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6782}
6783
6784/**
6785 * htmlCtxtReadFile:
6786 * @ctxt: an HTML parser context
6787 * @filename: a file or URL
6788 * @encoding: the document encoding, or NULL
6789 * @options: a combination of htmlParserOption(s)
6790 *
6791 * parse an XML file from the filesystem or the network.
6792 * This reuses the existing @ctxt parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02006793 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006794 * Returns the resulting document tree
6795 */
6796htmlDocPtr
6797htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
6798 const char *encoding, int options)
6799{
6800 xmlParserInputPtr stream;
6801
6802 if (filename == NULL)
6803 return (NULL);
6804 if (ctxt == NULL)
6805 return (NULL);
6806
6807 htmlCtxtReset(ctxt);
6808
Daniel Veillard29614c72004-11-26 10:47:26 +00006809 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006810 if (stream == NULL) {
6811 return (NULL);
6812 }
6813 inputPush(ctxt, stream);
6814 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
6815}
6816
6817/**
6818 * htmlCtxtReadMemory:
6819 * @ctxt: an HTML parser context
6820 * @buffer: a pointer to a char array
6821 * @size: the size of the array
6822 * @URL: the base URL to use for the document
6823 * @encoding: the document encoding, or NULL
6824 * @options: a combination of htmlParserOption(s)
6825 *
6826 * parse an XML in-memory document and build a tree.
6827 * This reuses the existing @ctxt parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02006828 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006829 * Returns the resulting document tree
6830 */
6831htmlDocPtr
6832htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
6833 const char *URL, const char *encoding, int options)
6834{
6835 xmlParserInputBufferPtr input;
6836 xmlParserInputPtr stream;
6837
6838 if (ctxt == NULL)
6839 return (NULL);
6840 if (buffer == NULL)
6841 return (NULL);
6842
6843 htmlCtxtReset(ctxt);
6844
6845 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
6846 if (input == NULL) {
6847 return(NULL);
6848 }
6849
6850 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6851 if (stream == NULL) {
6852 xmlFreeParserInputBuffer(input);
6853 return(NULL);
6854 }
6855
6856 inputPush(ctxt, stream);
6857 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6858}
6859
6860/**
6861 * htmlCtxtReadFd:
6862 * @ctxt: an HTML parser context
6863 * @fd: an open file descriptor
6864 * @URL: the base URL to use for the document
6865 * @encoding: the document encoding, or NULL
6866 * @options: a combination of htmlParserOption(s)
6867 *
6868 * parse an XML from a file descriptor and build a tree.
6869 * This reuses the existing @ctxt parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02006870 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006871 * Returns the resulting document tree
6872 */
6873htmlDocPtr
6874htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
6875 const char *URL, const char *encoding, int options)
6876{
6877 xmlParserInputBufferPtr input;
6878 xmlParserInputPtr stream;
6879
6880 if (fd < 0)
6881 return (NULL);
6882 if (ctxt == NULL)
6883 return (NULL);
6884
6885 htmlCtxtReset(ctxt);
6886
6887
6888 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6889 if (input == NULL)
6890 return (NULL);
6891 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6892 if (stream == NULL) {
6893 xmlFreeParserInputBuffer(input);
6894 return (NULL);
6895 }
6896 inputPush(ctxt, stream);
6897 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6898}
6899
6900/**
6901 * htmlCtxtReadIO:
6902 * @ctxt: an HTML parser context
6903 * @ioread: an I/O read function
6904 * @ioclose: an I/O close function
6905 * @ioctx: an I/O handler
6906 * @URL: the base URL to use for the document
6907 * @encoding: the document encoding, or NULL
6908 * @options: a combination of htmlParserOption(s)
6909 *
6910 * parse an HTML document from I/O functions and source and build a tree.
6911 * This reuses the existing @ctxt parser context
Daniel Veillarde77db162009-08-22 11:32:38 +02006912 *
Daniel Veillard9475a352003-09-26 12:47:50 +00006913 * Returns the resulting document tree
6914 */
6915htmlDocPtr
6916htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
6917 xmlInputCloseCallback ioclose, void *ioctx,
6918 const char *URL,
6919 const char *encoding, int options)
6920{
6921 xmlParserInputBufferPtr input;
6922 xmlParserInputPtr stream;
6923
6924 if (ioread == NULL)
6925 return (NULL);
6926 if (ctxt == NULL)
6927 return (NULL);
6928
6929 htmlCtxtReset(ctxt);
6930
6931 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6932 XML_CHAR_ENCODING_NONE);
6933 if (input == NULL)
6934 return (NULL);
6935 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6936 if (stream == NULL) {
6937 xmlFreeParserInputBuffer(input);
6938 return (NULL);
6939 }
6940 inputPush(ctxt, stream);
6941 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6942}
6943
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006944#define bottom_HTMLparser
6945#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00006946#endif /* LIBXML_HTML_ENABLED */