blob: 12afdd85c057ddf0ef3477d2c77a0ff39efc6e95 [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
3756 * @name: the node name
3757 *
3758 * Parse a content: comment, sub-element, reference or text.
3759 *
3760 */
3761
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003762static void
Owen Taylor3473f882001-02-23 17:55:21 +00003763htmlParseContent(htmlParserCtxtPtr ctxt) {
3764 xmlChar *currentNode;
3765 int depth;
3766
3767 currentNode = xmlStrdup(ctxt->name);
3768 depth = ctxt->nameNr;
3769 while (1) {
3770 long cons = ctxt->nbChars;
3771
3772 GROW;
3773 /*
3774 * Our tag or one of it's parent or children is ending.
3775 */
3776 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003777 if (htmlParseEndTag(ctxt) &&
3778 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3779 if (currentNode != NULL)
3780 xmlFree(currentNode);
3781 return;
3782 }
3783 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003784 }
3785
3786 /*
3787 * Has this node been popped out during parsing of
3788 * the next element
3789 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003790 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3791 (!xmlStrEqual(currentNode, ctxt->name)))
3792 {
Owen Taylor3473f882001-02-23 17:55:21 +00003793 if (currentNode != NULL) xmlFree(currentNode);
3794 return;
3795 }
3796
Daniel Veillardf9533d12001-03-03 10:04:57 +00003797 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3798 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003799 /*
3800 * Handle SCRIPT/STYLE separately
3801 */
3802 htmlParseScript(ctxt);
3803 } else {
3804 /*
3805 * Sometimes DOCTYPE arrives in the middle of the document
3806 */
3807 if ((CUR == '<') && (NXT(1) == '!') &&
3808 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3809 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3810 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3811 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003812 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3813 "Misplaced DOCTYPE declaration\n",
3814 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003815 htmlParseDocTypeDecl(ctxt);
3816 }
3817
3818 /*
3819 * First case : a comment
3820 */
3821 if ((CUR == '<') && (NXT(1) == '!') &&
3822 (NXT(2) == '-') && (NXT(3) == '-')) {
3823 htmlParseComment(ctxt);
3824 }
3825
3826 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003827 * Second case : a Processing Instruction.
3828 */
3829 else if ((CUR == '<') && (NXT(1) == '?')) {
3830 htmlParsePI(ctxt);
3831 }
3832
3833 /*
3834 * Third case : a sub-element.
Owen Taylor3473f882001-02-23 17:55:21 +00003835 */
3836 else if (CUR == '<') {
3837 htmlParseElement(ctxt);
3838 }
3839
3840 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003841 * Fourth case : a reference. If if has not been resolved,
Owen Taylor3473f882001-02-23 17:55:21 +00003842 * parsing returns it's Name, create the node
3843 */
3844 else if (CUR == '&') {
3845 htmlParseReference(ctxt);
3846 }
3847
3848 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003849 * Fifth case : end of the resource
Owen Taylor3473f882001-02-23 17:55:21 +00003850 */
3851 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003852 htmlAutoCloseOnEnd(ctxt);
3853 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003854 }
3855
3856 /*
3857 * Last case, text. Note that References are handled directly.
3858 */
3859 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003860 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003861 }
3862
3863 if (cons == ctxt->nbChars) {
3864 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003865 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3866 "detected an error in element content\n",
3867 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003868 }
3869 break;
3870 }
3871 }
3872 GROW;
3873 }
3874 if (currentNode != NULL) xmlFree(currentNode);
3875}
3876
3877/**
3878 * htmlParseElement:
3879 * @ctxt: an HTML parser context
3880 *
3881 * parse an HTML element, this is highly recursive
3882 *
3883 * [39] element ::= EmptyElemTag | STag content ETag
3884 *
3885 * [41] Attribute ::= Name Eq AttValue
3886 */
3887
3888void
3889htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003890 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003891 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00003892 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00003893 htmlParserNodeInfo node_info;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003894 int failed;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003895 int depth;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00003896 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00003897
Daniel Veillarda03e3652004-11-02 18:45:30 +00003898 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3899 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard597f1c12005-07-03 23:00:18 +00003900 "htmlParseElement: context error\n", NULL, NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003901 return;
3902 }
Owen Taylor3473f882001-02-23 17:55:21 +00003903 /* Capture start position */
3904 if (ctxt->record_info) {
3905 node_info.begin_pos = ctxt->input->consumed +
3906 (CUR_PTR - ctxt->input->base);
3907 node_info.begin_line = ctxt->input->line;
3908 }
3909
Daniel Veillard597f1c12005-07-03 23:00:18 +00003910 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003911 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003912 if (failed || (name == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003913 if (CUR == '>')
3914 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003915 return;
3916 }
Owen Taylor3473f882001-02-23 17:55:21 +00003917
3918 /*
3919 * Lookup the info for that element.
3920 */
3921 info = htmlTagLookup(name);
3922 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003923 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
3924 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003925 }
3926
3927 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003928 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00003929 */
3930 if ((CUR == '/') && (NXT(1) == '>')) {
3931 SKIP(2);
3932 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3933 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003934 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003935 return;
3936 }
3937
3938 if (CUR == '>') {
3939 NEXT;
3940 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003941 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3942 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003943
3944 /*
3945 * end of parsing of this node.
3946 */
3947 if (xmlStrEqual(name, ctxt->name)) {
3948 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00003949 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003950 }
3951
3952 /*
3953 * Capture end position and add node
3954 */
3955 if ( currentNode != NULL && ctxt->record_info ) {
3956 node_info.end_pos = ctxt->input->consumed +
3957 (CUR_PTR - ctxt->input->base);
3958 node_info.end_line = ctxt->input->line;
3959 node_info.node = ctxt->node;
3960 xmlParserAddNodeInfo(ctxt, &node_info);
3961 }
3962 return;
3963 }
3964
3965 /*
3966 * Check for an Empty Element from DTD definition
3967 */
3968 if ((info != NULL) && (info->empty)) {
3969 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3970 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003971 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003972 return;
3973 }
3974
3975 /*
3976 * Parse the content of the element:
3977 */
3978 currentNode = xmlStrdup(ctxt->name);
3979 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00003980 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00003981 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00003982 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00003983 if (oldptr==ctxt->input->cur) break;
Owen Taylor3473f882001-02-23 17:55:21 +00003984 if (ctxt->nameNr < depth) break;
3985 }
3986
Owen Taylor3473f882001-02-23 17:55:21 +00003987 /*
3988 * Capture end position and add node
3989 */
3990 if ( currentNode != NULL && ctxt->record_info ) {
3991 node_info.end_pos = ctxt->input->consumed +
3992 (CUR_PTR - ctxt->input->base);
3993 node_info.end_line = ctxt->input->line;
3994 node_info.node = ctxt->node;
3995 xmlParserAddNodeInfo(ctxt, &node_info);
3996 }
William M. Brack76e95df2003-10-18 16:20:14 +00003997 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003998 htmlAutoCloseOnEnd(ctxt);
3999 }
4000
Owen Taylor3473f882001-02-23 17:55:21 +00004001 if (currentNode != NULL)
4002 xmlFree(currentNode);
4003}
4004
4005/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004006 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00004007 * @ctxt: an HTML parser context
4008 *
4009 * parse an HTML document (and build a tree if using the standard SAX
4010 * interface).
4011 *
4012 * Returns 0, -1 in case of error. the parser context is augmented
4013 * as a result of the parsing.
4014 */
4015
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00004016int
Owen Taylor3473f882001-02-23 17:55:21 +00004017htmlParseDocument(htmlParserCtxtPtr ctxt) {
4018 xmlDtdPtr dtd;
4019
Daniel Veillardd0463562001-10-13 09:15:48 +00004020 xmlInitParser();
4021
Owen Taylor3473f882001-02-23 17:55:21 +00004022 htmlDefaultSAXHandlerInit();
Owen Taylor3473f882001-02-23 17:55:21 +00004023
Daniel Veillarda03e3652004-11-02 18:45:30 +00004024 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4025 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4026 "htmlParseDocument: context error\n", NULL, NULL);
4027 return(XML_ERR_INTERNAL_ERROR);
4028 }
4029 ctxt->html = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004030 GROW;
4031 /*
4032 * SAX: beginning of the document processing.
4033 */
4034 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4035 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4036
4037 /*
4038 * Wipe out everything which is before the first '<'
4039 */
4040 SKIP_BLANKS;
4041 if (CUR == 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004042 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
4043 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004044 }
4045
4046 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4047 ctxt->sax->startDocument(ctxt->userData);
4048
4049
4050 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004051 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004052 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004053 while (((CUR == '<') && (NXT(1) == '!') &&
4054 (NXT(2) == '-') && (NXT(3) == '-')) ||
4055 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004056 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004057 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004058 SKIP_BLANKS;
4059 }
4060
4061
4062 /*
4063 * Then possibly doc type declaration(s) and more Misc
4064 * (doctypedecl Misc*)?
4065 */
4066 if ((CUR == '<') && (NXT(1) == '!') &&
4067 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4068 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4069 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4070 (UPP(8) == 'E')) {
4071 htmlParseDocTypeDecl(ctxt);
4072 }
4073 SKIP_BLANKS;
4074
4075 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004076 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004077 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004078 while (((CUR == '<') && (NXT(1) == '!') &&
4079 (NXT(2) == '-') && (NXT(3) == '-')) ||
4080 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004081 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004082 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004083 SKIP_BLANKS;
4084 }
4085
4086 /*
4087 * Time to start parsing the tree itself
4088 */
4089 htmlParseContent(ctxt);
4090
4091 /*
4092 * autoclose
4093 */
4094 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004095 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004096
4097
4098 /*
4099 * SAX: end of the document processing.
4100 */
4101 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4102 ctxt->sax->endDocument(ctxt->userData);
4103
4104 if (ctxt->myDoc != NULL) {
4105 dtd = xmlGetIntSubset(ctxt->myDoc);
4106 if (dtd == NULL)
4107 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00004108 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004109 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4110 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4111 }
4112 if (! ctxt->wellFormed) return(-1);
4113 return(0);
4114}
4115
4116
4117/************************************************************************
4118 * *
4119 * Parser contexts handling *
4120 * *
4121 ************************************************************************/
4122
4123/**
William M. Brackedb65a72004-02-06 07:36:04 +00004124 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004125 * @ctxt: an HTML parser context
4126 *
4127 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00004128 *
4129 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00004130 */
4131
Daniel Veillardf403d292003-10-05 13:51:35 +00004132static int
Owen Taylor3473f882001-02-23 17:55:21 +00004133htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
4134{
4135 htmlSAXHandler *sax;
4136
Daniel Veillardf403d292003-10-05 13:51:35 +00004137 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004138 memset(ctxt, 0, sizeof(htmlParserCtxt));
4139
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004140 ctxt->dict = xmlDictCreate();
4141 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004142 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4143 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004144 }
Owen Taylor3473f882001-02-23 17:55:21 +00004145 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
4146 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004147 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4148 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004149 }
4150 else
4151 memset(sax, 0, sizeof(htmlSAXHandler));
4152
4153 /* Allocate the Input stack */
4154 ctxt->inputTab = (htmlParserInputPtr *)
4155 xmlMalloc(5 * sizeof(htmlParserInputPtr));
4156 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004157 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004158 ctxt->inputNr = 0;
4159 ctxt->inputMax = 0;
4160 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004161 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004162 }
4163 ctxt->inputNr = 0;
4164 ctxt->inputMax = 5;
4165 ctxt->input = NULL;
4166 ctxt->version = NULL;
4167 ctxt->encoding = NULL;
4168 ctxt->standalone = -1;
4169 ctxt->instate = XML_PARSER_START;
4170
4171 /* Allocate the Node stack */
4172 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
4173 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004174 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004175 ctxt->nodeNr = 0;
4176 ctxt->nodeMax = 0;
4177 ctxt->node = NULL;
4178 ctxt->inputNr = 0;
4179 ctxt->inputMax = 0;
4180 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004181 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004182 }
4183 ctxt->nodeNr = 0;
4184 ctxt->nodeMax = 10;
4185 ctxt->node = NULL;
4186
4187 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004188 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00004189 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004190 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004191 ctxt->nameNr = 0;
4192 ctxt->nameMax = 10;
4193 ctxt->name = NULL;
4194 ctxt->nodeNr = 0;
4195 ctxt->nodeMax = 0;
4196 ctxt->node = NULL;
4197 ctxt->inputNr = 0;
4198 ctxt->inputMax = 0;
4199 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004200 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004201 }
4202 ctxt->nameNr = 0;
4203 ctxt->nameMax = 10;
4204 ctxt->name = NULL;
4205
Daniel Veillard092643b2003-09-25 14:29:29 +00004206 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00004207 else {
4208 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00004209 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00004210 }
4211 ctxt->userData = ctxt;
4212 ctxt->myDoc = NULL;
4213 ctxt->wellFormed = 1;
4214 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00004215 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00004216 ctxt->html = 1;
Daniel Veillardeff45a92004-10-29 12:10:55 +00004217 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
William M. Brackedb65a72004-02-06 07:36:04 +00004218 ctxt->vctxt.userData = ctxt;
4219 ctxt->vctxt.error = xmlParserValidityError;
4220 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00004221 ctxt->record_info = 0;
4222 ctxt->validate = 0;
4223 ctxt->nbChars = 0;
4224 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00004225 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004226 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00004227 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004228}
4229
4230/**
4231 * htmlFreeParserCtxt:
4232 * @ctxt: an HTML parser context
4233 *
4234 * Free all the memory used by a parser context. However the parsed
4235 * document in ctxt->myDoc is not freed.
4236 */
4237
4238void
4239htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4240{
4241 xmlFreeParserCtxt(ctxt);
4242}
4243
4244/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004245 * htmlNewParserCtxt:
4246 *
4247 * Allocate and initialize a new parser context.
4248 *
4249 * Returns the xmlParserCtxtPtr or NULL
4250 */
4251
4252static htmlParserCtxtPtr
4253htmlNewParserCtxt(void)
4254{
4255 xmlParserCtxtPtr ctxt;
4256
4257 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4258 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004259 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004260 return(NULL);
4261 }
4262 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004263 if (htmlInitParserCtxt(ctxt) < 0) {
4264 htmlFreeParserCtxt(ctxt);
4265 return(NULL);
4266 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004267 return(ctxt);
4268}
4269
4270/**
4271 * htmlCreateMemoryParserCtxt:
4272 * @buffer: a pointer to a char array
4273 * @size: the size of the array
4274 *
4275 * Create a parser context for an HTML in-memory document.
4276 *
4277 * Returns the new parser context or NULL
4278 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004279htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004280htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4281 xmlParserCtxtPtr ctxt;
4282 xmlParserInputPtr input;
4283 xmlParserInputBufferPtr buf;
4284
4285 if (buffer == NULL)
4286 return(NULL);
4287 if (size <= 0)
4288 return(NULL);
4289
4290 ctxt = htmlNewParserCtxt();
4291 if (ctxt == NULL)
4292 return(NULL);
4293
4294 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4295 if (buf == NULL) return(NULL);
4296
4297 input = xmlNewInputStream(ctxt);
4298 if (input == NULL) {
4299 xmlFreeParserCtxt(ctxt);
4300 return(NULL);
4301 }
4302
4303 input->filename = NULL;
4304 input->buf = buf;
4305 input->base = input->buf->buffer->content;
4306 input->cur = input->buf->buffer->content;
4307 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4308
4309 inputPush(ctxt, input);
4310 return(ctxt);
4311}
4312
4313/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004314 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004315 * @cur: a pointer to an array of xmlChar
4316 * @encoding: a free form C string describing the HTML document encoding, or NULL
4317 *
4318 * Create a parser context for an HTML document.
4319 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004320 * TODO: check the need to add encoding handling there
4321 *
Owen Taylor3473f882001-02-23 17:55:21 +00004322 * Returns the new parser context or NULL
4323 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004324static htmlParserCtxtPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00004325htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004326 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004327 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004328
Daniel Veillard1d995272002-07-22 16:43:32 +00004329 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004330 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004331 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004332 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
4333
4334 if (encoding != NULL) {
4335 xmlCharEncoding enc;
4336 xmlCharEncodingHandlerPtr handler;
4337
4338 if (ctxt->input->encoding != NULL)
4339 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004340 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004341
4342 enc = xmlParseCharEncoding(encoding);
4343 /*
4344 * registered set of known encodings
4345 */
4346 if (enc != XML_CHAR_ENCODING_ERROR) {
4347 xmlSwitchEncoding(ctxt, enc);
4348 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004349 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4350 "Unsupported encoding %s\n",
4351 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004352 }
4353 } else {
4354 /*
4355 * fallback for unknown encodings
4356 */
4357 handler = xmlFindCharEncodingHandler((const char *) encoding);
4358 if (handler != NULL) {
4359 xmlSwitchToEncoding(ctxt, handler);
4360 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004361 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4362 "Unsupported encoding %s\n",
4363 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004364 }
4365 }
4366 }
4367 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004368}
4369
Daniel Veillard73b013f2003-09-30 12:36:01 +00004370#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004371/************************************************************************
4372 * *
4373 * Progressive parsing interfaces *
4374 * *
4375 ************************************************************************/
4376
4377/**
4378 * htmlParseLookupSequence:
4379 * @ctxt: an HTML parser context
4380 * @first: the first char to lookup
4381 * @next: the next char to lookup or zero
4382 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004383 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004384 *
4385 * Try to find if a sequence (first, next, third) or just (first next) or
4386 * (first) is available in the input stream.
4387 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4388 * to avoid rescanning sequences of bytes, it DOES change the state of the
4389 * parser, do not use liberally.
4390 * This is basically similar to xmlParseLookupSequence()
4391 *
4392 * Returns the index to the current parsing point if the full sequence
4393 * is available, -1 otherwise.
4394 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004395static int
Owen Taylor3473f882001-02-23 17:55:21 +00004396htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
William M. Brackc1939562003-08-05 15:52:22 +00004397 xmlChar next, xmlChar third, int iscomment) {
Owen Taylor3473f882001-02-23 17:55:21 +00004398 int base, len;
4399 htmlParserInputPtr in;
4400 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004401 int incomment = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004402
4403 in = ctxt->input;
4404 if (in == NULL) return(-1);
4405 base = in->cur - in->base;
4406 if (base < 0) return(-1);
4407 if (ctxt->checkIndex > base)
4408 base = ctxt->checkIndex;
4409 if (in->buf == NULL) {
4410 buf = in->base;
4411 len = in->length;
4412 } else {
4413 buf = in->buf->buffer->content;
4414 len = in->buf->buffer->use;
4415 }
4416 /* take into account the sequence length */
4417 if (third) len -= 2;
4418 else if (next) len --;
4419 for (;base < len;base++) {
William M. Brackc1939562003-08-05 15:52:22 +00004420 if (!incomment && (base + 4 < len) && !iscomment) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00004421 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
4422 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
4423 incomment = 1;
Daniel Veillard97e01882003-07-30 18:59:19 +00004424 /* do not increment past <! - some people use <!--> */
4425 base += 2;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004426 }
Daniel Veillardc1f78342001-11-10 11:43:05 +00004427 }
4428 if (incomment) {
William M. Brack4a557d92003-07-29 04:28:04 +00004429 if (base + 3 > len)
Daniel Veillardc1f78342001-11-10 11:43:05 +00004430 return(-1);
4431 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
4432 (buf[base + 2] == '>')) {
4433 incomment = 0;
4434 base += 2;
4435 }
4436 continue;
4437 }
Owen Taylor3473f882001-02-23 17:55:21 +00004438 if (buf[base] == first) {
4439 if (third != 0) {
4440 if ((buf[base + 1] != next) ||
4441 (buf[base + 2] != third)) continue;
4442 } else if (next != 0) {
4443 if (buf[base + 1] != next) continue;
4444 }
4445 ctxt->checkIndex = 0;
4446#ifdef DEBUG_PUSH
4447 if (next == 0)
4448 xmlGenericError(xmlGenericErrorContext,
4449 "HPP: lookup '%c' found at %d\n",
4450 first, base);
4451 else if (third == 0)
4452 xmlGenericError(xmlGenericErrorContext,
4453 "HPP: lookup '%c%c' found at %d\n",
4454 first, next, base);
4455 else
4456 xmlGenericError(xmlGenericErrorContext,
4457 "HPP: lookup '%c%c%c' found at %d\n",
4458 first, next, third, base);
4459#endif
4460 return(base - (in->cur - in->base));
4461 }
4462 }
4463 ctxt->checkIndex = base;
4464#ifdef DEBUG_PUSH
4465 if (next == 0)
4466 xmlGenericError(xmlGenericErrorContext,
4467 "HPP: lookup '%c' failed\n", first);
4468 else if (third == 0)
4469 xmlGenericError(xmlGenericErrorContext,
4470 "HPP: lookup '%c%c' failed\n", first, next);
4471 else
4472 xmlGenericError(xmlGenericErrorContext,
4473 "HPP: lookup '%c%c%c' failed\n", first, next, third);
4474#endif
4475 return(-1);
4476}
4477
4478/**
4479 * htmlParseTryOrFinish:
4480 * @ctxt: an HTML parser context
4481 * @terminate: last chunk indicator
4482 *
4483 * Try to progress on parsing
4484 *
4485 * Returns zero if no parsing was possible
4486 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004487static int
Owen Taylor3473f882001-02-23 17:55:21 +00004488htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
4489 int ret = 0;
4490 htmlParserInputPtr in;
4491 int avail = 0;
4492 xmlChar cur, next;
4493
4494#ifdef DEBUG_PUSH
4495 switch (ctxt->instate) {
4496 case XML_PARSER_EOF:
4497 xmlGenericError(xmlGenericErrorContext,
4498 "HPP: try EOF\n"); break;
4499 case XML_PARSER_START:
4500 xmlGenericError(xmlGenericErrorContext,
4501 "HPP: try START\n"); break;
4502 case XML_PARSER_MISC:
4503 xmlGenericError(xmlGenericErrorContext,
4504 "HPP: try MISC\n");break;
4505 case XML_PARSER_COMMENT:
4506 xmlGenericError(xmlGenericErrorContext,
4507 "HPP: try COMMENT\n");break;
4508 case XML_PARSER_PROLOG:
4509 xmlGenericError(xmlGenericErrorContext,
4510 "HPP: try PROLOG\n");break;
4511 case XML_PARSER_START_TAG:
4512 xmlGenericError(xmlGenericErrorContext,
4513 "HPP: try START_TAG\n");break;
4514 case XML_PARSER_CONTENT:
4515 xmlGenericError(xmlGenericErrorContext,
4516 "HPP: try CONTENT\n");break;
4517 case XML_PARSER_CDATA_SECTION:
4518 xmlGenericError(xmlGenericErrorContext,
4519 "HPP: try CDATA_SECTION\n");break;
4520 case XML_PARSER_END_TAG:
4521 xmlGenericError(xmlGenericErrorContext,
4522 "HPP: try END_TAG\n");break;
4523 case XML_PARSER_ENTITY_DECL:
4524 xmlGenericError(xmlGenericErrorContext,
4525 "HPP: try ENTITY_DECL\n");break;
4526 case XML_PARSER_ENTITY_VALUE:
4527 xmlGenericError(xmlGenericErrorContext,
4528 "HPP: try ENTITY_VALUE\n");break;
4529 case XML_PARSER_ATTRIBUTE_VALUE:
4530 xmlGenericError(xmlGenericErrorContext,
4531 "HPP: try ATTRIBUTE_VALUE\n");break;
4532 case XML_PARSER_DTD:
4533 xmlGenericError(xmlGenericErrorContext,
4534 "HPP: try DTD\n");break;
4535 case XML_PARSER_EPILOG:
4536 xmlGenericError(xmlGenericErrorContext,
4537 "HPP: try EPILOG\n");break;
4538 case XML_PARSER_PI:
4539 xmlGenericError(xmlGenericErrorContext,
4540 "HPP: try PI\n");break;
4541 case XML_PARSER_SYSTEM_LITERAL:
4542 xmlGenericError(xmlGenericErrorContext,
4543 "HPP: try SYSTEM_LITERAL\n");break;
4544 }
4545#endif
4546
4547 while (1) {
4548
4549 in = ctxt->input;
4550 if (in == NULL) break;
4551 if (in->buf == NULL)
4552 avail = in->length - (in->cur - in->base);
4553 else
4554 avail = in->buf->buffer->use - (in->cur - in->base);
4555 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004556 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004557 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4558 /*
4559 * SAX: end of the document processing.
4560 */
4561 ctxt->instate = XML_PARSER_EOF;
4562 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4563 ctxt->sax->endDocument(ctxt->userData);
4564 }
4565 }
4566 if (avail < 1)
4567 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00004568 cur = in->cur[0];
4569 if (cur == 0) {
4570 SKIP(1);
4571 continue;
4572 }
4573
Owen Taylor3473f882001-02-23 17:55:21 +00004574 switch (ctxt->instate) {
4575 case XML_PARSER_EOF:
4576 /*
4577 * Document parsing is done !
4578 */
4579 goto done;
4580 case XML_PARSER_START:
4581 /*
4582 * Very first chars read from the document flow.
4583 */
4584 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004585 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004586 SKIP_BLANKS;
4587 if (in->buf == NULL)
4588 avail = in->length - (in->cur - in->base);
4589 else
4590 avail = in->buf->buffer->use - (in->cur - in->base);
4591 }
4592 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4593 ctxt->sax->setDocumentLocator(ctxt->userData,
4594 &xmlDefaultSAXLocator);
4595 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4596 (!ctxt->disableSAX))
4597 ctxt->sax->startDocument(ctxt->userData);
4598
4599 cur = in->cur[0];
4600 next = in->cur[1];
4601 if ((cur == '<') && (next == '!') &&
4602 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4603 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4604 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4605 (UPP(8) == 'E')) {
4606 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004607 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004608 goto done;
4609#ifdef DEBUG_PUSH
4610 xmlGenericError(xmlGenericErrorContext,
4611 "HPP: Parsing internal subset\n");
4612#endif
4613 htmlParseDocTypeDecl(ctxt);
4614 ctxt->instate = XML_PARSER_PROLOG;
4615#ifdef DEBUG_PUSH
4616 xmlGenericError(xmlGenericErrorContext,
4617 "HPP: entering PROLOG\n");
4618#endif
4619 } else {
4620 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004621#ifdef DEBUG_PUSH
Daniel Veillard597f1c12005-07-03 23:00:18 +00004622 xmlGenericError(xmlGenericErrorContext,
4623 "HPP: entering MISC\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004624#endif
Daniel Veillard597f1c12005-07-03 23:00:18 +00004625 }
Owen Taylor3473f882001-02-23 17:55:21 +00004626 break;
4627 case XML_PARSER_MISC:
4628 SKIP_BLANKS;
4629 if (in->buf == NULL)
4630 avail = in->length - (in->cur - in->base);
4631 else
4632 avail = in->buf->buffer->use - (in->cur - in->base);
4633 if (avail < 2)
4634 goto done;
4635 cur = in->cur[0];
4636 next = in->cur[1];
4637 if ((cur == '<') && (next == '!') &&
4638 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4639 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004640 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004641 goto done;
4642#ifdef DEBUG_PUSH
4643 xmlGenericError(xmlGenericErrorContext,
4644 "HPP: Parsing Comment\n");
4645#endif
4646 htmlParseComment(ctxt);
4647 ctxt->instate = XML_PARSER_MISC;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004648 } else if ((cur == '<') && (next == '?')) {
4649 if ((!terminate) &&
4650 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4651 goto done;
4652#ifdef DEBUG_PUSH
4653 xmlGenericError(xmlGenericErrorContext,
4654 "HPP: Parsing PI\n");
4655#endif
4656 htmlParsePI(ctxt);
4657 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004658 } else if ((cur == '<') && (next == '!') &&
4659 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4660 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4661 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4662 (UPP(8) == 'E')) {
4663 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004664 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004665 goto done;
4666#ifdef DEBUG_PUSH
4667 xmlGenericError(xmlGenericErrorContext,
4668 "HPP: Parsing internal subset\n");
4669#endif
4670 htmlParseDocTypeDecl(ctxt);
4671 ctxt->instate = XML_PARSER_PROLOG;
4672#ifdef DEBUG_PUSH
4673 xmlGenericError(xmlGenericErrorContext,
4674 "HPP: entering PROLOG\n");
4675#endif
4676 } else if ((cur == '<') && (next == '!') &&
4677 (avail < 9)) {
4678 goto done;
4679 } else {
4680 ctxt->instate = XML_PARSER_START_TAG;
4681#ifdef DEBUG_PUSH
4682 xmlGenericError(xmlGenericErrorContext,
4683 "HPP: entering START_TAG\n");
4684#endif
4685 }
4686 break;
4687 case XML_PARSER_PROLOG:
4688 SKIP_BLANKS;
4689 if (in->buf == NULL)
4690 avail = in->length - (in->cur - in->base);
4691 else
4692 avail = in->buf->buffer->use - (in->cur - in->base);
4693 if (avail < 2)
4694 goto done;
4695 cur = in->cur[0];
4696 next = in->cur[1];
4697 if ((cur == '<') && (next == '!') &&
4698 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4699 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004700 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004701 goto done;
4702#ifdef DEBUG_PUSH
4703 xmlGenericError(xmlGenericErrorContext,
4704 "HPP: Parsing Comment\n");
4705#endif
4706 htmlParseComment(ctxt);
4707 ctxt->instate = XML_PARSER_PROLOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004708 } else if ((cur == '<') && (next == '?')) {
4709 if ((!terminate) &&
4710 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4711 goto done;
4712#ifdef DEBUG_PUSH
4713 xmlGenericError(xmlGenericErrorContext,
4714 "HPP: Parsing PI\n");
4715#endif
4716 htmlParsePI(ctxt);
4717 ctxt->instate = XML_PARSER_PROLOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004718 } else if ((cur == '<') && (next == '!') &&
4719 (avail < 4)) {
4720 goto done;
4721 } else {
4722 ctxt->instate = XML_PARSER_START_TAG;
4723#ifdef DEBUG_PUSH
4724 xmlGenericError(xmlGenericErrorContext,
4725 "HPP: entering START_TAG\n");
4726#endif
4727 }
4728 break;
4729 case XML_PARSER_EPILOG:
4730 if (in->buf == NULL)
4731 avail = in->length - (in->cur - in->base);
4732 else
4733 avail = in->buf->buffer->use - (in->cur - in->base);
4734 if (avail < 1)
4735 goto done;
4736 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004737 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004738 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004739 goto done;
4740 }
4741 if (avail < 2)
4742 goto done;
4743 next = in->cur[1];
4744 if ((cur == '<') && (next == '!') &&
4745 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4746 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004747 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004748 goto done;
4749#ifdef DEBUG_PUSH
4750 xmlGenericError(xmlGenericErrorContext,
4751 "HPP: Parsing Comment\n");
4752#endif
4753 htmlParseComment(ctxt);
4754 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004755 } else if ((cur == '<') && (next == '?')) {
4756 if ((!terminate) &&
4757 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4758 goto done;
4759#ifdef DEBUG_PUSH
4760 xmlGenericError(xmlGenericErrorContext,
4761 "HPP: Parsing PI\n");
4762#endif
4763 htmlParsePI(ctxt);
4764 ctxt->instate = XML_PARSER_EPILOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004765 } else if ((cur == '<') && (next == '!') &&
4766 (avail < 4)) {
4767 goto done;
4768 } else {
4769 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004770 ctxt->wellFormed = 0;
4771 ctxt->instate = XML_PARSER_EOF;
4772#ifdef DEBUG_PUSH
4773 xmlGenericError(xmlGenericErrorContext,
4774 "HPP: entering EOF\n");
4775#endif
4776 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4777 ctxt->sax->endDocument(ctxt->userData);
4778 goto done;
4779 }
4780 break;
4781 case XML_PARSER_START_TAG: {
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004782 const xmlChar *name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004783 int failed;
Daniel Veillardbb371292001-08-16 23:26:59 +00004784 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004785
4786 if (avail < 2)
4787 goto done;
4788 cur = in->cur[0];
4789 if (cur != '<') {
4790 ctxt->instate = XML_PARSER_CONTENT;
4791#ifdef DEBUG_PUSH
4792 xmlGenericError(xmlGenericErrorContext,
4793 "HPP: entering CONTENT\n");
4794#endif
4795 break;
4796 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004797 if (in->cur[1] == '/') {
4798 ctxt->instate = XML_PARSER_END_TAG;
4799 ctxt->checkIndex = 0;
4800#ifdef DEBUG_PUSH
4801 xmlGenericError(xmlGenericErrorContext,
4802 "HPP: entering END_TAG\n");
4803#endif
4804 break;
4805 }
Owen Taylor3473f882001-02-23 17:55:21 +00004806 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004807 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004808 goto done;
4809
Daniel Veillard597f1c12005-07-03 23:00:18 +00004810 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004811 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004812 if (failed ||
Owen Taylor3473f882001-02-23 17:55:21 +00004813 (name == NULL)) {
4814 if (CUR == '>')
4815 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004816 break;
4817 }
Owen Taylor3473f882001-02-23 17:55:21 +00004818
4819 /*
4820 * Lookup the info for that element.
4821 */
4822 info = htmlTagLookup(name);
4823 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004824 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4825 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004826 }
4827
4828 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004829 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004830 */
4831 if ((CUR == '/') && (NXT(1) == '>')) {
4832 SKIP(2);
4833 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4834 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004835 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004836 ctxt->instate = XML_PARSER_CONTENT;
4837#ifdef DEBUG_PUSH
4838 xmlGenericError(xmlGenericErrorContext,
4839 "HPP: entering CONTENT\n");
4840#endif
4841 break;
4842 }
4843
4844 if (CUR == '>') {
4845 NEXT;
4846 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004847 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4848 "Couldn't find end of Start Tag %s\n",
4849 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004850
4851 /*
4852 * end of parsing of this node.
4853 */
4854 if (xmlStrEqual(name, ctxt->name)) {
4855 nodePop(ctxt);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004856 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004857 }
4858
4859 ctxt->instate = XML_PARSER_CONTENT;
4860#ifdef DEBUG_PUSH
4861 xmlGenericError(xmlGenericErrorContext,
4862 "HPP: entering CONTENT\n");
4863#endif
4864 break;
4865 }
4866
4867 /*
4868 * Check for an Empty Element from DTD definition
4869 */
4870 if ((info != NULL) && (info->empty)) {
4871 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4872 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004873 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004874 }
4875 ctxt->instate = XML_PARSER_CONTENT;
4876#ifdef DEBUG_PUSH
4877 xmlGenericError(xmlGenericErrorContext,
4878 "HPP: entering CONTENT\n");
4879#endif
4880 break;
4881 }
4882 case XML_PARSER_CONTENT: {
4883 long cons;
4884 /*
4885 * Handle preparsed entities and charRef
4886 */
4887 if (ctxt->token != 0) {
4888 xmlChar chr[2] = { 0 , 0 } ;
4889
4890 chr[0] = (xmlChar) ctxt->token;
4891 htmlCheckParagraph(ctxt);
4892 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4893 ctxt->sax->characters(ctxt->userData, chr, 1);
4894 ctxt->token = 0;
4895 ctxt->checkIndex = 0;
4896 }
4897 if ((avail == 1) && (terminate)) {
4898 cur = in->cur[0];
4899 if ((cur != '<') && (cur != '&')) {
4900 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00004901 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004902 if (ctxt->sax->ignorableWhitespace != NULL)
4903 ctxt->sax->ignorableWhitespace(
4904 ctxt->userData, &cur, 1);
4905 } else {
4906 htmlCheckParagraph(ctxt);
4907 if (ctxt->sax->characters != NULL)
4908 ctxt->sax->characters(
4909 ctxt->userData, &cur, 1);
4910 }
4911 }
4912 ctxt->token = 0;
4913 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00004914 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00004915 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004916 }
Owen Taylor3473f882001-02-23 17:55:21 +00004917 }
4918 if (avail < 2)
4919 goto done;
4920 cur = in->cur[0];
4921 next = in->cur[1];
4922 cons = ctxt->nbChars;
4923 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4924 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4925 /*
4926 * Handle SCRIPT/STYLE separately
4927 */
4928 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004929 (htmlParseLookupSequence(ctxt, '<', '/', 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004930 goto done;
4931 htmlParseScript(ctxt);
4932 if ((cur == '<') && (next == '/')) {
4933 ctxt->instate = XML_PARSER_END_TAG;
4934 ctxt->checkIndex = 0;
4935#ifdef DEBUG_PUSH
4936 xmlGenericError(xmlGenericErrorContext,
4937 "HPP: entering END_TAG\n");
4938#endif
4939 break;
4940 }
4941 } else {
4942 /*
4943 * Sometimes DOCTYPE arrives in the middle of the document
4944 */
4945 if ((cur == '<') && (next == '!') &&
4946 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4947 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4948 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4949 (UPP(8) == 'E')) {
4950 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004951 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004952 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00004953 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4954 "Misplaced DOCTYPE declaration\n",
4955 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004956 htmlParseDocTypeDecl(ctxt);
4957 } else if ((cur == '<') && (next == '!') &&
4958 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4959 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004960 (htmlParseLookupSequence(
4961 ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004962 goto done;
4963#ifdef DEBUG_PUSH
4964 xmlGenericError(xmlGenericErrorContext,
4965 "HPP: Parsing Comment\n");
4966#endif
4967 htmlParseComment(ctxt);
4968 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004969 } else if ((cur == '<') && (next == '?')) {
4970 if ((!terminate) &&
4971 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4972 goto done;
4973#ifdef DEBUG_PUSH
4974 xmlGenericError(xmlGenericErrorContext,
4975 "HPP: Parsing PI\n");
4976#endif
4977 htmlParsePI(ctxt);
4978 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +00004979 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
4980 goto done;
4981 } else if ((cur == '<') && (next == '/')) {
4982 ctxt->instate = XML_PARSER_END_TAG;
4983 ctxt->checkIndex = 0;
4984#ifdef DEBUG_PUSH
4985 xmlGenericError(xmlGenericErrorContext,
4986 "HPP: entering END_TAG\n");
4987#endif
4988 break;
4989 } else if (cur == '<') {
4990 ctxt->instate = XML_PARSER_START_TAG;
4991 ctxt->checkIndex = 0;
4992#ifdef DEBUG_PUSH
4993 xmlGenericError(xmlGenericErrorContext,
4994 "HPP: entering START_TAG\n");
4995#endif
4996 break;
4997 } else if (cur == '&') {
4998 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004999 (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005000 goto done;
5001#ifdef DEBUG_PUSH
5002 xmlGenericError(xmlGenericErrorContext,
5003 "HPP: Parsing Reference\n");
5004#endif
5005 /* TODO: check generation of subtrees if noent !!! */
5006 htmlParseReference(ctxt);
5007 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005008 /*
5009 * check that the text sequence is complete
5010 * before handing out the data to the parser
5011 * to avoid problems with erroneous end of
5012 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00005013 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00005014 if ((!terminate) &&
5015 (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
5016 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00005017 ctxt->checkIndex = 0;
5018#ifdef DEBUG_PUSH
5019 xmlGenericError(xmlGenericErrorContext,
5020 "HPP: Parsing char data\n");
5021#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005022 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005023 }
5024 }
5025 if (cons == ctxt->nbChars) {
5026 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005027 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5028 "detected an error in element content\n",
5029 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005030 }
5031 NEXT;
5032 break;
5033 }
5034
5035 break;
5036 }
5037 case XML_PARSER_END_TAG:
5038 if (avail < 2)
5039 goto done;
5040 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005041 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005042 goto done;
5043 htmlParseEndTag(ctxt);
5044 if (ctxt->nameNr == 0) {
5045 ctxt->instate = XML_PARSER_EPILOG;
5046 } else {
5047 ctxt->instate = XML_PARSER_CONTENT;
5048 }
5049 ctxt->checkIndex = 0;
5050#ifdef DEBUG_PUSH
5051 xmlGenericError(xmlGenericErrorContext,
5052 "HPP: entering CONTENT\n");
5053#endif
5054 break;
5055 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00005056 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5057 "HPP: internal error, state == CDATA\n",
5058 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005059 ctxt->instate = XML_PARSER_CONTENT;
5060 ctxt->checkIndex = 0;
5061#ifdef DEBUG_PUSH
5062 xmlGenericError(xmlGenericErrorContext,
5063 "HPP: entering CONTENT\n");
5064#endif
5065 break;
5066 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00005067 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5068 "HPP: internal error, state == DTD\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_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00005078 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5079 "HPP: internal error, state == COMMENT\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_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00005089 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5090 "HPP: internal error, state == PI\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_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005100 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5101 "HPP: internal error, state == ENTITY_DECL\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_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005111 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5112 "HPP: internal error, state == ENTITY_VALUE\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 DTD\n");
5119#endif
5120 break;
5121 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005122 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5123 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
5124 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005125 ctxt->instate = XML_PARSER_START_TAG;
5126 ctxt->checkIndex = 0;
5127#ifdef DEBUG_PUSH
5128 xmlGenericError(xmlGenericErrorContext,
5129 "HPP: entering START_TAG\n");
5130#endif
5131 break;
5132 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005133 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5134 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
5135 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005136 ctxt->instate = XML_PARSER_CONTENT;
5137 ctxt->checkIndex = 0;
5138#ifdef DEBUG_PUSH
5139 xmlGenericError(xmlGenericErrorContext,
5140 "HPP: entering CONTENT\n");
5141#endif
5142 break;
5143 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005144 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5145 "HPP: internal error, state == XML_PARSER_IGNORE\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;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005154 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005155 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5156 "HPP: internal error, state == XML_PARSER_LITERAL\n",
5157 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +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;
5165
Owen Taylor3473f882001-02-23 17:55:21 +00005166 }
5167 }
5168done:
5169 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005170 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005171 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5172 /*
5173 * SAX: end of the document processing.
5174 */
5175 ctxt->instate = XML_PARSER_EOF;
5176 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5177 ctxt->sax->endDocument(ctxt->userData);
5178 }
5179 }
5180 if ((ctxt->myDoc != NULL) &&
5181 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5182 (ctxt->instate == XML_PARSER_EPILOG))) {
5183 xmlDtdPtr dtd;
5184 dtd = xmlGetIntSubset(ctxt->myDoc);
5185 if (dtd == NULL)
5186 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00005187 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00005188 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5189 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5190 }
5191#ifdef DEBUG_PUSH
5192 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5193#endif
5194 return(ret);
5195}
5196
5197/**
Owen Taylor3473f882001-02-23 17:55:21 +00005198 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00005199 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00005200 * @chunk: an char array
5201 * @size: the size in byte of the chunk
5202 * @terminate: last chunk indicator
5203 *
5204 * Parse a Chunk of memory
5205 *
5206 * Returns zero if no error, the xmlParserErrors otherwise.
5207 */
5208int
5209htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
5210 int terminate) {
Daniel Veillarda03e3652004-11-02 18:45:30 +00005211 if ((ctxt == NULL) || (ctxt->input == NULL)) {
5212 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5213 "htmlParseChunk: context error\n", NULL, NULL);
5214 return(XML_ERR_INTERNAL_ERROR);
5215 }
Owen Taylor3473f882001-02-23 17:55:21 +00005216 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5217 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
5218 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5219 int cur = ctxt->input->cur - ctxt->input->base;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005220 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00005221
Daniel Veillardd2755a82005-08-07 23:42:39 +00005222 res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5223 if (res < 0) {
5224 ctxt->errNo = XML_PARSER_EOF;
5225 ctxt->disableSAX = 1;
5226 return (XML_PARSER_EOF);
5227 }
Owen Taylor3473f882001-02-23 17:55:21 +00005228 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5229 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005230 ctxt->input->end =
5231 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005232#ifdef DEBUG_PUSH
5233 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5234#endif
5235
Daniel Veillard14f752c2003-08-09 11:44:50 +00005236#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00005237 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5238 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005239#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005240 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005241 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
5242 xmlParserInputBufferPtr in = ctxt->input->buf;
5243 if ((in->encoder != NULL) && (in->buffer != NULL) &&
5244 (in->raw != NULL)) {
5245 int nbchars;
5246
5247 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
5248 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005249 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
5250 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005251 return(XML_ERR_INVALID_ENCODING);
5252 }
5253 }
5254 }
Owen Taylor3473f882001-02-23 17:55:21 +00005255 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00005256 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00005257 if (terminate) {
5258 if ((ctxt->instate != XML_PARSER_EOF) &&
5259 (ctxt->instate != XML_PARSER_EPILOG) &&
5260 (ctxt->instate != XML_PARSER_MISC)) {
5261 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005262 ctxt->wellFormed = 0;
5263 }
5264 if (ctxt->instate != XML_PARSER_EOF) {
5265 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5266 ctxt->sax->endDocument(ctxt->userData);
5267 }
5268 ctxt->instate = XML_PARSER_EOF;
5269 }
5270 return((xmlParserErrors) ctxt->errNo);
5271}
5272
5273/************************************************************************
5274 * *
5275 * User entry points *
5276 * *
5277 ************************************************************************/
5278
5279/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005280 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005281 * @sax: a SAX handler
5282 * @user_data: The user data returned on SAX callbacks
5283 * @chunk: a pointer to an array of chars
5284 * @size: number of chars in the array
5285 * @filename: an optional file name or URI
5286 * @enc: an optional encoding
5287 *
5288 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005289 * The value of @filename is used for fetching external entities
5290 * and error/warning reports.
5291 *
5292 * Returns the new parser context or NULL
5293 */
5294htmlParserCtxtPtr
5295htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
5296 const char *chunk, int size, const char *filename,
5297 xmlCharEncoding enc) {
5298 htmlParserCtxtPtr ctxt;
5299 htmlParserInputPtr inputStream;
5300 xmlParserInputBufferPtr buf;
5301
Daniel Veillardd0463562001-10-13 09:15:48 +00005302 xmlInitParser();
5303
Owen Taylor3473f882001-02-23 17:55:21 +00005304 buf = xmlAllocParserInputBuffer(enc);
5305 if (buf == NULL) return(NULL);
5306
Daniel Veillardf403d292003-10-05 13:51:35 +00005307 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005308 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005309 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005310 return(NULL);
5311 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00005312 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
5313 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00005314 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00005315 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00005316 xmlFree(ctxt->sax);
5317 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
5318 if (ctxt->sax == NULL) {
5319 xmlFree(buf);
5320 xmlFree(ctxt);
5321 return(NULL);
5322 }
5323 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
5324 if (user_data != NULL)
5325 ctxt->userData = user_data;
5326 }
5327 if (filename == NULL) {
5328 ctxt->directory = NULL;
5329 } else {
5330 ctxt->directory = xmlParserGetDirectory(filename);
5331 }
5332
5333 inputStream = htmlNewInputStream(ctxt);
5334 if (inputStream == NULL) {
5335 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00005336 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005337 return(NULL);
5338 }
5339
5340 if (filename == NULL)
5341 inputStream->filename = NULL;
5342 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00005343 inputStream->filename = (char *)
5344 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00005345 inputStream->buf = buf;
5346 inputStream->base = inputStream->buf->buffer->content;
5347 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard5f704af2003-03-05 10:01:43 +00005348 inputStream->end =
5349 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005350
5351 inputPush(ctxt, inputStream);
5352
5353 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5354 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00005355 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5356 int cur = ctxt->input->cur - ctxt->input->base;
5357
Owen Taylor3473f882001-02-23 17:55:21 +00005358 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00005359
5360 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5361 ctxt->input->cur = ctxt->input->base + cur;
5362 ctxt->input->end =
5363 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005364#ifdef DEBUG_PUSH
5365 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5366#endif
5367 }
5368
5369 return(ctxt);
5370}
William M. Brack21e4ef22005-01-02 09:53:13 +00005371#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005372
5373/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005374 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005375 * @cur: a pointer to an array of xmlChar
5376 * @encoding: a free form C string describing the HTML document encoding, or NULL
5377 * @sax: the SAX handler block
5378 * @userData: if using SAX, this pointer will be provided on callbacks.
5379 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005380 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
5381 * to handle parse events. If sax is NULL, fallback to the default DOM
5382 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00005383 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005384 * Returns the resulting document tree unless SAX is NULL or the document is
5385 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005386 */
5387
5388htmlDocPtr
5389htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
5390 htmlDocPtr ret;
5391 htmlParserCtxtPtr ctxt;
5392
Daniel Veillardd0463562001-10-13 09:15:48 +00005393 xmlInitParser();
5394
Owen Taylor3473f882001-02-23 17:55:21 +00005395 if (cur == NULL) return(NULL);
5396
5397
5398 ctxt = htmlCreateDocParserCtxt(cur, encoding);
5399 if (ctxt == NULL) return(NULL);
5400 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00005401 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00005402 ctxt->sax = sax;
5403 ctxt->userData = userData;
5404 }
5405
5406 htmlParseDocument(ctxt);
5407 ret = ctxt->myDoc;
5408 if (sax != NULL) {
5409 ctxt->sax = NULL;
5410 ctxt->userData = NULL;
5411 }
5412 htmlFreeParserCtxt(ctxt);
5413
5414 return(ret);
5415}
5416
5417/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005418 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005419 * @cur: a pointer to an array of xmlChar
5420 * @encoding: a free form C string describing the HTML document encoding, or NULL
5421 *
5422 * parse an HTML in-memory document and build a tree.
5423 *
5424 * Returns the resulting document tree
5425 */
5426
5427htmlDocPtr
5428htmlParseDoc(xmlChar *cur, const char *encoding) {
5429 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
5430}
5431
5432
5433/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005434 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005435 * @filename: the filename
5436 * @encoding: a free form C string describing the HTML document encoding, or NULL
5437 *
5438 * Create a parser context for a file content.
5439 * Automatic support for ZLIB/Compress compressed document is provided
5440 * by default if found at compile-time.
5441 *
5442 * Returns the new parser context or NULL
5443 */
5444htmlParserCtxtPtr
5445htmlCreateFileParserCtxt(const char *filename, const char *encoding)
5446{
5447 htmlParserCtxtPtr ctxt;
5448 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005449 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00005450 /* htmlCharEncoding enc; */
5451 xmlChar *content, *content_line = (xmlChar *) "charset=";
5452
Daniel Veillarda03e3652004-11-02 18:45:30 +00005453 if (filename == NULL)
5454 return(NULL);
5455
Daniel Veillardf403d292003-10-05 13:51:35 +00005456 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005457 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005458 return(NULL);
5459 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005460 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
5461 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00005462#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005463 if (xmlDefaultSAXHandler.error != NULL) {
5464 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
5465 }
Daniel Veillard87247e82004-01-13 20:42:02 +00005466#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00005467 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005468 return(NULL);
5469 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005470
5471 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
5472 xmlFree(canonicFilename);
5473 if (inputStream == NULL) {
5474 xmlFreeParserCtxt(ctxt);
5475 return(NULL);
5476 }
Owen Taylor3473f882001-02-23 17:55:21 +00005477
5478 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005479
Owen Taylor3473f882001-02-23 17:55:21 +00005480 /* set encoding */
5481 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005482 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Owen Taylor3473f882001-02-23 17:55:21 +00005483 if (content) {
5484 strcpy ((char *)content, (char *)content_line);
5485 strcat ((char *)content, (char *)encoding);
5486 htmlCheckEncoding (ctxt, content);
5487 xmlFree (content);
5488 }
5489 }
5490
5491 return(ctxt);
5492}
5493
5494/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005495 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005496 * @filename: the filename
5497 * @encoding: a free form C string describing the HTML document encoding, or NULL
5498 * @sax: the SAX handler block
5499 * @userData: if using SAX, this pointer will be provided on callbacks.
5500 *
5501 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5502 * compressed document is provided by default if found at compile-time.
5503 * It use the given SAX function block to handle the parsing callback.
5504 * If sax is NULL, fallback to the default DOM tree building routines.
5505 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005506 * Returns the resulting document tree unless SAX is NULL or the document is
5507 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005508 */
5509
5510htmlDocPtr
5511htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
5512 void *userData) {
5513 htmlDocPtr ret;
5514 htmlParserCtxtPtr ctxt;
5515 htmlSAXHandlerPtr oldsax = NULL;
5516
Daniel Veillardd0463562001-10-13 09:15:48 +00005517 xmlInitParser();
5518
Owen Taylor3473f882001-02-23 17:55:21 +00005519 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5520 if (ctxt == NULL) return(NULL);
5521 if (sax != NULL) {
5522 oldsax = ctxt->sax;
5523 ctxt->sax = sax;
5524 ctxt->userData = userData;
5525 }
5526
5527 htmlParseDocument(ctxt);
5528
5529 ret = ctxt->myDoc;
5530 if (sax != NULL) {
5531 ctxt->sax = oldsax;
5532 ctxt->userData = NULL;
5533 }
5534 htmlFreeParserCtxt(ctxt);
5535
5536 return(ret);
5537}
5538
5539/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005540 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005541 * @filename: the filename
5542 * @encoding: a free form C string describing the HTML document encoding, or NULL
5543 *
5544 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5545 * compressed document is provided by default if found at compile-time.
5546 *
5547 * Returns the resulting document tree
5548 */
5549
5550htmlDocPtr
5551htmlParseFile(const char *filename, const char *encoding) {
5552 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
5553}
5554
5555/**
5556 * htmlHandleOmittedElem:
5557 * @val: int 0 or 1
5558 *
5559 * Set and return the previous value for handling HTML omitted tags.
5560 *
5561 * Returns the last value for 0 for no handling, 1 for auto insertion.
5562 */
5563
5564int
5565htmlHandleOmittedElem(int val) {
5566 int old = htmlOmittedDefaultValue;
5567
5568 htmlOmittedDefaultValue = val;
5569 return(old);
5570}
5571
Daniel Veillard930dfb62003-02-05 10:17:38 +00005572/**
5573 * htmlElementAllowedHere:
5574 * @parent: HTML parent element
5575 * @elt: HTML element
5576 *
5577 * Checks whether an HTML element may be a direct child of a parent element.
5578 * Note - doesn't check for deprecated elements
5579 *
5580 * Returns 1 if allowed; 0 otherwise.
5581 */
5582int
5583htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
5584 const char** p ;
5585
5586 if ( ! elt || ! parent || ! parent->subelts )
5587 return 0 ;
5588
5589 for ( p = parent->subelts; *p; ++p )
5590 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
5591 return 1 ;
5592
5593 return 0 ;
5594}
5595/**
5596 * htmlElementStatusHere:
5597 * @parent: HTML parent element
5598 * @elt: HTML element
5599 *
5600 * Checks whether an HTML element may be a direct child of a parent element.
5601 * and if so whether it is valid or deprecated.
5602 *
5603 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5604 */
5605htmlStatus
5606htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
5607 if ( ! parent || ! elt )
5608 return HTML_INVALID ;
5609 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
5610 return HTML_INVALID ;
5611
5612 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
5613}
5614/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005615 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00005616 * @elt: HTML element
5617 * @attr: HTML attribute
5618 * @legacy: whether to allow deprecated attributes
5619 *
5620 * Checks whether an attribute is valid for an element
5621 * Has full knowledge of Required and Deprecated attributes
5622 *
5623 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5624 */
5625htmlStatus
5626htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
5627 const char** p ;
5628
5629 if ( !elt || ! attr )
5630 return HTML_INVALID ;
5631
5632 if ( elt->attrs_req )
5633 for ( p = elt->attrs_req; *p; ++p)
5634 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5635 return HTML_REQUIRED ;
5636
5637 if ( elt->attrs_opt )
5638 for ( p = elt->attrs_opt; *p; ++p)
5639 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5640 return HTML_VALID ;
5641
5642 if ( legacy && elt->attrs_depr )
5643 for ( p = elt->attrs_depr; *p; ++p)
5644 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5645 return HTML_DEPRECATED ;
5646
5647 return HTML_INVALID ;
5648}
5649/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005650 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005651 * @node: an htmlNodePtr in a tree
5652 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00005653 * for Element nodes)
5654 *
5655 * Checks whether the tree node is valid. Experimental (the author
5656 * only uses the HTML enhancements in a SAX parser)
5657 *
5658 * Return: for Element nodes, a return from htmlElementAllowedHere (if
5659 * legacy allowed) or htmlElementStatusHere (otherwise).
5660 * for Attribute nodes, a return from htmlAttrAllowed
5661 * for other nodes, HTML_NA (no checks performed)
5662 */
5663htmlStatus
5664htmlNodeStatus(const htmlNodePtr node, int legacy) {
5665 if ( ! node )
5666 return HTML_INVALID ;
5667
5668 switch ( node->type ) {
5669 case XML_ELEMENT_NODE:
5670 return legacy
5671 ? ( htmlElementAllowedHere (
5672 htmlTagLookup(node->parent->name) , node->name
5673 ) ? HTML_VALID : HTML_INVALID )
5674 : htmlElementStatusHere(
5675 htmlTagLookup(node->parent->name) ,
5676 htmlTagLookup(node->name) )
5677 ;
5678 case XML_ATTRIBUTE_NODE:
5679 return htmlAttrAllowed(
5680 htmlTagLookup(node->parent->name) , node->name, legacy) ;
5681 default: return HTML_NA ;
5682 }
5683}
Daniel Veillard9475a352003-09-26 12:47:50 +00005684/************************************************************************
5685 * *
5686 * New set (2.6.0) of simpler and more flexible APIs *
5687 * *
5688 ************************************************************************/
5689/**
5690 * DICT_FREE:
5691 * @str: a string
5692 *
5693 * Free a string if it is not owned by the "dict" dictionnary in the
5694 * current scope
5695 */
5696#define DICT_FREE(str) \
5697 if ((str) && ((!dict) || \
5698 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
5699 xmlFree((char *)(str));
5700
5701/**
5702 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00005703 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00005704 *
5705 * Reset a parser context
5706 */
5707void
5708htmlCtxtReset(htmlParserCtxtPtr ctxt)
5709{
5710 xmlParserInputPtr input;
Daniel Veillarda03e3652004-11-02 18:45:30 +00005711 xmlDictPtr dict;
5712
5713 if (ctxt == NULL)
5714 return;
5715
5716 dict = ctxt->dict;
Daniel Veillard9475a352003-09-26 12:47:50 +00005717
5718 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
5719 xmlFreeInputStream(input);
5720 }
5721 ctxt->inputNr = 0;
5722 ctxt->input = NULL;
5723
5724 ctxt->spaceNr = 0;
Daniel Veillarda521d282004-11-09 14:59:59 +00005725 if (ctxt->spaceTab != NULL) {
5726 ctxt->spaceTab[0] = -1;
5727 ctxt->space = &ctxt->spaceTab[0];
5728 } else {
5729 ctxt->space = NULL;
5730 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005731
5732
5733 ctxt->nodeNr = 0;
5734 ctxt->node = NULL;
5735
5736 ctxt->nameNr = 0;
5737 ctxt->name = NULL;
5738
5739 DICT_FREE(ctxt->version);
5740 ctxt->version = NULL;
5741 DICT_FREE(ctxt->encoding);
5742 ctxt->encoding = NULL;
5743 DICT_FREE(ctxt->directory);
5744 ctxt->directory = NULL;
5745 DICT_FREE(ctxt->extSubURI);
5746 ctxt->extSubURI = NULL;
5747 DICT_FREE(ctxt->extSubSystem);
5748 ctxt->extSubSystem = NULL;
5749 if (ctxt->myDoc != NULL)
5750 xmlFreeDoc(ctxt->myDoc);
5751 ctxt->myDoc = NULL;
5752
5753 ctxt->standalone = -1;
5754 ctxt->hasExternalSubset = 0;
5755 ctxt->hasPErefs = 0;
5756 ctxt->html = 1;
5757 ctxt->external = 0;
5758 ctxt->instate = XML_PARSER_START;
5759 ctxt->token = 0;
5760
5761 ctxt->wellFormed = 1;
5762 ctxt->nsWellFormed = 1;
5763 ctxt->valid = 1;
5764 ctxt->vctxt.userData = ctxt;
5765 ctxt->vctxt.error = xmlParserValidityError;
5766 ctxt->vctxt.warning = xmlParserValidityWarning;
5767 ctxt->record_info = 0;
5768 ctxt->nbChars = 0;
5769 ctxt->checkIndex = 0;
5770 ctxt->inSubset = 0;
5771 ctxt->errNo = XML_ERR_OK;
5772 ctxt->depth = 0;
5773 ctxt->charset = XML_CHAR_ENCODING_UTF8;
5774 ctxt->catalogs = NULL;
5775 xmlInitNodeInfoSeq(&ctxt->node_seq);
5776
5777 if (ctxt->attsDefault != NULL) {
5778 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
5779 ctxt->attsDefault = NULL;
5780 }
5781 if (ctxt->attsSpecial != NULL) {
5782 xmlHashFree(ctxt->attsSpecial, NULL);
5783 ctxt->attsSpecial = NULL;
5784 }
5785}
5786
5787/**
5788 * htmlCtxtUseOptions:
5789 * @ctxt: an HTML parser context
5790 * @options: a combination of htmlParserOption(s)
5791 *
5792 * Applies the options to the parser context
5793 *
5794 * Returns 0 in case of success, the set of unknown or unimplemented options
5795 * in case of error.
5796 */
5797int
5798htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
5799{
Daniel Veillarda03e3652004-11-02 18:45:30 +00005800 if (ctxt == NULL)
5801 return(-1);
5802
Daniel Veillard9475a352003-09-26 12:47:50 +00005803 if (options & HTML_PARSE_NOWARNING) {
5804 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005805 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005806 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005807 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00005808 }
5809 if (options & HTML_PARSE_NOERROR) {
5810 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005811 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005812 ctxt->sax->fatalError = NULL;
5813 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005814 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00005815 }
5816 if (options & HTML_PARSE_PEDANTIC) {
5817 ctxt->pedantic = 1;
5818 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005819 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00005820 } else
5821 ctxt->pedantic = 0;
5822 if (options & XML_PARSE_NOBLANKS) {
5823 ctxt->keepBlanks = 0;
5824 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
5825 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005826 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00005827 } else
5828 ctxt->keepBlanks = 1;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00005829 if (options & HTML_PARSE_RECOVER) {
5830 ctxt->recovery = 1;
5831 } else
5832 ctxt->recovery = 0;
Daniel Veillard8874b942005-08-25 13:19:21 +00005833 if (options & HTML_PARSE_COMPACT) {
5834 ctxt->options |= HTML_PARSE_COMPACT;
5835 options -= HTML_PARSE_COMPACT;
5836 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005837 ctxt->dictNames = 0;
5838 return (options);
5839}
5840
5841/**
5842 * htmlDoRead:
5843 * @ctxt: an HTML parser context
5844 * @URL: the base URL to use for the document
5845 * @encoding: the document encoding, or NULL
5846 * @options: a combination of htmlParserOption(s)
5847 * @reuse: keep the context for reuse
5848 *
5849 * Common front-end for the htmlRead functions
5850 *
5851 * Returns the resulting document tree or NULL
5852 */
5853static htmlDocPtr
5854htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
5855 int options, int reuse)
5856{
5857 htmlDocPtr ret;
5858
5859 htmlCtxtUseOptions(ctxt, options);
5860 ctxt->html = 1;
5861 if (encoding != NULL) {
5862 xmlCharEncodingHandlerPtr hdlr;
5863
5864 hdlr = xmlFindCharEncodingHandler(encoding);
5865 if (hdlr != NULL)
5866 xmlSwitchToEncoding(ctxt, hdlr);
5867 }
5868 if ((URL != NULL) && (ctxt->input != NULL) &&
5869 (ctxt->input->filename == NULL))
5870 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
5871 htmlParseDocument(ctxt);
5872 ret = ctxt->myDoc;
5873 ctxt->myDoc = NULL;
5874 if (!reuse) {
5875 if ((ctxt->dictNames) &&
5876 (ret != NULL) &&
5877 (ret->dict == ctxt->dict))
5878 ctxt->dict = NULL;
5879 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00005880 }
5881 return (ret);
5882}
5883
5884/**
5885 * htmlReadDoc:
5886 * @cur: a pointer to a zero terminated string
5887 * @URL: the base URL to use for the document
5888 * @encoding: the document encoding, or NULL
5889 * @options: a combination of htmlParserOption(s)
5890 *
5891 * parse an XML in-memory document and build a tree.
5892 *
5893 * Returns the resulting document tree
5894 */
5895htmlDocPtr
5896htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
5897{
5898 htmlParserCtxtPtr ctxt;
5899
5900 if (cur == NULL)
5901 return (NULL);
5902
5903 ctxt = xmlCreateDocParserCtxt(cur);
5904 if (ctxt == NULL)
5905 return (NULL);
5906 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5907}
5908
5909/**
5910 * htmlReadFile:
5911 * @filename: a file or URL
5912 * @encoding: the document encoding, or NULL
5913 * @options: a combination of htmlParserOption(s)
5914 *
5915 * parse an XML file from the filesystem or the network.
5916 *
5917 * Returns the resulting document tree
5918 */
5919htmlDocPtr
5920htmlReadFile(const char *filename, const char *encoding, int options)
5921{
5922 htmlParserCtxtPtr ctxt;
5923
5924 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5925 if (ctxt == NULL)
5926 return (NULL);
5927 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
5928}
5929
5930/**
5931 * htmlReadMemory:
5932 * @buffer: a pointer to a char array
5933 * @size: the size of the array
5934 * @URL: the base URL to use for the document
5935 * @encoding: the document encoding, or NULL
5936 * @options: a combination of htmlParserOption(s)
5937 *
5938 * parse an XML in-memory document and build a tree.
5939 *
5940 * Returns the resulting document tree
5941 */
5942htmlDocPtr
5943htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
5944{
5945 htmlParserCtxtPtr ctxt;
5946
5947 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
5948 if (ctxt == NULL)
5949 return (NULL);
William M. Brackd43cdcd2004-08-03 15:13:29 +00005950 if (ctxt->sax != NULL)
5951 memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Daniel Veillard9475a352003-09-26 12:47:50 +00005952 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5953}
5954
5955/**
5956 * htmlReadFd:
5957 * @fd: an open file descriptor
5958 * @URL: the base URL to use for the document
5959 * @encoding: the document encoding, or NULL
5960 * @options: a combination of htmlParserOption(s)
5961 *
5962 * parse an XML from a file descriptor and build a tree.
5963 *
5964 * Returns the resulting document tree
5965 */
5966htmlDocPtr
5967htmlReadFd(int fd, const char *URL, const char *encoding, int options)
5968{
5969 htmlParserCtxtPtr ctxt;
5970 xmlParserInputBufferPtr input;
5971 xmlParserInputPtr stream;
5972
5973 if (fd < 0)
5974 return (NULL);
5975
5976 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5977 if (input == NULL)
5978 return (NULL);
5979 ctxt = xmlNewParserCtxt();
5980 if (ctxt == NULL) {
5981 xmlFreeParserInputBuffer(input);
5982 return (NULL);
5983 }
5984 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
5985 if (stream == NULL) {
5986 xmlFreeParserInputBuffer(input);
5987 xmlFreeParserCtxt(ctxt);
5988 return (NULL);
5989 }
5990 inputPush(ctxt, stream);
5991 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5992}
5993
5994/**
5995 * htmlReadIO:
5996 * @ioread: an I/O read function
5997 * @ioclose: an I/O close function
5998 * @ioctx: an I/O handler
5999 * @URL: the base URL to use for the document
6000 * @encoding: the document encoding, or NULL
6001 * @options: a combination of htmlParserOption(s)
6002 *
6003 * parse an HTML document from I/O functions and source and build a tree.
6004 *
6005 * Returns the resulting document tree
6006 */
6007htmlDocPtr
6008htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
6009 void *ioctx, const char *URL, const char *encoding, int options)
6010{
6011 htmlParserCtxtPtr ctxt;
6012 xmlParserInputBufferPtr input;
6013 xmlParserInputPtr stream;
6014
6015 if (ioread == NULL)
6016 return (NULL);
6017
6018 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6019 XML_CHAR_ENCODING_NONE);
6020 if (input == NULL)
6021 return (NULL);
6022 ctxt = xmlNewParserCtxt();
6023 if (ctxt == NULL) {
6024 xmlFreeParserInputBuffer(input);
6025 return (NULL);
6026 }
6027 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6028 if (stream == NULL) {
6029 xmlFreeParserInputBuffer(input);
6030 xmlFreeParserCtxt(ctxt);
6031 return (NULL);
6032 }
6033 inputPush(ctxt, stream);
6034 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6035}
6036
6037/**
6038 * htmlCtxtReadDoc:
6039 * @ctxt: an HTML parser context
6040 * @cur: a pointer to a zero terminated string
6041 * @URL: the base URL to use for the document
6042 * @encoding: the document encoding, or NULL
6043 * @options: a combination of htmlParserOption(s)
6044 *
6045 * parse an XML in-memory document and build a tree.
6046 * This reuses the existing @ctxt parser context
6047 *
6048 * Returns the resulting document tree
6049 */
6050htmlDocPtr
6051htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
6052 const char *URL, const char *encoding, int options)
6053{
6054 xmlParserInputPtr stream;
6055
6056 if (cur == NULL)
6057 return (NULL);
6058 if (ctxt == NULL)
6059 return (NULL);
6060
6061 htmlCtxtReset(ctxt);
6062
6063 stream = xmlNewStringInputStream(ctxt, cur);
6064 if (stream == NULL) {
6065 return (NULL);
6066 }
6067 inputPush(ctxt, stream);
6068 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6069}
6070
6071/**
6072 * htmlCtxtReadFile:
6073 * @ctxt: an HTML parser context
6074 * @filename: a file or URL
6075 * @encoding: the document encoding, or NULL
6076 * @options: a combination of htmlParserOption(s)
6077 *
6078 * parse an XML file from the filesystem or the network.
6079 * This reuses the existing @ctxt parser context
6080 *
6081 * Returns the resulting document tree
6082 */
6083htmlDocPtr
6084htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
6085 const char *encoding, int options)
6086{
6087 xmlParserInputPtr stream;
6088
6089 if (filename == NULL)
6090 return (NULL);
6091 if (ctxt == NULL)
6092 return (NULL);
6093
6094 htmlCtxtReset(ctxt);
6095
Daniel Veillard29614c72004-11-26 10:47:26 +00006096 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006097 if (stream == NULL) {
6098 return (NULL);
6099 }
6100 inputPush(ctxt, stream);
6101 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
6102}
6103
6104/**
6105 * htmlCtxtReadMemory:
6106 * @ctxt: an HTML parser context
6107 * @buffer: a pointer to a char array
6108 * @size: the size of the array
6109 * @URL: the base URL to use for the document
6110 * @encoding: the document encoding, or NULL
6111 * @options: a combination of htmlParserOption(s)
6112 *
6113 * parse an XML in-memory document and build a tree.
6114 * This reuses the existing @ctxt parser context
6115 *
6116 * Returns the resulting document tree
6117 */
6118htmlDocPtr
6119htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
6120 const char *URL, const char *encoding, int options)
6121{
6122 xmlParserInputBufferPtr input;
6123 xmlParserInputPtr stream;
6124
6125 if (ctxt == NULL)
6126 return (NULL);
6127 if (buffer == NULL)
6128 return (NULL);
6129
6130 htmlCtxtReset(ctxt);
6131
6132 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
6133 if (input == NULL) {
6134 return(NULL);
6135 }
6136
6137 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6138 if (stream == NULL) {
6139 xmlFreeParserInputBuffer(input);
6140 return(NULL);
6141 }
6142
6143 inputPush(ctxt, stream);
6144 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6145}
6146
6147/**
6148 * htmlCtxtReadFd:
6149 * @ctxt: an HTML parser context
6150 * @fd: an open file descriptor
6151 * @URL: the base URL to use for the document
6152 * @encoding: the document encoding, or NULL
6153 * @options: a combination of htmlParserOption(s)
6154 *
6155 * parse an XML from a file descriptor and build a tree.
6156 * This reuses the existing @ctxt parser context
6157 *
6158 * Returns the resulting document tree
6159 */
6160htmlDocPtr
6161htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
6162 const char *URL, const char *encoding, int options)
6163{
6164 xmlParserInputBufferPtr input;
6165 xmlParserInputPtr stream;
6166
6167 if (fd < 0)
6168 return (NULL);
6169 if (ctxt == NULL)
6170 return (NULL);
6171
6172 htmlCtxtReset(ctxt);
6173
6174
6175 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6176 if (input == NULL)
6177 return (NULL);
6178 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6179 if (stream == NULL) {
6180 xmlFreeParserInputBuffer(input);
6181 return (NULL);
6182 }
6183 inputPush(ctxt, stream);
6184 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6185}
6186
6187/**
6188 * htmlCtxtReadIO:
6189 * @ctxt: an HTML parser context
6190 * @ioread: an I/O read function
6191 * @ioclose: an I/O close function
6192 * @ioctx: an I/O handler
6193 * @URL: the base URL to use for the document
6194 * @encoding: the document encoding, or NULL
6195 * @options: a combination of htmlParserOption(s)
6196 *
6197 * parse an HTML document from I/O functions and source and build a tree.
6198 * This reuses the existing @ctxt parser context
6199 *
6200 * Returns the resulting document tree
6201 */
6202htmlDocPtr
6203htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
6204 xmlInputCloseCallback ioclose, void *ioctx,
6205 const char *URL,
6206 const char *encoding, int options)
6207{
6208 xmlParserInputBufferPtr input;
6209 xmlParserInputPtr stream;
6210
6211 if (ioread == NULL)
6212 return (NULL);
6213 if (ctxt == NULL)
6214 return (NULL);
6215
6216 htmlCtxtReset(ctxt);
6217
6218 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6219 XML_CHAR_ENCODING_NONE);
6220 if (input == NULL)
6221 return (NULL);
6222 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6223 if (stream == NULL) {
6224 xmlFreeParserInputBuffer(input);
6225 return (NULL);
6226 }
6227 inputPush(ctxt, stream);
6228 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6229}
6230
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006231#define bottom_HTMLparser
6232#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00006233#endif /* LIBXML_HTML_ENABLED */