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