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