Annotation of XML/HTMLparser.c, revision 1.21
1.1 daniel 1: /*
2: * HTMLparser.c : an HTML 4.0 non-verifying parser
3: *
4: * See Copyright for the status of this software.
5: *
6: * Daniel.Veillard@w3.org
7: */
8:
9: #ifdef WIN32
10: #define HAVE_FCNTL_H
11: #include <io.h>
12: #else
1.13 daniel 13: #include "config.h"
1.1 daniel 14: #endif
1.13 daniel 15:
1.1 daniel 16: #include <stdio.h>
1.13 daniel 17: #include <string.h> /* for memset() only */
18: #ifdef HAVE_CTYPE_H
1.1 daniel 19: #include <ctype.h>
1.13 daniel 20: #endif
21: #ifdef HAVE_STDLIB_H
1.1 daniel 22: #include <stdlib.h>
1.13 daniel 23: #endif
24: #ifdef HAVE_SYS_STAT_H
1.1 daniel 25: #include <sys/stat.h>
1.13 daniel 26: #endif
1.1 daniel 27: #ifdef HAVE_FCNTL_H
28: #include <fcntl.h>
29: #endif
30: #ifdef HAVE_UNISTD_H
31: #include <unistd.h>
32: #endif
33: #ifdef HAVE_ZLIB_H
34: #include <zlib.h>
35: #endif
36:
1.11 daniel 37: #include "xmlmemory.h"
1.1 daniel 38: #include "tree.h"
39: #include "HTMLparser.h"
40: #include "entities.h"
41: #include "encoding.h"
42: #include "valid.h"
43: #include "parserInternals.h"
1.5 daniel 44: #include "xmlIO.h"
45:
46: #define HTML_MAX_NAMELEN 1000
47: #define INPUT_CHUNK 50
1.1 daniel 48:
49: /* #define DEBUG */
50:
51: /************************************************************************
52: * *
53: * Parser stacks related functions and macros *
54: * *
55: ************************************************************************/
56:
57: /*
58: * Generic function for accessing stacks in the Parser Context
59: */
60:
61: #define PUSH_AND_POP(type, name) \
62: int html##name##Push(htmlParserCtxtPtr ctxt, type value) { \
63: if (ctxt->name##Nr >= ctxt->name##Max) { \
64: ctxt->name##Max *= 2; \
1.11 daniel 65: ctxt->name##Tab = (void *) xmlRealloc(ctxt->name##Tab, \
1.1 daniel 66: ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
67: if (ctxt->name##Tab == NULL) { \
68: fprintf(stderr, "realloc failed !\n"); \
69: exit(1); \
70: } \
71: } \
72: ctxt->name##Tab[ctxt->name##Nr] = value; \
73: ctxt->name = value; \
74: return(ctxt->name##Nr++); \
75: } \
76: type html##name##Pop(htmlParserCtxtPtr ctxt) { \
77: type ret; \
1.18 daniel 78: if (ctxt->name##Nr < 0) return(0); \
1.1 daniel 79: ctxt->name##Nr--; \
1.18 daniel 80: if (ctxt->name##Nr < 0) return(0); \
1.1 daniel 81: if (ctxt->name##Nr > 0) \
82: ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
83: else \
84: ctxt->name = NULL; \
85: ret = ctxt->name##Tab[ctxt->name##Nr]; \
86: ctxt->name##Tab[ctxt->name##Nr] = 0; \
87: return(ret); \
88: } \
89:
90: PUSH_AND_POP(xmlNodePtr, node)
1.15 daniel 91: PUSH_AND_POP(xmlChar*, name)
1.1 daniel 92:
93: /*
94: * Macros for accessing the content. Those should be used only by the parser,
95: * and not exported.
96: *
97: * Dirty macros, i.e. one need to make assumption on the context to use them
98: *
1.14 daniel 99: * CUR_PTR return the current pointer to the xmlChar to be parsed.
100: * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1.1 daniel 101: * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
102: * in UNICODE mode. This should be used internally by the parser
103: * only to compare to ASCII values otherwise it would break when
104: * running with UTF-8 encoding.
1.14 daniel 105: * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1.1 daniel 106: * to compare on ASCII based substring.
1.14 daniel 107: * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
1.1 daniel 108: * it should be used only to compare on ASCII based substring.
1.14 daniel 109: * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1.1 daniel 110: * strings within the parser.
111: *
112: * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
113: *
114: * CURRENT Returns the current char value, with the full decoding of
115: * UTF-8 if we are using this mode. It returns an int.
116: * NEXT Skip to the next character, this does the proper decoding
117: * in UTF-8 mode. It also pop-up unfinished entities on the fly.
118: * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
119: */
120:
121: #define CUR (*ctxt->input->cur)
122: #define UPPER (toupper(*ctxt->input->cur))
123: #define SKIP(val) ctxt->input->cur += (val)
124: #define NXT(val) ctxt->input->cur[(val)]
125: #define UPP(val) (toupper(ctxt->input->cur[(val)]))
126: #define CUR_PTR ctxt->input->cur
1.5 daniel 127: #define SHRINK xmlParserInputShrink(ctxt->input)
128: #define GROW xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
1.1 daniel 129:
130: #define SKIP_BLANKS \
131: while (IS_BLANK(*(ctxt->input->cur))) NEXT
132:
133: #ifndef USE_UTF_8
134: #define CURRENT (*ctxt->input->cur)
1.5 daniel 135: #define NEXT { \
136: if ((*ctxt->input->cur == 0) && \
137: (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) { \
138: xmlPopInput(ctxt); \
139: } else { \
140: if (*(ctxt->input->cur) == '\n') { \
141: ctxt->input->line++; ctxt->input->col = 1; \
142: } else ctxt->input->col++; \
143: ctxt->input->cur++; \
144: if (*ctxt->input->cur == 0) \
145: xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
146: }}
147:
148: /****************************************
1.1 daniel 149: #define NEXT ((*ctxt->input->cur) ? \
150: (((*(ctxt->input->cur) == '\n') ? \
151: (ctxt->input->line++, ctxt->input->col = 1) : \
1.5 daniel 152: (ctxt->input->col++)), \
153: (ctxt->input->cur++), \
154: ((*ctxt->input->cur) ? \
155: (xmlParserInputGrow(ctxt->input, 100), \
156: ctxt->input->cur): \
157: (ctxt->input->cur))) : \
158: ((xmlParserInputGrow(ctxt->input, 100) > 0) ? \
159: ctxt->input->cur: \
160: (xmlPopInput(ctxt), ctxt->input->cur)))
161: ****************************************/
1.1 daniel 162: #else
163: #endif
164:
165:
1.5 daniel 166:
1.1 daniel 167: /************************************************************************
168: * *
169: * The list of HTML elements and their properties *
170: * *
171: ************************************************************************/
172:
173: /*
174: * Start Tag: 1 means the start tag can be ommited
175: * End Tag: 1 means the end tag can be ommited
176: * 2 means it's forbidden (empty elements)
177: * Depr: this element is deprecated
178: * DTD: 1 means that this element is valid only in the Loose DTD
179: * 2 means that this element is valid only in the Frameset DTD
180: *
181: * Name,Start Tag,End Tag, Empty, Depr., DTD, Description
182: */
183: htmlElemDesc html40ElementTable[] = {
184: { "A", 0, 0, 0, 0, 0, "anchor " },
185: { "ABBR", 0, 0, 0, 0, 0, "abbreviated form" },
186: { "ACRONYM", 0, 0, 0, 0, 0, "" },
187: { "ADDRESS", 0, 0, 0, 0, 0, "information on author " },
188: { "APPLET", 0, 0, 0, 1, 1, "Java applet " },
189: { "AREA", 0, 2, 1, 0, 0, "client-side image map area " },
190: { "B", 0, 0, 0, 0, 0, "bold text style" },
191: { "BASE", 0, 2, 1, 0, 0, "document base URI " },
192: { "BASEFONT", 0, 2, 1, 1, 1, "base font size " },
193: { "BDO", 0, 0, 0, 0, 0, "I18N BiDi over-ride " },
194: { "BIG", 0, 0, 0, 0, 0, "large text style" },
195: { "BLOCKQUOTE", 0, 0, 0, 0, 0, "long quotation " },
196: { "BODY", 1, 1, 0, 0, 0, "document body " },
197: { "BR", 0, 2, 1, 0, 0, "forced line break " },
198: { "BUTTON", 0, 0, 0, 0, 0, "push button " },
199: { "CAPTION", 0, 0, 0, 0, 0, "table caption " },
200: { "CENTER", 0, 0, 0, 1, 1, "shorthand for DIV align=center " },
201: { "CITE", 0, 0, 0, 0, 0, "citation" },
202: { "CODE", 0, 0, 0, 0, 0, "computer code fragment" },
203: { "COL", 0, 2, 1, 0, 0, "table column " },
204: { "COLGROUP", 0, 1, 0, 0, 0, "table column group " },
205: { "DD", 0, 1, 0, 0, 0, "definition description " },
206: { "DEL", 0, 0, 0, 0, 0, "deleted text " },
207: { "DFN", 0, 0, 0, 0, 0, "instance definition" },
208: { "DIR", 0, 0, 0, 1, 1, "directory list" },
209: { "DIV", 0, 0, 0, 0, 0, "generic language/style container"},
210: { "DL", 0, 0, 0, 0, 0, "definition list " },
211: { "DT", 0, 1, 0, 0, 0, "definition term " },
212: { "EM", 0, 0, 0, 0, 0, "emphasis" },
213: { "FIELDSET", 0, 0, 0, 0, 0, "form control group " },
214: { "FONT", 0, 0, 0, 1, 1, "local change to font " },
215: { "FORM", 0, 0, 0, 0, 0, "interactive form " },
216: { "FRAME", 0, 2, 1, 0, 2, "subwindow " },
217: { "FRAMESET", 0, 0, 0, 0, 2, "window subdivision" },
218: { "H1", 0, 0, 0, 0, 0, "heading " },
219: { "H2", 0, 0, 0, 0, 0, "heading " },
220: { "H3", 0, 0, 0, 0, 0, "heading " },
221: { "H4", 0, 0, 0, 0, 0, "heading " },
222: { "H5", 0, 0, 0, 0, 0, "heading " },
223: { "H6", 0, 0, 0, 0, 0, "heading " },
224: { "HEAD", 1, 1, 0, 0, 0, "document head " },
225: { "HR", 0, 2, 1, 0, 0, "horizontal rule " },
226: { "HTML", 1, 1, 0, 0, 0, "document root element " },
227: { "I", 0, 0, 0, 0, 0, "italic text style" },
228: { "IFRAME", 0, 0, 0, 0, 1, "inline subwindow " },
229: { "IMG", 0, 2, 1, 0, 0, "Embedded image " },
230: { "INPUT", 0, 2, 1, 0, 0, "form control " },
231: { "INS", 0, 0, 0, 0, 0, "inserted text" },
232: { "ISINDEX", 0, 2, 1, 1, 1, "single line prompt " },
233: { "KBD", 0, 0, 0, 0, 0, "text to be entered by the user" },
234: { "LABEL", 0, 0, 0, 0, 0, "form field label text " },
235: { "LEGEND", 0, 0, 0, 0, 0, "fieldset legend " },
236: { "LI", 0, 1, 0, 0, 0, "list item " },
237: { "LINK", 0, 2, 1, 0, 0, "a media-independent link " },
238: { "MAP", 0, 0, 0, 0, 0, "client-side image map " },
239: { "MENU", 0, 0, 0, 1, 1, "menu list " },
240: { "META", 0, 2, 1, 0, 0, "generic metainformation " },
241: { "NOFRAMES", 0, 0, 0, 0, 2, "alternate content container for non frame-based rendering " },
242: { "NOSCRIPT", 0, 0, 0, 0, 0, "alternate content container for non script-based rendering " },
243: { "OBJECT", 0, 0, 0, 0, 0, "generic embedded object " },
244: { "OL", 0, 0, 0, 0, 0, "ordered list " },
245: { "OPTGROUP", 0, 0, 0, 0, 0, "option group " },
246: { "OPTION", 0, 1, 0, 0, 0, "selectable choice " },
247: { "P", 0, 1, 0, 0, 0, "paragraph " },
248: { "PARAM", 0, 2, 1, 0, 0, "named property value " },
249: { "PRE", 0, 0, 0, 0, 0, "preformatted text " },
250: { "Q", 0, 0, 0, 0, 0, "short inline quotation " },
251: { "S", 0, 0, 0, 1, 1, "strike-through text style" },
252: { "SAMP", 0, 0, 0, 0, 0, "sample program output, scripts, etc." },
253: { "SCRIPT", 0, 0, 0, 0, 0, "script statements " },
254: { "SELECT", 0, 0, 0, 0, 0, "option selector " },
255: { "SMALL", 0, 0, 0, 0, 0, "small text style" },
256: { "SPAN", 0, 0, 0, 0, 0, "generic language/style container " },
257: { "STRIKE", 0, 0, 0, 1, 1, "strike-through text" },
258: { "STRONG", 0, 0, 0, 0, 0, "strong emphasis" },
259: { "STYLE", 0, 0, 0, 0, 0, "style info " },
260: { "SUB", 0, 0, 0, 0, 0, "subscript" },
261: { "SUP", 0, 0, 0, 0, 0, "superscript " },
262: { "TABLE", 0, 0, 0, 0, 0, " " },
263: { "TBODY", 1, 1, 0, 0, 0, "table body " },
264: { "TD", 0, 1, 0, 0, 0, "table data cell" },
265: { "TEXTAREA", 0, 0, 0, 0, 0, "multi-line text field " },
266: { "TFOOT", 0, 1, 0, 0, 0, "table footer " },
267: { "TH", 0, 1, 0, 0, 0, "table header cell" },
268: { "THEAD", 0, 1, 0, 0, 0, "table header " },
269: { "TITLE", 0, 0, 0, 0, 0, "document title " },
270: { "TR", 0, 1, 0, 0, 0, "table row " },
271: { "TT", 0, 0, 0, 0, 0, "teletype or monospaced text style" },
272: { "U", 0, 0, 0, 1, 1, "underlined text style" },
273: { "UL", 0, 0, 0, 0, 0, "unordered list " },
274: { "VAR", 0, 0, 0, 0, 0, "instance of a variable or program argument" },
275: };
276:
277: /*
278: * start tags that imply the end of a current element
279: * any tag of each line implies the end of the current element if the type of
280: * that element is in the same line
281: */
1.8 daniel 282: char *htmlEquEnd[] = {
1.1 daniel 283: "DT", "DD", "LI", "OPTION", NULL,
284: "H1", "H2", "H3", "H4", "H5", "H6", NULL,
285: "OL", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP", NULL,
286: NULL
287: };
288: /*
289: * acording the HTML DTD, HR should be added to the 2nd line above, as it
290: * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
291: * because many documents contain rules in headings...
292: */
293:
294: /*
295: * start tags that imply the end of current element
296: */
1.8 daniel 297: char *htmlStartClose[] = {
1.1 daniel 298: "FORM", "FORM", "P", "HR", "H1", "H2", "H3", "H4", "H5", "H6",
299: "DL", "UL", "OL", "MENU", "DIR", "ADDRESS", "PRE",
300: "LISTING", "XMP", "HEAD", NULL,
301: "HEAD", "P", NULL,
302: "TITLE", "P", NULL,
303: "BODY", "HEAD", "STYLE", "LINK", "TITLE", "P", NULL,
304: "LI", "P", "H1", "H2", "H3", "H4", "H5", "H6", "DL", "ADDRESS",
1.15 daniel 305: "PRE", "LISTING", "XMP", "HEAD", "LI", NULL,
1.1 daniel 306: "HR", "P", "HEAD", NULL,
307: "H1", "P", "HEAD", NULL,
308: "H2", "P", "HEAD", NULL,
309: "H3", "P", "HEAD", NULL,
310: "H4", "P", "HEAD", NULL,
311: "H5", "P", "HEAD", NULL,
312: "H6", "P", "HEAD", NULL,
313: "DIR", "P", "HEAD", NULL,
314: "ADDRESS", "P", "HEAD", "UL", NULL,
315: "PRE", "P", "HEAD", "UL", NULL,
316: "LISTING", "P", "HEAD", NULL,
317: "XMP", "P", "HEAD", NULL,
318: "BLOCKQUOTE", "P", "HEAD", NULL,
319: "DL", "P", "DT", "MENU", "DIR", "ADDRESS", "PRE", "LISTING",
320: "XMP", "HEAD", NULL,
1.21 ! daniel 321: "DT", "P", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP",
! 322: "HEAD", "DD", NULL,
! 323: "DD", "P", "MENU", "DIR", "ADDRESS", "PRE", "LISTING", "XMP",
! 324: "HEAD", "DT", NULL,
1.1 daniel 325: "UL", "P", "HEAD", "OL", "MENU", "DIR", "ADDRESS", "PRE",
326: "LISTING", "XMP", NULL,
327: "OL", "P", "HEAD", "UL", NULL,
328: "MENU", "P", "HEAD", "UL", NULL,
329: "P", "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6", NULL,
330: "DIV", "P", "HEAD", NULL,
331: "NOSCRIPT", "P", "HEAD", NULL,
332: "CENTER", "FONT", "B", "I", "P", "HEAD", NULL,
333: "A", "A", NULL,
334: "CAPTION", "P", NULL,
335: "COLGROUP", "CAPTION", "COLGROUP", "COL", "P", NULL,
336: "COL", "CAPTION", "COL", "P", NULL,
337: "TABLE", "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6", "PRE",
338: "LISTING", "XMP", "A", NULL,
339: "TH", "TH", "TD", NULL,
1.16 daniel 340: "TD", "TH", "TD", "P", NULL,
341: "TR", "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", "P", NULL,
1.1 daniel 342: "THEAD", "CAPTION", "COL", "COLGROUP", NULL,
343: "TFOOT", "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", "THEAD",
1.16 daniel 344: "TBODY", "P", NULL,
1.1 daniel 345: "TBODY", "TH", "TD", "TR", "CAPTION", "COL", "COLGROUP", "THEAD",
1.16 daniel 346: "TFOOT", "TBODY", "P", NULL,
1.1 daniel 347: "OPTGROUP", "OPTION", NULL,
348: "FIELDSET", "LEGEND", "P", "HEAD", "H1", "H2", "H3", "H4", "H5", "H6",
349: "PRE", "LISTING", "XMP", "A", NULL,
350: NULL
351: };
352:
1.8 daniel 353: static char** htmlStartCloseIndex[100];
1.1 daniel 354: static int htmlStartCloseIndexinitialized = 0;
355:
356: /************************************************************************
357: * *
358: * functions to handle HTML specific data *
359: * *
360: ************************************************************************/
361:
362: /**
363: * htmlInitAutoClose:
364: *
365: * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
366: *
367: */
368: void
369: htmlInitAutoClose(void) {
370: int index, i = 0;
371:
372: if (htmlStartCloseIndexinitialized) return;
373:
374: for (index = 0;index < 100;index ++) htmlStartCloseIndex[index] = NULL;
375: index = 0;
376: while ((htmlStartClose[i] != NULL) && (index < 100 - 1)) {
377: htmlStartCloseIndex[index++] = &htmlStartClose[i];
378: while (htmlStartClose[i] != NULL) i++;
379: i++;
380: }
381: }
382:
383: /**
384: * htmlTagLookup:
385: * @tag: The tag name
386: *
387: * Lookup the HTML tag in the ElementTable
388: *
389: * Returns the related htmlElemDescPtr or NULL if not found.
390: */
391: htmlElemDescPtr
1.14 daniel 392: htmlTagLookup(const xmlChar *tag) {
1.1 daniel 393: int i = 0;
394:
395: for (i = 0; i < (sizeof(html40ElementTable) /
396: sizeof(html40ElementTable[0]));i++) {
1.8 daniel 397: if (!xmlStrcmp(tag, BAD_CAST html40ElementTable[i].name))
1.1 daniel 398: return(&html40ElementTable[i]);
399: }
400: return(NULL);
401: }
402:
403: /**
404: * htmlCheckAutoClose:
405: * @new: The new tag name
406: * @old: The old tag name
407: *
408: * Checks wether the new tag is one of the registered valid tags for closing old.
409: * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
410: *
411: * Returns 0 if no, 1 if yes.
412: */
413: int
1.14 daniel 414: htmlCheckAutoClose(const xmlChar *new, const xmlChar *old) {
1.1 daniel 415: int i, index;
1.8 daniel 416: char **close;
1.1 daniel 417:
418: if (htmlStartCloseIndexinitialized == 0) htmlInitAutoClose();
419:
420: /* inefficient, but not a big deal */
421: for (index = 0; index < 100;index++) {
422: close = htmlStartCloseIndex[index];
423: if (close == NULL) return(0);
1.8 daniel 424: if (!xmlStrcmp(BAD_CAST *close, new)) break;
1.1 daniel 425: }
426:
427: i = close - htmlStartClose;
428: i++;
429: while (htmlStartClose[i] != NULL) {
1.8 daniel 430: if (!xmlStrcmp(BAD_CAST htmlStartClose[i], old)) {
1.1 daniel 431: return(1);
432: }
433: i++;
434: }
435: return(0);
436: }
437:
438: /**
439: * htmlAutoClose:
440: * @ctxt: an HTML parser context
441: * @new: The new tag name
442: *
443: * The HTmL DtD allows a tag to implicitely close other tags.
444: * The list is kept in htmlStartClose array. This function is
445: * called when a new tag has been detected and generates the
446: * appropriates closes if possible/needed.
447: */
448: void
1.14 daniel 449: htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar *new) {
1.15 daniel 450: xmlChar *oldname;
451: while ((ctxt->name != NULL) &&
452: (htmlCheckAutoClose(new, ctxt->name))) {
1.1 daniel 453: #ifdef DEBUG
1.18 daniel 454: fprintf(stderr,"htmlAutoClose: %s closes %s\n", new, ctxt->name);
1.1 daniel 455: #endif
456: if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1.15 daniel 457: ctxt->sax->endElement(ctxt->userData, ctxt->name);
458: oldname = ctxt->name;
459: htmlnamePop(ctxt);
1.18 daniel 460: if (oldname != NULL) {
461: #ifdef DEBUG
462: fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
463: #endif
1.17 daniel 464: xmlFree(oldname);
1.18 daniel 465: }
1.1 daniel 466: }
467: }
468:
469: /**
470: * htmlAutoCloseOnClose:
471: * @ctxt: an HTML parser context
472: * @new: The new tag name
473: *
474: * The HTmL DtD allows an ending tag to implicitely close other tags.
475: */
476: void
1.14 daniel 477: htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar *new) {
1.1 daniel 478: htmlElemDescPtr info;
1.15 daniel 479: xmlChar *oldname;
1.18 daniel 480: int i;
1.1 daniel 481:
1.18 daniel 482: #ifdef DEBUG
483: fprintf(stderr,"Close of %s stack: %d elements\n", new, ctxt->nameNr);
484: for (i = 0;i < ctxt->nameNr;i++)
485: fprintf(stderr,"%d : %s\n", i, ctxt->nameTab[i]);
486: #endif
487:
488: for (i = (ctxt->nameNr - 1);i >= 0;i--) {
489: if (!xmlStrcmp(new, ctxt->nameTab[i])) break;
490: }
491: if (i < 0) return;
492:
493: while (xmlStrcmp(new, ctxt->name)) {
1.15 daniel 494: info = htmlTagLookup(ctxt->name);
1.1 daniel 495: if ((info == NULL) || (info->endTag == 1)) {
496: #ifdef DEBUG
1.18 daniel 497: fprintf(stderr,"htmlAutoCloseOnClose: %s closes %s\n", new, ctxt->name);
498: #endif
499: } else {
500: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
501: ctxt->sax->error(ctxt->userData,
502: "Opening and ending tag mismatch: %s and %s\n",
503: new, ctxt->name);
504: ctxt->wellFormed = 0;
505: }
506: if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
507: ctxt->sax->endElement(ctxt->userData, ctxt->name);
508: oldname = ctxt->name;
509: htmlnamePop(ctxt);
510: if (oldname != NULL) {
511: #ifdef DEBUG
512: fprintf(stderr,"htmlAutoCloseOnClose: popped %s\n", oldname);
1.1 daniel 513: #endif
1.18 daniel 514: xmlFree(oldname);
515: }
1.1 daniel 516: }
517: }
518:
519: /************************************************************************
520: * *
521: * The list of HTML predefined entities *
522: * *
523: ************************************************************************/
524:
525:
526: htmlEntityDesc html40EntitiesTable[] = {
527: /*
528: * the 4 absolute ones,
529: */
530: { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
531: { 38, "amp", "ampersand, U+0026 ISOnum" },
1.4 daniel 532: { 39, "apos", "single quote" },
1.1 daniel 533: { 60, "lt", "less-than sign, U+003C ISOnum" },
534: { 62, "gt", "greater-than sign, U+003E ISOnum" },
535:
536: /*
537: * A bunch still in the 128-255 range
538: * Replacing them depend really on the charset used.
539: */
540: { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
541: { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
542: { 162, "cent", "cent sign, U+00A2 ISOnum" },
543: { 163, "pound","pound sign, U+00A3 ISOnum" },
544: { 164, "curren","currency sign, U+00A4 ISOnum" },
545: { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
546: { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
547: { 167, "sect", "section sign, U+00A7 ISOnum" },
548: { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
549: { 169, "copy", "copyright sign, U+00A9 ISOnum" },
550: { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
551: { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
552: { 172, "not", "not sign, U+00AC ISOnum" },
553: { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
554: { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
555: { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
556: { 176, "deg", "degree sign, U+00B0 ISOnum" },
557: { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
558: { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
559: { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
560: { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
561: { 181, "micro","micro sign, U+00B5 ISOnum" },
562: { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1.7 daniel 563: { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1.1 daniel 564: { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
565: { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
566: { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1.7 daniel 567: { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1.1 daniel 568: { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
569: { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
570: { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
571: { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
572: { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
573: { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
574: { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
575: { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
576: { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
577: { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
578: { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
579: { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
580: { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
581: { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
582: { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
583: { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
584: { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
585: { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
586: { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
587: { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
588: { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
589: { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
590: { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
591: { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
592: { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
593: { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
594: { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
595: { 215, "times","multiplication sign, U+00D7 ISOnum" },
1.7 daniel 596: { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1.1 daniel 597: { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
598: { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
599: { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
600: { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
601: { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
602: { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
603: { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
604: { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
605: { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
606: { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
607: { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
608: { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
609: { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
610: { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
611: { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
612: { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
613: { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
614: { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
615: { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
616: { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
617: { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
618: { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
619: { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
620: { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
621: { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
622: { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
623: { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
624: { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
625: { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
626: { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
627: { 247, "divide","division sign, U+00F7 ISOnum" },
628: { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
629: { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
630: { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
631: { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
632: { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
633: { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
634: { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
635: { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
636:
637: /*
638: * Anything below should really be kept as entities references
639: */
640: { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
641:
642: { 913, "Alpha","greek capital letter alpha, U+0391" },
643: { 914, "Beta", "greek capital letter beta, U+0392" },
644: { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
645: { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
646: { 917, "Epsilon","greek capital letter epsilon, U+0395" },
647: { 918, "Zeta", "greek capital letter zeta, U+0396" },
648: { 919, "Eta", "greek capital letter eta, U+0397" },
649: { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
650: { 921, "Iota", "greek capital letter iota, U+0399" },
651: { 922, "Kappa","greek capital letter kappa, U+039A" },
652: { 923, "Lambda""greek capital letter lambda, U+039B ISOgrk3" },
653: { 924, "Mu", "greek capital letter mu, U+039C" },
654: { 925, "Nu", "greek capital letter nu, U+039D" },
655: { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
656: { 927, "Omicron","greek capital letter omicron, U+039F" },
657: { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
658: { 929, "Rho", "greek capital letter rho, U+03A1" },
659: { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
660: { 932, "Tau", "greek capital letter tau, U+03A4" },
661: { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
662: { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
663: { 935, "Chi", "greek capital letter chi, U+03A7" },
664: { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
665: { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
666:
667: { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
668: { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
669: { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
670: { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
671: { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
672: { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
673: { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
674: { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
675: { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
676: { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
677: { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
678: { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
679: { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
680: { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
681: { 959, "omicron","greek small letter omicron, U+03BF NEW" },
682: { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
683: { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
684: { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
685: { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
686: { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
687: { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
688: { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
689: { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
690: { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
691: { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
692: { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
693: { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
694: { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
695:
696: { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
697: { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
698: { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
699: { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
700: { 8254, "oline","overline = spacing overscore, U+203E NEW" },
701: { 8260, "frasl","fraction slash, U+2044 NEW" },
702:
1.7 daniel 703: { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1.1 daniel 704: { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
705: { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
706: { 8482, "trade","trade mark sign, U+2122 ISOnum" },
707: { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
708: { 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
709: { 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
710: { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
711: { 8595, "darr", "downwards arrow, U+2193 ISOnum" },
712: { 8596, "harr", "left right arrow, U+2194 ISOamsa" },
713: { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
714: { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
715: { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
716: { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
717: { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
718: { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
719:
720:
721: { 8704, "forall","for all, U+2200 ISOtech" },
722: { 8706, "part", "partial differential, U+2202 ISOtech" },
723: { 8707, "exist","there exists, U+2203 ISOtech" },
724: { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
725: { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
726: { 8712, "isin", "element of, U+2208 ISOtech" },
727: { 8713, "notin","not an element of, U+2209 ISOtech" },
728: { 8715, "ni", "contains as member, U+220B ISOtech" },
729: { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
730: { 8721, "sum", "n-ary sumation, U+2211 ISOamsb" },
731: { 8722, "minus","minus sign, U+2212 ISOtech" },
732: { 8727, "lowast","asterisk operator, U+2217 ISOtech" },
733: { 8730, "radic","square root = radical sign, U+221A ISOtech" },
734: { 8733, "prop", "proportional to, U+221D ISOtech" },
735: { 8734, "infin","infinity, U+221E ISOtech" },
736: { 8736, "ang", "angle, U+2220 ISOamso" },
737: { 8743, "and", "logical and = wedge, U+2227 ISOtech" },
738: { 8744, "or", "logical or = vee, U+2228 ISOtech" },
739: { 8745, "cap", "intersection = cap, U+2229 ISOtech" },
740: { 8746, "cup", "union = cup, U+222A ISOtech" },
741: { 8747, "int", "integral, U+222B ISOtech" },
742: { 8756, "there4","therefore, U+2234 ISOtech" },
743: { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
744: { 8773, "cong", "approximately equal to, U+2245 ISOtech" },
745: { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
746: { 8800, "ne", "not equal to, U+2260 ISOtech" },
747: { 8801, "equiv","identical to, U+2261 ISOtech" },
748: { 8804, "le", "less-than or equal to, U+2264 ISOtech" },
749: { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
750: { 8834, "sub", "subset of, U+2282 ISOtech" },
751: { 8835, "sup", "superset of, U+2283 ISOtech" },
752: { 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
753: { 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
754: { 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
755: { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
756: { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
757: { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
758: { 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
759: { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
760: { 8969, "rceil","right ceiling, U+2309 ISOamsc" },
761: { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
762: { 8971, "rfloor","right floor, U+230B ISOamsc" },
763: { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
764: { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
765: { 9674, "loz", "lozenge, U+25CA ISOpub" },
766:
767: { 9824, "spades","black spade suit, U+2660 ISOpub" },
768: { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
769: { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
770: { 9830, "diams","black diamond suit, U+2666 ISOpub" },
771:
772: { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
773: { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
774: { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
775: { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
776: { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
777: { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
778: { 732, "tilde","small tilde, U+02DC ISOdia" },
779:
780: { 8194, "ensp", "en space, U+2002 ISOpub" },
781: { 8195, "emsp", "em space, U+2003 ISOpub" },
782: { 8201, "thinsp","thin space, U+2009 ISOpub" },
783: { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
784: { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
785: { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
786: { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
787: { 8211, "ndash","en dash, U+2013 ISOpub" },
788: { 8212, "mdash","em dash, U+2014 ISOpub" },
789: { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
790: { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
791: { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
792: { 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
793: { 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
794: { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
795: { 8224, "dagger","dagger, U+2020 ISOpub" },
796: { 8225, "Dagger","double dagger, U+2021 ISOpub" },
797: { 8240, "permil","per mille sign, U+2030 ISOtech" },
798: { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1.7 daniel 799: { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1.1 daniel 800: { 8364, "euro", "euro sign, U+20AC NEW" }
801: };
802:
803: /************************************************************************
804: * *
805: * Commodity functions to handle entities *
806: * *
807: ************************************************************************/
808:
809: /*
810: * Macro used to grow the current buffer.
811: */
812: #define growBuffer(buffer) { \
813: buffer##_size *= 2; \
1.14 daniel 814: buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
1.1 daniel 815: if (buffer == NULL) { \
816: perror("realloc failed"); \
817: exit(1); \
818: } \
819: }
820:
821: /**
822: * htmlEntityLookup:
823: * @name: the entity name
824: *
825: * Lookup the given entity in EntitiesTable
826: *
827: * TODO: the linear scan is really ugly, an hash table is really needed.
828: *
829: * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
830: */
831: htmlEntityDescPtr
1.14 daniel 832: htmlEntityLookup(const xmlChar *name) {
1.1 daniel 833: int i;
834:
835: for (i = 0;i < (sizeof(html40EntitiesTable)/
836: sizeof(html40EntitiesTable[0]));i++) {
1.8 daniel 837: if (!xmlStrcmp(name, BAD_CAST html40EntitiesTable[i].name)) {
1.1 daniel 838: #ifdef DEBUG
1.18 daniel 839: fprintf(stderr,"Found entity %s\n", name);
1.1 daniel 840: #endif
841: return(&html40EntitiesTable[i]);
842: }
843: }
844: return(NULL);
845: }
846:
847:
848: /**
849: * htmlDecodeEntities:
850: * @ctxt: the parser context
851: * @len: the len to decode (in bytes !), -1 for no size limit
1.14 daniel 852: * @end: an end marker xmlChar, 0 if none
853: * @end2: an end marker xmlChar, 0 if none
854: * @end3: an end marker xmlChar, 0 if none
1.1 daniel 855: *
856: * Subtitute the HTML entities by their value
857: *
1.19 daniel 858: * DEPRECATED !!!!
1.1 daniel 859: *
860: * Returns A newly allocated string with the substitution done. The caller
861: * must deallocate it !
862: */
1.14 daniel 863: xmlChar *
1.1 daniel 864: htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
1.14 daniel 865: xmlChar end, xmlChar end2, xmlChar end3) {
866: xmlChar *buffer = NULL;
1.1 daniel 867: int buffer_size = 0;
1.14 daniel 868: xmlChar *out = NULL;
869: xmlChar *name = NULL;
1.1 daniel 870:
1.14 daniel 871: xmlChar *cur = NULL;
1.1 daniel 872: htmlEntityDescPtr ent;
1.5 daniel 873: int nbchars = 0;
1.1 daniel 874: unsigned int max = (unsigned int) len;
875:
876: /*
877: * allocate a translation buffer.
878: */
879: buffer_size = 1000;
1.14 daniel 880: buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
1.1 daniel 881: if (buffer == NULL) {
882: perror("htmlDecodeEntities: malloc failed");
883: return(NULL);
884: }
885: out = buffer;
886:
887: /*
888: * Ok loop until we reach one of the ending char or a size limit.
889: */
1.5 daniel 890: while ((nbchars < max) && (CUR != end) &&
1.1 daniel 891: (CUR != end2) && (CUR != end3)) {
892:
893: if (CUR == '&') {
894: if (NXT(1) == '#') {
895: int val = htmlParseCharRef(ctxt);
1.8 daniel 896: /* invalid for UTF-8 variable encoding !!!!! */
1.1 daniel 897: *out++ = val;
1.5 daniel 898: nbchars += 3; /* !!!! */
1.1 daniel 899: } else {
900: ent = htmlParseEntityRef(ctxt, &name);
901: if (name != NULL) {
902: if ((ent == NULL) || (ent->value <= 0) ||
903: (ent->value >= 255)) {
904: *out++ = '&';
905: cur = name;
906: while (*cur != 0) {
907: if (out - buffer > buffer_size - 100) {
908: int index = out - buffer;
909:
910: growBuffer(buffer);
911: out = &buffer[index];
912: }
913: *out++ = *cur++;
914: }
915: *out++ = ';';
916: } else {
1.8 daniel 917: /* invalid for UTF-8 variable encoding !!!!! */
1.14 daniel 918: *out++ = (xmlChar)ent->value;
1.1 daniel 919: if (out - buffer > buffer_size - 100) {
920: int index = out - buffer;
921:
922: growBuffer(buffer);
923: out = &buffer[index];
924: }
925: }
1.5 daniel 926: nbchars += 2 + xmlStrlen(name);
1.11 daniel 927: xmlFree(name);
1.1 daniel 928: }
929: }
930: } else {
1.8 daniel 931: /* invalid for UTF-8 , use COPY(out); !!!!! */
1.1 daniel 932: *out++ = CUR;
1.5 daniel 933: nbchars++;
1.1 daniel 934: if (out - buffer > buffer_size - 100) {
935: int index = out - buffer;
936:
937: growBuffer(buffer);
938: out = &buffer[index];
939: }
940: NEXT;
941: }
942: }
943: *out++ = 0;
944: return(buffer);
945: }
946:
947:
948: /************************************************************************
949: * *
950: * Commodity functions to handle encodings *
951: * *
952: ************************************************************************/
953:
954: /**
955: * htmlSwitchEncoding:
956: * @ctxt: the parser context
957: * @len: the len of @cur
958: *
959: * change the input functions when discovering the character encoding
960: * of a given entity.
961: *
962: */
963: void
964: htmlSwitchEncoding(htmlParserCtxtPtr ctxt, xmlCharEncoding enc)
965: {
966: switch (enc) {
967: case XML_CHAR_ENCODING_ERROR:
968: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
969: ctxt->sax->error(ctxt->userData, "encoding unknown\n");
970: ctxt->wellFormed = 0;
971: break;
972: case XML_CHAR_ENCODING_NONE:
973: /* let's assume it's UTF-8 without the XML decl */
974: return;
975: case XML_CHAR_ENCODING_UTF8:
976: /* default encoding, no conversion should be needed */
977: return;
978: case XML_CHAR_ENCODING_UTF16LE:
979: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
980: ctxt->sax->error(ctxt->userData,
981: "char encoding UTF16 little endian not supported\n");
982: break;
983: case XML_CHAR_ENCODING_UTF16BE:
984: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
985: ctxt->sax->error(ctxt->userData,
986: "char encoding UTF16 big endian not supported\n");
987: break;
988: case XML_CHAR_ENCODING_UCS4LE:
989: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
990: ctxt->sax->error(ctxt->userData,
991: "char encoding USC4 little endian not supported\n");
992: break;
993: case XML_CHAR_ENCODING_UCS4BE:
994: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
995: ctxt->sax->error(ctxt->userData,
996: "char encoding USC4 big endian not supported\n");
997: break;
998: case XML_CHAR_ENCODING_EBCDIC:
999: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1000: ctxt->sax->error(ctxt->userData,
1001: "char encoding EBCDIC not supported\n");
1002: break;
1003: case XML_CHAR_ENCODING_UCS4_2143:
1004: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1005: ctxt->sax->error(ctxt->userData,
1006: "char encoding UCS4 2143 not supported\n");
1007: break;
1008: case XML_CHAR_ENCODING_UCS4_3412:
1009: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1010: ctxt->sax->error(ctxt->userData,
1011: "char encoding UCS4 3412 not supported\n");
1012: break;
1013: case XML_CHAR_ENCODING_UCS2:
1014: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1015: ctxt->sax->error(ctxt->userData,
1016: "char encoding UCS2 not supported\n");
1017: break;
1018: case XML_CHAR_ENCODING_8859_1:
1019: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1020: ctxt->sax->error(ctxt->userData,
1021: "char encoding ISO_8859_1 ISO Latin 1 not supported\n");
1022: break;
1023: case XML_CHAR_ENCODING_8859_2:
1024: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1025: ctxt->sax->error(ctxt->userData,
1026: "char encoding ISO_8859_2 ISO Latin 2 not supported\n");
1027: break;
1028: case XML_CHAR_ENCODING_8859_3:
1029: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1030: ctxt->sax->error(ctxt->userData,
1031: "char encoding ISO_8859_3 not supported\n");
1032: break;
1033: case XML_CHAR_ENCODING_8859_4:
1034: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1035: ctxt->sax->error(ctxt->userData,
1036: "char encoding ISO_8859_4 not supported\n");
1037: break;
1038: case XML_CHAR_ENCODING_8859_5:
1039: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1040: ctxt->sax->error(ctxt->userData,
1041: "char encoding ISO_8859_5 not supported\n");
1042: break;
1043: case XML_CHAR_ENCODING_8859_6:
1044: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1045: ctxt->sax->error(ctxt->userData,
1046: "char encoding ISO_8859_6 not supported\n");
1047: break;
1048: case XML_CHAR_ENCODING_8859_7:
1049: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1050: ctxt->sax->error(ctxt->userData,
1051: "char encoding ISO_8859_7 not supported\n");
1052: break;
1053: case XML_CHAR_ENCODING_8859_8:
1054: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1055: ctxt->sax->error(ctxt->userData,
1056: "char encoding ISO_8859_8 not supported\n");
1057: break;
1058: case XML_CHAR_ENCODING_8859_9:
1059: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1060: ctxt->sax->error(ctxt->userData,
1061: "char encoding ISO_8859_9 not supported\n");
1062: break;
1063: case XML_CHAR_ENCODING_2022_JP:
1064: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1065: ctxt->sax->error(ctxt->userData,
1066: "char encoding ISO-2022-JPnot supported\n");
1067: break;
1068: case XML_CHAR_ENCODING_SHIFT_JIS:
1069: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1070: ctxt->sax->error(ctxt->userData,
1071: "char encoding Shift_JISnot supported\n");
1072: break;
1073: case XML_CHAR_ENCODING_EUC_JP:
1074: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1075: ctxt->sax->error(ctxt->userData,
1076: "char encoding EUC-JPnot supported\n");
1077: break;
1078: }
1079: }
1080:
1081:
1082: /************************************************************************
1083: * *
1084: * Commodity functions, cleanup needed ? *
1085: * *
1086: ************************************************************************/
1087:
1088: /**
1089: * areBlanks:
1090: * @ctxt: an HTML parser context
1.14 daniel 1091: * @str: a xmlChar *
1.1 daniel 1092: * @len: the size of @str
1093: *
1094: * Is this a sequence of blank chars that one can ignore ?
1095: *
1096: * Returns 1 if ignorable 0 otherwise.
1097: */
1098:
1.14 daniel 1099: static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1.1 daniel 1100: int i;
1101: xmlNodePtr lastChild;
1102:
1103: for (i = 0;i < len;i++)
1104: if (!(IS_BLANK(str[i]))) return(0);
1105:
1106: if (CUR != '<') return(0);
1107: if (ctxt->node == NULL) return(0);
1108: lastChild = xmlGetLastChild(ctxt->node);
1109: if (lastChild == NULL) {
1110: if (ctxt->node->content != NULL) return(0);
1111: } else if (xmlNodeIsText(lastChild))
1112: return(0);
1113: return(1);
1114: }
1115:
1116: /**
1117: * htmlHandleEntity:
1118: * @ctxt: an HTML parser context
1119: * @entity: an XML entity pointer.
1120: *
1121: * Default handling of an HTML entity, call the parser with the
1122: * substitution string
1123: */
1124:
1125: void
1126: htmlHandleEntity(htmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1127: int len;
1128:
1129: if (entity->content == NULL) {
1130: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1131: ctxt->sax->error(ctxt->userData, "htmlHandleEntity %s: content == NULL\n",
1132: entity->name);
1133: ctxt->wellFormed = 0;
1134: return;
1135: }
1136: len = xmlStrlen(entity->content);
1137:
1138: /*
1139: * Just handle the content as a set of chars.
1140: */
1141: if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
1142: ctxt->sax->characters(ctxt->userData, entity->content, len);
1143:
1144: }
1145:
1146: /**
1147: * htmlNewDoc:
1148: * @URI: URI for the dtd, or NULL
1149: * @ExternalID: the external ID of the DTD, or NULL
1150: *
1151: * Returns a new document
1152: */
1153: htmlDocPtr
1.14 daniel 1154: htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
1.1 daniel 1155: xmlDocPtr cur;
1156:
1157: /*
1158: * Allocate a new document and fill the fields.
1159: */
1.11 daniel 1160: cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1.1 daniel 1161: if (cur == NULL) {
1162: fprintf(stderr, "xmlNewDoc : malloc failed\n");
1163: return(NULL);
1164: }
1.10 daniel 1165: memset(cur, 0, sizeof(xmlDoc));
1.1 daniel 1166:
1.20 daniel 1167: cur->type = XML_HTML_DOCUMENT_NODE;
1.1 daniel 1168: cur->version = NULL;
1169: cur->intSubset = NULL;
1.8 daniel 1170: xmlCreateIntSubset(cur, BAD_CAST "HTML", ExternalID, URI);
1.1 daniel 1171: cur->name = NULL;
1172: cur->root = NULL;
1173: cur->extSubset = NULL;
1174: cur->oldNs = NULL;
1175: cur->encoding = NULL;
1176: cur->standalone = 1;
1177: cur->compression = 0;
1.12 daniel 1178: cur->ids = NULL;
1179: cur->refs = NULL;
1.1 daniel 1180: #ifndef XML_WITHOUT_CORBA
1181: cur->_private = NULL;
1182: cur->vepv = NULL;
1183: #endif
1184: return(cur);
1185: }
1186:
1187:
1188: /************************************************************************
1189: * *
1190: * The parser itself *
1191: * Relates to http://www.w3.org/TR/html40 *
1192: * *
1193: ************************************************************************/
1194:
1195: /************************************************************************
1196: * *
1197: * The parser itself *
1198: * *
1199: ************************************************************************/
1200:
1201: /**
1202: * htmlParseHTMLName:
1203: * @ctxt: an HTML parser context
1204: *
1205: * parse an HTML tag or attribute name, note that we convert it to uppercase
1206: * since HTML names are not case-sensitive.
1207: *
1208: * Returns the Tag Name parsed or NULL
1209: */
1210:
1.14 daniel 1211: xmlChar *
1.1 daniel 1212: htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
1.14 daniel 1213: xmlChar *ret = NULL;
1.1 daniel 1214: int i = 0;
1.14 daniel 1215: xmlChar loc[100];
1.1 daniel 1216:
1217: if (!IS_LETTER(CUR) && (CUR != '_') &&
1218: (CUR != ':')) return(NULL);
1219:
1220: while ((i < 100) && ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)))) {
1221: if ((CUR >= 0x61) && (CUR <= 0x7a)) loc[i] = CUR - 0x20;
1222: else loc[i] = CUR;
1223: i++;
1224:
1225: NEXT;
1226: }
1227:
1228: ret = xmlStrndup(loc, i);
1229:
1230: return(ret);
1231: }
1232:
1233: /**
1234: * htmlParseName:
1235: * @ctxt: an HTML parser context
1236: *
1237: * parse an HTML name, this routine is case sensistive.
1238: *
1239: * Returns the Name parsed or NULL
1240: */
1241:
1.14 daniel 1242: xmlChar *
1.1 daniel 1243: htmlParseName(htmlParserCtxtPtr ctxt) {
1.14 daniel 1244: xmlChar buf[HTML_MAX_NAMELEN];
1.5 daniel 1245: int len = 0;
1.1 daniel 1246:
1.5 daniel 1247: GROW;
1248: if (!IS_LETTER(CUR) && (CUR != '_')) {
1249: return(NULL);
1250: }
1.1 daniel 1251:
1252: while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1253: (CUR == '.') || (CUR == '-') ||
1254: (CUR == '_') || (CUR == ':') ||
1255: (IS_COMBINING(CUR)) ||
1.5 daniel 1256: (IS_EXTENDER(CUR))) {
1257: buf[len++] = CUR;
1.1 daniel 1258: NEXT;
1.5 daniel 1259: if (len >= HTML_MAX_NAMELEN) {
1260: fprintf(stderr,
1261: "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
1262: while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1263: (CUR == '.') || (CUR == '-') ||
1264: (CUR == '_') || (CUR == ':') ||
1265: (IS_COMBINING(CUR)) ||
1266: (IS_EXTENDER(CUR)))
1267: NEXT;
1268: break;
1269: }
1270: }
1271: return(xmlStrndup(buf, len));
1.1 daniel 1272: }
1273:
1274: /**
1275: * htmlParseHTMLAttribute:
1276: * @ctxt: an HTML parser context
1.19 daniel 1277: * @stop: a char stop value
1.1 daniel 1278: *
1.19 daniel 1279: * parse an HTML attribute value till the stop (quote), if
1280: * stop is 0 then it stops at the first space
1.1 daniel 1281: *
1.19 daniel 1282: * Returns the attribute parsed or NULL
1.1 daniel 1283: */
1284:
1.14 daniel 1285: xmlChar *
1.19 daniel 1286: htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
1.14 daniel 1287: xmlChar buf[HTML_MAX_NAMELEN];
1.5 daniel 1288: int len = 0;
1.1 daniel 1289:
1.5 daniel 1290: GROW;
1.19 daniel 1291: while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
1292: if ((stop == 0) && (IS_BLANK(CUR))) break;
1.5 daniel 1293: buf[len++] = CUR;
1.1 daniel 1294: NEXT;
1.5 daniel 1295: if (len >= HTML_MAX_NAMELEN) {
1296: fprintf(stderr,
1297: "htmlParseHTMLAttribute: reached HTML_MAX_NAMELEN limit\n");
1298: while ((!IS_BLANK(CUR)) && (CUR != '<') &&
1.19 daniel 1299: (CUR != '>') &&
1.5 daniel 1300: (CUR != '\'') && (CUR != '"'))
1301: NEXT;
1302: break;
1303: }
1304: }
1305: return(xmlStrndup(buf, len));
1.1 daniel 1306: }
1307:
1308: /**
1309: * htmlParseNmtoken:
1310: * @ctxt: an HTML parser context
1311: *
1312: * parse an HTML Nmtoken.
1313: *
1314: * Returns the Nmtoken parsed or NULL
1315: */
1316:
1.14 daniel 1317: xmlChar *
1.1 daniel 1318: htmlParseNmtoken(htmlParserCtxtPtr ctxt) {
1.14 daniel 1319: xmlChar buf[HTML_MAX_NAMELEN];
1.5 daniel 1320: int len = 0;
1.1 daniel 1321:
1.5 daniel 1322: GROW;
1.1 daniel 1323: while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1324: (CUR == '.') || (CUR == '-') ||
1325: (CUR == '_') || (CUR == ':') ||
1326: (IS_COMBINING(CUR)) ||
1.5 daniel 1327: (IS_EXTENDER(CUR))) {
1328: buf[len++] = CUR;
1.1 daniel 1329: NEXT;
1.5 daniel 1330: if (len >= HTML_MAX_NAMELEN) {
1331: fprintf(stderr,
1332: "htmlParseNmtoken: reached HTML_MAX_NAMELEN limit\n");
1333: while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
1334: (CUR == '.') || (CUR == '-') ||
1335: (CUR == '_') || (CUR == ':') ||
1336: (IS_COMBINING(CUR)) ||
1337: (IS_EXTENDER(CUR)))
1338: NEXT;
1339: break;
1340: }
1341: }
1342: return(xmlStrndup(buf, len));
1.1 daniel 1343: }
1344:
1345: /**
1346: * htmlParseEntityRef:
1347: * @ctxt: an HTML parser context
1348: * @str: location to store the entity name
1349: *
1350: * parse an HTML ENTITY references
1351: *
1352: * [68] EntityRef ::= '&' Name ';'
1353: *
1354: * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
1355: * if non-NULL *str will have to be freed by the caller.
1356: */
1357: htmlEntityDescPtr
1.14 daniel 1358: htmlParseEntityRef(htmlParserCtxtPtr ctxt, xmlChar **str) {
1359: xmlChar *name;
1.1 daniel 1360: htmlEntityDescPtr ent = NULL;
1361: *str = NULL;
1362:
1363: if (CUR == '&') {
1364: NEXT;
1365: name = htmlParseName(ctxt);
1366: if (name == NULL) {
1367: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1368: ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
1369: ctxt->wellFormed = 0;
1370: } else {
1.5 daniel 1371: GROW;
1.1 daniel 1372: if (CUR == ';') {
1373: NEXT;
1374: *str = name;
1375:
1376: /*
1377: * Lookup the entity in the table.
1378: */
1379: ent = htmlEntityLookup(name);
1380: } else {
1381: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1382: ctxt->sax->error(ctxt->userData,
1383: "htmlParseEntityRef: expecting ';'\n");
1384: ctxt->wellFormed = 0;
1385: if (ctxt->sax->characters != NULL) {
1.8 daniel 1386: ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1 daniel 1387: ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1388: }
1.11 daniel 1389: xmlFree(name);
1.1 daniel 1390: }
1391: }
1392: }
1393: return(ent);
1394: }
1395:
1396: /**
1397: * htmlParseAttValue:
1398: * @ctxt: an HTML parser context
1399: *
1400: * parse a value for an attribute
1401: * Note: the parser won't do substitution of entities here, this
1402: * will be handled later in xmlStringGetNodeList, unless it was
1403: * asked for ctxt->replaceEntities != 0
1404: *
1405: * Returns the AttValue parsed or NULL.
1406: */
1407:
1.14 daniel 1408: xmlChar *
1.1 daniel 1409: htmlParseAttValue(htmlParserCtxtPtr ctxt) {
1.14 daniel 1410: xmlChar *ret = NULL;
1.1 daniel 1411:
1412: if (CUR == '"') {
1413: NEXT;
1.19 daniel 1414: ret = htmlParseHTMLAttribute(ctxt, '"');
1.1 daniel 1415: if (CUR != '"') {
1416: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1417: ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
1418: ctxt->wellFormed = 0;
1419: } else
1420: NEXT;
1421: } else if (CUR == '\'') {
1422: NEXT;
1.19 daniel 1423: ret = htmlParseHTMLAttribute(ctxt, '\'');
1.1 daniel 1424: if (CUR != '\'') {
1425: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1426: ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
1427: ctxt->wellFormed = 0;
1428: } else
1429: NEXT;
1430: } else {
1431: /*
1432: * That's an HTMLism, the attribute value may not be quoted
1433: */
1.19 daniel 1434: ret = htmlParseHTMLAttribute(ctxt, 0);
1.1 daniel 1435: if (ret == NULL) {
1436: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1437: ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
1438: ctxt->wellFormed = 0;
1439: }
1440: }
1441: return(ret);
1442: }
1443:
1444: /**
1445: * htmlParseSystemLiteral:
1446: * @ctxt: an HTML parser context
1447: *
1448: * parse an HTML Literal
1449: *
1450: * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
1451: *
1452: * Returns the SystemLiteral parsed or NULL
1453: */
1454:
1.14 daniel 1455: xmlChar *
1.1 daniel 1456: htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
1.14 daniel 1457: const xmlChar *q;
1458: xmlChar *ret = NULL;
1.1 daniel 1459:
1460: if (CUR == '"') {
1461: NEXT;
1462: q = CUR_PTR;
1463: while ((IS_CHAR(CUR)) && (CUR != '"'))
1464: NEXT;
1465: if (!IS_CHAR(CUR)) {
1466: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1467: ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
1468: ctxt->wellFormed = 0;
1469: } else {
1470: ret = xmlStrndup(q, CUR_PTR - q);
1471: NEXT;
1472: }
1473: } else if (CUR == '\'') {
1474: NEXT;
1475: q = CUR_PTR;
1476: while ((IS_CHAR(CUR)) && (CUR != '\''))
1477: NEXT;
1478: if (!IS_CHAR(CUR)) {
1479: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1480: ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
1481: ctxt->wellFormed = 0;
1482: } else {
1483: ret = xmlStrndup(q, CUR_PTR - q);
1484: NEXT;
1485: }
1486: } else {
1487: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1488: ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
1489: ctxt->wellFormed = 0;
1490: }
1491:
1492: return(ret);
1493: }
1494:
1495: /**
1496: * htmlParsePubidLiteral:
1497: * @ctxt: an HTML parser context
1498: *
1499: * parse an HTML public literal
1500: *
1501: * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
1502: *
1503: * Returns the PubidLiteral parsed or NULL.
1504: */
1505:
1.14 daniel 1506: xmlChar *
1.1 daniel 1507: htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
1.14 daniel 1508: const xmlChar *q;
1509: xmlChar *ret = NULL;
1.1 daniel 1510: /*
1511: * Name ::= (Letter | '_') (NameChar)*
1512: */
1513: if (CUR == '"') {
1514: NEXT;
1515: q = CUR_PTR;
1516: while (IS_PUBIDCHAR(CUR)) NEXT;
1517: if (CUR != '"') {
1518: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1519: ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
1520: ctxt->wellFormed = 0;
1521: } else {
1522: ret = xmlStrndup(q, CUR_PTR - q);
1523: NEXT;
1524: }
1525: } else if (CUR == '\'') {
1526: NEXT;
1527: q = CUR_PTR;
1528: while ((IS_LETTER(CUR)) && (CUR != '\''))
1529: NEXT;
1530: if (!IS_LETTER(CUR)) {
1531: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1532: ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
1533: ctxt->wellFormed = 0;
1534: } else {
1535: ret = xmlStrndup(q, CUR_PTR - q);
1536: NEXT;
1537: }
1538: } else {
1539: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1540: ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
1541: ctxt->wellFormed = 0;
1542: }
1543:
1544: return(ret);
1545: }
1546:
1547: /**
1548: * htmlParseCharData:
1549: * @ctxt: an HTML parser context
1550: * @cdata: int indicating whether we are within a CDATA section
1551: *
1552: * parse a CharData section.
1553: * if we are within a CDATA section ']]>' marks an end of section.
1554: *
1555: * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
1556: */
1557:
1558: void
1559: htmlParseCharData(htmlParserCtxtPtr ctxt, int cdata) {
1.14 daniel 1560: const xmlChar *q;
1.1 daniel 1561:
1562: q = CUR_PTR;
1563: while ((IS_CHAR(CUR)) && (CUR != '<') &&
1564: (CUR != '&')) {
1565: if ((CUR == ']') && (NXT(1) == ']') &&
1566: (NXT(2) == '>')) {
1567: if (cdata) break;
1568: else {
1569: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1570: ctxt->sax->error(ctxt->userData,
1571: "Sequence ']]>' not allowed in content\n");
1572: ctxt->wellFormed = 0;
1573: }
1574: }
1575: NEXT;
1576: }
1577: if (q == CUR_PTR) return;
1578:
1579: /*
1580: * Ok the segment [q CUR_PTR] is to be consumed as chars.
1581: */
1582: if (ctxt->sax != NULL) {
1583: if (areBlanks(ctxt, q, CUR_PTR - q)) {
1584: if (ctxt->sax->ignorableWhitespace != NULL)
1585: ctxt->sax->ignorableWhitespace(ctxt->userData, q, CUR_PTR - q);
1586: } else {
1587: if (ctxt->sax->characters != NULL)
1588: ctxt->sax->characters(ctxt->userData, q, CUR_PTR - q);
1589: }
1590: }
1591: }
1592:
1593: /**
1594: * htmlParseExternalID:
1595: * @ctxt: an HTML parser context
1.14 daniel 1596: * @publicID: a xmlChar** receiving PubidLiteral
1.1 daniel 1597: * @strict: indicate whether we should restrict parsing to only
1598: * production [75], see NOTE below
1599: *
1600: * Parse an External ID or a Public ID
1601: *
1602: * NOTE: Productions [75] and [83] interract badly since [75] can generate
1603: * 'PUBLIC' S PubidLiteral S SystemLiteral
1604: *
1605: * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
1606: * | 'PUBLIC' S PubidLiteral S SystemLiteral
1607: *
1608: * [83] PublicID ::= 'PUBLIC' S PubidLiteral
1609: *
1610: * Returns the function returns SystemLiteral and in the second
1611: * case publicID receives PubidLiteral, is strict is off
1612: * it is possible to return NULL and have publicID set.
1613: */
1614:
1.14 daniel 1615: xmlChar *
1616: htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
1617: xmlChar *URI = NULL;
1.1 daniel 1618:
1619: if ((UPPER == 'S') && (UPP(1) == 'Y') &&
1620: (UPP(2) == 'S') && (UPP(3) == 'T') &&
1621: (UPP(4) == 'E') && (UPP(5) == 'M')) {
1622: SKIP(6);
1623: if (!IS_BLANK(CUR)) {
1624: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1625: ctxt->sax->error(ctxt->userData,
1626: "Space required after 'SYSTEM'\n");
1627: ctxt->wellFormed = 0;
1628: }
1629: SKIP_BLANKS;
1630: URI = htmlParseSystemLiteral(ctxt);
1631: if (URI == NULL) {
1632: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1633: ctxt->sax->error(ctxt->userData,
1634: "htmlParseExternalID: SYSTEM, no URI\n");
1635: ctxt->wellFormed = 0;
1636: }
1637: } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
1638: (UPP(2) == 'B') && (UPP(3) == 'L') &&
1639: (UPP(4) == 'I') && (UPP(5) == 'C')) {
1640: SKIP(6);
1641: if (!IS_BLANK(CUR)) {
1642: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1643: ctxt->sax->error(ctxt->userData,
1644: "Space required after 'PUBLIC'\n");
1645: ctxt->wellFormed = 0;
1646: }
1647: SKIP_BLANKS;
1648: *publicID = htmlParsePubidLiteral(ctxt);
1649: if (*publicID == NULL) {
1650: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1651: ctxt->sax->error(ctxt->userData,
1652: "htmlParseExternalID: PUBLIC, no Public Identifier\n");
1653: ctxt->wellFormed = 0;
1654: }
1.5 daniel 1655: SKIP_BLANKS;
1656: if ((CUR == '"') || (CUR == '\'')) {
1657: URI = htmlParseSystemLiteral(ctxt);
1.1 daniel 1658: }
1659: }
1660: return(URI);
1661: }
1662:
1663: /**
1664: * htmlParseComment:
1665: * @ctxt: an HTML parser context
1666: * @create: should we create a node, or just skip the content
1667: *
1668: * Parse an XML (SGML) comment <!-- .... -->
1669: *
1670: * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
1671: */
1672: void
1673: htmlParseComment(htmlParserCtxtPtr ctxt, int create) {
1.14 daniel 1674: const xmlChar *q, *start;
1675: const xmlChar *r;
1676: xmlChar *val;
1.1 daniel 1677:
1678: /*
1679: * Check that there is a comment right here.
1680: */
1681: if ((CUR != '<') || (NXT(1) != '!') ||
1682: (NXT(2) != '-') || (NXT(3) != '-')) return;
1683:
1684: SKIP(4);
1685: start = q = CUR_PTR;
1686: NEXT;
1687: r = CUR_PTR;
1688: NEXT;
1689: while (IS_CHAR(CUR) &&
1690: ((CUR == ':') || (CUR != '>') ||
1691: (*r != '-') || (*q != '-'))) {
1692: if ((*r == '-') && (*q == '-')) {
1693: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1694: ctxt->sax->error(ctxt->userData,
1695: "Comment must not contain '--' (double-hyphen)`\n");
1696: ctxt->wellFormed = 0;
1697: }
1698: NEXT;r++;q++;
1699: }
1700: if (!IS_CHAR(CUR)) {
1701: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1702: ctxt->sax->error(ctxt->userData, "Comment not terminated \n<!--%.50s\n", start);
1703: ctxt->wellFormed = 0;
1704: } else {
1705: NEXT;
1706: if (create) {
1707: val = xmlStrndup(start, q - start);
1708: if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL))
1709: ctxt->sax->comment(ctxt->userData, val);
1.11 daniel 1710: xmlFree(val);
1.1 daniel 1711: }
1712: }
1713: }
1714:
1715: /**
1716: * htmlParseCharRef:
1717: * @ctxt: an HTML parser context
1718: *
1719: * parse Reference declarations
1720: *
1721: * [66] CharRef ::= '&#' [0-9]+ ';' |
1722: * '&#x' [0-9a-fA-F]+ ';'
1723: *
1724: * Returns the value parsed (as an int)
1725: */
1726: int
1727: htmlParseCharRef(htmlParserCtxtPtr ctxt) {
1728: int val = 0;
1729:
1730: if ((CUR == '&') && (NXT(1) == '#') &&
1731: (NXT(2) == 'x')) {
1732: SKIP(3);
1733: while (CUR != ';') {
1734: if ((CUR >= '0') && (CUR <= '9'))
1735: val = val * 16 + (CUR - '0');
1736: else if ((CUR >= 'a') && (CUR <= 'f'))
1737: val = val * 16 + (CUR - 'a') + 10;
1738: else if ((CUR >= 'A') && (CUR <= 'F'))
1739: val = val * 16 + (CUR - 'A') + 10;
1740: else {
1741: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1742: ctxt->sax->error(ctxt->userData,
1743: "htmlParseCharRef: invalid hexadecimal value\n");
1744: ctxt->wellFormed = 0;
1745: val = 0;
1746: break;
1747: }
1748: NEXT;
1749: }
1750: if (CUR == ';')
1751: NEXT;
1752: } else if ((CUR == '&') && (NXT(1) == '#')) {
1753: SKIP(2);
1754: while (CUR != ';') {
1755: if ((CUR >= '0') && (CUR <= '9'))
1756: val = val * 10 + (CUR - '0');
1757: else {
1758: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1759: ctxt->sax->error(ctxt->userData,
1760: "htmlParseCharRef: invalid decimal value\n");
1761: ctxt->wellFormed = 0;
1762: val = 0;
1763: break;
1764: }
1765: NEXT;
1766: }
1767: if (CUR == ';')
1768: NEXT;
1769: } else {
1770: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1771: ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
1772: ctxt->wellFormed = 0;
1773: }
1774: /*
1775: * Check the value IS_CHAR ...
1776: */
1777: if (IS_CHAR(val)) {
1778: return(val);
1779: } else {
1780: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.14 daniel 1781: ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid xmlChar value %d\n",
1.1 daniel 1782: val);
1783: ctxt->wellFormed = 0;
1784: }
1785: return(0);
1786: }
1787:
1788:
1789: /**
1790: * htmlParseDocTypeDecl :
1791: * @ctxt: an HTML parser context
1792: *
1793: * parse a DOCTYPE declaration
1794: *
1795: * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
1796: * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
1797: */
1798:
1799: void
1800: htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
1.14 daniel 1801: xmlChar *name;
1802: xmlChar *ExternalID = NULL;
1803: xmlChar *URI = NULL;
1.1 daniel 1804:
1805: /*
1806: * We know that '<!DOCTYPE' has been detected.
1807: */
1808: SKIP(9);
1809:
1810: SKIP_BLANKS;
1811:
1812: /*
1813: * Parse the DOCTYPE name.
1814: */
1815: name = htmlParseName(ctxt);
1816: if (name == NULL) {
1817: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1818: ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
1819: ctxt->wellFormed = 0;
1820: }
1821: /*
1822: * Check that upper(name) == "HTML" !!!!!!!!!!!!!
1823: */
1824:
1825: SKIP_BLANKS;
1826:
1827: /*
1828: * Check for SystemID and ExternalID
1829: */
1.5 daniel 1830: URI = htmlParseExternalID(ctxt, &ExternalID, 0);
1.1 daniel 1831: SKIP_BLANKS;
1832:
1833: /*
1834: * We should be at the end of the DOCTYPE declaration.
1835: */
1836: if (CUR != '>') {
1837: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1838: ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
1839: ctxt->wellFormed = 0;
1840: /* We shouldn't try to resynchronize ... */
1841: } else {
1842: }
1843: NEXT;
1844:
1845: /*
1846: * Create the document accordingly to the DOCTYPE
1847: */
1848: ctxt->myDoc = htmlNewDoc(URI, ExternalID);
1849:
1850: /*
1851: * Cleanup, since we don't use all those identifiers
1852: */
1.11 daniel 1853: if (URI != NULL) xmlFree(URI);
1854: if (ExternalID != NULL) xmlFree(ExternalID);
1855: if (name != NULL) xmlFree(name);
1.1 daniel 1856: }
1857:
1858: /**
1859: * htmlParseAttribute:
1860: * @ctxt: an HTML parser context
1.14 daniel 1861: * @value: a xmlChar ** used to store the value of the attribute
1.1 daniel 1862: *
1863: * parse an attribute
1864: *
1865: * [41] Attribute ::= Name Eq AttValue
1866: *
1867: * [25] Eq ::= S? '=' S?
1868: *
1869: * With namespace:
1870: *
1871: * [NS 11] Attribute ::= QName Eq AttValue
1872: *
1873: * Also the case QName == xmlns:??? is handled independently as a namespace
1874: * definition.
1875: *
1876: * Returns the attribute name, and the value in *value.
1877: */
1878:
1.14 daniel 1879: xmlChar *
1880: htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
1881: xmlChar *name, *val;
1.1 daniel 1882:
1883: *value = NULL;
1884: name = htmlParseName(ctxt);
1885: if (name == NULL) {
1886: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1887: ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
1888: ctxt->wellFormed = 0;
1889: return(NULL);
1890: }
1891:
1892: /*
1893: * read the value
1894: */
1895: SKIP_BLANKS;
1896: if (CUR == '=') {
1897: NEXT;
1898: SKIP_BLANKS;
1899: val = htmlParseAttValue(ctxt);
1900: } else {
1901: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1902: ctxt->sax->error(ctxt->userData,
1903: "Specification mandate value for attribute %s\n", name);
1904: ctxt->wellFormed = 0;
1905: return(NULL);
1906: }
1907:
1908: *value = val;
1909: return(name);
1910: }
1911:
1912: /**
1913: * htmlParseStartTag:
1914: * @ctxt: an HTML parser context
1915: *
1916: * parse a start of tag either for rule element or
1917: * EmptyElement. In both case we don't parse the tag closing chars.
1918: *
1919: * [40] STag ::= '<' Name (S Attribute)* S? '>'
1920: *
1921: * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
1922: *
1923: * With namespace:
1924: *
1925: * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
1926: *
1927: * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
1928: *
1929: */
1930:
1.18 daniel 1931: void
1.1 daniel 1932: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
1.14 daniel 1933: xmlChar *name;
1934: xmlChar *attname;
1935: xmlChar *attvalue;
1936: const xmlChar **atts = NULL;
1.1 daniel 1937: int nbatts = 0;
1938: int maxatts = 0;
1939: int i;
1940:
1.18 daniel 1941: if (CUR != '<') return;
1.1 daniel 1942: NEXT;
1943:
1.19 daniel 1944: GROW;
1.1 daniel 1945: name = htmlParseHTMLName(ctxt);
1946: if (name == NULL) {
1947: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1948: ctxt->sax->error(ctxt->userData,
1949: "htmlParseStartTag: invalid element name\n");
1950: ctxt->wellFormed = 0;
1.18 daniel 1951: return;
1.1 daniel 1952: }
1953:
1954: /*
1955: * Check for auto-closure of HTML elements.
1956: */
1957: htmlAutoClose(ctxt, name);
1958:
1959: /*
1960: * Now parse the attributes, it ends up with the ending
1961: *
1962: * (S Attribute)* S?
1963: */
1964: SKIP_BLANKS;
1965: while ((IS_CHAR(CUR)) &&
1966: (CUR != '>') &&
1967: ((CUR != '/') || (NXT(1) != '>'))) {
1.14 daniel 1968: const xmlChar *q = CUR_PTR;
1.19 daniel 1969: int cons = ctxt->input->consumed;
1.1 daniel 1970:
1.19 daniel 1971: GROW;
1.1 daniel 1972: attname = htmlParseAttribute(ctxt, &attvalue);
1973: if ((attname != NULL) && (attvalue != NULL)) {
1974: /*
1975: * Well formedness requires at most one declaration of an attribute
1976: */
1977: for (i = 0; i < nbatts;i += 2) {
1978: if (!xmlStrcmp(atts[i], attname)) {
1979: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.19 daniel 1980: ctxt->sax->error(ctxt->userData,
1981: "Attribute %s redefined\n",
1982: attname);
1.1 daniel 1983: ctxt->wellFormed = 0;
1.11 daniel 1984: xmlFree(attname);
1985: xmlFree(attvalue);
1.19 daniel 1986: goto failed;
1.1 daniel 1987: }
1988: }
1989:
1990: /*
1991: * Add the pair to atts
1992: */
1993: if (atts == NULL) {
1994: maxatts = 10;
1.14 daniel 1995: atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
1.1 daniel 1996: if (atts == NULL) {
1997: fprintf(stderr, "malloc of %ld byte failed\n",
1.14 daniel 1998: maxatts * (long)sizeof(xmlChar *));
1.18 daniel 1999: if (name != NULL) xmlFree(name);
2000: return;
1.1 daniel 2001: }
2002: } else if (nbatts + 2 < maxatts) {
2003: maxatts *= 2;
1.14 daniel 2004: atts = (const xmlChar **) xmlRealloc(atts, maxatts * sizeof(xmlChar *));
1.1 daniel 2005: if (atts == NULL) {
2006: fprintf(stderr, "realloc of %ld byte failed\n",
1.14 daniel 2007: maxatts * (long)sizeof(xmlChar *));
1.18 daniel 2008: if (name != NULL) xmlFree(name);
2009: return;
1.1 daniel 2010: }
2011: }
2012: atts[nbatts++] = attname;
2013: atts[nbatts++] = attvalue;
2014: atts[nbatts] = NULL;
2015: atts[nbatts + 1] = NULL;
2016: }
2017:
1.19 daniel 2018: failed:
1.1 daniel 2019: SKIP_BLANKS;
1.19 daniel 2020: if ((cons == ctxt->input->consumed) && (q == CUR_PTR)) {
1.1 daniel 2021: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2022: ctxt->sax->error(ctxt->userData,
2023: "htmlParseStartTag: problem parsing attributes\n");
2024: ctxt->wellFormed = 0;
2025: break;
2026: }
2027: }
2028:
2029: /*
2030: * SAX: Start of Element !
2031: */
1.15 daniel 2032: htmlnamePush(ctxt, xmlStrdup(name));
1.18 daniel 2033: #ifdef DEBUG
2034: fprintf(stderr,"Start of element %s: pushed %s\n", name, ctxt->name);
2035: #endif
1.1 daniel 2036: if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
2037: ctxt->sax->startElement(ctxt->userData, name, atts);
2038:
2039: if (atts != NULL) {
1.14 daniel 2040: for (i = 0;i < nbatts;i++) xmlFree((xmlChar *) atts[i]);
1.11 daniel 2041: xmlFree(atts);
1.1 daniel 2042: }
1.18 daniel 2043: if (name != NULL) xmlFree(name);
1.1 daniel 2044: }
2045:
2046: /**
2047: * htmlParseEndTag:
2048: * @ctxt: an HTML parser context
2049: *
2050: * parse an end of tag
2051: *
2052: * [42] ETag ::= '</' Name S? '>'
2053: *
2054: * With namespace
2055: *
2056: * [NS 9] ETag ::= '</' QName S? '>'
2057: */
2058:
2059: void
1.18 daniel 2060: htmlParseEndTag(htmlParserCtxtPtr ctxt) {
1.14 daniel 2061: xmlChar *name;
1.15 daniel 2062: xmlChar *oldname;
1.1 daniel 2063: int i;
2064:
2065: if ((CUR != '<') || (NXT(1) != '/')) {
2066: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2067: ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
2068: ctxt->wellFormed = 0;
2069: return;
2070: }
2071: SKIP(2);
2072:
2073: name = htmlParseHTMLName(ctxt);
2074:
2075: /*
2076: * We should definitely be at the ending "S? '>'" part
2077: */
2078: SKIP_BLANKS;
2079: if ((!IS_CHAR(CUR)) || (CUR != '>')) {
2080: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2081: ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
2082: ctxt->wellFormed = 0;
2083: } else
2084: NEXT;
2085:
2086: /*
1.18 daniel 2087: * If the name read is not one of the element in the parsing stack
2088: * then return, it's just an error.
1.1 daniel 2089: */
1.18 daniel 2090: for (i = (ctxt->nameNr - 1);i >= 0;i--) {
2091: if (!xmlStrcmp(name, ctxt->nameTab[i])) break;
1.1 daniel 2092: }
2093: if (i < 0) {
2094: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.18 daniel 2095: ctxt->sax->error(ctxt->userData,
2096: "Unexpected end tag : %s\n", name);
1.11 daniel 2097: xmlFree(name);
1.1 daniel 2098: ctxt->wellFormed = 0;
2099: return;
2100: }
2101:
1.18 daniel 2102:
1.1 daniel 2103: /*
2104: * Check for auto-closure of HTML elements.
2105: */
1.18 daniel 2106:
1.1 daniel 2107: htmlAutoCloseOnClose(ctxt, name);
2108:
2109: /*
2110: * Well formedness constraints, opening and closing must match.
2111: * With the exception that the autoclose may have popped stuff out
2112: * of the stack.
2113: */
1.18 daniel 2114: if (xmlStrcmp(name, ctxt->name)) {
2115: #ifdef DEBUG
2116: fprintf(stderr,"End of tag %s: expecting %s\n", name, ctxt->name);
2117: #endif
1.15 daniel 2118: if ((ctxt->name != NULL) &&
2119: (xmlStrcmp(ctxt->name, name))) {
1.1 daniel 2120: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2121: ctxt->sax->error(ctxt->userData,
2122: "Opening and ending tag mismatch: %s and %s\n",
1.15 daniel 2123: name, ctxt->name);
1.1 daniel 2124: ctxt->wellFormed = 0;
2125: }
2126: }
2127:
2128: /*
2129: * SAX: End of Tag
2130: */
1.15 daniel 2131: oldname = ctxt->name;
1.18 daniel 2132: if (!xmlStrcmp(oldname, name)) {
2133: if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
2134: ctxt->sax->endElement(ctxt->userData, name);
2135: htmlnamePop(ctxt);
2136: if (oldname != NULL) {
2137: #ifdef DEBUG
2138: fprintf(stderr,"End of tag %s: popping out %s\n", name, oldname);
2139: #endif
2140: xmlFree(oldname);
2141: #ifdef DEBUG
2142: } else {
2143: fprintf(stderr,"End of tag %s: stack empty !!!\n", name);
2144: #endif
2145: }
2146: }
1.1 daniel 2147:
2148: if (name != NULL)
1.11 daniel 2149: xmlFree(name);
1.1 daniel 2150:
2151: return;
2152: }
2153:
2154:
2155: /**
2156: * htmlParseReference:
2157: * @ctxt: an HTML parser context
2158: *
2159: * parse and handle entity references in content,
2160: * this will end-up in a call to character() since this is either a
2161: * CharRef, or a predefined entity.
2162: */
2163: void
2164: htmlParseReference(htmlParserCtxtPtr ctxt) {
2165: htmlEntityDescPtr ent;
1.14 daniel 2166: xmlChar out[2];
2167: xmlChar *name;
1.1 daniel 2168: int val;
2169: if (CUR != '&') return;
2170:
2171: if (NXT(1) == '#') {
2172: val = htmlParseCharRef(ctxt);
1.8 daniel 2173: /* invalid for UTF-8 variable encoding !!!!! */
1.1 daniel 2174: out[0] = val;
2175: out[1] = 0;
2176: if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
2177: ctxt->sax->characters(ctxt->userData, out, 1);
2178: } else {
2179: ent = htmlParseEntityRef(ctxt, &name);
2180: if (name == NULL) return; /* Shall we output & anyway ? */
2181: if ((ent == NULL) || (ent->value <= 0) || (ent->value >= 255)) {
2182: if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
1.8 daniel 2183: ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1 daniel 2184: ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1.8 daniel 2185: ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1);
1.1 daniel 2186: }
2187: } else {
1.8 daniel 2188: /* invalid for UTF-8 variable encoding !!!!! */
1.1 daniel 2189: out[0] = ent->value;
2190: out[1] = 0;
2191: if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
2192: ctxt->sax->characters(ctxt->userData, out, 1);
2193: }
1.11 daniel 2194: xmlFree(name);
1.1 daniel 2195: }
2196: }
2197:
2198: /**
2199: * htmlParseContent:
2200: * @ctxt: an HTML parser context
2201: * @name: the node name
2202: *
2203: * Parse a content: comment, sub-element, reference or text.
2204: *
2205: */
2206:
2207: void
1.18 daniel 2208: htmlParseContent(htmlParserCtxtPtr ctxt) {
1.15 daniel 2209: xmlChar *currentNode;
1.18 daniel 2210: int depth;
1.1 daniel 2211:
1.15 daniel 2212: currentNode = ctxt->name;
1.18 daniel 2213: depth = ctxt->nameNr;
2214: while (1) {
1.14 daniel 2215: const xmlChar *test = CUR_PTR;
1.1 daniel 2216:
1.18 daniel 2217: GROW;
2218: /*
2219: * Our tag or one of it's parent or children is ending.
2220: */
2221: if ((CUR == '<') && (NXT(1) == '/')) {
2222: htmlParseEndTag(ctxt);
2223: return;
2224: }
2225:
2226: /*
2227: * Has this node been popped out during parsing of
2228: * the next element
2229: */
2230: if ((currentNode != ctxt->name) &&
2231: (depth >= ctxt->nameNr)) return;
2232:
1.1 daniel 2233: /*
2234: * First case : a comment
2235: */
2236: if ((CUR == '<') && (NXT(1) == '!') &&
2237: (NXT(2) == '-') && (NXT(3) == '-')) {
2238: htmlParseComment(ctxt, 1);
2239: }
2240:
2241: /*
2242: * Second case : a sub-element.
2243: */
2244: else if (CUR == '<') {
2245: htmlParseElement(ctxt);
2246: }
2247:
2248: /*
2249: * Third case : a reference. If if has not been resolved,
2250: * parsing returns it's Name, create the node
2251: */
2252: else if (CUR == '&') {
2253: htmlParseReference(ctxt);
2254: }
2255:
2256: /*
2257: * Last case, text. Note that References are handled directly.
2258: */
2259: else {
2260: htmlParseCharData(ctxt, 0);
2261: }
2262:
2263: if (test == CUR_PTR) {
2264: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2265: ctxt->sax->error(ctxt->userData,
2266: "detected an error in element content\n");
2267: ctxt->wellFormed = 0;
2268: break;
2269: }
1.17 daniel 2270:
1.5 daniel 2271: GROW;
1.1 daniel 2272: }
2273: }
2274:
2275: /**
2276: * htmlParseElement:
2277: * @ctxt: an HTML parser context
2278: *
2279: * parse an HTML element, this is highly recursive
2280: *
2281: * [39] element ::= EmptyElemTag | STag content ETag
2282: *
2283: * [41] Attribute ::= Name Eq AttValue
2284: */
2285:
2286: void
2287: htmlParseElement(htmlParserCtxtPtr ctxt) {
1.14 daniel 2288: const xmlChar *openTag = CUR_PTR;
1.18 daniel 2289: xmlChar *oldname;
1.14 daniel 2290: xmlChar *name;
1.16 daniel 2291: xmlChar *currentNode = NULL;
1.1 daniel 2292: htmlElemDescPtr info;
1.10 daniel 2293: htmlParserNodeInfo node_info;
1.18 daniel 2294: int depth = ctxt->nameNr;
1.1 daniel 2295:
2296: /* Capture start position */
1.10 daniel 2297: if (ctxt->record_info) {
2298: node_info.begin_pos = ctxt->input->consumed +
2299: (CUR_PTR - ctxt->input->base);
2300: node_info.begin_line = ctxt->input->line;
2301: }
1.1 daniel 2302:
1.18 daniel 2303: oldname = ctxt->name;
2304: htmlParseStartTag(ctxt);
2305: name = ctxt->name;
1.19 daniel 2306: #ifdef DEBUG
2307: if (oldname == NULL)
2308: fprintf(stderr, "Start of element %s\n", name);
2309: else if (name == NULL)
2310: fprintf(stderr, "Start of element failed, was %s\n", oldname);
2311: else
2312: fprintf(stderr, "Start of element %s, was %s\n", name, oldname);
2313: #endif
1.18 daniel 2314: if (((depth == ctxt->nameNr) && (oldname == ctxt->name)) ||
2315: (name == NULL)) {
1.19 daniel 2316: if (CUR == '>')
2317: NEXT;
1.1 daniel 2318: return;
2319: }
2320:
2321: /*
2322: * Lookup the info for that element.
2323: */
2324: info = htmlTagLookup(name);
2325: if (info == NULL) {
2326: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2327: ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
2328: name);
2329: ctxt->wellFormed = 0;
2330: } else if (info->depr) {
2331: /***************************
2332: if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
2333: ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
2334: name);
2335: ***************************/
2336: }
2337:
2338: /*
2339: * Check for an Empty Element labelled the XML/SGML way
2340: */
2341: if ((CUR == '/') && (NXT(1) == '>')) {
2342: SKIP(2);
2343: if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
2344: ctxt->sax->endElement(ctxt->userData, name);
1.15 daniel 2345: oldname = ctxt->name;
1.18 daniel 2346: #ifdef DEBUG
2347: fprintf(stderr,"End of tag the XML way: popping out %s\n", oldname);
2348: #endif
1.15 daniel 2349: htmlnamePop(ctxt);
1.17 daniel 2350: if (oldname != NULL)
2351: xmlFree(oldname);
1.1 daniel 2352: return;
2353: }
2354:
1.5 daniel 2355: if (CUR == '>') {
2356: NEXT;
2357: } else {
1.1 daniel 2358: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2359: ctxt->sax->error(ctxt->userData, "Couldn't find end of Start Tag\n%.30s\n",
2360: openTag);
2361: ctxt->wellFormed = 0;
2362:
2363: /*
2364: * end of parsing of this node.
2365: */
1.18 daniel 2366: if (!xmlStrcmp(name, ctxt->name)) {
2367: nodePop(ctxt);
2368: xmlFree(name);
2369: oldname = ctxt->name;
2370: #ifdef DEBUG
2371: fprintf(stderr,"End of start tag problem: popping out %s\n", oldname);
2372: #endif
2373: htmlnamePop(ctxt);
2374: if (oldname != NULL)
2375: xmlFree(oldname);
2376: }
1.10 daniel 2377:
2378: /*
2379: * Capture end position and add node
2380: */
2381: if ( currentNode != NULL && ctxt->record_info ) {
2382: node_info.end_pos = ctxt->input->consumed +
2383: (CUR_PTR - ctxt->input->base);
2384: node_info.end_line = ctxt->input->line;
1.15 daniel 2385: node_info.node = ctxt->node;
1.10 daniel 2386: xmlParserAddNodeInfo(ctxt, &node_info);
2387: }
1.1 daniel 2388: return;
2389: }
2390:
2391: /*
2392: * Check for an Empty Element from DTD definition
2393: */
2394: if ((info != NULL) && (info->empty)) {
2395: if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
2396: ctxt->sax->endElement(ctxt->userData, name);
1.15 daniel 2397: oldname = ctxt->name;
1.18 daniel 2398: #ifdef DEBUG
2399: fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
2400: #endif
1.15 daniel 2401: htmlnamePop(ctxt);
1.17 daniel 2402: if (oldname != NULL)
2403: xmlFree(oldname);
1.1 daniel 2404: return;
2405: }
2406:
2407: /*
2408: * Parse the content of the element:
2409: */
1.15 daniel 2410: currentNode = ctxt->name;
1.18 daniel 2411: depth = ctxt->nameNr;
2412: while (IS_CHAR(CUR)) {
2413: htmlParseContent(ctxt);
2414: if (ctxt->nameNr < depth) break;
2415: }
1.1 daniel 2416:
2417: if (!IS_CHAR(CUR)) {
2418: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2419: ctxt->sax->error(ctxt->userData,
1.18 daniel 2420: "Premature end of data in tag %s\n", currentNode);
1.1 daniel 2421: ctxt->wellFormed = 0;
2422:
2423: /*
2424: * end of parsing of this node.
2425: */
2426: nodePop(ctxt);
1.15 daniel 2427: oldname = ctxt->name;
1.18 daniel 2428: #ifdef DEBUG
2429: fprintf(stderr,"Premature end of tag %s : popping out %s\n", name, oldname);
2430: #endif
1.15 daniel 2431: htmlnamePop(ctxt);
1.17 daniel 2432: if (oldname != NULL)
2433: xmlFree(oldname);
1.1 daniel 2434: return;
2435: }
1.10 daniel 2436:
2437: /*
2438: * Capture end position and add node
2439: */
2440: if ( currentNode != NULL && ctxt->record_info ) {
2441: node_info.end_pos = ctxt->input->consumed +
2442: (CUR_PTR - ctxt->input->base);
2443: node_info.end_line = ctxt->input->line;
1.15 daniel 2444: node_info.node = ctxt->node;
1.10 daniel 2445: xmlParserAddNodeInfo(ctxt, &node_info);
2446: }
1.1 daniel 2447: }
2448:
2449: /**
2450: * htmlParseDocument :
2451: * @ctxt: an HTML parser context
2452: *
2453: * parse an HTML document (and build a tree if using the standard SAX
2454: * interface).
2455: *
2456: * Returns 0, -1 in case of error. the parser context is augmented
2457: * as a result of the parsing.
2458: */
2459:
2460: int
2461: htmlParseDocument(htmlParserCtxtPtr ctxt) {
2462: htmlDefaultSAXHandlerInit();
2463: ctxt->html = 1;
2464:
1.5 daniel 2465: GROW;
1.1 daniel 2466: /*
1.9 daniel 2467: * SAX: beginning of the document processing.
1.1 daniel 2468: */
2469: if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
2470: ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
2471:
2472: /*
2473: * Wipe out everything which is before the first '<'
2474: */
2475: if (IS_BLANK(CUR)) {
2476: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2477: ctxt->sax->error(ctxt->userData,
2478: "Extra spaces at the beginning of the document are not allowed\n");
2479: ctxt->wellFormed = 0;
2480: SKIP_BLANKS;
2481: }
2482:
2483: if (CUR == 0) {
2484: if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2485: ctxt->sax->error(ctxt->userData, "Document is empty\n");
2486: ctxt->wellFormed = 0;
2487: }
2488:
2489:
2490: /*
2491: * Then possibly doc type declaration(s) and more Misc
2492: * (doctypedecl Misc*)?
2493: */
2494: if ((CUR == '<') && (NXT(1) == '!') &&
2495: (UPP(2) == 'D') && (UPP(3) == 'O') &&
2496: (UPP(4) == 'C') && (UPP(5) == 'T') &&
2497: (UPP(6) == 'Y') && (UPP(7) == 'P') &&
2498: (UPP(8) == 'E')) {
2499: htmlParseDocTypeDecl(ctxt);
2500: }
2501: SKIP_BLANKS;
2502:
2503: /*
2504: * Create the document if not done already.
2505: */
2506: if (ctxt->myDoc == NULL) {
2507: ctxt->myDoc = htmlNewDoc(NULL, NULL);
2508: }
2509:
2510: /*
2511: * Time to start parsing the tree itself
2512: */
2513: htmlParseElement(ctxt);
2514:
2515: /*
2516: * SAX: end of the document processing.
2517: */
2518: if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
2519: ctxt->sax->endDocument(ctxt->userData);
2520: if (! ctxt->wellFormed) return(-1);
2521: return(0);
2522: }
2523:
2524:
2525: /********************************************************************************
2526: * *
2527: * Parser contexts handling *
2528: * *
2529: ********************************************************************************/
2530:
2531: /**
2532: * xmlInitParserCtxt:
2533: * @ctxt: an HTML parser context
2534: *
2535: * Initialize a parser context
2536: */
2537:
2538: void
2539: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
2540: {
2541: htmlSAXHandler *sax;
2542:
1.21 ! daniel 2543: if (ctxt == NULL) return;
! 2544: memset(ctxt, 0, sizeof(htmlParserCtxt));
! 2545:
1.11 daniel 2546: sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
1.1 daniel 2547: if (sax == NULL) {
2548: fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
2549: }
1.19 daniel 2550: memset(sax, 0, sizeof(htmlSAXHandler));
1.1 daniel 2551:
2552: /* Allocate the Input stack */
1.19 daniel 2553: ctxt->inputTab = (htmlParserInputPtr *)
2554: xmlMalloc(5 * sizeof(htmlParserInputPtr));
2555: if (ctxt->inputTab == NULL) {
2556: fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
2557: }
1.1 daniel 2558: ctxt->inputNr = 0;
2559: ctxt->inputMax = 5;
2560: ctxt->input = NULL;
2561: ctxt->version = NULL;
2562: ctxt->encoding = NULL;
2563: ctxt->standalone = -1;
2564:
2565: /* Allocate the Node stack */
1.11 daniel 2566: ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
1.1 daniel 2567: ctxt->nodeNr = 0;
2568: ctxt->nodeMax = 10;
2569: ctxt->node = NULL;
2570:
1.15 daniel 2571: /* Allocate the Name stack */
2572: ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
2573: ctxt->nameNr = 0;
2574: ctxt->nameMax = 10;
2575: ctxt->name = NULL;
2576:
1.1 daniel 2577: if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
2578: else {
2579: ctxt->sax = sax;
2580: memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
2581: }
2582: ctxt->userData = ctxt;
2583: ctxt->myDoc = NULL;
2584: ctxt->wellFormed = 1;
2585: ctxt->replaceEntities = 0;
2586: ctxt->html = 1;
2587: ctxt->record_info = 0;
1.21 ! daniel 2588: ctxt->validate = 0;
1.1 daniel 2589: xmlInitNodeInfoSeq(&ctxt->node_seq);
2590: }
2591:
2592: /**
2593: * htmlFreeParserCtxt:
2594: * @ctxt: an HTML parser context
2595: *
2596: * Free all the memory used by a parser context. However the parsed
2597: * document in ctxt->myDoc is not freed.
2598: */
2599:
2600: void
2601: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
2602: {
2603: htmlParserInputPtr input;
1.15 daniel 2604: xmlChar *oldname;
1.1 daniel 2605:
2606: if (ctxt == NULL) return;
2607:
2608: while ((input = inputPop(ctxt)) != NULL) {
2609: xmlFreeInputStream(input);
2610: }
2611:
1.11 daniel 2612: if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1.15 daniel 2613: while ((oldname = ctxt->name) != NULL) {
2614: htmlnamePop(ctxt);
1.17 daniel 2615: if (oldname != NULL)
2616: xmlFree(oldname);
1.15 daniel 2617: }
2618: if (ctxt->nameTab != NULL) xmlFree(ctxt->nameTab);
1.11 daniel 2619: if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
2620: if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1.1 daniel 2621: if ((ctxt->sax != NULL) && (ctxt->sax != &htmlDefaultSAXHandler))
1.11 daniel 2622: xmlFree(ctxt->sax);
2623: xmlFree(ctxt);
1.1 daniel 2624: }
2625:
2626: /**
2627: * htmlCreateDocParserCtxt :
1.14 daniel 2628: * @cur: a pointer to an array of xmlChar
1.1 daniel 2629: * @encoding: a free form C string describing the HTML document encoding, or NULL
2630: *
2631: * Create a parser context for an HTML document.
2632: *
2633: * Returns the new parser context or NULL
2634: */
2635: htmlParserCtxtPtr
1.14 daniel 2636: htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
1.1 daniel 2637: htmlParserCtxtPtr ctxt;
2638: htmlParserInputPtr input;
2639: /* htmlCharEncoding enc; */
2640:
1.11 daniel 2641: ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1 daniel 2642: if (ctxt == NULL) {
2643: perror("malloc");
2644: return(NULL);
2645: }
2646: htmlInitParserCtxt(ctxt);
1.11 daniel 2647: input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1 daniel 2648: if (input == NULL) {
2649: perror("malloc");
1.11 daniel 2650: xmlFree(ctxt);
1.1 daniel 2651: return(NULL);
2652: }
1.19 daniel 2653: memset(input, 0, sizeof(htmlParserInput));
1.1 daniel 2654:
2655: input->line = 1;
2656: input->col = 1;
2657: input->base = cur;
2658: input->cur = cur;
2659:
2660: inputPush(ctxt, input);
2661: return(ctxt);
2662: }
2663:
2664: /********************************************************************************
2665: * *
2666: * User entry points *
2667: * *
2668: ********************************************************************************/
2669:
2670: /**
2671: * htmlSAXParseDoc :
1.14 daniel 2672: * @cur: a pointer to an array of xmlChar
1.1 daniel 2673: * @encoding: a free form C string describing the HTML document encoding, or NULL
2674: * @sax: the SAX handler block
2675: * @userData: if using SAX, this pointer will be provided on callbacks.
2676: *
2677: * parse an HTML in-memory document and build a tree.
2678: * It use the given SAX function block to handle the parsing callback.
2679: * If sax is NULL, fallback to the default DOM tree building routines.
2680: *
2681: * Returns the resulting document tree
2682: */
2683:
2684: htmlDocPtr
1.14 daniel 2685: htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
1.1 daniel 2686: htmlDocPtr ret;
2687: htmlParserCtxtPtr ctxt;
2688:
2689: if (cur == NULL) return(NULL);
2690:
2691:
2692: ctxt = htmlCreateDocParserCtxt(cur, encoding);
2693: if (ctxt == NULL) return(NULL);
2694: if (sax != NULL) {
2695: ctxt->sax = sax;
2696: ctxt->userData = userData;
2697: }
2698:
2699: htmlParseDocument(ctxt);
2700: ret = ctxt->myDoc;
2701: if (sax != NULL) {
2702: ctxt->sax = NULL;
2703: ctxt->userData = NULL;
2704: }
2705: htmlFreeParserCtxt(ctxt);
2706:
2707: return(ret);
2708: }
2709:
2710: /**
2711: * htmlParseDoc :
1.14 daniel 2712: * @cur: a pointer to an array of xmlChar
1.1 daniel 2713: * @encoding: a free form C string describing the HTML document encoding, or NULL
2714: *
2715: * parse an HTML in-memory document and build a tree.
2716: *
2717: * Returns the resulting document tree
2718: */
2719:
2720: htmlDocPtr
1.14 daniel 2721: htmlParseDoc(xmlChar *cur, const char *encoding) {
1.1 daniel 2722: return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
2723: }
2724:
2725:
2726: /**
2727: * htmlCreateFileParserCtxt :
2728: * @filename: the filename
2729: * @encoding: a free form C string describing the HTML document encoding, or NULL
2730: *
2731: * Create a parser context for a file content.
2732: * Automatic support for ZLIB/Compress compressed document is provided
2733: * by default if found at compile-time.
2734: *
2735: * Returns the new parser context or NULL
2736: */
2737: htmlParserCtxtPtr
2738: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
2739: {
2740: htmlParserCtxtPtr ctxt;
2741: htmlParserInputPtr inputStream;
1.5 daniel 2742: xmlParserInputBufferPtr buf;
1.1 daniel 2743: /* htmlCharEncoding enc; */
2744:
1.5 daniel 2745: buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
2746: if (buf == NULL) return(NULL);
1.1 daniel 2747:
1.11 daniel 2748: ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1 daniel 2749: if (ctxt == NULL) {
2750: perror("malloc");
2751: return(NULL);
2752: }
1.19 daniel 2753: memset(ctxt, 0, sizeof(htmlParserCtxt));
1.1 daniel 2754: htmlInitParserCtxt(ctxt);
1.11 daniel 2755: inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1 daniel 2756: if (inputStream == NULL) {
2757: perror("malloc");
1.11 daniel 2758: xmlFree(ctxt);
1.1 daniel 2759: return(NULL);
2760: }
1.19 daniel 2761: memset(inputStream, 0, sizeof(htmlParserInput));
1.1 daniel 2762:
1.11 daniel 2763: inputStream->filename = xmlMemStrdup(filename);
1.1 daniel 2764: inputStream->line = 1;
2765: inputStream->col = 1;
1.5 daniel 2766: inputStream->buf = buf;
1.21 ! daniel 2767: inputStream->directory = NULL;
1.1 daniel 2768:
1.5 daniel 2769: inputStream->base = inputStream->buf->buffer->content;
2770: inputStream->cur = inputStream->buf->buffer->content;
2771: inputStream->free = NULL;
1.1 daniel 2772:
2773: inputPush(ctxt, inputStream);
2774: return(ctxt);
2775: }
2776:
2777: /**
2778: * htmlSAXParseFile :
2779: * @filename: the filename
2780: * @encoding: a free form C string describing the HTML document encoding, or NULL
2781: * @sax: the SAX handler block
2782: * @userData: if using SAX, this pointer will be provided on callbacks.
2783: *
2784: * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
2785: * compressed document is provided by default if found at compile-time.
2786: * It use the given SAX function block to handle the parsing callback.
2787: * If sax is NULL, fallback to the default DOM tree building routines.
2788: *
2789: * Returns the resulting document tree
2790: */
2791:
2792: htmlDocPtr
2793: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
2794: void *userData) {
2795: htmlDocPtr ret;
2796: htmlParserCtxtPtr ctxt;
2797:
2798: ctxt = htmlCreateFileParserCtxt(filename, encoding);
2799: if (ctxt == NULL) return(NULL);
2800: if (sax != NULL) {
2801: ctxt->sax = sax;
2802: ctxt->userData = userData;
2803: }
2804:
2805: htmlParseDocument(ctxt);
2806:
2807: ret = ctxt->myDoc;
2808: if (sax != NULL) {
2809: ctxt->sax = NULL;
2810: ctxt->userData = NULL;
2811: }
2812: htmlFreeParserCtxt(ctxt);
2813:
2814: return(ret);
2815: }
2816:
2817: /**
2818: * htmlParseFile :
2819: * @filename: the filename
2820: * @encoding: a free form C string describing the HTML document encoding, or NULL
2821: *
2822: * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
2823: * compressed document is provided by default if found at compile-time.
2824: *
2825: * Returns the resulting document tree
2826: */
2827:
2828: htmlDocPtr
2829: htmlParseFile(const char *filename, const char *encoding) {
2830: return(htmlSAXParseFile(filename, encoding, NULL, NULL));
2831: }
Webmaster