blob: 693b4d23a4237f3d8f8aada273dbc6f229f2a000 [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
Daniel Veillard065abe82006-07-03 08:55:04 +0000496static const char* const html_flow[] = { FLOW, NULL } ;
497static const char* const html_inline[] = { INLINE, NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000498
499/* placeholders: elts with content but no subelements */
Daniel Veillard065abe82006-07-03 08:55:04 +0000500static const char* const html_pcdata[] = { NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000501#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
Daniel Veillard065abe82006-07-03 08:55:04 +0000519static const char* const html_attrs[] = { ATTRS, NULL } ;
520static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
521static const char* const core_attrs[] = { COREATTRS, NULL } ;
522static const char* const i18n_attrs[] = { I18N, NULL } ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000523
524
525/* Other declarations that should go inline ... */
Daniel Veillard065abe82006-07-03 08:55:04 +0000526static const char* const a_attrs[] = { ATTRS, "charset", "type", "name",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000527 "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
528 "tabindex", "onfocus", "onblur", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000529static const char* const target_attr[] = { "target", NULL } ;
530static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ;
531static const char* const alt_attr[] = { "alt", NULL } ;
532static const char* const src_alt_attrs[] = { "src", "alt", NULL } ;
533static const char* const href_attrs[] = { "href", NULL } ;
534static const char* const clear_attrs[] = { "clear", NULL } ;
535static const char* const inline_p[] = { INLINE, "p", NULL } ;
536
537static const char* const flow_param[] = { FLOW, "param", NULL } ;
538static const char* const applet_attrs[] = { COREATTRS , "codebase",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000539 "archive", "alt", "name", "height", "width", "align",
540 "hspace", "vspace", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000541static const char* const area_attrs[] = { "shape", "coords", "href", "nohref",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000542 "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000543static const char* const basefont_attrs[] =
Daniel Veillard930dfb62003-02-05 10:17:38 +0000544 { "id", "size", "color", "face", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000545static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ;
546static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ;
547static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
548static const char* const body_depr[] = { "background", "bgcolor", "text",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000549 "link", "vlink", "alink", NULL } ;
Daniel Veillard065abe82006-07-03 08:55:04 +0000550static const char* const button_attrs[] = { ATTRS, "name", "value", "type",
Daniel Veillard930dfb62003-02-05 10:17:38 +0000551 "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
552
553
Daniel Veillard065abe82006-07-03 08:55:04 +0000554static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
555static const char* const col_elt[] = { "col", NULL } ;
556static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
557static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ;
558static const char* const dl_contents[] = { "dt", "dd", NULL } ;
559static const char* const compact_attr[] = { "compact", NULL } ;
560static const char* const label_attr[] = { "label", NULL } ;
561static const char* const fieldset_contents[] = { FLOW, "legend" } ;
562static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
563static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
564static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
565static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
566static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
567static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
568static const char* const head_attrs[] = { I18N, "profile", NULL } ;
569static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
570static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
571static const char* const version_attr[] = { "version", NULL } ;
572static const char* const html_content[] = { "head", "body", "frameset", NULL } ;
573static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
574static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
575static const char* const input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ;
576static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
577static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
578static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ;
579static const char* const align_attr[] = { "align", NULL } ;
580static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
581static const char* const map_contents[] = { BLOCK, "area", NULL } ;
582static const char* const name_attr[] = { "name", NULL } ;
583static const char* const action_attr[] = { "action", NULL } ;
584static const char* const blockli_elt[] = { BLOCK, "li", NULL } ;
585static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ;
586static const char* const content_attr[] = { "content", NULL } ;
587static const char* const type_attr[] = { "type", NULL } ;
588static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
589static const char* const object_contents[] = { FLOW, "param", NULL } ;
590static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
591static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
592static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ;
593static const char* const option_elt[] = { "option", NULL } ;
594static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
595static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
596static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
597static const char* const width_attr[] = { "width", NULL } ;
598static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
599static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
600static const char* const language_attr[] = { "language", NULL } ;
601static const char* const select_content[] = { "optgroup", "option", NULL } ;
602static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
603static const char* const style_attrs[] = { I18N, "media", "title", NULL } ;
604static const char* const table_attrs[] = { ATTRS "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
605static const char* const table_depr[] = { "align", "bgcolor", NULL } ;
606static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
607static const char* const tr_elt[] = { "tr", NULL } ;
608static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
609static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
610static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
611static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
612static const char* const tr_contents[] = { "th", "td", NULL } ;
613static const char* const bgcolor_attr[] = { "bgcolor", NULL } ;
614static const char* const li_elt[] = { "li", NULL } ;
615static const char* const ul_depr[] = { "type", "compact", NULL} ;
616static const char* const dir_attr[] = { "dir", NULL} ;
Daniel Veillard930dfb62003-02-05 10:17:38 +0000617
618#define DECL (const char**)
619
Daniel Veillard22090732001-07-16 00:06:07 +0000620static const htmlElemDesc
621html40ElementTable[] = {
Daniel Veillard930dfb62003-02-05 10:17:38 +0000622{ "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
623 DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
624},
625{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
626 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
627},
628{ "acronym", 0, 0, 0, 0, 0, 0, 1, "",
629 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
630},
631{ "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
632 DECL inline_p , NULL , DECL html_attrs, NULL, NULL
633},
634{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
635 DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
636},
637{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
638 EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
639},
640{ "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
641 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
642},
643{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
644 EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
645},
646{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
647 EMPTY , NULL , NULL, DECL basefont_attrs, NULL
648},
649{ "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
650 DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
651},
652{ "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
653 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
654},
655{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
656 DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
657},
658{ "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
659 DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
660},
661{ "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
662 EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
663},
664{ "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
665 DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
666},
667{ "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
668 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
669},
670{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
671 DECL html_flow , NULL , NULL, DECL html_attrs, NULL
672},
673{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
674 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
675},
676{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
677 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
678},
679{ "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
680 EMPTY , NULL , DECL col_attrs , NULL, NULL
681},
682{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
683 DECL col_elt , "col" , DECL col_attrs , NULL, NULL
684},
685{ "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
686 DECL html_flow , NULL , DECL html_attrs, NULL, NULL
687},
688{ "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
689 DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
690},
691{ "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
692 DECL html_inline , NULL , DECL html_attrs, NULL, NULL
693},
694{ "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
695 DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
696},
697{ "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
698 DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
699},
700{ "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
William M. Bracke978ae22007-03-21 06:16:02 +0000701 DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL
Daniel Veillard930dfb62003-02-05 10:17:38 +0000702},
703{ "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
704 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
705},
706{ "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
707 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
708},
709{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
710 DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
711},
712{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
713 DECL html_inline, NULL, NULL, DECL font_attrs, NULL
714},
715{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
716 DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
717},
718{ "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
719 EMPTY, NULL, NULL, DECL frame_attrs, NULL
720},
721{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
722 DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
723},
724{ "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
725 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
726},
727{ "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
728 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
729},
730{ "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
731 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
732},
733{ "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
734 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
735},
736{ "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
737 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
738},
739{ "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
740 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
741},
742{ "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
743 DECL head_contents, NULL, DECL head_attrs, NULL, NULL
744},
745{ "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
746 EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
747},
748{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
749 DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
750},
751{ "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
752 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
753},
754{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
755 DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
756},
757{ "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
William M. Bracke978ae22007-03-21 06:16:02 +0000758 EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs
Daniel Veillard930dfb62003-02-05 10:17:38 +0000759},
760{ "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
761 EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
762},
763{ "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
764 DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
765},
766{ "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
767 EMPTY, NULL, NULL, DECL prompt_attrs, NULL
768},
769{ "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
770 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
771},
772{ "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
773 DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
774},
775{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
776 DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
777},
778{ "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
779 DECL html_flow, NULL, DECL html_attrs, NULL, NULL
780},
781{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
782 EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
783},
784{ "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
William M. Bracke978ae22007-03-21 06:16:02 +0000785 DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000786},
787{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
788 DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
789},
790{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
791 EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
792},
793{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
794 DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
795},
796{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
797 DECL html_flow, "div", DECL html_attrs, NULL, NULL
798},
799{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
800 DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
801},
802{ "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
803 DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
804},
805{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
William M. Bracke978ae22007-03-21 06:16:02 +0000806 DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000807},
808{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
809 DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
810},
811{ "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
812 DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
813},
814{ "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
William M. Bracke978ae22007-03-21 06:16:02 +0000815 EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr
Daniel Veillard930dfb62003-02-05 10:17:38 +0000816},
817{ "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
818 DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
819},
820{ "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
821 DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
822},
823{ "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
824 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
825},
826{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
827 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
828},
829{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
830 DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
831},
832{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
833 DECL select_content, NULL, DECL select_attrs, NULL, NULL
834},
835{ "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
836 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
837},
838{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
839 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
840},
841{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
842 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
843},
844{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
845 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
846},
847{ "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
848 DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
849},
850{ "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
851 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
852},
853{ "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
854 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
855},
856{ "table", 0, 0, 0, 0, 0, 0, 0, "",
857 DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
858},
859{ "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
860 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
861},
862{ "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
863 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
864},
865{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
866 DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
867},
868{ "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
869 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
870},
871{ "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
872 DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
873},
874{ "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
875 DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
876},
877{ "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
878 DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
879},
880{ "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
881 DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
882},
883{ "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
884 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
885},
886{ "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
887 DECL html_inline, NULL, NULL, DECL html_attrs, NULL
888},
889{ "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
890 DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
891},
892{ "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
893 DECL html_inline, NULL, DECL html_attrs, NULL, NULL
894}
Owen Taylor3473f882001-02-23 17:55:21 +0000895};
896
897/*
Owen Taylor3473f882001-02-23 17:55:21 +0000898 * start tags that imply the end of current element
899 */
Daniel Veillard065abe82006-07-03 08:55:04 +0000900static const char * const htmlStartClose[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000901"form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
902 "dl", "ul", "ol", "menu", "dir", "address", "pre",
903 "listing", "xmp", "head", NULL,
904"head", "p", NULL,
905"title", "p", NULL,
906"body", "head", "style", "link", "title", "p", NULL,
Daniel Veillard25d5d9a2004-04-05 07:08:42 +0000907"frameset", "head", "style", "link", "title", "p", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000908"li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
909 "pre", "listing", "xmp", "head", "li", NULL,
910"hr", "p", "head", NULL,
911"h1", "p", "head", NULL,
912"h2", "p", "head", NULL,
913"h3", "p", "head", NULL,
914"h4", "p", "head", NULL,
915"h5", "p", "head", NULL,
916"h6", "p", "head", NULL,
917"dir", "p", "head", NULL,
918"address", "p", "head", "ul", NULL,
919"pre", "p", "head", "ul", NULL,
920"listing", "p", "head", NULL,
921"xmp", "p", "head", NULL,
922"blockquote", "p", "head", NULL,
923"dl", "p", "dt", "menu", "dir", "address", "pre", "listing",
924 "xmp", "head", NULL,
925"dt", "p", "menu", "dir", "address", "pre", "listing", "xmp",
926 "head", "dd", NULL,
927"dd", "p", "menu", "dir", "address", "pre", "listing", "xmp",
928 "head", "dt", NULL,
929"ul", "p", "head", "ol", "menu", "dir", "address", "pre",
930 "listing", "xmp", NULL,
931"ol", "p", "head", "ul", NULL,
932"menu", "p", "head", "ul", NULL,
933"p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
934"div", "p", "head", NULL,
935"noscript", "p", "head", NULL,
936"center", "font", "b", "i", "p", "head", NULL,
937"a", "a", NULL,
938"caption", "p", NULL,
939"colgroup", "caption", "colgroup", "col", "p", NULL,
940"col", "caption", "col", "p", NULL,
941"table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
942 "listing", "xmp", "a", NULL,
Daniel Veillard43dadeb2001-04-24 11:23:35 +0000943"th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
944"td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL,
Owen Taylor3473f882001-02-23 17:55:21 +0000945"tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
946"thead", "caption", "col", "colgroup", NULL,
947"tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead",
948 "tbody", "p", NULL,
949"tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead",
950 "tfoot", "tbody", "p", NULL,
951"optgroup", "option", NULL,
952"option", "option", NULL,
953"fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
954 "pre", "listing", "xmp", "a", NULL,
955NULL
956};
957
958/*
959 * The list of HTML elements which are supposed not to have
960 * CDATA content and where a p element will be implied
961 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000962 * TODO: extend that list by reading the HTML SGML DTD on
Owen Taylor3473f882001-02-23 17:55:21 +0000963 * implied paragraph
964 */
Daniel Veillard065abe82006-07-03 08:55:04 +0000965static const char *const htmlNoContentElements[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000966 "html",
967 "head",
Owen Taylor3473f882001-02-23 17:55:21 +0000968 NULL
969};
970
971/*
972 * The list of HTML attributes which are of content %Script;
973 * NOTE: when adding ones, check htmlIsScriptAttribute() since
974 * it assumes the name starts with 'on'
975 */
Daniel Veillard065abe82006-07-03 08:55:04 +0000976static const char *const htmlScriptAttributes[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000977 "onclick",
978 "ondblclick",
979 "onmousedown",
980 "onmouseup",
981 "onmouseover",
982 "onmousemove",
983 "onmouseout",
984 "onkeypress",
985 "onkeydown",
986 "onkeyup",
987 "onload",
988 "onunload",
989 "onfocus",
990 "onblur",
991 "onsubmit",
992 "onrest",
993 "onchange",
994 "onselect"
995};
996
Daniel Veillarda2bc3682001-05-03 08:27:20 +0000997/*
Daniel Veillard0a2a1632001-05-11 14:18:03 +0000998 * This table is used by the htmlparser to know what to do with
999 * broken html pages. By assigning different priorities to different
1000 * elements the parser can decide how to handle extra endtags.
1001 * Endtags are only allowed to close elements with lower or equal
1002 * priority.
1003 */
Daniel Veillarda2bc3682001-05-03 08:27:20 +00001004
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001005typedef struct {
1006 const char *name;
1007 int priority;
1008} elementPriority;
1009
Daniel Veillard22090732001-07-16 00:06:07 +00001010static const elementPriority htmlEndPriority[] = {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001011 {"div", 150},
1012 {"td", 160},
1013 {"th", 160},
1014 {"tr", 170},
1015 {"thead", 180},
1016 {"tbody", 180},
1017 {"tfoot", 180},
1018 {"table", 190},
1019 {"head", 200},
1020 {"body", 200},
1021 {"html", 220},
1022 {NULL, 100} /* Default priority */
1023};
Owen Taylor3473f882001-02-23 17:55:21 +00001024
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001025static const char** htmlStartCloseIndex[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001026static int htmlStartCloseIndexinitialized = 0;
1027
1028/************************************************************************
1029 * *
1030 * functions to handle HTML specific data *
1031 * *
1032 ************************************************************************/
1033
1034/**
1035 * htmlInitAutoClose:
1036 *
1037 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1038 * This is not reentrant. Call xmlInitParser() once before processing in
1039 * case of use in multithreaded programs.
1040 */
1041void
1042htmlInitAutoClose(void) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001043 int indx, i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001044
1045 if (htmlStartCloseIndexinitialized) return;
1046
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001047 for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL;
1048 indx = 0;
1049 while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) {
Daniel Veillard28aac0b2006-10-16 08:31:18 +00001050 htmlStartCloseIndex[indx++] = (const char**) &htmlStartClose[i];
Owen Taylor3473f882001-02-23 17:55:21 +00001051 while (htmlStartClose[i] != NULL) i++;
1052 i++;
1053 }
1054 htmlStartCloseIndexinitialized = 1;
1055}
1056
1057/**
1058 * htmlTagLookup:
1059 * @tag: The tag name in lowercase
1060 *
1061 * Lookup the HTML tag in the ElementTable
1062 *
1063 * Returns the related htmlElemDescPtr or NULL if not found.
1064 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001065const htmlElemDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001066htmlTagLookup(const xmlChar *tag) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001067 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001068
1069 for (i = 0; i < (sizeof(html40ElementTable) /
1070 sizeof(html40ElementTable[0]));i++) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +00001071 if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name))
William M. Brack78637da2003-07-31 14:47:38 +00001072 return((htmlElemDescPtr) &html40ElementTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001073 }
1074 return(NULL);
1075}
1076
1077/**
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001078 * htmlGetEndPriority:
1079 * @name: The name of the element to look up the priority for.
1080 *
1081 * Return value: The "endtag" priority.
1082 **/
1083static int
1084htmlGetEndPriority (const xmlChar *name) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001085 int i = 0;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001086
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001087 while ((htmlEndPriority[i].name != NULL) &&
1088 (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
1089 i++;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001090
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001091 return(htmlEndPriority[i].priority);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001092}
1093
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001094
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001095/**
Owen Taylor3473f882001-02-23 17:55:21 +00001096 * htmlCheckAutoClose:
1097 * @newtag: The new tag name
1098 * @oldtag: The old tag name
1099 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001100 * Checks whether the new tag is one of the registered valid tags for
1101 * closing old.
Owen Taylor3473f882001-02-23 17:55:21 +00001102 * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
1103 *
1104 * Returns 0 if no, 1 if yes.
1105 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001106static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001107htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
1108{
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001109 int i, indx;
1110 const char **closed = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001111
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001112 if (htmlStartCloseIndexinitialized == 0)
1113 htmlInitAutoClose();
Owen Taylor3473f882001-02-23 17:55:21 +00001114
1115 /* inefficient, but not a big deal */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001116 for (indx = 0; indx < 100; indx++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001117 closed = htmlStartCloseIndex[indx];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001118 if (closed == NULL)
1119 return (0);
1120 if (xmlStrEqual(BAD_CAST * closed, newtag))
1121 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001122 }
1123
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001124 i = closed - htmlStartClose;
Owen Taylor3473f882001-02-23 17:55:21 +00001125 i++;
1126 while (htmlStartClose[i] != NULL) {
1127 if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001128 return (1);
1129 }
1130 i++;
Owen Taylor3473f882001-02-23 17:55:21 +00001131 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001132 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001133}
1134
1135/**
1136 * htmlAutoCloseOnClose:
1137 * @ctxt: an HTML parser context
1138 * @newtag: The new tag name
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001139 * @force: force the tag closure
Owen Taylor3473f882001-02-23 17:55:21 +00001140 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001141 * The HTML DTD allows an ending tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001142 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001143static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001144htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1145{
1146 const htmlElemDesc *info;
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001147 int i, priority;
Owen Taylor3473f882001-02-23 17:55:21 +00001148
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001149 priority = htmlGetEndPriority(newtag);
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001150
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001151 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard0a2a1632001-05-11 14:18:03 +00001152
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001153 if (xmlStrEqual(newtag, ctxt->nameTab[i]))
1154 break;
1155 /*
1156 * A missplaced endtag can only close elements with lower
1157 * or equal priority, so if we find an element with higher
1158 * priority before we find an element with
1159 * matching name, we just ignore this endtag
1160 */
1161 if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
1162 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001163 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001164 if (i < 0)
1165 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001166
1167 while (!xmlStrEqual(newtag, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001168 info = htmlTagLookup(ctxt->name);
Daniel Veillardf403d292003-10-05 13:51:35 +00001169 if ((info != NULL) && (info->endTag == 3)) {
1170 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
1171 "Opening and ending tag mismatch: %s and %s\n",
Daniel Veillard05bcb7e2003-10-19 14:26:34 +00001172 newtag, ctxt->name);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001173 }
1174 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1175 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001176 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001177 }
1178}
1179
1180/**
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001181 * htmlAutoCloseOnEnd:
1182 * @ctxt: an HTML parser context
1183 *
1184 * Close all remaining tags at the end of the stream
1185 */
1186static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001187htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
1188{
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001189 int i;
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001190
William M. Brack899e64a2003-09-26 18:03:42 +00001191 if (ctxt->nameNr == 0)
1192 return;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001193 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001194 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1195 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001196 htmlnamePop(ctxt);
Daniel Veillarda3bfca52001-04-12 15:42:58 +00001197 }
1198}
1199
1200/**
Owen Taylor3473f882001-02-23 17:55:21 +00001201 * htmlAutoClose:
1202 * @ctxt: an HTML parser context
1203 * @newtag: The new tag name or NULL
1204 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001205 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001206 * The list is kept in htmlStartClose array. This function is
1207 * called when a new tag has been detected and generates the
1208 * appropriates closes if possible/needed.
1209 * If newtag is NULL this mean we are at the end of the resource
1210 * and we should check
1211 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001212static void
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001213htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1214{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001215 while ((newtag != NULL) && (ctxt->name != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001216 (htmlCheckAutoClose(newtag, ctxt->name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001217 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1218 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001219 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001220 }
1221 if (newtag == NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001222 htmlAutoCloseOnEnd(ctxt);
1223 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001224 }
1225 while ((newtag == NULL) && (ctxt->name != NULL) &&
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001226 ((xmlStrEqual(ctxt->name, BAD_CAST "head")) ||
1227 (xmlStrEqual(ctxt->name, BAD_CAST "body")) ||
1228 (xmlStrEqual(ctxt->name, BAD_CAST "html")))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001229 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1230 ctxt->sax->endElement(ctxt->userData, ctxt->name);
William M. Brack899e64a2003-09-26 18:03:42 +00001231 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001232 }
Owen Taylor3473f882001-02-23 17:55:21 +00001233}
1234
1235/**
1236 * htmlAutoCloseTag:
1237 * @doc: the HTML document
1238 * @name: The tag name
1239 * @elem: the HTML element
1240 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001241 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001242 * The list is kept in htmlStartClose array. This function checks
1243 * if the element or one of it's children would autoclose the
1244 * given tag.
1245 *
1246 * Returns 1 if autoclose, 0 otherwise
1247 */
1248int
1249htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
1250 htmlNodePtr child;
1251
1252 if (elem == NULL) return(1);
1253 if (xmlStrEqual(name, elem->name)) return(0);
1254 if (htmlCheckAutoClose(elem->name, name)) return(1);
1255 child = elem->children;
1256 while (child != NULL) {
1257 if (htmlAutoCloseTag(doc, name, child)) return(1);
1258 child = child->next;
1259 }
1260 return(0);
1261}
1262
1263/**
1264 * htmlIsAutoClosed:
1265 * @doc: the HTML document
1266 * @elem: the HTML element
1267 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001268 * The HTML DTD allows a tag to implicitly close other tags.
Owen Taylor3473f882001-02-23 17:55:21 +00001269 * The list is kept in htmlStartClose array. This function checks
1270 * if a tag is autoclosed by one of it's child
1271 *
1272 * Returns 1 if autoclosed, 0 otherwise
1273 */
1274int
1275htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
1276 htmlNodePtr child;
1277
1278 if (elem == NULL) return(1);
1279 child = elem->children;
1280 while (child != NULL) {
1281 if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
1282 child = child->next;
1283 }
1284 return(0);
1285}
1286
1287/**
1288 * htmlCheckImplied:
1289 * @ctxt: an HTML parser context
1290 * @newtag: The new tag name
1291 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001292 * The HTML DTD allows a tag to exists only implicitly
Owen Taylor3473f882001-02-23 17:55:21 +00001293 * called when a new tag has been detected and generates the
1294 * appropriates implicit tags if missing
1295 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001296static void
Owen Taylor3473f882001-02-23 17:55:21 +00001297htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1298 if (!htmlOmittedDefaultValue)
1299 return;
1300 if (xmlStrEqual(newtag, BAD_CAST"html"))
1301 return;
1302 if (ctxt->nameNr <= 0) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001303 htmlnamePush(ctxt, BAD_CAST"html");
Owen Taylor3473f882001-02-23 17:55:21 +00001304 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1305 ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
1306 }
1307 if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1308 return;
1309 if ((ctxt->nameNr <= 1) &&
1310 ((xmlStrEqual(newtag, BAD_CAST"script")) ||
1311 (xmlStrEqual(newtag, BAD_CAST"style")) ||
1312 (xmlStrEqual(newtag, BAD_CAST"meta")) ||
1313 (xmlStrEqual(newtag, BAD_CAST"link")) ||
1314 (xmlStrEqual(newtag, BAD_CAST"title")) ||
1315 (xmlStrEqual(newtag, BAD_CAST"base")))) {
1316 /*
1317 * dropped OBJECT ... i you put it first BODY will be
1318 * assumed !
1319 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001320 htmlnamePush(ctxt, BAD_CAST"head");
Owen Taylor3473f882001-02-23 17:55:21 +00001321 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1322 ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
1323 } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
1324 (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
1325 (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
1326 int i;
1327 for (i = 0;i < ctxt->nameNr;i++) {
1328 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
1329 return;
1330 }
1331 if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
1332 return;
1333 }
1334 }
1335
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001336 htmlnamePush(ctxt, BAD_CAST"body");
Owen Taylor3473f882001-02-23 17:55:21 +00001337 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1338 ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
1339 }
1340}
1341
1342/**
1343 * htmlCheckParagraph
1344 * @ctxt: an HTML parser context
1345 *
1346 * Check whether a p element need to be implied before inserting
1347 * characters in the current element.
1348 *
1349 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1350 * in case of error.
1351 */
1352
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001353static int
Owen Taylor3473f882001-02-23 17:55:21 +00001354htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
1355 const xmlChar *tag;
1356 int i;
1357
1358 if (ctxt == NULL)
1359 return(-1);
1360 tag = ctxt->name;
1361 if (tag == NULL) {
1362 htmlAutoClose(ctxt, BAD_CAST"p");
1363 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001364 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001365 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1366 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1367 return(1);
1368 }
1369 if (!htmlOmittedDefaultValue)
1370 return(0);
1371 for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1372 if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
Owen Taylor3473f882001-02-23 17:55:21 +00001373 htmlAutoClose(ctxt, BAD_CAST"p");
1374 htmlCheckImplied(ctxt, BAD_CAST"p");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001375 htmlnamePush(ctxt, BAD_CAST"p");
Owen Taylor3473f882001-02-23 17:55:21 +00001376 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1377 ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1378 return(1);
1379 }
1380 }
1381 return(0);
1382}
1383
1384/**
1385 * htmlIsScriptAttribute:
1386 * @name: an attribute name
1387 *
1388 * Check if an attribute is of content type Script
1389 *
1390 * Returns 1 is the attribute is a script 0 otherwise
1391 */
1392int
1393htmlIsScriptAttribute(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001394 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001395
1396 if (name == NULL)
1397 return(0);
1398 /*
1399 * all script attributes start with 'on'
1400 */
1401 if ((name[0] != 'o') || (name[1] != 'n'))
1402 return(0);
1403 for (i = 0;
1404 i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1405 i++) {
1406 if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1407 return(1);
1408 }
1409 return(0);
1410}
1411
1412/************************************************************************
1413 * *
1414 * The list of HTML predefined entities *
1415 * *
1416 ************************************************************************/
1417
1418
Daniel Veillard22090732001-07-16 00:06:07 +00001419static const htmlEntityDesc html40EntitiesTable[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001420/*
1421 * the 4 absolute ones, plus apostrophe.
1422 */
1423{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1424{ 38, "amp", "ampersand, U+0026 ISOnum" },
1425{ 39, "apos", "single quote" },
1426{ 60, "lt", "less-than sign, U+003C ISOnum" },
1427{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1428
1429/*
1430 * A bunch still in the 128-255 range
1431 * Replacing them depend really on the charset used.
1432 */
1433{ 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1434{ 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1435{ 162, "cent", "cent sign, U+00A2 ISOnum" },
1436{ 163, "pound","pound sign, U+00A3 ISOnum" },
1437{ 164, "curren","currency sign, U+00A4 ISOnum" },
1438{ 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
1439{ 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1440{ 167, "sect", "section sign, U+00A7 ISOnum" },
1441{ 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1442{ 169, "copy", "copyright sign, U+00A9 ISOnum" },
1443{ 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1444{ 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1445{ 172, "not", "not sign, U+00AC ISOnum" },
1446{ 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1447{ 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
1448{ 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1449{ 176, "deg", "degree sign, U+00B0 ISOnum" },
1450{ 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1451{ 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1452{ 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1453{ 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1454{ 181, "micro","micro sign, U+00B5 ISOnum" },
1455{ 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1456{ 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1457{ 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1458{ 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1459{ 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1460{ 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1461{ 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1462{ 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1463{ 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1464{ 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1465{ 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1466{ 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1467{ 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1468{ 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1469{ 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1470{ 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1471{ 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1472{ 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1473{ 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1474{ 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1475{ 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1476{ 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1477{ 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1478{ 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1479{ 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1480{ 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1481{ 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
1482{ 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1483{ 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1484{ 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1485{ 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1486{ 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1487{ 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1488{ 215, "times","multiplication sign, U+00D7 ISOnum" },
1489{ 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1490{ 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1491{ 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1492{ 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1493{ 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1494{ 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1495{ 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1496{ 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1497{ 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1498{ 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1499{ 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1500{ 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1501{ 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1502{ 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1503{ 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1504{ 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1505{ 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1506{ 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1507{ 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1508{ 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1509{ 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1510{ 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1511{ 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1512{ 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1513{ 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
1514{ 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1515{ 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1516{ 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1517{ 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1518{ 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1519{ 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1520{ 247, "divide","division sign, U+00F7 ISOnum" },
1521{ 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1522{ 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1523{ 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1524{ 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1525{ 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1526{ 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1527{ 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1528{ 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1529
1530{ 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1531{ 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
1532{ 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1533{ 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1534{ 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1535
1536/*
1537 * Anything below should really be kept as entities references
1538 */
1539{ 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1540
1541{ 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1542{ 732, "tilde","small tilde, U+02DC ISOdia" },
1543
1544{ 913, "Alpha","greek capital letter alpha, U+0391" },
1545{ 914, "Beta", "greek capital letter beta, U+0392" },
1546{ 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1547{ 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1548{ 917, "Epsilon","greek capital letter epsilon, U+0395" },
1549{ 918, "Zeta", "greek capital letter zeta, U+0396" },
1550{ 919, "Eta", "greek capital letter eta, U+0397" },
1551{ 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1552{ 921, "Iota", "greek capital letter iota, U+0399" },
1553{ 922, "Kappa","greek capital letter kappa, U+039A" },
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001554{ 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
Owen Taylor3473f882001-02-23 17:55:21 +00001555{ 924, "Mu", "greek capital letter mu, U+039C" },
1556{ 925, "Nu", "greek capital letter nu, U+039D" },
1557{ 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1558{ 927, "Omicron","greek capital letter omicron, U+039F" },
1559{ 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1560{ 929, "Rho", "greek capital letter rho, U+03A1" },
1561{ 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1562{ 932, "Tau", "greek capital letter tau, U+03A4" },
1563{ 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1564{ 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
1565{ 935, "Chi", "greek capital letter chi, U+03A7" },
1566{ 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
1567{ 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1568
1569{ 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1570{ 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1571{ 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1572{ 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
1573{ 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1574{ 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1575{ 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
1576{ 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
1577{ 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1578{ 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1579{ 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1580{ 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
1581{ 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
1582{ 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
1583{ 959, "omicron","greek small letter omicron, U+03BF NEW" },
1584{ 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1585{ 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
1586{ 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1587{ 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1588{ 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
1589{ 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1590{ 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
1591{ 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
1592{ 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
1593{ 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
1594{ 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1595{ 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1596{ 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
1597
1598{ 8194, "ensp", "en space, U+2002 ISOpub" },
1599{ 8195, "emsp", "em space, U+2003 ISOpub" },
1600{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1601{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1602{ 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
1603{ 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
1604{ 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
1605{ 8211, "ndash","en dash, U+2013 ISOpub" },
1606{ 8212, "mdash","em dash, U+2014 ISOpub" },
1607{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1608{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1609{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1610{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1611{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1612{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1613{ 8224, "dagger","dagger, U+2020 ISOpub" },
1614{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1615
1616{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1617{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1618
1619{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1620
1621{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1622{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1623
1624{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1625{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1626
1627{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1628{ 8260, "frasl","fraction slash, U+2044 NEW" },
1629
1630{ 8364, "euro", "euro sign, U+20AC NEW" },
1631
1632{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1633{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1634{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1635{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
1636{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
1637{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
1638{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
1639{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
1640{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
1641{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
1642{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
1643{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
1644{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
1645{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
1646{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
1647{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
1648
1649{ 8704, "forall","for all, U+2200 ISOtech" },
1650{ 8706, "part", "partial differential, U+2202 ISOtech" },
1651{ 8707, "exist","there exists, U+2203 ISOtech" },
1652{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
1653{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
1654{ 8712, "isin", "element of, U+2208 ISOtech" },
1655{ 8713, "notin","not an element of, U+2209 ISOtech" },
1656{ 8715, "ni", "contains as member, U+220B ISOtech" },
1657{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001658{ 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
Owen Taylor3473f882001-02-23 17:55:21 +00001659{ 8722, "minus","minus sign, U+2212 ISOtech" },
1660{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
1661{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
1662{ 8733, "prop", "proportional to, U+221D ISOtech" },
1663{ 8734, "infin","infinity, U+221E ISOtech" },
1664{ 8736, "ang", "angle, U+2220 ISOamso" },
1665{ 8743, "and", "logical and = wedge, U+2227 ISOtech" },
1666{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
1667{ 8745, "cap", "intersection = cap, U+2229 ISOtech" },
1668{ 8746, "cup", "union = cup, U+222A ISOtech" },
1669{ 8747, "int", "integral, U+222B ISOtech" },
1670{ 8756, "there4","therefore, U+2234 ISOtech" },
1671{ 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
1672{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
1673{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
1674{ 8800, "ne", "not equal to, U+2260 ISOtech" },
1675{ 8801, "equiv","identical to, U+2261 ISOtech" },
1676{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
1677{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
1678{ 8834, "sub", "subset of, U+2282 ISOtech" },
1679{ 8835, "sup", "superset of, U+2283 ISOtech" },
1680{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
1681{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
1682{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
1683{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
1684{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
1685{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
1686{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
1687{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
1688{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
1689{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
1690{ 8971, "rfloor","right floor, U+230B ISOamsc" },
1691{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
1692{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
1693{ 9674, "loz", "lozenge, U+25CA ISOpub" },
1694
1695{ 9824, "spades","black spade suit, U+2660 ISOpub" },
1696{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
1697{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
1698{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
1699
1700};
1701
1702/************************************************************************
1703 * *
1704 * Commodity functions to handle entities *
1705 * *
1706 ************************************************************************/
1707
1708/*
1709 * Macro used to grow the current buffer.
1710 */
1711#define growBuffer(buffer) { \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001712 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001713 buffer##_size *= 2; \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001714 tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
1715 if (tmp == NULL) { \
Daniel Veillardf403d292003-10-05 13:51:35 +00001716 htmlErrMemory(ctxt, "growing buffer\n"); \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001717 xmlFree(buffer); \
Owen Taylor3473f882001-02-23 17:55:21 +00001718 return(NULL); \
1719 } \
Daniel Veillard079f6a72004-09-23 13:15:03 +00001720 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001721}
1722
1723/**
1724 * htmlEntityLookup:
1725 * @name: the entity name
1726 *
1727 * Lookup the given entity in EntitiesTable
1728 *
1729 * TODO: the linear scan is really ugly, an hash table is really needed.
1730 *
1731 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1732 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001733const htmlEntityDesc *
Owen Taylor3473f882001-02-23 17:55:21 +00001734htmlEntityLookup(const xmlChar *name) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001735 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001736
1737 for (i = 0;i < (sizeof(html40EntitiesTable)/
1738 sizeof(html40EntitiesTable[0]));i++) {
1739 if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
William M. Brack78637da2003-07-31 14:47:38 +00001740 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001741 }
1742 }
1743 return(NULL);
1744}
1745
1746/**
1747 * htmlEntityValueLookup:
1748 * @value: the entity's unicode value
1749 *
1750 * Lookup the given entity in EntitiesTable
1751 *
1752 * TODO: the linear scan is really ugly, an hash table is really needed.
1753 *
1754 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
1755 */
Daniel Veillardbb371292001-08-16 23:26:59 +00001756const htmlEntityDesc *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001757htmlEntityValueLookup(unsigned int value) {
1758 unsigned int i;
Owen Taylor3473f882001-02-23 17:55:21 +00001759
1760 for (i = 0;i < (sizeof(html40EntitiesTable)/
1761 sizeof(html40EntitiesTable[0]));i++) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001762 if (html40EntitiesTable[i].value >= value) {
1763 if (html40EntitiesTable[i].value > value)
Owen Taylor3473f882001-02-23 17:55:21 +00001764 break;
William M. Brack78637da2003-07-31 14:47:38 +00001765 return((htmlEntityDescPtr) &html40EntitiesTable[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001766 }
Owen Taylor3473f882001-02-23 17:55:21 +00001767 }
1768 return(NULL);
1769}
1770
1771/**
1772 * UTF8ToHtml:
1773 * @out: a pointer to an array of bytes to store the result
1774 * @outlen: the length of @out
1775 * @in: a pointer to an array of UTF-8 chars
1776 * @inlen: the length of @in
1777 *
1778 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1779 * plus HTML entities block of chars out.
1780 *
1781 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1782 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001783 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001784 * The value of @outlen after return is the number of octets consumed.
1785 */
1786int
1787UTF8ToHtml(unsigned char* out, int *outlen,
1788 const unsigned char* in, int *inlen) {
1789 const unsigned char* processed = in;
1790 const unsigned char* outend;
1791 const unsigned char* outstart = out;
1792 const unsigned char* instart = in;
1793 const unsigned char* inend;
1794 unsigned int c, d;
1795 int trailing;
1796
Daniel Veillardce682bc2004-11-05 17:22:25 +00001797 if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001798 if (in == NULL) {
1799 /*
1800 * initialization nothing to do
1801 */
1802 *outlen = 0;
1803 *inlen = 0;
1804 return(0);
1805 }
1806 inend = in + (*inlen);
1807 outend = out + (*outlen);
1808 while (in < inend) {
1809 d = *in++;
1810 if (d < 0x80) { c= d; trailing= 0; }
1811 else if (d < 0xC0) {
1812 /* trailing byte in leading position */
1813 *outlen = out - outstart;
1814 *inlen = processed - instart;
1815 return(-2);
1816 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1817 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1818 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1819 else {
1820 /* no chance for this in Ascii */
1821 *outlen = out - outstart;
1822 *inlen = processed - instart;
1823 return(-2);
1824 }
1825
1826 if (inend - in < trailing) {
1827 break;
1828 }
1829
1830 for ( ; trailing; trailing--) {
1831 if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
1832 break;
1833 c <<= 6;
1834 c |= d & 0x3F;
1835 }
1836
1837 /* assertion: c is a single UTF-4 value */
1838 if (c < 0x80) {
1839 if (out + 1 >= outend)
1840 break;
1841 *out++ = c;
1842 } else {
1843 int len;
Daniel Veillardbb371292001-08-16 23:26:59 +00001844 const htmlEntityDesc * ent;
Daniel Veillard1032ac42006-11-23 16:18:30 +00001845 const char *cp;
1846 char nbuf[16];
Owen Taylor3473f882001-02-23 17:55:21 +00001847
1848 /*
1849 * Try to lookup a predefined HTML entity for it
1850 */
1851
1852 ent = htmlEntityValueLookup(c);
1853 if (ent == NULL) {
Daniel Veillard1032ac42006-11-23 16:18:30 +00001854 snprintf(nbuf, sizeof(nbuf), "#%u", c);
1855 cp = nbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00001856 }
Daniel Veillard1032ac42006-11-23 16:18:30 +00001857 else
1858 cp = ent->name;
1859 len = strlen(cp);
Owen Taylor3473f882001-02-23 17:55:21 +00001860 if (out + 2 + len >= outend)
1861 break;
1862 *out++ = '&';
Daniel Veillard1032ac42006-11-23 16:18:30 +00001863 memcpy(out, cp, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001864 out += len;
1865 *out++ = ';';
1866 }
1867 processed = in;
1868 }
1869 *outlen = out - outstart;
1870 *inlen = processed - instart;
1871 return(0);
1872}
1873
1874/**
1875 * htmlEncodeEntities:
1876 * @out: a pointer to an array of bytes to store the result
1877 * @outlen: the length of @out
1878 * @in: a pointer to an array of UTF-8 chars
1879 * @inlen: the length of @in
1880 * @quoteChar: the quote character to escape (' or ") or zero.
1881 *
1882 * Take a block of UTF-8 chars in and try to convert it to an ASCII
1883 * plus HTML entities block of chars out.
1884 *
1885 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
1886 * The value of @inlen after return is the number of octets consumed
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001887 * as the return value is positive, else unpredictable.
Owen Taylor3473f882001-02-23 17:55:21 +00001888 * The value of @outlen after return is the number of octets consumed.
1889 */
1890int
1891htmlEncodeEntities(unsigned char* out, int *outlen,
1892 const unsigned char* in, int *inlen, int quoteChar) {
1893 const unsigned char* processed = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00001894 const unsigned char* outend;
Owen Taylor3473f882001-02-23 17:55:21 +00001895 const unsigned char* outstart = out;
1896 const unsigned char* instart = in;
Daniel Veillardce682bc2004-11-05 17:22:25 +00001897 const unsigned char* inend;
Owen Taylor3473f882001-02-23 17:55:21 +00001898 unsigned int c, d;
1899 int trailing;
1900
Daniel Veillardce682bc2004-11-05 17:22:25 +00001901 if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL))
1902 return(-1);
1903 outend = out + (*outlen);
1904 inend = in + (*inlen);
Owen Taylor3473f882001-02-23 17:55:21 +00001905 while (in < inend) {
1906 d = *in++;
1907 if (d < 0x80) { c= d; trailing= 0; }
1908 else if (d < 0xC0) {
1909 /* trailing byte in leading position */
1910 *outlen = out - outstart;
1911 *inlen = processed - instart;
1912 return(-2);
1913 } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
1914 else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
1915 else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
1916 else {
1917 /* no chance for this in Ascii */
1918 *outlen = out - outstart;
1919 *inlen = processed - instart;
1920 return(-2);
1921 }
1922
1923 if (inend - in < trailing)
1924 break;
1925
1926 while (trailing--) {
1927 if (((d= *in++) & 0xC0) != 0x80) {
1928 *outlen = out - outstart;
1929 *inlen = processed - instart;
1930 return(-2);
1931 }
1932 c <<= 6;
1933 c |= d & 0x3F;
1934 }
1935
1936 /* assertion: c is a single UTF-4 value */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001937 if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
1938 (c != '&') && (c != '<') && (c != '>')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001939 if (out >= outend)
1940 break;
1941 *out++ = c;
1942 } else {
Daniel Veillardbb371292001-08-16 23:26:59 +00001943 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00001944 const char *cp;
1945 char nbuf[16];
1946 int len;
1947
1948 /*
1949 * Try to lookup a predefined HTML entity for it
1950 */
1951 ent = htmlEntityValueLookup(c);
1952 if (ent == NULL) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001953 snprintf(nbuf, sizeof(nbuf), "#%u", c);
Owen Taylor3473f882001-02-23 17:55:21 +00001954 cp = nbuf;
1955 }
1956 else
1957 cp = ent->name;
1958 len = strlen(cp);
1959 if (out + 2 + len > outend)
1960 break;
1961 *out++ = '&';
1962 memcpy(out, cp, len);
1963 out += len;
1964 *out++ = ';';
1965 }
1966 processed = in;
1967 }
1968 *outlen = out - outstart;
1969 *inlen = processed - instart;
1970 return(0);
1971}
1972
Owen Taylor3473f882001-02-23 17:55:21 +00001973/************************************************************************
1974 * *
1975 * Commodity functions to handle streams *
1976 * *
1977 ************************************************************************/
1978
1979/**
Owen Taylor3473f882001-02-23 17:55:21 +00001980 * htmlNewInputStream:
1981 * @ctxt: an HTML parser context
1982 *
1983 * Create a new input stream structure
1984 * Returns the new input stream or NULL
1985 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001986static htmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001987htmlNewInputStream(htmlParserCtxtPtr ctxt) {
1988 htmlParserInputPtr input;
1989
1990 input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1991 if (input == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00001992 htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001993 return(NULL);
1994 }
1995 memset(input, 0, sizeof(htmlParserInput));
1996 input->filename = NULL;
1997 input->directory = NULL;
1998 input->base = NULL;
1999 input->cur = NULL;
2000 input->buf = NULL;
2001 input->line = 1;
2002 input->col = 1;
2003 input->buf = NULL;
2004 input->free = NULL;
2005 input->version = NULL;
2006 input->consumed = 0;
2007 input->length = 0;
2008 return(input);
2009}
2010
2011
2012/************************************************************************
2013 * *
2014 * Commodity functions, cleanup needed ? *
2015 * *
2016 ************************************************************************/
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002017/*
2018 * all tags allowing pc data from the html 4.01 loose dtd
2019 * NOTE: it might be more apropriate to integrate this information
2020 * into the html40ElementTable array but I don't want to risk any
2021 * binary incomptibility
2022 */
2023static const char *allowPCData[] = {
2024 "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
2025 "blockquote", "body", "button", "caption", "center", "cite", "code",
2026 "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
2027 "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
2028 "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
2029 "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
2030};
Owen Taylor3473f882001-02-23 17:55:21 +00002031
2032/**
2033 * areBlanks:
2034 * @ctxt: an HTML parser context
2035 * @str: a xmlChar *
2036 * @len: the size of @str
2037 *
2038 * Is this a sequence of blank chars that one can ignore ?
2039 *
2040 * Returns 1 if ignorable 0 otherwise.
2041 */
2042
2043static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002044 unsigned int i;
2045 int j;
Owen Taylor3473f882001-02-23 17:55:21 +00002046 xmlNodePtr lastChild;
Daniel Veillard36d73402005-09-01 09:52:30 +00002047 xmlDtdPtr dtd;
Owen Taylor3473f882001-02-23 17:55:21 +00002048
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002049 for (j = 0;j < len;j++)
William M. Brack76e95df2003-10-18 16:20:14 +00002050 if (!(IS_BLANK_CH(str[j]))) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002051
2052 if (CUR == 0) return(1);
2053 if (CUR != '<') return(0);
2054 if (ctxt->name == NULL)
2055 return(1);
2056 if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
2057 return(1);
2058 if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
2059 return(1);
Daniel Veillard36d73402005-09-01 09:52:30 +00002060
2061 /* Only strip CDATA children of the body tag for strict HTML DTDs */
2062 if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) {
2063 dtd = xmlGetIntSubset(ctxt->myDoc);
2064 if (dtd != NULL && dtd->ExternalID != NULL) {
2065 if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") ||
2066 !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN"))
2067 return(1);
2068 }
2069 }
2070
Owen Taylor3473f882001-02-23 17:55:21 +00002071 if (ctxt->node == NULL) return(0);
2072 lastChild = xmlGetLastChild(ctxt->node);
Daniel Veillard18a65092004-05-11 15:57:42 +00002073 while ((lastChild) && (lastChild->type == XML_COMMENT_NODE))
2074 lastChild = lastChild->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00002075 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002076 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2077 (ctxt->node->content != NULL)) return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002078 /* keep ws in constructs like ...<b> </b>...
2079 for all tags "b" allowing PCDATA */
2080 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2081 if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
2082 return(0);
2083 }
2084 }
Owen Taylor3473f882001-02-23 17:55:21 +00002085 } else if (xmlNodeIsText(lastChild)) {
2086 return(0);
Daniel Veillard8c9872c2002-07-05 18:17:10 +00002087 } else {
2088 /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
2089 for all tags "p" allowing PCDATA */
2090 for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2091 if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
2092 return(0);
2093 }
2094 }
Owen Taylor3473f882001-02-23 17:55:21 +00002095 }
2096 return(1);
2097}
2098
2099/**
Owen Taylor3473f882001-02-23 17:55:21 +00002100 * htmlNewDocNoDtD:
2101 * @URI: URI for the dtd, or NULL
2102 * @ExternalID: the external ID of the DTD, or NULL
2103 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002104 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2105 * are NULL
2106 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002107 * Returns a new document, do not initialize the DTD if not provided
Owen Taylor3473f882001-02-23 17:55:21 +00002108 */
2109htmlDocPtr
2110htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
2111 xmlDocPtr cur;
2112
2113 /*
2114 * Allocate a new document and fill the fields.
2115 */
2116 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
2117 if (cur == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002118 htmlErrMemory(NULL, "HTML document creation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002119 return(NULL);
2120 }
2121 memset(cur, 0, sizeof(xmlDoc));
2122
2123 cur->type = XML_HTML_DOCUMENT_NODE;
2124 cur->version = NULL;
2125 cur->intSubset = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002126 cur->doc = cur;
2127 cur->name = NULL;
2128 cur->children = NULL;
2129 cur->extSubset = NULL;
2130 cur->oldNs = NULL;
2131 cur->encoding = NULL;
2132 cur->standalone = 1;
2133 cur->compression = 0;
2134 cur->ids = NULL;
2135 cur->refs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002136 cur->_private = NULL;
Daniel Veillard7cc23572004-07-29 11:20:30 +00002137 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillardb6b0fd82001-10-22 12:31:11 +00002138 if ((ExternalID != NULL) ||
2139 (URI != NULL))
Daniel Veillard40412cd2003-09-03 13:28:32 +00002140 xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
Owen Taylor3473f882001-02-23 17:55:21 +00002141 return(cur);
2142}
2143
2144/**
2145 * htmlNewDoc:
2146 * @URI: URI for the dtd, or NULL
2147 * @ExternalID: the external ID of the DTD, or NULL
2148 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002149 * Creates a new HTML document
2150 *
Owen Taylor3473f882001-02-23 17:55:21 +00002151 * Returns a new document
2152 */
2153htmlDocPtr
2154htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
2155 if ((URI == NULL) && (ExternalID == NULL))
2156 return(htmlNewDocNoDtD(
Daniel Veillard64269352001-05-04 17:52:34 +00002157 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
2158 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
Owen Taylor3473f882001-02-23 17:55:21 +00002159
2160 return(htmlNewDocNoDtD(URI, ExternalID));
2161}
2162
2163
2164/************************************************************************
2165 * *
2166 * The parser itself *
2167 * Relates to http://www.w3.org/TR/html40 *
2168 * *
2169 ************************************************************************/
2170
2171/************************************************************************
2172 * *
2173 * The parser itself *
2174 * *
2175 ************************************************************************/
2176
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002177static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002178
Owen Taylor3473f882001-02-23 17:55:21 +00002179/**
2180 * htmlParseHTMLName:
2181 * @ctxt: an HTML parser context
2182 *
2183 * parse an HTML tag or attribute name, note that we convert it to lowercase
2184 * since HTML names are not case-sensitive.
2185 *
2186 * Returns the Tag Name parsed or NULL
2187 */
2188
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002189static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002190htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002191 int i = 0;
2192 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2193
William M. Brackd1757ab2004-10-02 22:07:48 +00002194 if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002195 (CUR != ':')) return(NULL);
2196
2197 while ((i < HTML_PARSER_BUFFER_SIZE) &&
William M. Brackd1757ab2004-10-02 22:07:48 +00002198 ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002199 (CUR == ':') || (CUR == '-') || (CUR == '_'))) {
2200 if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2201 else loc[i] = CUR;
2202 i++;
2203
2204 NEXT;
2205 }
2206
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002207 return(xmlDictLookup(ctxt->dict, loc, i));
Owen Taylor3473f882001-02-23 17:55:21 +00002208}
2209
Daniel Veillard890fd9f2006-10-27 12:53:28 +00002210
2211/**
2212 * htmlParseHTMLName_nonInvasive:
2213 * @ctxt: an HTML parser context
2214 *
2215 * parse an HTML tag or attribute name, note that we convert it to lowercase
2216 * since HTML names are not case-sensitive, this doesn't consume the data
2217 * from the stream, it's a look-ahead
2218 *
2219 * Returns the Tag Name parsed or NULL
2220 */
2221
2222static const xmlChar *
2223htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) {
2224 int i = 0;
2225 xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2226
2227 if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') &&
2228 (NXT(1) != ':')) return(NULL);
2229
2230 while ((i < HTML_PARSER_BUFFER_SIZE) &&
2231 ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) ||
2232 (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) {
2233 if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20;
2234 else loc[i] = NXT(1+i);
2235 i++;
2236 }
2237
2238 return(xmlDictLookup(ctxt->dict, loc, i));
2239}
2240
2241
Owen Taylor3473f882001-02-23 17:55:21 +00002242/**
2243 * htmlParseName:
2244 * @ctxt: an HTML parser context
2245 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002246 * parse an HTML name, this routine is case sensitive.
Owen Taylor3473f882001-02-23 17:55:21 +00002247 *
2248 * Returns the Name parsed or NULL
2249 */
2250
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002251static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002252htmlParseName(htmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002253 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002254 const xmlChar *ret;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002255 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002256
2257 GROW;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002258
2259 /*
2260 * Accelerator for simple ASCII names
2261 */
2262 in = ctxt->input->cur;
2263 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2264 ((*in >= 0x41) && (*in <= 0x5A)) ||
2265 (*in == '_') || (*in == ':')) {
2266 in++;
2267 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2268 ((*in >= 0x41) && (*in <= 0x5A)) ||
2269 ((*in >= 0x30) && (*in <= 0x39)) ||
2270 (*in == '_') || (*in == '-') ||
2271 (*in == ':') || (*in == '.'))
2272 in++;
2273 if ((*in > 0) && (*in < 0x80)) {
2274 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002275 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002276 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002277 ctxt->nbChars += count;
2278 ctxt->input->col += count;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002279 return(ret);
2280 }
2281 }
2282 return(htmlParseNameComplex(ctxt));
2283}
2284
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002285static const xmlChar *
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002286htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002287 int len = 0, l;
2288 int c;
2289 int count = 0;
2290
2291 /*
2292 * Handler for more complex cases
2293 */
2294 GROW;
2295 c = CUR_CHAR(l);
2296 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2297 (!IS_LETTER(c) && (c != '_') &&
2298 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002299 return(NULL);
2300 }
2301
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002302 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
2303 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2304 (c == '.') || (c == '-') ||
2305 (c == '_') || (c == ':') ||
2306 (IS_COMBINING(c)) ||
2307 (IS_EXTENDER(c)))) {
2308 if (count++ > 100) {
2309 count = 0;
2310 GROW;
2311 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002312 len += l;
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002313 NEXTL(l);
2314 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002315 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002316 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002317}
2318
Daniel Veillarde55e8e42003-01-10 12:50:02 +00002319
Owen Taylor3473f882001-02-23 17:55:21 +00002320/**
2321 * htmlParseHTMLAttribute:
2322 * @ctxt: an HTML parser context
2323 * @stop: a char stop value
2324 *
2325 * parse an HTML attribute value till the stop (quote), if
2326 * stop is 0 then it stops at the first space
2327 *
2328 * Returns the attribute parsed or NULL
2329 */
2330
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002331static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002332htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
2333 xmlChar *buffer = NULL;
2334 int buffer_size = 0;
2335 xmlChar *out = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002336 const xmlChar *name = NULL;
2337 const xmlChar *cur = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00002338 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00002339
2340 /*
2341 * allocate a translation buffer.
2342 */
2343 buffer_size = HTML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002344 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002345 if (buffer == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002346 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002347 return(NULL);
2348 }
2349 out = buffer;
2350
2351 /*
2352 * Ok loop until we reach one of the ending chars
2353 */
Daniel Veillard957fdcf2001-11-06 22:50:19 +00002354 while ((CUR != 0) && (CUR != stop)) {
2355 if ((stop == 0) && (CUR == '>')) break;
William M. Brack76e95df2003-10-18 16:20:14 +00002356 if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
Owen Taylor3473f882001-02-23 17:55:21 +00002357 if (CUR == '&') {
2358 if (NXT(1) == '#') {
2359 unsigned int c;
2360 int bits;
2361
2362 c = htmlParseCharRef(ctxt);
2363 if (c < 0x80)
2364 { *out++ = c; bits= -6; }
2365 else if (c < 0x800)
2366 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2367 else if (c < 0x10000)
2368 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2369 else
2370 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2371
2372 for ( ; bits >= 0; bits-= 6) {
2373 *out++ = ((c >> bits) & 0x3F) | 0x80;
2374 }
Daniel Veillardce02dbc2002-10-22 19:14:58 +00002375
2376 if (out - buffer > buffer_size - 100) {
2377 int indx = out - buffer;
2378
2379 growBuffer(buffer);
2380 out = &buffer[indx];
2381 }
Owen Taylor3473f882001-02-23 17:55:21 +00002382 } else {
2383 ent = htmlParseEntityRef(ctxt, &name);
2384 if (name == NULL) {
2385 *out++ = '&';
2386 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002387 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002388
2389 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002390 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002391 }
2392 } else if (ent == NULL) {
2393 *out++ = '&';
2394 cur = name;
2395 while (*cur != 0) {
2396 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002397 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002398
2399 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002400 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002401 }
2402 *out++ = *cur++;
2403 }
Owen Taylor3473f882001-02-23 17:55:21 +00002404 } else {
2405 unsigned int c;
2406 int bits;
2407
2408 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002409 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002410
2411 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002412 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002413 }
Daniel Veillard48519092006-10-17 15:56:35 +00002414 c = ent->value;
Owen Taylor3473f882001-02-23 17:55:21 +00002415 if (c < 0x80)
2416 { *out++ = c; bits= -6; }
2417 else if (c < 0x800)
2418 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2419 else if (c < 0x10000)
2420 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2421 else
2422 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2423
2424 for ( ; bits >= 0; bits-= 6) {
2425 *out++ = ((c >> bits) & 0x3F) | 0x80;
2426 }
Owen Taylor3473f882001-02-23 17:55:21 +00002427 }
2428 }
2429 } else {
2430 unsigned int c;
2431 int bits, l;
2432
2433 if (out - buffer > buffer_size - 100) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002434 int indx = out - buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00002435
2436 growBuffer(buffer);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002437 out = &buffer[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00002438 }
2439 c = CUR_CHAR(l);
2440 if (c < 0x80)
2441 { *out++ = c; bits= -6; }
2442 else if (c < 0x800)
2443 { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
2444 else if (c < 0x10000)
2445 { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
2446 else
2447 { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
2448
2449 for ( ; bits >= 0; bits-= 6) {
2450 *out++ = ((c >> bits) & 0x3F) | 0x80;
2451 }
2452 NEXT;
2453 }
2454 }
2455 *out++ = 0;
2456 return(buffer);
2457}
2458
2459/**
Owen Taylor3473f882001-02-23 17:55:21 +00002460 * htmlParseEntityRef:
2461 * @ctxt: an HTML parser context
2462 * @str: location to store the entity name
2463 *
2464 * parse an HTML ENTITY references
2465 *
2466 * [68] EntityRef ::= '&' Name ';'
2467 *
2468 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2469 * if non-NULL *str will have to be freed by the caller.
2470 */
Daniel Veillardbb371292001-08-16 23:26:59 +00002471const htmlEntityDesc *
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002472htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
2473 const xmlChar *name;
Daniel Veillardbb371292001-08-16 23:26:59 +00002474 const htmlEntityDesc * ent = NULL;
Daniel Veillard42595322004-11-08 10:52:06 +00002475
2476 if (str != NULL) *str = NULL;
2477 if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002478
2479 if (CUR == '&') {
2480 NEXT;
2481 name = htmlParseName(ctxt);
2482 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002483 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2484 "htmlParseEntityRef: no name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002485 } else {
2486 GROW;
2487 if (CUR == ';') {
Daniel Veillard42595322004-11-08 10:52:06 +00002488 if (str != NULL)
2489 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002490
2491 /*
2492 * Lookup the entity in the table.
2493 */
2494 ent = htmlEntityLookup(name);
2495 if (ent != NULL) /* OK that's ugly !!! */
2496 NEXT;
2497 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002498 htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
2499 "htmlParseEntityRef: expecting ';'\n",
2500 NULL, NULL);
Daniel Veillard42595322004-11-08 10:52:06 +00002501 if (str != NULL)
2502 *str = name;
Owen Taylor3473f882001-02-23 17:55:21 +00002503 }
2504 }
2505 }
2506 return(ent);
2507}
2508
2509/**
2510 * htmlParseAttValue:
2511 * @ctxt: an HTML parser context
2512 *
2513 * parse a value for an attribute
2514 * Note: the parser won't do substitution of entities here, this
2515 * will be handled later in xmlStringGetNodeList, unless it was
2516 * asked for ctxt->replaceEntities != 0
2517 *
2518 * Returns the AttValue parsed or NULL.
2519 */
2520
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002521static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002522htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2523 xmlChar *ret = NULL;
2524
2525 if (CUR == '"') {
2526 NEXT;
2527 ret = htmlParseHTMLAttribute(ctxt, '"');
2528 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002529 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2530 "AttValue: \" expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002531 } else
2532 NEXT;
2533 } else if (CUR == '\'') {
2534 NEXT;
2535 ret = htmlParseHTMLAttribute(ctxt, '\'');
2536 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002537 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2538 "AttValue: ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002539 } else
2540 NEXT;
2541 } else {
2542 /*
2543 * That's an HTMLism, the attribute value may not be quoted
2544 */
2545 ret = htmlParseHTMLAttribute(ctxt, 0);
2546 if (ret == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002547 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
2548 "AttValue: no value found\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002549 }
2550 }
2551 return(ret);
2552}
2553
2554/**
2555 * htmlParseSystemLiteral:
2556 * @ctxt: an HTML parser context
2557 *
2558 * parse an HTML Literal
2559 *
2560 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2561 *
2562 * Returns the SystemLiteral parsed or NULL
2563 */
2564
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002565static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002566htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2567 const xmlChar *q;
2568 xmlChar *ret = NULL;
2569
2570 if (CUR == '"') {
2571 NEXT;
2572 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002573 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00002574 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002575 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002576 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2577 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002578 } else {
2579 ret = xmlStrndup(q, CUR_PTR - q);
2580 NEXT;
2581 }
2582 } else if (CUR == '\'') {
2583 NEXT;
2584 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002585 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002586 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00002587 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002588 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2589 "Unfinished SystemLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002590 } else {
2591 ret = xmlStrndup(q, CUR_PTR - q);
2592 NEXT;
2593 }
2594 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002595 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2596 " or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002597 }
2598
2599 return(ret);
2600}
2601
2602/**
2603 * htmlParsePubidLiteral:
2604 * @ctxt: an HTML parser context
2605 *
2606 * parse an HTML public literal
2607 *
2608 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2609 *
2610 * Returns the PubidLiteral parsed or NULL.
2611 */
2612
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002613static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002614htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
2615 const xmlChar *q;
2616 xmlChar *ret = NULL;
2617 /*
2618 * Name ::= (Letter | '_') (NameChar)*
2619 */
2620 if (CUR == '"') {
2621 NEXT;
2622 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002623 while (IS_PUBIDCHAR_CH(CUR)) NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00002624 if (CUR != '"') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002625 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2626 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002627 } else {
2628 ret = xmlStrndup(q, CUR_PTR - q);
2629 NEXT;
2630 }
2631 } else if (CUR == '\'') {
2632 NEXT;
2633 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00002634 while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00002635 NEXT;
Daniel Veillard6560a422003-03-27 21:25:38 +00002636 if (CUR != '\'') {
Daniel Veillardf403d292003-10-05 13:51:35 +00002637 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2638 "Unfinished PubidLiteral\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002639 } else {
2640 ret = xmlStrndup(q, CUR_PTR - q);
2641 NEXT;
2642 }
2643 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00002644 htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2645 "PubidLiteral \" or ' expected\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002646 }
2647
2648 return(ret);
2649}
2650
2651/**
2652 * htmlParseScript:
2653 * @ctxt: an HTML parser context
2654 *
2655 * parse the content of an HTML SCRIPT or STYLE element
2656 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
2657 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
2658 * http://www.w3.org/TR/html4/types.html#type-script
2659 * http://www.w3.org/TR/html4/types.html#h-6.15
2660 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
2661 *
2662 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
2663 * element and the value of intrinsic event attributes. User agents must
2664 * not evaluate script data as HTML markup but instead must pass it on as
2665 * data to a script engine.
2666 * NOTES:
2667 * - The content is passed like CDATA
2668 * - the attributes for style and scripting "onXXX" are also described
2669 * as CDATA but SGML allows entities references in attributes so their
2670 * processing is identical as other attributes
2671 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002672static void
Owen Taylor3473f882001-02-23 17:55:21 +00002673htmlParseScript(htmlParserCtxtPtr ctxt) {
Daniel Veillard7d2b3232005-07-14 08:57:39 +00002674 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
Owen Taylor3473f882001-02-23 17:55:21 +00002675 int nbchar = 0;
Daniel Veillard358fef42005-07-13 16:37:38 +00002676 int cur,l;
Owen Taylor3473f882001-02-23 17:55:21 +00002677
2678 SHRINK;
Daniel Veillard358fef42005-07-13 16:37:38 +00002679 cur = CUR_CHAR(l);
William M. Brack76e95df2003-10-18 16:20:14 +00002680 while (IS_CHAR_CH(cur)) {
Daniel Veillard42720242007-04-16 07:02:31 +00002681 if ((cur == '<') && (NXT(1) == '/')) {
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002682 /*
2683 * One should break here, the specification is clear:
2684 * Authors should therefore escape "</" within the content.
2685 * Escape mechanisms are specific to each scripting or
2686 * style sheet language.
2687 *
2688 * In recovery mode, only break if end tag match the
2689 * current tag, effectively ignoring all tags inside the
2690 * script/style block and treating the entire block as
2691 * CDATA.
2692 */
2693 if (ctxt->recovery) {
2694 if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2,
2695 xmlStrlen(ctxt->name)) == 0)
2696 {
2697 break; /* while */
2698 } else {
2699 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard2cf36a12005-10-25 12:21:29 +00002700 "Element %s embeds close tag\n",
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002701 ctxt->name, NULL);
2702 }
2703 } else {
2704 if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
2705 ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
2706 {
2707 break; /* while */
2708 }
2709 }
Owen Taylor3473f882001-02-23 17:55:21 +00002710 }
Daniel Veillard358fef42005-07-13 16:37:38 +00002711 COPY_BUF(l,buf,nbchar,cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002712 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2713 if (ctxt->sax->cdataBlock!= NULL) {
2714 /*
2715 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2716 */
2717 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002718 } else if (ctxt->sax->characters != NULL) {
2719 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002720 }
2721 nbchar = 0;
2722 }
Daniel Veillardb9900082005-10-25 12:36:29 +00002723 GROW;
Daniel Veillard358fef42005-07-13 16:37:38 +00002724 NEXTL(l);
2725 cur = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002726 }
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00002727
Daniel Veillard68716a72006-10-16 09:32:17 +00002728 if ((!(IS_CHAR_CH(cur))) && (!((cur == 0) && (ctxt->progressive)))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002729 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2730 "Invalid char in CDATA 0x%X\n", cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002731 NEXT;
2732 }
2733
2734 if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2735 if (ctxt->sax->cdataBlock!= NULL) {
2736 /*
2737 * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
2738 */
2739 ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00002740 } else if (ctxt->sax->characters != NULL) {
2741 ctxt->sax->characters(ctxt->userData, buf, nbchar);
Owen Taylor3473f882001-02-23 17:55:21 +00002742 }
2743 }
2744}
2745
2746
2747/**
2748 * htmlParseCharData:
2749 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002750 *
2751 * parse a CharData section.
2752 * if we are within a CDATA section ']]>' marks an end of section.
2753 *
2754 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
2755 */
2756
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002757static void
2758htmlParseCharData(htmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00002759 xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
2760 int nbchar = 0;
2761 int cur, l;
2762
2763 SHRINK;
2764 cur = CUR_CHAR(l);
2765 while (((cur != '<') || (ctxt->token == '<')) &&
2766 ((cur != '&') || (ctxt->token == '&')) &&
2767 (IS_CHAR(cur))) {
2768 COPY_BUF(l,buf,nbchar,cur);
2769 if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
2770 /*
2771 * Ok the segment is to be consumed as chars.
2772 */
2773 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2774 if (areBlanks(ctxt, buf, nbchar)) {
2775 if (ctxt->sax->ignorableWhitespace != NULL)
2776 ctxt->sax->ignorableWhitespace(ctxt->userData,
2777 buf, nbchar);
2778 } else {
2779 htmlCheckParagraph(ctxt);
2780 if (ctxt->sax->characters != NULL)
2781 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2782 }
2783 }
2784 nbchar = 0;
2785 }
2786 NEXTL(l);
2787 cur = CUR_CHAR(l);
Daniel Veillard358a9892003-02-04 15:22:32 +00002788 if (cur == 0) {
2789 SHRINK;
2790 GROW;
2791 cur = CUR_CHAR(l);
2792 }
Owen Taylor3473f882001-02-23 17:55:21 +00002793 }
2794 if (nbchar != 0) {
Daniel Veillardd2755a82005-08-07 23:42:39 +00002795 buf[nbchar] = 0;
2796
Owen Taylor3473f882001-02-23 17:55:21 +00002797 /*
2798 * Ok the segment is to be consumed as chars.
2799 */
2800 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
2801 if (areBlanks(ctxt, buf, nbchar)) {
2802 if (ctxt->sax->ignorableWhitespace != NULL)
2803 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
2804 } else {
2805 htmlCheckParagraph(ctxt);
2806 if (ctxt->sax->characters != NULL)
2807 ctxt->sax->characters(ctxt->userData, buf, nbchar);
2808 }
2809 }
Daniel Veillard7cc95c02001-10-17 15:45:12 +00002810 } else {
2811 /*
2812 * Loop detection
2813 */
2814 if (cur == 0)
2815 ctxt->instate = XML_PARSER_EOF;
Owen Taylor3473f882001-02-23 17:55:21 +00002816 }
2817}
2818
2819/**
2820 * htmlParseExternalID:
2821 * @ctxt: an HTML parser context
2822 * @publicID: a xmlChar** receiving PubidLiteral
Owen Taylor3473f882001-02-23 17:55:21 +00002823 *
2824 * Parse an External ID or a Public ID
2825 *
Owen Taylor3473f882001-02-23 17:55:21 +00002826 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
2827 * | 'PUBLIC' S PubidLiteral S SystemLiteral
2828 *
2829 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
2830 *
2831 * Returns the function returns SystemLiteral and in the second
2832 * case publicID receives PubidLiteral, is strict is off
2833 * it is possible to return NULL and have publicID set.
2834 */
2835
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002836static xmlChar *
2837htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
Owen Taylor3473f882001-02-23 17:55:21 +00002838 xmlChar *URI = NULL;
2839
2840 if ((UPPER == 'S') && (UPP(1) == 'Y') &&
2841 (UPP(2) == 'S') && (UPP(3) == 'T') &&
2842 (UPP(4) == 'E') && (UPP(5) == 'M')) {
2843 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002844 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002845 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2846 "Space required after 'SYSTEM'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002847 }
2848 SKIP_BLANKS;
2849 URI = htmlParseSystemLiteral(ctxt);
2850 if (URI == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002851 htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
2852 "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002853 }
2854 } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
2855 (UPP(2) == 'B') && (UPP(3) == 'L') &&
2856 (UPP(4) == 'I') && (UPP(5) == 'C')) {
2857 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00002858 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002859 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2860 "Space required after 'PUBLIC'\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002861 }
2862 SKIP_BLANKS;
2863 *publicID = htmlParsePubidLiteral(ctxt);
2864 if (*publicID == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00002865 htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
2866 "htmlParseExternalID: PUBLIC, no Public Identifier\n",
2867 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002868 }
2869 SKIP_BLANKS;
2870 if ((CUR == '"') || (CUR == '\'')) {
2871 URI = htmlParseSystemLiteral(ctxt);
2872 }
2873 }
2874 return(URI);
2875}
2876
2877/**
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002878 * xmlParsePI:
2879 * @ctxt: an XML parser context
2880 *
2881 * parse an XML Processing Instruction.
2882 *
2883 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
2884 */
2885static void
2886htmlParsePI(htmlParserCtxtPtr ctxt) {
2887 xmlChar *buf = NULL;
2888 int len = 0;
2889 int size = HTML_PARSER_BUFFER_SIZE;
2890 int cur, l;
2891 const xmlChar *target;
2892 xmlParserInputState state;
2893 int count = 0;
2894
2895 if ((RAW == '<') && (NXT(1) == '?')) {
2896 state = ctxt->instate;
2897 ctxt->instate = XML_PARSER_PI;
2898 /*
2899 * this is a Processing Instruction.
2900 */
2901 SKIP(2);
2902 SHRINK;
2903
2904 /*
2905 * Parse the target name and check for special support like
2906 * namespace.
2907 */
2908 target = htmlParseName(ctxt);
2909 if (target != NULL) {
2910 if (RAW == '>') {
2911 SKIP(1);
2912
2913 /*
2914 * SAX: PI detected.
2915 */
2916 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2917 (ctxt->sax->processingInstruction != NULL))
2918 ctxt->sax->processingInstruction(ctxt->userData,
2919 target, NULL);
2920 ctxt->instate = state;
2921 return;
2922 }
2923 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
2924 if (buf == NULL) {
2925 htmlErrMemory(ctxt, NULL);
2926 ctxt->instate = state;
2927 return;
2928 }
2929 cur = CUR;
2930 if (!IS_BLANK(cur)) {
2931 htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
2932 "ParsePI: PI %s space expected\n", target, NULL);
2933 }
2934 SKIP_BLANKS;
2935 cur = CUR_CHAR(l);
2936 while (IS_CHAR(cur) && (cur != '>')) {
2937 if (len + 5 >= size) {
2938 xmlChar *tmp;
2939
2940 size *= 2;
2941 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2942 if (tmp == NULL) {
2943 htmlErrMemory(ctxt, NULL);
2944 xmlFree(buf);
2945 ctxt->instate = state;
2946 return;
2947 }
2948 buf = tmp;
2949 }
2950 count++;
2951 if (count > 50) {
2952 GROW;
2953 count = 0;
2954 }
2955 COPY_BUF(l,buf,len,cur);
2956 NEXTL(l);
2957 cur = CUR_CHAR(l);
2958 if (cur == 0) {
2959 SHRINK;
2960 GROW;
2961 cur = CUR_CHAR(l);
2962 }
2963 }
2964 buf[len] = 0;
2965 if (cur != '>') {
2966 htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
2967 "ParsePI: PI %s never end ...\n", target, NULL);
2968 } else {
2969 SKIP(1);
2970
2971 /*
2972 * SAX: PI detected.
2973 */
2974 if ((ctxt->sax) && (!ctxt->disableSAX) &&
2975 (ctxt->sax->processingInstruction != NULL))
2976 ctxt->sax->processingInstruction(ctxt->userData,
2977 target, buf);
2978 }
2979 xmlFree(buf);
2980 } else {
2981 htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
2982 "PI is not started correctly", NULL, NULL);
2983 }
2984 ctxt->instate = state;
2985 }
2986}
2987
2988/**
Owen Taylor3473f882001-02-23 17:55:21 +00002989 * htmlParseComment:
2990 * @ctxt: an HTML parser context
2991 *
2992 * Parse an XML (SGML) comment <!-- .... -->
2993 *
2994 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
2995 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002996static void
Owen Taylor3473f882001-02-23 17:55:21 +00002997htmlParseComment(htmlParserCtxtPtr ctxt) {
2998 xmlChar *buf = NULL;
2999 int len;
3000 int size = HTML_PARSER_BUFFER_SIZE;
3001 int q, ql;
3002 int r, rl;
3003 int cur, l;
3004 xmlParserInputState state;
3005
3006 /*
3007 * Check that there is a comment right here.
3008 */
3009 if ((RAW != '<') || (NXT(1) != '!') ||
3010 (NXT(2) != '-') || (NXT(3) != '-')) return;
3011
3012 state = ctxt->instate;
3013 ctxt->instate = XML_PARSER_COMMENT;
3014 SHRINK;
3015 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003016 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003017 if (buf == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003018 htmlErrMemory(ctxt, "buffer allocation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003019 ctxt->instate = state;
3020 return;
3021 }
3022 q = CUR_CHAR(ql);
3023 NEXTL(ql);
3024 r = CUR_CHAR(rl);
3025 NEXTL(rl);
3026 cur = CUR_CHAR(l);
3027 len = 0;
3028 while (IS_CHAR(cur) &&
3029 ((cur != '>') ||
3030 (r != '-') || (q != '-'))) {
3031 if (len + 5 >= size) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003032 xmlChar *tmp;
3033
Owen Taylor3473f882001-02-23 17:55:21 +00003034 size *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003035 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3036 if (tmp == NULL) {
3037 xmlFree(buf);
Daniel Veillardf403d292003-10-05 13:51:35 +00003038 htmlErrMemory(ctxt, "growing buffer failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003039 ctxt->instate = state;
3040 return;
3041 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003042 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003043 }
3044 COPY_BUF(ql,buf,len,q);
3045 q = r;
3046 ql = rl;
3047 r = cur;
3048 rl = l;
3049 NEXTL(l);
3050 cur = CUR_CHAR(l);
3051 if (cur == 0) {
3052 SHRINK;
3053 GROW;
3054 cur = CUR_CHAR(l);
3055 }
3056 }
3057 buf[len] = 0;
3058 if (!IS_CHAR(cur)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003059 htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3060 "Comment not terminated \n<!--%.50s\n", buf, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003061 xmlFree(buf);
3062 } else {
3063 NEXT;
3064 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3065 (!ctxt->disableSAX))
3066 ctxt->sax->comment(ctxt->userData, buf);
3067 xmlFree(buf);
3068 }
3069 ctxt->instate = state;
3070}
3071
3072/**
3073 * htmlParseCharRef:
3074 * @ctxt: an HTML parser context
3075 *
3076 * parse Reference declarations
3077 *
3078 * [66] CharRef ::= '&#' [0-9]+ ';' |
3079 * '&#x' [0-9a-fA-F]+ ';'
3080 *
3081 * Returns the value parsed (as an int)
3082 */
3083int
3084htmlParseCharRef(htmlParserCtxtPtr ctxt) {
3085 int val = 0;
3086
Daniel Veillarda03e3652004-11-02 18:45:30 +00003087 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3088 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3089 "htmlParseCharRef: context error\n",
3090 NULL, NULL);
3091 return(0);
3092 }
Owen Taylor3473f882001-02-23 17:55:21 +00003093 if ((CUR == '&') && (NXT(1) == '#') &&
Daniel Veillardc59d8262003-11-20 21:59:12 +00003094 ((NXT(2) == 'x') || NXT(2) == 'X')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003095 SKIP(3);
3096 while (CUR != ';') {
3097 if ((CUR >= '0') && (CUR <= '9'))
3098 val = val * 16 + (CUR - '0');
3099 else if ((CUR >= 'a') && (CUR <= 'f'))
3100 val = val * 16 + (CUR - 'a') + 10;
3101 else if ((CUR >= 'A') && (CUR <= 'F'))
3102 val = val * 16 + (CUR - 'A') + 10;
3103 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003104 htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
3105 "htmlParseCharRef: invalid hexadecimal value\n",
3106 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003107 return(0);
3108 }
3109 NEXT;
3110 }
3111 if (CUR == ';')
3112 NEXT;
3113 } else if ((CUR == '&') && (NXT(1) == '#')) {
3114 SKIP(2);
3115 while (CUR != ';') {
3116 if ((CUR >= '0') && (CUR <= '9'))
3117 val = val * 10 + (CUR - '0');
3118 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003119 htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
3120 "htmlParseCharRef: invalid decimal value\n",
3121 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 return(0);
3123 }
3124 NEXT;
3125 }
3126 if (CUR == ';')
3127 NEXT;
3128 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003129 htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
3130 "htmlParseCharRef: invalid value\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003131 }
3132 /*
3133 * Check the value IS_CHAR ...
3134 */
3135 if (IS_CHAR(val)) {
3136 return(val);
3137 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003138 htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3139 "htmlParseCharRef: invalid xmlChar value %d\n",
3140 val);
Owen Taylor3473f882001-02-23 17:55:21 +00003141 }
3142 return(0);
3143}
3144
3145
3146/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00003147 * htmlParseDocTypeDecl:
Owen Taylor3473f882001-02-23 17:55:21 +00003148 * @ctxt: an HTML parser context
3149 *
3150 * parse a DOCTYPE declaration
3151 *
3152 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3153 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3154 */
3155
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003156static void
Owen Taylor3473f882001-02-23 17:55:21 +00003157htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003158 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003159 xmlChar *ExternalID = NULL;
3160 xmlChar *URI = NULL;
3161
3162 /*
3163 * We know that '<!DOCTYPE' has been detected.
3164 */
3165 SKIP(9);
3166
3167 SKIP_BLANKS;
3168
3169 /*
3170 * Parse the DOCTYPE name.
3171 */
3172 name = htmlParseName(ctxt);
3173 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003174 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3175 "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3176 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003177 }
3178 /*
3179 * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3180 */
3181
3182 SKIP_BLANKS;
3183
3184 /*
3185 * Check for SystemID and ExternalID
3186 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003187 URI = htmlParseExternalID(ctxt, &ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003188 SKIP_BLANKS;
3189
3190 /*
3191 * We should be at the end of the DOCTYPE declaration.
3192 */
3193 if (CUR != '>') {
Daniel Veillardf403d292003-10-05 13:51:35 +00003194 htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
3195 "DOCTYPE improperly terminated\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003196 /* We shouldn't try to resynchronize ... */
3197 }
3198 NEXT;
3199
3200 /*
3201 * Create or update the document accordingly to the DOCTYPE
3202 */
3203 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3204 (!ctxt->disableSAX))
3205 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3206
3207 /*
3208 * Cleanup, since we don't use all those identifiers
3209 */
3210 if (URI != NULL) xmlFree(URI);
3211 if (ExternalID != NULL) xmlFree(ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +00003212}
3213
3214/**
3215 * htmlParseAttribute:
3216 * @ctxt: an HTML parser context
3217 * @value: a xmlChar ** used to store the value of the attribute
3218 *
3219 * parse an attribute
3220 *
3221 * [41] Attribute ::= Name Eq AttValue
3222 *
3223 * [25] Eq ::= S? '=' S?
3224 *
3225 * With namespace:
3226 *
3227 * [NS 11] Attribute ::= QName Eq AttValue
3228 *
3229 * Also the case QName == xmlns:??? is handled independently as a namespace
3230 * definition.
3231 *
3232 * Returns the attribute name, and the value in *value.
3233 */
3234
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003235static const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003236htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003237 const xmlChar *name;
3238 xmlChar *val = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003239
3240 *value = NULL;
3241 name = htmlParseHTMLName(ctxt);
3242 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003243 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3244 "error parsing attribute name\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003245 return(NULL);
3246 }
3247
3248 /*
3249 * read the value
3250 */
3251 SKIP_BLANKS;
3252 if (CUR == '=') {
3253 NEXT;
3254 SKIP_BLANKS;
3255 val = htmlParseAttValue(ctxt);
Daniel Veillardc47d2632006-10-17 16:13:27 +00003256 } else if (htmlIsBooleanAttr(name)) {
3257 /*
3258 * assume a minimized attribute
3259 */
3260 val = xmlStrdup(name);
Owen Taylor3473f882001-02-23 17:55:21 +00003261 }
3262
3263 *value = val;
3264 return(name);
3265}
3266
3267/**
3268 * htmlCheckEncoding:
3269 * @ctxt: an HTML parser context
3270 * @attvalue: the attribute value
3271 *
3272 * Checks an http-equiv attribute from a Meta tag to detect
3273 * the encoding
3274 * If a new encoding is detected the parser is switched to decode
3275 * it and pass UTF8
3276 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003277static void
Owen Taylor3473f882001-02-23 17:55:21 +00003278htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3279 const xmlChar *encoding;
3280
3281 if ((ctxt == NULL) || (attvalue == NULL))
3282 return;
3283
3284 /* do not change encoding */
3285 if (ctxt->input->encoding != NULL)
3286 return;
3287
3288 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset=");
3289 if (encoding != NULL) {
3290 encoding += 8;
3291 } else {
3292 encoding = xmlStrcasestr(attvalue, BAD_CAST"charset =");
3293 if (encoding != NULL)
3294 encoding += 9;
3295 }
3296 if (encoding != NULL) {
3297 xmlCharEncoding enc;
3298 xmlCharEncodingHandlerPtr handler;
3299
3300 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3301
3302 if (ctxt->input->encoding != NULL)
3303 xmlFree((xmlChar *) ctxt->input->encoding);
3304 ctxt->input->encoding = xmlStrdup(encoding);
3305
3306 enc = xmlParseCharEncoding((const char *) encoding);
3307 /*
3308 * registered set of known encodings
3309 */
3310 if (enc != XML_CHAR_ENCODING_ERROR) {
Daniel Veillard7e303562006-10-16 13:14:55 +00003311 if (((enc == XML_CHAR_ENCODING_UTF16LE) ||
3312 (enc == XML_CHAR_ENCODING_UTF16BE) ||
3313 (enc == XML_CHAR_ENCODING_UCS4LE) ||
3314 (enc == XML_CHAR_ENCODING_UCS4BE)) &&
3315 (ctxt->input->buf != NULL) &&
3316 (ctxt->input->buf->encoder == NULL)) {
3317 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3318 "htmlCheckEncoding: wrong encoding meta\n",
3319 NULL, NULL);
3320 } else {
3321 xmlSwitchEncoding(ctxt, enc);
3322 }
Owen Taylor3473f882001-02-23 17:55:21 +00003323 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3324 } else {
3325 /*
3326 * fallback for unknown encodings
3327 */
3328 handler = xmlFindCharEncodingHandler((const char *) encoding);
3329 if (handler != NULL) {
3330 xmlSwitchToEncoding(ctxt, handler);
3331 ctxt->charset = XML_CHAR_ENCODING_UTF8;
3332 } else {
3333 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3334 }
3335 }
3336
3337 if ((ctxt->input->buf != NULL) &&
3338 (ctxt->input->buf->encoder != NULL) &&
3339 (ctxt->input->buf->raw != NULL) &&
3340 (ctxt->input->buf->buffer != NULL)) {
3341 int nbchars;
3342 int processed;
3343
3344 /*
3345 * convert as much as possible to the parser reading buffer.
3346 */
3347 processed = ctxt->input->cur - ctxt->input->base;
3348 xmlBufferShrink(ctxt->input->buf->buffer, processed);
3349 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
3350 ctxt->input->buf->buffer,
3351 ctxt->input->buf->raw);
3352 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003353 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3354 "htmlCheckEncoding: encoder error\n",
3355 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003356 }
3357 ctxt->input->base =
3358 ctxt->input->cur = ctxt->input->buf->buffer->content;
3359 }
3360 }
3361}
3362
3363/**
3364 * htmlCheckMeta:
3365 * @ctxt: an HTML parser context
3366 * @atts: the attributes values
3367 *
3368 * Checks an attributes from a Meta tag
3369 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003370static void
Owen Taylor3473f882001-02-23 17:55:21 +00003371htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3372 int i;
3373 const xmlChar *att, *value;
3374 int http = 0;
3375 const xmlChar *content = NULL;
3376
3377 if ((ctxt == NULL) || (atts == NULL))
3378 return;
3379
3380 i = 0;
3381 att = atts[i++];
3382 while (att != NULL) {
3383 value = atts[i++];
3384 if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3385 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3386 http = 1;
3387 else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3388 content = value;
3389 att = atts[i++];
3390 }
3391 if ((http) && (content != NULL))
3392 htmlCheckEncoding(ctxt, content);
3393
3394}
3395
3396/**
3397 * htmlParseStartTag:
3398 * @ctxt: an HTML parser context
3399 *
3400 * parse a start of tag either for rule element or
3401 * EmptyElement. In both case we don't parse the tag closing chars.
3402 *
3403 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3404 *
3405 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3406 *
3407 * With namespace:
3408 *
3409 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3410 *
3411 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3412 *
Daniel Veillard597f1c12005-07-03 23:00:18 +00003413 * Returns 0 in case of success and -1 in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003414 */
3415
Daniel Veillard597f1c12005-07-03 23:00:18 +00003416static int
Owen Taylor3473f882001-02-23 17:55:21 +00003417htmlParseStartTag(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003418 const xmlChar *name;
3419 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00003420 xmlChar *attvalue;
Daniel Veillard30e76072006-03-09 14:13:55 +00003421 const xmlChar **atts;
Owen Taylor3473f882001-02-23 17:55:21 +00003422 int nbatts = 0;
Daniel Veillard30e76072006-03-09 14:13:55 +00003423 int maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003424 int meta = 0;
3425 int i;
3426
Daniel Veillarda03e3652004-11-02 18:45:30 +00003427 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3428 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3429 "htmlParseStartTag: context error\n", NULL, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003430 return -1;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003431 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003432 if (CUR != '<') return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003433 NEXT;
3434
Daniel Veillard30e76072006-03-09 14:13:55 +00003435 atts = ctxt->atts;
3436 maxatts = ctxt->maxatts;
3437
Owen Taylor3473f882001-02-23 17:55:21 +00003438 GROW;
3439 name = htmlParseHTMLName(ctxt);
3440 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003441 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3442 "htmlParseStartTag: invalid element name\n",
3443 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003444 /* Dump the bogus tag like browsers do */
William M. Brack76e95df2003-10-18 16:20:14 +00003445 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
Owen Taylor3473f882001-02-23 17:55:21 +00003446 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003447 return -1;
Owen Taylor3473f882001-02-23 17:55:21 +00003448 }
3449 if (xmlStrEqual(name, BAD_CAST"meta"))
3450 meta = 1;
3451
3452 /*
3453 * Check for auto-closure of HTML elements.
3454 */
3455 htmlAutoClose(ctxt, name);
3456
3457 /*
3458 * Check for implied HTML elements.
3459 */
3460 htmlCheckImplied(ctxt, name);
3461
3462 /*
3463 * Avoid html at any level > 0, head at any level != 1
3464 * or any attempt to recurse body
3465 */
3466 if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003467 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3468 "htmlParseStartTag: misplaced <html> tag\n",
3469 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003470 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003471 }
3472 if ((ctxt->nameNr != 1) &&
3473 (xmlStrEqual(name, BAD_CAST"head"))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003474 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3475 "htmlParseStartTag: misplaced <head> tag\n",
3476 name, NULL);
Daniel Veillard597f1c12005-07-03 23:00:18 +00003477 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003478 }
3479 if (xmlStrEqual(name, BAD_CAST"body")) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003480 int indx;
3481 for (indx = 0;indx < ctxt->nameNr;indx++) {
3482 if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003483 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3484 "htmlParseStartTag: misplaced <body> tag\n",
3485 name, NULL);
Daniel Veillardc59d8262003-11-20 21:59:12 +00003486 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
3487 NEXT;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003488 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003489 }
3490 }
3491 }
3492
3493 /*
3494 * Now parse the attributes, it ends up with the ending
3495 *
3496 * (S Attribute)* S?
3497 */
3498 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003499 while ((IS_CHAR_CH(CUR)) &&
Owen Taylor3473f882001-02-23 17:55:21 +00003500 (CUR != '>') &&
3501 ((CUR != '/') || (NXT(1) != '>'))) {
3502 long cons = ctxt->nbChars;
3503
3504 GROW;
3505 attname = htmlParseAttribute(ctxt, &attvalue);
3506 if (attname != NULL) {
3507
3508 /*
3509 * Well formedness requires at most one declaration of an attribute
3510 */
3511 for (i = 0; i < nbatts;i += 2) {
3512 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003513 htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
3514 "Attribute %s redefined\n", attname, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003515 if (attvalue != NULL)
3516 xmlFree(attvalue);
3517 goto failed;
3518 }
3519 }
3520
3521 /*
3522 * Add the pair to atts
3523 */
3524 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003525 maxatts = 22; /* allow for 10 attrs by default */
3526 atts = (const xmlChar **)
3527 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00003528 if (atts == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003529 htmlErrMemory(ctxt, NULL);
3530 if (attvalue != NULL)
3531 xmlFree(attvalue);
3532 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003533 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003534 ctxt->atts = atts;
3535 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003536 } else if (nbatts + 4 > maxatts) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003537 const xmlChar **n;
3538
Owen Taylor3473f882001-02-23 17:55:21 +00003539 maxatts *= 2;
Daniel Veillardf403d292003-10-05 13:51:35 +00003540 n = (const xmlChar **) xmlRealloc((void *) atts,
3541 maxatts * sizeof(const xmlChar *));
3542 if (n == NULL) {
3543 htmlErrMemory(ctxt, NULL);
3544 if (attvalue != NULL)
3545 xmlFree(attvalue);
3546 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00003547 }
Daniel Veillardf403d292003-10-05 13:51:35 +00003548 atts = n;
3549 ctxt->atts = atts;
3550 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00003551 }
3552 atts[nbatts++] = attname;
3553 atts[nbatts++] = attvalue;
3554 atts[nbatts] = NULL;
3555 atts[nbatts + 1] = NULL;
3556 }
3557 else {
Daniel Veillardf403d292003-10-05 13:51:35 +00003558 if (attvalue != NULL)
3559 xmlFree(attvalue);
Owen Taylor3473f882001-02-23 17:55:21 +00003560 /* Dump the bogus attribute string up to the next blank or
3561 * the end of the tag. */
William M. Brack76e95df2003-10-18 16:20:14 +00003562 while ((IS_CHAR_CH(CUR)) &&
3563 !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
Daniel Veillard34ba3872003-07-15 13:34:05 +00003564 ((CUR != '/') || (NXT(1) != '>')))
Owen Taylor3473f882001-02-23 17:55:21 +00003565 NEXT;
3566 }
3567
3568failed:
3569 SKIP_BLANKS;
3570 if (cons == ctxt->nbChars) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003571 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3572 "htmlParseStartTag: problem parsing attributes\n",
3573 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003574 break;
3575 }
3576 }
3577
3578 /*
3579 * Handle specific association to the META tag
3580 */
William M. Bracke978ae22007-03-21 06:16:02 +00003581 if (meta && (nbatts != 0))
Owen Taylor3473f882001-02-23 17:55:21 +00003582 htmlCheckMeta(ctxt, atts);
3583
3584 /*
3585 * SAX: Start of Element !
3586 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003587 htmlnamePush(ctxt, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003588 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
3589 if (nbatts != 0)
3590 ctxt->sax->startElement(ctxt->userData, name, atts);
3591 else
3592 ctxt->sax->startElement(ctxt->userData, name, NULL);
3593 }
Owen Taylor3473f882001-02-23 17:55:21 +00003594
3595 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003596 for (i = 1;i < nbatts;i += 2) {
Owen Taylor3473f882001-02-23 17:55:21 +00003597 if (atts[i] != NULL)
3598 xmlFree((xmlChar *) atts[i]);
3599 }
Owen Taylor3473f882001-02-23 17:55:21 +00003600 }
Daniel Veillard597f1c12005-07-03 23:00:18 +00003601
3602 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003603}
3604
3605/**
3606 * htmlParseEndTag:
3607 * @ctxt: an HTML parser context
3608 *
3609 * parse an end of tag
3610 *
3611 * [42] ETag ::= '</' Name S? '>'
3612 *
3613 * With namespace
3614 *
3615 * [NS 9] ETag ::= '</' QName S? '>'
Daniel Veillardf420ac52001-07-04 16:04:09 +00003616 *
3617 * Returns 1 if the current level should be closed.
Owen Taylor3473f882001-02-23 17:55:21 +00003618 */
3619
Daniel Veillardf420ac52001-07-04 16:04:09 +00003620static int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003621htmlParseEndTag(htmlParserCtxtPtr ctxt)
3622{
3623 const xmlChar *name;
3624 const xmlChar *oldname;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003625 int i, ret;
Owen Taylor3473f882001-02-23 17:55:21 +00003626
3627 if ((CUR != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003628 htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
3629 "htmlParseEndTag: '</' not found\n", NULL, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003630 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003631 }
3632 SKIP(2);
3633
3634 name = htmlParseHTMLName(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003635 if (name == NULL)
3636 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003637
3638 /*
3639 * We should definitely be at the ending "S? '>'" part
3640 */
3641 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00003642 if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003643 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
3644 "End tag : expected '>'\n", NULL, NULL);
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00003645 if (ctxt->recovery) {
3646 /*
3647 * We're not at the ending > !!
3648 * Error, unless in recover mode where we search forwards
3649 * until we find a >
3650 */
3651 while (CUR != '\0' && CUR != '>') NEXT;
3652 NEXT;
3653 }
Owen Taylor3473f882001-02-23 17:55:21 +00003654 } else
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003655 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003656
3657 /*
3658 * If the name read is not one of the element in the parsing stack
3659 * then return, it's just an error.
3660 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003661 for (i = (ctxt->nameNr - 1); i >= 0; i--) {
3662 if (xmlStrEqual(name, ctxt->nameTab[i]))
3663 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003664 }
3665 if (i < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003666 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3667 "Unexpected end tag : %s\n", name, NULL);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003668 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00003669 }
3670
3671
3672 /*
3673 * Check for auto-closure of HTML elements.
3674 */
3675
3676 htmlAutoCloseOnClose(ctxt, name);
3677
3678 /*
3679 * Well formedness constraints, opening and closing must match.
3680 * With the exception that the autoclose may have popped stuff out
3681 * of the stack.
3682 */
3683 if (!xmlStrEqual(name, ctxt->name)) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003684 if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003685 htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3686 "Opening and ending tag mismatch: %s and %s\n",
3687 name, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00003688 }
3689 }
3690
3691 /*
3692 * SAX: End of Tag
3693 */
3694 oldname = ctxt->name;
3695 if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003696 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3697 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00003698 htmlnamePop(ctxt);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003699 ret = 1;
Daniel Veillardf420ac52001-07-04 16:04:09 +00003700 } else {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003701 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003702 }
3703
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003704 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003705}
3706
3707
3708/**
3709 * htmlParseReference:
3710 * @ctxt: an HTML parser context
3711 *
3712 * parse and handle entity references in content,
3713 * this will end-up in a call to character() since this is either a
3714 * CharRef, or a predefined entity.
3715 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003716static void
Owen Taylor3473f882001-02-23 17:55:21 +00003717htmlParseReference(htmlParserCtxtPtr ctxt) {
Daniel Veillardbb371292001-08-16 23:26:59 +00003718 const htmlEntityDesc * ent;
Owen Taylor3473f882001-02-23 17:55:21 +00003719 xmlChar out[6];
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003720 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003721 if (CUR != '&') return;
3722
3723 if (NXT(1) == '#') {
3724 unsigned int c;
3725 int bits, i = 0;
3726
3727 c = htmlParseCharRef(ctxt);
3728 if (c == 0)
3729 return;
3730
3731 if (c < 0x80) { out[i++]= c; bits= -6; }
3732 else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3733 else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3734 else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3735
3736 for ( ; bits >= 0; bits-= 6) {
3737 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3738 }
3739 out[i] = 0;
3740
3741 htmlCheckParagraph(ctxt);
3742 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3743 ctxt->sax->characters(ctxt->userData, out, i);
3744 } else {
3745 ent = htmlParseEntityRef(ctxt, &name);
3746 if (name == NULL) {
3747 htmlCheckParagraph(ctxt);
3748 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3749 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3750 return;
3751 }
Daniel Veillarde645e8c2002-10-22 17:35:37 +00003752 if ((ent == NULL) || !(ent->value > 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003753 htmlCheckParagraph(ctxt);
3754 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
3755 ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
3756 ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
3757 /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
3758 }
3759 } else {
3760 unsigned int c;
3761 int bits, i = 0;
3762
3763 c = ent->value;
3764 if (c < 0x80)
3765 { out[i++]= c; bits= -6; }
3766 else if (c < 0x800)
3767 { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
3768 else if (c < 0x10000)
3769 { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
3770 else
3771 { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
3772
3773 for ( ; bits >= 0; bits-= 6) {
3774 out[i++]= ((c >> bits) & 0x3F) | 0x80;
3775 }
3776 out[i] = 0;
3777
3778 htmlCheckParagraph(ctxt);
3779 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3780 ctxt->sax->characters(ctxt->userData, out, i);
3781 }
Owen Taylor3473f882001-02-23 17:55:21 +00003782 }
3783}
3784
3785/**
3786 * htmlParseContent:
3787 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00003788 *
3789 * Parse a content: comment, sub-element, reference or text.
Owen Taylor3473f882001-02-23 17:55:21 +00003790 */
3791
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003792static void
Owen Taylor3473f882001-02-23 17:55:21 +00003793htmlParseContent(htmlParserCtxtPtr ctxt) {
3794 xmlChar *currentNode;
3795 int depth;
Daniel Veillard890fd9f2006-10-27 12:53:28 +00003796 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003797
3798 currentNode = xmlStrdup(ctxt->name);
3799 depth = ctxt->nameNr;
3800 while (1) {
3801 long cons = ctxt->nbChars;
3802
3803 GROW;
3804 /*
3805 * Our tag or one of it's parent or children is ending.
3806 */
3807 if ((CUR == '<') && (NXT(1) == '/')) {
Daniel Veillardf420ac52001-07-04 16:04:09 +00003808 if (htmlParseEndTag(ctxt) &&
3809 ((currentNode != NULL) || (ctxt->nameNr == 0))) {
3810 if (currentNode != NULL)
3811 xmlFree(currentNode);
3812 return;
3813 }
3814 continue; /* while */
Owen Taylor3473f882001-02-23 17:55:21 +00003815 }
3816
Daniel Veillard890fd9f2006-10-27 12:53:28 +00003817 else if ((CUR == '<') &&
3818 ((IS_ASCII_LETTER(NXT(1))) ||
3819 (NXT(1) == '_') || (NXT(1) == ':'))) {
3820 name = htmlParseHTMLName_nonInvasive(ctxt);
3821 if (name == NULL) {
3822 htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3823 "htmlParseStartTag: invalid element name\n",
3824 NULL, NULL);
3825 /* Dump the bogus tag like browsers do */
3826 while ((IS_CHAR_CH(CUR)) && (CUR != '>'))
3827 NEXT;
3828
3829 if (currentNode != NULL)
3830 xmlFree(currentNode);
3831 return;
3832 }
3833
3834 if (ctxt->name != NULL) {
3835 if (htmlCheckAutoClose(name, ctxt->name) == 1) {
3836 htmlAutoClose(ctxt, name);
3837 continue;
3838 }
3839 }
3840 }
3841
Owen Taylor3473f882001-02-23 17:55:21 +00003842 /*
3843 * Has this node been popped out during parsing of
3844 * the next element
3845 */
Daniel Veillardf420ac52001-07-04 16:04:09 +00003846 if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
3847 (!xmlStrEqual(currentNode, ctxt->name)))
3848 {
Owen Taylor3473f882001-02-23 17:55:21 +00003849 if (currentNode != NULL) xmlFree(currentNode);
3850 return;
3851 }
3852
Daniel Veillardf9533d12001-03-03 10:04:57 +00003853 if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
3854 (xmlStrEqual(currentNode, BAD_CAST"style")))) {
Owen Taylor3473f882001-02-23 17:55:21 +00003855 /*
3856 * Handle SCRIPT/STYLE separately
3857 */
3858 htmlParseScript(ctxt);
3859 } else {
3860 /*
3861 * Sometimes DOCTYPE arrives in the middle of the document
3862 */
3863 if ((CUR == '<') && (NXT(1) == '!') &&
3864 (UPP(2) == 'D') && (UPP(3) == 'O') &&
3865 (UPP(4) == 'C') && (UPP(5) == 'T') &&
3866 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
3867 (UPP(8) == 'E')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003868 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3869 "Misplaced DOCTYPE declaration\n",
3870 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003871 htmlParseDocTypeDecl(ctxt);
3872 }
3873
3874 /*
3875 * First case : a comment
3876 */
3877 if ((CUR == '<') && (NXT(1) == '!') &&
3878 (NXT(2) == '-') && (NXT(3) == '-')) {
3879 htmlParseComment(ctxt);
3880 }
3881
3882 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003883 * Second case : a Processing Instruction.
3884 */
3885 else if ((CUR == '<') && (NXT(1) == '?')) {
3886 htmlParsePI(ctxt);
3887 }
3888
3889 /*
3890 * Third case : a sub-element.
Owen Taylor3473f882001-02-23 17:55:21 +00003891 */
3892 else if (CUR == '<') {
3893 htmlParseElement(ctxt);
3894 }
3895
3896 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003897 * Fourth case : a reference. If if has not been resolved,
Owen Taylor3473f882001-02-23 17:55:21 +00003898 * parsing returns it's Name, create the node
3899 */
3900 else if (CUR == '&') {
3901 htmlParseReference(ctxt);
3902 }
3903
3904 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00003905 * Fifth case : end of the resource
Owen Taylor3473f882001-02-23 17:55:21 +00003906 */
3907 else if (CUR == 0) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00003908 htmlAutoCloseOnEnd(ctxt);
3909 break;
Owen Taylor3473f882001-02-23 17:55:21 +00003910 }
3911
3912 /*
3913 * Last case, text. Note that References are handled directly.
3914 */
3915 else {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003916 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003917 }
3918
3919 if (cons == ctxt->nbChars) {
3920 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003921 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3922 "detected an error in element content\n",
3923 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003924 }
3925 break;
3926 }
3927 }
3928 GROW;
3929 }
3930 if (currentNode != NULL) xmlFree(currentNode);
3931}
3932
3933/**
Daniel Veillard499cc922006-01-18 17:22:35 +00003934 * htmlParseContent:
3935 * @ctxt: an HTML parser context
3936 *
3937 * Parse a content: comment, sub-element, reference or text.
3938 */
3939
3940void
3941__htmlParseContent(void *ctxt) {
3942 if (ctxt != NULL)
3943 htmlParseContent((htmlParserCtxtPtr) ctxt);
3944}
3945
3946/**
Owen Taylor3473f882001-02-23 17:55:21 +00003947 * htmlParseElement:
3948 * @ctxt: an HTML parser context
3949 *
3950 * parse an HTML element, this is highly recursive
3951 *
3952 * [39] element ::= EmptyElemTag | STag content ETag
3953 *
3954 * [41] Attribute ::= Name Eq AttValue
3955 */
3956
3957void
3958htmlParseElement(htmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003959 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003960 xmlChar *currentNode = NULL;
Daniel Veillardbb371292001-08-16 23:26:59 +00003961 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00003962 htmlParserNodeInfo node_info;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003963 int failed;
Daniel Veillarda03e3652004-11-02 18:45:30 +00003964 int depth;
Daniel Veillard3fbe8e32001-10-06 13:30:33 +00003965 const xmlChar *oldptr;
Owen Taylor3473f882001-02-23 17:55:21 +00003966
Daniel Veillarda03e3652004-11-02 18:45:30 +00003967 if ((ctxt == NULL) || (ctxt->input == NULL)) {
3968 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard597f1c12005-07-03 23:00:18 +00003969 "htmlParseElement: context error\n", NULL, NULL);
Daniel Veillarda03e3652004-11-02 18:45:30 +00003970 return;
3971 }
Owen Taylor3473f882001-02-23 17:55:21 +00003972 /* Capture start position */
3973 if (ctxt->record_info) {
3974 node_info.begin_pos = ctxt->input->consumed +
3975 (CUR_PTR - ctxt->input->base);
3976 node_info.begin_line = ctxt->input->line;
3977 }
3978
Daniel Veillard597f1c12005-07-03 23:00:18 +00003979 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003980 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00003981 if (failed || (name == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003982 if (CUR == '>')
3983 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00003984 return;
3985 }
Owen Taylor3473f882001-02-23 17:55:21 +00003986
3987 /*
3988 * Lookup the info for that element.
3989 */
3990 info = htmlTagLookup(name);
3991 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003992 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
3993 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003994 }
3995
3996 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003997 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00003998 */
3999 if ((CUR == '/') && (NXT(1) == '>')) {
4000 SKIP(2);
4001 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4002 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00004003 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004004 return;
4005 }
4006
4007 if (CUR == '>') {
4008 NEXT;
4009 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004010 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4011 "Couldn't find end of Start Tag %s\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004012
4013 /*
4014 * end of parsing of this node.
4015 */
4016 if (xmlStrEqual(name, ctxt->name)) {
4017 nodePop(ctxt);
Daniel Veillardf403d292003-10-05 13:51:35 +00004018 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004019 }
4020
4021 /*
4022 * Capture end position and add node
4023 */
Daniel Veillard30e76072006-03-09 14:13:55 +00004024 if (ctxt->record_info) {
Owen Taylor3473f882001-02-23 17:55:21 +00004025 node_info.end_pos = ctxt->input->consumed +
4026 (CUR_PTR - ctxt->input->base);
4027 node_info.end_line = ctxt->input->line;
4028 node_info.node = ctxt->node;
4029 xmlParserAddNodeInfo(ctxt, &node_info);
4030 }
4031 return;
4032 }
4033
4034 /*
4035 * Check for an Empty Element from DTD definition
4036 */
4037 if ((info != NULL) && (info->empty)) {
4038 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4039 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillardf403d292003-10-05 13:51:35 +00004040 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004041 return;
4042 }
4043
4044 /*
4045 * Parse the content of the element:
4046 */
4047 currentNode = xmlStrdup(ctxt->name);
4048 depth = ctxt->nameNr;
William M. Brack76e95df2003-10-18 16:20:14 +00004049 while (IS_CHAR_CH(CUR)) {
William M. Brackd28e48a2001-09-23 01:55:08 +00004050 oldptr = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004051 htmlParseContent(ctxt);
William M. Brackd28e48a2001-09-23 01:55:08 +00004052 if (oldptr==ctxt->input->cur) break;
Owen Taylor3473f882001-02-23 17:55:21 +00004053 if (ctxt->nameNr < depth) break;
4054 }
4055
Owen Taylor3473f882001-02-23 17:55:21 +00004056 /*
4057 * Capture end position and add node
4058 */
4059 if ( currentNode != NULL && ctxt->record_info ) {
4060 node_info.end_pos = ctxt->input->consumed +
4061 (CUR_PTR - ctxt->input->base);
4062 node_info.end_line = ctxt->input->line;
4063 node_info.node = ctxt->node;
4064 xmlParserAddNodeInfo(ctxt, &node_info);
4065 }
William M. Brack76e95df2003-10-18 16:20:14 +00004066 if (!IS_CHAR_CH(CUR)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004067 htmlAutoCloseOnEnd(ctxt);
4068 }
4069
Owen Taylor3473f882001-02-23 17:55:21 +00004070 if (currentNode != NULL)
4071 xmlFree(currentNode);
4072}
4073
4074/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004075 * htmlParseDocument:
Owen Taylor3473f882001-02-23 17:55:21 +00004076 * @ctxt: an HTML parser context
4077 *
4078 * parse an HTML document (and build a tree if using the standard SAX
4079 * interface).
4080 *
4081 * Returns 0, -1 in case of error. the parser context is augmented
4082 * as a result of the parsing.
4083 */
4084
Daniel Veillard1b31e4a2002-05-27 14:44:50 +00004085int
Owen Taylor3473f882001-02-23 17:55:21 +00004086htmlParseDocument(htmlParserCtxtPtr ctxt) {
4087 xmlDtdPtr dtd;
4088
Daniel Veillardd0463562001-10-13 09:15:48 +00004089 xmlInitParser();
4090
Owen Taylor3473f882001-02-23 17:55:21 +00004091 htmlDefaultSAXHandlerInit();
Owen Taylor3473f882001-02-23 17:55:21 +00004092
Daniel Veillarda03e3652004-11-02 18:45:30 +00004093 if ((ctxt == NULL) || (ctxt->input == NULL)) {
4094 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4095 "htmlParseDocument: context error\n", NULL, NULL);
4096 return(XML_ERR_INTERNAL_ERROR);
4097 }
4098 ctxt->html = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00004099 GROW;
4100 /*
4101 * SAX: beginning of the document processing.
4102 */
4103 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4104 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4105
4106 /*
4107 * Wipe out everything which is before the first '<'
4108 */
4109 SKIP_BLANKS;
4110 if (CUR == 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004111 htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
4112 "Document is empty\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004113 }
4114
4115 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4116 ctxt->sax->startDocument(ctxt->userData);
4117
4118
4119 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004120 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004121 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004122 while (((CUR == '<') && (NXT(1) == '!') &&
4123 (NXT(2) == '-') && (NXT(3) == '-')) ||
4124 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004125 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004126 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004127 SKIP_BLANKS;
4128 }
4129
4130
4131 /*
4132 * Then possibly doc type declaration(s) and more Misc
4133 * (doctypedecl Misc*)?
4134 */
4135 if ((CUR == '<') && (NXT(1) == '!') &&
4136 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4137 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4138 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4139 (UPP(8) == 'E')) {
4140 htmlParseDocTypeDecl(ctxt);
4141 }
4142 SKIP_BLANKS;
4143
4144 /*
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004145 * Parse possible comments and PIs before any content
Owen Taylor3473f882001-02-23 17:55:21 +00004146 */
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004147 while (((CUR == '<') && (NXT(1) == '!') &&
4148 (NXT(2) == '-') && (NXT(3) == '-')) ||
4149 ((CUR == '<') && (NXT(1) == '?'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00004150 htmlParseComment(ctxt);
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004151 htmlParsePI(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004152 SKIP_BLANKS;
4153 }
4154
4155 /*
4156 * Time to start parsing the tree itself
4157 */
4158 htmlParseContent(ctxt);
4159
4160 /*
4161 * autoclose
4162 */
4163 if (CUR == 0)
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004164 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004165
4166
4167 /*
4168 * SAX: end of the document processing.
4169 */
4170 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4171 ctxt->sax->endDocument(ctxt->userData);
4172
4173 if (ctxt->myDoc != NULL) {
4174 dtd = xmlGetIntSubset(ctxt->myDoc);
4175 if (dtd == NULL)
4176 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00004177 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00004178 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
4179 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
4180 }
4181 if (! ctxt->wellFormed) return(-1);
4182 return(0);
4183}
4184
4185
4186/************************************************************************
4187 * *
4188 * Parser contexts handling *
4189 * *
4190 ************************************************************************/
4191
4192/**
William M. Brackedb65a72004-02-06 07:36:04 +00004193 * htmlInitParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004194 * @ctxt: an HTML parser context
4195 *
4196 * Initialize a parser context
Daniel Veillardf403d292003-10-05 13:51:35 +00004197 *
4198 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00004199 */
4200
Daniel Veillardf403d292003-10-05 13:51:35 +00004201static int
Owen Taylor3473f882001-02-23 17:55:21 +00004202htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
4203{
4204 htmlSAXHandler *sax;
4205
Daniel Veillardf403d292003-10-05 13:51:35 +00004206 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004207 memset(ctxt, 0, sizeof(htmlParserCtxt));
4208
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004209 ctxt->dict = xmlDictCreate();
4210 if (ctxt->dict == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004211 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4212 return(-1);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004213 }
Owen Taylor3473f882001-02-23 17:55:21 +00004214 sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
4215 if (sax == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004216 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
4217 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004218 }
4219 else
4220 memset(sax, 0, sizeof(htmlSAXHandler));
4221
4222 /* Allocate the Input stack */
4223 ctxt->inputTab = (htmlParserInputPtr *)
4224 xmlMalloc(5 * sizeof(htmlParserInputPtr));
4225 if (ctxt->inputTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004226 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004227 ctxt->inputNr = 0;
4228 ctxt->inputMax = 0;
4229 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004230 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004231 }
4232 ctxt->inputNr = 0;
4233 ctxt->inputMax = 5;
4234 ctxt->input = NULL;
4235 ctxt->version = NULL;
4236 ctxt->encoding = NULL;
4237 ctxt->standalone = -1;
4238 ctxt->instate = XML_PARSER_START;
4239
4240 /* Allocate the Node stack */
4241 ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
4242 if (ctxt->nodeTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004243 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004244 ctxt->nodeNr = 0;
4245 ctxt->nodeMax = 0;
4246 ctxt->node = NULL;
4247 ctxt->inputNr = 0;
4248 ctxt->inputMax = 0;
4249 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004250 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004251 }
4252 ctxt->nodeNr = 0;
4253 ctxt->nodeMax = 10;
4254 ctxt->node = NULL;
4255
4256 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004257 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00004258 if (ctxt->nameTab == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004259 htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004260 ctxt->nameNr = 0;
4261 ctxt->nameMax = 10;
4262 ctxt->name = NULL;
4263 ctxt->nodeNr = 0;
4264 ctxt->nodeMax = 0;
4265 ctxt->node = NULL;
4266 ctxt->inputNr = 0;
4267 ctxt->inputMax = 0;
4268 ctxt->input = NULL;
Daniel Veillardf403d292003-10-05 13:51:35 +00004269 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00004270 }
4271 ctxt->nameNr = 0;
4272 ctxt->nameMax = 10;
4273 ctxt->name = NULL;
4274
Daniel Veillard092643b2003-09-25 14:29:29 +00004275 if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
Owen Taylor3473f882001-02-23 17:55:21 +00004276 else {
4277 ctxt->sax = sax;
Daniel Veillard092643b2003-09-25 14:29:29 +00004278 memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +00004279 }
4280 ctxt->userData = ctxt;
4281 ctxt->myDoc = NULL;
4282 ctxt->wellFormed = 1;
4283 ctxt->replaceEntities = 0;
Daniel Veillard635ef722001-10-29 11:48:19 +00004284 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00004285 ctxt->html = 1;
Daniel Veillardeff45a92004-10-29 12:10:55 +00004286 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
William M. Brackedb65a72004-02-06 07:36:04 +00004287 ctxt->vctxt.userData = ctxt;
4288 ctxt->vctxt.error = xmlParserValidityError;
4289 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00004290 ctxt->record_info = 0;
4291 ctxt->validate = 0;
4292 ctxt->nbChars = 0;
4293 ctxt->checkIndex = 0;
Daniel Veillarddc2cee22001-08-22 16:30:37 +00004294 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004295 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillardf403d292003-10-05 13:51:35 +00004296 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00004297}
4298
4299/**
4300 * htmlFreeParserCtxt:
4301 * @ctxt: an HTML parser context
4302 *
4303 * Free all the memory used by a parser context. However the parsed
4304 * document in ctxt->myDoc is not freed.
4305 */
4306
4307void
4308htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
4309{
4310 xmlFreeParserCtxt(ctxt);
4311}
4312
4313/**
Daniel Veillard1d995272002-07-22 16:43:32 +00004314 * htmlNewParserCtxt:
4315 *
4316 * Allocate and initialize a new parser context.
4317 *
Daniel Veillard34c647c2006-09-21 06:53:59 +00004318 * Returns the htmlParserCtxtPtr or NULL in case of allocation error
Daniel Veillard1d995272002-07-22 16:43:32 +00004319 */
4320
Daniel Veillard34c647c2006-09-21 06:53:59 +00004321htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004322htmlNewParserCtxt(void)
4323{
4324 xmlParserCtxtPtr ctxt;
4325
4326 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
4327 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004328 htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
Daniel Veillard1d995272002-07-22 16:43:32 +00004329 return(NULL);
4330 }
4331 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillardf403d292003-10-05 13:51:35 +00004332 if (htmlInitParserCtxt(ctxt) < 0) {
4333 htmlFreeParserCtxt(ctxt);
4334 return(NULL);
4335 }
Daniel Veillard1d995272002-07-22 16:43:32 +00004336 return(ctxt);
4337}
4338
4339/**
4340 * htmlCreateMemoryParserCtxt:
4341 * @buffer: a pointer to a char array
4342 * @size: the size of the array
4343 *
4344 * Create a parser context for an HTML in-memory document.
4345 *
4346 * Returns the new parser context or NULL
4347 */
Daniel Veillard02ea1412003-04-09 12:08:47 +00004348htmlParserCtxtPtr
Daniel Veillard1d995272002-07-22 16:43:32 +00004349htmlCreateMemoryParserCtxt(const char *buffer, int size) {
4350 xmlParserCtxtPtr ctxt;
4351 xmlParserInputPtr input;
4352 xmlParserInputBufferPtr buf;
4353
4354 if (buffer == NULL)
4355 return(NULL);
4356 if (size <= 0)
4357 return(NULL);
4358
4359 ctxt = htmlNewParserCtxt();
4360 if (ctxt == NULL)
4361 return(NULL);
4362
4363 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
4364 if (buf == NULL) return(NULL);
4365
4366 input = xmlNewInputStream(ctxt);
4367 if (input == NULL) {
4368 xmlFreeParserCtxt(ctxt);
4369 return(NULL);
4370 }
4371
4372 input->filename = NULL;
4373 input->buf = buf;
4374 input->base = input->buf->buffer->content;
4375 input->cur = input->buf->buffer->content;
4376 input->end = &input->buf->buffer->content[input->buf->buffer->use];
4377
4378 inputPush(ctxt, input);
4379 return(ctxt);
4380}
4381
4382/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004383 * htmlCreateDocParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00004384 * @cur: a pointer to an array of xmlChar
4385 * @encoding: a free form C string describing the HTML document encoding, or NULL
4386 *
4387 * Create a parser context for an HTML document.
4388 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004389 * TODO: check the need to add encoding handling there
4390 *
Owen Taylor3473f882001-02-23 17:55:21 +00004391 * Returns the new parser context or NULL
4392 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004393static htmlParserCtxtPtr
Daniel Veillard4d1320f2007-04-26 08:55:33 +00004394htmlCreateDocParserCtxt(const xmlChar *cur, const char *encoding) {
Daniel Veillard1d995272002-07-22 16:43:32 +00004395 int len;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004396 htmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00004397
Daniel Veillard1d995272002-07-22 16:43:32 +00004398 if (cur == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00004399 return(NULL);
Daniel Veillard1d995272002-07-22 16:43:32 +00004400 len = xmlStrlen(cur);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004401 ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
Daniel Veillard739e9d02007-04-27 09:33:58 +00004402 if (ctxt == NULL)
4403 return(NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004404
4405 if (encoding != NULL) {
4406 xmlCharEncoding enc;
4407 xmlCharEncodingHandlerPtr handler;
4408
4409 if (ctxt->input->encoding != NULL)
4410 xmlFree((xmlChar *) ctxt->input->encoding);
Daniel Veillarde8ed6202003-08-14 23:39:01 +00004411 ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004412
4413 enc = xmlParseCharEncoding(encoding);
4414 /*
4415 * registered set of known encodings
4416 */
4417 if (enc != XML_CHAR_ENCODING_ERROR) {
4418 xmlSwitchEncoding(ctxt, enc);
4419 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004420 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4421 "Unsupported encoding %s\n",
4422 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004423 }
4424 } else {
4425 /*
4426 * fallback for unknown encodings
4427 */
4428 handler = xmlFindCharEncodingHandler((const char *) encoding);
4429 if (handler != NULL) {
4430 xmlSwitchToEncoding(ctxt, handler);
4431 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004432 htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
4433 "Unsupported encoding %s\n",
4434 (const xmlChar *) encoding, NULL);
Daniel Veillarde5b110b2003-02-04 14:43:39 +00004435 }
4436 }
4437 }
4438 return(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004439}
4440
Daniel Veillard73b013f2003-09-30 12:36:01 +00004441#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004442/************************************************************************
4443 * *
4444 * Progressive parsing interfaces *
4445 * *
4446 ************************************************************************/
4447
4448/**
4449 * htmlParseLookupSequence:
4450 * @ctxt: an HTML parser context
4451 * @first: the first char to lookup
4452 * @next: the next char to lookup or zero
4453 * @third: the next char to lookup or zero
William M. Brack78637da2003-07-31 14:47:38 +00004454 * @comment: flag to force checking inside comments
Owen Taylor3473f882001-02-23 17:55:21 +00004455 *
4456 * Try to find if a sequence (first, next, third) or just (first next) or
4457 * (first) is available in the input stream.
4458 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
4459 * to avoid rescanning sequences of bytes, it DOES change the state of the
4460 * parser, do not use liberally.
4461 * This is basically similar to xmlParseLookupSequence()
4462 *
4463 * Returns the index to the current parsing point if the full sequence
4464 * is available, -1 otherwise.
4465 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004466static int
Owen Taylor3473f882001-02-23 17:55:21 +00004467htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
William M. Brackc1939562003-08-05 15:52:22 +00004468 xmlChar next, xmlChar third, int iscomment) {
Owen Taylor3473f882001-02-23 17:55:21 +00004469 int base, len;
4470 htmlParserInputPtr in;
4471 const xmlChar *buf;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004472 int incomment = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004473
4474 in = ctxt->input;
4475 if (in == NULL) return(-1);
4476 base = in->cur - in->base;
4477 if (base < 0) return(-1);
4478 if (ctxt->checkIndex > base)
4479 base = ctxt->checkIndex;
4480 if (in->buf == NULL) {
4481 buf = in->base;
4482 len = in->length;
4483 } else {
4484 buf = in->buf->buffer->content;
4485 len = in->buf->buffer->use;
4486 }
4487 /* take into account the sequence length */
4488 if (third) len -= 2;
4489 else if (next) len --;
4490 for (;base < len;base++) {
William M. Brackc1939562003-08-05 15:52:22 +00004491 if (!incomment && (base + 4 < len) && !iscomment) {
Daniel Veillardc1f78342001-11-10 11:43:05 +00004492 if ((buf[base] == '<') && (buf[base + 1] == '!') &&
4493 (buf[base + 2] == '-') && (buf[base + 3] == '-')) {
4494 incomment = 1;
Daniel Veillard97e01882003-07-30 18:59:19 +00004495 /* do not increment past <! - some people use <!--> */
4496 base += 2;
Daniel Veillardc1f78342001-11-10 11:43:05 +00004497 }
Daniel Veillardc1f78342001-11-10 11:43:05 +00004498 }
4499 if (incomment) {
William M. Brack4a557d92003-07-29 04:28:04 +00004500 if (base + 3 > len)
Daniel Veillardc1f78342001-11-10 11:43:05 +00004501 return(-1);
4502 if ((buf[base] == '-') && (buf[base + 1] == '-') &&
4503 (buf[base + 2] == '>')) {
4504 incomment = 0;
4505 base += 2;
4506 }
4507 continue;
4508 }
Owen Taylor3473f882001-02-23 17:55:21 +00004509 if (buf[base] == first) {
4510 if (third != 0) {
4511 if ((buf[base + 1] != next) ||
4512 (buf[base + 2] != third)) continue;
4513 } else if (next != 0) {
4514 if (buf[base + 1] != next) continue;
4515 }
4516 ctxt->checkIndex = 0;
4517#ifdef DEBUG_PUSH
4518 if (next == 0)
4519 xmlGenericError(xmlGenericErrorContext,
4520 "HPP: lookup '%c' found at %d\n",
4521 first, base);
4522 else if (third == 0)
4523 xmlGenericError(xmlGenericErrorContext,
4524 "HPP: lookup '%c%c' found at %d\n",
4525 first, next, base);
4526 else
4527 xmlGenericError(xmlGenericErrorContext,
4528 "HPP: lookup '%c%c%c' found at %d\n",
4529 first, next, third, base);
4530#endif
4531 return(base - (in->cur - in->base));
4532 }
4533 }
4534 ctxt->checkIndex = base;
4535#ifdef DEBUG_PUSH
4536 if (next == 0)
4537 xmlGenericError(xmlGenericErrorContext,
4538 "HPP: lookup '%c' failed\n", first);
4539 else if (third == 0)
4540 xmlGenericError(xmlGenericErrorContext,
4541 "HPP: lookup '%c%c' failed\n", first, next);
4542 else
4543 xmlGenericError(xmlGenericErrorContext,
4544 "HPP: lookup '%c%c%c' failed\n", first, next, third);
4545#endif
4546 return(-1);
4547}
4548
4549/**
4550 * htmlParseTryOrFinish:
4551 * @ctxt: an HTML parser context
4552 * @terminate: last chunk indicator
4553 *
4554 * Try to progress on parsing
4555 *
4556 * Returns zero if no parsing was possible
4557 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004558static int
Owen Taylor3473f882001-02-23 17:55:21 +00004559htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
4560 int ret = 0;
4561 htmlParserInputPtr in;
4562 int avail = 0;
4563 xmlChar cur, next;
4564
4565#ifdef DEBUG_PUSH
4566 switch (ctxt->instate) {
4567 case XML_PARSER_EOF:
4568 xmlGenericError(xmlGenericErrorContext,
4569 "HPP: try EOF\n"); break;
4570 case XML_PARSER_START:
4571 xmlGenericError(xmlGenericErrorContext,
4572 "HPP: try START\n"); break;
4573 case XML_PARSER_MISC:
4574 xmlGenericError(xmlGenericErrorContext,
4575 "HPP: try MISC\n");break;
4576 case XML_PARSER_COMMENT:
4577 xmlGenericError(xmlGenericErrorContext,
4578 "HPP: try COMMENT\n");break;
4579 case XML_PARSER_PROLOG:
4580 xmlGenericError(xmlGenericErrorContext,
4581 "HPP: try PROLOG\n");break;
4582 case XML_PARSER_START_TAG:
4583 xmlGenericError(xmlGenericErrorContext,
4584 "HPP: try START_TAG\n");break;
4585 case XML_PARSER_CONTENT:
4586 xmlGenericError(xmlGenericErrorContext,
4587 "HPP: try CONTENT\n");break;
4588 case XML_PARSER_CDATA_SECTION:
4589 xmlGenericError(xmlGenericErrorContext,
4590 "HPP: try CDATA_SECTION\n");break;
4591 case XML_PARSER_END_TAG:
4592 xmlGenericError(xmlGenericErrorContext,
4593 "HPP: try END_TAG\n");break;
4594 case XML_PARSER_ENTITY_DECL:
4595 xmlGenericError(xmlGenericErrorContext,
4596 "HPP: try ENTITY_DECL\n");break;
4597 case XML_PARSER_ENTITY_VALUE:
4598 xmlGenericError(xmlGenericErrorContext,
4599 "HPP: try ENTITY_VALUE\n");break;
4600 case XML_PARSER_ATTRIBUTE_VALUE:
4601 xmlGenericError(xmlGenericErrorContext,
4602 "HPP: try ATTRIBUTE_VALUE\n");break;
4603 case XML_PARSER_DTD:
4604 xmlGenericError(xmlGenericErrorContext,
4605 "HPP: try DTD\n");break;
4606 case XML_PARSER_EPILOG:
4607 xmlGenericError(xmlGenericErrorContext,
4608 "HPP: try EPILOG\n");break;
4609 case XML_PARSER_PI:
4610 xmlGenericError(xmlGenericErrorContext,
4611 "HPP: try PI\n");break;
4612 case XML_PARSER_SYSTEM_LITERAL:
4613 xmlGenericError(xmlGenericErrorContext,
4614 "HPP: try SYSTEM_LITERAL\n");break;
4615 }
4616#endif
4617
4618 while (1) {
4619
4620 in = ctxt->input;
4621 if (in == NULL) break;
4622 if (in->buf == NULL)
4623 avail = in->length - (in->cur - in->base);
4624 else
4625 avail = in->buf->buffer->use - (in->cur - in->base);
4626 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00004627 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004628 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
4629 /*
4630 * SAX: end of the document processing.
4631 */
4632 ctxt->instate = XML_PARSER_EOF;
4633 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4634 ctxt->sax->endDocument(ctxt->userData);
4635 }
4636 }
4637 if (avail < 1)
4638 goto done;
Daniel Veillard45269b82003-04-22 13:21:57 +00004639 cur = in->cur[0];
4640 if (cur == 0) {
4641 SKIP(1);
4642 continue;
4643 }
4644
Owen Taylor3473f882001-02-23 17:55:21 +00004645 switch (ctxt->instate) {
4646 case XML_PARSER_EOF:
4647 /*
4648 * Document parsing is done !
4649 */
4650 goto done;
4651 case XML_PARSER_START:
4652 /*
4653 * Very first chars read from the document flow.
4654 */
4655 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004656 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004657 SKIP_BLANKS;
4658 if (in->buf == NULL)
4659 avail = in->length - (in->cur - in->base);
4660 else
4661 avail = in->buf->buffer->use - (in->cur - in->base);
4662 }
4663 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4664 ctxt->sax->setDocumentLocator(ctxt->userData,
4665 &xmlDefaultSAXLocator);
4666 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
4667 (!ctxt->disableSAX))
4668 ctxt->sax->startDocument(ctxt->userData);
4669
4670 cur = in->cur[0];
4671 next = in->cur[1];
4672 if ((cur == '<') && (next == '!') &&
4673 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4674 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4675 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4676 (UPP(8) == 'E')) {
4677 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004678 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004679 goto done;
4680#ifdef DEBUG_PUSH
4681 xmlGenericError(xmlGenericErrorContext,
4682 "HPP: Parsing internal subset\n");
4683#endif
4684 htmlParseDocTypeDecl(ctxt);
4685 ctxt->instate = XML_PARSER_PROLOG;
4686#ifdef DEBUG_PUSH
4687 xmlGenericError(xmlGenericErrorContext,
4688 "HPP: entering PROLOG\n");
4689#endif
4690 } else {
4691 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004692#ifdef DEBUG_PUSH
Daniel Veillard597f1c12005-07-03 23:00:18 +00004693 xmlGenericError(xmlGenericErrorContext,
4694 "HPP: entering MISC\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004695#endif
Daniel Veillard597f1c12005-07-03 23:00:18 +00004696 }
Owen Taylor3473f882001-02-23 17:55:21 +00004697 break;
4698 case XML_PARSER_MISC:
4699 SKIP_BLANKS;
4700 if (in->buf == NULL)
4701 avail = in->length - (in->cur - in->base);
4702 else
4703 avail = in->buf->buffer->use - (in->cur - in->base);
4704 if (avail < 2)
4705 goto done;
4706 cur = in->cur[0];
4707 next = in->cur[1];
4708 if ((cur == '<') && (next == '!') &&
4709 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4710 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004711 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004712 goto done;
4713#ifdef DEBUG_PUSH
4714 xmlGenericError(xmlGenericErrorContext,
4715 "HPP: Parsing Comment\n");
4716#endif
4717 htmlParseComment(ctxt);
4718 ctxt->instate = XML_PARSER_MISC;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004719 } else if ((cur == '<') && (next == '?')) {
4720 if ((!terminate) &&
4721 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4722 goto done;
4723#ifdef DEBUG_PUSH
4724 xmlGenericError(xmlGenericErrorContext,
4725 "HPP: Parsing PI\n");
4726#endif
4727 htmlParsePI(ctxt);
4728 ctxt->instate = XML_PARSER_MISC;
Owen Taylor3473f882001-02-23 17:55:21 +00004729 } else if ((cur == '<') && (next == '!') &&
4730 (UPP(2) == 'D') && (UPP(3) == 'O') &&
4731 (UPP(4) == 'C') && (UPP(5) == 'T') &&
4732 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4733 (UPP(8) == 'E')) {
4734 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004735 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004736 goto done;
4737#ifdef DEBUG_PUSH
4738 xmlGenericError(xmlGenericErrorContext,
4739 "HPP: Parsing internal subset\n");
4740#endif
4741 htmlParseDocTypeDecl(ctxt);
4742 ctxt->instate = XML_PARSER_PROLOG;
4743#ifdef DEBUG_PUSH
4744 xmlGenericError(xmlGenericErrorContext,
4745 "HPP: entering PROLOG\n");
4746#endif
4747 } else if ((cur == '<') && (next == '!') &&
4748 (avail < 9)) {
4749 goto done;
4750 } else {
4751 ctxt->instate = XML_PARSER_START_TAG;
4752#ifdef DEBUG_PUSH
4753 xmlGenericError(xmlGenericErrorContext,
4754 "HPP: entering START_TAG\n");
4755#endif
4756 }
4757 break;
4758 case XML_PARSER_PROLOG:
4759 SKIP_BLANKS;
4760 if (in->buf == NULL)
4761 avail = in->length - (in->cur - in->base);
4762 else
4763 avail = in->buf->buffer->use - (in->cur - in->base);
4764 if (avail < 2)
4765 goto done;
4766 cur = in->cur[0];
4767 next = in->cur[1];
4768 if ((cur == '<') && (next == '!') &&
4769 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4770 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004771 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004772 goto done;
4773#ifdef DEBUG_PUSH
4774 xmlGenericError(xmlGenericErrorContext,
4775 "HPP: Parsing Comment\n");
4776#endif
4777 htmlParseComment(ctxt);
4778 ctxt->instate = XML_PARSER_PROLOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004779 } else if ((cur == '<') && (next == '?')) {
4780 if ((!terminate) &&
4781 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4782 goto done;
4783#ifdef DEBUG_PUSH
4784 xmlGenericError(xmlGenericErrorContext,
4785 "HPP: Parsing PI\n");
4786#endif
4787 htmlParsePI(ctxt);
4788 ctxt->instate = XML_PARSER_PROLOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004789 } else if ((cur == '<') && (next == '!') &&
4790 (avail < 4)) {
4791 goto done;
4792 } else {
4793 ctxt->instate = XML_PARSER_START_TAG;
4794#ifdef DEBUG_PUSH
4795 xmlGenericError(xmlGenericErrorContext,
4796 "HPP: entering START_TAG\n");
4797#endif
4798 }
4799 break;
4800 case XML_PARSER_EPILOG:
4801 if (in->buf == NULL)
4802 avail = in->length - (in->cur - in->base);
4803 else
4804 avail = in->buf->buffer->use - (in->cur - in->base);
4805 if (avail < 1)
4806 goto done;
4807 cur = in->cur[0];
William M. Brack76e95df2003-10-18 16:20:14 +00004808 if (IS_BLANK_CH(cur)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004809 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004810 goto done;
4811 }
4812 if (avail < 2)
4813 goto done;
4814 next = in->cur[1];
4815 if ((cur == '<') && (next == '!') &&
4816 (in->cur[2] == '-') && (in->cur[3] == '-')) {
4817 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004818 (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004819 goto done;
4820#ifdef DEBUG_PUSH
4821 xmlGenericError(xmlGenericErrorContext,
4822 "HPP: Parsing Comment\n");
4823#endif
4824 htmlParseComment(ctxt);
4825 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00004826 } else if ((cur == '<') && (next == '?')) {
4827 if ((!terminate) &&
4828 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
4829 goto done;
4830#ifdef DEBUG_PUSH
4831 xmlGenericError(xmlGenericErrorContext,
4832 "HPP: Parsing PI\n");
4833#endif
4834 htmlParsePI(ctxt);
4835 ctxt->instate = XML_PARSER_EPILOG;
Owen Taylor3473f882001-02-23 17:55:21 +00004836 } else if ((cur == '<') && (next == '!') &&
4837 (avail < 4)) {
4838 goto done;
4839 } else {
4840 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00004841 ctxt->wellFormed = 0;
4842 ctxt->instate = XML_PARSER_EOF;
4843#ifdef DEBUG_PUSH
4844 xmlGenericError(xmlGenericErrorContext,
4845 "HPP: entering EOF\n");
4846#endif
4847 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4848 ctxt->sax->endDocument(ctxt->userData);
4849 goto done;
4850 }
4851 break;
4852 case XML_PARSER_START_TAG: {
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004853 const xmlChar *name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004854 int failed;
Daniel Veillardbb371292001-08-16 23:26:59 +00004855 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +00004856
4857 if (avail < 2)
4858 goto done;
4859 cur = in->cur[0];
4860 if (cur != '<') {
4861 ctxt->instate = XML_PARSER_CONTENT;
4862#ifdef DEBUG_PUSH
4863 xmlGenericError(xmlGenericErrorContext,
4864 "HPP: entering CONTENT\n");
4865#endif
4866 break;
4867 }
Daniel Veillardf69bb4b2001-05-19 13:24:56 +00004868 if (in->cur[1] == '/') {
4869 ctxt->instate = XML_PARSER_END_TAG;
4870 ctxt->checkIndex = 0;
4871#ifdef DEBUG_PUSH
4872 xmlGenericError(xmlGenericErrorContext,
4873 "HPP: entering END_TAG\n");
4874#endif
4875 break;
4876 }
Owen Taylor3473f882001-02-23 17:55:21 +00004877 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00004878 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004879 goto done;
4880
Daniel Veillard597f1c12005-07-03 23:00:18 +00004881 failed = htmlParseStartTag(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004882 name = ctxt->name;
Daniel Veillard597f1c12005-07-03 23:00:18 +00004883 if (failed ||
Owen Taylor3473f882001-02-23 17:55:21 +00004884 (name == NULL)) {
4885 if (CUR == '>')
4886 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00004887 break;
4888 }
Owen Taylor3473f882001-02-23 17:55:21 +00004889
4890 /*
4891 * Lookup the info for that element.
4892 */
4893 info = htmlTagLookup(name);
4894 if (info == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004895 htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4896 "Tag %s invalid\n", name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004897 }
4898
4899 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004900 * Check for an Empty Element labeled the XML/SGML way
Owen Taylor3473f882001-02-23 17:55:21 +00004901 */
4902 if ((CUR == '/') && (NXT(1) == '>')) {
4903 SKIP(2);
4904 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4905 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004906 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004907 ctxt->instate = XML_PARSER_CONTENT;
4908#ifdef DEBUG_PUSH
4909 xmlGenericError(xmlGenericErrorContext,
4910 "HPP: entering CONTENT\n");
4911#endif
4912 break;
4913 }
4914
4915 if (CUR == '>') {
4916 NEXT;
4917 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004918 htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4919 "Couldn't find end of Start Tag %s\n",
4920 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004921
4922 /*
4923 * end of parsing of this node.
4924 */
4925 if (xmlStrEqual(name, ctxt->name)) {
4926 nodePop(ctxt);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004927 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004928 }
4929
4930 ctxt->instate = XML_PARSER_CONTENT;
4931#ifdef DEBUG_PUSH
4932 xmlGenericError(xmlGenericErrorContext,
4933 "HPP: entering CONTENT\n");
4934#endif
4935 break;
4936 }
4937
4938 /*
4939 * Check for an Empty Element from DTD definition
4940 */
4941 if ((info != NULL) && (info->empty)) {
4942 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4943 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard6a0baa02005-12-10 11:11:12 +00004944 htmlnamePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004945 }
4946 ctxt->instate = XML_PARSER_CONTENT;
4947#ifdef DEBUG_PUSH
4948 xmlGenericError(xmlGenericErrorContext,
4949 "HPP: entering CONTENT\n");
4950#endif
4951 break;
4952 }
4953 case XML_PARSER_CONTENT: {
4954 long cons;
4955 /*
4956 * Handle preparsed entities and charRef
4957 */
4958 if (ctxt->token != 0) {
4959 xmlChar chr[2] = { 0 , 0 } ;
4960
4961 chr[0] = (xmlChar) ctxt->token;
4962 htmlCheckParagraph(ctxt);
4963 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4964 ctxt->sax->characters(ctxt->userData, chr, 1);
4965 ctxt->token = 0;
4966 ctxt->checkIndex = 0;
4967 }
4968 if ((avail == 1) && (terminate)) {
4969 cur = in->cur[0];
4970 if ((cur != '<') && (cur != '&')) {
4971 if (ctxt->sax != NULL) {
William M. Brack76e95df2003-10-18 16:20:14 +00004972 if (IS_BLANK_CH(cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00004973 if (ctxt->sax->ignorableWhitespace != NULL)
4974 ctxt->sax->ignorableWhitespace(
4975 ctxt->userData, &cur, 1);
4976 } else {
4977 htmlCheckParagraph(ctxt);
4978 if (ctxt->sax->characters != NULL)
4979 ctxt->sax->characters(
4980 ctxt->userData, &cur, 1);
4981 }
4982 }
4983 ctxt->token = 0;
4984 ctxt->checkIndex = 0;
Daniel Veillardbc6e1a32002-11-18 15:07:25 +00004985 in->cur++;
William M. Brack1633d182001-10-05 15:41:19 +00004986 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004987 }
Owen Taylor3473f882001-02-23 17:55:21 +00004988 }
4989 if (avail < 2)
4990 goto done;
4991 cur = in->cur[0];
4992 next = in->cur[1];
4993 cons = ctxt->nbChars;
4994 if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
4995 (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
4996 /*
4997 * Handle SCRIPT/STYLE separately
4998 */
Daniel Veillard68716a72006-10-16 09:32:17 +00004999 if (!terminate) {
5000 int idx;
5001 xmlChar val;
5002
5003 idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0);
5004 if (idx < 0)
5005 goto done;
5006 val = in->cur[idx + 2];
5007 if (val == 0) /* bad cut of input */
5008 goto done;
5009 }
Owen Taylor3473f882001-02-23 17:55:21 +00005010 htmlParseScript(ctxt);
5011 if ((cur == '<') && (next == '/')) {
5012 ctxt->instate = XML_PARSER_END_TAG;
5013 ctxt->checkIndex = 0;
5014#ifdef DEBUG_PUSH
5015 xmlGenericError(xmlGenericErrorContext,
5016 "HPP: entering END_TAG\n");
5017#endif
5018 break;
5019 }
5020 } else {
5021 /*
5022 * Sometimes DOCTYPE arrives in the middle of the document
5023 */
5024 if ((cur == '<') && (next == '!') &&
5025 (UPP(2) == 'D') && (UPP(3) == 'O') &&
5026 (UPP(4) == 'C') && (UPP(5) == 'T') &&
5027 (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5028 (UPP(8) == 'E')) {
5029 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005030 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005031 goto done;
Daniel Veillardf403d292003-10-05 13:51:35 +00005032 htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
5033 "Misplaced DOCTYPE declaration\n",
5034 BAD_CAST "DOCTYPE" , NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005035 htmlParseDocTypeDecl(ctxt);
5036 } else if ((cur == '<') && (next == '!') &&
5037 (in->cur[2] == '-') && (in->cur[3] == '-')) {
5038 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005039 (htmlParseLookupSequence(
5040 ctxt, '-', '-', '>', 1) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005041 goto done;
5042#ifdef DEBUG_PUSH
5043 xmlGenericError(xmlGenericErrorContext,
5044 "HPP: Parsing Comment\n");
5045#endif
5046 htmlParseComment(ctxt);
5047 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00005048 } else if ((cur == '<') && (next == '?')) {
5049 if ((!terminate) &&
5050 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
5051 goto done;
5052#ifdef DEBUG_PUSH
5053 xmlGenericError(xmlGenericErrorContext,
5054 "HPP: Parsing PI\n");
5055#endif
5056 htmlParsePI(ctxt);
5057 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +00005058 } else if ((cur == '<') && (next == '!') && (avail < 4)) {
5059 goto done;
5060 } else if ((cur == '<') && (next == '/')) {
5061 ctxt->instate = XML_PARSER_END_TAG;
5062 ctxt->checkIndex = 0;
5063#ifdef DEBUG_PUSH
5064 xmlGenericError(xmlGenericErrorContext,
5065 "HPP: entering END_TAG\n");
5066#endif
5067 break;
5068 } else if (cur == '<') {
5069 ctxt->instate = XML_PARSER_START_TAG;
5070 ctxt->checkIndex = 0;
5071#ifdef DEBUG_PUSH
5072 xmlGenericError(xmlGenericErrorContext,
5073 "HPP: entering START_TAG\n");
5074#endif
5075 break;
5076 } else if (cur == '&') {
5077 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005078 (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005079 goto done;
5080#ifdef DEBUG_PUSH
5081 xmlGenericError(xmlGenericErrorContext,
5082 "HPP: Parsing Reference\n");
5083#endif
5084 /* TODO: check generation of subtrees if noent !!! */
5085 htmlParseReference(ctxt);
5086 } else {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005087 /*
5088 * check that the text sequence is complete
5089 * before handing out the data to the parser
5090 * to avoid problems with erroneous end of
5091 * data detection.
Owen Taylor3473f882001-02-23 17:55:21 +00005092 */
Daniel Veillard14f752c2003-08-09 11:44:50 +00005093 if ((!terminate) &&
5094 (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
5095 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00005096 ctxt->checkIndex = 0;
5097#ifdef DEBUG_PUSH
5098 xmlGenericError(xmlGenericErrorContext,
5099 "HPP: Parsing char data\n");
5100#endif
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005101 htmlParseCharData(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005102 }
5103 }
5104 if (cons == ctxt->nbChars) {
5105 if (ctxt->node != NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005106 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5107 "detected an error in element content\n",
5108 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005109 }
5110 NEXT;
5111 break;
5112 }
5113
5114 break;
5115 }
5116 case XML_PARSER_END_TAG:
5117 if (avail < 2)
5118 goto done;
5119 if ((!terminate) &&
Daniel Veillard97e01882003-07-30 18:59:19 +00005120 (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00005121 goto done;
5122 htmlParseEndTag(ctxt);
5123 if (ctxt->nameNr == 0) {
5124 ctxt->instate = XML_PARSER_EPILOG;
5125 } else {
5126 ctxt->instate = XML_PARSER_CONTENT;
5127 }
5128 ctxt->checkIndex = 0;
5129#ifdef DEBUG_PUSH
5130 xmlGenericError(xmlGenericErrorContext,
5131 "HPP: entering CONTENT\n");
5132#endif
5133 break;
5134 case XML_PARSER_CDATA_SECTION:
Daniel Veillardf403d292003-10-05 13:51:35 +00005135 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5136 "HPP: internal error, state == CDATA\n",
5137 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005138 ctxt->instate = XML_PARSER_CONTENT;
5139 ctxt->checkIndex = 0;
5140#ifdef DEBUG_PUSH
5141 xmlGenericError(xmlGenericErrorContext,
5142 "HPP: entering CONTENT\n");
5143#endif
5144 break;
5145 case XML_PARSER_DTD:
Daniel Veillardf403d292003-10-05 13:51:35 +00005146 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5147 "HPP: internal error, state == DTD\n",
5148 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005149 ctxt->instate = XML_PARSER_CONTENT;
5150 ctxt->checkIndex = 0;
5151#ifdef DEBUG_PUSH
5152 xmlGenericError(xmlGenericErrorContext,
5153 "HPP: entering CONTENT\n");
5154#endif
5155 break;
5156 case XML_PARSER_COMMENT:
Daniel Veillardf403d292003-10-05 13:51:35 +00005157 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5158 "HPP: internal error, state == COMMENT\n",
5159 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005160 ctxt->instate = XML_PARSER_CONTENT;
5161 ctxt->checkIndex = 0;
5162#ifdef DEBUG_PUSH
5163 xmlGenericError(xmlGenericErrorContext,
5164 "HPP: entering CONTENT\n");
5165#endif
5166 break;
5167 case XML_PARSER_PI:
Daniel Veillardf403d292003-10-05 13:51:35 +00005168 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5169 "HPP: internal error, state == PI\n",
5170 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005171 ctxt->instate = XML_PARSER_CONTENT;
5172 ctxt->checkIndex = 0;
5173#ifdef DEBUG_PUSH
5174 xmlGenericError(xmlGenericErrorContext,
5175 "HPP: entering CONTENT\n");
5176#endif
5177 break;
5178 case XML_PARSER_ENTITY_DECL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005179 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5180 "HPP: internal error, state == ENTITY_DECL\n",
5181 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005182 ctxt->instate = XML_PARSER_CONTENT;
5183 ctxt->checkIndex = 0;
5184#ifdef DEBUG_PUSH
5185 xmlGenericError(xmlGenericErrorContext,
5186 "HPP: entering CONTENT\n");
5187#endif
5188 break;
5189 case XML_PARSER_ENTITY_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005190 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5191 "HPP: internal error, state == ENTITY_VALUE\n",
5192 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005193 ctxt->instate = XML_PARSER_CONTENT;
5194 ctxt->checkIndex = 0;
5195#ifdef DEBUG_PUSH
5196 xmlGenericError(xmlGenericErrorContext,
5197 "HPP: entering DTD\n");
5198#endif
5199 break;
5200 case XML_PARSER_ATTRIBUTE_VALUE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005201 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5202 "HPP: internal error, state == ATTRIBUTE_VALUE\n",
5203 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005204 ctxt->instate = XML_PARSER_START_TAG;
5205 ctxt->checkIndex = 0;
5206#ifdef DEBUG_PUSH
5207 xmlGenericError(xmlGenericErrorContext,
5208 "HPP: entering START_TAG\n");
5209#endif
5210 break;
5211 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005212 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5213 "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
5214 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005215 ctxt->instate = XML_PARSER_CONTENT;
5216 ctxt->checkIndex = 0;
5217#ifdef DEBUG_PUSH
5218 xmlGenericError(xmlGenericErrorContext,
5219 "HPP: entering CONTENT\n");
5220#endif
5221 break;
5222 case XML_PARSER_IGNORE:
Daniel Veillardf403d292003-10-05 13:51:35 +00005223 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5224 "HPP: internal error, state == XML_PARSER_IGNORE\n",
5225 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005226 ctxt->instate = XML_PARSER_CONTENT;
5227 ctxt->checkIndex = 0;
5228#ifdef DEBUG_PUSH
5229 xmlGenericError(xmlGenericErrorContext,
5230 "HPP: entering CONTENT\n");
5231#endif
5232 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005233 case XML_PARSER_PUBLIC_LITERAL:
Daniel Veillardf403d292003-10-05 13:51:35 +00005234 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5235 "HPP: internal error, state == XML_PARSER_LITERAL\n",
5236 NULL, NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005237 ctxt->instate = XML_PARSER_CONTENT;
5238 ctxt->checkIndex = 0;
5239#ifdef DEBUG_PUSH
5240 xmlGenericError(xmlGenericErrorContext,
5241 "HPP: entering CONTENT\n");
5242#endif
5243 break;
5244
Owen Taylor3473f882001-02-23 17:55:21 +00005245 }
5246 }
5247done:
5248 if ((avail == 0) && (terminate)) {
Daniel Veillarda3bfca52001-04-12 15:42:58 +00005249 htmlAutoCloseOnEnd(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005250 if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5251 /*
5252 * SAX: end of the document processing.
5253 */
5254 ctxt->instate = XML_PARSER_EOF;
5255 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5256 ctxt->sax->endDocument(ctxt->userData);
5257 }
5258 }
5259 if ((ctxt->myDoc != NULL) &&
5260 ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5261 (ctxt->instate == XML_PARSER_EPILOG))) {
5262 xmlDtdPtr dtd;
5263 dtd = xmlGetIntSubset(ctxt->myDoc);
5264 if (dtd == NULL)
5265 ctxt->myDoc->intSubset =
Daniel Veillard40412cd2003-09-03 13:28:32 +00005266 xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
Owen Taylor3473f882001-02-23 17:55:21 +00005267 BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5268 BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5269 }
5270#ifdef DEBUG_PUSH
5271 xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5272#endif
5273 return(ret);
5274}
5275
5276/**
Owen Taylor3473f882001-02-23 17:55:21 +00005277 * htmlParseChunk:
Daniel Veillardf403d292003-10-05 13:51:35 +00005278 * @ctxt: an HTML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00005279 * @chunk: an char array
5280 * @size: the size in byte of the chunk
5281 * @terminate: last chunk indicator
5282 *
5283 * Parse a Chunk of memory
5284 *
5285 * Returns zero if no error, the xmlParserErrors otherwise.
5286 */
5287int
5288htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
5289 int terminate) {
Daniel Veillarda03e3652004-11-02 18:45:30 +00005290 if ((ctxt == NULL) || (ctxt->input == NULL)) {
5291 htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
5292 "htmlParseChunk: context error\n", NULL, NULL);
5293 return(XML_ERR_INTERNAL_ERROR);
5294 }
Owen Taylor3473f882001-02-23 17:55:21 +00005295 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5296 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
5297 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5298 int cur = ctxt->input->cur - ctxt->input->base;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005299 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00005300
Daniel Veillardd2755a82005-08-07 23:42:39 +00005301 res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5302 if (res < 0) {
5303 ctxt->errNo = XML_PARSER_EOF;
5304 ctxt->disableSAX = 1;
5305 return (XML_PARSER_EOF);
5306 }
Owen Taylor3473f882001-02-23 17:55:21 +00005307 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5308 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillardd2755a82005-08-07 23:42:39 +00005309 ctxt->input->end =
5310 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005311#ifdef DEBUG_PUSH
5312 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5313#endif
5314
Daniel Veillard14f752c2003-08-09 11:44:50 +00005315#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00005316 if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5317 htmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005318#endif
Owen Taylor3473f882001-02-23 17:55:21 +00005319 } else if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard14f752c2003-08-09 11:44:50 +00005320 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
5321 xmlParserInputBufferPtr in = ctxt->input->buf;
5322 if ((in->encoder != NULL) && (in->buffer != NULL) &&
5323 (in->raw != NULL)) {
5324 int nbchars;
5325
5326 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
5327 if (nbchars < 0) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005328 htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
5329 "encoder error\n", NULL, NULL);
Daniel Veillard14f752c2003-08-09 11:44:50 +00005330 return(XML_ERR_INVALID_ENCODING);
5331 }
5332 }
5333 }
Owen Taylor3473f882001-02-23 17:55:21 +00005334 }
Daniel Veillard14f752c2003-08-09 11:44:50 +00005335 htmlParseTryOrFinish(ctxt, terminate);
Owen Taylor3473f882001-02-23 17:55:21 +00005336 if (terminate) {
5337 if ((ctxt->instate != XML_PARSER_EOF) &&
5338 (ctxt->instate != XML_PARSER_EPILOG) &&
5339 (ctxt->instate != XML_PARSER_MISC)) {
5340 ctxt->errNo = XML_ERR_DOCUMENT_END;
Owen Taylor3473f882001-02-23 17:55:21 +00005341 ctxt->wellFormed = 0;
5342 }
5343 if (ctxt->instate != XML_PARSER_EOF) {
5344 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5345 ctxt->sax->endDocument(ctxt->userData);
5346 }
5347 ctxt->instate = XML_PARSER_EOF;
5348 }
5349 return((xmlParserErrors) ctxt->errNo);
5350}
5351
5352/************************************************************************
5353 * *
5354 * User entry points *
5355 * *
5356 ************************************************************************/
5357
5358/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005359 * htmlCreatePushParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005360 * @sax: a SAX handler
5361 * @user_data: The user data returned on SAX callbacks
5362 * @chunk: a pointer to an array of chars
5363 * @size: number of chars in the array
5364 * @filename: an optional file name or URI
5365 * @enc: an optional encoding
5366 *
5367 * Create a parser context for using the HTML parser in push mode
Owen Taylor3473f882001-02-23 17:55:21 +00005368 * The value of @filename is used for fetching external entities
5369 * and error/warning reports.
5370 *
5371 * Returns the new parser context or NULL
5372 */
5373htmlParserCtxtPtr
5374htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
5375 const char *chunk, int size, const char *filename,
5376 xmlCharEncoding enc) {
5377 htmlParserCtxtPtr ctxt;
5378 htmlParserInputPtr inputStream;
5379 xmlParserInputBufferPtr buf;
5380
Daniel Veillardd0463562001-10-13 09:15:48 +00005381 xmlInitParser();
5382
Owen Taylor3473f882001-02-23 17:55:21 +00005383 buf = xmlAllocParserInputBuffer(enc);
5384 if (buf == NULL) return(NULL);
5385
Daniel Veillardf403d292003-10-05 13:51:35 +00005386 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005387 if (ctxt == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005388 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005389 return(NULL);
5390 }
Daniel Veillard77a90a72003-03-22 00:04:05 +00005391 if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
5392 ctxt->charset=XML_CHAR_ENCODING_UTF8;
Owen Taylor3473f882001-02-23 17:55:21 +00005393 if (sax != NULL) {
Daniel Veillard092643b2003-09-25 14:29:29 +00005394 if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
Owen Taylor3473f882001-02-23 17:55:21 +00005395 xmlFree(ctxt->sax);
5396 ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
5397 if (ctxt->sax == NULL) {
5398 xmlFree(buf);
5399 xmlFree(ctxt);
5400 return(NULL);
5401 }
5402 memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
5403 if (user_data != NULL)
5404 ctxt->userData = user_data;
5405 }
5406 if (filename == NULL) {
5407 ctxt->directory = NULL;
5408 } else {
5409 ctxt->directory = xmlParserGetDirectory(filename);
5410 }
5411
5412 inputStream = htmlNewInputStream(ctxt);
5413 if (inputStream == NULL) {
5414 xmlFreeParserCtxt(ctxt);
Daniel Veillard77a90a72003-03-22 00:04:05 +00005415 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00005416 return(NULL);
5417 }
5418
5419 if (filename == NULL)
5420 inputStream->filename = NULL;
5421 else
Daniel Veillard5f704af2003-03-05 10:01:43 +00005422 inputStream->filename = (char *)
5423 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00005424 inputStream->buf = buf;
5425 inputStream->base = inputStream->buf->buffer->content;
5426 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard5f704af2003-03-05 10:01:43 +00005427 inputStream->end =
5428 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005429
5430 inputPush(ctxt, inputStream);
5431
5432 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5433 (ctxt->input->buf != NULL)) {
Daniel Veillard5f704af2003-03-05 10:01:43 +00005434 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5435 int cur = ctxt->input->cur - ctxt->input->base;
5436
Owen Taylor3473f882001-02-23 17:55:21 +00005437 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillard5f704af2003-03-05 10:01:43 +00005438
5439 ctxt->input->base = ctxt->input->buf->buffer->content + base;
5440 ctxt->input->cur = ctxt->input->base + cur;
5441 ctxt->input->end =
5442 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00005443#ifdef DEBUG_PUSH
5444 xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5445#endif
5446 }
Daniel Veillard68716a72006-10-16 09:32:17 +00005447 ctxt->progressive = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005448
5449 return(ctxt);
5450}
William M. Brack21e4ef22005-01-02 09:53:13 +00005451#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005452
5453/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005454 * htmlSAXParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005455 * @cur: a pointer to an array of xmlChar
5456 * @encoding: a free form C string describing the HTML document encoding, or NULL
5457 * @sax: the SAX handler block
5458 * @userData: if using SAX, this pointer will be provided on callbacks.
5459 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005460 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
5461 * to handle parse events. If sax is NULL, fallback to the default DOM
5462 * behavior and return a tree.
Owen Taylor3473f882001-02-23 17:55:21 +00005463 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005464 * Returns the resulting document tree unless SAX is NULL or the document is
5465 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005466 */
5467
5468htmlDocPtr
5469htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
5470 htmlDocPtr ret;
5471 htmlParserCtxtPtr ctxt;
5472
Daniel Veillardd0463562001-10-13 09:15:48 +00005473 xmlInitParser();
5474
Owen Taylor3473f882001-02-23 17:55:21 +00005475 if (cur == NULL) return(NULL);
5476
5477
5478 ctxt = htmlCreateDocParserCtxt(cur, encoding);
5479 if (ctxt == NULL) return(NULL);
5480 if (sax != NULL) {
Daniel Veillardb19ba832003-08-14 00:33:46 +00005481 if (ctxt->sax != NULL) xmlFree (ctxt->sax);
Owen Taylor3473f882001-02-23 17:55:21 +00005482 ctxt->sax = sax;
5483 ctxt->userData = userData;
5484 }
5485
5486 htmlParseDocument(ctxt);
5487 ret = ctxt->myDoc;
5488 if (sax != NULL) {
5489 ctxt->sax = NULL;
5490 ctxt->userData = NULL;
5491 }
5492 htmlFreeParserCtxt(ctxt);
5493
5494 return(ret);
5495}
5496
5497/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005498 * htmlParseDoc:
Owen Taylor3473f882001-02-23 17:55:21 +00005499 * @cur: a pointer to an array of xmlChar
5500 * @encoding: a free form C string describing the HTML document encoding, or NULL
5501 *
5502 * parse an HTML in-memory document and build a tree.
5503 *
5504 * Returns the resulting document tree
5505 */
5506
5507htmlDocPtr
5508htmlParseDoc(xmlChar *cur, const char *encoding) {
5509 return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
5510}
5511
5512
5513/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005514 * htmlCreateFileParserCtxt:
Owen Taylor3473f882001-02-23 17:55:21 +00005515 * @filename: the filename
5516 * @encoding: a free form C string describing the HTML document encoding, or NULL
5517 *
5518 * Create a parser context for a file content.
5519 * Automatic support for ZLIB/Compress compressed document is provided
5520 * by default if found at compile-time.
5521 *
5522 * Returns the new parser context or NULL
5523 */
5524htmlParserCtxtPtr
5525htmlCreateFileParserCtxt(const char *filename, const char *encoding)
5526{
5527 htmlParserCtxtPtr ctxt;
5528 htmlParserInputPtr inputStream;
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005529 char *canonicFilename;
Owen Taylor3473f882001-02-23 17:55:21 +00005530 /* htmlCharEncoding enc; */
5531 xmlChar *content, *content_line = (xmlChar *) "charset=";
5532
Daniel Veillarda03e3652004-11-02 18:45:30 +00005533 if (filename == NULL)
5534 return(NULL);
5535
Daniel Veillardf403d292003-10-05 13:51:35 +00005536 ctxt = htmlNewParserCtxt();
Owen Taylor3473f882001-02-23 17:55:21 +00005537 if (ctxt == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005538 return(NULL);
5539 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005540 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
5541 if (canonicFilename == NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00005542#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005543 if (xmlDefaultSAXHandler.error != NULL) {
5544 xmlDefaultSAXHandler.error(NULL, "out of memory\n");
5545 }
Daniel Veillard87247e82004-01-13 20:42:02 +00005546#endif
Daniel Veillard104caa32003-05-13 22:54:05 +00005547 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005548 return(NULL);
5549 }
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005550
5551 inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
5552 xmlFree(canonicFilename);
5553 if (inputStream == NULL) {
5554 xmlFreeParserCtxt(ctxt);
5555 return(NULL);
5556 }
Owen Taylor3473f882001-02-23 17:55:21 +00005557
5558 inputPush(ctxt, inputStream);
Daniel Veillarde8b09e42003-05-13 22:14:13 +00005559
Owen Taylor3473f882001-02-23 17:55:21 +00005560 /* set encoding */
5561 if (encoding) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00005562 content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
Owen Taylor3473f882001-02-23 17:55:21 +00005563 if (content) {
5564 strcpy ((char *)content, (char *)content_line);
5565 strcat ((char *)content, (char *)encoding);
5566 htmlCheckEncoding (ctxt, content);
5567 xmlFree (content);
5568 }
5569 }
5570
5571 return(ctxt);
5572}
5573
5574/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005575 * htmlSAXParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005576 * @filename: the filename
5577 * @encoding: a free form C string describing the HTML document encoding, or NULL
5578 * @sax: the SAX handler block
5579 * @userData: if using SAX, this pointer will be provided on callbacks.
5580 *
5581 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5582 * compressed document is provided by default if found at compile-time.
5583 * It use the given SAX function block to handle the parsing callback.
5584 * If sax is NULL, fallback to the default DOM tree building routines.
5585 *
Daniel Veillard4d65a1c2001-07-04 22:06:23 +00005586 * Returns the resulting document tree unless SAX is NULL or the document is
5587 * not well formed.
Owen Taylor3473f882001-02-23 17:55:21 +00005588 */
5589
5590htmlDocPtr
5591htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
5592 void *userData) {
5593 htmlDocPtr ret;
5594 htmlParserCtxtPtr ctxt;
5595 htmlSAXHandlerPtr oldsax = NULL;
5596
Daniel Veillardd0463562001-10-13 09:15:48 +00005597 xmlInitParser();
5598
Owen Taylor3473f882001-02-23 17:55:21 +00005599 ctxt = htmlCreateFileParserCtxt(filename, encoding);
5600 if (ctxt == NULL) return(NULL);
5601 if (sax != NULL) {
5602 oldsax = ctxt->sax;
5603 ctxt->sax = sax;
5604 ctxt->userData = userData;
5605 }
5606
5607 htmlParseDocument(ctxt);
5608
5609 ret = ctxt->myDoc;
5610 if (sax != NULL) {
5611 ctxt->sax = oldsax;
5612 ctxt->userData = NULL;
5613 }
5614 htmlFreeParserCtxt(ctxt);
5615
5616 return(ret);
5617}
5618
5619/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005620 * htmlParseFile:
Owen Taylor3473f882001-02-23 17:55:21 +00005621 * @filename: the filename
5622 * @encoding: a free form C string describing the HTML document encoding, or NULL
5623 *
5624 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
5625 * compressed document is provided by default if found at compile-time.
5626 *
5627 * Returns the resulting document tree
5628 */
5629
5630htmlDocPtr
5631htmlParseFile(const char *filename, const char *encoding) {
5632 return(htmlSAXParseFile(filename, encoding, NULL, NULL));
5633}
5634
5635/**
5636 * htmlHandleOmittedElem:
5637 * @val: int 0 or 1
5638 *
5639 * Set and return the previous value for handling HTML omitted tags.
5640 *
5641 * Returns the last value for 0 for no handling, 1 for auto insertion.
5642 */
5643
5644int
5645htmlHandleOmittedElem(int val) {
5646 int old = htmlOmittedDefaultValue;
5647
5648 htmlOmittedDefaultValue = val;
5649 return(old);
5650}
5651
Daniel Veillard930dfb62003-02-05 10:17:38 +00005652/**
5653 * htmlElementAllowedHere:
5654 * @parent: HTML parent element
5655 * @elt: HTML element
5656 *
5657 * Checks whether an HTML element may be a direct child of a parent element.
5658 * Note - doesn't check for deprecated elements
5659 *
5660 * Returns 1 if allowed; 0 otherwise.
5661 */
5662int
5663htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
5664 const char** p ;
5665
5666 if ( ! elt || ! parent || ! parent->subelts )
5667 return 0 ;
5668
5669 for ( p = parent->subelts; *p; ++p )
5670 if ( !xmlStrcmp((const xmlChar *)*p, elt) )
5671 return 1 ;
5672
5673 return 0 ;
5674}
5675/**
5676 * htmlElementStatusHere:
5677 * @parent: HTML parent element
5678 * @elt: HTML element
5679 *
5680 * Checks whether an HTML element may be a direct child of a parent element.
5681 * and if so whether it is valid or deprecated.
5682 *
5683 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5684 */
5685htmlStatus
5686htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
5687 if ( ! parent || ! elt )
5688 return HTML_INVALID ;
5689 if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
5690 return HTML_INVALID ;
5691
5692 return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
5693}
5694/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005695 * htmlAttrAllowed:
Daniel Veillard930dfb62003-02-05 10:17:38 +00005696 * @elt: HTML element
5697 * @attr: HTML attribute
5698 * @legacy: whether to allow deprecated attributes
5699 *
5700 * Checks whether an attribute is valid for an element
5701 * Has full knowledge of Required and Deprecated attributes
5702 *
5703 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
5704 */
5705htmlStatus
5706htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
5707 const char** p ;
5708
5709 if ( !elt || ! attr )
5710 return HTML_INVALID ;
5711
5712 if ( elt->attrs_req )
5713 for ( p = elt->attrs_req; *p; ++p)
5714 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5715 return HTML_REQUIRED ;
5716
5717 if ( elt->attrs_opt )
5718 for ( p = elt->attrs_opt; *p; ++p)
5719 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5720 return HTML_VALID ;
5721
5722 if ( legacy && elt->attrs_depr )
5723 for ( p = elt->attrs_depr; *p; ++p)
5724 if ( !xmlStrcmp((const xmlChar*)*p, attr) )
5725 return HTML_DEPRECATED ;
5726
5727 return HTML_INVALID ;
5728}
5729/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005730 * htmlNodeStatus:
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005731 * @node: an htmlNodePtr in a tree
5732 * @legacy: whether to allow deprecated elements (YES is faster here
Daniel Veillard930dfb62003-02-05 10:17:38 +00005733 * for Element nodes)
5734 *
5735 * Checks whether the tree node is valid. Experimental (the author
5736 * only uses the HTML enhancements in a SAX parser)
5737 *
5738 * Return: for Element nodes, a return from htmlElementAllowedHere (if
5739 * legacy allowed) or htmlElementStatusHere (otherwise).
5740 * for Attribute nodes, a return from htmlAttrAllowed
5741 * for other nodes, HTML_NA (no checks performed)
5742 */
5743htmlStatus
5744htmlNodeStatus(const htmlNodePtr node, int legacy) {
5745 if ( ! node )
5746 return HTML_INVALID ;
5747
5748 switch ( node->type ) {
5749 case XML_ELEMENT_NODE:
5750 return legacy
5751 ? ( htmlElementAllowedHere (
5752 htmlTagLookup(node->parent->name) , node->name
5753 ) ? HTML_VALID : HTML_INVALID )
5754 : htmlElementStatusHere(
5755 htmlTagLookup(node->parent->name) ,
5756 htmlTagLookup(node->name) )
5757 ;
5758 case XML_ATTRIBUTE_NODE:
5759 return htmlAttrAllowed(
5760 htmlTagLookup(node->parent->name) , node->name, legacy) ;
5761 default: return HTML_NA ;
5762 }
5763}
Daniel Veillard9475a352003-09-26 12:47:50 +00005764/************************************************************************
5765 * *
5766 * New set (2.6.0) of simpler and more flexible APIs *
5767 * *
5768 ************************************************************************/
5769/**
5770 * DICT_FREE:
5771 * @str: a string
5772 *
5773 * Free a string if it is not owned by the "dict" dictionnary in the
5774 * current scope
5775 */
5776#define DICT_FREE(str) \
5777 if ((str) && ((!dict) || \
5778 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
5779 xmlFree((char *)(str));
5780
5781/**
5782 * htmlCtxtReset:
Daniel Veillardf403d292003-10-05 13:51:35 +00005783 * @ctxt: an HTML parser context
Daniel Veillard9475a352003-09-26 12:47:50 +00005784 *
5785 * Reset a parser context
5786 */
5787void
5788htmlCtxtReset(htmlParserCtxtPtr ctxt)
5789{
5790 xmlParserInputPtr input;
Daniel Veillarda03e3652004-11-02 18:45:30 +00005791 xmlDictPtr dict;
5792
5793 if (ctxt == NULL)
5794 return;
5795
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005796 xmlInitParser();
Daniel Veillarda03e3652004-11-02 18:45:30 +00005797 dict = ctxt->dict;
Daniel Veillard9475a352003-09-26 12:47:50 +00005798
5799 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
5800 xmlFreeInputStream(input);
5801 }
5802 ctxt->inputNr = 0;
5803 ctxt->input = NULL;
5804
5805 ctxt->spaceNr = 0;
Daniel Veillarda521d282004-11-09 14:59:59 +00005806 if (ctxt->spaceTab != NULL) {
5807 ctxt->spaceTab[0] = -1;
5808 ctxt->space = &ctxt->spaceTab[0];
5809 } else {
5810 ctxt->space = NULL;
5811 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005812
5813
5814 ctxt->nodeNr = 0;
5815 ctxt->node = NULL;
5816
5817 ctxt->nameNr = 0;
5818 ctxt->name = NULL;
5819
5820 DICT_FREE(ctxt->version);
5821 ctxt->version = NULL;
5822 DICT_FREE(ctxt->encoding);
5823 ctxt->encoding = NULL;
5824 DICT_FREE(ctxt->directory);
5825 ctxt->directory = NULL;
5826 DICT_FREE(ctxt->extSubURI);
5827 ctxt->extSubURI = NULL;
5828 DICT_FREE(ctxt->extSubSystem);
5829 ctxt->extSubSystem = NULL;
5830 if (ctxt->myDoc != NULL)
5831 xmlFreeDoc(ctxt->myDoc);
5832 ctxt->myDoc = NULL;
5833
5834 ctxt->standalone = -1;
5835 ctxt->hasExternalSubset = 0;
5836 ctxt->hasPErefs = 0;
5837 ctxt->html = 1;
5838 ctxt->external = 0;
5839 ctxt->instate = XML_PARSER_START;
5840 ctxt->token = 0;
5841
5842 ctxt->wellFormed = 1;
5843 ctxt->nsWellFormed = 1;
5844 ctxt->valid = 1;
5845 ctxt->vctxt.userData = ctxt;
5846 ctxt->vctxt.error = xmlParserValidityError;
5847 ctxt->vctxt.warning = xmlParserValidityWarning;
5848 ctxt->record_info = 0;
5849 ctxt->nbChars = 0;
5850 ctxt->checkIndex = 0;
5851 ctxt->inSubset = 0;
5852 ctxt->errNo = XML_ERR_OK;
5853 ctxt->depth = 0;
Daniel Veillard772869f2006-11-08 09:16:56 +00005854 ctxt->charset = XML_CHAR_ENCODING_NONE;
Daniel Veillard9475a352003-09-26 12:47:50 +00005855 ctxt->catalogs = NULL;
5856 xmlInitNodeInfoSeq(&ctxt->node_seq);
5857
5858 if (ctxt->attsDefault != NULL) {
5859 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
5860 ctxt->attsDefault = NULL;
5861 }
5862 if (ctxt->attsSpecial != NULL) {
5863 xmlHashFree(ctxt->attsSpecial, NULL);
5864 ctxt->attsSpecial = NULL;
5865 }
5866}
5867
5868/**
5869 * htmlCtxtUseOptions:
5870 * @ctxt: an HTML parser context
5871 * @options: a combination of htmlParserOption(s)
5872 *
5873 * Applies the options to the parser context
5874 *
5875 * Returns 0 in case of success, the set of unknown or unimplemented options
5876 * in case of error.
5877 */
5878int
5879htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
5880{
Daniel Veillarda03e3652004-11-02 18:45:30 +00005881 if (ctxt == NULL)
5882 return(-1);
5883
Daniel Veillard9475a352003-09-26 12:47:50 +00005884 if (options & HTML_PARSE_NOWARNING) {
5885 ctxt->sax->warning = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005886 ctxt->vctxt.warning = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005887 options -= XML_PARSE_NOWARNING;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005888 ctxt->options |= XML_PARSE_NOWARNING;
Daniel Veillard9475a352003-09-26 12:47:50 +00005889 }
5890 if (options & HTML_PARSE_NOERROR) {
5891 ctxt->sax->error = NULL;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005892 ctxt->vctxt.error = NULL;
Daniel Veillard9475a352003-09-26 12:47:50 +00005893 ctxt->sax->fatalError = NULL;
5894 options -= XML_PARSE_NOERROR;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005895 ctxt->options |= XML_PARSE_NOERROR;
Daniel Veillard9475a352003-09-26 12:47:50 +00005896 }
5897 if (options & HTML_PARSE_PEDANTIC) {
5898 ctxt->pedantic = 1;
5899 options -= XML_PARSE_PEDANTIC;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005900 ctxt->options |= XML_PARSE_PEDANTIC;
Daniel Veillard9475a352003-09-26 12:47:50 +00005901 } else
5902 ctxt->pedantic = 0;
5903 if (options & XML_PARSE_NOBLANKS) {
5904 ctxt->keepBlanks = 0;
5905 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
5906 options -= XML_PARSE_NOBLANKS;
Daniel Veillardd3669b22004-02-25 12:34:55 +00005907 ctxt->options |= XML_PARSE_NOBLANKS;
Daniel Veillard9475a352003-09-26 12:47:50 +00005908 } else
5909 ctxt->keepBlanks = 1;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00005910 if (options & HTML_PARSE_RECOVER) {
5911 ctxt->recovery = 1;
Daniel Veillardaf616a72006-10-17 20:18:39 +00005912 options -= HTML_PARSE_RECOVER;
Daniel Veillardea4b0ba2005-08-23 16:06:08 +00005913 } else
5914 ctxt->recovery = 0;
Daniel Veillard8874b942005-08-25 13:19:21 +00005915 if (options & HTML_PARSE_COMPACT) {
5916 ctxt->options |= HTML_PARSE_COMPACT;
5917 options -= HTML_PARSE_COMPACT;
5918 }
Daniel Veillard9475a352003-09-26 12:47:50 +00005919 ctxt->dictNames = 0;
5920 return (options);
5921}
5922
5923/**
5924 * htmlDoRead:
5925 * @ctxt: an HTML parser context
5926 * @URL: the base URL to use for the document
5927 * @encoding: the document encoding, or NULL
5928 * @options: a combination of htmlParserOption(s)
5929 * @reuse: keep the context for reuse
5930 *
5931 * Common front-end for the htmlRead functions
5932 *
5933 * Returns the resulting document tree or NULL
5934 */
5935static htmlDocPtr
5936htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
5937 int options, int reuse)
5938{
5939 htmlDocPtr ret;
5940
5941 htmlCtxtUseOptions(ctxt, options);
5942 ctxt->html = 1;
5943 if (encoding != NULL) {
5944 xmlCharEncodingHandlerPtr hdlr;
5945
5946 hdlr = xmlFindCharEncodingHandler(encoding);
5947 if (hdlr != NULL)
5948 xmlSwitchToEncoding(ctxt, hdlr);
5949 }
5950 if ((URL != NULL) && (ctxt->input != NULL) &&
5951 (ctxt->input->filename == NULL))
5952 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
5953 htmlParseDocument(ctxt);
5954 ret = ctxt->myDoc;
5955 ctxt->myDoc = NULL;
5956 if (!reuse) {
5957 if ((ctxt->dictNames) &&
5958 (ret != NULL) &&
5959 (ret->dict == ctxt->dict))
5960 ctxt->dict = NULL;
5961 xmlFreeParserCtxt(ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00005962 }
5963 return (ret);
5964}
5965
5966/**
5967 * htmlReadDoc:
5968 * @cur: a pointer to a zero terminated string
5969 * @URL: the base URL to use for the document
5970 * @encoding: the document encoding, or NULL
5971 * @options: a combination of htmlParserOption(s)
5972 *
5973 * parse an XML in-memory document and build a tree.
5974 *
5975 * Returns the resulting document tree
5976 */
5977htmlDocPtr
5978htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
5979{
5980 htmlParserCtxtPtr ctxt;
5981
5982 if (cur == NULL)
5983 return (NULL);
5984
Daniel Veillardf1a27c62006-10-13 22:33:03 +00005985 xmlInitParser();
Daniel Veillard8a82ae12006-10-17 20:04:10 +00005986 ctxt = htmlCreateDocParserCtxt(cur, NULL);
Daniel Veillard9475a352003-09-26 12:47:50 +00005987 if (ctxt == NULL)
5988 return (NULL);
5989 return (htmlDoRead(ctxt, URL, encoding, options, 0));
5990}
5991
5992/**
5993 * htmlReadFile:
5994 * @filename: a file or URL
5995 * @encoding: the document encoding, or NULL
5996 * @options: a combination of htmlParserOption(s)
5997 *
5998 * parse an XML file from the filesystem or the network.
5999 *
6000 * Returns the resulting document tree
6001 */
6002htmlDocPtr
6003htmlReadFile(const char *filename, const char *encoding, int options)
6004{
6005 htmlParserCtxtPtr ctxt;
6006
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006007 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006008 ctxt = htmlCreateFileParserCtxt(filename, encoding);
6009 if (ctxt == NULL)
6010 return (NULL);
6011 return (htmlDoRead(ctxt, NULL, NULL, options, 0));
6012}
6013
6014/**
6015 * htmlReadMemory:
6016 * @buffer: a pointer to a char array
6017 * @size: the size of the array
6018 * @URL: the base URL to use for the document
6019 * @encoding: the document encoding, or NULL
6020 * @options: a combination of htmlParserOption(s)
6021 *
6022 * parse an XML in-memory document and build a tree.
6023 *
6024 * Returns the resulting document tree
6025 */
6026htmlDocPtr
6027htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
6028{
6029 htmlParserCtxtPtr ctxt;
6030
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006031 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006032 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
6033 if (ctxt == NULL)
6034 return (NULL);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006035 htmlDefaultSAXHandlerInit();
William M. Brackd43cdcd2004-08-03 15:13:29 +00006036 if (ctxt->sax != NULL)
6037 memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
Daniel Veillard9475a352003-09-26 12:47:50 +00006038 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6039}
6040
6041/**
6042 * htmlReadFd:
6043 * @fd: an open file descriptor
6044 * @URL: the base URL to use for the document
6045 * @encoding: the document encoding, or NULL
6046 * @options: a combination of htmlParserOption(s)
6047 *
6048 * parse an XML from a file descriptor and build a tree.
6049 *
6050 * Returns the resulting document tree
6051 */
6052htmlDocPtr
6053htmlReadFd(int fd, const char *URL, const char *encoding, int options)
6054{
6055 htmlParserCtxtPtr ctxt;
6056 xmlParserInputBufferPtr input;
6057 xmlParserInputPtr stream;
6058
6059 if (fd < 0)
6060 return (NULL);
6061
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006062 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006063 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6064 if (input == NULL)
6065 return (NULL);
6066 ctxt = xmlNewParserCtxt();
6067 if (ctxt == NULL) {
6068 xmlFreeParserInputBuffer(input);
6069 return (NULL);
6070 }
6071 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6072 if (stream == NULL) {
6073 xmlFreeParserInputBuffer(input);
6074 xmlFreeParserCtxt(ctxt);
6075 return (NULL);
6076 }
6077 inputPush(ctxt, stream);
6078 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6079}
6080
6081/**
6082 * htmlReadIO:
6083 * @ioread: an I/O read function
6084 * @ioclose: an I/O close function
6085 * @ioctx: an I/O handler
6086 * @URL: the base URL to use for the document
6087 * @encoding: the document encoding, or NULL
6088 * @options: a combination of htmlParserOption(s)
6089 *
6090 * parse an HTML document from I/O functions and source and build a tree.
6091 *
6092 * Returns the resulting document tree
6093 */
6094htmlDocPtr
6095htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
6096 void *ioctx, const char *URL, const char *encoding, int options)
6097{
6098 htmlParserCtxtPtr ctxt;
6099 xmlParserInputBufferPtr input;
6100 xmlParserInputPtr stream;
6101
6102 if (ioread == NULL)
6103 return (NULL);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00006104 xmlInitParser();
Daniel Veillard9475a352003-09-26 12:47:50 +00006105
6106 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6107 XML_CHAR_ENCODING_NONE);
6108 if (input == NULL)
6109 return (NULL);
Daniel Veillard8a82ae12006-10-17 20:04:10 +00006110 ctxt = htmlNewParserCtxt();
Daniel Veillard9475a352003-09-26 12:47:50 +00006111 if (ctxt == NULL) {
6112 xmlFreeParserInputBuffer(input);
6113 return (NULL);
6114 }
6115 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6116 if (stream == NULL) {
6117 xmlFreeParserInputBuffer(input);
6118 xmlFreeParserCtxt(ctxt);
6119 return (NULL);
6120 }
6121 inputPush(ctxt, stream);
6122 return (htmlDoRead(ctxt, URL, encoding, options, 0));
6123}
6124
6125/**
6126 * htmlCtxtReadDoc:
6127 * @ctxt: an HTML parser context
6128 * @cur: a pointer to a zero terminated string
6129 * @URL: the base URL to use for the document
6130 * @encoding: the document encoding, or NULL
6131 * @options: a combination of htmlParserOption(s)
6132 *
6133 * parse an XML in-memory document and build a tree.
6134 * This reuses the existing @ctxt parser context
6135 *
6136 * Returns the resulting document tree
6137 */
6138htmlDocPtr
6139htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
6140 const char *URL, const char *encoding, int options)
6141{
6142 xmlParserInputPtr stream;
6143
6144 if (cur == NULL)
6145 return (NULL);
6146 if (ctxt == NULL)
6147 return (NULL);
6148
6149 htmlCtxtReset(ctxt);
6150
6151 stream = xmlNewStringInputStream(ctxt, cur);
6152 if (stream == NULL) {
6153 return (NULL);
6154 }
6155 inputPush(ctxt, stream);
6156 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6157}
6158
6159/**
6160 * htmlCtxtReadFile:
6161 * @ctxt: an HTML parser context
6162 * @filename: a file or URL
6163 * @encoding: the document encoding, or NULL
6164 * @options: a combination of htmlParserOption(s)
6165 *
6166 * parse an XML file from the filesystem or the network.
6167 * This reuses the existing @ctxt parser context
6168 *
6169 * Returns the resulting document tree
6170 */
6171htmlDocPtr
6172htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
6173 const char *encoding, int options)
6174{
6175 xmlParserInputPtr stream;
6176
6177 if (filename == NULL)
6178 return (NULL);
6179 if (ctxt == NULL)
6180 return (NULL);
6181
6182 htmlCtxtReset(ctxt);
6183
Daniel Veillard29614c72004-11-26 10:47:26 +00006184 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard9475a352003-09-26 12:47:50 +00006185 if (stream == NULL) {
6186 return (NULL);
6187 }
6188 inputPush(ctxt, stream);
6189 return (htmlDoRead(ctxt, NULL, encoding, options, 1));
6190}
6191
6192/**
6193 * htmlCtxtReadMemory:
6194 * @ctxt: an HTML parser context
6195 * @buffer: a pointer to a char array
6196 * @size: the size of the array
6197 * @URL: the base URL to use for the document
6198 * @encoding: the document encoding, or NULL
6199 * @options: a combination of htmlParserOption(s)
6200 *
6201 * parse an XML in-memory document and build a tree.
6202 * This reuses the existing @ctxt parser context
6203 *
6204 * Returns the resulting document tree
6205 */
6206htmlDocPtr
6207htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
6208 const char *URL, const char *encoding, int options)
6209{
6210 xmlParserInputBufferPtr input;
6211 xmlParserInputPtr stream;
6212
6213 if (ctxt == NULL)
6214 return (NULL);
6215 if (buffer == NULL)
6216 return (NULL);
6217
6218 htmlCtxtReset(ctxt);
6219
6220 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
6221 if (input == NULL) {
6222 return(NULL);
6223 }
6224
6225 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6226 if (stream == NULL) {
6227 xmlFreeParserInputBuffer(input);
6228 return(NULL);
6229 }
6230
6231 inputPush(ctxt, stream);
6232 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6233}
6234
6235/**
6236 * htmlCtxtReadFd:
6237 * @ctxt: an HTML parser context
6238 * @fd: an open file descriptor
6239 * @URL: the base URL to use for the document
6240 * @encoding: the document encoding, or NULL
6241 * @options: a combination of htmlParserOption(s)
6242 *
6243 * parse an XML from a file descriptor and build a tree.
6244 * This reuses the existing @ctxt parser context
6245 *
6246 * Returns the resulting document tree
6247 */
6248htmlDocPtr
6249htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
6250 const char *URL, const char *encoding, int options)
6251{
6252 xmlParserInputBufferPtr input;
6253 xmlParserInputPtr stream;
6254
6255 if (fd < 0)
6256 return (NULL);
6257 if (ctxt == NULL)
6258 return (NULL);
6259
6260 htmlCtxtReset(ctxt);
6261
6262
6263 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
6264 if (input == NULL)
6265 return (NULL);
6266 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6267 if (stream == NULL) {
6268 xmlFreeParserInputBuffer(input);
6269 return (NULL);
6270 }
6271 inputPush(ctxt, stream);
6272 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6273}
6274
6275/**
6276 * htmlCtxtReadIO:
6277 * @ctxt: an HTML parser context
6278 * @ioread: an I/O read function
6279 * @ioclose: an I/O close function
6280 * @ioctx: an I/O handler
6281 * @URL: the base URL to use for the document
6282 * @encoding: the document encoding, or NULL
6283 * @options: a combination of htmlParserOption(s)
6284 *
6285 * parse an HTML document from I/O functions and source and build a tree.
6286 * This reuses the existing @ctxt parser context
6287 *
6288 * Returns the resulting document tree
6289 */
6290htmlDocPtr
6291htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
6292 xmlInputCloseCallback ioclose, void *ioctx,
6293 const char *URL,
6294 const char *encoding, int options)
6295{
6296 xmlParserInputBufferPtr input;
6297 xmlParserInputPtr stream;
6298
6299 if (ioread == NULL)
6300 return (NULL);
6301 if (ctxt == NULL)
6302 return (NULL);
6303
6304 htmlCtxtReset(ctxt);
6305
6306 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
6307 XML_CHAR_ENCODING_NONE);
6308 if (input == NULL)
6309 return (NULL);
6310 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
6311 if (stream == NULL) {
6312 xmlFreeParserInputBuffer(input);
6313 return (NULL);
6314 }
6315 inputPush(ctxt, stream);
6316 return (htmlDoRead(ctxt, URL, encoding, options, 1));
6317}
6318
Daniel Veillard5d4644e2005-04-01 13:11:58 +00006319#define bottom_HTMLparser
6320#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00006321#endif /* LIBXML_HTML_ENABLED */