blob: c02ef0ae43d70a44c202d86299267d1dc229ae30 [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 Veillardf403d292003-10-05 13:51:35 +000062 * Some factorized error routines *
63 * *
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 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000150 * Parser stacks related functions and macros *
151 * *
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{
166 if (ctxt->nameNr >= ctxt->nameMax) {
167 ctxt->nameMax *= 2;
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000168 ctxt->nameTab = (const xmlChar * *)
Igor Zlatkovicd37c1392003-08-28 10:34:33 +0000169 xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +0000170 ctxt->nameMax *
171 sizeof(ctxt->nameTab[0]));
172 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000173 htmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000174 return (0);
175 }
176 }
177 ctxt->nameTab[ctxt->nameNr] = value;
178 ctxt->name = value;
179 return (ctxt->nameNr++);
180}
181/**
182 * htmlnamePop:
183 * @ctxt: an HTML parser context
184 *
185 * Pops the top element name from the name stack
186 *
187 * Returns the name just removed
188 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000189static const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000190htmlnamePop(htmlParserCtxtPtr ctxt)
191{
Daniel Veillard2fdbd322003-08-18 12:15:38 +0000192 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000193
Daniel Veillard1c732d22002-11-30 11:22:59 +0000194 if (ctxt->nameNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +0000195 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000196 ctxt->nameNr--;
197 if (ctxt->nameNr < 0)
Daniel Veillard24505b02005-07-28 23:49:35 +0000198 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000199 if (ctxt->nameNr > 0)
200 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
201 else
202 ctxt->name = NULL;
203 ret = ctxt->nameTab[ctxt->nameNr];
Daniel Veillard24505b02005-07-28 23:49:35 +0000204 ctxt->nameTab[ctxt->nameNr] = NULL;
Daniel Veillard1c732d22002-11-30 11:22:59 +0000205 return (ret);
206}
Owen Taylor3473f882001-02-23 17:55:21 +0000207
208/*
209 * Macros for accessing the content. Those should be used only by the parser,
210 * and not exported.
211 *
212 * Dirty macros, i.e. one need to make assumption on the context to use them
213 *
214 * CUR_PTR return the current pointer to the xmlChar to be parsed.
215 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
216 * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
217 * in UNICODE mode. This should be used internally by the parser
218 * only to compare to ASCII values otherwise it would break when
219 * running with UTF-8 encoding.
220 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
221 * to compare on ASCII based substring.
222 * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
223 * it should be used only to compare on ASCII based substring.
224 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +0000225 * strings without newlines within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +0000226 *
227 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
228 *
229 * CURRENT Returns the current char value, with the full decoding of
230 * UTF-8 if we are using this mode. It returns an int.
231 * NEXT Skip to the next character, this does the proper decoding
232 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +0000233 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +0000234 * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
235 */
236
237#define UPPER (toupper(*ctxt->input->cur))
238
Daniel Veillard77a90a72003-03-22 00:04:05 +0000239#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val)
Owen Taylor3473f882001-02-23 17:55:21 +0000240
241#define NXT(val) ctxt->input->cur[(val)]
242
243#define UPP(val) (toupper(ctxt->input->cur[(val)]))
244
245#define CUR_PTR ctxt->input->cur
246
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000247#define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
248 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
249 xmlParserInputShrink(ctxt->input)
Owen Taylor3473f882001-02-23 17:55:21 +0000250
Daniel Veillard652f9aa2003-10-28 22:04:45 +0000251#define GROW if ((ctxt->progressive == 0) && \
252 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
253 xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
Owen Taylor3473f882001-02-23 17:55:21 +0000254
255#define CURRENT ((int) (*ctxt->input->cur))
256
257#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
258
259/* Inported from XML */
260
Daniel Veillard561b7f82002-03-20 21:55:57 +0000261/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
262#define CUR ((int) (*ctxt->input->cur))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000263#define NEXT xmlNextChar(ctxt)
Owen Taylor3473f882001-02-23 17:55:21 +0000264
Daniel Veillard561b7f82002-03-20 21:55:57 +0000265#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
Owen Taylor3473f882001-02-23 17:55:21 +0000266#define NXT(val) ctxt->input->cur[(val)]
267#define CUR_PTR ctxt->input->cur
268
269
270#define NEXTL(l) do { \
271 if (*(ctxt->input->cur) == '\n') { \
272 ctxt->input->line++; ctxt->input->col = 1; \
273 } else ctxt->input->col++; \
274 ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \
275 } while (0)
276
277/************
278 \
279 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
280 if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
281 ************/
282
283#define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
284#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
285
286#define COPY_BUF(l,b,i,v) \
287 if (l == 1) b[i++] = (xmlChar) v; \
288 else i += xmlCopyChar(l,&b[i],v)
289
290/**
291 * htmlCurrentChar:
292 * @ctxt: the HTML parser context
293 * @len: pointer to the length of the char read
294 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000295 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000296 * bytes in the input buffer. Implement the end of line normalization:
297 * 2.11 End-of-Line Handling
298 * If the encoding is unspecified, in the case we find an ISO-Latin-1
299 * char, then the encoding converter is plugged in automatically.
300 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000301 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000302 */
303
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000304static int
Owen Taylor3473f882001-02-23 17:55:21 +0000305htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
306 if (ctxt->instate == XML_PARSER_EOF)
307 return(0);
308
309 if (ctxt->token != 0) {
310 *len = 0;
311 return(ctxt->token);
312 }
313 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
314 /*
315 * We are supposed to handle UTF8, check it's valid
316 * From rfc2044: encoding of the Unicode values on UTF-8:
317 *
318 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
319 * 0000 0000-0000 007F 0xxxxxxx
320 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
321 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
322 *
323 * Check for the 0x110000 limit too
324 */
325 const unsigned char *cur = ctxt->input->cur;
326 unsigned char c;
327 unsigned int val;
328
329 c = *cur;
330 if (c & 0x80) {
331 if (cur[1] == 0)
332 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
333 if ((cur[1] & 0xc0) != 0x80)
334 goto encoding_error;
335 if ((c & 0xe0) == 0xe0) {
336
337 if (cur[2] == 0)
338 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
339 if ((cur[2] & 0xc0) != 0x80)
340 goto encoding_error;
341 if ((c & 0xf0) == 0xf0) {
342 if (cur[3] == 0)
343 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
344 if (((c & 0xf8) != 0xf0) ||
345 ((cur[3] & 0xc0) != 0x80))
346 goto encoding_error;
347 /* 4-byte code */
348 *len = 4;
349 val = (cur[0] & 0x7) << 18;
350 val |= (cur[1] & 0x3f) << 12;
351 val |= (cur[2] & 0x3f) << 6;
352 val |= cur[3] & 0x3f;
353 } else {
354 /* 3-byte code */
355 *len = 3;
356 val = (cur[0] & 0xf) << 12;
357 val |= (cur[1] & 0x3f) << 6;
358 val |= cur[2] & 0x3f;
359 }
360 } else {
361 /* 2-byte code */
362 *len = 2;
363 val = (cur[0] & 0x1f) << 6;
364 val |= cur[1] & 0x3f;
365 }
366 if (!IS_CHAR(val)) {
Daniel Veillardf403d292003-10-05 13:51:35 +0000367 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
368 "Char 0x%X out of allowed range\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000369 }
370 return(val);
371 } else {
372 /* 1-byte code */
373 *len = 1;
374 return((int) *ctxt->input->cur);
375 }
376 }
377 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000378 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000379 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000380 * XML constructs only use < 128 chars
381 */
382 *len = 1;
383 if ((int) *ctxt->input->cur < 0x80)
384 return((int) *ctxt->input->cur);
385
386 /*
387 * Humm this is bad, do an automatic flow conversion
388 */
389 xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
390 ctxt->charset = XML_CHAR_ENCODING_UTF8;
391 return(xmlCurrentChar(ctxt, len));
392
393encoding_error:
394 /*
395 * If we detect an UTF8 error that probably mean that the
396 * input encoding didn't get properly advertized in the
397 * declaration header. Report the error and switch the encoding
398 * to ISO-Latin-1 (if you don't like this policy, just declare the
399 * encoding !)
400 */
Daniel Veillarda03e3652004-11-02 18:45:30 +0000401 {
402 char buffer[150];
403
404 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Owen Taylor3473f882001-02-23 17:55:21 +0000405 ctxt->input->cur[0], ctxt->input->cur[1],
406 ctxt->input->cur[2], ctxt->input->cur[3]);
Daniel Veillarda03e3652004-11-02 18:45:30 +0000407 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
408 "Input is not proper UTF-8, indicate encoding !\n",
409 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000410 }
411
412 ctxt->charset = XML_CHAR_ENCODING_8859_1;
413 *len = 1;
414 return((int) *ctxt->input->cur);
415}
416
417/**
Owen Taylor3473f882001-02-23 17:55:21 +0000418 * htmlSkipBlankChars:
419 * @ctxt: the HTML parser context
420 *
421 * skip all blanks character found at that point in the input streams.
422 *
423 * Returns the number of space chars skipped
424 */
425
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000426static int
Owen Taylor3473f882001-02-23 17:55:21 +0000427htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
428 int res = 0;
429
William M. Brack76e95df2003-10-18 16:20:14 +0000430 while (IS_BLANK_CH(*(ctxt->input->cur))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000431 if ((*ctxt->input->cur == 0) &&
432 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
433 xmlPopInput(ctxt);
434 } else {
435 if (*(ctxt->input->cur) == '\n') {
436 ctxt->input->line++; ctxt->input->col = 1;
437 } else ctxt->input->col++;
438 ctxt->input->cur++;
439 ctxt->nbChars++;
440 if (*ctxt->input->cur == 0)
441 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
442 }
443 res++;
444 }
445 return(res);
446}
447
448
449
450/************************************************************************
451 * *
452 * The list of HTML elements and their properties *
453 * *
454 ************************************************************************/
455
456/*
457 * Start Tag: 1 means the start tag can be ommited
458 * End Tag: 1 means the end tag can be ommited
459 * 2 means it's forbidden (empty elements)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000460 * 3 means the tag is stylistic and should be closed easily
Owen Taylor3473f882001-02-23 17:55:21 +0000461 * Depr: this element is deprecated
462 * DTD: 1 means that this element is valid only in the Loose DTD
463 * 2 means that this element is valid only in the Frameset DTD
464 *
Daniel Veillard02bb1702001-06-13 21:11:59 +0000465 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
Daniel Veillard930dfb62003-02-05 10:17:38 +0000466 , subElements , impliedsubelt , Attributes, userdata
Owen Taylor3473f882001-02-23 17:55:21 +0000467 */
Daniel Veillard930dfb62003-02-05 10:17:38 +0000468
469/* Definitions and a couple of vars for HTML Elements */
470
471#define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000472#define NB_FONTSTYLE 8
Daniel Veillard930dfb62003-02-05 10:17:38 +0000473#define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000474#define NB_PHRASE 10
Daniel Veillard930dfb62003-02-05 10:17:38 +0000475#define SPECIAL "a", "img", "applet", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000476#define NB_SPECIAL 15
Daniel Veillard930dfb62003-02-05 10:17:38 +0000477#define INLINE PCDATA FONTSTYLE PHRASE SPECIAL FORMCTRL
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000478#define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL
479#define BLOCK HEADING, LIST "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
480#define NB_BLOCK NB_HEADING + NB_LIST + 14
Daniel Veillard930dfb62003-02-05 10:17:38 +0000481#define FORMCTRL "input", "select", "textarea", "label", "button"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000482#define NB_FORMCTRL 5
Daniel Veillard930dfb62003-02-05 10:17:38 +0000483#define PCDATA
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000484#define NB_PCDATA 0
Daniel Veillard930dfb62003-02-05 10:17:38 +0000485#define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000486#define NB_HEADING 6
Daniel Veillard930dfb62003-02-05 10:17:38 +0000487#define LIST "ul", "ol", "dir", "menu"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000488#define NB_LIST 4
Daniel Veillard930dfb62003-02-05 10:17:38 +0000489#define MODIFIER
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000490#define NB_MODIFIER 0
Daniel Veillard930dfb62003-02-05 10:17:38 +0000491#define FLOW BLOCK,INLINE
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000492#define NB_FLOW NB_BLOCK + NB_INLINE
Daniel Veillard930dfb62003-02-05 10:17:38 +0000493#define EMPTY NULL
494
495
496static const char* html_flow[] = { FLOW, NULL } ;
497static const char* html_inline[] = { INLINE, NULL } ;
498
499/* placeholders: elts with content but no subelements */
500static const char* html_pcdata[] = { NULL } ;
501#define html_cdata html_pcdata
502
503
504/* ... and for HTML Attributes */
505
506#define COREATTRS "id", "class", "style", "title"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000507#define NB_COREATTRS 4
Daniel Veillard930dfb62003-02-05 10:17:38 +0000508#define I18N "lang", "dir"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000509#define NB_I18N 2
Daniel Veillard930dfb62003-02-05 10:17:38 +0000510#define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000511#define NB_EVENTS 9
Daniel Veillard930dfb62003-02-05 10:17:38 +0000512#define ATTRS COREATTRS,I18N,EVENTS
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000513#define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS
Daniel Veillard930dfb62003-02-05 10:17:38 +0000514#define CELLHALIGN "align", "char", "charoff"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000515#define NB_CELLHALIGN 3
Daniel Veillard930dfb62003-02-05 10:17:38 +0000516#define CELLVALIGN "valign"
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000517#define NB_CELLVALIGN 1
Daniel Veillard930dfb62003-02-05 10:17:38 +0000518
519static const char* html_attrs[] = { ATTRS, NULL } ;
520static const char* core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
521static const char* core_attrs[] = { COREATTRS, NULL } ;
522static const char* i18n_attrs[] = { I18N, NULL } ;
523
524
525/* Other declarations that should go inline ... */
526static const char* a_attrs[] = { ATTRS, "charset", "type", "name",
527 "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
528 "tabindex", "onfocus", "onblur", NULL } ;
529static const char* target_attr[] = { "target", NULL } ;
530static const char* rows_cols_attr[] = { "rows", "cols", NULL } ;
531static const char* alt_attr[] = { "alt", NULL } ;
532static const char* src_alt_attrs[] = { "src", "alt", NULL } ;
533static const char* href_attrs[] = { "href", NULL } ;
534static const char* clear_attrs[] = { "clear", NULL } ;
535static const char* inline_p[] = { INLINE, "p", NULL } ;
536static const char* flow_param[] = { FLOW, "param", NULL } ;
537static const char* applet_attrs[] = { COREATTRS , "codebase",
538 "archive", "alt", "name", "height", "width", "align",
539 "hspace", "vspace", NULL } ;
540static const char* area_attrs[] = { "shape", "coords", "href", "nohref",
541 "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
542static const char* basefont_attrs[] =
543 { "id", "size", "color", "face", NULL } ;
544static const char* quote_attrs[] = { ATTRS, "cite", NULL } ;
545static const char* body_contents[] = { FLOW, "ins", "del", NULL } ;
546static const char* body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
547static const char* body_depr[] = { "background", "bgcolor", "text",
548 "link", "vlink", "alink", NULL } ;
549static const char* button_attrs[] = { ATTRS, "name", "value", "type",
550 "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
551
552
553static const char* col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
554static const char* col_elt[] = { "col", NULL } ;
555static const char* edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
556static const char* compact_attrs[] = { ATTRS, "compact", NULL } ;
557static const char* dl_contents[] = { "dt", "dd", NULL } ;
558static const char* compact_attr[] = { "compact", NULL } ;
559static const char* label_attr[] = { "label", NULL } ;
560static const char* fieldset_contents[] = { FLOW, "legend" } ;
561static const char* font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
562static const char* form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
563static const char* form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
564static const char* frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
565static const char* frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
566static const char* frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
567static const char* head_attrs[] = { I18N, "profile", NULL } ;
568static const char* head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
569static const char* hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
570static const char* version_attr[] = { "version", NULL } ;
571static const char* html_content[] = { "head", "body", "frameset", NULL } ;
572static const char* iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
573static const char* img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
574static const char* input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ;
575static const char* prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
576static const char* label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
577static const char* legend_attrs[] = { ATTRS, "accesskey", NULL } ;
578static const char* align_attr[] = { "align", NULL } ;
579static const char* link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
580static const char* map_contents[] = { BLOCK, "area", NULL } ;
581static const char* name_attr[] = { "name", NULL } ;
582static const char* action_attr[] = { "action", NULL } ;
583static const char* blockli_elt[] = { BLOCK, "li", NULL } ;
584static const char* meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ;
585static const char* content_attr[] = { "content", NULL } ;
586static const char* type_attr[] = { "type", NULL } ;
587static const char* noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
588static const char* object_contents[] = { FLOW, "param", NULL } ;
589static const char* object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
590static const char* object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
591static const char* ol_attrs[] = { "type", "compact", "start", NULL} ;
592static const char* option_elt[] = { "option", NULL } ;
593static const char* optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
594static const char* option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
595static const char* param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
596static const char* width_attr[] = { "width", NULL } ;
597static const char* pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
598static const char* script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
599static const char* language_attr[] = { "language", NULL } ;
600static const char* select_content[] = { "optgroup", "option", NULL } ;
601static const char* select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
602static const char* style_attrs[] = { I18N, "media", "title", NULL } ;
603static const char* table_attrs[] = { ATTRS "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
604static const char* table_depr[] = { "align", "bgcolor", NULL } ;
605static const char* table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
606static const char* tr_elt[] = { "tr", NULL } ;
607static const char* talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
608static const char* th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
609static const char* th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
610static const char* textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
611static const char* tr_contents[] = { "th", "td", NULL } ;
612static const char* bgcolor_attr[] = { "bgcolor", NULL } ;
613static const char* li_elt[] = { "li", NULL } ;
614static const char* ul_depr[] = { "type", "compact", NULL} ;
615static const char* dir_attr[] = { "dir", NULL} ;
616
617#define DECL (const char**)
618
Daniel Veillard22090732001-07-16 00:06:07 +0000619static const htmlElemDesc
620html40ElementTable[] = {
Daniel Veillard930dfb62003-02-05 10:17:38 +0000621{ "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
622 DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
623},
624{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
625 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
626},
627{ "acronym", 0, 0, 0, 0, 0, 0, 1, "",
628 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
629},
630{ "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
631 DECL inline_p , NULL , DECL html_attrs, NULL, NULL
632},
633{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
634 DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
635},
636{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
637 EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
638},
639{ "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
640 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
641},
642{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
643 EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
644},
645{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
646 EMPTY , NULL , NULL, DECL basefont_attrs, NULL
647},
648{ "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
649 DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
650},
651{ "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
652 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
653},
654{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
655 DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
656},
657{ "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
658 DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
659},
660{ "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
661 EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
662},
663{ "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
664 DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
665},
666{ "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
667 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
668},
669{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
670 DECL html_flow , NULL , NULL, DECL html_attrs, NULL
671},
672{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
673 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
674},
675{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
676 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
677},
678{ "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
679 EMPTY , NULL , DECL col_attrs , NULL, NULL
680},
681{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
682 DECL col_elt , "col" , DECL col_attrs , NULL, NULL
683},
684{ "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
685 DECL html_flow , NULL , DECL html_attrs, NULL, NULL
686},
687{ "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
688 DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
689},
690{ "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
691 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
692},
693{ "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
694 DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
695},
696{ "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
697 DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
698},
699{ "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
700 DECL dl_contents , "dd" , html_attrs, DECL compact_attr, NULL
701},
702{ "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
703 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
704},
705{ "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
706 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
707},
708{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
709 DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
710},
711{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
712 DECL html_inline, NULL, NULL, DECL font_attrs, NULL
713},
714{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
715 DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
716},
717{ "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
718 EMPTY, NULL, NULL, DECL frame_attrs, NULL
719},
720{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
721 DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
722},
723{ "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
724 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
725},
726{ "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
727 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
728},
729{ "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
730 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
731},
732{ "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
733 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
734},
735{ "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
736 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
737},
738{ "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
739 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
740},
741{ "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
742 DECL head_contents, NULL, DECL head_attrs, NULL, NULL
743},
744{ "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
745 EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
746},
747{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
748 DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
749},
750{ "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
751 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
752},
753{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
754 DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
755},
756{ "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
757 EMPTY, NULL, DECL img_attrs, DECL align_attr, src_alt_attrs
758},
759{ "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
760 EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
761},
762{ "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
763 DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
764},
765{ "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
766 EMPTY, NULL, NULL, DECL prompt_attrs, NULL
767},
768{ "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
769 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
770},
771{ "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
772 DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
773},
774{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
775 DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
776},
777{ "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
778 DECL html_flow, NULL, DECL html_attrs, NULL, NULL
779},
780{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
781 EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
782},
783{ "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
784 DECL map_contents , NULL, DECL html_attrs , NULL, name_attr
785},
786{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
787 DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
788},
789{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
790 EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
791},
792{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
793 DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
794},
795{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
796 DECL html_flow, "div", DECL html_attrs, NULL, NULL
797},
798{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
799 DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
800},
801{ "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
802 DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
803},
804{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
805 option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
806},
807{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
808 DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
809},
810{ "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
811 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
812},
813{ "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
814 EMPTY, NULL, DECL param_attrs, NULL, name_attr
815},
816{ "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
817 DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
818},
819{ "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
820 DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
821},
822{ "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
823 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
824},
825{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
826 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
827},
828{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
829 DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
830},
831{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
832 DECL select_content, NULL, DECL select_attrs, NULL, NULL
833},
834{ "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
835 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
836},
837{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
838 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
839},
840{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
841 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
842},
843{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
844 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
845},
846{ "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
847 DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
848},
849{ "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
850 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
851},
852{ "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
853 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
854},
855{ "table", 0, 0, 0, 0, 0, 0, 0, "",
856 DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
857},
858{ "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
859 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
860},
861{ "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
862 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
863},
864{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
865 DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
866},
867{ "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
868 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
869},
870{ "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
871 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
872},
873{ "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
874 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
875},
876{ "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
877 DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
878},
879{ "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
880 DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
881},
882{ "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
883 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
884},
885{ "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
886 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
887},
888{ "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
889 DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
890},
891{ "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
892 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
893}
Owen Taylor3473f882001-02-23 17:55:21 +0000894};
895
896/*
Owen Taylor3473f882001-02-23 17:55:21 +0000897 * start tags that imply the end of current element
898 */
Daniel Veillard22090732001-07-16 00:06:07 +0000899static const char *htmlStartClose[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000900"form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
901 "dl", "ul", "ol", "menu", "dir", "address", "pre",
902 "listing", "xmp", "head", NULL,
903"head", "p", NULL,
904"title", "p", NULL,
905"body", "head", "style", "link", "title", "p", NULL,
Daniel Veillard25d5d9a2004-04-05 07:08:42 +0000906"frameset", "head", "style", "link", "title", "p", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000907"li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
908 "pre", "listing", "xmp", "head", "li", NULL,
909"hr", "p", "head", NULL,
910"h1", "p", "head", NULL,
911"h2", "p", "head", NULL,
912"h3", "p", "head", NULL,
913"h4", "p", "head", NULL,
914"h5", "p", "head", NULL,
915"h6", "p", "head", NULL,
916"dir", "p", "head", NULL,
917"address", "p", "head", "ul", NULL,
918"pre", "p", "head", "ul", NULL,
919"listing", "p", "head", NULL,
920"xmp", "p", "head", NULL,
921"blockquote", "p", "head", NULL,
922"dl", "p", "dt", "menu", "dir", "address", "pre", "listing",
923 "xmp", "head", NULL,
924"dt", "p", "menu", "dir", "address", "pre", "listing", "xmp",
925 "head", "dd", NULL,
926"dd", "p", "menu", "dir", "address", "pre", "listing", "xmp",
927 "head", "dt", NULL,
928"ul", "p", "head", "ol", "menu", "dir", "address", "pre",
929 "listing", "xmp", NULL,
930"ol", "p", "head", "ul", NULL,
931"menu", "p", "head", "ul", NULL,
932"p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
933"div", "p", "head", NULL,
934"noscript", "p", "head", NULL,
935"center", "font", "b", "i", "p", "head", NULL,
936"a", "a", NULL,
937"caption", "p", NULL,
938"colgroup", "caption", "colgroup", "col", "p", NULL,
939"col", "caption", "col", "p", NULL,
940"table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
941 "listing", "xmp", "a", NULL,
Daniel Veillard43dadeb2001-04-24 11:23:35 +0000942"th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
943"td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000944"tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
945"thead", "caption", "col", "colgroup", NULL,
946"tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead",
947 "tbody", "p", NULL,
948"tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead",
949 "tfoot", "tbody", "p", NULL,
950"optgroup", "option", NULL,
951"option", "option", NULL,
952"fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
953 "pre", "listing", "xmp", "a", NULL,
954NULL
955};
956
957/*
958 * The list of HTML elements which are supposed not to have
959 * CDATA content and where a p element will be implied
960 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000961 * TODO: extend that list by reading the HTML SGML DTD on
Owen Taylor3473f882001-02-23 17:55:21 +0000962 * implied paragraph
963 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000964static const char *htmlNoContentElements[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000965 "html",
966 "head",
Owen Taylor3473f882001-02-23 17:55:21 +0000967 NULL
968};
969
970/*
971 * The list of HTML attributes which are of content %Script;
972 * NOTE: when adding ones, check htmlIsScriptAttribute() since
973 * it assumes the name starts with 'on'
974 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000975static const char *htmlScriptAttributes[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000976 "onclick",
977 "ondblclick",
978 "onmousedown",
979 "onmouseup",
980 "onmouseover",
981 "onmousemove",
982 "onmouseout",
983 "onkeypress",
984 "onkeydown",
985 "onkeyup",
986 "onload",
987 "onunload",
988 "onfocus",
989 "onblur",
990 "onsubmit",
991 "onrest",
992 "onchange",
993 "onselect"
994};
995
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000996/*
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000997 * This table is used by the htmlparser to know what to do with
998 * broken html pages. By assigning different priorities to different
999 * elements the parser can decide how to handle extra endtags.
1000 * Endtags are only allowed to close elements with lower or equal
1001 * priority.
1002 */
Daniel Veillarda2bc3682001-05-03 08:27:20 +00001003
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001004typedef struct {
1005 const char *name;
1006 int priority;
1007} elementPriority;
1008
Daniel Veillard22090732001-07-16 00:06:07 +00001009static const elementPriority htmlEndPriority[] = {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001010 {"div", 150},
1011 {"td", 160},
1012 {"th", 160},
1013 {"tr", 170},
1014 {"thead", 180},
1015 {"tbody", 180},
1016 {"tfoot", 180},
1017 {"table", 190},
1018 {"head", 200},
1019 {"body", 200},
1020 {"html", 220},
1021 {NULL, 100} /* Default priority */
1022};
Owen Taylor3473f882001-02-23 17:55:21 +00001023
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001024static const char** htmlStartCloseIndex[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001025static int htmlStartCloseIndexinitialized = 0;
1026
1027/************************************************************************
1028 * *
1029 * functions to handle HTML specific data *
1030 * *
1031 ************************************************************************/
1032
1033/**
1034 * htmlInitAutoClose:
1035 *
1036 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1037 * This is not reentrant. Call xmlInitParser() once before processing in
1038 * case of use in multithreaded programs.
1039 */
1040void
1041htmlInitAutoClose(void) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001042 int indx, i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001043
1044 if (htmlStartCloseIndexinitialized) return;
1045
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001046 for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL;
1047 indx = 0;
1048 while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) {
1049 htmlStartCloseIndex[indx++] = &htmlStartClose[i];
Owen Taylor3473f882001-02-23 17:55:21 +00001050 while (htmlStartClose[i] != NULL) i++;
1051 i++;
1052 }
1053 htmlStartCloseIndexinitialized = 1;
1054}
1055
1056/**
1057 * htmlTagLookup:
1058 * @tag: The tag name in lowercase
1059 *
1060 * Lookup the HTML tag in the ElementTable
1061 *
1062 * Returns the related htmlElemDescPtr or NULL if not found.
1063 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001064const htmlElemDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001065htmlTagLookup(const xmlChar *tag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001066 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001067
1068 for (i = 0; i < (sizeof(html40ElementTable) /
1069 sizeof(html40ElementTable[0]));i++) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +00001070 if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
William M. Brack78637da2003-07-31 14:47:38 +00001071 return((htmlElemDescPtr) &html40ElementTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001072 }
1073 return(NULL);
1074}
1075
1076/**
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001077 * htmlGetEndPriority:
1078 * @name: The name of the element to look up the priority for.
1079 *
1080 * Return value: The "endtag" priority.
1081 **/
1082static int
1083htmlGetEndPriority (const xmlChar *name) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001084 int i = 0;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001085
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001086 while ((htmlEndPriority[i].name != NULL) &&
1087 (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
1088 i++;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001089
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001090 return(htmlEndPriority[i].priority);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001091}
1092
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001093
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001094/**
Owen Taylor3473f882001-02-23 17:55:21 +00001095 * htmlCheckAutoClose:
1096 * @newtag: The new tag name
1097 * @oldtag: The old tag name
1098 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001099 * Checks whether the new tag is one of the registered valid tags for
1100 * closing old.
Owen Taylor3473f882001-02-23 17:55:21 +00001101 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1102 *
1103 * Returns 0 if no, 1 if yes.
1104 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001105static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001106htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
1107{
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001108 int i, indx;
1109 const char **closed = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001110
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001111 if (htmlStartCloseIndexinitialized == 0)
1112 htmlInitAutoClose();
Owen Taylor3473f882001-02-23 17:55:21 +00001113
1114 /* inefficient, but not a big deal */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001115 for (indx = 0; indx < 100; indx++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001116 closed = htmlStartCloseIndex[indx];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001117 if (closed == NULL)
1118 return (0);
1119 if (xmlStrEqual(BAD_CAST * closed, newtag))
1120 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001121 }
1122
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001123 i = closed - htmlStartClose;
Owen Taylor3473f882001-02-23 17:55:21 +00001124 i++;
1125 while (htmlStartClose[i] != NULL) {
1126 if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001127 return (1);
1128 }
1129 i++;
Owen Taylor3473f882001-02-23 17:55:21 +00001130 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001131 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001132}
1133
1134/**
1135 * htmlAutoCloseOnClose:
1136 * @ctxt: an HTML parser context
1137 * @newtag: The new tag name
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001138 * @force: force the tag closure
Owen Taylor3473f882001-02-23 17:55:21 +00001139 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001140 * The HTML DTD allows an ending tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001141 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001142static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001143htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1144{
1145 const htmlElemDesc *info;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001146 int i, priority;
Owen Taylor3473f882001-02-23 17:55:21 +00001147
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001148 priority = htmlGetEndPriority(newtag);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001149
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001150 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001151
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001152 if (xmlStrEqual(newtag, ctxt->nameTab[i]))
1153 break;
1154 /*
1155 * A missplaced endtag can only close elements with lower
1156 * or equal priority, so if we find an element with higher
1157 * priority before we find an element with
1158 * matching name, we just ignore this endtag
1159 */
1160 if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
1161 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001162 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001163 if (i < 0)
1164 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001165
1166 while (!xmlStrEqual(newtag, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001167 info = htmlTagLookup(ctxt->name);
Daniel Veillardf403d292003-10-05 13:51:35 +00001168 if ((info != NULL) && (info->endTag == 3)) {
1169 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
1170 "Opening and ending tag mismatch: %s and %s\n",
Daniel Veillard05bcb7e2003-10-19 14:26:34 +00001171 newtag, ctxt->name);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001172 }
1173 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1174 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001175 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001176 }
1177}
1178
1179/**
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001180 * htmlAutoCloseOnEnd:
1181 * @ctxt: an HTML parser context
1182 *
1183 * Close all remaining tags at the end of the stream
1184 */
1185static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001186htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
1187{
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001188 int i;
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001189
William M. Brack899e64a2003-09-26 18:03:42 +00001190 if (ctxt->nameNr == 0)
1191 return;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001192 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001193 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1194 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001195 htmlnamePop(ctxt);
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001196 }
1197}
1198
1199/**
Owen Taylor3473f882001-02-23 17:55:21 +00001200 * htmlAutoClose:
1201 * @ctxt: an HTML parser context
1202 * @newtag: The new tag name or NULL
1203 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001204 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001205 * The list is kept in htmlStartClose array. This function is
1206 * called when a new tag has been detected and generates the
1207 * appropriates closes if possible/needed.
1208 * If newtag is NULL this mean we are at the end of the resource
1209 * and we should check
1210 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001211static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001212htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1213{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001214 while ((newtag != NULL) && (ctxt->name != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001215 (htmlCheckAutoClose(newtag, ctxt->name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001216 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1217 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001218 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001219 }
1220 if (newtag == NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001221 htmlAutoCloseOnEnd(ctxt);
1222 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001223 }
1224 while ((newtag == NULL) && (ctxt->name != NULL) &&
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001225 ((xmlStrEqual(ctxt->name, BAD_CAST "head")) ||
1226 (xmlStrEqual(ctxt->name, BAD_CAST "body")) ||
1227 (xmlStrEqual(ctxt->name, BAD_CAST "html")))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001228 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1229 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001230 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001231 }
Owen Taylor3473f882001-02-23 17:55:21 +00001232}
1233
1234/**
1235 * htmlAutoCloseTag:
1236 * @doc: the HTML document
1237 * @name: The tag name
1238 * @elem: the HTML element
1239 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001240 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001241 * The list is kept in htmlStartClose array. This function checks
1242 * if the element or one of it's children would autoclose the
1243 * given tag.
1244 *
1245 * Returns 1 if autoclose, 0 otherwise
1246 */
1247int
1248htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
1249 htmlNodePtr child;
1250
1251 if (elem == NULL) return(1);
1252 if (xmlStrEqual(name, elem->name)) return(0);
1253 if (htmlCheckAutoClose(elem->name, name)) return(1);
1254 child = elem->children;
1255 while (child != NULL) {
1256 if (htmlAutoCloseTag(doc, name, child)) return(1);
1257 child = child->next;
1258 }
1259 return(0);
1260}
1261
1262/**
1263 * htmlIsAutoClosed:
1264 * @doc: the HTML document
1265 * @elem: the HTML element
1266 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001267 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001268 * The list is kept in htmlStartClose array. This function checks
1269 * if a tag is autoclosed by one of it's child
1270 *
1271 * Returns 1 if autoclosed, 0 otherwise
1272 */
1273int
1274htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
1275 htmlNodePtr child;
1276
1277 if (elem == NULL) return(1);
1278 child = elem->children;
1279 while (child != NULL) {
1280 if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
1281 child = child->next;
1282 }
1283 return(0);
1284}
1285
1286/**
1287 * htmlCheckImplied:
1288 * @ctxt: an HTML parser context
1289 * @newtag: The new tag name
1290 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001291 * The HTML DTD allows a tag to exists only implicitly
Owen Taylor3473f882001-02-23 17:55:21 +00001292 * called when a new tag has been detected and generates the
1293 * appropriates implicit tags if missing
1294 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001295static void
Owen Taylor3473f882001-02-23 17:55:21 +00001296htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1297 if (!htmlOmittedDefaultValue)
1298 return;
1299 if (xmlStrEqual(newtag, BAD_CAST"html"))
1300 return;
1301 if (ctxt->nameNr <= 0) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001302 htmlnamePush(ctxt, BAD_CAST"html");
Owen Taylor3473f882001-02-23 17:55:21 +00001303 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1304 ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
1305 }
1306 if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1307 return;
1308 if ((ctxt->nameNr <= 1) &&
1309 ((xmlStrEqual(newtag, BAD_CAST"script")) ||
1310 (xmlStrEqual(newtag, BAD_CAST"style")) ||
1311 (xmlStrEqual(newtag, BAD_CAST"meta")) ||
1312 (xmlStrEqual(newtag, BAD_CAST"link")) ||
1313 (xmlStrEqual(newtag, BAD_CAST"title")) ||
1314 (xmlStrEqual(newtag, BAD_CAST"base")))) {
1315 /*
1316 * dropped OBJECT ... i you put it first BODY will be
1317 * assumed !
1318 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001319 htmlnamePush(ctxt, BAD_CAST"head");
Owen Taylor3473f882001-02-23 17:55:21 +00001320 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1321 ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
1322 } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
1323 (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
1324 (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
1325 int i;
1326 for (i = 0;i < ctxt->nameNr;i++) {
1327 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
1328 return;
1329 }
1330 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
1331 return;
1332 }
1333 }
1334
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001335 htmlnamePush(ctxt, BAD_CAST"body");
Owen Taylor3473f882001-02-23 17:55:21 +00001336 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1337 ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
1338 }
1339}
1340
1341/**
1342 * htmlCheckParagraph
1343 * @ctxt: an HTML parser context
1344 *
1345 * Check whether a p element need to be implied before inserting
1346 * characters in the current element.
1347 *
1348 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1349 * in case of error.
1350 */
1351
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001352static int
Owen Taylor3473f882001-02-23 17:55:21 +00001353htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
1354 const xmlChar *tag;
1355 int i;
1356
1357 if (ctxt == NULL)
1358 return(-1);
1359 tag = ctxt->name;
1360 if (tag == NULL) {
1361 htmlAutoClose(ctxt, BAD_CAST"p");
1362 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001363 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001364 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1365 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1366 return(1);
1367 }
1368 if (!htmlOmittedDefaultValue)
1369 return(0);
1370 for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1371 if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
Owen Taylor3473f882001-02-23 17:55:21 +00001372 htmlAutoClose(ctxt, BAD_CAST"p");
1373 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001374 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001375 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1376 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1377 return(1);
1378 }
1379 }
1380 return(0);
1381}
1382
1383/**
1384 * htmlIsScriptAttribute:
1385 * @name: an attribute name
1386 *
1387 * Check if an attribute is of content type Script
1388 *
1389 * Returns 1 is the attribute is a script 0 otherwise
1390 */
1391int
1392htmlIsScriptAttribute(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001393 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001394
1395 if (name == NULL)
1396 return(0);
1397 /*
1398 * all script attributes start with 'on'
1399 */
1400 if ((name[0] != 'o') || (name[1] != 'n'))
1401 return(0);
1402 for (i = 0;
1403 i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1404 i++) {
1405 if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1406 return(1);
1407 }
1408 return(0);
1409}
1410
1411/************************************************************************
1412 * *
1413 * The list of HTML predefined entities *
1414 * *
1415 ************************************************************************/
1416
1417
Daniel Veillard22090732001-07-16 00:06:07 +00001418static const htmlEntityDesc html40EntitiesTable[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001419/*
1420 * the 4 absolute ones, plus apostrophe.
1421 */
1422{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1423{ 38, "amp", "ampersand, U+0026 ISOnum" },
1424{ 39, "apos", "single quote" },
1425{ 60, "lt", "less-than sign, U+003C ISOnum" },
1426{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1427
1428/*
1429 * A bunch still in the 128-255 range
1430 * Replacing them depend really on the charset used.
1431 */
1432{ 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1433{ 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1434{ 162, "cent", "cent sign, U+00A2 ISOnum" },
1435{ 163, "pound","pound sign, U+00A3 ISOnum" },
1436{ 164, "curren","currency sign, U+00A4 ISOnum" },
1437{ 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1438{ 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1439{ 167, "sect", "section sign, U+00A7 ISOnum" },
1440{ 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1441{ 169, "copy", "copyright sign, U+00A9 ISOnum" },
1442{ 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1443{ 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1444{ 172, "not", "not sign, U+00AC ISOnum" },
1445{ 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1446{ 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1447{ 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1448{ 176, "deg", "degree sign, U+00B0 ISOnum" },
1449{ 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1450{ 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1451{ 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1452{ 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1453{ 181, "micro","micro sign, U+00B5 ISOnum" },
1454{ 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1455{ 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1456{ 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1457{ 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1458{ 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1459{ 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1460{ 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1461{ 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1462{ 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1463{ 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1464{ 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1465{ 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1466{ 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1467{ 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1468{ 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1469{ 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1470{ 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1471{ 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1472{ 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1473{ 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1474{ 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1475{ 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1476{ 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1477{ 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1478{ 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1479{ 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1480{ 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1481{ 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1482{ 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1483{ 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1484{ 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1485{ 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1486{ 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1487{ 215, "times","multiplication sign, U+00D7 ISOnum" },
1488{ 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1489{ 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1490{ 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1491{ 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1492{ 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1493{ 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1494{ 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1495{ 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1496{ 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1497{ 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1498{ 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1499{ 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1500{ 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1501{ 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1502{ 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1503{ 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1504{ 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1505{ 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1506{ 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1507{ 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1508{ 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1509{ 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1510{ 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1511{ 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1512{ 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1513{ 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1514{ 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1515{ 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1516{ 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1517{ 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1518{ 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1519{ 247, "divide","division sign, U+00F7 ISOnum" },
1520{ 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1521{ 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1522{ 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1523{ 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1524{ 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1525{ 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1526{ 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1527{ 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1528
1529{ 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1530{ 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1531{ 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1532{ 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1533{ 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1534
1535/*
1536 * Anything below should really be kept as entities references
1537 */
1538{ 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1539
1540{ 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1541{ 732, "tilde","small tilde, U+02DC ISOdia" },
1542
1543{ 913, "Alpha","greek capital letter alpha, U+0391" },
1544{ 914, "Beta", "greek capital letter beta, U+0392" },
1545{ 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1546{ 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1547{ 917, "Epsilon","greek capital letter epsilon, U+0395" },
1548{ 918, "Zeta", "greek capital letter zeta, U+0396" },
1549{ 919, "Eta", "greek capital letter eta, U+0397" },
1550{ 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1551{ 921, "Iota", "greek capital letter iota, U+0399" },
1552{ 922, "Kappa","greek capital letter kappa, U+039A" },
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001553{ 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
Owen Taylor3473f882001-02-23 17:55:21 +00001554{ 924, "Mu", "greek capital letter mu, U+039C" },
1555{ 925, "Nu", "greek capital letter nu, U+039D" },
1556{ 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1557{ 927, "Omicron","greek capital letter omicron, U+039F" },
1558{ 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1559{ 929, "Rho", "greek capital letter rho, U+03A1" },
1560{ 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1561{ 932, "Tau", "greek capital letter tau, U+03A4" },
1562{ 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1563{ 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1564{ 935, "Chi", "greek capital letter chi, U+03A7" },
1565{ 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1566{ 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1567
1568{ 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1569{ 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1570{ 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1571{ 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1572{ 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1573{ 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1574{ 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1575{ 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1576{ 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1577{ 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1578{ 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1579{ 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1580{ 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1581{ 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1582{ 959, "omicron","greek small letter omicron, U+03BF NEW" },
1583{ 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1584{ 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1585{ 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1586{ 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1587{ 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1588{ 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1589{ 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1590{ 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1591{ 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1592{ 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1593{ 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1594{ 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1595{ 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1596
1597{ 8194, "ensp", "en space, U+2002 ISOpub" },
1598{ 8195, "emsp", "em space, U+2003 ISOpub" },
1599{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1600{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1601{ 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1602{ 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1603{ 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1604{ 8211, "ndash","en dash, U+2013 ISOpub" },
1605{ 8212, "mdash","em dash, U+2014 ISOpub" },
1606{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1607{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1608{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1609{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1610{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1611{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1612{ 8224, "dagger","dagger, U+2020 ISOpub" },
1613{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1614
1615{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1616{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1617
1618{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1619
1620{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1621{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1622
1623{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1624{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1625
1626{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1627{ 8260, "frasl","fraction slash, U+2044 NEW" },
1628
1629{ 8364, "euro", "euro sign, U+20AC NEW" },
1630
1631{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1632{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1633{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1634{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
1635{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
1636{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
1637{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
1638{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
1639{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
1640{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
1641{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
1642{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
1643{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
1644{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
1645{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
1646{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
1647
1648{ 8704, "forall","for all, U+2200 ISOtech" },
1649{ 8706, "part", "partial differential, U+2202 ISOtech" },
1650{ 8707, "exist","there exists, U+2203 ISOtech" },
1651{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
1652{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
1653{ 8712, "isin", "element of, U+2208 ISOtech" },
1654{ 8713, "notin","not an element of, U+2209 ISOtech" },
1655{ 8715, "ni", "contains as member, U+220B ISOtech" },
1656{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001657{ 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
Owen Taylor3473f882001-02-23 17:55:21 +00001658{ 8722, "minus","minus sign, U+2212 ISOtech" },
1659{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
1660{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
1661{ 8733, "prop", "proportional to, U+221D ISOtech" },
1662{ 8734, "infin","infinity, U+221E ISOtech" },
1663{ 8736, "ang", "angle, U+2220 ISOamso" },
1664{ 8743, "and", "logical and = wedge, U+2227 ISOtech" },
1665{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
1666{ 8745, "cap", "intersection = cap, U+2229 ISOtech" },
1667{ 8746, "cup", "union = cup, U+222A ISOtech" },
1668{ 8747, "int", "integral, U+222B ISOtech" },
1669{ 8756, "there4","therefore, U+2234 ISOtech" },
1670{ 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
1671{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
1672{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
1673{ 8800, "ne", "not equal to, U+2260 ISOtech" },
1674{ 8801, "equiv","identical to, U+2261 ISOtech" },
1675{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
1676{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
1677{ 8834, "sub", "subset of, U+2282 ISOtech" },
1678{ 8835, "sup", "superset of, U+2283 ISOtech" },
1679{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
1680{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
1681{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
1682{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
1683{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
1684{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
1685{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
1686{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
1687{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
1688{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
1689{ 8971, "rfloor","right floor, U+230B ISOamsc" },
1690{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
1691{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
1692{ 9674, "loz", "lozenge, U+25CA ISOpub" },
1693
1694{ 9824, "spades","black spade suit, U+2660 ISOpub" },
1695{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
1696{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
1697{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
1698
1699};
1700
1701/************************************************************************
1702 * *
1703 * Commodity functions to handle entities *
1704 * *
1705 ************************************************************************/
1706
1707/*
1708 * Macro used to grow the current buffer.
1709 */
1710#define growBuffer(buffer) { \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001711 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001712 buffer##_size *= 2; \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001713 tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
1714 if (tmp == NULL) { \
Daniel Veillardf403d292003-10-05 13:51:35 +00001715 htmlErrMemory(ctxt, "growing buffer\n"); \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001716 xmlFree(buffer); \
Owen Taylor3473f882001-02-23 17:55:21 +00001717 return(NULL); \
1718 } \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001719 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001720}
1721
1722/**
1723 * htmlEntityLookup:
1724 * @name: the entity name
1725 *
1726 * Lookup the given entity in EntitiesTable
1727 *
1728 * TODO: the linear scan is really ugly, an hash table is really needed.
1729 *
1730 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1731 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001732const htmlEntityDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001733htmlEntityLookup(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001734 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001735
1736 for (i = 0;i < (sizeof(html40EntitiesTable)/
1737 sizeof(html40EntitiesTable[0]));i++) {
1738 if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
William M. Brack78637da2003-07-31 14:47:38 +00001739 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001740 }
1741 }
1742 return(NULL);
1743}
1744
1745/**
1746 * htmlEntityValueLookup:
1747 * @value: the entity's unicode value
1748 *
1749 * Lookup the given entity in EntitiesTable
1750 *
1751 * TODO: the linear scan is really ugly, an hash table is really needed.
1752 *
1753 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1754 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001755const htmlEntityDesc *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001756htmlEntityValueLookup(unsigned int value) {
1757 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001758
1759 for (i = 0;i < (sizeof(html40EntitiesTable)/
1760 sizeof(html40EntitiesTable[0]));i++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001761 if (html40EntitiesTable[i].value >= value) {
1762 if (html40EntitiesTable[i].value > value)
Owen Taylor3473f882001-02-23 17:55:21 +00001763 break;
William M. Brack78637da2003-07-31 14:47:38 +00001764 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001765 }
Owen Taylor3473f882001-02-23 17:55:21 +00001766 }
1767 return(NULL);
1768}
1769
1770/**
1771 * UTF8ToHtml:
1772 * @out: a pointer to an array of bytes to store the result
1773 * @outlen: the length of @out
1774 * @in: a pointer to an array of UTF-8 chars
1775 * @inlen: the length of @in
1776 *
1777 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1778 * plus HTML entities block of chars out.
1779 *
1780 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1781 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001782 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001783 * The value of @outlen after return is the number of octets consumed.
1784 */
1785int
1786UTF8ToHtml(unsigned char* out, int *outlen,
1787 const unsigned char* in, int *inlen) {
1788 const unsigned char* processed = in;
1789 const unsigned char* outend;
1790 const unsigned char* outstart = out;
1791 const unsigned char* instart = in;
1792 const unsigned char* inend;
1793 unsigned int c, d;
1794 int trailing;
1795
Daniel Veillardce682bc2004-11-05 17:22:25 +00001796 if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001797 if (in == NULL) {
1798 /*
1799 * initialization nothing to do
1800 */
1801 *outlen = 0;
1802 *inlen = 0;
1803 return(0);
1804 }
1805 inend = in + (*inlen);
1806 outend = out + (*outlen);
1807 while (in < inend) {
1808 d = *in++;
1809 if (d < 0x80) { c= d; trailing= 0; }
1810 else if (d < 0xC0) {
1811 /* trailing byte in leading position */
1812 *outlen = out - outstart;
1813 *inlen = processed - instart;
1814 return(-2);
1815 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1816 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1817 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1818 else {
1819 /* no chance for this in Ascii */
1820 *outlen = out - outstart;
1821 *inlen = processed - instart;
1822 return(-2);
1823 }
1824
1825 if (inend - in < trailing) {
1826 break;
1827 }
1828
1829 for ( ; trailing; trailing--) {
1830 if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
1831 break;
1832 c <<= 6;
1833 c |= d & 0x3F;
1834 }
1835
1836 /* assertion: c is a single UTF-4 value */
1837 if (c < 0x80) {
1838 if (out + 1 >= outend)
1839 break;
1840 *out++ = c;
1841 } else {
1842 int len;
Daniel Veillardbb371292001-08-16 23:26:59 +00001843 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00001844
1845 /*
1846 * Try to lookup a predefined HTML entity for it
1847 */
1848
1849 ent = htmlEntityValueLookup(c);
1850 if (ent == NULL) {
1851 /* no chance for this in Ascii */
1852 *outlen = out - outstart;
1853 *inlen = processed - instart;
1854 return(-2);
1855 }
1856 len = strlen(ent->name);
1857 if (out + 2 + len >= outend)
1858 break;
1859 *out++ = '&';
1860 memcpy(out, ent->name, len);
1861 out += len;
1862 *out++ = ';';
1863 }
1864 processed = in;
1865 }
1866 *outlen = out - outstart;
1867 *inlen = processed - instart;
1868 return(0);
1869}
1870
1871/**
1872 * htmlEncodeEntities:
1873 * @out: a pointer to an array of bytes to store the result
1874 * @outlen: the length of @out
1875 * @in: a pointer to an array of UTF-8 chars
1876 * @inlen: the length of @in
1877 * @quoteChar: the quote character to escape (' or ") or zero.
1878 *
1879 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1880 * plus HTML entities block of chars out.
1881 *
1882 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1883 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001884 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001885 * The value of @outlen after return is the number of octets consumed.
1886 */
1887int
1888htmlEncodeEntities(unsigned char* out, int *outlen,
1889 const unsigned char* in, int *inlen, int quoteChar) {
1890 const unsigned char* processed = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00001891 const unsigned char* outend;
Owen Taylor3473f882001-02-23 17:55:21 +00001892 const unsigned char* outstart = out;
1893 const unsigned char* instart = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00001894 const unsigned char* inend;
Owen Taylor3473f882001-02-23 17:55:21 +00001895 unsigned int c, d;
1896 int trailing;
1897
Daniel Veillardce682bc2004-11-05 17:22:25 +00001898 if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL))
1899 return(-1);
1900 outend = out + (*outlen);
1901 inend = in + (*inlen);
Owen Taylor3473f882001-02-23 17:55:21 +00001902 while (in < inend) {
1903 d = *in++;
1904 if (d < 0x80) { c= d; trailing= 0; }
1905 else if (d < 0xC0) {
1906 /* trailing byte in leading position */
1907 *outlen = out - outstart;
1908 *inlen = processed - instart;
1909 return(-2);
1910 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1911 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1912 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1913 else {
1914 /* no chance for this in Ascii */
1915 *outlen = out - outstart;
1916 *inlen = processed - instart;
1917 return(-2);
1918 }
1919
1920 if (inend - in < trailing)
1921 break;
1922
1923 while (trailing--) {
1924 if (((d= *in++) & 0xC0) != 0x80) {
1925 *outlen = out - outstart;
1926 *inlen = processed - instart;
1927 return(-2);
1928 }
1929 c <<= 6;
1930 c |= d & 0x3F;
1931 }
1932
1933 /* assertion: c is a single UTF-4 value */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001934 if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
1935 (c != '&') && (c != '<') && (c != '>')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001936 if (out >= outend)
1937 break;
1938 *out++ = c;
1939 } else {
Daniel Veillardbb371292001-08-16 23:26:59 +00001940 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00001941 const char *cp;
1942 char nbuf[16];
1943 int len;
1944
1945 /*
1946 * Try to lookup a predefined HTML entity for it
1947 */
1948 ent = htmlEntityValueLookup(c);
1949 if (ent == NULL) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001950 snprintf(nbuf, sizeof(nbuf), "#%u", c);
Owen Taylor3473f882001-02-23 17:55:21 +00001951 cp = nbuf;
1952 }
1953 else
1954 cp = ent->name;
1955 len = strlen(cp);
1956 if (out + 2 + len > outend)
1957 break;
1958 *out++ = '&';
1959 memcpy(out, cp, len);
1960 out += len;
1961 *out++ = ';';
1962 }
1963 processed = in;
1964 }
1965 *outlen = out - outstart;
1966 *inlen = processed - instart;
1967 return(0);
1968}
1969
Owen Taylor3473f882001-02-23 17:55:21 +00001970/************************************************************************
1971 * *
1972 * Commodity functions to handle streams *
1973 * *
1974 ************************************************************************/
1975
1976/**
Owen Taylor3473f882001-02-23 17:55:21 +00001977 * htmlNewInputStream:
1978 * @ctxt: an HTML parser context
1979 *
1980 * Create a new input stream structure
1981 * Returns the new input stream or NULL
1982 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001983static htmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001984htmlNewInputStream(htmlParserCtxtPtr ctxt) {
1985 htmlParserInputPtr input;
1986
1987 input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1988 if (input == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00001989 htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001990 return(NULL);
1991 }
1992 memset(input, 0, sizeof(htmlParserInput));
1993 input->filename = NULL;
1994 input->directory = NULL;
1995 input->base = NULL;
1996 input->cur = NULL;
1997 input->buf = NULL;
1998 input->line = 1;
1999 input->col = 1;
2000 input->buf = NULL;
2001 input->free = NULL;
2002 input->version = NULL;
2003 input->consumed = 0;
2004 input->length = 0;
2005 return(input);
2006}
2007
2008
2009/************************************************************************
2010 * *
2011 * Commodity functions, cleanup needed ? *
2012 * *
2013 ************************************************************************/
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002014/*
2015 * all tags allowing pc data from the html 4.01 loose dtd
2016 * NOTE: it might be more apropriate to integrate this information
2017 * into the html40ElementTable array but I don't want to risk any
2018 * binary incomptibility
2019 */
2020static const char *allowPCData[] = {
2021 "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
2022 "blockquote", "body", "button", "caption", "center", "cite", "code",
2023 "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
2024 "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
2025 "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
2026 "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
2027};
Owen Taylor3473f882001-02-23 17:55:21 +00002028
2029/**
2030 * areBlanks:
2031 * @ctxt: an HTML parser context
2032 * @str: a xmlChar *
2033 * @len: the size of @str
2034 *
2035 * Is this a sequence of blank chars that one can ignore ?
2036 *
2037 * Returns 1 if ignorable 0 otherwise.
2038 */
2039
2040static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002041 unsigned int i;
2042 int j;
Owen Taylor3473f882001-02-23 17:55:21 +00002043 xmlNodePtr lastChild;
Daniel Veillard36d73402005-09-01 09:52:30 +00002044 xmlDtdPtr dtd;
Owen Taylor3473f882001-02-23 17:55:21 +00002045
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002046 for (j = 0;j < len;j++)
William M. Brack76e95df2003-10-18 16:20:14 +00002047 if (!(IS_BLANK_CH(str[j]))) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002048
2049 if (CUR == 0) return(1);
2050 if (CUR != '<') return(0);
2051 if (ctxt->name == NULL)
2052 return(1);
2053 if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
2054 return(1);
2055 if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
2056 return(1);
Daniel Veillard36d73402005-09-01 09:52:30 +00002057
2058 /* Only strip CDATA children of the body tag for strict HTML DTDs */
2059 if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) {
2060 dtd = xmlGetIntSubset(ctxt->myDoc);
2061 if (dtd != NULL && dtd->ExternalID != NULL) {
2062 if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") ||
2063 !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN"))
2064 return(1);
2065 }
2066 }
2067
Owen Taylor3473f882001-02-23 17:55:21 +00002068 if (ctxt->node == NULL) return(0);
2069 lastChild = xmlGetLastChild(ctxt->node);
Daniel Veillard18a65092004-05-11 15:57:42 +00002070 while ((lastChild) && (lastChild->type == XML_COMMENT_NODE))
2071 lastChild = lastChild->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00002072 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002073 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2074 (ctxt->node->content != NULL)) return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002075 /* keep ws in constructs like ...<b> </b>...
2076 for all tags "b" allowing PCDATA */
2077 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2078 if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
2079 return(0);
2080 }
2081 }
Owen Taylor3473f882001-02-23 17:55:21 +00002082 } else if (xmlNodeIsText(lastChild)) {
2083 return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002084 } else {
2085 /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
2086 for all tags "p" allowing PCDATA */
2087 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2088 if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
2089 return(0);
2090 }
2091 }
Owen Taylor3473f882001-02-23 17:55:21 +00002092 }
2093 return(1);
2094}
2095
2096/**
Owen Taylor3473f882001-02-23 17:55:21 +00002097 * htmlNewDocNoDtD:
2098 * @URI: URI for the dtd, or NULL
2099 * @ExternalID: the external ID of the DTD, or NULL
2100 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002101 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2102 * are NULL
2103 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002104 * Returns a new document, do not initialize the DTD if not provided
Owen Taylor3473f882001-02-23 17:55:21 +00002105 */
2106htmlDocPtr
2107htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
2108 xmlDocPtr cur;
2109
2110 /*
2111 * Allocate a new document and fill the fields.
2112 */
2113 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
2114 if (cur == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002115 htmlErrMemory(NULL, "HTML document creation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002116 return(NULL);
2117 }
2118 memset(cur, 0, sizeof(xmlDoc));
2119
2120 cur->type = XML_HTML_DOCUMENT_NODE;
2121 cur->version = NULL;
2122 cur->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002123 cur->doc = cur;
2124 cur->name = NULL;
2125 cur->children = NULL;
2126 cur->extSubset = NULL;
2127 cur->oldNs = NULL;
2128 cur->encoding = NULL;
2129 cur->standalone = 1;
2130 cur->compression = 0;
2131 cur->ids = NULL;
2132 cur->refs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002133 cur->_private = NULL;
Daniel Veillard7cc23572004-07-29 11:20:30 +00002134 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillardb6b0fd82001-10-22 12:31:11 +00002135 if ((ExternalID != NULL) ||
2136 (URI != NULL))
Daniel Veillard40412cd2003-09-03 13:28:32 +00002137 xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002138 return(cur);
2139}
2140
2141/**
2142 * htmlNewDoc:
2143 * @URI: URI for the dtd, or NULL
2144 * @ExternalID: the external ID of the DTD, or NULL
2145 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002146 * Creates a new HTML document
2147 *
Owen Taylor3473f882001-02-23 17:55:21 +00002148 * Returns a new document
2149 */
2150htmlDocPtr
2151htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
2152 if ((URI == NULL) && (ExternalID == NULL))
2153 return(htmlNewDocNoDtD(
Daniel Veillard64269352001-05-04 17:52:34 +00002154 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
2155 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
Owen Taylor3473f882001-02-23 17:55:21 +00002156
2157 return(htmlNewDocNoDtD(URI, ExternalID));
2158}
2159
2160
2161/************************************************************************
2162 * *
2163 * The parser itself *
2164 * Relates to http://www.w3.org/TR/html40 *
2165 * *
2166 ************************************************************************/
2167
2168/************************************************************************
2169 * *
2170 * The parser itself *
2171 * *
2172 ************************************************************************/
2173
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002174static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002175
Owen Taylor3473f882001-02-23 17:55:21 +00002176/**
2177 * htmlParseHTMLName:
2178 * @ctxt: an HTML parser context
2179 *
2180 * parse an HTML tag or attribute name, note that we convert it to lowercase
2181 * since HTML names are not case-sensitive.
2182 *
2183 * Returns the Tag Name parsed or NULL
2184 */
2185
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002186static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002187htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002188 int i = 0;
2189 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2190
William M. Brackd1757ab2004-10-02 22:07:48 +00002191 if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002192 (CUR != ':')) return(NULL);
2193
2194 while ((i < HTML_PARSER_BUFFER_SIZE) &&
William M. Brackd1757ab2004-10-02 22:07:48 +00002195 ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002196 (CUR == ':') || (CUR == '-') || (CUR == '_'))) {
2197 if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2198 else loc[i] = CUR;
2199 i++;
2200
2201 NEXT;
2202 }
2203
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002204 return(xmlDictLookup(ctxt->dict, loc, i));
Owen Taylor3473f882001-02-23 17:55:21 +00002205}
2206
2207/**
2208 * htmlParseName:
2209 * @ctxt: an HTML parser context
2210 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002211 * parse an HTML name, this routine is case sensitive.
Owen Taylor3473f882001-02-23 17:55:21 +00002212 *
2213 * Returns the Name parsed or NULL
2214 */
2215
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002216static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002217htmlParseName(htmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002218 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002219 const xmlChar *ret;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002220 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002221
2222 GROW;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002223
2224 /*
2225 * Accelerator for simple ASCII names
2226 */
2227 in = ctxt->input->cur;
2228 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2229 ((*in >= 0x41) && (*in <= 0x5A)) ||
2230 (*in == '_') || (*in == ':')) {
2231 in++;
2232 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2233 ((*in >= 0x41) && (*in <= 0x5A)) ||
2234 ((*in >= 0x30) && (*in <= 0x39)) ||
2235 (*in == '_') || (*in == '-') ||
2236 (*in == ':') || (*in == '.'))
2237 in++;
2238 if ((*in > 0) && (*in < 0x80)) {
2239 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002240 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002241 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002242 ctxt->nbChars += count;
2243 ctxt->input->col += count;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002244 return(ret);
2245 }
2246 }
2247 return(htmlParseNameComplex(ctxt));
2248}
2249
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002250static const xmlChar *
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002251htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002252 int len = 0, l;
2253 int c;
2254 int count = 0;
2255
2256 /*
2257 * Handler for more complex cases
2258 */
2259 GROW;
2260 c = CUR_CHAR(l);
2261 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2262 (!IS_LETTER(c) && (c != '_') &&
2263 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002264 return(NULL);
2265 }
2266
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002267 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
2268 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2269 (c == '.') || (c == '-') ||
2270 (c == '_') || (c == ':') ||
2271 (IS_COMBINING(c)) ||
2272 (IS_EXTENDER(c)))) {
2273 if (count++ > 100) {
2274 count = 0;
2275 GROW;
2276 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002277 len += l;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002278 NEXTL(l);
2279 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002280 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002281 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002282}
2283
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002284
Owen Taylor3473f882001-02-23 17:55:21 +00002285/**
2286 * htmlParseHTMLAttribute:
2287 * @ctxt: an HTML parser context
2288 * @stop: a char stop value
2289 *
2290 * parse an HTML attribute value till the stop (quote), if
2291 * stop is 0 then it stops at the first space
2292 *
2293 * Returns the attribute parsed or NULL
2294 */
2295
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002296static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002297htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
2298 xmlChar *buffer = NULL;
2299 int buffer_size = 0;
2300 xmlChar *out = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002301 const xmlChar *name = NULL;
2302 const xmlChar *cur = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00002303 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00002304
2305 /*
2306 * allocate a translation buffer.
2307 */
2308 buffer_size = HTML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002309 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002310 if (buffer == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002311 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002312 return(NULL);
2313 }
2314 out = buffer;
2315
2316 /*
2317 * Ok loop until we reach one of the ending chars
2318 */
Daniel Veillard957fdcf2001-11-06 22:50:19 +00002319 while ((CUR != 0) && (CUR != stop)) {
2320 if ((stop == 0) && (CUR == '>')) break;
William M. Brack76e95df2003-10-18 16:20:14 +00002321 if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
Owen Taylor3473f882001-02-23 17:55:21 +00002322 if (CUR == '&') {
2323 if (NXT(1) == '#') {
2324 unsigned int c;
2325 int bits;
2326
2327 c = htmlParseCharRef(ctxt);
2328 if (c < 0x80)
2329 { *out++ = c; bits= -6; }
2330 else if (c < 0x800)
2331 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2332 else if (c < 0x10000)
2333 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2334 else
2335 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2336
2337 for ( ; bits >= 0; bits-= 6) {
2338 *out++ = ((c >> bits) & 0x3F) | 0x80;
2339 }
Daniel Veillardce02dbc2002-10-22 19:14:58 +00002340
2341 if (out - buffer > buffer_size - 100) {
2342 int indx = out - buffer;
2343
2344 growBuffer(buffer);
2345 out = &buffer[indx];
2346 }
Owen Taylor3473f882001-02-23 17:55:21 +00002347 } else {
2348 ent = htmlParseEntityRef(ctxt, &name);
2349 if (name == NULL) {
2350 *out++ = '&';
2351 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002352 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002353
2354 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002355 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002356 }
2357 } else if (ent == NULL) {
2358 *out++ = '&';
2359 cur = name;
2360 while (*cur != 0) {
2361 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002362 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002363
2364 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002365 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002366 }
2367 *out++ = *cur++;
2368 }
Owen Taylor3473f882001-02-23 17:55:21 +00002369 } else {
2370 unsigned int c;
2371 int bits;
2372
2373 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002374 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002375
2376 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002377 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002378 }
2379 c = (xmlChar)ent->value;
2380 if (c < 0x80)
2381 { *out++ = c; bits= -6; }
2382 else if (c < 0x800)
2383 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2384 else if (c < 0x10000)
2385 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2386 else
2387 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2388
2389 for ( ; bits >= 0; bits-= 6) {
2390 *out++ = ((c >> bits) & 0x3F) | 0x80;
2391 }
Owen Taylor3473f882001-02-23 17:55:21 +00002392 }
2393 }
2394 } else {
2395 unsigned int c;
2396 int bits, l;
2397
2398 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002399 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002400
2401 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002402 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002403 }
2404 c = CUR_CHAR(l);
2405 if (c < 0x80)
2406 { *out++ = c; bits= -6; }
2407 else if (c < 0x800)
2408 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2409 else if (c < 0x10000)
2410 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2411 else
2412 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2413
2414 for ( ; bits >= 0; bits-= 6) {
2415 *out++ = ((c >> bits) & 0x3F) | 0x80;
2416 }
2417 NEXT;
2418 }
2419 }
2420 *out++ = 0;
2421 return(buffer);
2422}
2423
2424/**
Owen Taylor3473f882001-02-23 17:55:21 +00002425 * htmlParseEntityRef:
2426 * @ctxt: an HTML parser context
2427 * @str: location to store the entity name
2428 *
2429 * parse an HTML ENTITY references
2430 *
2431 * [68] EntityRef ::= '&' Name ';'
2432 *
2433 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2434 * if non-NULL *str will have to be freed by the caller.
2435 */
Daniel Veillardbb371292001-08-16 23:26:59 +00002436const htmlEntityDesc *
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002437htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
2438 const xmlChar *name;
Daniel Veillardbb371292001-08-16 23:26:59 +00002439 const htmlEntityDesc * ent = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00002440
2441 if (str != NULL) *str = NULL;
2442 if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002443
2444 if (CUR == '&') {
2445 NEXT;
2446 name = htmlParseName(ctxt);
2447 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002448 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2449 "htmlParseEntityRef: no name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002450 } else {
2451 GROW;
2452 if (CUR == ';') {
Daniel Veillard42595322004-11-08 10:52:06 +00002453 if (str != NULL)
2454 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002455
2456 /*
2457 * Lookup the entity in the table.
2458 */
2459 ent = htmlEntityLookup(name);
2460 if (ent != NULL) /* OK that's ugly !!! */
2461 NEXT;
2462 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002463 htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
2464 "htmlParseEntityRef: expecting ';'\n",
2465 NULL, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002466 if (str != NULL)
2467 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002468 }
2469 }
2470 }
2471 return(ent);
2472}
2473
2474/**
2475 * htmlParseAttValue:
2476 * @ctxt: an HTML parser context
2477 *
2478 * parse a value for an attribute
2479 * Note: the parser won't do substitution of entities here, this
2480 * will be handled later in xmlStringGetNodeList, unless it was
2481 * asked for ctxt->replaceEntities != 0
2482 *
2483 * Returns the AttValue parsed or NULL.
2484 */
2485
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002486static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002487htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2488 xmlChar *ret = NULL;
2489
2490 if (CUR == '"') {
2491 NEXT;
2492 ret = htmlParseHTMLAttribute(ctxt, '"');
2493 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002494 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2495 "AttValue: \" expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002496 } else
2497 NEXT;
2498 } else if (CUR == '\'') {
2499 NEXT;
2500 ret = htmlParseHTMLAttribute(ctxt, '\'');
2501 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002502 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2503 "AttValue: ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002504 } else
2505 NEXT;
2506 } else {
2507 /*
2508 * That's an HTMLism, the attribute value may not be quoted
2509 */
2510 ret = htmlParseHTMLAttribute(ctxt, 0);
2511 if (ret == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002512 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
2513 "AttValue: no value found\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002514 }
2515 }
2516 return(ret);
2517}
2518
2519/**
2520 * htmlParseSystemLiteral:
2521 * @ctxt: an HTML parser context
2522 *
2523 * parse an HTML Literal
2524 *
2525 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2526 *
2527 * Returns the SystemLiteral parsed or NULL
2528 */
2529
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002530static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002531htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2532 const xmlChar *q;
2533 xmlChar *ret = NULL;
2534
2535 if (CUR == '"') {
2536 NEXT;
2537 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002538 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00002539 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002540 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002541 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2542 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002543 } else {
2544 ret = xmlStrndup(q, CUR_PTR - q);
2545 NEXT;
2546 }
2547 } else if (CUR == '\'') {
2548 NEXT;
2549 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002550 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002551 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002552 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002553 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2554 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002555 } else {
2556 ret = xmlStrndup(q, CUR_PTR - q);
2557 NEXT;
2558 }
2559 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002560 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2561 " or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002562 }
2563
2564 return(ret);
2565}
2566
2567/**
2568 * htmlParsePubidLiteral:
2569 * @ctxt: an HTML parser context
2570 *
2571 * parse an HTML public literal
2572 *
2573 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2574 *
2575 * Returns the PubidLiteral parsed or NULL.
2576 */
2577
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002578static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002579htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
2580 const xmlChar *q;
2581 xmlChar *ret = NULL;
2582 /*
2583 * Name ::= (Letter | '_') (NameChar)*
2584 */
2585 if (CUR == '"') {
2586 NEXT;
2587 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002588 while (IS_PUBIDCHAR_CH(CUR)) NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00002589 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002590 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2591 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002592 } else {
2593 ret = xmlStrndup(q, CUR_PTR - q);
2594 NEXT;
2595 }
2596 } else if (CUR == '\'') {
2597 NEXT;
2598 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002599 while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002600 NEXT;
Daniel Veillard6560a422003-03-27 21:25:38 +00002601 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002602 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2603 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002604 } else {
2605 ret = xmlStrndup(q, CUR_PTR - q);
2606 NEXT;
2607 }
2608 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002609 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2610 "PubidLiteral \" or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002611 }
2612
2613 return(ret);
2614}
2615
2616/**
2617 * htmlParseScript:
2618 * @ctxt: an HTML parser context
2619 *
2620 * parse the content of an HTML SCRIPT or STYLE element
2621 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
2622 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
2623 * http://www.w3.org/TR/html4/types.html#type-script
2624 * http://www.w3.org/TR/html4/types.html#h-6.15
2625 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
2626 *
2627 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
2628 * element and the value of intrinsic event attributes. User agents must
2629 * not evaluate script data as HTML markup but instead must pass it on as
2630 * data to a script engine.
2631 * NOTES:
2632 * - The content is passed like CDATA
2633 * - the attributes for style and scripting "onXXX" are also described
2634 * as CDATA but SGML allows entities references in attributes so their
2635 * processing is identical as other attributes
2636 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002637static void
Owen Taylor3473f882001-02-23 17:55:21 +00002638htmlParseScript(htmlParserCtxtPtr ctxt) {
Daniel Veillard7d2b3232005-07-14 08:57:39 +00002639 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
Owen Taylor3473f882001-02-23 17:55:21 +00002640 int nbchar = 0;
Daniel Veillard358fef42005-07-13 16:37:38 +00002641 int cur,l;
Owen Taylor3473f882001-02-23 17:55:21 +00002642
2643 SHRINK;
Daniel Veillard358fef42005-07-13 16:37:38 +00002644 cur = CUR_CHAR(l);
William M. Brack76e95df2003-10-18 16:20:14 +00002645 while (IS_CHAR_CH(cur)) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00002646 if ((cur == '<') && (NXT(1) == '!') && (NXT(2) == '-') &&
2647 (NXT(3) == '-')) {
2648 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2649 if (ctxt->sax->cdataBlock!= NULL) {
2650 /*
2651 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2652 */
2653 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002654 } else if (ctxt->sax->characters != NULL) {
2655 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Daniel Veillardc1f78342001-11-10 11:43:05 +00002656 }
2657 }
2658 nbchar = 0;
2659 htmlParseComment(ctxt);
Daniel Veillard358fef42005-07-13 16:37:38 +00002660 cur = CUR_CHAR(l);
Daniel Veillardc1f78342001-11-10 11:43:05 +00002661 continue;
2662 } else if ((cur == '<') && (NXT(1) == '/')) {
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002663 /*
2664 * One should break here, the specification is clear:
2665 * Authors should therefore escape "</" within the content.
2666 * Escape mechanisms are specific to each scripting or
2667 * style sheet language.
2668 *
2669 * In recovery mode, only break if end tag match the
2670 * current tag, effectively ignoring all tags inside the
2671 * script/style block and treating the entire block as
2672 * CDATA.
2673 */
2674 if (ctxt->recovery) {
2675 if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2,
2676 xmlStrlen(ctxt->name)) == 0)
2677 {
2678 break; /* while */
2679 } else {
2680 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard2cf36a12005-10-25 12:21:29 +00002681 "Element %s embeds close tag\n",
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002682 ctxt->name, NULL);
2683 }
2684 } else {
2685 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
2686 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
2687 {
2688 break; /* while */
2689 }
2690 }
Owen Taylor3473f882001-02-23 17:55:21 +00002691 }
Daniel Veillard358fef42005-07-13 16:37:38 +00002692 COPY_BUF(l,buf,nbchar,cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002693 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2694 if (ctxt->sax->cdataBlock!= NULL) {
2695 /*
2696 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2697 */
2698 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002699 } else if (ctxt->sax->characters != NULL) {
2700 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002701 }
2702 nbchar = 0;
2703 }
Daniel Veillard358fef42005-07-13 16:37:38 +00002704 NEXTL(l);
2705 cur = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002706 }
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002707
William M. Brack76e95df2003-10-18 16:20:14 +00002708 if (!(IS_CHAR_CH(cur))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002709 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2710 "Invalid char in CDATA 0x%X\n", cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002711 NEXT;
2712 }
2713
2714 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2715 if (ctxt->sax->cdataBlock!= NULL) {
2716 /*
2717 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2718 */
2719 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002720 } else if (ctxt->sax->characters != NULL) {
2721 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002722 }
2723 }
2724}
2725
2726
2727/**
2728 * htmlParseCharData:
2729 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002730 *
2731 * parse a CharData section.
2732 * if we are within a CDATA section ']]>' marks an end of section.
2733 *
2734 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2735 */
2736
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002737static void
2738htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002739 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2740 int nbchar = 0;
2741 int cur, l;
2742
2743 SHRINK;
2744 cur = CUR_CHAR(l);
2745 while (((cur != '<') || (ctxt->token == '<')) &&
2746 ((cur != '&') || (ctxt->token == '&')) &&
2747 (IS_CHAR(cur))) {
2748 COPY_BUF(l,buf,nbchar,cur);
2749 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2750 /*
2751 * Ok the segment is to be consumed as chars.
2752 */
2753 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2754 if (areBlanks(ctxt, buf, nbchar)) {
2755 if (ctxt->sax->ignorableWhitespace != NULL)
2756 ctxt->sax->ignorableWhitespace(ctxt->userData,
2757 buf, nbchar);
2758 } else {
2759 htmlCheckParagraph(ctxt);
2760 if (ctxt->sax->characters != NULL)
2761 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2762 }
2763 }
2764 nbchar = 0;
2765 }
2766 NEXTL(l);
2767 cur = CUR_CHAR(l);
Daniel Veillard358a9892003-02-04 15:22:32 +00002768 if (cur == 0) {
2769 SHRINK;
2770 GROW;
2771 cur = CUR_CHAR(l);
2772 }
Owen Taylor3473f882001-02-23 17:55:21 +00002773 }
2774 if (nbchar != 0) {
Daniel Veillardd2755a82005-08-07 23:42:39 +00002775 buf[nbchar] = 0;
2776
Owen Taylor3473f882001-02-23 17:55:21 +00002777 /*
2778 * Ok the segment is to be consumed as chars.
2779 */
2780 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2781 if (areBlanks(ctxt, buf, nbchar)) {
2782 if (ctxt->sax->ignorableWhitespace != NULL)
2783 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2784 } else {
2785 htmlCheckParagraph(ctxt);
2786 if (ctxt->sax->characters != NULL)
2787 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2788 }
2789 }
Daniel Veillard7cc95c02001-10-17 15:45:12 +00002790 } else {
2791 /*
2792 * Loop detection
2793 */
2794 if (cur == 0)
2795 ctxt->instate = XML_PARSER_EOF;
Owen Taylor3473f882001-02-23 17:55:21 +00002796 }
2797}
2798
2799/**
2800 * htmlParseExternalID:
2801 * @ctxt: an HTML parser context
2802 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002803 *
2804 * Parse an External ID or a Public ID
2805 *
Owen Taylor3473f882001-02-23 17:55:21 +00002806 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
2807 * | 'PUBLIC' S PubidLiteral S SystemLiteral
2808 *
2809 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
2810 *
2811 * Returns the function returns SystemLiteral and in the second
2812 * case publicID receives PubidLiteral, is strict is off
2813 * it is possible to return NULL and have publicID set.
2814 */
2815
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002816static xmlChar *
2817htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00002818 xmlChar *URI = NULL;
2819
2820 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2821 (UPP(2) == 'S') && (UPP(3) == 'T') &&
2822 (UPP(4) == 'E') && (UPP(5) == 'M')) {
2823 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002824 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002825 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2826 "Space required after 'SYSTEM'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002827 }
2828 SKIP_BLANKS;
2829 URI = htmlParseSystemLiteral(ctxt);
2830 if (URI == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002831 htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
2832 "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002833 }
2834 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2835 (UPP(2) == 'B') && (UPP(3) == 'L') &&
2836 (UPP(4) == 'I') && (UPP(5) == 'C')) {
2837 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002838 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002839 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2840 "Space required after 'PUBLIC'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002841 }
2842 SKIP_BLANKS;
2843 *publicID = htmlParsePubidLiteral(ctxt);
2844 if (*publicID == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002845 htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
2846 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
2847 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002848 }
2849 SKIP_BLANKS;
2850 if ((CUR == '"') || (CUR == '\'')) {
2851 URI = htmlParseSystemLiteral(ctxt);
2852 }
2853 }
2854 return(URI);
2855}
2856
2857/**
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002858 * xmlParsePI:
2859 * @ctxt: an XML parser context
2860 *
2861 * parse an XML Processing Instruction.
2862 *
2863 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
2864 */
2865static void
2866htmlParsePI(htmlParserCtxtPtr ctxt) {
2867 xmlChar *buf = NULL;
2868 int len = 0;
2869 int size = HTML_PARSER_BUFFER_SIZE;
2870 int cur, l;
2871 const xmlChar *target;
2872 xmlParserInputState state;
2873 int count = 0;
2874
2875 if ((RAW == '<') && (NXT(1) == '?')) {
2876 state = ctxt->instate;
2877 ctxt->instate = XML_PARSER_PI;
2878 /*
2879 * this is a Processing Instruction.
2880 */
2881 SKIP(2);
2882 SHRINK;
2883
2884 /*
2885 * Parse the target name and check for special support like
2886 * namespace.
2887 */
2888 target = htmlParseName(ctxt);
2889 if (target != NULL) {
2890 if (RAW == '>') {
2891 SKIP(1);
2892
2893 /*
2894 * SAX: PI detected.
2895 */
2896 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2897 (ctxt->sax->processingInstruction != NULL))
2898 ctxt->sax->processingInstruction(ctxt->userData,
2899 target, NULL);
2900 ctxt->instate = state;
2901 return;
2902 }
2903 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
2904 if (buf == NULL) {
2905 htmlErrMemory(ctxt, NULL);
2906 ctxt->instate = state;
2907 return;
2908 }
2909 cur = CUR;
2910 if (!IS_BLANK(cur)) {
2911 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2912 "ParsePI: PI %s space expected\n", target, NULL);
2913 }
2914 SKIP_BLANKS;
2915 cur = CUR_CHAR(l);
2916 while (IS_CHAR(cur) && (cur != '>')) {
2917 if (len + 5 >= size) {
2918 xmlChar *tmp;
2919
2920 size *= 2;
2921 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2922 if (tmp == NULL) {
2923 htmlErrMemory(ctxt, NULL);
2924 xmlFree(buf);
2925 ctxt->instate = state;
2926 return;
2927 }
2928 buf = tmp;
2929 }
2930 count++;
2931 if (count > 50) {
2932 GROW;
2933 count = 0;
2934 }
2935 COPY_BUF(l,buf,len,cur);
2936 NEXTL(l);
2937 cur = CUR_CHAR(l);
2938 if (cur == 0) {
2939 SHRINK;
2940 GROW;
2941 cur = CUR_CHAR(l);
2942 }
2943 }
2944 buf[len] = 0;
2945 if (cur != '>') {
2946 htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
2947 "ParsePI: PI %s never end ...\n", target, NULL);
2948 } else {
2949 SKIP(1);
2950
2951 /*
2952 * SAX: PI detected.
2953 */
2954 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2955 (ctxt->sax->processingInstruction != NULL))
2956 ctxt->sax->processingInstruction(ctxt->userData,
2957 target, buf);
2958 }
2959 xmlFree(buf);
2960 } else {
2961 htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
2962 "PI is not started correctly", NULL, NULL);
2963 }
2964 ctxt->instate = state;
2965 }
2966}
2967
2968/**
Owen Taylor3473f882001-02-23 17:55:21 +00002969 * htmlParseComment:
2970 * @ctxt: an HTML parser context
2971 *
2972 * Parse an XML (SGML) comment <!-- .... -->
2973 *
2974 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
2975 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002976static void
Owen Taylor3473f882001-02-23 17:55:21 +00002977htmlParseComment(htmlParserCtxtPtr ctxt) {
2978 xmlChar *buf = NULL;
2979 int len;
2980 int size = HTML_PARSER_BUFFER_SIZE;
2981 int q, ql;
2982 int r, rl;
2983 int cur, l;
2984 xmlParserInputState state;
2985
2986 /*
2987 * Check that there is a comment right here.
2988 */
2989 if ((RAW != '<') || (NXT(1) != '!') ||
2990 (NXT(2) != '-') || (NXT(3) != '-')) return;
2991
2992 state = ctxt->instate;
2993 ctxt->instate = XML_PARSER_COMMENT;
2994 SHRINK;
2995 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002996 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002997 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002998 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002999 ctxt->instate = state;
3000 return;
3001 }
3002 q = CUR_CHAR(ql);
3003 NEXTL(ql);
3004 r = CUR_CHAR(rl);
3005 NEXTL(rl);
3006 cur = CUR_CHAR(l);
3007 len = 0;
3008 while (IS_CHAR(cur) &&
3009 ((cur != '>') ||
3010 (r != '-') || (q != '-'))) {
3011 if (len + 5 >= size) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003012 xmlChar *tmp;
3013
Owen Taylor3473f882001-02-23 17:55:21 +00003014 size *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003015 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3016 if (tmp == NULL) {
3017 xmlFree(buf);
Daniel Veillardf403d292003-10-05 13:51:35 +00003018 htmlErrMemory(ctxt, "growing buffer failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003019 ctxt->instate = state;
3020 return;
3021 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003022 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003023 }
3024 COPY_BUF(ql,buf,len,q);
3025 q = r;
3026 ql = rl;
3027 r = cur;
3028 rl = l;
3029 NEXTL(l);
3030 cur = CUR_CHAR(l);
3031 if (cur == 0) {
3032 SHRINK;
3033 GROW;
3034 cur = CUR_CHAR(l);
3035 }
3036 }
3037 buf[len] = 0;
3038 if (!IS_CHAR(cur)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003039 htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3040 "Comment not terminated \n<!--%.50s\n", buf, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003041 xmlFree(buf);
3042 } else {
3043 NEXT;
3044 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3045 (!ctxt->disableSAX))
3046 ctxt->sax->comment(ctxt->userData, buf);
3047 xmlFree(buf);
3048 }
3049 ctxt->instate = state;
3050}
3051
3052/**
3053 * htmlParseCharRef:
3054 * @ctxt: an HTML parser context
3055 *
3056 * parse Reference declarations
3057 *
3058 * [66] CharRef ::= '&#' [0-9]+ ';' |
3059 * '&#x' [0-9a-fA-F]+ ';'
3060 *
3061 * Returns the value parsed (as an int)
3062 */
3063int
3064htmlParseCharRef(htmlParserCtxtPtr ctxt) {
3065 int val = 0;
3066
Daniel Veillarda03e3652004-11-02 18:45:30 +00003067 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3068 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3069 "htmlParseCharRef: context error\n",
3070 NULL, NULL);
3071 return(0);
3072 }
Owen Taylor3473f882001-02-23 17:55:21 +00003073 if ((CUR == '&') && (NXT(1) == '#') &&
Daniel Veillardc59d8262003-11-20 21:59:12 +00003074 ((NXT(2) == 'x') || NXT(2) == 'X')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003075 SKIP(3);
3076 while (CUR != ';') {
3077 if ((CUR >= '0') && (CUR <= '9'))
3078 val = val * 16 + (CUR - '0');
3079 else if ((CUR >= 'a') && (CUR <= 'f'))
3080 val = val * 16 + (CUR - 'a') + 10;
3081 else if ((CUR >= 'A') && (CUR <= 'F'))
3082 val = val * 16 + (CUR - 'A') + 10;
3083 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003084 htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
3085 "htmlParseCharRef: invalid hexadecimal value\n",
3086 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003087 return(0);
3088 }
3089 NEXT;
3090 }
3091 if (CUR == ';')
3092 NEXT;
3093 } else if ((CUR == '&') && (NXT(1) == '#')) {
3094 SKIP(2);
3095 while (CUR != ';') {
3096 if ((CUR >= '0') && (CUR <= '9'))
3097 val = val * 10 + (CUR - '0');
3098 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003099 htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
3100 "htmlParseCharRef: invalid decimal value\n",
3101 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003102 return(0);
3103 }
3104 NEXT;
3105 }
3106 if (CUR == ';')
3107 NEXT;
3108 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003109 htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
3110 "htmlParseCharRef: invalid value\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003111 }
3112 /*
3113 * Check the value IS_CHAR ...
3114 */
3115 if (IS_CHAR(val)) {
3116 return(val);
3117 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003118 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3119 "htmlParseCharRef: invalid xmlChar value %d\n",
3120 val);
Owen Taylor3473f882001-02-23 17:55:21 +00003121 }
3122 return(0);
3123}
3124
3125
3126/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003127 * htmlParseDocTypeDecl:
Owen Taylor3473f882001-02-23 17:55:21 +00003128 * @ctxt: an HTML parser context
3129 *
3130 * parse a DOCTYPE declaration
3131 *
3132 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3133 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3134 */
3135
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003136static void
Owen Taylor3473f882001-02-23 17:55:21 +00003137htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003138 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003139 xmlChar *ExternalID = NULL;
3140 xmlChar *URI = NULL;
3141
3142 /*
3143 * We know that '<!DOCTYPE' has been detected.
3144 */
3145 SKIP(9);
3146
3147 SKIP_BLANKS;
3148
3149 /*
3150 * Parse the DOCTYPE name.
3151 */
3152 name = htmlParseName(ctxt);
3153 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003154 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3155 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3156 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003157 }
3158 /*
3159 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3160 */
3161
3162 SKIP_BLANKS;
3163
3164 /*
3165 * Check for SystemID and ExternalID
3166 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003167 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003168 SKIP_BLANKS;
3169
3170 /*
3171 * We should be at the end of the DOCTYPE declaration.
3172 */
3173 if (CUR != '>') {
Daniel Veillardf403d292003-10-05 13:51:35 +00003174 htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
3175 "DOCTYPE improperly terminated\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003176 /* We shouldn't try to resynchronize ... */
3177 }
3178 NEXT;
3179
3180 /*
3181 * Create or update the document accordingly to the DOCTYPE
3182 */
3183 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3184 (!ctxt->disableSAX))
3185 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3186
3187 /*
3188 * Cleanup, since we don't use all those identifiers
3189 */
3190 if (URI != NULL) xmlFree(URI);
3191 if (ExternalID != NULL) xmlFree(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003192}
3193
3194/**
3195 * htmlParseAttribute:
3196 * @ctxt: an HTML parser context
3197 * @value: a xmlChar ** used to store the value of the attribute
3198 *
3199 * parse an attribute
3200 *
3201 * [41] Attribute ::= Name Eq AttValue
3202 *
3203 * [25] Eq ::= S? '=' S?
3204 *
3205 * With namespace:
3206 *
3207 * [NS 11] Attribute ::= QName Eq AttValue
3208 *
3209 * Also the case QName == xmlns:??? is handled independently as a namespace
3210 * definition.
3211 *
3212 * Returns the attribute name, and the value in *value.
3213 */
3214
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003215static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003216htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003217 const xmlChar *name;
3218 xmlChar *val = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003219
3220 *value = NULL;
3221 name = htmlParseHTMLName(ctxt);
3222 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003223 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3224 "error parsing attribute name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003225 return(NULL);
3226 }
3227
3228 /*
3229 * read the value
3230 */
3231 SKIP_BLANKS;
3232 if (CUR == '=') {
3233 NEXT;
3234 SKIP_BLANKS;
3235 val = htmlParseAttValue(ctxt);
3236 /******
3237 } else {
3238 * TODO : some attribute must have values, some may not
3239 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3240 ctxt->sax->warning(ctxt->userData,
3241 "No value for attribute %s\n", name); */
3242 }
3243
3244 *value = val;
3245 return(name);
3246}
3247
3248/**
3249 * htmlCheckEncoding:
3250 * @ctxt: an HTML parser context
3251 * @attvalue: the attribute value
3252 *
3253 * Checks an http-equiv attribute from a Meta tag to detect
3254 * the encoding
3255 * If a new encoding is detected the parser is switched to decode
3256 * it and pass UTF8
3257 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003258static void
Owen Taylor3473f882001-02-23 17:55:21 +00003259htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3260 const xmlChar *encoding;
3261
3262 if ((ctxt == NULL) || (attvalue == NULL))
3263 return;
3264
3265 /* do not change encoding */
3266 if (ctxt->input->encoding != NULL)
3267 return;
3268
3269 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
3270 if (encoding != NULL) {
3271 encoding += 8;
3272 } else {
3273 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
3274 if (encoding != NULL)
3275 encoding += 9;
3276 }
3277 if (encoding != NULL) {
3278 xmlCharEncoding enc;
3279 xmlCharEncodingHandlerPtr handler;
3280
3281 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3282
3283 if (ctxt->input->encoding != NULL)
3284 xmlFree((xmlChar *) ctxt->input->encoding);
3285 ctxt->input->encoding = xmlStrdup(encoding);
3286
3287 enc = xmlParseCharEncoding((const char *) encoding);
3288 /*
3289 * registered set of known encodings
3290 */
3291 if (enc != XML_CHAR_ENCODING_ERROR) {
3292 xmlSwitchEncoding(ctxt, enc);
3293 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3294 } else {
3295 /*
3296 * fallback for unknown encodings
3297 */
3298 handler = xmlFindCharEncodingHandler((const char *) encoding);
3299 if (handler != NULL) {
3300 xmlSwitchToEncoding(ctxt, handler);
3301 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3302 } else {
3303 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3304 }
3305 }
3306
3307 if ((ctxt->input->buf != NULL) &&
3308 (ctxt->input->buf->encoder != NULL) &&
3309 (ctxt->input->buf->raw != NULL) &&
3310 (ctxt->input->buf->buffer != NULL)) {
3311 int nbchars;
3312 int processed;
3313
3314 /*
3315 * convert as much as possible to the parser reading buffer.
3316 */
3317 processed = ctxt->input->cur - ctxt->input->base;
3318 xmlBufferShrink(ctxt->input->buf->buffer, processed);
3319 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
3320 ctxt->input->buf->buffer,
3321 ctxt->input->buf->raw);
3322 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003323 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3324 "htmlCheckEncoding: encoder error\n",
3325 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003326 }
3327 ctxt->input->base =
3328 ctxt->input->cur = ctxt->input->buf->buffer->content;
3329 }
3330 }
3331}
3332
3333/**
3334 * htmlCheckMeta:
3335 * @ctxt: an HTML parser context
3336 * @atts: the attributes values
3337 *
3338 * Checks an attributes from a Meta tag
3339 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003340static void
Owen Taylor3473f882001-02-23 17:55:21 +00003341htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3342 int i;
3343 const xmlChar *att, *value;
3344 int http = 0;
3345 const xmlChar *content = NULL;
3346
3347 if ((ctxt == NULL) || (atts == NULL))
3348 return;
3349
3350 i = 0;
3351 att = atts[i++];
3352 while (att != NULL) {
3353 value = atts[i++];
3354 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3355 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3356 http = 1;
3357 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3358 content = value;
3359 att = atts[i++];
3360 }
3361 if ((http) && (content != NULL))
3362 htmlCheckEncoding(ctxt, content);
3363
3364}
3365
3366/**
3367 * htmlParseStartTag:
3368 * @ctxt: an HTML parser context
3369 *
3370 * parse a start of tag either for rule element or
3371 * EmptyElement. In both case we don't parse the tag closing chars.
3372 *
3373 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3374 *
3375 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3376 *
3377 * With namespace:
3378 *
3379 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3380 *
3381 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3382 *
Daniel Veillard597f1c12005-07-03 23:00:18 +00003383 * Returns 0 in case of success and -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003384 */
3385
Daniel Veillard597f1c12005-07-03 23:00:18 +00003386static int
Owen Taylor3473f882001-02-23 17:55:21 +00003387htmlParseStartTag(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003388 const xmlChar *name;
3389 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00003390 xmlChar *attvalue;
Daniel Veillardf403d292003-10-05 13:51:35 +00003391 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00003392 int nbatts = 0;
Daniel Veillardf403d292003-10-05 13:51:35 +00003393 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003394 int meta = 0;
3395 int i;
3396
Daniel Veillarda03e3652004-11-02 18:45:30 +00003397 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3398 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3399 "htmlParseStartTag: context error\n", NULL, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003400 return -1;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003401 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003402 if (CUR != '<') return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003403 NEXT;
3404
3405 GROW;
3406 name = htmlParseHTMLName(ctxt);
3407 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003408 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3409 "htmlParseStartTag: invalid element name\n",
3410 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003411 /* Dump the bogus tag like browsers do */
William M. Brack76e95df2003-10-18 16:20:14 +00003412 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
Owen Taylor3473f882001-02-23 17:55:21 +00003413 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003414 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003415 }
3416 if (xmlStrEqual(name, BAD_CAST"meta"))
3417 meta = 1;
3418
3419 /*
3420 * Check for auto-closure of HTML elements.
3421 */
3422 htmlAutoClose(ctxt, name);
3423
3424 /*
3425 * Check for implied HTML elements.
3426 */
3427 htmlCheckImplied(ctxt, name);
3428
3429 /*
3430 * Avoid html at any level > 0, head at any level != 1
3431 * or any attempt to recurse body
3432 */
3433 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003434 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3435 "htmlParseStartTag: misplaced <html> tag\n",
3436 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003437 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003438 }
3439 if ((ctxt->nameNr != 1) &&
3440 (xmlStrEqual(name, BAD_CAST"head"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003441 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3442 "htmlParseStartTag: misplaced <head> tag\n",
3443 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003444 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003445 }
3446 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003447 int indx;
3448 for (indx = 0;indx < ctxt->nameNr;indx++) {
3449 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003450 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3451 "htmlParseStartTag: misplaced <body> tag\n",
3452 name, NULL);
Daniel Veillardc59d8262003-11-20 21:59:12 +00003453 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
3454 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003455 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003456 }
3457 }
3458 }
3459
3460 /*
3461 * Now parse the attributes, it ends up with the ending
3462 *
3463 * (S Attribute)* S?
3464 */
3465 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003466 while ((IS_CHAR_CH(CUR)) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003467 (CUR != '>') &&
3468 ((CUR != '/') || (NXT(1) != '>'))) {
3469 long cons = ctxt->nbChars;
3470
3471 GROW;
3472 attname = htmlParseAttribute(ctxt, &attvalue);
3473 if (attname != NULL) {
3474
3475 /*
3476 * Well formedness requires at most one declaration of an attribute
3477 */
3478 for (i = 0; i < nbatts;i += 2) {
3479 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003480 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
3481 "Attribute %s redefined\n", attname, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003482 if (attvalue != NULL)
3483 xmlFree(attvalue);
3484 goto failed;
3485 }
3486 }
3487
3488 /*
3489 * Add the pair to atts
3490 */
3491 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003492 maxatts = 22; /* allow for 10 attrs by default */
3493 atts = (const xmlChar **)
3494 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003495 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003496 htmlErrMemory(ctxt, NULL);
3497 if (attvalue != NULL)
3498 xmlFree(attvalue);
3499 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003500 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003501 ctxt->atts = atts;
3502 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003503 } else if (nbatts + 4 > maxatts) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003504 const xmlChar **n;
3505
Owen Taylor3473f882001-02-23 17:55:21 +00003506 maxatts *= 2;
Daniel Veillardf403d292003-10-05 13:51:35 +00003507 n = (const xmlChar **) xmlRealloc((void *) atts,
3508 maxatts * sizeof(const xmlChar *));
3509 if (n == NULL) {
3510 htmlErrMemory(ctxt, NULL);
3511 if (attvalue != NULL)
3512 xmlFree(attvalue);
3513 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003514 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003515 atts = n;
3516 ctxt->atts = atts;
3517 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003518 }
3519 atts[nbatts++] = attname;
3520 atts[nbatts++] = attvalue;
3521 atts[nbatts] = NULL;
3522 atts[nbatts + 1] = NULL;
3523 }
3524 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003525 if (attvalue != NULL)
3526 xmlFree(attvalue);
Owen Taylor3473f882001-02-23 17:55:21 +00003527 /* Dump the bogus attribute string up to the next blank or
3528 * the end of the tag. */
William M. Brack76e95df2003-10-18 16:20:14 +00003529 while ((IS_CHAR_CH(CUR)) &&
3530 !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
Daniel Veillard34ba3872003-07-15 13:34:05 +00003531 ((CUR != '/') || (NXT(1) != '>')))
Owen Taylor3473f882001-02-23 17:55:21 +00003532 NEXT;
3533 }
3534
3535failed:
3536 SKIP_BLANKS;
3537 if (cons == ctxt->nbChars) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003538 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3539 "htmlParseStartTag: problem parsing attributes\n",
3540 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003541 break;
3542 }
3543 }
3544
3545 /*
3546 * Handle specific association to the META tag
3547 */
3548 if (meta)
3549 htmlCheckMeta(ctxt, atts);
3550
3551 /*
3552 * SAX: Start of Element !
3553 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003554 htmlnamePush(ctxt, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003555 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
3556 if (nbatts != 0)
3557 ctxt->sax->startElement(ctxt->userData, name, atts);
3558 else
3559 ctxt->sax->startElement(ctxt->userData, name, NULL);
3560 }
Owen Taylor3473f882001-02-23 17:55:21 +00003561
3562 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003563 for (i = 1;i < nbatts;i += 2) {
Owen Taylor3473f882001-02-23 17:55:21 +00003564 if (atts[i] != NULL)
3565 xmlFree((xmlChar *) atts[i]);
3566 }
Owen Taylor3473f882001-02-23 17:55:21 +00003567 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003568
3569 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003570}
3571
3572/**
3573 * htmlParseEndTag:
3574 * @ctxt: an HTML parser context
3575 *
3576 * parse an end of tag
3577 *
3578 * [42] ETag ::= '</' Name S? '>'
3579 *
3580 * With namespace
3581 *
3582 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003583 *
3584 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003585 */
3586
Daniel Veillardf420ac52001-07-04 16:04:09 +00003587static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003588htmlParseEndTag(htmlParserCtxtPtr ctxt)
3589{
3590 const xmlChar *name;
3591 const xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003592 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003593
3594 if ((CUR != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003595 htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
3596 "htmlParseEndTag: '</' not found\n", NULL, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003597 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003598 }
3599 SKIP(2);
3600
3601 name = htmlParseHTMLName(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003602 if (name == NULL)
3603 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003604
3605 /*
3606 * We should definitely be at the ending "S? '>'" part
3607 */
3608 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003609 if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003610 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3611 "End tag : expected '>'\n", NULL, NULL);
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00003612 if (ctxt->recovery) {
3613 /*
3614 * We're not at the ending > !!
3615 * Error, unless in recover mode where we search forwards
3616 * until we find a >
3617 */
3618 while (CUR != '\0' && CUR != '>') NEXT;
3619 NEXT;
3620 }
Owen Taylor3473f882001-02-23 17:55:21 +00003621 } else
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003622 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003623
3624 /*
3625 * If the name read is not one of the element in the parsing stack
3626 * then return, it's just an error.
3627 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003628 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
3629 if (xmlStrEqual(name, ctxt->nameTab[i]))
3630 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003631 }
3632 if (i < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003633 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3634 "Unexpected end tag : %s\n", name, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003635 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003636 }
3637
3638
3639 /*
3640 * Check for auto-closure of HTML elements.
3641 */
3642
3643 htmlAutoCloseOnClose(ctxt, name);
3644
3645 /*
3646 * Well formedness constraints, opening and closing must match.
3647 * With the exception that the autoclose may have popped stuff out
3648 * of the stack.
3649 */
3650 if (!xmlStrEqual(name, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003651 if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003652 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3653 "Opening and ending tag mismatch: %s and %s\n",
3654 name, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003655 }
3656 }
3657
3658 /*
3659 * SAX: End of Tag
3660 */
3661 oldname = ctxt->name;
3662 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003663 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3664 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003665 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003666 ret = 1;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003667 } else {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003668 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003669 }
3670
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003671 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003672}
3673
3674
3675/**
3676 * htmlParseReference:
3677 * @ctxt: an HTML parser context
3678 *
3679 * parse and handle entity references in content,
3680 * this will end-up in a call to character() since this is either a
3681 * CharRef, or a predefined entity.
3682 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003683static void
Owen Taylor3473f882001-02-23 17:55:21 +00003684htmlParseReference(htmlParserCtxtPtr ctxt) {
Daniel Veillardbb371292001-08-16 23:26:59 +00003685 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00003686 xmlChar out[6];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003687 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003688 if (CUR != '&') return;
3689
3690 if (NXT(1) == '#') {
3691 unsigned int c;
3692 int bits, i = 0;
3693
3694 c = htmlParseCharRef(ctxt);
3695 if (c == 0)
3696 return;
3697
3698 if (c < 0x80) { out[i++]= c; bits= -6; }
3699 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3700 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3701 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3702
3703 for ( ; bits >= 0; bits-= 6) {
3704 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3705 }
3706 out[i] = 0;
3707
3708 htmlCheckParagraph(ctxt);
3709 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3710 ctxt->sax->characters(ctxt->userData, out, i);
3711 } else {
3712 ent = htmlParseEntityRef(ctxt, &name);
3713 if (name == NULL) {
3714 htmlCheckParagraph(ctxt);
3715 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3716 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3717 return;
3718 }
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003719 if ((ent == NULL) || !(ent->value > 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003720 htmlCheckParagraph(ctxt);
3721 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3722 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3723 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3724 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3725 }
3726 } else {
3727 unsigned int c;
3728 int bits, i = 0;
3729
3730 c = ent->value;
3731 if (c < 0x80)
3732 { out[i++]= c; bits= -6; }
3733 else if (c < 0x800)
3734 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3735 else if (c < 0x10000)
3736 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3737 else
3738 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3739
3740 for ( ; bits >= 0; bits-= 6) {
3741 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3742 }
3743 out[i] = 0;
3744
3745 htmlCheckParagraph(ctxt);
3746 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3747 ctxt->sax->characters(ctxt->userData, out, i);
3748 }
Owen Taylor3473f882001-02-23 17:55:21 +00003749 }
3750}
3751
3752/**
3753 * htmlParseContent:
3754 * @ctxt: an HTML parser context
3755 * @name: the node name
3756 *
3757 * Parse a content: comment, sub-element, reference or text.
3758 *
3759 */
3760
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003761static void
Owen Taylor3473f882001-02-23 17:55:21 +00003762htmlParseContent(htmlParserCtxtPtr ctxt) {
3763 xmlChar *currentNode;
3764 int depth;
3765
3766 currentNode = xmlStrdup(ctxt->name);
3767 depth = ctxt->nameNr;
3768 while (1) {
3769 long cons = ctxt->nbChars;
3770
3771 GROW;
3772 /*
3773 * Our tag or one of it's parent or children is ending.
3774 */
3775 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003776 if (htmlParseEndTag(ctxt) &&
3777 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3778 if (currentNode != NULL)
3779 xmlFree(currentNode);
3780 return;
3781 }
3782 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003783 }
3784
3785 /*
3786 * Has this node been popped out during parsing of
3787 * the next element
3788 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003789 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3790 (!xmlStrEqual(currentNode, ctxt->name)))
3791 {
Owen Taylor3473f882001-02-23 17:55:21 +00003792 if (currentNode != NULL) xmlFree(currentNode);
3793 return;
3794 }
3795
Daniel Veillardf9533d12001-03-03 10:04:57 +00003796 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3797 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003798 /*
3799 * Handle SCRIPT/STYLE separately
3800 */
3801 htmlParseScript(ctxt);
3802 } else {
3803 /*
3804 * Sometimes DOCTYPE arrives in the middle of the document
3805 */
3806 if ((CUR == '<') && (NXT(1) == '!') &&
3807 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3808 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3809 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3810 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003811 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3812 "Misplaced DOCTYPE declaration\n",
3813 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003814 htmlParseDocTypeDecl(ctxt);
3815 }
3816
3817 /*
3818 * First case : a comment
3819 */
3820 if ((CUR == '<') && (NXT(1) == '!') &&
3821 (NXT(2) == '-') && (NXT(3) == '-')) {
3822 htmlParseComment(ctxt);
3823 }
3824
3825 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003826 * Second case : a Processing Instruction.
3827 */
3828 else if ((CUR == '<') && (NXT(1) == '?')) {
3829 htmlParsePI(ctxt);
3830 }
3831
3832 /*
3833 * Third case : a sub-element.
Owen Taylor3473f882001-02-23 17:55:21 +00003834 */
3835 else if (CUR == '<') {
3836 htmlParseElement(ctxt);
3837 }
3838
3839 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003840 * Fourth case : a reference. If if has not been resolved,
Owen Taylor3473f882001-02-23 17:55:21 +00003841 * parsing returns it's Name, create the node
3842 */
3843 else if (CUR == '&') {
3844 htmlParseReference(ctxt);
3845 }
3846
3847 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003848 * Fifth case : end of the resource
Owen Taylor3473f882001-02-23 17:55:21 +00003849 */
3850 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003851 htmlAutoCloseOnEnd(ctxt);
3852 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003853 }
3854
3855 /*
3856 * Last case, text. Note that References are handled directly.
3857 */
3858 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003859 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003860 }
3861
3862 if (cons == ctxt->nbChars) {
3863 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003864 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3865 "detected an error in element content\n",
3866 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003867 }
3868 break;
3869 }
3870 }
3871 GROW;
3872 }
3873 if (currentNode != NULL) xmlFree(currentNode);
3874}
3875
3876/**
3877 * htmlParseElement:
3878 * @ctxt: an HTML parser context
3879 *
3880 * parse an HTML element, this is highly recursive
3881 *
3882 * [39] element ::= EmptyElemTag | STag content ETag
3883 *
3884 * [41] Attribute ::= Name Eq AttValue
3885 */
3886
3887void
3888htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003889 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003890 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00003891 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00003892 htmlParserNodeInfo node_info;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003893 int failed;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003894 int depth;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00003895 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00003896
Daniel Veillarda03e3652004-11-02 18:45:30 +00003897 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3898 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard597f1c12005-07-03 23:00:18 +00003899 "htmlParseElement: context error\n", NULL, NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003900 return;
3901 }
Owen Taylor3473f882001-02-23 17:55:21 +00003902 /* Capture start position */
3903 if (ctxt->record_info) {
3904 node_info.begin_pos = ctxt->input->consumed +
3905 (CUR_PTR - ctxt->input->base);
3906 node_info.begin_line = ctxt->input->line;
3907 }
3908
Daniel Veillard597f1c12005-07-03 23:00:18 +00003909 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003910 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003911 if (failed || (name == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003912 if (CUR == '>')
3913 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003914 return;
3915 }
Owen Taylor3473f882001-02-23 17:55:21 +00003916
3917 /*
3918 * Lookup the info for that element.
3919 */
3920 info = htmlTagLookup(name);
3921 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003922 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
3923 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003924 }
3925
3926 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003927 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00003928 */
3929 if ((CUR == '/') && (NXT(1) == '>')) {
3930 SKIP(2);
3931 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3932 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003933 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003934 return;
3935 }
3936
3937 if (CUR == '>') {
3938 NEXT;
3939 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003940 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3941 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003942
3943 /*
3944 * end of parsing of this node.
3945 */
3946 if (xmlStrEqual(name, ctxt->name)) {
3947 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00003948 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003949 }
3950
3951 /*
3952 * Capture end position and add node
3953 */
3954 if ( currentNode != NULL && ctxt->record_info ) {
3955 node_info.end_pos = ctxt->input->consumed +
3956 (CUR_PTR - ctxt->input->base);
3957 node_info.end_line = ctxt->input->line;
3958 node_info.node = ctxt->node;
3959 xmlParserAddNodeInfo(ctxt, &node_info);
3960 }
3961 return;
3962 }
3963
3964 /*
3965 * Check for an Empty Element from DTD definition
3966 */
3967 if ((info != NULL) && (info->empty)) {
3968 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3969 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003970 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003971 return;
3972 }
3973
3974 /*
3975 * Parse the content of the element:
3976 */
3977 currentNode = xmlStrdup(ctxt->name);
3978 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00003979 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00003980 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00003981 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00003982 if (oldptr==ctxt->input->cur) break;
Owen Taylor3473f882001-02-23 17:55:21 +00003983 if (ctxt->nameNr < depth) break;
3984 }
3985
Owen Taylor3473f882001-02-23 17:55:21 +00003986 /*
3987 * Capture end position and add node
3988 */
3989 if ( currentNode != NULL && ctxt->record_info ) {
3990 node_info.end_pos = ctxt->input->consumed +
3991 (CUR_PTR - ctxt->input->base);
3992 node_info.end_line = ctxt->input->line;
3993 node_info.node = ctxt->node;
3994 xmlParserAddNodeInfo(ctxt, &node_info);
3995 }
William M. Brack76e95df2003-10-18 16:20:14 +00003996 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003997 htmlAutoCloseOnEnd(ctxt);
3998 }
3999
Owen Taylor3473f882001-02-23 17:55:21 +00004000 if (currentNode != NULL)
4001 xmlFree(currentNode);
4002}
4003
4004/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004005 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00004006 * @ctxt: an HTML parser context
4007 *
4008 * parse an HTML document (and build a tree if using the standard SAX
4009 * interface).
4010 *
4011 * Returns 0, -1 in case of error. the parser context is augmented
4012 * as a result of the parsing.
4013 */
4014
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00004015int
Owen Taylor3473f882001-02-23 17:55:21 +00004016htmlParseDocument(htmlParserCtxtPtr ctxt) {
4017 xmlDtdPtr dtd;
4018
Daniel Veillardd0463562001-10-13 09:15:48 +00004019 xmlInitParser();
4020
Owen Taylor3473f882001-02-23 17:55:21 +00004021 htmlDefaultSAXHandlerInit();
Owen Taylor3473f882001-02-23 17:55:21 +00004022
Daniel Veillarda03e3652004-11-02 18:45:30 +00004023 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4024 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4025 "htmlParseDocument: context error\n", NULL, NULL);
4026 return(XML_ERR_INTERNAL_ERROR);
4027 }
4028 ctxt->html = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004029 GROW;
4030 /*
4031 * SAX: beginning of the document processing.
4032 */
4033 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4034 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4035
4036 /*
4037 * Wipe out everything which is before the first '<'
4038 */
4039 SKIP_BLANKS;
4040 if (CUR == 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004041 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
4042 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004043 }
4044
4045 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4046 ctxt->sax->startDocument(ctxt->userData);
4047
4048
4049 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004050 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004051 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004052 while (((CUR == '<') && (NXT(1) == '!') &&
4053 (NXT(2) == '-') && (NXT(3) == '-')) ||
4054 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004055 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004056 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004057 SKIP_BLANKS;
4058 }
4059
4060
4061 /*
4062 * Then possibly doc type declaration(s) and more Misc
4063 * (doctypedecl Misc*)?
4064 */
4065 if ((CUR == '<') && (NXT(1) == '!') &&
4066 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4067 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4068 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4069 (UPP(8) == 'E')) {
4070 htmlParseDocTypeDecl(ctxt);
4071 }
4072 SKIP_BLANKS;
4073
4074 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004075 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004076 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004077 while (((CUR == '<') && (NXT(1) == '!') &&
4078 (NXT(2) == '-') && (NXT(3) == '-')) ||
4079 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004080 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004081 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004082 SKIP_BLANKS;
4083 }
4084
4085 /*
4086 * Time to start parsing the tree itself
4087 */
4088 htmlParseContent(ctxt);
4089
4090 /*
4091 * autoclose
4092 */
4093 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004094 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004095
4096
4097 /*
4098 * SAX: end of the document processing.
4099 */
4100 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4101 ctxt->sax->endDocument(ctxt->userData);
4102
4103 if (ctxt->myDoc != NULL) {
4104 dtd = xmlGetIntSubset(ctxt->myDoc);
4105 if (dtd == NULL)
4106 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00004107 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004108 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4109 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4110 }
4111 if (! ctxt->wellFormed) return(-1);
4112 return(0);
4113}
4114
4115
4116/************************************************************************
4117 * *
4118 * Parser contexts handling *
4119 * *
4120 ************************************************************************/
4121
4122/**
William M. Brackedb65a72004-02-06 07:36:04 +00004123 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004124 * @ctxt: an HTML parser context
4125 *
4126 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00004127 *
4128 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00004129 */
4130
Daniel Veillardf403d292003-10-05 13:51:35 +00004131static int
Owen Taylor3473f882001-02-23 17:55:21 +00004132htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
4133{
4134 htmlSAXHandler *sax;
4135
Daniel Veillardf403d292003-10-05 13:51:35 +00004136 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004137 memset(ctxt, 0, sizeof(htmlParserCtxt));
4138
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004139 ctxt->dict = xmlDictCreate();
4140 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004141 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4142 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004143 }
Owen Taylor3473f882001-02-23 17:55:21 +00004144 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
4145 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004146 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4147 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004148 }
4149 else
4150 memset(sax, 0, sizeof(htmlSAXHandler));
4151
4152 /* Allocate the Input stack */
4153 ctxt->inputTab = (htmlParserInputPtr *)
4154 xmlMalloc(5 * sizeof(htmlParserInputPtr));
4155 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004156 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004157 ctxt->inputNr = 0;
4158 ctxt->inputMax = 0;
4159 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004160 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004161 }
4162 ctxt->inputNr = 0;
4163 ctxt->inputMax = 5;
4164 ctxt->input = NULL;
4165 ctxt->version = NULL;
4166 ctxt->encoding = NULL;
4167 ctxt->standalone = -1;
4168 ctxt->instate = XML_PARSER_START;
4169
4170 /* Allocate the Node stack */
4171 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
4172 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004173 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004174 ctxt->nodeNr = 0;
4175 ctxt->nodeMax = 0;
4176 ctxt->node = NULL;
4177 ctxt->inputNr = 0;
4178 ctxt->inputMax = 0;
4179 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004180 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004181 }
4182 ctxt->nodeNr = 0;
4183 ctxt->nodeMax = 10;
4184 ctxt->node = NULL;
4185
4186 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004187 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00004188 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004189 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004190 ctxt->nameNr = 0;
4191 ctxt->nameMax = 10;
4192 ctxt->name = NULL;
4193 ctxt->nodeNr = 0;
4194 ctxt->nodeMax = 0;
4195 ctxt->node = NULL;
4196 ctxt->inputNr = 0;
4197 ctxt->inputMax = 0;
4198 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004199 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004200 }
4201 ctxt->nameNr = 0;
4202 ctxt->nameMax = 10;
4203 ctxt->name = NULL;
4204
Daniel Veillard092643b2003-09-25 14:29:29 +00004205 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00004206 else {
4207 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00004208 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00004209 }
4210 ctxt->userData = ctxt;
4211 ctxt->myDoc = NULL;
4212 ctxt->wellFormed = 1;
4213 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00004214 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00004215 ctxt->html = 1;
Daniel Veillardeff45a92004-10-29 12:10:55 +00004216 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
William M. Brackedb65a72004-02-06 07:36:04 +00004217 ctxt->vctxt.userData = ctxt;
4218 ctxt->vctxt.error = xmlParserValidityError;
4219 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00004220 ctxt->record_info = 0;
4221 ctxt->validate = 0;
4222 ctxt->nbChars = 0;
4223 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00004224 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004225 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00004226 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004227}
4228
4229/**
4230 * htmlFreeParserCtxt:
4231 * @ctxt: an HTML parser context
4232 *
4233 * Free all the memory used by a parser context. However the parsed
4234 * document in ctxt->myDoc is not freed.
4235 */
4236
4237void
4238htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4239{
4240 xmlFreeParserCtxt(ctxt);
4241}
4242
4243/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004244 * htmlNewParserCtxt:
4245 *
4246 * Allocate and initialize a new parser context.
4247 *
4248 * Returns the xmlParserCtxtPtr or NULL
4249 */
4250
4251static htmlParserCtxtPtr
4252htmlNewParserCtxt(void)
4253{
4254 xmlParserCtxtPtr ctxt;
4255
4256 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4257 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004258 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004259 return(NULL);
4260 }
4261 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004262 if (htmlInitParserCtxt(ctxt) < 0) {
4263 htmlFreeParserCtxt(ctxt);
4264 return(NULL);
4265 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004266 return(ctxt);
4267}
4268
4269/**
4270 * htmlCreateMemoryParserCtxt:
4271 * @buffer: a pointer to a char array
4272 * @size: the size of the array
4273 *
4274 * Create a parser context for an HTML in-memory document.
4275 *
4276 * Returns the new parser context or NULL
4277 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004278htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004279htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4280 xmlParserCtxtPtr ctxt;
4281 xmlParserInputPtr input;
4282 xmlParserInputBufferPtr buf;
4283
4284 if (buffer == NULL)
4285 return(NULL);
4286 if (size <= 0)
4287 return(NULL);
4288
4289 ctxt = htmlNewParserCtxt();
4290 if (ctxt == NULL)
4291 return(NULL);
4292
4293 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4294 if (buf == NULL) return(NULL);
4295
4296 input = xmlNewInputStream(ctxt);
4297 if (input == NULL) {
4298 xmlFreeParserCtxt(ctxt);
4299 return(NULL);
4300 }
4301
4302 input->filename = NULL;
4303 input->buf = buf;
4304 input->base = input->buf->buffer->content;
4305 input->cur = input->buf->buffer->content;
4306 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4307
4308 inputPush(ctxt, input);
4309 return(ctxt);
4310}
4311
4312/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004313 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004314 * @cur: a pointer to an array of xmlChar
4315 * @encoding: a free form C string describing the HTML document encoding, or NULL
4316 *
4317 * Create a parser context for an HTML document.
4318 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004319 * TODO: check the need to add encoding handling there
4320 *
Owen Taylor3473f882001-02-23 17:55:21 +00004321 * Returns the new parser context or NULL
4322 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004323static htmlParserCtxtPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004324htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004325 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004326 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004327
Daniel Veillard1d995272002-07-22 16:43:32 +00004328 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004329 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004330 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004331 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
4332
4333 if (encoding != NULL) {
4334 xmlCharEncoding enc;
4335 xmlCharEncodingHandlerPtr handler;
4336
4337 if (ctxt->input->encoding != NULL)
4338 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004339 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004340
4341 enc = xmlParseCharEncoding(encoding);
4342 /*
4343 * registered set of known encodings
4344 */
4345 if (enc != XML_CHAR_ENCODING_ERROR) {
4346 xmlSwitchEncoding(ctxt, enc);
4347 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004348 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4349 "Unsupported encoding %s\n",
4350 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004351 }
4352 } else {
4353 /*
4354 * fallback for unknown encodings
4355 */
4356 handler = xmlFindCharEncodingHandler((const char *) encoding);
4357 if (handler != NULL) {
4358 xmlSwitchToEncoding(ctxt, handler);
4359 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004360 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4361 "Unsupported encoding %s\n",
4362 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004363 }
4364 }
4365 }
4366 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004367}
4368
Daniel Veillard73b013f2003-09-30 12:36:01 +00004369#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004370/************************************************************************
4371 * *
4372 * Progressive parsing interfaces *
4373 * *
4374 ************************************************************************/
4375
4376/**
4377 * htmlParseLookupSequence:
4378 * @ctxt: an HTML parser context
4379 * @first: the first char to lookup
4380 * @next: the next char to lookup or zero
4381 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004382 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004383 *
4384 * Try to find if a sequence (first, next, third) or just (first next) or
4385 * (first) is available in the input stream.
4386 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4387 * to avoid rescanning sequences of bytes, it DOES change the state of the
4388 * parser, do not use liberally.
4389 * This is basically similar to xmlParseLookupSequence()
4390 *
4391 * Returns the index to the current parsing point if the full sequence
4392 * is available, -1 otherwise.
4393 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004394static int
Owen Taylor3473f882001-02-23 17:55:21 +00004395htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
William M. Brackc1939562003-08-05 15:52:22 +00004396 xmlChar next, xmlChar third, int iscomment) {
Owen Taylor3473f882001-02-23 17:55:21 +00004397 int base, len;
4398 htmlParserInputPtr in;
4399 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004400 int incomment = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004401
4402 in = ctxt->input;
4403 if (in == NULL) return(-1);
4404 base = in->cur - in->base;
4405 if (base < 0) return(-1);
4406 if (ctxt->checkIndex > base)
4407 base = ctxt->checkIndex;
4408 if (in->buf == NULL) {
4409 buf = in->base;
4410 len = in->length;
4411 } else {
4412 buf = in->buf->buffer->content;
4413 len = in->buf->buffer->use;
4414 }
4415 /* take into account the sequence length */
4416 if (third) len -= 2;
4417 else if (next) len --;
4418 for (;base < len;base++) {
William M. Brackc1939562003-08-05 15:52:22 +00004419 if (!incomment && (base + 4 < len) && !iscomment) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00004420 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
4421 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
4422 incomment = 1;
Daniel Veillard97e01882003-07-30 18:59:19 +00004423 /* do not increment past <! - some people use <!--> */
4424 base += 2;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004425 }
Daniel Veillardc1f78342001-11-10 11:43:05 +00004426 }
4427 if (incomment) {
William M. Brack4a557d92003-07-29 04:28:04 +00004428 if (base + 3 > len)
Daniel Veillardc1f78342001-11-10 11:43:05 +00004429 return(-1);
4430 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
4431 (buf[base + 2] == '>')) {
4432 incomment = 0;
4433 base += 2;
4434 }
4435 continue;
4436 }
Owen Taylor3473f882001-02-23 17:55:21 +00004437 if (buf[base] == first) {
4438 if (third != 0) {
4439 if ((buf[base + 1] != next) ||
4440 (buf[base + 2] != third)) continue;
4441 } else if (next != 0) {
4442 if (buf[base + 1] != next) continue;
4443 }
4444 ctxt->checkIndex = 0;
4445#ifdef DEBUG_PUSH
4446 if (next == 0)
4447 xmlGenericError(xmlGenericErrorContext,
4448 "HPP: lookup '%c' found at %d\n",
4449 first, base);
4450 else if (third == 0)
4451 xmlGenericError(xmlGenericErrorContext,
4452 "HPP: lookup '%c%c' found at %d\n",
4453 first, next, base);
4454 else
4455 xmlGenericError(xmlGenericErrorContext,
4456 "HPP: lookup '%c%c%c' found at %d\n",
4457 first, next, third, base);
4458#endif
4459 return(base - (in->cur - in->base));
4460 }
4461 }
4462 ctxt->checkIndex = base;
4463#ifdef DEBUG_PUSH
4464 if (next == 0)
4465 xmlGenericError(xmlGenericErrorContext,
4466 "HPP: lookup '%c' failed\n", first);
4467 else if (third == 0)
4468 xmlGenericError(xmlGenericErrorContext,
4469 "HPP: lookup '%c%c' failed\n", first, next);
4470 else
4471 xmlGenericError(xmlGenericErrorContext,
4472 "HPP: lookup '%c%c%c' failed\n", first, next, third);
4473#endif
4474 return(-1);
4475}
4476
4477/**
4478 * htmlParseTryOrFinish:
4479 * @ctxt: an HTML parser context
4480 * @terminate: last chunk indicator
4481 *
4482 * Try to progress on parsing
4483 *
4484 * Returns zero if no parsing was possible
4485 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004486static int
Owen Taylor3473f882001-02-23 17:55:21 +00004487htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
4488 int ret = 0;
4489 htmlParserInputPtr in;
4490 int avail = 0;
4491 xmlChar cur, next;
4492
4493#ifdef DEBUG_PUSH
4494 switch (ctxt->instate) {
4495 case XML_PARSER_EOF:
4496 xmlGenericError(xmlGenericErrorContext,
4497 "HPP: try EOF\n"); break;
4498 case XML_PARSER_START:
4499 xmlGenericError(xmlGenericErrorContext,
4500 "HPP: try START\n"); break;
4501 case XML_PARSER_MISC:
4502 xmlGenericError(xmlGenericErrorContext,
4503 "HPP: try MISC\n");break;
4504 case XML_PARSER_COMMENT:
4505 xmlGenericError(xmlGenericErrorContext,
4506 "HPP: try COMMENT\n");break;
4507 case XML_PARSER_PROLOG:
4508 xmlGenericError(xmlGenericErrorContext,
4509 "HPP: try PROLOG\n");break;
4510 case XML_PARSER_START_TAG:
4511 xmlGenericError(xmlGenericErrorContext,
4512 "HPP: try START_TAG\n");break;
4513 case XML_PARSER_CONTENT:
4514 xmlGenericError(xmlGenericErrorContext,
4515 "HPP: try CONTENT\n");break;
4516 case XML_PARSER_CDATA_SECTION:
4517 xmlGenericError(xmlGenericErrorContext,
4518 "HPP: try CDATA_SECTION\n");break;
4519 case XML_PARSER_END_TAG:
4520 xmlGenericError(xmlGenericErrorContext,
4521 "HPP: try END_TAG\n");break;
4522 case XML_PARSER_ENTITY_DECL:
4523 xmlGenericError(xmlGenericErrorContext,
4524 "HPP: try ENTITY_DECL\n");break;
4525 case XML_PARSER_ENTITY_VALUE:
4526 xmlGenericError(xmlGenericErrorContext,
4527 "HPP: try ENTITY_VALUE\n");break;
4528 case XML_PARSER_ATTRIBUTE_VALUE:
4529 xmlGenericError(xmlGenericErrorContext,
4530 "HPP: try ATTRIBUTE_VALUE\n");break;
4531 case XML_PARSER_DTD:
4532 xmlGenericError(xmlGenericErrorContext,
4533 "HPP: try DTD\n");break;
4534 case XML_PARSER_EPILOG:
4535 xmlGenericError(xmlGenericErrorContext,
4536 "HPP: try EPILOG\n");break;
4537 case XML_PARSER_PI:
4538 xmlGenericError(xmlGenericErrorContext,
4539 "HPP: try PI\n");break;
4540 case XML_PARSER_SYSTEM_LITERAL:
4541 xmlGenericError(xmlGenericErrorContext,
4542 "HPP: try SYSTEM_LITERAL\n");break;
4543 }
4544#endif
4545
4546 while (1) {
4547
4548 in = ctxt->input;
4549 if (in == NULL) break;
4550 if (in->buf == NULL)
4551 avail = in->length - (in->cur - in->base);
4552 else
4553 avail = in->buf->buffer->use - (in->cur - in->base);
4554 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004555 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004556 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4557 /*
4558 * SAX: end of the document processing.
4559 */
4560 ctxt->instate = XML_PARSER_EOF;
4561 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4562 ctxt->sax->endDocument(ctxt->userData);
4563 }
4564 }
4565 if (avail < 1)
4566 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00004567 cur = in->cur[0];
4568 if (cur == 0) {
4569 SKIP(1);
4570 continue;
4571 }
4572
Owen Taylor3473f882001-02-23 17:55:21 +00004573 switch (ctxt->instate) {
4574 case XML_PARSER_EOF:
4575 /*
4576 * Document parsing is done !
4577 */
4578 goto done;
4579 case XML_PARSER_START:
4580 /*
4581 * Very first chars read from the document flow.
4582 */
4583 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004584 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004585 SKIP_BLANKS;
4586 if (in->buf == NULL)
4587 avail = in->length - (in->cur - in->base);
4588 else
4589 avail = in->buf->buffer->use - (in->cur - in->base);
4590 }
4591 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4592 ctxt->sax->setDocumentLocator(ctxt->userData,
4593 &xmlDefaultSAXLocator);
4594 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4595 (!ctxt->disableSAX))
4596 ctxt->sax->startDocument(ctxt->userData);
4597
4598 cur = in->cur[0];
4599 next = in->cur[1];
4600 if ((cur == '<') && (next == '!') &&
4601 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4602 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4603 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4604 (UPP(8) == 'E')) {
4605 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004606 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004607 goto done;
4608#ifdef DEBUG_PUSH
4609 xmlGenericError(xmlGenericErrorContext,
4610 "HPP: Parsing internal subset\n");
4611#endif
4612 htmlParseDocTypeDecl(ctxt);
4613 ctxt->instate = XML_PARSER_PROLOG;
4614#ifdef DEBUG_PUSH
4615 xmlGenericError(xmlGenericErrorContext,
4616 "HPP: entering PROLOG\n");
4617#endif
4618 } else {
4619 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004620#ifdef DEBUG_PUSH
Daniel Veillard597f1c12005-07-03 23:00:18 +00004621 xmlGenericError(xmlGenericErrorContext,
4622 "HPP: entering MISC\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004623#endif
Daniel Veillard597f1c12005-07-03 23:00:18 +00004624 }
Owen Taylor3473f882001-02-23 17:55:21 +00004625 break;
4626 case XML_PARSER_MISC:
4627 SKIP_BLANKS;
4628 if (in->buf == NULL)
4629 avail = in->length - (in->cur - in->base);
4630 else
4631 avail = in->buf->buffer->use - (in->cur - in->base);
4632 if (avail < 2)
4633 goto done;
4634 cur = in->cur[0];
4635 next = in->cur[1];
4636 if ((cur == '<') && (next == '!') &&
4637 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4638 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004639 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004640 goto done;
4641#ifdef DEBUG_PUSH
4642 xmlGenericError(xmlGenericErrorContext,
4643 "HPP: Parsing Comment\n");
4644#endif
4645 htmlParseComment(ctxt);
4646 ctxt->instate = XML_PARSER_MISC;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004647 } else if ((cur == '<') && (next == '?')) {
4648 if ((!terminate) &&
4649 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4650 goto done;
4651#ifdef DEBUG_PUSH
4652 xmlGenericError(xmlGenericErrorContext,
4653 "HPP: Parsing PI\n");
4654#endif
4655 htmlParsePI(ctxt);
4656 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004657 } else if ((cur == '<') && (next == '!') &&
4658 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4659 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4660 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4661 (UPP(8) == 'E')) {
4662 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004663 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004664 goto done;
4665#ifdef DEBUG_PUSH
4666 xmlGenericError(xmlGenericErrorContext,
4667 "HPP: Parsing internal subset\n");
4668#endif
4669 htmlParseDocTypeDecl(ctxt);
4670 ctxt->instate = XML_PARSER_PROLOG;
4671#ifdef DEBUG_PUSH
4672 xmlGenericError(xmlGenericErrorContext,
4673 "HPP: entering PROLOG\n");
4674#endif
4675 } else if ((cur == '<') && (next == '!') &&
4676 (avail < 9)) {
4677 goto done;
4678 } else {
4679 ctxt->instate = XML_PARSER_START_TAG;
4680#ifdef DEBUG_PUSH
4681 xmlGenericError(xmlGenericErrorContext,
4682 "HPP: entering START_TAG\n");
4683#endif
4684 }
4685 break;
4686 case XML_PARSER_PROLOG:
4687 SKIP_BLANKS;
4688 if (in->buf == NULL)
4689 avail = in->length - (in->cur - in->base);
4690 else
4691 avail = in->buf->buffer->use - (in->cur - in->base);
4692 if (avail < 2)
4693 goto done;
4694 cur = in->cur[0];
4695 next = in->cur[1];
4696 if ((cur == '<') && (next == '!') &&
4697 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4698 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004699 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004700 goto done;
4701#ifdef DEBUG_PUSH
4702 xmlGenericError(xmlGenericErrorContext,
4703 "HPP: Parsing Comment\n");
4704#endif
4705 htmlParseComment(ctxt);
4706 ctxt->instate = XML_PARSER_PROLOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004707 } else if ((cur == '<') && (next == '?')) {
4708 if ((!terminate) &&
4709 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4710 goto done;
4711#ifdef DEBUG_PUSH
4712 xmlGenericError(xmlGenericErrorContext,
4713 "HPP: Parsing PI\n");
4714#endif
4715 htmlParsePI(ctxt);
4716 ctxt->instate = XML_PARSER_PROLOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004717 } else if ((cur == '<') && (next == '!') &&
4718 (avail < 4)) {
4719 goto done;
4720 } else {
4721 ctxt->instate = XML_PARSER_START_TAG;
4722#ifdef DEBUG_PUSH
4723 xmlGenericError(xmlGenericErrorContext,
4724 "HPP: entering START_TAG\n");
4725#endif
4726 }
4727 break;
4728 case XML_PARSER_EPILOG:
4729 if (in->buf == NULL)
4730 avail = in->length - (in->cur - in->base);
4731 else
4732 avail = in->buf->buffer->use - (in->cur - in->base);
4733 if (avail < 1)
4734 goto done;
4735 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004736 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004737 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004738 goto done;
4739 }
4740 if (avail < 2)
4741 goto done;
4742 next = in->cur[1];
4743 if ((cur == '<') && (next == '!') &&
4744 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4745 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004746 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004747 goto done;
4748#ifdef DEBUG_PUSH
4749 xmlGenericError(xmlGenericErrorContext,
4750 "HPP: Parsing Comment\n");
4751#endif
4752 htmlParseComment(ctxt);
4753 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004754 } else if ((cur == '<') && (next == '?')) {
4755 if ((!terminate) &&
4756 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4757 goto done;
4758#ifdef DEBUG_PUSH
4759 xmlGenericError(xmlGenericErrorContext,
4760 "HPP: Parsing PI\n");
4761#endif
4762 htmlParsePI(ctxt);
4763 ctxt->instate = XML_PARSER_EPILOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004764 } else if ((cur == '<') && (next == '!') &&
4765 (avail < 4)) {
4766 goto done;
4767 } else {
4768 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004769 ctxt->wellFormed = 0;
4770 ctxt->instate = XML_PARSER_EOF;
4771#ifdef DEBUG_PUSH
4772 xmlGenericError(xmlGenericErrorContext,
4773 "HPP: entering EOF\n");
4774#endif
4775 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4776 ctxt->sax->endDocument(ctxt->userData);
4777 goto done;
4778 }
4779 break;
4780 case XML_PARSER_START_TAG: {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004781 const xmlChar *name, *oldname;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004782 int failed;
Daniel Veillardbb371292001-08-16 23:26:59 +00004783 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004784
4785 if (avail < 2)
4786 goto done;
4787 cur = in->cur[0];
4788 if (cur != '<') {
4789 ctxt->instate = XML_PARSER_CONTENT;
4790#ifdef DEBUG_PUSH
4791 xmlGenericError(xmlGenericErrorContext,
4792 "HPP: entering CONTENT\n");
4793#endif
4794 break;
4795 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004796 if (in->cur[1] == '/') {
4797 ctxt->instate = XML_PARSER_END_TAG;
4798 ctxt->checkIndex = 0;
4799#ifdef DEBUG_PUSH
4800 xmlGenericError(xmlGenericErrorContext,
4801 "HPP: entering END_TAG\n");
4802#endif
4803 break;
4804 }
Owen Taylor3473f882001-02-23 17:55:21 +00004805 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004806 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004807 goto done;
4808
Daniel Veillard597f1c12005-07-03 23:00:18 +00004809 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004810 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004811 if (failed ||
Owen Taylor3473f882001-02-23 17:55:21 +00004812 (name == NULL)) {
4813 if (CUR == '>')
4814 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004815 break;
4816 }
Owen Taylor3473f882001-02-23 17:55:21 +00004817
4818 /*
4819 * Lookup the info for that element.
4820 */
4821 info = htmlTagLookup(name);
4822 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004823 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4824 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004825 }
4826
4827 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004828 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004829 */
4830 if ((CUR == '/') && (NXT(1) == '>')) {
4831 SKIP(2);
4832 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4833 ctxt->sax->endElement(ctxt->userData, name);
4834 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004835 ctxt->instate = XML_PARSER_CONTENT;
4836#ifdef DEBUG_PUSH
4837 xmlGenericError(xmlGenericErrorContext,
4838 "HPP: entering CONTENT\n");
4839#endif
4840 break;
4841 }
4842
4843 if (CUR == '>') {
4844 NEXT;
4845 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004846 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4847 "Couldn't find end of Start Tag %s\n",
4848 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004849
4850 /*
4851 * end of parsing of this node.
4852 */
4853 if (xmlStrEqual(name, ctxt->name)) {
4854 nodePop(ctxt);
4855 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004856 }
4857
4858 ctxt->instate = XML_PARSER_CONTENT;
4859#ifdef DEBUG_PUSH
4860 xmlGenericError(xmlGenericErrorContext,
4861 "HPP: entering CONTENT\n");
4862#endif
4863 break;
4864 }
4865
4866 /*
4867 * Check for an Empty Element from DTD definition
4868 */
4869 if ((info != NULL) && (info->empty)) {
4870 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4871 ctxt->sax->endElement(ctxt->userData, name);
4872 oldname = htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004873 }
4874 ctxt->instate = XML_PARSER_CONTENT;
4875#ifdef DEBUG_PUSH
4876 xmlGenericError(xmlGenericErrorContext,
4877 "HPP: entering CONTENT\n");
4878#endif
4879 break;
4880 }
4881 case XML_PARSER_CONTENT: {
4882 long cons;
4883 /*
4884 * Handle preparsed entities and charRef
4885 */
4886 if (ctxt->token != 0) {
4887 xmlChar chr[2] = { 0 , 0 } ;
4888
4889 chr[0] = (xmlChar) ctxt->token;
4890 htmlCheckParagraph(ctxt);
4891 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4892 ctxt->sax->characters(ctxt->userData, chr, 1);
4893 ctxt->token = 0;
4894 ctxt->checkIndex = 0;
4895 }
4896 if ((avail == 1) && (terminate)) {
4897 cur = in->cur[0];
4898 if ((cur != '<') && (cur != '&')) {
4899 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00004900 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004901 if (ctxt->sax->ignorableWhitespace != NULL)
4902 ctxt->sax->ignorableWhitespace(
4903 ctxt->userData, &cur, 1);
4904 } else {
4905 htmlCheckParagraph(ctxt);
4906 if (ctxt->sax->characters != NULL)
4907 ctxt->sax->characters(
4908 ctxt->userData, &cur, 1);
4909 }
4910 }
4911 ctxt->token = 0;
4912 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00004913 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00004914 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004915 }
Owen Taylor3473f882001-02-23 17:55:21 +00004916 }
4917 if (avail < 2)
4918 goto done;
4919 cur = in->cur[0];
4920 next = in->cur[1];
4921 cons = ctxt->nbChars;
4922 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4923 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4924 /*
4925 * Handle SCRIPT/STYLE separately
4926 */
4927 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004928 (htmlParseLookupSequence(ctxt, '<', '/', 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004929 goto done;
4930 htmlParseScript(ctxt);
4931 if ((cur == '<') && (next == '/')) {
4932 ctxt->instate = XML_PARSER_END_TAG;
4933 ctxt->checkIndex = 0;
4934#ifdef DEBUG_PUSH
4935 xmlGenericError(xmlGenericErrorContext,
4936 "HPP: entering END_TAG\n");
4937#endif
4938 break;
4939 }
4940 } else {
4941 /*
4942 * Sometimes DOCTYPE arrives in the middle of the document
4943 */
4944 if ((cur == '<') && (next == '!') &&
4945 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4946 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4947 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4948 (UPP(8) == 'E')) {
4949 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004950 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004951 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00004952 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4953 "Misplaced DOCTYPE declaration\n",
4954 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004955 htmlParseDocTypeDecl(ctxt);
4956 } else if ((cur == '<') && (next == '!') &&
4957 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4958 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004959 (htmlParseLookupSequence(
4960 ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004961 goto done;
4962#ifdef DEBUG_PUSH
4963 xmlGenericError(xmlGenericErrorContext,
4964 "HPP: Parsing Comment\n");
4965#endif
4966 htmlParseComment(ctxt);
4967 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004968 } else if ((cur == '<') && (next == '?')) {
4969 if ((!terminate) &&
4970 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4971 goto done;
4972#ifdef DEBUG_PUSH
4973 xmlGenericError(xmlGenericErrorContext,
4974 "HPP: Parsing PI\n");
4975#endif
4976 htmlParsePI(ctxt);
4977 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +00004978 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
4979 goto done;
4980 } else if ((cur == '<') && (next == '/')) {
4981 ctxt->instate = XML_PARSER_END_TAG;
4982 ctxt->checkIndex = 0;
4983#ifdef DEBUG_PUSH
4984 xmlGenericError(xmlGenericErrorContext,
4985 "HPP: entering END_TAG\n");
4986#endif
4987 break;
4988 } else if (cur == '<') {
4989 ctxt->instate = XML_PARSER_START_TAG;
4990 ctxt->checkIndex = 0;
4991#ifdef DEBUG_PUSH
4992 xmlGenericError(xmlGenericErrorContext,
4993 "HPP: entering START_TAG\n");
4994#endif
4995 break;
4996 } else if (cur == '&') {
4997 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004998 (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004999 goto done;
5000#ifdef DEBUG_PUSH
5001 xmlGenericError(xmlGenericErrorContext,
5002 "HPP: Parsing Reference\n");
5003#endif
5004 /* TODO: check generation of subtrees if noent !!! */
5005 htmlParseReference(ctxt);
5006 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005007 /*
5008 * check that the text sequence is complete
5009 * before handing out the data to the parser
5010 * to avoid problems with erroneous end of
5011 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00005012 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00005013 if ((!terminate) &&
5014 (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
5015 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00005016 ctxt->checkIndex = 0;
5017#ifdef DEBUG_PUSH
5018 xmlGenericError(xmlGenericErrorContext,
5019 "HPP: Parsing char data\n");
5020#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005021 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005022 }
5023 }
5024 if (cons == ctxt->nbChars) {
5025 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005026 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5027 "detected an error in element content\n",
5028 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005029 }
5030 NEXT;
5031 break;
5032 }
5033
5034 break;
5035 }
5036 case XML_PARSER_END_TAG:
5037 if (avail < 2)
5038 goto done;
5039 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005040 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005041 goto done;
5042 htmlParseEndTag(ctxt);
5043 if (ctxt->nameNr == 0) {
5044 ctxt->instate = XML_PARSER_EPILOG;
5045 } else {
5046 ctxt->instate = XML_PARSER_CONTENT;
5047 }
5048 ctxt->checkIndex = 0;
5049#ifdef DEBUG_PUSH
5050 xmlGenericError(xmlGenericErrorContext,
5051 "HPP: entering CONTENT\n");
5052#endif
5053 break;
5054 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00005055 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5056 "HPP: internal error, state == CDATA\n",
5057 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005058 ctxt->instate = XML_PARSER_CONTENT;
5059 ctxt->checkIndex = 0;
5060#ifdef DEBUG_PUSH
5061 xmlGenericError(xmlGenericErrorContext,
5062 "HPP: entering CONTENT\n");
5063#endif
5064 break;
5065 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00005066 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5067 "HPP: internal error, state == DTD\n",
5068 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005069 ctxt->instate = XML_PARSER_CONTENT;
5070 ctxt->checkIndex = 0;
5071#ifdef DEBUG_PUSH
5072 xmlGenericError(xmlGenericErrorContext,
5073 "HPP: entering CONTENT\n");
5074#endif
5075 break;
5076 case XML_PARSER_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00005077 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5078 "HPP: internal error, state == COMMENT\n",
5079 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005080 ctxt->instate = XML_PARSER_CONTENT;
5081 ctxt->checkIndex = 0;
5082#ifdef DEBUG_PUSH
5083 xmlGenericError(xmlGenericErrorContext,
5084 "HPP: entering CONTENT\n");
5085#endif
5086 break;
5087 case XML_PARSER_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00005088 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5089 "HPP: internal error, state == PI\n",
5090 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005091 ctxt->instate = XML_PARSER_CONTENT;
5092 ctxt->checkIndex = 0;
5093#ifdef DEBUG_PUSH
5094 xmlGenericError(xmlGenericErrorContext,
5095 "HPP: entering CONTENT\n");
5096#endif
5097 break;
5098 case XML_PARSER_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005099 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5100 "HPP: internal error, state == ENTITY_DECL\n",
5101 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005102 ctxt->instate = XML_PARSER_CONTENT;
5103 ctxt->checkIndex = 0;
5104#ifdef DEBUG_PUSH
5105 xmlGenericError(xmlGenericErrorContext,
5106 "HPP: entering CONTENT\n");
5107#endif
5108 break;
5109 case XML_PARSER_ENTITY_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005110 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5111 "HPP: internal error, state == ENTITY_VALUE\n",
5112 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005113 ctxt->instate = XML_PARSER_CONTENT;
5114 ctxt->checkIndex = 0;
5115#ifdef DEBUG_PUSH
5116 xmlGenericError(xmlGenericErrorContext,
5117 "HPP: entering DTD\n");
5118#endif
5119 break;
5120 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005121 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5122 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
5123 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005124 ctxt->instate = XML_PARSER_START_TAG;
5125 ctxt->checkIndex = 0;
5126#ifdef DEBUG_PUSH
5127 xmlGenericError(xmlGenericErrorContext,
5128 "HPP: entering START_TAG\n");
5129#endif
5130 break;
5131 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005132 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5133 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
5134 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005135 ctxt->instate = XML_PARSER_CONTENT;
5136 ctxt->checkIndex = 0;
5137#ifdef DEBUG_PUSH
5138 xmlGenericError(xmlGenericErrorContext,
5139 "HPP: entering CONTENT\n");
5140#endif
5141 break;
5142 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005143 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5144 "HPP: internal error, state == XML_PARSER_IGNORE\n",
5145 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005146 ctxt->instate = XML_PARSER_CONTENT;
5147 ctxt->checkIndex = 0;
5148#ifdef DEBUG_PUSH
5149 xmlGenericError(xmlGenericErrorContext,
5150 "HPP: entering CONTENT\n");
5151#endif
5152 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005153 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005154 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5155 "HPP: internal error, state == XML_PARSER_LITERAL\n",
5156 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005157 ctxt->instate = XML_PARSER_CONTENT;
5158 ctxt->checkIndex = 0;
5159#ifdef DEBUG_PUSH
5160 xmlGenericError(xmlGenericErrorContext,
5161 "HPP: entering CONTENT\n");
5162#endif
5163 break;
5164
Owen Taylor3473f882001-02-23 17:55:21 +00005165 }
5166 }
5167done:
5168 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005169 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005170 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5171 /*
5172 * SAX: end of the document processing.
5173 */
5174 ctxt->instate = XML_PARSER_EOF;
5175 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5176 ctxt->sax->endDocument(ctxt->userData);
5177 }
5178 }
5179 if ((ctxt->myDoc != NULL) &&
5180 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5181 (ctxt->instate == XML_PARSER_EPILOG))) {
5182 xmlDtdPtr dtd;
5183 dtd = xmlGetIntSubset(ctxt->myDoc);
5184 if (dtd == NULL)
5185 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00005186 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00005187 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5188 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5189 }
5190#ifdef DEBUG_PUSH
5191 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5192#endif
5193 return(ret);
5194}
5195
5196/**
Owen Taylor3473f882001-02-23 17:55:21 +00005197 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00005198 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00005199 * @chunk: an char array
5200 * @size: the size in byte of the chunk
5201 * @terminate: last chunk indicator
5202 *
5203 * Parse a Chunk of memory
5204 *
5205 * Returns zero if no error, the xmlParserErrors otherwise.
5206 */
5207int
5208htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
5209 int terminate) {
Daniel Veillarda03e3652004-11-02 18:45:30 +00005210 if ((ctxt == NULL) || (ctxt->input == NULL)) {
5211 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5212 "htmlParseChunk: context error\n", NULL, NULL);
5213 return(XML_ERR_INTERNAL_ERROR);
5214 }
Owen Taylor3473f882001-02-23 17:55:21 +00005215 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5216 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
5217 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5218 int cur = ctxt->input->cur - ctxt->input->base;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005219 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00005220
Daniel Veillardd2755a82005-08-07 23:42:39 +00005221 res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5222 if (res < 0) {
5223 ctxt->errNo = XML_PARSER_EOF;
5224 ctxt->disableSAX = 1;
5225 return (XML_PARSER_EOF);
5226 }
Owen Taylor3473f882001-02-23 17:55:21 +00005227 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5228 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005229 ctxt->input->end =
5230 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005231#ifdef DEBUG_PUSH
5232 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5233#endif
5234
Daniel Veillard14f752c2003-08-09 11:44:50 +00005235#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00005236 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5237 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005238#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005239 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005240 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
5241 xmlParserInputBufferPtr in = ctxt->input->buf;
5242 if ((in->encoder != NULL) && (in->buffer != NULL) &&
5243 (in->raw != NULL)) {
5244 int nbchars;
5245
5246 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
5247 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005248 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
5249 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005250 return(XML_ERR_INVALID_ENCODING);
5251 }
5252 }
5253 }
Owen Taylor3473f882001-02-23 17:55:21 +00005254 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00005255 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00005256 if (terminate) {
5257 if ((ctxt->instate != XML_PARSER_EOF) &&
5258 (ctxt->instate != XML_PARSER_EPILOG) &&
5259 (ctxt->instate != XML_PARSER_MISC)) {
5260 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005261 ctxt->wellFormed = 0;
5262 }
5263 if (ctxt->instate != XML_PARSER_EOF) {
5264 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5265 ctxt->sax->endDocument(ctxt->userData);
5266 }
5267 ctxt->instate = XML_PARSER_EOF;
5268 }
5269 return((xmlParserErrors) ctxt->errNo);
5270}
5271
5272/************************************************************************
5273 * *
5274 * User entry points *
5275 * *
5276 ************************************************************************/
5277
5278/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005279 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005280 * @sax: a SAX handler
5281 * @user_data: The user data returned on SAX callbacks
5282 * @chunk: a pointer to an array of chars
5283 * @size: number of chars in the array
5284 * @filename: an optional file name or URI
5285 * @enc: an optional encoding
5286 *
5287 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005288 * The value of @filename is used for fetching external entities
5289 * and error/warning reports.
5290 *
5291 * Returns the new parser context or NULL
5292 */
5293htmlParserCtxtPtr
5294htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
5295 const char *chunk, int size, const char *filename,
5296 xmlCharEncoding enc) {
5297 htmlParserCtxtPtr ctxt;
5298 htmlParserInputPtr inputStream;
5299 xmlParserInputBufferPtr buf;
5300
Daniel Veillardd0463562001-10-13 09:15:48 +00005301 xmlInitParser();
5302
Owen Taylor3473f882001-02-23 17:55:21 +00005303 buf = xmlAllocParserInputBuffer(enc);
5304 if (buf == NULL) return(NULL);
5305
Daniel Veillardf403d292003-10-05 13:51:35 +00005306 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005307 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005308 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005309 return(NULL);
5310 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00005311 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
5312 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00005313 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00005314 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00005315 xmlFree(ctxt->sax);
5316 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
5317 if (ctxt->sax == NULL) {
5318 xmlFree(buf);
5319 xmlFree(ctxt);
5320 return(NULL);
5321 }
5322 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
5323 if (user_data != NULL)
5324 ctxt->userData = user_data;
5325 }
5326 if (filename == NULL) {
5327 ctxt->directory = NULL;
5328 } else {
5329 ctxt->directory = xmlParserGetDirectory(filename);
5330 }
5331
5332 inputStream = htmlNewInputStream(ctxt);
5333 if (inputStream == NULL) {
5334 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00005335 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005336 return(NULL);
5337 }
5338
5339 if (filename == NULL)
5340 inputStream->filename = NULL;
5341 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00005342 inputStream->filename = (char *)
5343 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00005344 inputStream->buf = buf;
5345 inputStream->base = inputStream->buf->buffer->content;
5346 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard5f704af2003-03-05 10:01:43 +00005347 inputStream->end =
5348 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005349
5350 inputPush(ctxt, inputStream);
5351
5352 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5353 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00005354 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5355 int cur = ctxt->input->cur - ctxt->input->base;
5356
Owen Taylor3473f882001-02-23 17:55:21 +00005357 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00005358
5359 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5360 ctxt->input->cur = ctxt->input->base + cur;
5361 ctxt->input->end =
5362 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005363#ifdef DEBUG_PUSH
5364 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5365#endif
5366 }
5367
5368 return(ctxt);
5369}
William M. Brack21e4ef22005-01-02 09:53:13 +00005370#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005371
5372/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005373 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005374 * @cur: a pointer to an array of xmlChar
5375 * @encoding: a free form C string describing the HTML document encoding, or NULL
5376 * @sax: the SAX handler block
5377 * @userData: if using SAX, this pointer will be provided on callbacks.
5378 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005379 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
5380 * to handle parse events. If sax is NULL, fallback to the default DOM
5381 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00005382 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005383 * Returns the resulting document tree unless SAX is NULL or the document is
5384 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005385 */
5386
5387htmlDocPtr
5388htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
5389 htmlDocPtr ret;
5390 htmlParserCtxtPtr ctxt;
5391
Daniel Veillardd0463562001-10-13 09:15:48 +00005392 xmlInitParser();
5393
Owen Taylor3473f882001-02-23 17:55:21 +00005394 if (cur == NULL) return(NULL);
5395
5396
5397 ctxt = htmlCreateDocParserCtxt(cur, encoding);
5398 if (ctxt == NULL) return(NULL);
5399 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00005400 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00005401 ctxt->sax = sax;
5402 ctxt->userData = userData;
5403 }
5404
5405 htmlParseDocument(ctxt);
5406 ret = ctxt->myDoc;
5407 if (sax != NULL) {
5408 ctxt->sax = NULL;
5409 ctxt->userData = NULL;
5410 }
5411 htmlFreeParserCtxt(ctxt);
5412
5413 return(ret);
5414}
5415
5416/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005417 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005418 * @cur: a pointer to an array of xmlChar
5419 * @encoding: a free form C string describing the HTML document encoding, or NULL
5420 *
5421 * parse an HTML in-memory document and build a tree.
5422 *
5423 * Returns the resulting document tree
5424 */
5425
5426htmlDocPtr
5427htmlParseDoc(xmlChar *cur, const char *encoding) {
5428 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
5429}
5430
5431
5432/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005433 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005434 * @filename: the filename
5435 * @encoding: a free form C string describing the HTML document encoding, or NULL
5436 *
5437 * Create a parser context for a file content.
5438 * Automatic support for ZLIB/Compress compressed document is provided
5439 * by default if found at compile-time.
5440 *
5441 * Returns the new parser context or NULL
5442 */
5443htmlParserCtxtPtr
5444htmlCreateFileParserCtxt(const char *filename, const char *encoding)
5445{
5446 htmlParserCtxtPtr ctxt;
5447 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005448 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00005449 /* htmlCharEncoding enc; */
5450 xmlChar *content, *content_line = (xmlChar *) "charset=";
5451
Daniel Veillarda03e3652004-11-02 18:45:30 +00005452 if (filename == NULL)
5453 return(NULL);
5454
Daniel Veillardf403d292003-10-05 13:51:35 +00005455 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005456 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005457 return(NULL);
5458 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005459 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
5460 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00005461#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005462 if (xmlDefaultSAXHandler.error != NULL) {
5463 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
5464 }
Daniel Veillard87247e82004-01-13 20:42:02 +00005465#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00005466 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005467 return(NULL);
5468 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005469
5470 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
5471 xmlFree(canonicFilename);
5472 if (inputStream == NULL) {
5473 xmlFreeParserCtxt(ctxt);
5474 return(NULL);
5475 }
Owen Taylor3473f882001-02-23 17:55:21 +00005476
5477 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005478
Owen Taylor3473f882001-02-23 17:55:21 +00005479 /* set encoding */
5480 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005481 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Owen Taylor3473f882001-02-23 17:55:21 +00005482 if (content) {
5483 strcpy ((char *)content, (char *)content_line);
5484 strcat ((char *)content, (char *)encoding);
5485 htmlCheckEncoding (ctxt, content);
5486 xmlFree (content);
5487 }
5488 }
5489
5490 return(ctxt);
5491}
5492
5493/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005494 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005495 * @filename: the filename
5496 * @encoding: a free form C string describing the HTML document encoding, or NULL
5497 * @sax: the SAX handler block
5498 * @userData: if using SAX, this pointer will be provided on callbacks.
5499 *
5500 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5501 * compressed document is provided by default if found at compile-time.
5502 * It use the given SAX function block to handle the parsing callback.
5503 * If sax is NULL, fallback to the default DOM tree building routines.
5504 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005505 * Returns the resulting document tree unless SAX is NULL or the document is
5506 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005507 */
5508
5509htmlDocPtr
5510htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
5511 void *userData) {
5512 htmlDocPtr ret;
5513 htmlParserCtxtPtr ctxt;
5514 htmlSAXHandlerPtr oldsax = NULL;
5515
Daniel Veillardd0463562001-10-13 09:15:48 +00005516 xmlInitParser();
5517
Owen Taylor3473f882001-02-23 17:55:21 +00005518 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5519 if (ctxt == NULL) return(NULL);
5520 if (sax != NULL) {
5521 oldsax = ctxt->sax;
5522 ctxt->sax = sax;
5523 ctxt->userData = userData;
5524 }
5525
5526 htmlParseDocument(ctxt);
5527
5528 ret = ctxt->myDoc;
5529 if (sax != NULL) {
5530 ctxt->sax = oldsax;
5531 ctxt->userData = NULL;
5532 }
5533 htmlFreeParserCtxt(ctxt);
5534
5535 return(ret);
5536}
5537
5538/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005539 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005540 * @filename: the filename
5541 * @encoding: a free form C string describing the HTML document encoding, or NULL
5542 *
5543 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5544 * compressed document is provided by default if found at compile-time.
5545 *
5546 * Returns the resulting document tree
5547 */
5548
5549htmlDocPtr
5550htmlParseFile(const char *filename, const char *encoding) {
5551 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
5552}
5553
5554/**
5555 * htmlHandleOmittedElem:
5556 * @val: int 0 or 1
5557 *
5558 * Set and return the previous value for handling HTML omitted tags.
5559 *
5560 * Returns the last value for 0 for no handling, 1 for auto insertion.
5561 */
5562
5563int
5564htmlHandleOmittedElem(int val) {
5565 int old = htmlOmittedDefaultValue;
5566
5567 htmlOmittedDefaultValue = val;
5568 return(old);
5569}
5570
Daniel Veillard930dfb62003-02-05 10:17:38 +00005571/**
5572 * htmlElementAllowedHere:
5573 * @parent: HTML parent element
5574 * @elt: HTML element
5575 *
5576 * Checks whether an HTML element may be a direct child of a parent element.
5577 * Note - doesn't check for deprecated elements
5578 *
5579 * Returns 1 if allowed; 0 otherwise.
5580 */
5581int
5582htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
5583 const char** p ;
5584
5585 if ( ! elt || ! parent || ! parent->subelts )
5586 return 0 ;
5587
5588 for ( p = parent->subelts; *p; ++p )
5589 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
5590 return 1 ;
5591
5592 return 0 ;
5593}
5594/**
5595 * htmlElementStatusHere:
5596 * @parent: HTML parent element
5597 * @elt: HTML element
5598 *
5599 * Checks whether an HTML element may be a direct child of a parent element.
5600 * and if so whether it is valid or deprecated.
5601 *
5602 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5603 */
5604htmlStatus
5605htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
5606 if ( ! parent || ! elt )
5607 return HTML_INVALID ;
5608 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
5609 return HTML_INVALID ;
5610
5611 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
5612}
5613/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005614 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00005615 * @elt: HTML element
5616 * @attr: HTML attribute
5617 * @legacy: whether to allow deprecated attributes
5618 *
5619 * Checks whether an attribute is valid for an element
5620 * Has full knowledge of Required and Deprecated attributes
5621 *
5622 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5623 */
5624htmlStatus
5625htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
5626 const char** p ;
5627
5628 if ( !elt || ! attr )
5629 return HTML_INVALID ;
5630
5631 if ( elt->attrs_req )
5632 for ( p = elt->attrs_req; *p; ++p)
5633 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5634 return HTML_REQUIRED ;
5635
5636 if ( elt->attrs_opt )
5637 for ( p = elt->attrs_opt; *p; ++p)
5638 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5639 return HTML_VALID ;
5640
5641 if ( legacy && elt->attrs_depr )
5642 for ( p = elt->attrs_depr; *p; ++p)
5643 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5644 return HTML_DEPRECATED ;
5645
5646 return HTML_INVALID ;
5647}
5648/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005649 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005650 * @node: an htmlNodePtr in a tree
5651 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00005652 * for Element nodes)
5653 *
5654 * Checks whether the tree node is valid. Experimental (the author
5655 * only uses the HTML enhancements in a SAX parser)
5656 *
5657 * Return: for Element nodes, a return from htmlElementAllowedHere (if
5658 * legacy allowed) or htmlElementStatusHere (otherwise).
5659 * for Attribute nodes, a return from htmlAttrAllowed
5660 * for other nodes, HTML_NA (no checks performed)
5661 */
5662htmlStatus
5663htmlNodeStatus(const htmlNodePtr node, int legacy) {
5664 if ( ! node )
5665 return HTML_INVALID ;
5666
5667 switch ( node->type ) {
5668 case XML_ELEMENT_NODE:
5669 return legacy
5670 ? ( htmlElementAllowedHere (
5671 htmlTagLookup(node->parent->name) , node->name
5672 ) ? HTML_VALID : HTML_INVALID )
5673 : htmlElementStatusHere(
5674 htmlTagLookup(node->parent->name) ,
5675 htmlTagLookup(node->name) )
5676 ;
5677 case XML_ATTRIBUTE_NODE:
5678 return htmlAttrAllowed(
5679 htmlTagLookup(node->parent->name) , node->name, legacy) ;
5680 default: return HTML_NA ;
5681 }
5682}
Daniel Veillard9475a352003-09-26 12:47:50 +00005683/************************************************************************
5684 * *
5685 * New set (2.6.0) of simpler and more flexible APIs *
5686 * *
5687 ************************************************************************/
5688/**
5689 * DICT_FREE:
5690 * @str: a string
5691 *
5692 * Free a string if it is not owned by the "dict" dictionnary in the
5693 * current scope
5694 */
5695#define DICT_FREE(str) \
5696 if ((str) && ((!dict) || \
5697 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
5698 xmlFree((char *)(str));
5699
5700/**
5701 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00005702 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00005703 *
5704 * Reset a parser context
5705 */
5706void
5707htmlCtxtReset(htmlParserCtxtPtr ctxt)
5708{
5709 xmlParserInputPtr input;
Daniel Veillarda03e3652004-11-02 18:45:30 +00005710 xmlDictPtr dict;
5711
5712 if (ctxt == NULL)
5713 return;
5714
5715 dict = ctxt->dict;
Daniel Veillard9475a352003-09-26 12:47:50 +00005716
5717 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
5718 xmlFreeInputStream(input);
5719 }
5720 ctxt->inputNr = 0;
5721 ctxt->input = NULL;
5722
5723 ctxt->spaceNr = 0;
Daniel Veillarda521d282004-11-09 14:59:59 +00005724 if (ctxt->spaceTab != NULL) {
5725 ctxt->spaceTab[0] = -1;
5726 ctxt->space = &ctxt->spaceTab[0];
5727 } else {
5728 ctxt->space = NULL;
5729 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005730
5731
5732 ctxt->nodeNr = 0;
5733 ctxt->node = NULL;
5734
5735 ctxt->nameNr = 0;
5736 ctxt->name = NULL;
5737
5738 DICT_FREE(ctxt->version);
5739 ctxt->version = NULL;
5740 DICT_FREE(ctxt->encoding);
5741 ctxt->encoding = NULL;
5742 DICT_FREE(ctxt->directory);
5743 ctxt->directory = NULL;
5744 DICT_FREE(ctxt->extSubURI);
5745 ctxt->extSubURI = NULL;
5746 DICT_FREE(ctxt->extSubSystem);
5747 ctxt->extSubSystem = NULL;
5748 if (ctxt->myDoc != NULL)
5749 xmlFreeDoc(ctxt->myDoc);
5750 ctxt->myDoc = NULL;
5751
5752 ctxt->standalone = -1;
5753 ctxt->hasExternalSubset = 0;
5754 ctxt->hasPErefs = 0;
5755 ctxt->html = 1;
5756 ctxt->external = 0;
5757 ctxt->instate = XML_PARSER_START;
5758 ctxt->token = 0;
5759
5760 ctxt->wellFormed = 1;
5761 ctxt->nsWellFormed = 1;
5762 ctxt->valid = 1;
5763 ctxt->vctxt.userData = ctxt;
5764 ctxt->vctxt.error = xmlParserValidityError;
5765 ctxt->vctxt.warning = xmlParserValidityWarning;
5766 ctxt->record_info = 0;
5767 ctxt->nbChars = 0;
5768 ctxt->checkIndex = 0;
5769 ctxt->inSubset = 0;
5770 ctxt->errNo = XML_ERR_OK;
5771 ctxt->depth = 0;
5772 ctxt->charset = XML_CHAR_ENCODING_UTF8;
5773 ctxt->catalogs = NULL;
5774 xmlInitNodeInfoSeq(&ctxt->node_seq);
5775
5776 if (ctxt->attsDefault != NULL) {
5777 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
5778 ctxt->attsDefault = NULL;
5779 }
5780 if (ctxt->attsSpecial != NULL) {
5781 xmlHashFree(ctxt->attsSpecial, NULL);
5782 ctxt->attsSpecial = NULL;
5783 }
5784}
5785
5786/**
5787 * htmlCtxtUseOptions:
5788 * @ctxt: an HTML parser context
5789 * @options: a combination of htmlParserOption(s)
5790 *
5791 * Applies the options to the parser context
5792 *
5793 * Returns 0 in case of success, the set of unknown or unimplemented options
5794 * in case of error.
5795 */
5796int
5797htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
5798{
Daniel Veillarda03e3652004-11-02 18:45:30 +00005799 if (ctxt == NULL)
5800 return(-1);
5801
Daniel Veillard9475a352003-09-26 12:47:50 +00005802 if (options & HTML_PARSE_NOWARNING) {
5803 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005804 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005805 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005806 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00005807 }
5808 if (options & HTML_PARSE_NOERROR) {
5809 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005810 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005811 ctxt->sax->fatalError = NULL;
5812 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005813 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00005814 }
5815 if (options & HTML_PARSE_PEDANTIC) {
5816 ctxt->pedantic = 1;
5817 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005818 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00005819 } else
5820 ctxt->pedantic = 0;
5821 if (options & XML_PARSE_NOBLANKS) {
5822 ctxt->keepBlanks = 0;
5823 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
5824 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005825 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00005826 } else
5827 ctxt->keepBlanks = 1;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00005828 if (options & HTML_PARSE_RECOVER) {
5829 ctxt->recovery = 1;
5830 } else
5831 ctxt->recovery = 0;
Daniel Veillard8874b942005-08-25 13:19:21 +00005832 if (options & HTML_PARSE_COMPACT) {
5833 ctxt->options |= HTML_PARSE_COMPACT;
5834 options -= HTML_PARSE_COMPACT;
5835 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005836 ctxt->dictNames = 0;
5837 return (options);
5838}
5839
5840/**
5841 * htmlDoRead:
5842 * @ctxt: an HTML parser context
5843 * @URL: the base URL to use for the document
5844 * @encoding: the document encoding, or NULL
5845 * @options: a combination of htmlParserOption(s)
5846 * @reuse: keep the context for reuse
5847 *
5848 * Common front-end for the htmlRead functions
5849 *
5850 * Returns the resulting document tree or NULL
5851 */
5852static htmlDocPtr
5853htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
5854 int options, int reuse)
5855{
5856 htmlDocPtr ret;
5857
5858 htmlCtxtUseOptions(ctxt, options);
5859 ctxt->html = 1;
5860 if (encoding != NULL) {
5861 xmlCharEncodingHandlerPtr hdlr;
5862
5863 hdlr = xmlFindCharEncodingHandler(encoding);
5864 if (hdlr != NULL)
5865 xmlSwitchToEncoding(ctxt, hdlr);
5866 }
5867 if ((URL != NULL) && (ctxt->input != NULL) &&
5868 (ctxt->input->filename == NULL))
5869 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
5870 htmlParseDocument(ctxt);
5871 ret = ctxt->myDoc;
5872 ctxt->myDoc = NULL;
5873 if (!reuse) {
5874 if ((ctxt->dictNames) &&
5875 (ret != NULL) &&
5876 (ret->dict == ctxt->dict))
5877 ctxt->dict = NULL;
5878 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00005879 }
5880 return (ret);
5881}
5882
5883/**
5884 * htmlReadDoc:
5885 * @cur: a pointer to a zero terminated string
5886 * @URL: the base URL to use for the document
5887 * @encoding: the document encoding, or NULL
5888 * @options: a combination of htmlParserOption(s)
5889 *
5890 * parse an XML in-memory document and build a tree.
5891 *
5892 * Returns the resulting document tree
5893 */
5894htmlDocPtr
5895htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
5896{
5897 htmlParserCtxtPtr ctxt;
5898
5899 if (cur == NULL)
5900 return (NULL);
5901
5902 ctxt = xmlCreateDocParserCtxt(cur);
5903 if (ctxt == NULL)
5904 return (NULL);
5905 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5906}
5907
5908/**
5909 * htmlReadFile:
5910 * @filename: a file or URL
5911 * @encoding: the document encoding, or NULL
5912 * @options: a combination of htmlParserOption(s)
5913 *
5914 * parse an XML file from the filesystem or the network.
5915 *
5916 * Returns the resulting document tree
5917 */
5918htmlDocPtr
5919htmlReadFile(const char *filename, const char *encoding, int options)
5920{
5921 htmlParserCtxtPtr ctxt;
5922
5923 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5924 if (ctxt == NULL)
5925 return (NULL);
5926 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
5927}
5928
5929/**
5930 * htmlReadMemory:
5931 * @buffer: a pointer to a char array
5932 * @size: the size of the array
5933 * @URL: the base URL to use for the document
5934 * @encoding: the document encoding, or NULL
5935 * @options: a combination of htmlParserOption(s)
5936 *
5937 * parse an XML in-memory document and build a tree.
5938 *
5939 * Returns the resulting document tree
5940 */
5941htmlDocPtr
5942htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
5943{
5944 htmlParserCtxtPtr ctxt;
5945
5946 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
5947 if (ctxt == NULL)
5948 return (NULL);
William M. Brackd43cdcd2004-08-03 15:13:29 +00005949 if (ctxt->sax != NULL)
5950 memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Daniel Veillard9475a352003-09-26 12:47:50 +00005951 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5952}
5953
5954/**
5955 * htmlReadFd:
5956 * @fd: an open file descriptor
5957 * @URL: the base URL to use for the document
5958 * @encoding: the document encoding, or NULL
5959 * @options: a combination of htmlParserOption(s)
5960 *
5961 * parse an XML from a file descriptor and build a tree.
5962 *
5963 * Returns the resulting document tree
5964 */
5965htmlDocPtr
5966htmlReadFd(int fd, const char *URL, const char *encoding, int options)
5967{
5968 htmlParserCtxtPtr ctxt;
5969 xmlParserInputBufferPtr input;
5970 xmlParserInputPtr stream;
5971
5972 if (fd < 0)
5973 return (NULL);
5974
5975 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5976 if (input == NULL)
5977 return (NULL);
5978 ctxt = xmlNewParserCtxt();
5979 if (ctxt == NULL) {
5980 xmlFreeParserInputBuffer(input);
5981 return (NULL);
5982 }
5983 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5984 if (stream == NULL) {
5985 xmlFreeParserInputBuffer(input);
5986 xmlFreeParserCtxt(ctxt);
5987 return (NULL);
5988 }
5989 inputPush(ctxt, stream);
5990 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5991}
5992
5993/**
5994 * htmlReadIO:
5995 * @ioread: an I/O read function
5996 * @ioclose: an I/O close function
5997 * @ioctx: an I/O handler
5998 * @URL: the base URL to use for the document
5999 * @encoding: the document encoding, or NULL
6000 * @options: a combination of htmlParserOption(s)
6001 *
6002 * parse an HTML document from I/O functions and source and build a tree.
6003 *
6004 * Returns the resulting document tree
6005 */
6006htmlDocPtr
6007htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
6008 void *ioctx, const char *URL, const char *encoding, int options)
6009{
6010 htmlParserCtxtPtr ctxt;
6011 xmlParserInputBufferPtr input;
6012 xmlParserInputPtr stream;
6013
6014 if (ioread == NULL)
6015 return (NULL);
6016
6017 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6018 XML_CHAR_ENCODING_NONE);
6019 if (input == NULL)
6020 return (NULL);
6021 ctxt = xmlNewParserCtxt();
6022 if (ctxt == NULL) {
6023 xmlFreeParserInputBuffer(input);
6024 return (NULL);
6025 }
6026 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6027 if (stream == NULL) {
6028 xmlFreeParserInputBuffer(input);
6029 xmlFreeParserCtxt(ctxt);
6030 return (NULL);
6031 }
6032 inputPush(ctxt, stream);
6033 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6034}
6035
6036/**
6037 * htmlCtxtReadDoc:
6038 * @ctxt: an HTML parser context
6039 * @cur: a pointer to a zero terminated string
6040 * @URL: the base URL to use for the document
6041 * @encoding: the document encoding, or NULL
6042 * @options: a combination of htmlParserOption(s)
6043 *
6044 * parse an XML in-memory document and build a tree.
6045 * This reuses the existing @ctxt parser context
6046 *
6047 * Returns the resulting document tree
6048 */
6049htmlDocPtr
6050htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
6051 const char *URL, const char *encoding, int options)
6052{
6053 xmlParserInputPtr stream;
6054
6055 if (cur == NULL)
6056 return (NULL);
6057 if (ctxt == NULL)
6058 return (NULL);
6059
6060 htmlCtxtReset(ctxt);
6061
6062 stream = xmlNewStringInputStream(ctxt, cur);
6063 if (stream == NULL) {
6064 return (NULL);
6065 }
6066 inputPush(ctxt, stream);
6067 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6068}
6069
6070/**
6071 * htmlCtxtReadFile:
6072 * @ctxt: an HTML parser context
6073 * @filename: a file or URL
6074 * @encoding: the document encoding, or NULL
6075 * @options: a combination of htmlParserOption(s)
6076 *
6077 * parse an XML file from the filesystem or the network.
6078 * This reuses the existing @ctxt parser context
6079 *
6080 * Returns the resulting document tree
6081 */
6082htmlDocPtr
6083htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
6084 const char *encoding, int options)
6085{
6086 xmlParserInputPtr stream;
6087
6088 if (filename == NULL)
6089 return (NULL);
6090 if (ctxt == NULL)
6091 return (NULL);
6092
6093 htmlCtxtReset(ctxt);
6094
Daniel Veillard29614c72004-11-26 10:47:26 +00006095 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006096 if (stream == NULL) {
6097 return (NULL);
6098 }
6099 inputPush(ctxt, stream);
6100 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
6101}
6102
6103/**
6104 * htmlCtxtReadMemory:
6105 * @ctxt: an HTML parser context
6106 * @buffer: a pointer to a char array
6107 * @size: the size of the array
6108 * @URL: the base URL to use for the document
6109 * @encoding: the document encoding, or NULL
6110 * @options: a combination of htmlParserOption(s)
6111 *
6112 * parse an XML in-memory document and build a tree.
6113 * This reuses the existing @ctxt parser context
6114 *
6115 * Returns the resulting document tree
6116 */
6117htmlDocPtr
6118htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
6119 const char *URL, const char *encoding, int options)
6120{
6121 xmlParserInputBufferPtr input;
6122 xmlParserInputPtr stream;
6123
6124 if (ctxt == NULL)
6125 return (NULL);
6126 if (buffer == NULL)
6127 return (NULL);
6128
6129 htmlCtxtReset(ctxt);
6130
6131 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
6132 if (input == NULL) {
6133 return(NULL);
6134 }
6135
6136 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6137 if (stream == NULL) {
6138 xmlFreeParserInputBuffer(input);
6139 return(NULL);
6140 }
6141
6142 inputPush(ctxt, stream);
6143 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6144}
6145
6146/**
6147 * htmlCtxtReadFd:
6148 * @ctxt: an HTML parser context
6149 * @fd: an open file descriptor
6150 * @URL: the base URL to use for the document
6151 * @encoding: the document encoding, or NULL
6152 * @options: a combination of htmlParserOption(s)
6153 *
6154 * parse an XML from a file descriptor and build a tree.
6155 * This reuses the existing @ctxt parser context
6156 *
6157 * Returns the resulting document tree
6158 */
6159htmlDocPtr
6160htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
6161 const char *URL, const char *encoding, int options)
6162{
6163 xmlParserInputBufferPtr input;
6164 xmlParserInputPtr stream;
6165
6166 if (fd < 0)
6167 return (NULL);
6168 if (ctxt == NULL)
6169 return (NULL);
6170
6171 htmlCtxtReset(ctxt);
6172
6173
6174 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6175 if (input == NULL)
6176 return (NULL);
6177 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6178 if (stream == NULL) {
6179 xmlFreeParserInputBuffer(input);
6180 return (NULL);
6181 }
6182 inputPush(ctxt, stream);
6183 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6184}
6185
6186/**
6187 * htmlCtxtReadIO:
6188 * @ctxt: an HTML parser context
6189 * @ioread: an I/O read function
6190 * @ioclose: an I/O close function
6191 * @ioctx: an I/O handler
6192 * @URL: the base URL to use for the document
6193 * @encoding: the document encoding, or NULL
6194 * @options: a combination of htmlParserOption(s)
6195 *
6196 * parse an HTML document from I/O functions and source and build a tree.
6197 * This reuses the existing @ctxt parser context
6198 *
6199 * Returns the resulting document tree
6200 */
6201htmlDocPtr
6202htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
6203 xmlInputCloseCallback ioclose, void *ioctx,
6204 const char *URL,
6205 const char *encoding, int options)
6206{
6207 xmlParserInputBufferPtr input;
6208 xmlParserInputPtr stream;
6209
6210 if (ioread == NULL)
6211 return (NULL);
6212 if (ctxt == NULL)
6213 return (NULL);
6214
6215 htmlCtxtReset(ctxt);
6216
6217 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6218 XML_CHAR_ENCODING_NONE);
6219 if (input == NULL)
6220 return (NULL);
6221 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6222 if (stream == NULL) {
6223 xmlFreeParserInputBuffer(input);
6224 return (NULL);
6225 }
6226 inputPush(ctxt, stream);
6227 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6228}
6229
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006230#define bottom_HTMLparser
6231#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00006232#endif /* LIBXML_HTML_ENABLED */