blob: 5cbc904043c501ded26e01ecb5d26303b19a7ce4 [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 Veillardb9900082005-10-25 12:36:29 +00002704 GROW;
Daniel Veillard358fef42005-07-13 16:37:38 +00002705 NEXTL(l);
2706 cur = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002707 }
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002708
William M. Brack76e95df2003-10-18 16:20:14 +00002709 if (!(IS_CHAR_CH(cur))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002710 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2711 "Invalid char in CDATA 0x%X\n", cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002712 NEXT;
2713 }
2714
2715 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2716 if (ctxt->sax->cdataBlock!= NULL) {
2717 /*
2718 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2719 */
2720 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002721 } else if (ctxt->sax->characters != NULL) {
2722 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002723 }
2724 }
2725}
2726
2727
2728/**
2729 * htmlParseCharData:
2730 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002731 *
2732 * parse a CharData section.
2733 * if we are within a CDATA section ']]>' marks an end of section.
2734 *
2735 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2736 */
2737
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002738static void
2739htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002740 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2741 int nbchar = 0;
2742 int cur, l;
2743
2744 SHRINK;
2745 cur = CUR_CHAR(l);
2746 while (((cur != '<') || (ctxt->token == '<')) &&
2747 ((cur != '&') || (ctxt->token == '&')) &&
2748 (IS_CHAR(cur))) {
2749 COPY_BUF(l,buf,nbchar,cur);
2750 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2751 /*
2752 * Ok the segment is to be consumed as chars.
2753 */
2754 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2755 if (areBlanks(ctxt, buf, nbchar)) {
2756 if (ctxt->sax->ignorableWhitespace != NULL)
2757 ctxt->sax->ignorableWhitespace(ctxt->userData,
2758 buf, nbchar);
2759 } else {
2760 htmlCheckParagraph(ctxt);
2761 if (ctxt->sax->characters != NULL)
2762 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2763 }
2764 }
2765 nbchar = 0;
2766 }
2767 NEXTL(l);
2768 cur = CUR_CHAR(l);
Daniel Veillard358a9892003-02-04 15:22:32 +00002769 if (cur == 0) {
2770 SHRINK;
2771 GROW;
2772 cur = CUR_CHAR(l);
2773 }
Owen Taylor3473f882001-02-23 17:55:21 +00002774 }
2775 if (nbchar != 0) {
Daniel Veillardd2755a82005-08-07 23:42:39 +00002776 buf[nbchar] = 0;
2777
Owen Taylor3473f882001-02-23 17:55:21 +00002778 /*
2779 * Ok the segment is to be consumed as chars.
2780 */
2781 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2782 if (areBlanks(ctxt, buf, nbchar)) {
2783 if (ctxt->sax->ignorableWhitespace != NULL)
2784 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2785 } else {
2786 htmlCheckParagraph(ctxt);
2787 if (ctxt->sax->characters != NULL)
2788 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2789 }
2790 }
Daniel Veillard7cc95c02001-10-17 15:45:12 +00002791 } else {
2792 /*
2793 * Loop detection
2794 */
2795 if (cur == 0)
2796 ctxt->instate = XML_PARSER_EOF;
Owen Taylor3473f882001-02-23 17:55:21 +00002797 }
2798}
2799
2800/**
2801 * htmlParseExternalID:
2802 * @ctxt: an HTML parser context
2803 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002804 *
2805 * Parse an External ID or a Public ID
2806 *
Owen Taylor3473f882001-02-23 17:55:21 +00002807 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
2808 * | 'PUBLIC' S PubidLiteral S SystemLiteral
2809 *
2810 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
2811 *
2812 * Returns the function returns SystemLiteral and in the second
2813 * case publicID receives PubidLiteral, is strict is off
2814 * it is possible to return NULL and have publicID set.
2815 */
2816
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002817static xmlChar *
2818htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00002819 xmlChar *URI = NULL;
2820
2821 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2822 (UPP(2) == 'S') && (UPP(3) == 'T') &&
2823 (UPP(4) == 'E') && (UPP(5) == 'M')) {
2824 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002825 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002826 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2827 "Space required after 'SYSTEM'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002828 }
2829 SKIP_BLANKS;
2830 URI = htmlParseSystemLiteral(ctxt);
2831 if (URI == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002832 htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
2833 "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002834 }
2835 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2836 (UPP(2) == 'B') && (UPP(3) == 'L') &&
2837 (UPP(4) == 'I') && (UPP(5) == 'C')) {
2838 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002839 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002840 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2841 "Space required after 'PUBLIC'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002842 }
2843 SKIP_BLANKS;
2844 *publicID = htmlParsePubidLiteral(ctxt);
2845 if (*publicID == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002846 htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
2847 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
2848 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002849 }
2850 SKIP_BLANKS;
2851 if ((CUR == '"') || (CUR == '\'')) {
2852 URI = htmlParseSystemLiteral(ctxt);
2853 }
2854 }
2855 return(URI);
2856}
2857
2858/**
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002859 * xmlParsePI:
2860 * @ctxt: an XML parser context
2861 *
2862 * parse an XML Processing Instruction.
2863 *
2864 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
2865 */
2866static void
2867htmlParsePI(htmlParserCtxtPtr ctxt) {
2868 xmlChar *buf = NULL;
2869 int len = 0;
2870 int size = HTML_PARSER_BUFFER_SIZE;
2871 int cur, l;
2872 const xmlChar *target;
2873 xmlParserInputState state;
2874 int count = 0;
2875
2876 if ((RAW == '<') && (NXT(1) == '?')) {
2877 state = ctxt->instate;
2878 ctxt->instate = XML_PARSER_PI;
2879 /*
2880 * this is a Processing Instruction.
2881 */
2882 SKIP(2);
2883 SHRINK;
2884
2885 /*
2886 * Parse the target name and check for special support like
2887 * namespace.
2888 */
2889 target = htmlParseName(ctxt);
2890 if (target != NULL) {
2891 if (RAW == '>') {
2892 SKIP(1);
2893
2894 /*
2895 * SAX: PI detected.
2896 */
2897 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2898 (ctxt->sax->processingInstruction != NULL))
2899 ctxt->sax->processingInstruction(ctxt->userData,
2900 target, NULL);
2901 ctxt->instate = state;
2902 return;
2903 }
2904 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
2905 if (buf == NULL) {
2906 htmlErrMemory(ctxt, NULL);
2907 ctxt->instate = state;
2908 return;
2909 }
2910 cur = CUR;
2911 if (!IS_BLANK(cur)) {
2912 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2913 "ParsePI: PI %s space expected\n", target, NULL);
2914 }
2915 SKIP_BLANKS;
2916 cur = CUR_CHAR(l);
2917 while (IS_CHAR(cur) && (cur != '>')) {
2918 if (len + 5 >= size) {
2919 xmlChar *tmp;
2920
2921 size *= 2;
2922 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2923 if (tmp == NULL) {
2924 htmlErrMemory(ctxt, NULL);
2925 xmlFree(buf);
2926 ctxt->instate = state;
2927 return;
2928 }
2929 buf = tmp;
2930 }
2931 count++;
2932 if (count > 50) {
2933 GROW;
2934 count = 0;
2935 }
2936 COPY_BUF(l,buf,len,cur);
2937 NEXTL(l);
2938 cur = CUR_CHAR(l);
2939 if (cur == 0) {
2940 SHRINK;
2941 GROW;
2942 cur = CUR_CHAR(l);
2943 }
2944 }
2945 buf[len] = 0;
2946 if (cur != '>') {
2947 htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
2948 "ParsePI: PI %s never end ...\n", target, NULL);
2949 } else {
2950 SKIP(1);
2951
2952 /*
2953 * SAX: PI detected.
2954 */
2955 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2956 (ctxt->sax->processingInstruction != NULL))
2957 ctxt->sax->processingInstruction(ctxt->userData,
2958 target, buf);
2959 }
2960 xmlFree(buf);
2961 } else {
2962 htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
2963 "PI is not started correctly", NULL, NULL);
2964 }
2965 ctxt->instate = state;
2966 }
2967}
2968
2969/**
Owen Taylor3473f882001-02-23 17:55:21 +00002970 * htmlParseComment:
2971 * @ctxt: an HTML parser context
2972 *
2973 * Parse an XML (SGML) comment <!-- .... -->
2974 *
2975 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
2976 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002977static void
Owen Taylor3473f882001-02-23 17:55:21 +00002978htmlParseComment(htmlParserCtxtPtr ctxt) {
2979 xmlChar *buf = NULL;
2980 int len;
2981 int size = HTML_PARSER_BUFFER_SIZE;
2982 int q, ql;
2983 int r, rl;
2984 int cur, l;
2985 xmlParserInputState state;
2986
2987 /*
2988 * Check that there is a comment right here.
2989 */
2990 if ((RAW != '<') || (NXT(1) != '!') ||
2991 (NXT(2) != '-') || (NXT(3) != '-')) return;
2992
2993 state = ctxt->instate;
2994 ctxt->instate = XML_PARSER_COMMENT;
2995 SHRINK;
2996 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002997 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002998 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002999 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003000 ctxt->instate = state;
3001 return;
3002 }
3003 q = CUR_CHAR(ql);
3004 NEXTL(ql);
3005 r = CUR_CHAR(rl);
3006 NEXTL(rl);
3007 cur = CUR_CHAR(l);
3008 len = 0;
3009 while (IS_CHAR(cur) &&
3010 ((cur != '>') ||
3011 (r != '-') || (q != '-'))) {
3012 if (len + 5 >= size) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003013 xmlChar *tmp;
3014
Owen Taylor3473f882001-02-23 17:55:21 +00003015 size *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003016 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3017 if (tmp == NULL) {
3018 xmlFree(buf);
Daniel Veillardf403d292003-10-05 13:51:35 +00003019 htmlErrMemory(ctxt, "growing buffer failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003020 ctxt->instate = state;
3021 return;
3022 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003023 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003024 }
3025 COPY_BUF(ql,buf,len,q);
3026 q = r;
3027 ql = rl;
3028 r = cur;
3029 rl = l;
3030 NEXTL(l);
3031 cur = CUR_CHAR(l);
3032 if (cur == 0) {
3033 SHRINK;
3034 GROW;
3035 cur = CUR_CHAR(l);
3036 }
3037 }
3038 buf[len] = 0;
3039 if (!IS_CHAR(cur)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003040 htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3041 "Comment not terminated \n<!--%.50s\n", buf, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003042 xmlFree(buf);
3043 } else {
3044 NEXT;
3045 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3046 (!ctxt->disableSAX))
3047 ctxt->sax->comment(ctxt->userData, buf);
3048 xmlFree(buf);
3049 }
3050 ctxt->instate = state;
3051}
3052
3053/**
3054 * htmlParseCharRef:
3055 * @ctxt: an HTML parser context
3056 *
3057 * parse Reference declarations
3058 *
3059 * [66] CharRef ::= '&#' [0-9]+ ';' |
3060 * '&#x' [0-9a-fA-F]+ ';'
3061 *
3062 * Returns the value parsed (as an int)
3063 */
3064int
3065htmlParseCharRef(htmlParserCtxtPtr ctxt) {
3066 int val = 0;
3067
Daniel Veillarda03e3652004-11-02 18:45:30 +00003068 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3069 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3070 "htmlParseCharRef: context error\n",
3071 NULL, NULL);
3072 return(0);
3073 }
Owen Taylor3473f882001-02-23 17:55:21 +00003074 if ((CUR == '&') && (NXT(1) == '#') &&
Daniel Veillardc59d8262003-11-20 21:59:12 +00003075 ((NXT(2) == 'x') || NXT(2) == 'X')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003076 SKIP(3);
3077 while (CUR != ';') {
3078 if ((CUR >= '0') && (CUR <= '9'))
3079 val = val * 16 + (CUR - '0');
3080 else if ((CUR >= 'a') && (CUR <= 'f'))
3081 val = val * 16 + (CUR - 'a') + 10;
3082 else if ((CUR >= 'A') && (CUR <= 'F'))
3083 val = val * 16 + (CUR - 'A') + 10;
3084 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003085 htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
3086 "htmlParseCharRef: invalid hexadecimal value\n",
3087 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003088 return(0);
3089 }
3090 NEXT;
3091 }
3092 if (CUR == ';')
3093 NEXT;
3094 } else if ((CUR == '&') && (NXT(1) == '#')) {
3095 SKIP(2);
3096 while (CUR != ';') {
3097 if ((CUR >= '0') && (CUR <= '9'))
3098 val = val * 10 + (CUR - '0');
3099 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003100 htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
3101 "htmlParseCharRef: invalid decimal value\n",
3102 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003103 return(0);
3104 }
3105 NEXT;
3106 }
3107 if (CUR == ';')
3108 NEXT;
3109 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003110 htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
3111 "htmlParseCharRef: invalid value\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003112 }
3113 /*
3114 * Check the value IS_CHAR ...
3115 */
3116 if (IS_CHAR(val)) {
3117 return(val);
3118 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003119 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3120 "htmlParseCharRef: invalid xmlChar value %d\n",
3121 val);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 }
3123 return(0);
3124}
3125
3126
3127/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003128 * htmlParseDocTypeDecl:
Owen Taylor3473f882001-02-23 17:55:21 +00003129 * @ctxt: an HTML parser context
3130 *
3131 * parse a DOCTYPE declaration
3132 *
3133 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3134 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3135 */
3136
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003137static void
Owen Taylor3473f882001-02-23 17:55:21 +00003138htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003139 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003140 xmlChar *ExternalID = NULL;
3141 xmlChar *URI = NULL;
3142
3143 /*
3144 * We know that '<!DOCTYPE' has been detected.
3145 */
3146 SKIP(9);
3147
3148 SKIP_BLANKS;
3149
3150 /*
3151 * Parse the DOCTYPE name.
3152 */
3153 name = htmlParseName(ctxt);
3154 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003155 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3156 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3157 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003158 }
3159 /*
3160 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3161 */
3162
3163 SKIP_BLANKS;
3164
3165 /*
3166 * Check for SystemID and ExternalID
3167 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003168 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003169 SKIP_BLANKS;
3170
3171 /*
3172 * We should be at the end of the DOCTYPE declaration.
3173 */
3174 if (CUR != '>') {
Daniel Veillardf403d292003-10-05 13:51:35 +00003175 htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
3176 "DOCTYPE improperly terminated\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003177 /* We shouldn't try to resynchronize ... */
3178 }
3179 NEXT;
3180
3181 /*
3182 * Create or update the document accordingly to the DOCTYPE
3183 */
3184 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3185 (!ctxt->disableSAX))
3186 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3187
3188 /*
3189 * Cleanup, since we don't use all those identifiers
3190 */
3191 if (URI != NULL) xmlFree(URI);
3192 if (ExternalID != NULL) xmlFree(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003193}
3194
3195/**
3196 * htmlParseAttribute:
3197 * @ctxt: an HTML parser context
3198 * @value: a xmlChar ** used to store the value of the attribute
3199 *
3200 * parse an attribute
3201 *
3202 * [41] Attribute ::= Name Eq AttValue
3203 *
3204 * [25] Eq ::= S? '=' S?
3205 *
3206 * With namespace:
3207 *
3208 * [NS 11] Attribute ::= QName Eq AttValue
3209 *
3210 * Also the case QName == xmlns:??? is handled independently as a namespace
3211 * definition.
3212 *
3213 * Returns the attribute name, and the value in *value.
3214 */
3215
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003216static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003217htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003218 const xmlChar *name;
3219 xmlChar *val = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003220
3221 *value = NULL;
3222 name = htmlParseHTMLName(ctxt);
3223 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003224 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3225 "error parsing attribute name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003226 return(NULL);
3227 }
3228
3229 /*
3230 * read the value
3231 */
3232 SKIP_BLANKS;
3233 if (CUR == '=') {
3234 NEXT;
3235 SKIP_BLANKS;
3236 val = htmlParseAttValue(ctxt);
3237 /******
3238 } else {
3239 * TODO : some attribute must have values, some may not
3240 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3241 ctxt->sax->warning(ctxt->userData,
3242 "No value for attribute %s\n", name); */
3243 }
3244
3245 *value = val;
3246 return(name);
3247}
3248
3249/**
3250 * htmlCheckEncoding:
3251 * @ctxt: an HTML parser context
3252 * @attvalue: the attribute value
3253 *
3254 * Checks an http-equiv attribute from a Meta tag to detect
3255 * the encoding
3256 * If a new encoding is detected the parser is switched to decode
3257 * it and pass UTF8
3258 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003259static void
Owen Taylor3473f882001-02-23 17:55:21 +00003260htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3261 const xmlChar *encoding;
3262
3263 if ((ctxt == NULL) || (attvalue == NULL))
3264 return;
3265
3266 /* do not change encoding */
3267 if (ctxt->input->encoding != NULL)
3268 return;
3269
3270 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
3271 if (encoding != NULL) {
3272 encoding += 8;
3273 } else {
3274 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
3275 if (encoding != NULL)
3276 encoding += 9;
3277 }
3278 if (encoding != NULL) {
3279 xmlCharEncoding enc;
3280 xmlCharEncodingHandlerPtr handler;
3281
3282 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3283
3284 if (ctxt->input->encoding != NULL)
3285 xmlFree((xmlChar *) ctxt->input->encoding);
3286 ctxt->input->encoding = xmlStrdup(encoding);
3287
3288 enc = xmlParseCharEncoding((const char *) encoding);
3289 /*
3290 * registered set of known encodings
3291 */
3292 if (enc != XML_CHAR_ENCODING_ERROR) {
3293 xmlSwitchEncoding(ctxt, enc);
3294 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3295 } else {
3296 /*
3297 * fallback for unknown encodings
3298 */
3299 handler = xmlFindCharEncodingHandler((const char *) encoding);
3300 if (handler != NULL) {
3301 xmlSwitchToEncoding(ctxt, handler);
3302 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3303 } else {
3304 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3305 }
3306 }
3307
3308 if ((ctxt->input->buf != NULL) &&
3309 (ctxt->input->buf->encoder != NULL) &&
3310 (ctxt->input->buf->raw != NULL) &&
3311 (ctxt->input->buf->buffer != NULL)) {
3312 int nbchars;
3313 int processed;
3314
3315 /*
3316 * convert as much as possible to the parser reading buffer.
3317 */
3318 processed = ctxt->input->cur - ctxt->input->base;
3319 xmlBufferShrink(ctxt->input->buf->buffer, processed);
3320 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
3321 ctxt->input->buf->buffer,
3322 ctxt->input->buf->raw);
3323 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003324 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3325 "htmlCheckEncoding: encoder error\n",
3326 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003327 }
3328 ctxt->input->base =
3329 ctxt->input->cur = ctxt->input->buf->buffer->content;
3330 }
3331 }
3332}
3333
3334/**
3335 * htmlCheckMeta:
3336 * @ctxt: an HTML parser context
3337 * @atts: the attributes values
3338 *
3339 * Checks an attributes from a Meta tag
3340 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003341static void
Owen Taylor3473f882001-02-23 17:55:21 +00003342htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3343 int i;
3344 const xmlChar *att, *value;
3345 int http = 0;
3346 const xmlChar *content = NULL;
3347
3348 if ((ctxt == NULL) || (atts == NULL))
3349 return;
3350
3351 i = 0;
3352 att = atts[i++];
3353 while (att != NULL) {
3354 value = atts[i++];
3355 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3356 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3357 http = 1;
3358 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3359 content = value;
3360 att = atts[i++];
3361 }
3362 if ((http) && (content != NULL))
3363 htmlCheckEncoding(ctxt, content);
3364
3365}
3366
3367/**
3368 * htmlParseStartTag:
3369 * @ctxt: an HTML parser context
3370 *
3371 * parse a start of tag either for rule element or
3372 * EmptyElement. In both case we don't parse the tag closing chars.
3373 *
3374 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3375 *
3376 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3377 *
3378 * With namespace:
3379 *
3380 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3381 *
3382 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3383 *
Daniel Veillard597f1c12005-07-03 23:00:18 +00003384 * Returns 0 in case of success and -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003385 */
3386
Daniel Veillard597f1c12005-07-03 23:00:18 +00003387static int
Owen Taylor3473f882001-02-23 17:55:21 +00003388htmlParseStartTag(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003389 const xmlChar *name;
3390 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00003391 xmlChar *attvalue;
Daniel Veillardf403d292003-10-05 13:51:35 +00003392 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00003393 int nbatts = 0;
Daniel Veillardf403d292003-10-05 13:51:35 +00003394 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003395 int meta = 0;
3396 int i;
3397
Daniel Veillarda03e3652004-11-02 18:45:30 +00003398 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3399 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3400 "htmlParseStartTag: context error\n", NULL, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003401 return -1;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003402 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003403 if (CUR != '<') return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003404 NEXT;
3405
3406 GROW;
3407 name = htmlParseHTMLName(ctxt);
3408 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003409 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3410 "htmlParseStartTag: invalid element name\n",
3411 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003412 /* Dump the bogus tag like browsers do */
William M. Brack76e95df2003-10-18 16:20:14 +00003413 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
Owen Taylor3473f882001-02-23 17:55:21 +00003414 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003415 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003416 }
3417 if (xmlStrEqual(name, BAD_CAST"meta"))
3418 meta = 1;
3419
3420 /*
3421 * Check for auto-closure of HTML elements.
3422 */
3423 htmlAutoClose(ctxt, name);
3424
3425 /*
3426 * Check for implied HTML elements.
3427 */
3428 htmlCheckImplied(ctxt, name);
3429
3430 /*
3431 * Avoid html at any level > 0, head at any level != 1
3432 * or any attempt to recurse body
3433 */
3434 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003435 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3436 "htmlParseStartTag: misplaced <html> tag\n",
3437 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003438 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003439 }
3440 if ((ctxt->nameNr != 1) &&
3441 (xmlStrEqual(name, BAD_CAST"head"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003442 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3443 "htmlParseStartTag: misplaced <head> tag\n",
3444 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003445 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003446 }
3447 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003448 int indx;
3449 for (indx = 0;indx < ctxt->nameNr;indx++) {
3450 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003451 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3452 "htmlParseStartTag: misplaced <body> tag\n",
3453 name, NULL);
Daniel Veillardc59d8262003-11-20 21:59:12 +00003454 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
3455 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003456 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003457 }
3458 }
3459 }
3460
3461 /*
3462 * Now parse the attributes, it ends up with the ending
3463 *
3464 * (S Attribute)* S?
3465 */
3466 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003467 while ((IS_CHAR_CH(CUR)) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003468 (CUR != '>') &&
3469 ((CUR != '/') || (NXT(1) != '>'))) {
3470 long cons = ctxt->nbChars;
3471
3472 GROW;
3473 attname = htmlParseAttribute(ctxt, &attvalue);
3474 if (attname != NULL) {
3475
3476 /*
3477 * Well formedness requires at most one declaration of an attribute
3478 */
3479 for (i = 0; i < nbatts;i += 2) {
3480 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003481 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
3482 "Attribute %s redefined\n", attname, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003483 if (attvalue != NULL)
3484 xmlFree(attvalue);
3485 goto failed;
3486 }
3487 }
3488
3489 /*
3490 * Add the pair to atts
3491 */
3492 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003493 maxatts = 22; /* allow for 10 attrs by default */
3494 atts = (const xmlChar **)
3495 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003496 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003497 htmlErrMemory(ctxt, NULL);
3498 if (attvalue != NULL)
3499 xmlFree(attvalue);
3500 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003501 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003502 ctxt->atts = atts;
3503 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003504 } else if (nbatts + 4 > maxatts) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003505 const xmlChar **n;
3506
Owen Taylor3473f882001-02-23 17:55:21 +00003507 maxatts *= 2;
Daniel Veillardf403d292003-10-05 13:51:35 +00003508 n = (const xmlChar **) xmlRealloc((void *) atts,
3509 maxatts * sizeof(const xmlChar *));
3510 if (n == NULL) {
3511 htmlErrMemory(ctxt, NULL);
3512 if (attvalue != NULL)
3513 xmlFree(attvalue);
3514 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003515 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003516 atts = n;
3517 ctxt->atts = atts;
3518 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003519 }
3520 atts[nbatts++] = attname;
3521 atts[nbatts++] = attvalue;
3522 atts[nbatts] = NULL;
3523 atts[nbatts + 1] = NULL;
3524 }
3525 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003526 if (attvalue != NULL)
3527 xmlFree(attvalue);
Owen Taylor3473f882001-02-23 17:55:21 +00003528 /* Dump the bogus attribute string up to the next blank or
3529 * the end of the tag. */
William M. Brack76e95df2003-10-18 16:20:14 +00003530 while ((IS_CHAR_CH(CUR)) &&
3531 !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
Daniel Veillard34ba3872003-07-15 13:34:05 +00003532 ((CUR != '/') || (NXT(1) != '>')))
Owen Taylor3473f882001-02-23 17:55:21 +00003533 NEXT;
3534 }
3535
3536failed:
3537 SKIP_BLANKS;
3538 if (cons == ctxt->nbChars) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003539 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3540 "htmlParseStartTag: problem parsing attributes\n",
3541 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003542 break;
3543 }
3544 }
3545
3546 /*
3547 * Handle specific association to the META tag
3548 */
3549 if (meta)
3550 htmlCheckMeta(ctxt, atts);
3551
3552 /*
3553 * SAX: Start of Element !
3554 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003555 htmlnamePush(ctxt, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003556 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
3557 if (nbatts != 0)
3558 ctxt->sax->startElement(ctxt->userData, name, atts);
3559 else
3560 ctxt->sax->startElement(ctxt->userData, name, NULL);
3561 }
Owen Taylor3473f882001-02-23 17:55:21 +00003562
3563 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003564 for (i = 1;i < nbatts;i += 2) {
Owen Taylor3473f882001-02-23 17:55:21 +00003565 if (atts[i] != NULL)
3566 xmlFree((xmlChar *) atts[i]);
3567 }
Owen Taylor3473f882001-02-23 17:55:21 +00003568 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003569
3570 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003571}
3572
3573/**
3574 * htmlParseEndTag:
3575 * @ctxt: an HTML parser context
3576 *
3577 * parse an end of tag
3578 *
3579 * [42] ETag ::= '</' Name S? '>'
3580 *
3581 * With namespace
3582 *
3583 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003584 *
3585 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003586 */
3587
Daniel Veillardf420ac52001-07-04 16:04:09 +00003588static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003589htmlParseEndTag(htmlParserCtxtPtr ctxt)
3590{
3591 const xmlChar *name;
3592 const xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003593 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003594
3595 if ((CUR != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003596 htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
3597 "htmlParseEndTag: '</' not found\n", NULL, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003598 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003599 }
3600 SKIP(2);
3601
3602 name = htmlParseHTMLName(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003603 if (name == NULL)
3604 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003605
3606 /*
3607 * We should definitely be at the ending "S? '>'" part
3608 */
3609 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003610 if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003611 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3612 "End tag : expected '>'\n", NULL, NULL);
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00003613 if (ctxt->recovery) {
3614 /*
3615 * We're not at the ending > !!
3616 * Error, unless in recover mode where we search forwards
3617 * until we find a >
3618 */
3619 while (CUR != '\0' && CUR != '>') NEXT;
3620 NEXT;
3621 }
Owen Taylor3473f882001-02-23 17:55:21 +00003622 } else
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003623 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003624
3625 /*
3626 * If the name read is not one of the element in the parsing stack
3627 * then return, it's just an error.
3628 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003629 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
3630 if (xmlStrEqual(name, ctxt->nameTab[i]))
3631 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003632 }
3633 if (i < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003634 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3635 "Unexpected end tag : %s\n", name, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003636 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003637 }
3638
3639
3640 /*
3641 * Check for auto-closure of HTML elements.
3642 */
3643
3644 htmlAutoCloseOnClose(ctxt, name);
3645
3646 /*
3647 * Well formedness constraints, opening and closing must match.
3648 * With the exception that the autoclose may have popped stuff out
3649 * of the stack.
3650 */
3651 if (!xmlStrEqual(name, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003652 if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003653 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3654 "Opening and ending tag mismatch: %s and %s\n",
3655 name, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003656 }
3657 }
3658
3659 /*
3660 * SAX: End of Tag
3661 */
3662 oldname = ctxt->name;
3663 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003664 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3665 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003666 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003667 ret = 1;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003668 } else {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003669 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003670 }
3671
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003672 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003673}
3674
3675
3676/**
3677 * htmlParseReference:
3678 * @ctxt: an HTML parser context
3679 *
3680 * parse and handle entity references in content,
3681 * this will end-up in a call to character() since this is either a
3682 * CharRef, or a predefined entity.
3683 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003684static void
Owen Taylor3473f882001-02-23 17:55:21 +00003685htmlParseReference(htmlParserCtxtPtr ctxt) {
Daniel Veillardbb371292001-08-16 23:26:59 +00003686 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00003687 xmlChar out[6];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003688 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003689 if (CUR != '&') return;
3690
3691 if (NXT(1) == '#') {
3692 unsigned int c;
3693 int bits, i = 0;
3694
3695 c = htmlParseCharRef(ctxt);
3696 if (c == 0)
3697 return;
3698
3699 if (c < 0x80) { out[i++]= c; bits= -6; }
3700 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3701 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3702 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3703
3704 for ( ; bits >= 0; bits-= 6) {
3705 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3706 }
3707 out[i] = 0;
3708
3709 htmlCheckParagraph(ctxt);
3710 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3711 ctxt->sax->characters(ctxt->userData, out, i);
3712 } else {
3713 ent = htmlParseEntityRef(ctxt, &name);
3714 if (name == NULL) {
3715 htmlCheckParagraph(ctxt);
3716 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3717 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3718 return;
3719 }
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003720 if ((ent == NULL) || !(ent->value > 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003721 htmlCheckParagraph(ctxt);
3722 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3723 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3724 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3725 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3726 }
3727 } else {
3728 unsigned int c;
3729 int bits, i = 0;
3730
3731 c = ent->value;
3732 if (c < 0x80)
3733 { out[i++]= c; bits= -6; }
3734 else if (c < 0x800)
3735 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3736 else if (c < 0x10000)
3737 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3738 else
3739 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3740
3741 for ( ; bits >= 0; bits-= 6) {
3742 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3743 }
3744 out[i] = 0;
3745
3746 htmlCheckParagraph(ctxt);
3747 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3748 ctxt->sax->characters(ctxt->userData, out, i);
3749 }
Owen Taylor3473f882001-02-23 17:55:21 +00003750 }
3751}
3752
3753/**
3754 * htmlParseContent:
3755 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00003756 *
3757 * Parse a content: comment, sub-element, reference or text.
Owen Taylor3473f882001-02-23 17:55:21 +00003758 */
3759
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003760static void
Owen Taylor3473f882001-02-23 17:55:21 +00003761htmlParseContent(htmlParserCtxtPtr ctxt) {
3762 xmlChar *currentNode;
3763 int depth;
3764
3765 currentNode = xmlStrdup(ctxt->name);
3766 depth = ctxt->nameNr;
3767 while (1) {
3768 long cons = ctxt->nbChars;
3769
3770 GROW;
3771 /*
3772 * Our tag or one of it's parent or children is ending.
3773 */
3774 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003775 if (htmlParseEndTag(ctxt) &&
3776 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3777 if (currentNode != NULL)
3778 xmlFree(currentNode);
3779 return;
3780 }
3781 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003782 }
3783
3784 /*
3785 * Has this node been popped out during parsing of
3786 * the next element
3787 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003788 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3789 (!xmlStrEqual(currentNode, ctxt->name)))
3790 {
Owen Taylor3473f882001-02-23 17:55:21 +00003791 if (currentNode != NULL) xmlFree(currentNode);
3792 return;
3793 }
3794
Daniel Veillardf9533d12001-03-03 10:04:57 +00003795 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3796 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003797 /*
3798 * Handle SCRIPT/STYLE separately
3799 */
3800 htmlParseScript(ctxt);
3801 } else {
3802 /*
3803 * Sometimes DOCTYPE arrives in the middle of the document
3804 */
3805 if ((CUR == '<') && (NXT(1) == '!') &&
3806 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3807 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3808 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3809 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003810 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3811 "Misplaced DOCTYPE declaration\n",
3812 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003813 htmlParseDocTypeDecl(ctxt);
3814 }
3815
3816 /*
3817 * First case : a comment
3818 */
3819 if ((CUR == '<') && (NXT(1) == '!') &&
3820 (NXT(2) == '-') && (NXT(3) == '-')) {
3821 htmlParseComment(ctxt);
3822 }
3823
3824 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003825 * Second case : a Processing Instruction.
3826 */
3827 else if ((CUR == '<') && (NXT(1) == '?')) {
3828 htmlParsePI(ctxt);
3829 }
3830
3831 /*
3832 * Third case : a sub-element.
Owen Taylor3473f882001-02-23 17:55:21 +00003833 */
3834 else if (CUR == '<') {
3835 htmlParseElement(ctxt);
3836 }
3837
3838 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003839 * Fourth case : a reference. If if has not been resolved,
Owen Taylor3473f882001-02-23 17:55:21 +00003840 * parsing returns it's Name, create the node
3841 */
3842 else if (CUR == '&') {
3843 htmlParseReference(ctxt);
3844 }
3845
3846 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003847 * Fifth case : end of the resource
Owen Taylor3473f882001-02-23 17:55:21 +00003848 */
3849 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003850 htmlAutoCloseOnEnd(ctxt);
3851 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003852 }
3853
3854 /*
3855 * Last case, text. Note that References are handled directly.
3856 */
3857 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003858 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003859 }
3860
3861 if (cons == ctxt->nbChars) {
3862 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003863 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3864 "detected an error in element content\n",
3865 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003866 }
3867 break;
3868 }
3869 }
3870 GROW;
3871 }
3872 if (currentNode != NULL) xmlFree(currentNode);
3873}
3874
3875/**
Daniel Veillard499cc922006-01-18 17:22:35 +00003876 * htmlParseContent:
3877 * @ctxt: an HTML parser context
3878 *
3879 * Parse a content: comment, sub-element, reference or text.
3880 */
3881
3882void
3883__htmlParseContent(void *ctxt) {
3884 if (ctxt != NULL)
3885 htmlParseContent((htmlParserCtxtPtr) ctxt);
3886}
3887
3888/**
Owen Taylor3473f882001-02-23 17:55:21 +00003889 * htmlParseElement:
3890 * @ctxt: an HTML parser context
3891 *
3892 * parse an HTML element, this is highly recursive
3893 *
3894 * [39] element ::= EmptyElemTag | STag content ETag
3895 *
3896 * [41] Attribute ::= Name Eq AttValue
3897 */
3898
3899void
3900htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003901 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003902 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00003903 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00003904 htmlParserNodeInfo node_info;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003905 int failed;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003906 int depth;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00003907 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00003908
Daniel Veillarda03e3652004-11-02 18:45:30 +00003909 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3910 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard597f1c12005-07-03 23:00:18 +00003911 "htmlParseElement: context error\n", NULL, NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003912 return;
3913 }
Owen Taylor3473f882001-02-23 17:55:21 +00003914 /* Capture start position */
3915 if (ctxt->record_info) {
3916 node_info.begin_pos = ctxt->input->consumed +
3917 (CUR_PTR - ctxt->input->base);
3918 node_info.begin_line = ctxt->input->line;
3919 }
3920
Daniel Veillard597f1c12005-07-03 23:00:18 +00003921 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003922 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003923 if (failed || (name == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003924 if (CUR == '>')
3925 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003926 return;
3927 }
Owen Taylor3473f882001-02-23 17:55:21 +00003928
3929 /*
3930 * Lookup the info for that element.
3931 */
3932 info = htmlTagLookup(name);
3933 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003934 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
3935 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003936 }
3937
3938 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003939 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00003940 */
3941 if ((CUR == '/') && (NXT(1) == '>')) {
3942 SKIP(2);
3943 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3944 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003945 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003946 return;
3947 }
3948
3949 if (CUR == '>') {
3950 NEXT;
3951 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003952 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3953 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003954
3955 /*
3956 * end of parsing of this node.
3957 */
3958 if (xmlStrEqual(name, ctxt->name)) {
3959 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00003960 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003961 }
3962
3963 /*
3964 * Capture end position and add node
3965 */
3966 if ( currentNode != NULL && ctxt->record_info ) {
3967 node_info.end_pos = ctxt->input->consumed +
3968 (CUR_PTR - ctxt->input->base);
3969 node_info.end_line = ctxt->input->line;
3970 node_info.node = ctxt->node;
3971 xmlParserAddNodeInfo(ctxt, &node_info);
3972 }
3973 return;
3974 }
3975
3976 /*
3977 * Check for an Empty Element from DTD definition
3978 */
3979 if ((info != NULL) && (info->empty)) {
3980 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3981 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003982 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003983 return;
3984 }
3985
3986 /*
3987 * Parse the content of the element:
3988 */
3989 currentNode = xmlStrdup(ctxt->name);
3990 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00003991 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00003992 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00003993 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00003994 if (oldptr==ctxt->input->cur) break;
Owen Taylor3473f882001-02-23 17:55:21 +00003995 if (ctxt->nameNr < depth) break;
3996 }
3997
Owen Taylor3473f882001-02-23 17:55:21 +00003998 /*
3999 * Capture end position and add node
4000 */
4001 if ( currentNode != NULL && ctxt->record_info ) {
4002 node_info.end_pos = ctxt->input->consumed +
4003 (CUR_PTR - ctxt->input->base);
4004 node_info.end_line = ctxt->input->line;
4005 node_info.node = ctxt->node;
4006 xmlParserAddNodeInfo(ctxt, &node_info);
4007 }
William M. Brack76e95df2003-10-18 16:20:14 +00004008 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004009 htmlAutoCloseOnEnd(ctxt);
4010 }
4011
Owen Taylor3473f882001-02-23 17:55:21 +00004012 if (currentNode != NULL)
4013 xmlFree(currentNode);
4014}
4015
4016/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004017 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00004018 * @ctxt: an HTML parser context
4019 *
4020 * parse an HTML document (and build a tree if using the standard SAX
4021 * interface).
4022 *
4023 * Returns 0, -1 in case of error. the parser context is augmented
4024 * as a result of the parsing.
4025 */
4026
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00004027int
Owen Taylor3473f882001-02-23 17:55:21 +00004028htmlParseDocument(htmlParserCtxtPtr ctxt) {
4029 xmlDtdPtr dtd;
4030
Daniel Veillardd0463562001-10-13 09:15:48 +00004031 xmlInitParser();
4032
Owen Taylor3473f882001-02-23 17:55:21 +00004033 htmlDefaultSAXHandlerInit();
Owen Taylor3473f882001-02-23 17:55:21 +00004034
Daniel Veillarda03e3652004-11-02 18:45:30 +00004035 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4036 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4037 "htmlParseDocument: context error\n", NULL, NULL);
4038 return(XML_ERR_INTERNAL_ERROR);
4039 }
4040 ctxt->html = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004041 GROW;
4042 /*
4043 * SAX: beginning of the document processing.
4044 */
4045 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4046 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4047
4048 /*
4049 * Wipe out everything which is before the first '<'
4050 */
4051 SKIP_BLANKS;
4052 if (CUR == 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004053 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
4054 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004055 }
4056
4057 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4058 ctxt->sax->startDocument(ctxt->userData);
4059
4060
4061 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004062 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004063 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004064 while (((CUR == '<') && (NXT(1) == '!') &&
4065 (NXT(2) == '-') && (NXT(3) == '-')) ||
4066 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004067 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004068 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004069 SKIP_BLANKS;
4070 }
4071
4072
4073 /*
4074 * Then possibly doc type declaration(s) and more Misc
4075 * (doctypedecl Misc*)?
4076 */
4077 if ((CUR == '<') && (NXT(1) == '!') &&
4078 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4079 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4080 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4081 (UPP(8) == 'E')) {
4082 htmlParseDocTypeDecl(ctxt);
4083 }
4084 SKIP_BLANKS;
4085
4086 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004087 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004088 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004089 while (((CUR == '<') && (NXT(1) == '!') &&
4090 (NXT(2) == '-') && (NXT(3) == '-')) ||
4091 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004092 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004093 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004094 SKIP_BLANKS;
4095 }
4096
4097 /*
4098 * Time to start parsing the tree itself
4099 */
4100 htmlParseContent(ctxt);
4101
4102 /*
4103 * autoclose
4104 */
4105 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004106 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004107
4108
4109 /*
4110 * SAX: end of the document processing.
4111 */
4112 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4113 ctxt->sax->endDocument(ctxt->userData);
4114
4115 if (ctxt->myDoc != NULL) {
4116 dtd = xmlGetIntSubset(ctxt->myDoc);
4117 if (dtd == NULL)
4118 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00004119 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004120 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4121 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4122 }
4123 if (! ctxt->wellFormed) return(-1);
4124 return(0);
4125}
4126
4127
4128/************************************************************************
4129 * *
4130 * Parser contexts handling *
4131 * *
4132 ************************************************************************/
4133
4134/**
William M. Brackedb65a72004-02-06 07:36:04 +00004135 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004136 * @ctxt: an HTML parser context
4137 *
4138 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00004139 *
4140 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00004141 */
4142
Daniel Veillardf403d292003-10-05 13:51:35 +00004143static int
Owen Taylor3473f882001-02-23 17:55:21 +00004144htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
4145{
4146 htmlSAXHandler *sax;
4147
Daniel Veillardf403d292003-10-05 13:51:35 +00004148 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004149 memset(ctxt, 0, sizeof(htmlParserCtxt));
4150
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004151 ctxt->dict = xmlDictCreate();
4152 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004153 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4154 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004155 }
Owen Taylor3473f882001-02-23 17:55:21 +00004156 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
4157 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004158 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4159 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004160 }
4161 else
4162 memset(sax, 0, sizeof(htmlSAXHandler));
4163
4164 /* Allocate the Input stack */
4165 ctxt->inputTab = (htmlParserInputPtr *)
4166 xmlMalloc(5 * sizeof(htmlParserInputPtr));
4167 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004168 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004169 ctxt->inputNr = 0;
4170 ctxt->inputMax = 0;
4171 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004172 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004173 }
4174 ctxt->inputNr = 0;
4175 ctxt->inputMax = 5;
4176 ctxt->input = NULL;
4177 ctxt->version = NULL;
4178 ctxt->encoding = NULL;
4179 ctxt->standalone = -1;
4180 ctxt->instate = XML_PARSER_START;
4181
4182 /* Allocate the Node stack */
4183 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
4184 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004185 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004186 ctxt->nodeNr = 0;
4187 ctxt->nodeMax = 0;
4188 ctxt->node = NULL;
4189 ctxt->inputNr = 0;
4190 ctxt->inputMax = 0;
4191 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004192 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004193 }
4194 ctxt->nodeNr = 0;
4195 ctxt->nodeMax = 10;
4196 ctxt->node = NULL;
4197
4198 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004199 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00004200 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004201 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004202 ctxt->nameNr = 0;
4203 ctxt->nameMax = 10;
4204 ctxt->name = NULL;
4205 ctxt->nodeNr = 0;
4206 ctxt->nodeMax = 0;
4207 ctxt->node = NULL;
4208 ctxt->inputNr = 0;
4209 ctxt->inputMax = 0;
4210 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004211 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004212 }
4213 ctxt->nameNr = 0;
4214 ctxt->nameMax = 10;
4215 ctxt->name = NULL;
4216
Daniel Veillard092643b2003-09-25 14:29:29 +00004217 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00004218 else {
4219 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00004220 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00004221 }
4222 ctxt->userData = ctxt;
4223 ctxt->myDoc = NULL;
4224 ctxt->wellFormed = 1;
4225 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00004226 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00004227 ctxt->html = 1;
Daniel Veillardeff45a92004-10-29 12:10:55 +00004228 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
William M. Brackedb65a72004-02-06 07:36:04 +00004229 ctxt->vctxt.userData = ctxt;
4230 ctxt->vctxt.error = xmlParserValidityError;
4231 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00004232 ctxt->record_info = 0;
4233 ctxt->validate = 0;
4234 ctxt->nbChars = 0;
4235 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00004236 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004237 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00004238 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004239}
4240
4241/**
4242 * htmlFreeParserCtxt:
4243 * @ctxt: an HTML parser context
4244 *
4245 * Free all the memory used by a parser context. However the parsed
4246 * document in ctxt->myDoc is not freed.
4247 */
4248
4249void
4250htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4251{
4252 xmlFreeParserCtxt(ctxt);
4253}
4254
4255/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004256 * htmlNewParserCtxt:
4257 *
4258 * Allocate and initialize a new parser context.
4259 *
4260 * Returns the xmlParserCtxtPtr or NULL
4261 */
4262
4263static htmlParserCtxtPtr
4264htmlNewParserCtxt(void)
4265{
4266 xmlParserCtxtPtr ctxt;
4267
4268 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4269 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004270 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004271 return(NULL);
4272 }
4273 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004274 if (htmlInitParserCtxt(ctxt) < 0) {
4275 htmlFreeParserCtxt(ctxt);
4276 return(NULL);
4277 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004278 return(ctxt);
4279}
4280
4281/**
4282 * htmlCreateMemoryParserCtxt:
4283 * @buffer: a pointer to a char array
4284 * @size: the size of the array
4285 *
4286 * Create a parser context for an HTML in-memory document.
4287 *
4288 * Returns the new parser context or NULL
4289 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004290htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004291htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4292 xmlParserCtxtPtr ctxt;
4293 xmlParserInputPtr input;
4294 xmlParserInputBufferPtr buf;
4295
4296 if (buffer == NULL)
4297 return(NULL);
4298 if (size <= 0)
4299 return(NULL);
4300
4301 ctxt = htmlNewParserCtxt();
4302 if (ctxt == NULL)
4303 return(NULL);
4304
4305 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4306 if (buf == NULL) return(NULL);
4307
4308 input = xmlNewInputStream(ctxt);
4309 if (input == NULL) {
4310 xmlFreeParserCtxt(ctxt);
4311 return(NULL);
4312 }
4313
4314 input->filename = NULL;
4315 input->buf = buf;
4316 input->base = input->buf->buffer->content;
4317 input->cur = input->buf->buffer->content;
4318 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4319
4320 inputPush(ctxt, input);
4321 return(ctxt);
4322}
4323
4324/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004325 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004326 * @cur: a pointer to an array of xmlChar
4327 * @encoding: a free form C string describing the HTML document encoding, or NULL
4328 *
4329 * Create a parser context for an HTML document.
4330 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004331 * TODO: check the need to add encoding handling there
4332 *
Owen Taylor3473f882001-02-23 17:55:21 +00004333 * Returns the new parser context or NULL
4334 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004335static htmlParserCtxtPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004336htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004337 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004338 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004339
Daniel Veillard1d995272002-07-22 16:43:32 +00004340 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004341 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004342 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004343 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
4344
4345 if (encoding != NULL) {
4346 xmlCharEncoding enc;
4347 xmlCharEncodingHandlerPtr handler;
4348
4349 if (ctxt->input->encoding != NULL)
4350 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004351 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004352
4353 enc = xmlParseCharEncoding(encoding);
4354 /*
4355 * registered set of known encodings
4356 */
4357 if (enc != XML_CHAR_ENCODING_ERROR) {
4358 xmlSwitchEncoding(ctxt, enc);
4359 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
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 } else {
4365 /*
4366 * fallback for unknown encodings
4367 */
4368 handler = xmlFindCharEncodingHandler((const char *) encoding);
4369 if (handler != NULL) {
4370 xmlSwitchToEncoding(ctxt, handler);
4371 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004372 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4373 "Unsupported encoding %s\n",
4374 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004375 }
4376 }
4377 }
4378 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004379}
4380
Daniel Veillard73b013f2003-09-30 12:36:01 +00004381#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004382/************************************************************************
4383 * *
4384 * Progressive parsing interfaces *
4385 * *
4386 ************************************************************************/
4387
4388/**
4389 * htmlParseLookupSequence:
4390 * @ctxt: an HTML parser context
4391 * @first: the first char to lookup
4392 * @next: the next char to lookup or zero
4393 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004394 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004395 *
4396 * Try to find if a sequence (first, next, third) or just (first next) or
4397 * (first) is available in the input stream.
4398 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4399 * to avoid rescanning sequences of bytes, it DOES change the state of the
4400 * parser, do not use liberally.
4401 * This is basically similar to xmlParseLookupSequence()
4402 *
4403 * Returns the index to the current parsing point if the full sequence
4404 * is available, -1 otherwise.
4405 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004406static int
Owen Taylor3473f882001-02-23 17:55:21 +00004407htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
William M. Brackc1939562003-08-05 15:52:22 +00004408 xmlChar next, xmlChar third, int iscomment) {
Owen Taylor3473f882001-02-23 17:55:21 +00004409 int base, len;
4410 htmlParserInputPtr in;
4411 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004412 int incomment = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004413
4414 in = ctxt->input;
4415 if (in == NULL) return(-1);
4416 base = in->cur - in->base;
4417 if (base < 0) return(-1);
4418 if (ctxt->checkIndex > base)
4419 base = ctxt->checkIndex;
4420 if (in->buf == NULL) {
4421 buf = in->base;
4422 len = in->length;
4423 } else {
4424 buf = in->buf->buffer->content;
4425 len = in->buf->buffer->use;
4426 }
4427 /* take into account the sequence length */
4428 if (third) len -= 2;
4429 else if (next) len --;
4430 for (;base < len;base++) {
William M. Brackc1939562003-08-05 15:52:22 +00004431 if (!incomment && (base + 4 < len) && !iscomment) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00004432 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
4433 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
4434 incomment = 1;
Daniel Veillard97e01882003-07-30 18:59:19 +00004435 /* do not increment past <! - some people use <!--> */
4436 base += 2;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004437 }
Daniel Veillardc1f78342001-11-10 11:43:05 +00004438 }
4439 if (incomment) {
William M. Brack4a557d92003-07-29 04:28:04 +00004440 if (base + 3 > len)
Daniel Veillardc1f78342001-11-10 11:43:05 +00004441 return(-1);
4442 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
4443 (buf[base + 2] == '>')) {
4444 incomment = 0;
4445 base += 2;
4446 }
4447 continue;
4448 }
Owen Taylor3473f882001-02-23 17:55:21 +00004449 if (buf[base] == first) {
4450 if (third != 0) {
4451 if ((buf[base + 1] != next) ||
4452 (buf[base + 2] != third)) continue;
4453 } else if (next != 0) {
4454 if (buf[base + 1] != next) continue;
4455 }
4456 ctxt->checkIndex = 0;
4457#ifdef DEBUG_PUSH
4458 if (next == 0)
4459 xmlGenericError(xmlGenericErrorContext,
4460 "HPP: lookup '%c' found at %d\n",
4461 first, base);
4462 else if (third == 0)
4463 xmlGenericError(xmlGenericErrorContext,
4464 "HPP: lookup '%c%c' found at %d\n",
4465 first, next, base);
4466 else
4467 xmlGenericError(xmlGenericErrorContext,
4468 "HPP: lookup '%c%c%c' found at %d\n",
4469 first, next, third, base);
4470#endif
4471 return(base - (in->cur - in->base));
4472 }
4473 }
4474 ctxt->checkIndex = base;
4475#ifdef DEBUG_PUSH
4476 if (next == 0)
4477 xmlGenericError(xmlGenericErrorContext,
4478 "HPP: lookup '%c' failed\n", first);
4479 else if (third == 0)
4480 xmlGenericError(xmlGenericErrorContext,
4481 "HPP: lookup '%c%c' failed\n", first, next);
4482 else
4483 xmlGenericError(xmlGenericErrorContext,
4484 "HPP: lookup '%c%c%c' failed\n", first, next, third);
4485#endif
4486 return(-1);
4487}
4488
4489/**
4490 * htmlParseTryOrFinish:
4491 * @ctxt: an HTML parser context
4492 * @terminate: last chunk indicator
4493 *
4494 * Try to progress on parsing
4495 *
4496 * Returns zero if no parsing was possible
4497 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004498static int
Owen Taylor3473f882001-02-23 17:55:21 +00004499htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
4500 int ret = 0;
4501 htmlParserInputPtr in;
4502 int avail = 0;
4503 xmlChar cur, next;
4504
4505#ifdef DEBUG_PUSH
4506 switch (ctxt->instate) {
4507 case XML_PARSER_EOF:
4508 xmlGenericError(xmlGenericErrorContext,
4509 "HPP: try EOF\n"); break;
4510 case XML_PARSER_START:
4511 xmlGenericError(xmlGenericErrorContext,
4512 "HPP: try START\n"); break;
4513 case XML_PARSER_MISC:
4514 xmlGenericError(xmlGenericErrorContext,
4515 "HPP: try MISC\n");break;
4516 case XML_PARSER_COMMENT:
4517 xmlGenericError(xmlGenericErrorContext,
4518 "HPP: try COMMENT\n");break;
4519 case XML_PARSER_PROLOG:
4520 xmlGenericError(xmlGenericErrorContext,
4521 "HPP: try PROLOG\n");break;
4522 case XML_PARSER_START_TAG:
4523 xmlGenericError(xmlGenericErrorContext,
4524 "HPP: try START_TAG\n");break;
4525 case XML_PARSER_CONTENT:
4526 xmlGenericError(xmlGenericErrorContext,
4527 "HPP: try CONTENT\n");break;
4528 case XML_PARSER_CDATA_SECTION:
4529 xmlGenericError(xmlGenericErrorContext,
4530 "HPP: try CDATA_SECTION\n");break;
4531 case XML_PARSER_END_TAG:
4532 xmlGenericError(xmlGenericErrorContext,
4533 "HPP: try END_TAG\n");break;
4534 case XML_PARSER_ENTITY_DECL:
4535 xmlGenericError(xmlGenericErrorContext,
4536 "HPP: try ENTITY_DECL\n");break;
4537 case XML_PARSER_ENTITY_VALUE:
4538 xmlGenericError(xmlGenericErrorContext,
4539 "HPP: try ENTITY_VALUE\n");break;
4540 case XML_PARSER_ATTRIBUTE_VALUE:
4541 xmlGenericError(xmlGenericErrorContext,
4542 "HPP: try ATTRIBUTE_VALUE\n");break;
4543 case XML_PARSER_DTD:
4544 xmlGenericError(xmlGenericErrorContext,
4545 "HPP: try DTD\n");break;
4546 case XML_PARSER_EPILOG:
4547 xmlGenericError(xmlGenericErrorContext,
4548 "HPP: try EPILOG\n");break;
4549 case XML_PARSER_PI:
4550 xmlGenericError(xmlGenericErrorContext,
4551 "HPP: try PI\n");break;
4552 case XML_PARSER_SYSTEM_LITERAL:
4553 xmlGenericError(xmlGenericErrorContext,
4554 "HPP: try SYSTEM_LITERAL\n");break;
4555 }
4556#endif
4557
4558 while (1) {
4559
4560 in = ctxt->input;
4561 if (in == NULL) break;
4562 if (in->buf == NULL)
4563 avail = in->length - (in->cur - in->base);
4564 else
4565 avail = in->buf->buffer->use - (in->cur - in->base);
4566 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004567 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004568 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4569 /*
4570 * SAX: end of the document processing.
4571 */
4572 ctxt->instate = XML_PARSER_EOF;
4573 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4574 ctxt->sax->endDocument(ctxt->userData);
4575 }
4576 }
4577 if (avail < 1)
4578 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00004579 cur = in->cur[0];
4580 if (cur == 0) {
4581 SKIP(1);
4582 continue;
4583 }
4584
Owen Taylor3473f882001-02-23 17:55:21 +00004585 switch (ctxt->instate) {
4586 case XML_PARSER_EOF:
4587 /*
4588 * Document parsing is done !
4589 */
4590 goto done;
4591 case XML_PARSER_START:
4592 /*
4593 * Very first chars read from the document flow.
4594 */
4595 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004596 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004597 SKIP_BLANKS;
4598 if (in->buf == NULL)
4599 avail = in->length - (in->cur - in->base);
4600 else
4601 avail = in->buf->buffer->use - (in->cur - in->base);
4602 }
4603 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4604 ctxt->sax->setDocumentLocator(ctxt->userData,
4605 &xmlDefaultSAXLocator);
4606 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4607 (!ctxt->disableSAX))
4608 ctxt->sax->startDocument(ctxt->userData);
4609
4610 cur = in->cur[0];
4611 next = in->cur[1];
4612 if ((cur == '<') && (next == '!') &&
4613 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4614 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4615 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4616 (UPP(8) == 'E')) {
4617 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004618 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004619 goto done;
4620#ifdef DEBUG_PUSH
4621 xmlGenericError(xmlGenericErrorContext,
4622 "HPP: Parsing internal subset\n");
4623#endif
4624 htmlParseDocTypeDecl(ctxt);
4625 ctxt->instate = XML_PARSER_PROLOG;
4626#ifdef DEBUG_PUSH
4627 xmlGenericError(xmlGenericErrorContext,
4628 "HPP: entering PROLOG\n");
4629#endif
4630 } else {
4631 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004632#ifdef DEBUG_PUSH
Daniel Veillard597f1c12005-07-03 23:00:18 +00004633 xmlGenericError(xmlGenericErrorContext,
4634 "HPP: entering MISC\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004635#endif
Daniel Veillard597f1c12005-07-03 23:00:18 +00004636 }
Owen Taylor3473f882001-02-23 17:55:21 +00004637 break;
4638 case XML_PARSER_MISC:
4639 SKIP_BLANKS;
4640 if (in->buf == NULL)
4641 avail = in->length - (in->cur - in->base);
4642 else
4643 avail = in->buf->buffer->use - (in->cur - in->base);
4644 if (avail < 2)
4645 goto done;
4646 cur = in->cur[0];
4647 next = in->cur[1];
4648 if ((cur == '<') && (next == '!') &&
4649 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4650 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004651 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004652 goto done;
4653#ifdef DEBUG_PUSH
4654 xmlGenericError(xmlGenericErrorContext,
4655 "HPP: Parsing Comment\n");
4656#endif
4657 htmlParseComment(ctxt);
4658 ctxt->instate = XML_PARSER_MISC;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004659 } else if ((cur == '<') && (next == '?')) {
4660 if ((!terminate) &&
4661 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4662 goto done;
4663#ifdef DEBUG_PUSH
4664 xmlGenericError(xmlGenericErrorContext,
4665 "HPP: Parsing PI\n");
4666#endif
4667 htmlParsePI(ctxt);
4668 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004669 } else if ((cur == '<') && (next == '!') &&
4670 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4671 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4672 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4673 (UPP(8) == 'E')) {
4674 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004675 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004676 goto done;
4677#ifdef DEBUG_PUSH
4678 xmlGenericError(xmlGenericErrorContext,
4679 "HPP: Parsing internal subset\n");
4680#endif
4681 htmlParseDocTypeDecl(ctxt);
4682 ctxt->instate = XML_PARSER_PROLOG;
4683#ifdef DEBUG_PUSH
4684 xmlGenericError(xmlGenericErrorContext,
4685 "HPP: entering PROLOG\n");
4686#endif
4687 } else if ((cur == '<') && (next == '!') &&
4688 (avail < 9)) {
4689 goto done;
4690 } else {
4691 ctxt->instate = XML_PARSER_START_TAG;
4692#ifdef DEBUG_PUSH
4693 xmlGenericError(xmlGenericErrorContext,
4694 "HPP: entering START_TAG\n");
4695#endif
4696 }
4697 break;
4698 case XML_PARSER_PROLOG:
4699 SKIP_BLANKS;
4700 if (in->buf == NULL)
4701 avail = in->length - (in->cur - in->base);
4702 else
4703 avail = in->buf->buffer->use - (in->cur - in->base);
4704 if (avail < 2)
4705 goto done;
4706 cur = in->cur[0];
4707 next = in->cur[1];
4708 if ((cur == '<') && (next == '!') &&
4709 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4710 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004711 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004712 goto done;
4713#ifdef DEBUG_PUSH
4714 xmlGenericError(xmlGenericErrorContext,
4715 "HPP: Parsing Comment\n");
4716#endif
4717 htmlParseComment(ctxt);
4718 ctxt->instate = XML_PARSER_PROLOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004719 } else if ((cur == '<') && (next == '?')) {
4720 if ((!terminate) &&
4721 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4722 goto done;
4723#ifdef DEBUG_PUSH
4724 xmlGenericError(xmlGenericErrorContext,
4725 "HPP: Parsing PI\n");
4726#endif
4727 htmlParsePI(ctxt);
4728 ctxt->instate = XML_PARSER_PROLOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004729 } else if ((cur == '<') && (next == '!') &&
4730 (avail < 4)) {
4731 goto done;
4732 } else {
4733 ctxt->instate = XML_PARSER_START_TAG;
4734#ifdef DEBUG_PUSH
4735 xmlGenericError(xmlGenericErrorContext,
4736 "HPP: entering START_TAG\n");
4737#endif
4738 }
4739 break;
4740 case XML_PARSER_EPILOG:
4741 if (in->buf == NULL)
4742 avail = in->length - (in->cur - in->base);
4743 else
4744 avail = in->buf->buffer->use - (in->cur - in->base);
4745 if (avail < 1)
4746 goto done;
4747 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004748 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004749 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004750 goto done;
4751 }
4752 if (avail < 2)
4753 goto done;
4754 next = in->cur[1];
4755 if ((cur == '<') && (next == '!') &&
4756 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4757 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004758 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004759 goto done;
4760#ifdef DEBUG_PUSH
4761 xmlGenericError(xmlGenericErrorContext,
4762 "HPP: Parsing Comment\n");
4763#endif
4764 htmlParseComment(ctxt);
4765 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004766 } else if ((cur == '<') && (next == '?')) {
4767 if ((!terminate) &&
4768 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4769 goto done;
4770#ifdef DEBUG_PUSH
4771 xmlGenericError(xmlGenericErrorContext,
4772 "HPP: Parsing PI\n");
4773#endif
4774 htmlParsePI(ctxt);
4775 ctxt->instate = XML_PARSER_EPILOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004776 } else if ((cur == '<') && (next == '!') &&
4777 (avail < 4)) {
4778 goto done;
4779 } else {
4780 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004781 ctxt->wellFormed = 0;
4782 ctxt->instate = XML_PARSER_EOF;
4783#ifdef DEBUG_PUSH
4784 xmlGenericError(xmlGenericErrorContext,
4785 "HPP: entering EOF\n");
4786#endif
4787 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4788 ctxt->sax->endDocument(ctxt->userData);
4789 goto done;
4790 }
4791 break;
4792 case XML_PARSER_START_TAG: {
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004793 const xmlChar *name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004794 int failed;
Daniel Veillardbb371292001-08-16 23:26:59 +00004795 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004796
4797 if (avail < 2)
4798 goto done;
4799 cur = in->cur[0];
4800 if (cur != '<') {
4801 ctxt->instate = XML_PARSER_CONTENT;
4802#ifdef DEBUG_PUSH
4803 xmlGenericError(xmlGenericErrorContext,
4804 "HPP: entering CONTENT\n");
4805#endif
4806 break;
4807 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004808 if (in->cur[1] == '/') {
4809 ctxt->instate = XML_PARSER_END_TAG;
4810 ctxt->checkIndex = 0;
4811#ifdef DEBUG_PUSH
4812 xmlGenericError(xmlGenericErrorContext,
4813 "HPP: entering END_TAG\n");
4814#endif
4815 break;
4816 }
Owen Taylor3473f882001-02-23 17:55:21 +00004817 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004818 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004819 goto done;
4820
Daniel Veillard597f1c12005-07-03 23:00:18 +00004821 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004822 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004823 if (failed ||
Owen Taylor3473f882001-02-23 17:55:21 +00004824 (name == NULL)) {
4825 if (CUR == '>')
4826 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004827 break;
4828 }
Owen Taylor3473f882001-02-23 17:55:21 +00004829
4830 /*
4831 * Lookup the info for that element.
4832 */
4833 info = htmlTagLookup(name);
4834 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004835 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4836 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004837 }
4838
4839 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004840 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004841 */
4842 if ((CUR == '/') && (NXT(1) == '>')) {
4843 SKIP(2);
4844 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4845 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004846 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004847 ctxt->instate = XML_PARSER_CONTENT;
4848#ifdef DEBUG_PUSH
4849 xmlGenericError(xmlGenericErrorContext,
4850 "HPP: entering CONTENT\n");
4851#endif
4852 break;
4853 }
4854
4855 if (CUR == '>') {
4856 NEXT;
4857 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004858 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4859 "Couldn't find end of Start Tag %s\n",
4860 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004861
4862 /*
4863 * end of parsing of this node.
4864 */
4865 if (xmlStrEqual(name, ctxt->name)) {
4866 nodePop(ctxt);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004867 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004868 }
4869
4870 ctxt->instate = XML_PARSER_CONTENT;
4871#ifdef DEBUG_PUSH
4872 xmlGenericError(xmlGenericErrorContext,
4873 "HPP: entering CONTENT\n");
4874#endif
4875 break;
4876 }
4877
4878 /*
4879 * Check for an Empty Element from DTD definition
4880 */
4881 if ((info != NULL) && (info->empty)) {
4882 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4883 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004884 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004885 }
4886 ctxt->instate = XML_PARSER_CONTENT;
4887#ifdef DEBUG_PUSH
4888 xmlGenericError(xmlGenericErrorContext,
4889 "HPP: entering CONTENT\n");
4890#endif
4891 break;
4892 }
4893 case XML_PARSER_CONTENT: {
4894 long cons;
4895 /*
4896 * Handle preparsed entities and charRef
4897 */
4898 if (ctxt->token != 0) {
4899 xmlChar chr[2] = { 0 , 0 } ;
4900
4901 chr[0] = (xmlChar) ctxt->token;
4902 htmlCheckParagraph(ctxt);
4903 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4904 ctxt->sax->characters(ctxt->userData, chr, 1);
4905 ctxt->token = 0;
4906 ctxt->checkIndex = 0;
4907 }
4908 if ((avail == 1) && (terminate)) {
4909 cur = in->cur[0];
4910 if ((cur != '<') && (cur != '&')) {
4911 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00004912 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004913 if (ctxt->sax->ignorableWhitespace != NULL)
4914 ctxt->sax->ignorableWhitespace(
4915 ctxt->userData, &cur, 1);
4916 } else {
4917 htmlCheckParagraph(ctxt);
4918 if (ctxt->sax->characters != NULL)
4919 ctxt->sax->characters(
4920 ctxt->userData, &cur, 1);
4921 }
4922 }
4923 ctxt->token = 0;
4924 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00004925 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00004926 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004927 }
Owen Taylor3473f882001-02-23 17:55:21 +00004928 }
4929 if (avail < 2)
4930 goto done;
4931 cur = in->cur[0];
4932 next = in->cur[1];
4933 cons = ctxt->nbChars;
4934 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4935 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4936 /*
4937 * Handle SCRIPT/STYLE separately
4938 */
4939 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004940 (htmlParseLookupSequence(ctxt, '<', '/', 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004941 goto done;
4942 htmlParseScript(ctxt);
4943 if ((cur == '<') && (next == '/')) {
4944 ctxt->instate = XML_PARSER_END_TAG;
4945 ctxt->checkIndex = 0;
4946#ifdef DEBUG_PUSH
4947 xmlGenericError(xmlGenericErrorContext,
4948 "HPP: entering END_TAG\n");
4949#endif
4950 break;
4951 }
4952 } else {
4953 /*
4954 * Sometimes DOCTYPE arrives in the middle of the document
4955 */
4956 if ((cur == '<') && (next == '!') &&
4957 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4958 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4959 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4960 (UPP(8) == 'E')) {
4961 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004962 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004963 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00004964 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4965 "Misplaced DOCTYPE declaration\n",
4966 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004967 htmlParseDocTypeDecl(ctxt);
4968 } else if ((cur == '<') && (next == '!') &&
4969 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4970 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004971 (htmlParseLookupSequence(
4972 ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004973 goto done;
4974#ifdef DEBUG_PUSH
4975 xmlGenericError(xmlGenericErrorContext,
4976 "HPP: Parsing Comment\n");
4977#endif
4978 htmlParseComment(ctxt);
4979 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004980 } else if ((cur == '<') && (next == '?')) {
4981 if ((!terminate) &&
4982 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4983 goto done;
4984#ifdef DEBUG_PUSH
4985 xmlGenericError(xmlGenericErrorContext,
4986 "HPP: Parsing PI\n");
4987#endif
4988 htmlParsePI(ctxt);
4989 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +00004990 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
4991 goto done;
4992 } else if ((cur == '<') && (next == '/')) {
4993 ctxt->instate = XML_PARSER_END_TAG;
4994 ctxt->checkIndex = 0;
4995#ifdef DEBUG_PUSH
4996 xmlGenericError(xmlGenericErrorContext,
4997 "HPP: entering END_TAG\n");
4998#endif
4999 break;
5000 } else if (cur == '<') {
5001 ctxt->instate = XML_PARSER_START_TAG;
5002 ctxt->checkIndex = 0;
5003#ifdef DEBUG_PUSH
5004 xmlGenericError(xmlGenericErrorContext,
5005 "HPP: entering START_TAG\n");
5006#endif
5007 break;
5008 } else if (cur == '&') {
5009 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005010 (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005011 goto done;
5012#ifdef DEBUG_PUSH
5013 xmlGenericError(xmlGenericErrorContext,
5014 "HPP: Parsing Reference\n");
5015#endif
5016 /* TODO: check generation of subtrees if noent !!! */
5017 htmlParseReference(ctxt);
5018 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005019 /*
5020 * check that the text sequence is complete
5021 * before handing out the data to the parser
5022 * to avoid problems with erroneous end of
5023 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00005024 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00005025 if ((!terminate) &&
5026 (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
5027 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00005028 ctxt->checkIndex = 0;
5029#ifdef DEBUG_PUSH
5030 xmlGenericError(xmlGenericErrorContext,
5031 "HPP: Parsing char data\n");
5032#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005033 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005034 }
5035 }
5036 if (cons == ctxt->nbChars) {
5037 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005038 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5039 "detected an error in element content\n",
5040 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005041 }
5042 NEXT;
5043 break;
5044 }
5045
5046 break;
5047 }
5048 case XML_PARSER_END_TAG:
5049 if (avail < 2)
5050 goto done;
5051 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005052 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005053 goto done;
5054 htmlParseEndTag(ctxt);
5055 if (ctxt->nameNr == 0) {
5056 ctxt->instate = XML_PARSER_EPILOG;
5057 } else {
5058 ctxt->instate = XML_PARSER_CONTENT;
5059 }
5060 ctxt->checkIndex = 0;
5061#ifdef DEBUG_PUSH
5062 xmlGenericError(xmlGenericErrorContext,
5063 "HPP: entering CONTENT\n");
5064#endif
5065 break;
5066 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00005067 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5068 "HPP: internal error, state == CDATA\n",
5069 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005070 ctxt->instate = XML_PARSER_CONTENT;
5071 ctxt->checkIndex = 0;
5072#ifdef DEBUG_PUSH
5073 xmlGenericError(xmlGenericErrorContext,
5074 "HPP: entering CONTENT\n");
5075#endif
5076 break;
5077 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00005078 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5079 "HPP: internal error, state == DTD\n",
5080 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005081 ctxt->instate = XML_PARSER_CONTENT;
5082 ctxt->checkIndex = 0;
5083#ifdef DEBUG_PUSH
5084 xmlGenericError(xmlGenericErrorContext,
5085 "HPP: entering CONTENT\n");
5086#endif
5087 break;
5088 case XML_PARSER_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00005089 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5090 "HPP: internal error, state == COMMENT\n",
5091 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005092 ctxt->instate = XML_PARSER_CONTENT;
5093 ctxt->checkIndex = 0;
5094#ifdef DEBUG_PUSH
5095 xmlGenericError(xmlGenericErrorContext,
5096 "HPP: entering CONTENT\n");
5097#endif
5098 break;
5099 case XML_PARSER_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00005100 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5101 "HPP: internal error, state == PI\n",
5102 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005103 ctxt->instate = XML_PARSER_CONTENT;
5104 ctxt->checkIndex = 0;
5105#ifdef DEBUG_PUSH
5106 xmlGenericError(xmlGenericErrorContext,
5107 "HPP: entering CONTENT\n");
5108#endif
5109 break;
5110 case XML_PARSER_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005111 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5112 "HPP: internal error, state == ENTITY_DECL\n",
5113 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005114 ctxt->instate = XML_PARSER_CONTENT;
5115 ctxt->checkIndex = 0;
5116#ifdef DEBUG_PUSH
5117 xmlGenericError(xmlGenericErrorContext,
5118 "HPP: entering CONTENT\n");
5119#endif
5120 break;
5121 case XML_PARSER_ENTITY_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005122 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5123 "HPP: internal error, state == ENTITY_VALUE\n",
5124 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005125 ctxt->instate = XML_PARSER_CONTENT;
5126 ctxt->checkIndex = 0;
5127#ifdef DEBUG_PUSH
5128 xmlGenericError(xmlGenericErrorContext,
5129 "HPP: entering DTD\n");
5130#endif
5131 break;
5132 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005133 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5134 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
5135 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005136 ctxt->instate = XML_PARSER_START_TAG;
5137 ctxt->checkIndex = 0;
5138#ifdef DEBUG_PUSH
5139 xmlGenericError(xmlGenericErrorContext,
5140 "HPP: entering START_TAG\n");
5141#endif
5142 break;
5143 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005144 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5145 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
5146 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005147 ctxt->instate = XML_PARSER_CONTENT;
5148 ctxt->checkIndex = 0;
5149#ifdef DEBUG_PUSH
5150 xmlGenericError(xmlGenericErrorContext,
5151 "HPP: entering CONTENT\n");
5152#endif
5153 break;
5154 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005155 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5156 "HPP: internal error, state == XML_PARSER_IGNORE\n",
5157 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005158 ctxt->instate = XML_PARSER_CONTENT;
5159 ctxt->checkIndex = 0;
5160#ifdef DEBUG_PUSH
5161 xmlGenericError(xmlGenericErrorContext,
5162 "HPP: entering CONTENT\n");
5163#endif
5164 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005165 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005166 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5167 "HPP: internal error, state == XML_PARSER_LITERAL\n",
5168 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005169 ctxt->instate = XML_PARSER_CONTENT;
5170 ctxt->checkIndex = 0;
5171#ifdef DEBUG_PUSH
5172 xmlGenericError(xmlGenericErrorContext,
5173 "HPP: entering CONTENT\n");
5174#endif
5175 break;
5176
Owen Taylor3473f882001-02-23 17:55:21 +00005177 }
5178 }
5179done:
5180 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005181 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005182 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5183 /*
5184 * SAX: end of the document processing.
5185 */
5186 ctxt->instate = XML_PARSER_EOF;
5187 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5188 ctxt->sax->endDocument(ctxt->userData);
5189 }
5190 }
5191 if ((ctxt->myDoc != NULL) &&
5192 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5193 (ctxt->instate == XML_PARSER_EPILOG))) {
5194 xmlDtdPtr dtd;
5195 dtd = xmlGetIntSubset(ctxt->myDoc);
5196 if (dtd == NULL)
5197 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00005198 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00005199 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5200 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5201 }
5202#ifdef DEBUG_PUSH
5203 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5204#endif
5205 return(ret);
5206}
5207
5208/**
Owen Taylor3473f882001-02-23 17:55:21 +00005209 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00005210 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00005211 * @chunk: an char array
5212 * @size: the size in byte of the chunk
5213 * @terminate: last chunk indicator
5214 *
5215 * Parse a Chunk of memory
5216 *
5217 * Returns zero if no error, the xmlParserErrors otherwise.
5218 */
5219int
5220htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
5221 int terminate) {
Daniel Veillarda03e3652004-11-02 18:45:30 +00005222 if ((ctxt == NULL) || (ctxt->input == NULL)) {
5223 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5224 "htmlParseChunk: context error\n", NULL, NULL);
5225 return(XML_ERR_INTERNAL_ERROR);
5226 }
Owen Taylor3473f882001-02-23 17:55:21 +00005227 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5228 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
5229 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5230 int cur = ctxt->input->cur - ctxt->input->base;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005231 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00005232
Daniel Veillardd2755a82005-08-07 23:42:39 +00005233 res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5234 if (res < 0) {
5235 ctxt->errNo = XML_PARSER_EOF;
5236 ctxt->disableSAX = 1;
5237 return (XML_PARSER_EOF);
5238 }
Owen Taylor3473f882001-02-23 17:55:21 +00005239 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5240 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005241 ctxt->input->end =
5242 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005243#ifdef DEBUG_PUSH
5244 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5245#endif
5246
Daniel Veillard14f752c2003-08-09 11:44:50 +00005247#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00005248 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5249 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005250#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005251 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005252 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
5253 xmlParserInputBufferPtr in = ctxt->input->buf;
5254 if ((in->encoder != NULL) && (in->buffer != NULL) &&
5255 (in->raw != NULL)) {
5256 int nbchars;
5257
5258 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
5259 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005260 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
5261 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005262 return(XML_ERR_INVALID_ENCODING);
5263 }
5264 }
5265 }
Owen Taylor3473f882001-02-23 17:55:21 +00005266 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00005267 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00005268 if (terminate) {
5269 if ((ctxt->instate != XML_PARSER_EOF) &&
5270 (ctxt->instate != XML_PARSER_EPILOG) &&
5271 (ctxt->instate != XML_PARSER_MISC)) {
5272 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005273 ctxt->wellFormed = 0;
5274 }
5275 if (ctxt->instate != XML_PARSER_EOF) {
5276 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5277 ctxt->sax->endDocument(ctxt->userData);
5278 }
5279 ctxt->instate = XML_PARSER_EOF;
5280 }
5281 return((xmlParserErrors) ctxt->errNo);
5282}
5283
5284/************************************************************************
5285 * *
5286 * User entry points *
5287 * *
5288 ************************************************************************/
5289
5290/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005291 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005292 * @sax: a SAX handler
5293 * @user_data: The user data returned on SAX callbacks
5294 * @chunk: a pointer to an array of chars
5295 * @size: number of chars in the array
5296 * @filename: an optional file name or URI
5297 * @enc: an optional encoding
5298 *
5299 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005300 * The value of @filename is used for fetching external entities
5301 * and error/warning reports.
5302 *
5303 * Returns the new parser context or NULL
5304 */
5305htmlParserCtxtPtr
5306htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
5307 const char *chunk, int size, const char *filename,
5308 xmlCharEncoding enc) {
5309 htmlParserCtxtPtr ctxt;
5310 htmlParserInputPtr inputStream;
5311 xmlParserInputBufferPtr buf;
5312
Daniel Veillardd0463562001-10-13 09:15:48 +00005313 xmlInitParser();
5314
Owen Taylor3473f882001-02-23 17:55:21 +00005315 buf = xmlAllocParserInputBuffer(enc);
5316 if (buf == NULL) return(NULL);
5317
Daniel Veillardf403d292003-10-05 13:51:35 +00005318 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005319 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005320 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005321 return(NULL);
5322 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00005323 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
5324 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00005325 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00005326 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00005327 xmlFree(ctxt->sax);
5328 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
5329 if (ctxt->sax == NULL) {
5330 xmlFree(buf);
5331 xmlFree(ctxt);
5332 return(NULL);
5333 }
5334 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
5335 if (user_data != NULL)
5336 ctxt->userData = user_data;
5337 }
5338 if (filename == NULL) {
5339 ctxt->directory = NULL;
5340 } else {
5341 ctxt->directory = xmlParserGetDirectory(filename);
5342 }
5343
5344 inputStream = htmlNewInputStream(ctxt);
5345 if (inputStream == NULL) {
5346 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00005347 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005348 return(NULL);
5349 }
5350
5351 if (filename == NULL)
5352 inputStream->filename = NULL;
5353 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00005354 inputStream->filename = (char *)
5355 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00005356 inputStream->buf = buf;
5357 inputStream->base = inputStream->buf->buffer->content;
5358 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard5f704af2003-03-05 10:01:43 +00005359 inputStream->end =
5360 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005361
5362 inputPush(ctxt, inputStream);
5363
5364 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5365 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00005366 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5367 int cur = ctxt->input->cur - ctxt->input->base;
5368
Owen Taylor3473f882001-02-23 17:55:21 +00005369 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00005370
5371 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5372 ctxt->input->cur = ctxt->input->base + cur;
5373 ctxt->input->end =
5374 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005375#ifdef DEBUG_PUSH
5376 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5377#endif
5378 }
5379
5380 return(ctxt);
5381}
William M. Brack21e4ef22005-01-02 09:53:13 +00005382#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005383
5384/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005385 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005386 * @cur: a pointer to an array of xmlChar
5387 * @encoding: a free form C string describing the HTML document encoding, or NULL
5388 * @sax: the SAX handler block
5389 * @userData: if using SAX, this pointer will be provided on callbacks.
5390 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005391 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
5392 * to handle parse events. If sax is NULL, fallback to the default DOM
5393 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00005394 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005395 * Returns the resulting document tree unless SAX is NULL or the document is
5396 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005397 */
5398
5399htmlDocPtr
5400htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
5401 htmlDocPtr ret;
5402 htmlParserCtxtPtr ctxt;
5403
Daniel Veillardd0463562001-10-13 09:15:48 +00005404 xmlInitParser();
5405
Owen Taylor3473f882001-02-23 17:55:21 +00005406 if (cur == NULL) return(NULL);
5407
5408
5409 ctxt = htmlCreateDocParserCtxt(cur, encoding);
5410 if (ctxt == NULL) return(NULL);
5411 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00005412 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00005413 ctxt->sax = sax;
5414 ctxt->userData = userData;
5415 }
5416
5417 htmlParseDocument(ctxt);
5418 ret = ctxt->myDoc;
5419 if (sax != NULL) {
5420 ctxt->sax = NULL;
5421 ctxt->userData = NULL;
5422 }
5423 htmlFreeParserCtxt(ctxt);
5424
5425 return(ret);
5426}
5427
5428/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005429 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005430 * @cur: a pointer to an array of xmlChar
5431 * @encoding: a free form C string describing the HTML document encoding, or NULL
5432 *
5433 * parse an HTML in-memory document and build a tree.
5434 *
5435 * Returns the resulting document tree
5436 */
5437
5438htmlDocPtr
5439htmlParseDoc(xmlChar *cur, const char *encoding) {
5440 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
5441}
5442
5443
5444/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005445 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005446 * @filename: the filename
5447 * @encoding: a free form C string describing the HTML document encoding, or NULL
5448 *
5449 * Create a parser context for a file content.
5450 * Automatic support for ZLIB/Compress compressed document is provided
5451 * by default if found at compile-time.
5452 *
5453 * Returns the new parser context or NULL
5454 */
5455htmlParserCtxtPtr
5456htmlCreateFileParserCtxt(const char *filename, const char *encoding)
5457{
5458 htmlParserCtxtPtr ctxt;
5459 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005460 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00005461 /* htmlCharEncoding enc; */
5462 xmlChar *content, *content_line = (xmlChar *) "charset=";
5463
Daniel Veillarda03e3652004-11-02 18:45:30 +00005464 if (filename == NULL)
5465 return(NULL);
5466
Daniel Veillardf403d292003-10-05 13:51:35 +00005467 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005468 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005469 return(NULL);
5470 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005471 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
5472 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00005473#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005474 if (xmlDefaultSAXHandler.error != NULL) {
5475 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
5476 }
Daniel Veillard87247e82004-01-13 20:42:02 +00005477#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00005478 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005479 return(NULL);
5480 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005481
5482 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
5483 xmlFree(canonicFilename);
5484 if (inputStream == NULL) {
5485 xmlFreeParserCtxt(ctxt);
5486 return(NULL);
5487 }
Owen Taylor3473f882001-02-23 17:55:21 +00005488
5489 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005490
Owen Taylor3473f882001-02-23 17:55:21 +00005491 /* set encoding */
5492 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005493 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Owen Taylor3473f882001-02-23 17:55:21 +00005494 if (content) {
5495 strcpy ((char *)content, (char *)content_line);
5496 strcat ((char *)content, (char *)encoding);
5497 htmlCheckEncoding (ctxt, content);
5498 xmlFree (content);
5499 }
5500 }
5501
5502 return(ctxt);
5503}
5504
5505/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005506 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005507 * @filename: the filename
5508 * @encoding: a free form C string describing the HTML document encoding, or NULL
5509 * @sax: the SAX handler block
5510 * @userData: if using SAX, this pointer will be provided on callbacks.
5511 *
5512 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5513 * compressed document is provided by default if found at compile-time.
5514 * It use the given SAX function block to handle the parsing callback.
5515 * If sax is NULL, fallback to the default DOM tree building routines.
5516 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005517 * Returns the resulting document tree unless SAX is NULL or the document is
5518 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005519 */
5520
5521htmlDocPtr
5522htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
5523 void *userData) {
5524 htmlDocPtr ret;
5525 htmlParserCtxtPtr ctxt;
5526 htmlSAXHandlerPtr oldsax = NULL;
5527
Daniel Veillardd0463562001-10-13 09:15:48 +00005528 xmlInitParser();
5529
Owen Taylor3473f882001-02-23 17:55:21 +00005530 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5531 if (ctxt == NULL) return(NULL);
5532 if (sax != NULL) {
5533 oldsax = ctxt->sax;
5534 ctxt->sax = sax;
5535 ctxt->userData = userData;
5536 }
5537
5538 htmlParseDocument(ctxt);
5539
5540 ret = ctxt->myDoc;
5541 if (sax != NULL) {
5542 ctxt->sax = oldsax;
5543 ctxt->userData = NULL;
5544 }
5545 htmlFreeParserCtxt(ctxt);
5546
5547 return(ret);
5548}
5549
5550/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005551 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005552 * @filename: the filename
5553 * @encoding: a free form C string describing the HTML document encoding, or NULL
5554 *
5555 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5556 * compressed document is provided by default if found at compile-time.
5557 *
5558 * Returns the resulting document tree
5559 */
5560
5561htmlDocPtr
5562htmlParseFile(const char *filename, const char *encoding) {
5563 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
5564}
5565
5566/**
5567 * htmlHandleOmittedElem:
5568 * @val: int 0 or 1
5569 *
5570 * Set and return the previous value for handling HTML omitted tags.
5571 *
5572 * Returns the last value for 0 for no handling, 1 for auto insertion.
5573 */
5574
5575int
5576htmlHandleOmittedElem(int val) {
5577 int old = htmlOmittedDefaultValue;
5578
5579 htmlOmittedDefaultValue = val;
5580 return(old);
5581}
5582
Daniel Veillard930dfb62003-02-05 10:17:38 +00005583/**
5584 * htmlElementAllowedHere:
5585 * @parent: HTML parent element
5586 * @elt: HTML element
5587 *
5588 * Checks whether an HTML element may be a direct child of a parent element.
5589 * Note - doesn't check for deprecated elements
5590 *
5591 * Returns 1 if allowed; 0 otherwise.
5592 */
5593int
5594htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
5595 const char** p ;
5596
5597 if ( ! elt || ! parent || ! parent->subelts )
5598 return 0 ;
5599
5600 for ( p = parent->subelts; *p; ++p )
5601 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
5602 return 1 ;
5603
5604 return 0 ;
5605}
5606/**
5607 * htmlElementStatusHere:
5608 * @parent: HTML parent element
5609 * @elt: HTML element
5610 *
5611 * Checks whether an HTML element may be a direct child of a parent element.
5612 * and if so whether it is valid or deprecated.
5613 *
5614 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5615 */
5616htmlStatus
5617htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
5618 if ( ! parent || ! elt )
5619 return HTML_INVALID ;
5620 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
5621 return HTML_INVALID ;
5622
5623 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
5624}
5625/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005626 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00005627 * @elt: HTML element
5628 * @attr: HTML attribute
5629 * @legacy: whether to allow deprecated attributes
5630 *
5631 * Checks whether an attribute is valid for an element
5632 * Has full knowledge of Required and Deprecated attributes
5633 *
5634 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5635 */
5636htmlStatus
5637htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
5638 const char** p ;
5639
5640 if ( !elt || ! attr )
5641 return HTML_INVALID ;
5642
5643 if ( elt->attrs_req )
5644 for ( p = elt->attrs_req; *p; ++p)
5645 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5646 return HTML_REQUIRED ;
5647
5648 if ( elt->attrs_opt )
5649 for ( p = elt->attrs_opt; *p; ++p)
5650 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5651 return HTML_VALID ;
5652
5653 if ( legacy && elt->attrs_depr )
5654 for ( p = elt->attrs_depr; *p; ++p)
5655 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5656 return HTML_DEPRECATED ;
5657
5658 return HTML_INVALID ;
5659}
5660/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005661 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005662 * @node: an htmlNodePtr in a tree
5663 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00005664 * for Element nodes)
5665 *
5666 * Checks whether the tree node is valid. Experimental (the author
5667 * only uses the HTML enhancements in a SAX parser)
5668 *
5669 * Return: for Element nodes, a return from htmlElementAllowedHere (if
5670 * legacy allowed) or htmlElementStatusHere (otherwise).
5671 * for Attribute nodes, a return from htmlAttrAllowed
5672 * for other nodes, HTML_NA (no checks performed)
5673 */
5674htmlStatus
5675htmlNodeStatus(const htmlNodePtr node, int legacy) {
5676 if ( ! node )
5677 return HTML_INVALID ;
5678
5679 switch ( node->type ) {
5680 case XML_ELEMENT_NODE:
5681 return legacy
5682 ? ( htmlElementAllowedHere (
5683 htmlTagLookup(node->parent->name) , node->name
5684 ) ? HTML_VALID : HTML_INVALID )
5685 : htmlElementStatusHere(
5686 htmlTagLookup(node->parent->name) ,
5687 htmlTagLookup(node->name) )
5688 ;
5689 case XML_ATTRIBUTE_NODE:
5690 return htmlAttrAllowed(
5691 htmlTagLookup(node->parent->name) , node->name, legacy) ;
5692 default: return HTML_NA ;
5693 }
5694}
Daniel Veillard9475a352003-09-26 12:47:50 +00005695/************************************************************************
5696 * *
5697 * New set (2.6.0) of simpler and more flexible APIs *
5698 * *
5699 ************************************************************************/
5700/**
5701 * DICT_FREE:
5702 * @str: a string
5703 *
5704 * Free a string if it is not owned by the "dict" dictionnary in the
5705 * current scope
5706 */
5707#define DICT_FREE(str) \
5708 if ((str) && ((!dict) || \
5709 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
5710 xmlFree((char *)(str));
5711
5712/**
5713 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00005714 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00005715 *
5716 * Reset a parser context
5717 */
5718void
5719htmlCtxtReset(htmlParserCtxtPtr ctxt)
5720{
5721 xmlParserInputPtr input;
Daniel Veillarda03e3652004-11-02 18:45:30 +00005722 xmlDictPtr dict;
5723
5724 if (ctxt == NULL)
5725 return;
5726
5727 dict = ctxt->dict;
Daniel Veillard9475a352003-09-26 12:47:50 +00005728
5729 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
5730 xmlFreeInputStream(input);
5731 }
5732 ctxt->inputNr = 0;
5733 ctxt->input = NULL;
5734
5735 ctxt->spaceNr = 0;
Daniel Veillarda521d282004-11-09 14:59:59 +00005736 if (ctxt->spaceTab != NULL) {
5737 ctxt->spaceTab[0] = -1;
5738 ctxt->space = &ctxt->spaceTab[0];
5739 } else {
5740 ctxt->space = NULL;
5741 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005742
5743
5744 ctxt->nodeNr = 0;
5745 ctxt->node = NULL;
5746
5747 ctxt->nameNr = 0;
5748 ctxt->name = NULL;
5749
5750 DICT_FREE(ctxt->version);
5751 ctxt->version = NULL;
5752 DICT_FREE(ctxt->encoding);
5753 ctxt->encoding = NULL;
5754 DICT_FREE(ctxt->directory);
5755 ctxt->directory = NULL;
5756 DICT_FREE(ctxt->extSubURI);
5757 ctxt->extSubURI = NULL;
5758 DICT_FREE(ctxt->extSubSystem);
5759 ctxt->extSubSystem = NULL;
5760 if (ctxt->myDoc != NULL)
5761 xmlFreeDoc(ctxt->myDoc);
5762 ctxt->myDoc = NULL;
5763
5764 ctxt->standalone = -1;
5765 ctxt->hasExternalSubset = 0;
5766 ctxt->hasPErefs = 0;
5767 ctxt->html = 1;
5768 ctxt->external = 0;
5769 ctxt->instate = XML_PARSER_START;
5770 ctxt->token = 0;
5771
5772 ctxt->wellFormed = 1;
5773 ctxt->nsWellFormed = 1;
5774 ctxt->valid = 1;
5775 ctxt->vctxt.userData = ctxt;
5776 ctxt->vctxt.error = xmlParserValidityError;
5777 ctxt->vctxt.warning = xmlParserValidityWarning;
5778 ctxt->record_info = 0;
5779 ctxt->nbChars = 0;
5780 ctxt->checkIndex = 0;
5781 ctxt->inSubset = 0;
5782 ctxt->errNo = XML_ERR_OK;
5783 ctxt->depth = 0;
5784 ctxt->charset = XML_CHAR_ENCODING_UTF8;
5785 ctxt->catalogs = NULL;
5786 xmlInitNodeInfoSeq(&ctxt->node_seq);
5787
5788 if (ctxt->attsDefault != NULL) {
5789 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
5790 ctxt->attsDefault = NULL;
5791 }
5792 if (ctxt->attsSpecial != NULL) {
5793 xmlHashFree(ctxt->attsSpecial, NULL);
5794 ctxt->attsSpecial = NULL;
5795 }
5796}
5797
5798/**
5799 * htmlCtxtUseOptions:
5800 * @ctxt: an HTML parser context
5801 * @options: a combination of htmlParserOption(s)
5802 *
5803 * Applies the options to the parser context
5804 *
5805 * Returns 0 in case of success, the set of unknown or unimplemented options
5806 * in case of error.
5807 */
5808int
5809htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
5810{
Daniel Veillarda03e3652004-11-02 18:45:30 +00005811 if (ctxt == NULL)
5812 return(-1);
5813
Daniel Veillard9475a352003-09-26 12:47:50 +00005814 if (options & HTML_PARSE_NOWARNING) {
5815 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005816 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005817 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005818 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00005819 }
5820 if (options & HTML_PARSE_NOERROR) {
5821 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005822 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005823 ctxt->sax->fatalError = NULL;
5824 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005825 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00005826 }
5827 if (options & HTML_PARSE_PEDANTIC) {
5828 ctxt->pedantic = 1;
5829 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005830 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00005831 } else
5832 ctxt->pedantic = 0;
5833 if (options & XML_PARSE_NOBLANKS) {
5834 ctxt->keepBlanks = 0;
5835 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
5836 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005837 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00005838 } else
5839 ctxt->keepBlanks = 1;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00005840 if (options & HTML_PARSE_RECOVER) {
5841 ctxt->recovery = 1;
5842 } else
5843 ctxt->recovery = 0;
Daniel Veillard8874b942005-08-25 13:19:21 +00005844 if (options & HTML_PARSE_COMPACT) {
5845 ctxt->options |= HTML_PARSE_COMPACT;
5846 options -= HTML_PARSE_COMPACT;
5847 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005848 ctxt->dictNames = 0;
5849 return (options);
5850}
5851
5852/**
5853 * htmlDoRead:
5854 * @ctxt: an HTML parser context
5855 * @URL: the base URL to use for the document
5856 * @encoding: the document encoding, or NULL
5857 * @options: a combination of htmlParserOption(s)
5858 * @reuse: keep the context for reuse
5859 *
5860 * Common front-end for the htmlRead functions
5861 *
5862 * Returns the resulting document tree or NULL
5863 */
5864static htmlDocPtr
5865htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
5866 int options, int reuse)
5867{
5868 htmlDocPtr ret;
5869
5870 htmlCtxtUseOptions(ctxt, options);
5871 ctxt->html = 1;
5872 if (encoding != NULL) {
5873 xmlCharEncodingHandlerPtr hdlr;
5874
5875 hdlr = xmlFindCharEncodingHandler(encoding);
5876 if (hdlr != NULL)
5877 xmlSwitchToEncoding(ctxt, hdlr);
5878 }
5879 if ((URL != NULL) && (ctxt->input != NULL) &&
5880 (ctxt->input->filename == NULL))
5881 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
5882 htmlParseDocument(ctxt);
5883 ret = ctxt->myDoc;
5884 ctxt->myDoc = NULL;
5885 if (!reuse) {
5886 if ((ctxt->dictNames) &&
5887 (ret != NULL) &&
5888 (ret->dict == ctxt->dict))
5889 ctxt->dict = NULL;
5890 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00005891 }
5892 return (ret);
5893}
5894
5895/**
5896 * htmlReadDoc:
5897 * @cur: a pointer to a zero terminated string
5898 * @URL: the base URL to use for the document
5899 * @encoding: the document encoding, or NULL
5900 * @options: a combination of htmlParserOption(s)
5901 *
5902 * parse an XML in-memory document and build a tree.
5903 *
5904 * Returns the resulting document tree
5905 */
5906htmlDocPtr
5907htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
5908{
5909 htmlParserCtxtPtr ctxt;
5910
5911 if (cur == NULL)
5912 return (NULL);
5913
5914 ctxt = xmlCreateDocParserCtxt(cur);
5915 if (ctxt == NULL)
5916 return (NULL);
5917 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5918}
5919
5920/**
5921 * htmlReadFile:
5922 * @filename: a file or URL
5923 * @encoding: the document encoding, or NULL
5924 * @options: a combination of htmlParserOption(s)
5925 *
5926 * parse an XML file from the filesystem or the network.
5927 *
5928 * Returns the resulting document tree
5929 */
5930htmlDocPtr
5931htmlReadFile(const char *filename, const char *encoding, int options)
5932{
5933 htmlParserCtxtPtr ctxt;
5934
5935 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5936 if (ctxt == NULL)
5937 return (NULL);
5938 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
5939}
5940
5941/**
5942 * htmlReadMemory:
5943 * @buffer: a pointer to a char array
5944 * @size: the size of the array
5945 * @URL: the base URL to use for the document
5946 * @encoding: the document encoding, or NULL
5947 * @options: a combination of htmlParserOption(s)
5948 *
5949 * parse an XML in-memory document and build a tree.
5950 *
5951 * Returns the resulting document tree
5952 */
5953htmlDocPtr
5954htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
5955{
5956 htmlParserCtxtPtr ctxt;
5957
5958 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
5959 if (ctxt == NULL)
5960 return (NULL);
William M. Brackd43cdcd2004-08-03 15:13:29 +00005961 if (ctxt->sax != NULL)
5962 memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Daniel Veillard9475a352003-09-26 12:47:50 +00005963 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5964}
5965
5966/**
5967 * htmlReadFd:
5968 * @fd: an open file descriptor
5969 * @URL: the base URL to use for the document
5970 * @encoding: the document encoding, or NULL
5971 * @options: a combination of htmlParserOption(s)
5972 *
5973 * parse an XML from a file descriptor and build a tree.
5974 *
5975 * Returns the resulting document tree
5976 */
5977htmlDocPtr
5978htmlReadFd(int fd, const char *URL, const char *encoding, int options)
5979{
5980 htmlParserCtxtPtr ctxt;
5981 xmlParserInputBufferPtr input;
5982 xmlParserInputPtr stream;
5983
5984 if (fd < 0)
5985 return (NULL);
5986
5987 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5988 if (input == NULL)
5989 return (NULL);
5990 ctxt = xmlNewParserCtxt();
5991 if (ctxt == NULL) {
5992 xmlFreeParserInputBuffer(input);
5993 return (NULL);
5994 }
5995 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5996 if (stream == NULL) {
5997 xmlFreeParserInputBuffer(input);
5998 xmlFreeParserCtxt(ctxt);
5999 return (NULL);
6000 }
6001 inputPush(ctxt, stream);
6002 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6003}
6004
6005/**
6006 * htmlReadIO:
6007 * @ioread: an I/O read function
6008 * @ioclose: an I/O close function
6009 * @ioctx: an I/O handler
6010 * @URL: the base URL to use for the document
6011 * @encoding: the document encoding, or NULL
6012 * @options: a combination of htmlParserOption(s)
6013 *
6014 * parse an HTML document from I/O functions and source and build a tree.
6015 *
6016 * Returns the resulting document tree
6017 */
6018htmlDocPtr
6019htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
6020 void *ioctx, const char *URL, const char *encoding, int options)
6021{
6022 htmlParserCtxtPtr ctxt;
6023 xmlParserInputBufferPtr input;
6024 xmlParserInputPtr stream;
6025
6026 if (ioread == NULL)
6027 return (NULL);
6028
6029 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6030 XML_CHAR_ENCODING_NONE);
6031 if (input == NULL)
6032 return (NULL);
6033 ctxt = xmlNewParserCtxt();
6034 if (ctxt == NULL) {
6035 xmlFreeParserInputBuffer(input);
6036 return (NULL);
6037 }
6038 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6039 if (stream == NULL) {
6040 xmlFreeParserInputBuffer(input);
6041 xmlFreeParserCtxt(ctxt);
6042 return (NULL);
6043 }
6044 inputPush(ctxt, stream);
6045 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6046}
6047
6048/**
6049 * htmlCtxtReadDoc:
6050 * @ctxt: an HTML parser context
6051 * @cur: a pointer to a zero terminated string
6052 * @URL: the base URL to use for the document
6053 * @encoding: the document encoding, or NULL
6054 * @options: a combination of htmlParserOption(s)
6055 *
6056 * parse an XML in-memory document and build a tree.
6057 * This reuses the existing @ctxt parser context
6058 *
6059 * Returns the resulting document tree
6060 */
6061htmlDocPtr
6062htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
6063 const char *URL, const char *encoding, int options)
6064{
6065 xmlParserInputPtr stream;
6066
6067 if (cur == NULL)
6068 return (NULL);
6069 if (ctxt == NULL)
6070 return (NULL);
6071
6072 htmlCtxtReset(ctxt);
6073
6074 stream = xmlNewStringInputStream(ctxt, cur);
6075 if (stream == NULL) {
6076 return (NULL);
6077 }
6078 inputPush(ctxt, stream);
6079 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6080}
6081
6082/**
6083 * htmlCtxtReadFile:
6084 * @ctxt: an HTML parser context
6085 * @filename: a file or URL
6086 * @encoding: the document encoding, or NULL
6087 * @options: a combination of htmlParserOption(s)
6088 *
6089 * parse an XML file from the filesystem or the network.
6090 * This reuses the existing @ctxt parser context
6091 *
6092 * Returns the resulting document tree
6093 */
6094htmlDocPtr
6095htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
6096 const char *encoding, int options)
6097{
6098 xmlParserInputPtr stream;
6099
6100 if (filename == NULL)
6101 return (NULL);
6102 if (ctxt == NULL)
6103 return (NULL);
6104
6105 htmlCtxtReset(ctxt);
6106
Daniel Veillard29614c72004-11-26 10:47:26 +00006107 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006108 if (stream == NULL) {
6109 return (NULL);
6110 }
6111 inputPush(ctxt, stream);
6112 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
6113}
6114
6115/**
6116 * htmlCtxtReadMemory:
6117 * @ctxt: an HTML parser context
6118 * @buffer: a pointer to a char array
6119 * @size: the size of the array
6120 * @URL: the base URL to use for the document
6121 * @encoding: the document encoding, or NULL
6122 * @options: a combination of htmlParserOption(s)
6123 *
6124 * parse an XML in-memory document and build a tree.
6125 * This reuses the existing @ctxt parser context
6126 *
6127 * Returns the resulting document tree
6128 */
6129htmlDocPtr
6130htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
6131 const char *URL, const char *encoding, int options)
6132{
6133 xmlParserInputBufferPtr input;
6134 xmlParserInputPtr stream;
6135
6136 if (ctxt == NULL)
6137 return (NULL);
6138 if (buffer == NULL)
6139 return (NULL);
6140
6141 htmlCtxtReset(ctxt);
6142
6143 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
6144 if (input == NULL) {
6145 return(NULL);
6146 }
6147
6148 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6149 if (stream == NULL) {
6150 xmlFreeParserInputBuffer(input);
6151 return(NULL);
6152 }
6153
6154 inputPush(ctxt, stream);
6155 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6156}
6157
6158/**
6159 * htmlCtxtReadFd:
6160 * @ctxt: an HTML parser context
6161 * @fd: an open file descriptor
6162 * @URL: the base URL to use for the document
6163 * @encoding: the document encoding, or NULL
6164 * @options: a combination of htmlParserOption(s)
6165 *
6166 * parse an XML from a file descriptor and build a tree.
6167 * This reuses the existing @ctxt parser context
6168 *
6169 * Returns the resulting document tree
6170 */
6171htmlDocPtr
6172htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
6173 const char *URL, const char *encoding, int options)
6174{
6175 xmlParserInputBufferPtr input;
6176 xmlParserInputPtr stream;
6177
6178 if (fd < 0)
6179 return (NULL);
6180 if (ctxt == NULL)
6181 return (NULL);
6182
6183 htmlCtxtReset(ctxt);
6184
6185
6186 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6187 if (input == NULL)
6188 return (NULL);
6189 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6190 if (stream == NULL) {
6191 xmlFreeParserInputBuffer(input);
6192 return (NULL);
6193 }
6194 inputPush(ctxt, stream);
6195 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6196}
6197
6198/**
6199 * htmlCtxtReadIO:
6200 * @ctxt: an HTML parser context
6201 * @ioread: an I/O read function
6202 * @ioclose: an I/O close function
6203 * @ioctx: an I/O handler
6204 * @URL: the base URL to use for the document
6205 * @encoding: the document encoding, or NULL
6206 * @options: a combination of htmlParserOption(s)
6207 *
6208 * parse an HTML document from I/O functions and source and build a tree.
6209 * This reuses the existing @ctxt parser context
6210 *
6211 * Returns the resulting document tree
6212 */
6213htmlDocPtr
6214htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
6215 xmlInputCloseCallback ioclose, void *ioctx,
6216 const char *URL,
6217 const char *encoding, int options)
6218{
6219 xmlParserInputBufferPtr input;
6220 xmlParserInputPtr stream;
6221
6222 if (ioread == NULL)
6223 return (NULL);
6224 if (ctxt == NULL)
6225 return (NULL);
6226
6227 htmlCtxtReset(ctxt);
6228
6229 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6230 XML_CHAR_ENCODING_NONE);
6231 if (input == NULL)
6232 return (NULL);
6233 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6234 if (stream == NULL) {
6235 xmlFreeParserInputBuffer(input);
6236 return (NULL);
6237 }
6238 inputPush(ctxt, stream);
6239 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6240}
6241
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006242#define bottom_HTMLparser
6243#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00006244#endif /* LIBXML_HTML_ENABLED */