Coverage Report

Created: 2022-06-08 06:16

/src/libxml2/HTMLparser.c
Line
Count
Source (jump to first uncovered line)
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.com
7
 */
8
9
#define IN_LIBXML
10
#include "libxml.h"
11
#ifdef LIBXML_HTML_ENABLED
12
13
#include <string.h>
14
#include <ctype.h>
15
#include <stdlib.h>
16
17
#include <libxml/xmlmemory.h>
18
#include <libxml/tree.h>
19
#include <libxml/parser.h>
20
#include <libxml/parserInternals.h>
21
#include <libxml/xmlerror.h>
22
#include <libxml/HTMLparser.h>
23
#include <libxml/HTMLtree.h>
24
#include <libxml/entities.h>
25
#include <libxml/encoding.h>
26
#include <libxml/valid.h>
27
#include <libxml/xmlIO.h>
28
#include <libxml/globals.h>
29
#include <libxml/uri.h>
30
31
#include "buf.h"
32
#include "enc.h"
33
34
#define HTML_MAX_NAMELEN 1000
35
0
#define HTML_PARSER_BIG_BUFFER_SIZE 1000
36
0
#define HTML_PARSER_BUFFER_SIZE 100
37
38
/* #define DEBUG */
39
/* #define DEBUG_PUSH */
40
41
static int htmlOmittedDefaultValue = 1;
42
43
xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
44
           xmlChar end, xmlChar  end2, xmlChar end3);
45
static void htmlParseComment(htmlParserCtxtPtr ctxt);
46
47
/************************************************************************
48
 *                  *
49
 *    Some factorized error routines        *
50
 *                  *
51
 ************************************************************************/
52
53
/**
54
 * htmlErrMemory:
55
 * @ctxt:  an HTML parser context
56
 * @extra:  extra information
57
 *
58
 * Handle a redefinition of attribute error
59
 */
60
static void
61
htmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
62
0
{
63
0
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
64
0
        (ctxt->instate == XML_PARSER_EOF))
65
0
  return;
66
0
    if (ctxt != NULL) {
67
0
        ctxt->errNo = XML_ERR_NO_MEMORY;
68
0
        ctxt->instate = XML_PARSER_EOF;
69
0
        ctxt->disableSAX = 1;
70
0
    }
71
0
    if (extra)
72
0
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
73
0
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
74
0
                        NULL, NULL, 0, 0,
75
0
                        "Memory allocation failed : %s\n", extra);
76
0
    else
77
0
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
78
0
                        XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
79
0
                        NULL, NULL, 0, 0, "Memory allocation failed\n");
80
0
}
81
82
/**
83
 * htmlParseErr:
84
 * @ctxt:  an HTML parser context
85
 * @error:  the error number
86
 * @msg:  the error message
87
 * @str1:  string infor
88
 * @str2:  string infor
89
 *
90
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
91
 */
92
static void LIBXML_ATTR_FORMAT(3,0)
93
htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
94
             const char *msg, const xmlChar *str1, const xmlChar *str2)
95
0
{
96
0
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
97
0
        (ctxt->instate == XML_PARSER_EOF))
98
0
  return;
99
0
    if (ctxt != NULL)
100
0
  ctxt->errNo = error;
101
0
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
102
0
                    XML_ERR_ERROR, NULL, 0,
103
0
        (const char *) str1, (const char *) str2,
104
0
        NULL, 0, 0,
105
0
        msg, str1, str2);
106
0
    if (ctxt != NULL)
107
0
  ctxt->wellFormed = 0;
108
0
}
109
110
/**
111
 * htmlParseErrInt:
112
 * @ctxt:  an HTML parser context
113
 * @error:  the error number
114
 * @msg:  the error message
115
 * @val:  integer info
116
 *
117
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
118
 */
119
static void LIBXML_ATTR_FORMAT(3,0)
120
htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
121
             const char *msg, int val)
122
0
{
123
0
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
124
0
        (ctxt->instate == XML_PARSER_EOF))
125
0
  return;
126
0
    if (ctxt != NULL)
127
0
  ctxt->errNo = error;
128
0
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
129
0
                    XML_ERR_ERROR, NULL, 0, NULL, NULL,
130
0
        NULL, val, 0, msg, val);
131
0
    if (ctxt != NULL)
132
0
  ctxt->wellFormed = 0;
133
0
}
134
135
/************************************************************************
136
 *                  *
137
 *  Parser stacks related functions and macros    *
138
 *                  *
139
 ************************************************************************/
140
141
/**
142
 * htmlnamePush:
143
 * @ctxt:  an HTML parser context
144
 * @value:  the element name
145
 *
146
 * Pushes a new element name on top of the name stack
147
 *
148
 * Returns 0 in case of error, the index in the stack otherwise
149
 */
150
static int
151
htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value)
152
0
{
153
0
    if ((ctxt->html < 3) && (xmlStrEqual(value, BAD_CAST "head")))
154
0
        ctxt->html = 3;
155
0
    if ((ctxt->html < 10) && (xmlStrEqual(value, BAD_CAST "body")))
156
0
        ctxt->html = 10;
157
0
    if (ctxt->nameNr >= ctxt->nameMax) {
158
0
        ctxt->nameMax *= 2;
159
0
        ctxt->nameTab = (const xmlChar * *)
160
0
                         xmlRealloc((xmlChar * *)ctxt->nameTab,
161
0
                                    ctxt->nameMax *
162
0
                                    sizeof(ctxt->nameTab[0]));
163
0
        if (ctxt->nameTab == NULL) {
164
0
            htmlErrMemory(ctxt, NULL);
165
0
            return (0);
166
0
        }
167
0
    }
168
0
    ctxt->nameTab[ctxt->nameNr] = value;
169
0
    ctxt->name = value;
170
0
    return (ctxt->nameNr++);
171
0
}
172
/**
173
 * htmlnamePop:
174
 * @ctxt: an HTML parser context
175
 *
176
 * Pops the top element name from the name stack
177
 *
178
 * Returns the name just removed
179
 */
180
static const xmlChar *
181
htmlnamePop(htmlParserCtxtPtr ctxt)
182
0
{
183
0
    const xmlChar *ret;
184
185
0
    if (ctxt->nameNr <= 0)
186
0
        return (NULL);
187
0
    ctxt->nameNr--;
188
0
    if (ctxt->nameNr < 0)
189
0
        return (NULL);
190
0
    if (ctxt->nameNr > 0)
191
0
        ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
192
0
    else
193
0
        ctxt->name = NULL;
194
0
    ret = ctxt->nameTab[ctxt->nameNr];
195
0
    ctxt->nameTab[ctxt->nameNr] = NULL;
196
0
    return (ret);
197
0
}
198
199
/**
200
 * htmlNodeInfoPush:
201
 * @ctxt:  an HTML parser context
202
 * @value:  the node info
203
 *
204
 * Pushes a new element name on top of the node info stack
205
 *
206
 * Returns 0 in case of error, the index in the stack otherwise
207
 */
208
static int
209
htmlNodeInfoPush(htmlParserCtxtPtr ctxt, htmlParserNodeInfo *value)
210
0
{
211
0
    if (ctxt->nodeInfoNr >= ctxt->nodeInfoMax) {
212
0
        if (ctxt->nodeInfoMax == 0)
213
0
                ctxt->nodeInfoMax = 5;
214
0
        ctxt->nodeInfoMax *= 2;
215
0
        ctxt->nodeInfoTab = (htmlParserNodeInfo *)
216
0
                         xmlRealloc((htmlParserNodeInfo *)ctxt->nodeInfoTab,
217
0
                                    ctxt->nodeInfoMax *
218
0
                                    sizeof(ctxt->nodeInfoTab[0]));
219
0
        if (ctxt->nodeInfoTab == NULL) {
220
0
            htmlErrMemory(ctxt, NULL);
221
0
            return (0);
222
0
        }
223
0
    }
224
0
    ctxt->nodeInfoTab[ctxt->nodeInfoNr] = *value;
225
0
    ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
226
0
    return (ctxt->nodeInfoNr++);
227
0
}
228
229
/**
230
 * htmlNodeInfoPop:
231
 * @ctxt:  an HTML parser context
232
 *
233
 * Pops the top element name from the node info stack
234
 *
235
 * Returns 0 in case of error, the pointer to NodeInfo otherwise
236
 */
237
static htmlParserNodeInfo *
238
htmlNodeInfoPop(htmlParserCtxtPtr ctxt)
239
0
{
240
0
    if (ctxt->nodeInfoNr <= 0)
241
0
        return (NULL);
242
0
    ctxt->nodeInfoNr--;
243
0
    if (ctxt->nodeInfoNr < 0)
244
0
        return (NULL);
245
0
    if (ctxt->nodeInfoNr > 0)
246
0
        ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr - 1];
247
0
    else
248
0
        ctxt->nodeInfo = NULL;
249
0
    return &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
250
0
}
251
252
/*
253
 * Macros for accessing the content. Those should be used only by the parser,
254
 * and not exported.
255
 *
256
 * Dirty macros, i.e. one need to make assumption on the context to use them
257
 *
258
 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
259
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
260
 *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
261
 *           in UNICODE mode. This should be used internally by the parser
262
 *           only to compare to ASCII values otherwise it would break when
263
 *           running with UTF-8 encoding.
264
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
265
 *           to compare on ASCII based substring.
266
 *   UPP(n)  returns the n'th next xmlChar converted to uppercase. Same as CUR
267
 *           it should be used only to compare on ASCII based substring.
268
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
269
 *           strings without newlines within the parser.
270
 *
271
 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
272
 *
273
 *   CURRENT Returns the current char value, with the full decoding of
274
 *           UTF-8 if we are using this mode. It returns an int.
275
 *   NEXT    Skip to the next character, this does the proper decoding
276
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
277
 *   NEXTL(l) Skip the current unicode character of l xmlChars long.
278
 *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
279
 */
280
281
0
#define UPPER (toupper(*ctxt->input->cur))
282
283
0
#define SKIP(val) ctxt->input->cur += (val),ctxt->input->col+=(val)
284
285
0
#define NXT(val) ctxt->input->cur[(val)]
286
287
0
#define UPP(val) (toupper(ctxt->input->cur[(val)]))
288
289
0
#define CUR_PTR ctxt->input->cur
290
0
#define BASE_PTR ctxt->input->base
291
292
0
#define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
293
0
       (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
294
0
  xmlParserInputShrink(ctxt->input)
295
296
0
#define GROW if ((ctxt->progressive == 0) &&       \
297
0
     (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK))  \
298
0
  xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
299
300
#define CURRENT ((int) (*ctxt->input->cur))
301
302
0
#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
303
304
/* Imported from XML */
305
306
/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
307
0
#define CUR ((int) (*ctxt->input->cur))
308
0
#define NEXT xmlNextChar(ctxt)
309
310
0
#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
311
312
313
0
#define NEXTL(l) do {             \
314
0
    if (*(ctxt->input->cur) == '\n') {         \
315
0
  ctxt->input->line++; ctxt->input->col = 1;      \
316
0
    } else ctxt->input->col++;           \
317
0
    ctxt->token = 0; ctxt->input->cur += l;       \
318
0
  } while (0)
319
320
/************
321
    \
322
    if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
323
    if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
324
 ************/
325
326
0
#define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
327
#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
328
329
#define COPY_BUF(l,b,i,v)           \
330
0
    if (l == 1) b[i++] = (xmlChar) v;         \
331
0
    else i += xmlCopyChar(l,&b[i],v)
332
333
/**
334
 * htmlFindEncoding:
335
 * @the HTML parser context
336
 *
337
 * Ty to find and encoding in the current data available in the input
338
 * buffer this is needed to try to switch to the proper encoding when
339
 * one face a character error.
340
 * That's an heuristic, since it's operating outside of parsing it could
341
 * try to use a meta which had been commented out, that's the reason it
342
 * should only be used in case of error, not as a default.
343
 *
344
 * Returns an encoding string or NULL if not found, the string need to
345
 *   be freed
346
 */
347
static xmlChar *
348
0
htmlFindEncoding(xmlParserCtxtPtr ctxt) {
349
0
    const xmlChar *start, *cur, *end;
350
351
0
    if ((ctxt == NULL) || (ctxt->input == NULL) ||
352
0
        (ctxt->input->encoding != NULL) || (ctxt->input->buf == NULL) ||
353
0
        (ctxt->input->buf->encoder != NULL))
354
0
        return(NULL);
355
0
    if ((ctxt->input->cur == NULL) || (ctxt->input->end == NULL))
356
0
        return(NULL);
357
358
0
    start = ctxt->input->cur;
359
0
    end = ctxt->input->end;
360
    /* we also expect the input buffer to be zero terminated */
361
0
    if (*end != 0)
362
0
        return(NULL);
363
364
0
    cur = xmlStrcasestr(start, BAD_CAST "HTTP-EQUIV");
365
0
    if (cur == NULL)
366
0
        return(NULL);
367
0
    cur = xmlStrcasestr(cur, BAD_CAST  "CONTENT");
368
0
    if (cur == NULL)
369
0
        return(NULL);
370
0
    cur = xmlStrcasestr(cur, BAD_CAST  "CHARSET=");
371
0
    if (cur == NULL)
372
0
        return(NULL);
373
0
    cur += 8;
374
0
    start = cur;
375
0
    while (((*cur >= 'A') && (*cur <= 'Z')) ||
376
0
           ((*cur >= 'a') && (*cur <= 'z')) ||
377
0
           ((*cur >= '0') && (*cur <= '9')) ||
378
0
           (*cur == '-') || (*cur == '_') || (*cur == ':') || (*cur == '/'))
379
0
           cur++;
380
0
    if (cur == start)
381
0
        return(NULL);
382
0
    return(xmlStrndup(start, cur - start));
383
0
}
384
385
/**
386
 * htmlCurrentChar:
387
 * @ctxt:  the HTML parser context
388
 * @len:  pointer to the length of the char read
389
 *
390
 * The current char value, if using UTF-8 this may actually span multiple
391
 * bytes in the input buffer. Implement the end of line normalization:
392
 * 2.11 End-of-Line Handling
393
 * If the encoding is unspecified, in the case we find an ISO-Latin-1
394
 * char, then the encoding converter is plugged in automatically.
395
 *
396
 * Returns the current char value and its length
397
 */
398
399
static int
400
0
htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
401
0
    const unsigned char *cur;
402
0
    unsigned char c;
403
0
    unsigned int val;
404
405
0
    if (ctxt->instate == XML_PARSER_EOF)
406
0
  return(0);
407
408
0
    if (ctxt->token != 0) {
409
0
  *len = 0;
410
0
  return(ctxt->token);
411
0
    }
412
0
    if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
413
0
        xmlChar * guess;
414
0
        xmlCharEncodingHandlerPtr handler;
415
416
        /*
417
         * Assume it's a fixed length encoding (1) with
418
         * a compatible encoding for the ASCII set, since
419
         * HTML constructs only use < 128 chars
420
         */
421
0
        if ((int) *ctxt->input->cur < 0x80) {
422
0
            *len = 1;
423
0
            if ((*ctxt->input->cur == 0) &&
424
0
                (ctxt->input->cur < ctxt->input->end)) {
425
0
                htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
426
0
                                "Char 0x%X out of allowed range\n", 0);
427
0
                return(' ');
428
0
            }
429
0
            return((int) *ctxt->input->cur);
430
0
        }
431
432
        /*
433
         * Humm this is bad, do an automatic flow conversion
434
         */
435
0
        guess = htmlFindEncoding(ctxt);
436
0
        if (guess == NULL) {
437
0
            xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
438
0
        } else {
439
0
            if (ctxt->input->encoding != NULL)
440
0
                xmlFree((xmlChar *) ctxt->input->encoding);
441
0
            ctxt->input->encoding = guess;
442
0
            handler = xmlFindCharEncodingHandler((const char *) guess);
443
0
            if (handler != NULL) {
444
                /*
445
                 * Don't use UTF-8 encoder which isn't required and
446
                 * can produce invalid UTF-8.
447
                 */
448
0
                if (!xmlStrEqual(BAD_CAST handler->name, BAD_CAST "UTF-8"))
449
0
                    xmlSwitchToEncoding(ctxt, handler);
450
0
            } else {
451
0
                htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
452
0
                             "Unsupported encoding %s", guess, NULL);
453
0
            }
454
0
        }
455
0
        ctxt->charset = XML_CHAR_ENCODING_UTF8;
456
0
    }
457
458
    /*
459
     * We are supposed to handle UTF8, check it's valid
460
     * From rfc2044: encoding of the Unicode values on UTF-8:
461
     *
462
     * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
463
     * 0000 0000-0000 007F   0xxxxxxx
464
     * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
465
     * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
466
     *
467
     * Check for the 0x110000 limit too
468
     */
469
0
    cur = ctxt->input->cur;
470
0
    c = *cur;
471
0
    if (c & 0x80) {
472
0
        if ((c & 0x40) == 0)
473
0
            goto encoding_error;
474
0
        if (cur[1] == 0) {
475
0
            xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
476
0
            cur = ctxt->input->cur;
477
0
        }
478
0
        if ((cur[1] & 0xc0) != 0x80)
479
0
            goto encoding_error;
480
0
        if ((c & 0xe0) == 0xe0) {
481
482
0
            if (cur[2] == 0) {
483
0
                xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
484
0
                cur = ctxt->input->cur;
485
0
            }
486
0
            if ((cur[2] & 0xc0) != 0x80)
487
0
                goto encoding_error;
488
0
            if ((c & 0xf0) == 0xf0) {
489
0
                if (cur[3] == 0) {
490
0
                    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
491
0
                    cur = ctxt->input->cur;
492
0
                }
493
0
                if (((c & 0xf8) != 0xf0) ||
494
0
                    ((cur[3] & 0xc0) != 0x80))
495
0
                    goto encoding_error;
496
                /* 4-byte code */
497
0
                *len = 4;
498
0
                val = (cur[0] & 0x7) << 18;
499
0
                val |= (cur[1] & 0x3f) << 12;
500
0
                val |= (cur[2] & 0x3f) << 6;
501
0
                val |= cur[3] & 0x3f;
502
0
                if (val < 0x10000)
503
0
                    goto encoding_error;
504
0
            } else {
505
              /* 3-byte code */
506
0
                *len = 3;
507
0
                val = (cur[0] & 0xf) << 12;
508
0
                val |= (cur[1] & 0x3f) << 6;
509
0
                val |= cur[2] & 0x3f;
510
0
                if (val < 0x800)
511
0
                    goto encoding_error;
512
0
            }
513
0
        } else {
514
          /* 2-byte code */
515
0
            *len = 2;
516
0
            val = (cur[0] & 0x1f) << 6;
517
0
            val |= cur[1] & 0x3f;
518
0
            if (val < 0x80)
519
0
                goto encoding_error;
520
0
        }
521
0
        if (!IS_CHAR(val)) {
522
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
523
0
                            "Char 0x%X out of allowed range\n", val);
524
0
        }
525
0
        return(val);
526
0
    } else {
527
0
        if ((*ctxt->input->cur == 0) &&
528
0
            (ctxt->input->cur < ctxt->input->end)) {
529
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
530
0
                            "Char 0x%X out of allowed range\n", 0);
531
0
            *len = 1;
532
0
            return(' ');
533
0
        }
534
        /* 1-byte code */
535
0
        *len = 1;
536
0
        return((int) *ctxt->input->cur);
537
0
    }
538
539
0
encoding_error:
540
    /*
541
     * If we detect an UTF8 error that probably mean that the
542
     * input encoding didn't get properly advertised in the
543
     * declaration header. Report the error and switch the encoding
544
     * to ISO-Latin-1 (if you don't like this policy, just declare the
545
     * encoding !)
546
     */
547
0
    {
548
0
        char buffer[150];
549
550
0
  if (ctxt->input->end - ctxt->input->cur >= 4) {
551
0
      snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
552
0
          ctxt->input->cur[0], ctxt->input->cur[1],
553
0
          ctxt->input->cur[2], ctxt->input->cur[3]);
554
0
  } else {
555
0
      snprintf(buffer, 149, "Bytes: 0x%02X\n", ctxt->input->cur[0]);
556
0
  }
557
0
  htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
558
0
         "Input is not proper UTF-8, indicate encoding !\n",
559
0
         BAD_CAST buffer, NULL);
560
0
    }
561
562
    /*
563
     * Don't switch encodings twice. Note that if there's an encoder, we
564
     * shouldn't receive invalid UTF-8 anyway.
565
     *
566
     * Note that if ctxt->input->buf == NULL, switching encodings is
567
     * impossible, see Gitlab issue #34.
568
     */
569
0
    if ((ctxt->input->buf != NULL) &&
570
0
        (ctxt->input->buf->encoder == NULL))
571
0
        xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
572
0
    *len = 1;
573
0
    return((int) *ctxt->input->cur);
574
0
}
575
576
/**
577
 * htmlSkipBlankChars:
578
 * @ctxt:  the HTML parser context
579
 *
580
 * skip all blanks character found at that point in the input streams.
581
 *
582
 * Returns the number of space chars skipped
583
 */
584
585
static int
586
0
htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
587
0
    int res = 0;
588
589
0
    while (IS_BLANK_CH(*(ctxt->input->cur))) {
590
0
  if ((*ctxt->input->cur == 0) &&
591
0
      (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
592
0
    xmlPopInput(ctxt);
593
0
  } else {
594
0
      if (*(ctxt->input->cur) == '\n') {
595
0
    ctxt->input->line++; ctxt->input->col = 1;
596
0
      } else ctxt->input->col++;
597
0
      ctxt->input->cur++;
598
0
      if (*ctxt->input->cur == 0)
599
0
    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
600
0
  }
601
0
  if (res < INT_MAX)
602
0
      res++;
603
0
    }
604
0
    return(res);
605
0
}
606
607
608
609
/************************************************************************
610
 *                  *
611
 *  The list of HTML elements and their properties    *
612
 *                  *
613
 ************************************************************************/
614
615
/*
616
 *  Start Tag: 1 means the start tag can be omitted
617
 *  End Tag:   1 means the end tag can be omitted
618
 *             2 means it's forbidden (empty elements)
619
 *             3 means the tag is stylistic and should be closed easily
620
 *  Depr:      this element is deprecated
621
 *  DTD:       1 means that this element is valid only in the Loose DTD
622
 *             2 means that this element is valid only in the Frameset DTD
623
 *
624
 * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
625
  , subElements , impliedsubelt , Attributes, userdata
626
 */
627
628
/* Definitions and a couple of vars for HTML Elements */
629
630
#define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
631
#define NB_FONTSTYLE 8
632
#define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
633
#define NB_PHRASE 10
634
#define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
635
#define NB_SPECIAL 16
636
#define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL
637
#define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL
638
#define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
639
#define NB_BLOCK NB_HEADING + NB_LIST + 14
640
#define FORMCTRL "input", "select", "textarea", "label", "button"
641
#define NB_FORMCTRL 5
642
#define PCDATA
643
#define NB_PCDATA 0
644
#define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
645
#define NB_HEADING 6
646
#define LIST "ul", "ol", "dir", "menu"
647
#define NB_LIST 4
648
#define MODIFIER
649
#define NB_MODIFIER 0
650
#define FLOW BLOCK,INLINE
651
#define NB_FLOW NB_BLOCK + NB_INLINE
652
#define EMPTY NULL
653
654
655
static const char* const html_flow[] = { FLOW, NULL } ;
656
static const char* const html_inline[] = { INLINE, NULL } ;
657
658
/* placeholders: elts with content but no subelements */
659
static const char* const html_pcdata[] = { NULL } ;
660
#define html_cdata html_pcdata
661
662
663
/* ... and for HTML Attributes */
664
665
#define COREATTRS "id", "class", "style", "title"
666
#define NB_COREATTRS 4
667
#define I18N "lang", "dir"
668
#define NB_I18N 2
669
#define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
670
#define NB_EVENTS 9
671
#define ATTRS COREATTRS,I18N,EVENTS
672
#define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS
673
#define CELLHALIGN "align", "char", "charoff"
674
#define NB_CELLHALIGN 3
675
#define CELLVALIGN "valign"
676
#define NB_CELLVALIGN 1
677
678
static const char* const html_attrs[] = { ATTRS, NULL } ;
679
static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
680
static const char* const core_attrs[] = { COREATTRS, NULL } ;
681
static const char* const i18n_attrs[] = { I18N, NULL } ;
682
683
684
/* Other declarations that should go inline ... */
685
static const char* const a_attrs[] = { ATTRS, "charset", "type", "name",
686
  "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
687
  "tabindex", "onfocus", "onblur", NULL } ;
688
static const char* const target_attr[] = { "target", NULL } ;
689
static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ;
690
static const char* const alt_attr[] = { "alt", NULL } ;
691
static const char* const src_alt_attrs[] = { "src", "alt", NULL } ;
692
static const char* const href_attrs[] = { "href", NULL } ;
693
static const char* const clear_attrs[] = { "clear", NULL } ;
694
static const char* const inline_p[] = { INLINE, "p", NULL } ;
695
696
static const char* const flow_param[] = { FLOW, "param", NULL } ;
697
static const char* const applet_attrs[] = { COREATTRS , "codebase",
698
    "archive", "alt", "name", "height", "width", "align",
699
    "hspace", "vspace", NULL } ;
700
static const char* const area_attrs[] = { "shape", "coords", "href", "nohref",
701
  "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
702
static const char* const basefont_attrs[] =
703
  { "id", "size", "color", "face", NULL } ;
704
static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ;
705
static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ;
706
static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
707
static const char* const body_depr[] = { "background", "bgcolor", "text",
708
  "link", "vlink", "alink", NULL } ;
709
static const char* const button_attrs[] = { ATTRS, "name", "value", "type",
710
  "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
711
712
713
static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
714
static const char* const col_elt[] = { "col", NULL } ;
715
static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
716
static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ;
717
static const char* const dl_contents[] = { "dt", "dd", NULL } ;
718
static const char* const compact_attr[] = { "compact", NULL } ;
719
static const char* const label_attr[] = { "label", NULL } ;
720
static const char* const fieldset_contents[] = { FLOW, "legend" } ;
721
static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
722
static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
723
static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
724
static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
725
static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
726
static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
727
static const char* const head_attrs[] = { I18N, "profile", NULL } ;
728
static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
729
static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
730
static const char* const version_attr[] = { "version", NULL } ;
731
static const char* const html_content[] = { "head", "body", "frameset", NULL } ;
732
static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
733
static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
734
static const char* const embed_attrs[] = { COREATTRS, "align", "alt", "border", "code", "codebase", "frameborder", "height", "hidden", "hspace", "name", "palette", "pluginspace", "pluginurl", "src", "type", "units", "vspace", "width", NULL } ;
735
static const char* const input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ;
736
static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
737
static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
738
static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ;
739
static const char* const align_attr[] = { "align", NULL } ;
740
static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
741
static const char* const map_contents[] = { BLOCK, "area", NULL } ;
742
static const char* const name_attr[] = { "name", NULL } ;
743
static const char* const action_attr[] = { "action", NULL } ;
744
static const char* const blockli_elt[] = { BLOCK, "li", NULL } ;
745
static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", "charset", NULL } ;
746
static const char* const content_attr[] = { "content", NULL } ;
747
static const char* const type_attr[] = { "type", NULL } ;
748
static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
749
static const char* const object_contents[] = { FLOW, "param", NULL } ;
750
static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
751
static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
752
static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ;
753
static const char* const option_elt[] = { "option", NULL } ;
754
static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
755
static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
756
static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
757
static const char* const width_attr[] = { "width", NULL } ;
758
static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
759
static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
760
static const char* const language_attr[] = { "language", NULL } ;
761
static const char* const select_content[] = { "optgroup", "option", NULL } ;
762
static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
763
static const char* const style_attrs[] = { I18N, "media", "title", NULL } ;
764
static const char* const table_attrs[] = { ATTRS, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
765
static const char* const table_depr[] = { "align", "bgcolor", NULL } ;
766
static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
767
static const char* const tr_elt[] = { "tr", NULL } ;
768
static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
769
static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
770
static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
771
static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
772
static const char* const tr_contents[] = { "th", "td", NULL } ;
773
static const char* const bgcolor_attr[] = { "bgcolor", NULL } ;
774
static const char* const li_elt[] = { "li", NULL } ;
775
static const char* const ul_depr[] = { "type", "compact", NULL} ;
776
static const char* const dir_attr[] = { "dir", NULL} ;
777
778
#define DECL (const char**)
779
780
static const htmlElemDesc
781
html40ElementTable[] = {
782
{ "a",    0, 0, 0, 0, 0, 0, 1, "anchor ",
783
  DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
784
},
785
{ "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
786
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
787
},
788
{ "acronym",  0, 0, 0, 0, 0, 0, 1, "",
789
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
790
},
791
{ "address",  0, 0, 0, 0, 0, 0, 0, "information on author ",
792
  DECL inline_p  , NULL , DECL html_attrs, NULL, NULL
793
},
794
{ "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
795
  DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
796
},
797
{ "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
798
  EMPTY ,  NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
799
},
800
{ "b",    0, 3, 0, 0, 0, 0, 1, "bold text style",
801
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
802
},
803
{ "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
804
  EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
805
},
806
{ "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
807
  EMPTY , NULL , NULL, DECL basefont_attrs, NULL
808
},
809
{ "bdo",  0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
810
  DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
811
},
812
{ "big",  0, 3, 0, 0, 0, 0, 1, "large text style",
813
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
814
},
815
{ "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
816
  DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
817
},
818
{ "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
819
  DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
820
},
821
{ "br",   0, 2, 2, 1, 0, 0, 1, "forced line break ",
822
  EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
823
},
824
{ "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
825
  DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
826
},
827
{ "caption",  0, 0, 0, 0, 0, 0, 0, "table caption ",
828
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
829
},
830
{ "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
831
  DECL html_flow , NULL , NULL, DECL html_attrs, NULL
832
},
833
{ "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
834
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
835
},
836
{ "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
837
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
838
},
839
{ "col",  0, 2, 2, 1, 0, 0, 0, "table column ",
840
  EMPTY , NULL , DECL col_attrs , NULL, NULL
841
},
842
{ "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
843
  DECL col_elt , "col" , DECL col_attrs , NULL, NULL
844
},
845
{ "dd",   0, 1, 0, 0, 0, 0, 0, "definition description ",
846
  DECL html_flow , NULL , DECL html_attrs, NULL, NULL
847
},
848
{ "del",  0, 0, 0, 0, 0, 0, 2, "deleted text ",
849
  DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
850
},
851
{ "dfn",  0, 0, 0, 0, 0, 0, 1, "instance definition",
852
  DECL html_inline , NULL , DECL html_attrs, NULL, NULL
853
},
854
{ "dir",  0, 0, 0, 0, 1, 1, 0, "directory list",
855
  DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
856
},
857
{ "div",  0, 0, 0, 0, 0, 0, 0, "generic language/style container",
858
  DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
859
},
860
{ "dl",   0, 0, 0, 0, 0, 0, 0, "definition list ",
861
  DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL
862
},
863
{ "dt",   0, 1, 0, 0, 0, 0, 0, "definition term ",
864
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
865
},
866
{ "em",   0, 3, 0, 0, 0, 0, 1, "emphasis",
867
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
868
},
869
{ "embed",  0, 1, 0, 0, 1, 1, 1, "generic embedded object ",
870
  EMPTY, NULL, DECL embed_attrs, NULL, NULL
871
},
872
{ "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
873
  DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
874
},
875
{ "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
876
  DECL html_inline, NULL, NULL, DECL font_attrs, NULL
877
},
878
{ "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
879
  DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
880
},
881
{ "frame",  0, 2, 2, 1, 0, 2, 0, "subwindow " ,
882
  EMPTY, NULL, NULL, DECL frame_attrs, NULL
883
},
884
{ "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
885
  DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
886
},
887
{ "h1",   0, 0, 0, 0, 0, 0, 0, "heading ",
888
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
889
},
890
{ "h2",   0, 0, 0, 0, 0, 0, 0, "heading ",
891
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
892
},
893
{ "h3",   0, 0, 0, 0, 0, 0, 0, "heading ",
894
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
895
},
896
{ "h4",   0, 0, 0, 0, 0, 0, 0, "heading ",
897
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
898
},
899
{ "h5",   0, 0, 0, 0, 0, 0, 0, "heading ",
900
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
901
},
902
{ "h6",   0, 0, 0, 0, 0, 0, 0, "heading ",
903
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
904
},
905
{ "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
906
  DECL head_contents, NULL, DECL head_attrs, NULL, NULL
907
},
908
{ "hr",   0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
909
  EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
910
},
911
{ "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
912
  DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
913
},
914
{ "i",    0, 3, 0, 0, 0, 0, 1, "italic text style",
915
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
916
},
917
{ "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
918
  DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
919
},
920
{ "img",  0, 2, 2, 1, 0, 0, 1, "embedded image ",
921
  EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs
922
},
923
{ "input",  0, 2, 2, 1, 0, 0, 1, "form control ",
924
  EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
925
},
926
{ "ins",  0, 0, 0, 0, 0, 0, 2, "inserted text",
927
  DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
928
},
929
{ "isindex",  0, 2, 2, 1, 1, 1, 0, "single line prompt ",
930
  EMPTY, NULL, NULL, DECL prompt_attrs, NULL
931
},
932
{ "kbd",  0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
933
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
934
},
935
{ "label",  0, 0, 0, 0, 0, 0, 1, "form field label text ",
936
  DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
937
},
938
{ "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
939
  DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
940
},
941
{ "li",   0, 1, 1, 0, 0, 0, 0, "list item ",
942
  DECL html_flow, NULL, DECL html_attrs, NULL, NULL
943
},
944
{ "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
945
  EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
946
},
947
{ "map",  0, 0, 0, 0, 0, 0, 2, "client-side image map ",
948
  DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr
949
},
950
{ "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
951
  DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
952
},
953
{ "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
954
  EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
955
},
956
{ "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
957
  DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
958
},
959
{ "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
960
  DECL html_flow, "div", DECL html_attrs, NULL, NULL
961
},
962
{ "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
963
  DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
964
},
965
{ "ol",   0, 0, 0, 0, 0, 0, 0, "ordered list ",
966
  DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
967
},
968
{ "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
969
  DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
970
},
971
{ "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
972
  DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
973
},
974
{ "p",    0, 1, 0, 0, 0, 0, 0, "paragraph ",
975
  DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
976
},
977
{ "param",  0, 2, 2, 1, 0, 0, 0, "named property value ",
978
  EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr
979
},
980
{ "pre",  0, 0, 0, 0, 0, 0, 0, "preformatted text ",
981
  DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
982
},
983
{ "q",    0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
984
  DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
985
},
986
{ "s",    0, 3, 0, 0, 1, 1, 1, "strike-through text style",
987
  DECL html_inline, NULL, NULL, DECL html_attrs, NULL
988
},
989
{ "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
990
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
991
},
992
{ "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
993
  DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
994
},
995
{ "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
996
  DECL select_content, NULL, DECL select_attrs, NULL, NULL
997
},
998
{ "small",  0, 3, 0, 0, 0, 0, 1, "small text style",
999
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1000
},
1001
{ "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
1002
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1003
},
1004
{ "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
1005
  DECL html_inline, NULL, NULL, DECL html_attrs, NULL
1006
},
1007
{ "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
1008
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1009
},
1010
{ "style",  0, 0, 0, 0, 0, 0, 0, "style info ",
1011
  DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
1012
},
1013
{ "sub",  0, 3, 0, 0, 0, 0, 1, "subscript",
1014
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1015
},
1016
{ "sup",  0, 3, 0, 0, 0, 0, 1, "superscript ",
1017
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1018
},
1019
{ "table",  0, 0, 0, 0, 0, 0, 0, "",
1020
  DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
1021
},
1022
{ "tbody",  1, 0, 0, 0, 0, 0, 0, "table body ",
1023
  DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1024
},
1025
{ "td",   0, 0, 0, 0, 0, 0, 0, "table data cell",
1026
  DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
1027
},
1028
{ "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
1029
  DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
1030
},
1031
{ "tfoot",  0, 1, 0, 0, 0, 0, 0, "table footer ",
1032
  DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1033
},
1034
{ "th",   0, 1, 0, 0, 0, 0, 0, "table header cell",
1035
  DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
1036
},
1037
{ "thead",  0, 1, 0, 0, 0, 0, 0, "table header ",
1038
  DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
1039
},
1040
{ "title",  0, 0, 0, 0, 0, 0, 0, "document title ",
1041
  DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
1042
},
1043
{ "tr",   0, 0, 0, 0, 0, 0, 0, "table row ",
1044
  DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
1045
},
1046
{ "tt",   0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
1047
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1048
},
1049
{ "u",    0, 3, 0, 0, 1, 1, 1, "underlined text style",
1050
  DECL html_inline, NULL, NULL, DECL html_attrs, NULL
1051
},
1052
{ "ul",   0, 0, 0, 0, 0, 0, 0, "unordered list ",
1053
  DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
1054
},
1055
{ "var",  0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
1056
  DECL html_inline, NULL, DECL html_attrs, NULL, NULL
1057
}
1058
};
1059
1060
typedef struct {
1061
    const char *oldTag;
1062
    const char *newTag;
1063
} htmlStartCloseEntry;
1064
1065
/*
1066
 * start tags that imply the end of current element
1067
 */
1068
static const htmlStartCloseEntry htmlStartClose[] = {
1069
    { "a", "a" },
1070
    { "a", "fieldset" },
1071
    { "a", "table" },
1072
    { "a", "td" },
1073
    { "a", "th" },
1074
    { "address", "dd" },
1075
    { "address", "dl" },
1076
    { "address", "dt" },
1077
    { "address", "form" },
1078
    { "address", "li" },
1079
    { "address", "ul" },
1080
    { "b", "center" },
1081
    { "b", "p" },
1082
    { "b", "td" },
1083
    { "b", "th" },
1084
    { "big", "p" },
1085
    { "caption", "col" },
1086
    { "caption", "colgroup" },
1087
    { "caption", "tbody" },
1088
    { "caption", "tfoot" },
1089
    { "caption", "thead" },
1090
    { "caption", "tr" },
1091
    { "col", "col" },
1092
    { "col", "colgroup" },
1093
    { "col", "tbody" },
1094
    { "col", "tfoot" },
1095
    { "col", "thead" },
1096
    { "col", "tr" },
1097
    { "colgroup", "colgroup" },
1098
    { "colgroup", "tbody" },
1099
    { "colgroup", "tfoot" },
1100
    { "colgroup", "thead" },
1101
    { "colgroup", "tr" },
1102
    { "dd", "dt" },
1103
    { "dir", "dd" },
1104
    { "dir", "dl" },
1105
    { "dir", "dt" },
1106
    { "dir", "form" },
1107
    { "dir", "ul" },
1108
    { "dl", "form" },
1109
    { "dl", "li" },
1110
    { "dt", "dd" },
1111
    { "dt", "dl" },
1112
    { "font", "center" },
1113
    { "font", "td" },
1114
    { "font", "th" },
1115
    { "form", "form" },
1116
    { "h1", "fieldset" },
1117
    { "h1", "form" },
1118
    { "h1", "li" },
1119
    { "h1", "p" },
1120
    { "h1", "table" },
1121
    { "h2", "fieldset" },
1122
    { "h2", "form" },
1123
    { "h2", "li" },
1124
    { "h2", "p" },
1125
    { "h2", "table" },
1126
    { "h3", "fieldset" },
1127
    { "h3", "form" },
1128
    { "h3", "li" },
1129
    { "h3", "p" },
1130
    { "h3", "table" },
1131
    { "h4", "fieldset" },
1132
    { "h4", "form" },
1133
    { "h4", "li" },
1134
    { "h4", "p" },
1135
    { "h4", "table" },
1136
    { "h5", "fieldset" },
1137
    { "h5", "form" },
1138
    { "h5", "li" },
1139
    { "h5", "p" },
1140
    { "h5", "table" },
1141
    { "h6", "fieldset" },
1142
    { "h6", "form" },
1143
    { "h6", "li" },
1144
    { "h6", "p" },
1145
    { "h6", "table" },
1146
    { "head", "a" },
1147
    { "head", "abbr" },
1148
    { "head", "acronym" },
1149
    { "head", "address" },
1150
    { "head", "b" },
1151
    { "head", "bdo" },
1152
    { "head", "big" },
1153
    { "head", "blockquote" },
1154
    { "head", "body" },
1155
    { "head", "br" },
1156
    { "head", "center" },
1157
    { "head", "cite" },
1158
    { "head", "code" },
1159
    { "head", "dd" },
1160
    { "head", "dfn" },
1161
    { "head", "dir" },
1162
    { "head", "div" },
1163
    { "head", "dl" },
1164
    { "head", "dt" },
1165
    { "head", "em" },
1166
    { "head", "fieldset" },
1167
    { "head", "font" },
1168
    { "head", "form" },
1169
    { "head", "frameset" },
1170
    { "head", "h1" },
1171
    { "head", "h2" },
1172
    { "head", "h3" },
1173
    { "head", "h4" },
1174
    { "head", "h5" },
1175
    { "head", "h6" },
1176
    { "head", "hr" },
1177
    { "head", "i" },
1178
    { "head", "iframe" },
1179
    { "head", "img" },
1180
    { "head", "kbd" },
1181
    { "head", "li" },
1182
    { "head", "listing" },
1183
    { "head", "map" },
1184
    { "head", "menu" },
1185
    { "head", "ol" },
1186
    { "head", "p" },
1187
    { "head", "pre" },
1188
    { "head", "q" },
1189
    { "head", "s" },
1190
    { "head", "samp" },
1191
    { "head", "small" },
1192
    { "head", "span" },
1193
    { "head", "strike" },
1194
    { "head", "strong" },
1195
    { "head", "sub" },
1196
    { "head", "sup" },
1197
    { "head", "table" },
1198
    { "head", "tt" },
1199
    { "head", "u" },
1200
    { "head", "ul" },
1201
    { "head", "var" },
1202
    { "head", "xmp" },
1203
    { "hr", "form" },
1204
    { "i", "center" },
1205
    { "i", "p" },
1206
    { "i", "td" },
1207
    { "i", "th" },
1208
    { "legend", "fieldset" },
1209
    { "li", "li" },
1210
    { "link", "body" },
1211
    { "link", "frameset" },
1212
    { "listing", "dd" },
1213
    { "listing", "dl" },
1214
    { "listing", "dt" },
1215
    { "listing", "fieldset" },
1216
    { "listing", "form" },
1217
    { "listing", "li" },
1218
    { "listing", "table" },
1219
    { "listing", "ul" },
1220
    { "menu", "dd" },
1221
    { "menu", "dl" },
1222
    { "menu", "dt" },
1223
    { "menu", "form" },
1224
    { "menu", "ul" },
1225
    { "ol", "form" },
1226
    { "ol", "ul" },
1227
    { "option", "optgroup" },
1228
    { "option", "option" },
1229
    { "p", "address" },
1230
    { "p", "blockquote" },
1231
    { "p", "body" },
1232
    { "p", "caption" },
1233
    { "p", "center" },
1234
    { "p", "col" },
1235
    { "p", "colgroup" },
1236
    { "p", "dd" },
1237
    { "p", "dir" },
1238
    { "p", "div" },
1239
    { "p", "dl" },
1240
    { "p", "dt" },
1241
    { "p", "fieldset" },
1242
    { "p", "form" },
1243
    { "p", "frameset" },
1244
    { "p", "h1" },
1245
    { "p", "h2" },
1246
    { "p", "h3" },
1247
    { "p", "h4" },
1248
    { "p", "h5" },
1249
    { "p", "h6" },
1250
    { "p", "head" },
1251
    { "p", "hr" },
1252
    { "p", "li" },
1253
    { "p", "listing" },
1254
    { "p", "menu" },
1255
    { "p", "ol" },
1256
    { "p", "p" },
1257
    { "p", "pre" },
1258
    { "p", "table" },
1259
    { "p", "tbody" },
1260
    { "p", "td" },
1261
    { "p", "tfoot" },
1262
    { "p", "th" },
1263
    { "p", "title" },
1264
    { "p", "tr" },
1265
    { "p", "ul" },
1266
    { "p", "xmp" },
1267
    { "pre", "dd" },
1268
    { "pre", "dl" },
1269
    { "pre", "dt" },
1270
    { "pre", "fieldset" },
1271
    { "pre", "form" },
1272
    { "pre", "li" },
1273
    { "pre", "table" },
1274
    { "pre", "ul" },
1275
    { "s", "p" },
1276
    { "script", "noscript" },
1277
    { "small", "p" },
1278
    { "span", "td" },
1279
    { "span", "th" },
1280
    { "strike", "p" },
1281
    { "style", "body" },
1282
    { "style", "frameset" },
1283
    { "tbody", "tbody" },
1284
    { "tbody", "tfoot" },
1285
    { "td", "tbody" },
1286
    { "td", "td" },
1287
    { "td", "tfoot" },
1288
    { "td", "th" },
1289
    { "td", "tr" },
1290
    { "tfoot", "tbody" },
1291
    { "th", "tbody" },
1292
    { "th", "td" },
1293
    { "th", "tfoot" },
1294
    { "th", "th" },
1295
    { "th", "tr" },
1296
    { "thead", "tbody" },
1297
    { "thead", "tfoot" },
1298
    { "title", "body" },
1299
    { "title", "frameset" },
1300
    { "tr", "tbody" },
1301
    { "tr", "tfoot" },
1302
    { "tr", "tr" },
1303
    { "tt", "p" },
1304
    { "u", "p" },
1305
    { "u", "td" },
1306
    { "u", "th" },
1307
    { "ul", "address" },
1308
    { "ul", "form" },
1309
    { "ul", "menu" },
1310
    { "ul", "ol" },
1311
    { "ul", "pre" },
1312
    { "xmp", "dd" },
1313
    { "xmp", "dl" },
1314
    { "xmp", "dt" },
1315
    { "xmp", "fieldset" },
1316
    { "xmp", "form" },
1317
    { "xmp", "li" },
1318
    { "xmp", "table" },
1319
    { "xmp", "ul" }
1320
};
1321
1322
/*
1323
 * The list of HTML elements which are supposed not to have
1324
 * CDATA content and where a p element will be implied
1325
 *
1326
 * TODO: extend that list by reading the HTML SGML DTD on
1327
 *       implied paragraph
1328
 */
1329
static const char *const htmlNoContentElements[] = {
1330
    "html",
1331
    "head",
1332
    NULL
1333
};
1334
1335
/*
1336
 * The list of HTML attributes which are of content %Script;
1337
 * NOTE: when adding ones, check htmlIsScriptAttribute() since
1338
 *       it assumes the name starts with 'on'
1339
 */
1340
static const char *const htmlScriptAttributes[] = {
1341
    "onclick",
1342
    "ondblclick",
1343
    "onmousedown",
1344
    "onmouseup",
1345
    "onmouseover",
1346
    "onmousemove",
1347
    "onmouseout",
1348
    "onkeypress",
1349
    "onkeydown",
1350
    "onkeyup",
1351
    "onload",
1352
    "onunload",
1353
    "onfocus",
1354
    "onblur",
1355
    "onsubmit",
1356
    "onreset",
1357
    "onchange",
1358
    "onselect"
1359
};
1360
1361
/*
1362
 * This table is used by the htmlparser to know what to do with
1363
 * broken html pages. By assigning different priorities to different
1364
 * elements the parser can decide how to handle extra endtags.
1365
 * Endtags are only allowed to close elements with lower or equal
1366
 * priority.
1367
 */
1368
1369
typedef struct {
1370
    const char *name;
1371
    int priority;
1372
} elementPriority;
1373
1374
static const elementPriority htmlEndPriority[] = {
1375
    {"div",   150},
1376
    {"td",    160},
1377
    {"th",    160},
1378
    {"tr",    170},
1379
    {"thead", 180},
1380
    {"tbody", 180},
1381
    {"tfoot", 180},
1382
    {"table", 190},
1383
    {"head",  200},
1384
    {"body",  200},
1385
    {"html",  220},
1386
    {NULL,    100} /* Default priority */
1387
};
1388
1389
/************************************************************************
1390
 *                  *
1391
 *  functions to handle HTML specific data      *
1392
 *                  *
1393
 ************************************************************************/
1394
1395
/**
1396
 * htmlInitAutoClose:
1397
 *
1398
 * DEPRECATED: This function will be made private. Call xmlInitParser to
1399
 * initialize the library.
1400
 *
1401
 * This is a no-op now.
1402
 */
1403
void
1404
1
htmlInitAutoClose(void) {
1405
1
}
1406
1407
static int
1408
0
htmlCompareTags(const void *key, const void *member) {
1409
0
    const xmlChar *tag = (const xmlChar *) key;
1410
0
    const htmlElemDesc *desc = (const htmlElemDesc *) member;
1411
1412
0
    return(xmlStrcasecmp(tag, BAD_CAST desc->name));
1413
0
}
1414
1415
/**
1416
 * htmlTagLookup:
1417
 * @tag:  The tag name in lowercase
1418
 *
1419
 * Lookup the HTML tag in the ElementTable
1420
 *
1421
 * Returns the related htmlElemDescPtr or NULL if not found.
1422
 */
1423
const htmlElemDesc *
1424
0
htmlTagLookup(const xmlChar *tag) {
1425
0
    if (tag == NULL)
1426
0
        return(NULL);
1427
1428
0
    return((const htmlElemDesc *) bsearch(tag, html40ElementTable,
1429
0
                sizeof(html40ElementTable) / sizeof(htmlElemDesc),
1430
0
                sizeof(htmlElemDesc), htmlCompareTags));
1431
0
}
1432
1433
/**
1434
 * htmlGetEndPriority:
1435
 * @name: The name of the element to look up the priority for.
1436
 *
1437
 * Return value: The "endtag" priority.
1438
 **/
1439
static int
1440
0
htmlGetEndPriority (const xmlChar *name) {
1441
0
    int i = 0;
1442
1443
0
    while ((htmlEndPriority[i].name != NULL) &&
1444
0
     (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
1445
0
  i++;
1446
1447
0
    return(htmlEndPriority[i].priority);
1448
0
}
1449
1450
1451
static int
1452
0
htmlCompareStartClose(const void *vkey, const void *member) {
1453
0
    const htmlStartCloseEntry *key = (const htmlStartCloseEntry *) vkey;
1454
0
    const htmlStartCloseEntry *entry = (const htmlStartCloseEntry *) member;
1455
0
    int ret;
1456
1457
0
    ret = strcmp(key->oldTag, entry->oldTag);
1458
0
    if (ret == 0)
1459
0
        ret = strcmp(key->newTag, entry->newTag);
1460
1461
0
    return(ret);
1462
0
}
1463
1464
/**
1465
 * htmlCheckAutoClose:
1466
 * @newtag:  The new tag name
1467
 * @oldtag:  The old tag name
1468
 *
1469
 * Checks whether the new tag is one of the registered valid tags for
1470
 * closing old.
1471
 *
1472
 * Returns 0 if no, 1 if yes.
1473
 */
1474
static int
1475
htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
1476
0
{
1477
0
    htmlStartCloseEntry key;
1478
0
    void *res;
1479
1480
0
    key.oldTag = (const char *) oldtag;
1481
0
    key.newTag = (const char *) newtag;
1482
0
    res = bsearch(&key, htmlStartClose,
1483
0
            sizeof(htmlStartClose) / sizeof(htmlStartCloseEntry),
1484
0
            sizeof(htmlStartCloseEntry), htmlCompareStartClose);
1485
0
    return(res != NULL);
1486
0
}
1487
1488
/**
1489
 * htmlAutoCloseOnClose:
1490
 * @ctxt:  an HTML parser context
1491
 * @newtag:  The new tag name
1492
 * @force:  force the tag closure
1493
 *
1494
 * The HTML DTD allows an ending tag to implicitly close other tags.
1495
 */
1496
static void
1497
htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1498
0
{
1499
0
    const htmlElemDesc *info;
1500
0
    int i, priority;
1501
1502
0
    priority = htmlGetEndPriority(newtag);
1503
1504
0
    for (i = (ctxt->nameNr - 1); i >= 0; i--) {
1505
1506
0
        if (xmlStrEqual(newtag, ctxt->nameTab[i]))
1507
0
            break;
1508
        /*
1509
         * A misplaced endtag can only close elements with lower
1510
         * or equal priority, so if we find an element with higher
1511
         * priority before we find an element with
1512
         * matching name, we just ignore this endtag
1513
         */
1514
0
        if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
1515
0
            return;
1516
0
    }
1517
0
    if (i < 0)
1518
0
        return;
1519
1520
0
    while (!xmlStrEqual(newtag, ctxt->name)) {
1521
0
        info = htmlTagLookup(ctxt->name);
1522
0
        if ((info != NULL) && (info->endTag == 3)) {
1523
0
            htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
1524
0
                   "Opening and ending tag mismatch: %s and %s\n",
1525
0
       newtag, ctxt->name);
1526
0
        }
1527
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1528
0
            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1529
0
  htmlnamePop(ctxt);
1530
0
    }
1531
0
}
1532
1533
/**
1534
 * htmlAutoCloseOnEnd:
1535
 * @ctxt:  an HTML parser context
1536
 *
1537
 * Close all remaining tags at the end of the stream
1538
 */
1539
static void
1540
htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
1541
0
{
1542
0
    int i;
1543
1544
0
    if (ctxt->nameNr == 0)
1545
0
        return;
1546
0
    for (i = (ctxt->nameNr - 1); i >= 0; i--) {
1547
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1548
0
            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1549
0
  htmlnamePop(ctxt);
1550
0
    }
1551
0
}
1552
1553
/**
1554
 * htmlAutoClose:
1555
 * @ctxt:  an HTML parser context
1556
 * @newtag:  The new tag name or NULL
1557
 *
1558
 * The HTML DTD allows a tag to implicitly close other tags.
1559
 * The list is kept in htmlStartClose array. This function is
1560
 * called when a new tag has been detected and generates the
1561
 * appropriates closes if possible/needed.
1562
 * If newtag is NULL this mean we are at the end of the resource
1563
 * and we should check
1564
 */
1565
static void
1566
htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
1567
0
{
1568
0
    while ((newtag != NULL) && (ctxt->name != NULL) &&
1569
0
           (htmlCheckAutoClose(newtag, ctxt->name))) {
1570
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1571
0
            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1572
0
  htmlnamePop(ctxt);
1573
0
    }
1574
0
    if (newtag == NULL) {
1575
0
        htmlAutoCloseOnEnd(ctxt);
1576
0
        return;
1577
0
    }
1578
0
    while ((newtag == NULL) && (ctxt->name != NULL) &&
1579
0
           ((xmlStrEqual(ctxt->name, BAD_CAST "head")) ||
1580
0
            (xmlStrEqual(ctxt->name, BAD_CAST "body")) ||
1581
0
            (xmlStrEqual(ctxt->name, BAD_CAST "html")))) {
1582
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1583
0
            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1584
0
  htmlnamePop(ctxt);
1585
0
    }
1586
0
}
1587
1588
/**
1589
 * htmlAutoCloseTag:
1590
 * @doc:  the HTML document
1591
 * @name:  The tag name
1592
 * @elem:  the HTML element
1593
 *
1594
 * The HTML DTD allows a tag to implicitly close other tags.
1595
 * The list is kept in htmlStartClose array. This function checks
1596
 * if the element or one of it's children would autoclose the
1597
 * given tag.
1598
 *
1599
 * Returns 1 if autoclose, 0 otherwise
1600
 */
1601
int
1602
0
htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
1603
0
    htmlNodePtr child;
1604
1605
0
    if (elem == NULL) return(1);
1606
0
    if (xmlStrEqual(name, elem->name)) return(0);
1607
0
    if (htmlCheckAutoClose(elem->name, name)) return(1);
1608
0
    child = elem->children;
1609
0
    while (child != NULL) {
1610
0
        if (htmlAutoCloseTag(doc, name, child)) return(1);
1611
0
  child = child->next;
1612
0
    }
1613
0
    return(0);
1614
0
}
1615
1616
/**
1617
 * htmlIsAutoClosed:
1618
 * @doc:  the HTML document
1619
 * @elem:  the HTML element
1620
 *
1621
 * The HTML DTD allows a tag to implicitly close other tags.
1622
 * The list is kept in htmlStartClose array. This function checks
1623
 * if a tag is autoclosed by one of it's child
1624
 *
1625
 * Returns 1 if autoclosed, 0 otherwise
1626
 */
1627
int
1628
0
htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
1629
0
    htmlNodePtr child;
1630
1631
0
    if (elem == NULL) return(1);
1632
0
    child = elem->children;
1633
0
    while (child != NULL) {
1634
0
  if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
1635
0
  child = child->next;
1636
0
    }
1637
0
    return(0);
1638
0
}
1639
1640
/**
1641
 * htmlCheckImplied:
1642
 * @ctxt:  an HTML parser context
1643
 * @newtag:  The new tag name
1644
 *
1645
 * The HTML DTD allows a tag to exists only implicitly
1646
 * called when a new tag has been detected and generates the
1647
 * appropriates implicit tags if missing
1648
 */
1649
static void
1650
0
htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
1651
0
    int i;
1652
1653
0
    if (ctxt->options & HTML_PARSE_NOIMPLIED)
1654
0
        return;
1655
0
    if (!htmlOmittedDefaultValue)
1656
0
  return;
1657
0
    if (xmlStrEqual(newtag, BAD_CAST"html"))
1658
0
  return;
1659
0
    if (ctxt->nameNr <= 0) {
1660
0
  htmlnamePush(ctxt, BAD_CAST"html");
1661
0
  if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1662
0
      ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
1663
0
    }
1664
0
    if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
1665
0
        return;
1666
0
    if ((ctxt->nameNr <= 1) &&
1667
0
        ((xmlStrEqual(newtag, BAD_CAST"script")) ||
1668
0
   (xmlStrEqual(newtag, BAD_CAST"style")) ||
1669
0
   (xmlStrEqual(newtag, BAD_CAST"meta")) ||
1670
0
   (xmlStrEqual(newtag, BAD_CAST"link")) ||
1671
0
   (xmlStrEqual(newtag, BAD_CAST"title")) ||
1672
0
   (xmlStrEqual(newtag, BAD_CAST"base")))) {
1673
0
        if (ctxt->html >= 3) {
1674
            /* we already saw or generated an <head> before */
1675
0
            return;
1676
0
        }
1677
        /*
1678
         * dropped OBJECT ... i you put it first BODY will be
1679
         * assumed !
1680
         */
1681
0
        htmlnamePush(ctxt, BAD_CAST"head");
1682
0
        if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1683
0
            ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
1684
0
    } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
1685
0
         (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
1686
0
         (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
1687
0
        if (ctxt->html >= 10) {
1688
            /* we already saw or generated a <body> before */
1689
0
            return;
1690
0
        }
1691
0
  for (i = 0;i < ctxt->nameNr;i++) {
1692
0
      if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
1693
0
    return;
1694
0
      }
1695
0
      if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
1696
0
    return;
1697
0
      }
1698
0
  }
1699
1700
0
  htmlnamePush(ctxt, BAD_CAST"body");
1701
0
  if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1702
0
      ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
1703
0
    }
1704
0
}
1705
1706
/**
1707
 * htmlCheckParagraph
1708
 * @ctxt:  an HTML parser context
1709
 *
1710
 * Check whether a p element need to be implied before inserting
1711
 * characters in the current element.
1712
 *
1713
 * Returns 1 if a paragraph has been inserted, 0 if not and -1
1714
 *         in case of error.
1715
 */
1716
1717
static int
1718
0
htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
1719
0
    const xmlChar *tag;
1720
0
    int i;
1721
1722
0
    if (ctxt == NULL)
1723
0
  return(-1);
1724
0
    tag = ctxt->name;
1725
0
    if (tag == NULL) {
1726
0
  htmlAutoClose(ctxt, BAD_CAST"p");
1727
0
  htmlCheckImplied(ctxt, BAD_CAST"p");
1728
0
  htmlnamePush(ctxt, BAD_CAST"p");
1729
0
  if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1730
0
      ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1731
0
  return(1);
1732
0
    }
1733
0
    if (!htmlOmittedDefaultValue)
1734
0
  return(0);
1735
0
    for (i = 0; htmlNoContentElements[i] != NULL; i++) {
1736
0
  if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
1737
0
      htmlAutoClose(ctxt, BAD_CAST"p");
1738
0
      htmlCheckImplied(ctxt, BAD_CAST"p");
1739
0
      htmlnamePush(ctxt, BAD_CAST"p");
1740
0
      if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
1741
0
    ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
1742
0
      return(1);
1743
0
  }
1744
0
    }
1745
0
    return(0);
1746
0
}
1747
1748
/**
1749
 * htmlIsScriptAttribute:
1750
 * @name:  an attribute name
1751
 *
1752
 * Check if an attribute is of content type Script
1753
 *
1754
 * Returns 1 is the attribute is a script 0 otherwise
1755
 */
1756
int
1757
0
htmlIsScriptAttribute(const xmlChar *name) {
1758
0
    unsigned int i;
1759
1760
0
    if (name == NULL)
1761
0
      return(0);
1762
    /*
1763
     * all script attributes start with 'on'
1764
     */
1765
0
    if ((name[0] != 'o') || (name[1] != 'n'))
1766
0
      return(0);
1767
0
    for (i = 0;
1768
0
   i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
1769
0
   i++) {
1770
0
  if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
1771
0
      return(1);
1772
0
    }
1773
0
    return(0);
1774
0
}
1775
1776
/************************************************************************
1777
 *                  *
1778
 *  The list of HTML predefined entities      *
1779
 *                  *
1780
 ************************************************************************/
1781
1782
1783
static const htmlEntityDesc  html40EntitiesTable[] = {
1784
/*
1785
 * the 4 absolute ones, plus apostrophe.
1786
 */
1787
{ 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
1788
{ 38, "amp",  "ampersand, U+0026 ISOnum" },
1789
{ 39, "apos", "single quote" },
1790
{ 60, "lt", "less-than sign, U+003C ISOnum" },
1791
{ 62, "gt", "greater-than sign, U+003E ISOnum" },
1792
1793
/*
1794
 * A bunch still in the 128-255 range
1795
 * Replacing them depend really on the charset used.
1796
 */
1797
{ 160,  "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
1798
{ 161,  "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
1799
{ 162,  "cent", "cent sign, U+00A2 ISOnum" },
1800
{ 163,  "pound","pound sign, U+00A3 ISOnum" },
1801
{ 164,  "curren","currency sign, U+00A4 ISOnum" },
1802
{ 165,  "yen",  "yen sign = yuan sign, U+00A5 ISOnum" },
1803
{ 166,  "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
1804
{ 167,  "sect", "section sign, U+00A7 ISOnum" },
1805
{ 168,  "uml",  "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
1806
{ 169,  "copy", "copyright sign, U+00A9 ISOnum" },
1807
{ 170,  "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
1808
{ 171,  "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
1809
{ 172,  "not",  "not sign, U+00AC ISOnum" },
1810
{ 173,  "shy",  "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
1811
{ 174,  "reg",  "registered sign = registered trade mark sign, U+00AE ISOnum" },
1812
{ 175,  "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
1813
{ 176,  "deg",  "degree sign, U+00B0 ISOnum" },
1814
{ 177,  "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
1815
{ 178,  "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
1816
{ 179,  "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
1817
{ 180,  "acute","acute accent = spacing acute, U+00B4 ISOdia" },
1818
{ 181,  "micro","micro sign, U+00B5 ISOnum" },
1819
{ 182,  "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1820
{ 183,  "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1821
{ 184,  "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
1822
{ 185,  "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
1823
{ 186,  "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1824
{ 187,  "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1825
{ 188,  "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
1826
{ 189,  "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
1827
{ 190,  "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
1828
{ 191,  "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
1829
{ 192,  "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
1830
{ 193,  "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
1831
{ 194,  "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
1832
{ 195,  "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
1833
{ 196,  "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
1834
{ 197,  "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
1835
{ 198,  "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
1836
{ 199,  "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
1837
{ 200,  "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
1838
{ 201,  "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
1839
{ 202,  "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
1840
{ 203,  "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
1841
{ 204,  "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
1842
{ 205,  "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
1843
{ 206,  "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
1844
{ 207,  "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
1845
{ 208,  "ETH",  "latin capital letter ETH, U+00D0 ISOlat1" },
1846
{ 209,  "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
1847
{ 210,  "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
1848
{ 211,  "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
1849
{ 212,  "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
1850
{ 213,  "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
1851
{ 214,  "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
1852
{ 215,  "times","multiplication sign, U+00D7 ISOnum" },
1853
{ 216,  "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1854
{ 217,  "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
1855
{ 218,  "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
1856
{ 219,  "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
1857
{ 220,  "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
1858
{ 221,  "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
1859
{ 222,  "THORN","latin capital letter THORN, U+00DE ISOlat1" },
1860
{ 223,  "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
1861
{ 224,  "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
1862
{ 225,  "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
1863
{ 226,  "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
1864
{ 227,  "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
1865
{ 228,  "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
1866
{ 229,  "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
1867
{ 230,  "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
1868
{ 231,  "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
1869
{ 232,  "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
1870
{ 233,  "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
1871
{ 234,  "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
1872
{ 235,  "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
1873
{ 236,  "igrave","latin small letter i with grave, U+00EC ISOlat1" },
1874
{ 237,  "iacute","latin small letter i with acute, U+00ED ISOlat1" },
1875
{ 238,  "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
1876
{ 239,  "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
1877
{ 240,  "eth",  "latin small letter eth, U+00F0 ISOlat1" },
1878
{ 241,  "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
1879
{ 242,  "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
1880
{ 243,  "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
1881
{ 244,  "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
1882
{ 245,  "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
1883
{ 246,  "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
1884
{ 247,  "divide","division sign, U+00F7 ISOnum" },
1885
{ 248,  "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
1886
{ 249,  "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
1887
{ 250,  "uacute","latin small letter u with acute, U+00FA ISOlat1" },
1888
{ 251,  "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
1889
{ 252,  "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
1890
{ 253,  "yacute","latin small letter y with acute, U+00FD ISOlat1" },
1891
{ 254,  "thorn","latin small letter thorn with, U+00FE ISOlat1" },
1892
{ 255,  "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
1893
1894
{ 338,  "OElig","latin capital ligature OE, U+0152 ISOlat2" },
1895
{ 339,  "oelig","latin small ligature oe, U+0153 ISOlat2" },
1896
{ 352,  "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
1897
{ 353,  "scaron","latin small letter s with caron, U+0161 ISOlat2" },
1898
{ 376,  "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
1899
1900
/*
1901
 * Anything below should really be kept as entities references
1902
 */
1903
{ 402,  "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
1904
1905
{ 710,  "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
1906
{ 732,  "tilde","small tilde, U+02DC ISOdia" },
1907
1908
{ 913,  "Alpha","greek capital letter alpha, U+0391" },
1909
{ 914,  "Beta", "greek capital letter beta, U+0392" },
1910
{ 915,  "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
1911
{ 916,  "Delta","greek capital letter delta, U+0394 ISOgrk3" },
1912
{ 917,  "Epsilon","greek capital letter epsilon, U+0395" },
1913
{ 918,  "Zeta", "greek capital letter zeta, U+0396" },
1914
{ 919,  "Eta",  "greek capital letter eta, U+0397" },
1915
{ 920,  "Theta","greek capital letter theta, U+0398 ISOgrk3" },
1916
{ 921,  "Iota", "greek capital letter iota, U+0399" },
1917
{ 922,  "Kappa","greek capital letter kappa, U+039A" },
1918
{ 923,  "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
1919
{ 924,  "Mu", "greek capital letter mu, U+039C" },
1920
{ 925,  "Nu", "greek capital letter nu, U+039D" },
1921
{ 926,  "Xi", "greek capital letter xi, U+039E ISOgrk3" },
1922
{ 927,  "Omicron","greek capital letter omicron, U+039F" },
1923
{ 928,  "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
1924
{ 929,  "Rho",  "greek capital letter rho, U+03A1" },
1925
{ 931,  "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
1926
{ 932,  "Tau",  "greek capital letter tau, U+03A4" },
1927
{ 933,  "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
1928
{ 934,  "Phi",  "greek capital letter phi, U+03A6 ISOgrk3" },
1929
{ 935,  "Chi",  "greek capital letter chi, U+03A7" },
1930
{ 936,  "Psi",  "greek capital letter psi, U+03A8 ISOgrk3" },
1931
{ 937,  "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
1932
1933
{ 945,  "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
1934
{ 946,  "beta", "greek small letter beta, U+03B2 ISOgrk3" },
1935
{ 947,  "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
1936
{ 948,  "delta","greek small letter delta, U+03B4 ISOgrk3" },
1937
{ 949,  "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
1938
{ 950,  "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
1939
{ 951,  "eta",  "greek small letter eta, U+03B7 ISOgrk3" },
1940
{ 952,  "theta","greek small letter theta, U+03B8 ISOgrk3" },
1941
{ 953,  "iota", "greek small letter iota, U+03B9 ISOgrk3" },
1942
{ 954,  "kappa","greek small letter kappa, U+03BA ISOgrk3" },
1943
{ 955,  "lambda","greek small letter lambda, U+03BB ISOgrk3" },
1944
{ 956,  "mu", "greek small letter mu, U+03BC ISOgrk3" },
1945
{ 957,  "nu", "greek small letter nu, U+03BD ISOgrk3" },
1946
{ 958,  "xi", "greek small letter xi, U+03BE ISOgrk3" },
1947
{ 959,  "omicron","greek small letter omicron, U+03BF NEW" },
1948
{ 960,  "pi", "greek small letter pi, U+03C0 ISOgrk3" },
1949
{ 961,  "rho",  "greek small letter rho, U+03C1 ISOgrk3" },
1950
{ 962,  "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
1951
{ 963,  "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
1952
{ 964,  "tau",  "greek small letter tau, U+03C4 ISOgrk3" },
1953
{ 965,  "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
1954
{ 966,  "phi",  "greek small letter phi, U+03C6 ISOgrk3" },
1955
{ 967,  "chi",  "greek small letter chi, U+03C7 ISOgrk3" },
1956
{ 968,  "psi",  "greek small letter psi, U+03C8 ISOgrk3" },
1957
{ 969,  "omega","greek small letter omega, U+03C9 ISOgrk3" },
1958
{ 977,  "thetasym","greek small letter theta symbol, U+03D1 NEW" },
1959
{ 978,  "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
1960
{ 982,  "piv",  "greek pi symbol, U+03D6 ISOgrk3" },
1961
1962
{ 8194, "ensp", "en space, U+2002 ISOpub" },
1963
{ 8195, "emsp", "em space, U+2003 ISOpub" },
1964
{ 8201, "thinsp","thin space, U+2009 ISOpub" },
1965
{ 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
1966
{ 8205, "zwj",  "zero width joiner, U+200D NEW RFC 2070" },
1967
{ 8206, "lrm",  "left-to-right mark, U+200E NEW RFC 2070" },
1968
{ 8207, "rlm",  "right-to-left mark, U+200F NEW RFC 2070" },
1969
{ 8211, "ndash","en dash, U+2013 ISOpub" },
1970
{ 8212, "mdash","em dash, U+2014 ISOpub" },
1971
{ 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
1972
{ 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
1973
{ 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
1974
{ 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
1975
{ 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
1976
{ 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
1977
{ 8224, "dagger","dagger, U+2020 ISOpub" },
1978
{ 8225, "Dagger","double dagger, U+2021 ISOpub" },
1979
1980
{ 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
1981
{ 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
1982
1983
{ 8240, "permil","per mille sign, U+2030 ISOtech" },
1984
1985
{ 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
1986
{ 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
1987
1988
{ 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1989
{ 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1990
1991
{ 8254, "oline","overline = spacing overscore, U+203E NEW" },
1992
{ 8260, "frasl","fraction slash, U+2044 NEW" },
1993
1994
{ 8364, "euro", "euro sign, U+20AC NEW" },
1995
1996
{ 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
1997
{ 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1998
{ 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
1999
{ 8482, "trade","trade mark sign, U+2122 ISOnum" },
2000
{ 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
2001
{ 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
2002
{ 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
2003
{ 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
2004
{ 8595, "darr", "downwards arrow, U+2193 ISOnum" },
2005
{ 8596, "harr", "left right arrow, U+2194 ISOamsa" },
2006
{ 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
2007
{ 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
2008
{ 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
2009
{ 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
2010
{ 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
2011
{ 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
2012
2013
{ 8704, "forall","for all, U+2200 ISOtech" },
2014
{ 8706, "part", "partial differential, U+2202 ISOtech" },
2015
{ 8707, "exist","there exists, U+2203 ISOtech" },
2016
{ 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
2017
{ 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
2018
{ 8712, "isin", "element of, U+2208 ISOtech" },
2019
{ 8713, "notin","not an element of, U+2209 ISOtech" },
2020
{ 8715, "ni", "contains as member, U+220B ISOtech" },
2021
{ 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
2022
{ 8721, "sum",  "n-ary summation, U+2211 ISOamsb" },
2023
{ 8722, "minus","minus sign, U+2212 ISOtech" },
2024
{ 8727, "lowast","asterisk operator, U+2217 ISOtech" },
2025
{ 8730, "radic","square root = radical sign, U+221A ISOtech" },
2026
{ 8733, "prop", "proportional to, U+221D ISOtech" },
2027
{ 8734, "infin","infinity, U+221E ISOtech" },
2028
{ 8736, "ang",  "angle, U+2220 ISOamso" },
2029
{ 8743, "and",  "logical and = wedge, U+2227 ISOtech" },
2030
{ 8744, "or", "logical or = vee, U+2228 ISOtech" },
2031
{ 8745, "cap",  "intersection = cap, U+2229 ISOtech" },
2032
{ 8746, "cup",  "union = cup, U+222A ISOtech" },
2033
{ 8747, "int",  "integral, U+222B ISOtech" },
2034
{ 8756, "there4","therefore, U+2234 ISOtech" },
2035
{ 8764, "sim",  "tilde operator = varies with = similar to, U+223C ISOtech" },
2036
{ 8773, "cong", "approximately equal to, U+2245 ISOtech" },
2037
{ 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
2038
{ 8800, "ne", "not equal to, U+2260 ISOtech" },
2039
{ 8801, "equiv","identical to, U+2261 ISOtech" },
2040
{ 8804, "le", "less-than or equal to, U+2264 ISOtech" },
2041
{ 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
2042
{ 8834, "sub",  "subset of, U+2282 ISOtech" },
2043
{ 8835, "sup",  "superset of, U+2283 ISOtech" },
2044
{ 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
2045
{ 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
2046
{ 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
2047
{ 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
2048
{ 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
2049
{ 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
2050
{ 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
2051
{ 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
2052
{ 8969, "rceil","right ceiling, U+2309 ISOamsc" },
2053
{ 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
2054
{ 8971, "rfloor","right floor, U+230B ISOamsc" },
2055
{ 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
2056
{ 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
2057
{ 9674, "loz",  "lozenge, U+25CA ISOpub" },
2058
2059
{ 9824, "spades","black spade suit, U+2660 ISOpub" },
2060
{ 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
2061
{ 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
2062
{ 9830, "diams","black diamond suit, U+2666 ISOpub" },
2063
2064
};
2065
2066
/************************************************************************
2067
 *                  *
2068
 *    Commodity functions to handle entities      *
2069
 *                  *
2070
 ************************************************************************/
2071
2072
/*
2073
 * Macro used to grow the current buffer.
2074
 */
2075
0
#define growBuffer(buffer) {           \
2076
0
    xmlChar *tmp;             \
2077
0
    buffer##_size *= 2;             \
2078
0
    tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
2079
0
    if (tmp == NULL) {           \
2080
0
  htmlErrMemory(ctxt, "growing buffer\n");      \
2081
0
  xmlFree(buffer);            \
2082
0
  return(NULL);             \
2083
0
    }                  \
2084
0
    buffer = tmp;             \
2085
0
}
2086
2087
/**
2088
 * htmlEntityLookup:
2089
 * @name: the entity name
2090
 *
2091
 * Lookup the given entity in EntitiesTable
2092
 *
2093
 * TODO: the linear scan is really ugly, an hash table is really needed.
2094
 *
2095
 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
2096
 */
2097
const htmlEntityDesc *
2098
0
htmlEntityLookup(const xmlChar *name) {
2099
0
    unsigned int i;
2100
2101
0
    for (i = 0;i < (sizeof(html40EntitiesTable)/
2102
0
                    sizeof(html40EntitiesTable[0]));i++) {
2103
0
        if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
2104
0
            return((htmlEntityDescPtr) &html40EntitiesTable[i]);
2105
0
  }
2106
0
    }
2107
0
    return(NULL);
2108
0
}
2109
2110
/**
2111
 * htmlEntityValueLookup:
2112
 * @value: the entity's unicode value
2113
 *
2114
 * Lookup the given entity in EntitiesTable
2115
 *
2116
 * TODO: the linear scan is really ugly, an hash table is really needed.
2117
 *
2118
 * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
2119
 */
2120
const htmlEntityDesc *
2121
0
htmlEntityValueLookup(unsigned int value) {
2122
0
    unsigned int i;
2123
2124
0
    for (i = 0;i < (sizeof(html40EntitiesTable)/
2125
0
                    sizeof(html40EntitiesTable[0]));i++) {
2126
0
        if (html40EntitiesTable[i].value >= value) {
2127
0
      if (html40EntitiesTable[i].value > value)
2128
0
    break;
2129
0
            return((htmlEntityDescPtr) &html40EntitiesTable[i]);
2130
0
  }
2131
0
    }
2132
0
    return(NULL);
2133
0
}
2134
2135
/**
2136
 * UTF8ToHtml:
2137
 * @out:  a pointer to an array of bytes to store the result
2138
 * @outlen:  the length of @out
2139
 * @in:  a pointer to an array of UTF-8 chars
2140
 * @inlen:  the length of @in
2141
 *
2142
 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2143
 * plus HTML entities block of chars out.
2144
 *
2145
 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2146
 * The value of @inlen after return is the number of octets consumed
2147
 *     as the return value is positive, else unpredictable.
2148
 * The value of @outlen after return is the number of octets consumed.
2149
 */
2150
int
2151
UTF8ToHtml(unsigned char* out, int *outlen,
2152
0
              const unsigned char* in, int *inlen) {
2153
0
    const unsigned char* processed = in;
2154
0
    const unsigned char* outend;
2155
0
    const unsigned char* outstart = out;
2156
0
    const unsigned char* instart = in;
2157
0
    const unsigned char* inend;
2158
0
    unsigned int c, d;
2159
0
    int trailing;
2160
2161
0
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
2162
0
    if (in == NULL) {
2163
        /*
2164
   * initialization nothing to do
2165
   */
2166
0
  *outlen = 0;
2167
0
  *inlen = 0;
2168
0
  return(0);
2169
0
    }
2170
0
    inend = in + (*inlen);
2171
0
    outend = out + (*outlen);
2172
0
    while (in < inend) {
2173
0
  d = *in++;
2174
0
  if      (d < 0x80)  { c= d; trailing= 0; }
2175
0
  else if (d < 0xC0) {
2176
      /* trailing byte in leading position */
2177
0
      *outlen = out - outstart;
2178
0
      *inlen = processed - instart;
2179
0
      return(-2);
2180
0
        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2181
0
        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2182
0
        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2183
0
  else {
2184
      /* no chance for this in Ascii */
2185
0
      *outlen = out - outstart;
2186
0
      *inlen = processed - instart;
2187
0
      return(-2);
2188
0
  }
2189
2190
0
  if (inend - in < trailing) {
2191
0
      break;
2192
0
  }
2193
2194
0
  for ( ; trailing; trailing--) {
2195
0
      if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
2196
0
    break;
2197
0
      c <<= 6;
2198
0
      c |= d & 0x3F;
2199
0
  }
2200
2201
  /* assertion: c is a single UTF-4 value */
2202
0
  if (c < 0x80) {
2203
0
      if (out + 1 >= outend)
2204
0
    break;
2205
0
      *out++ = c;
2206
0
  } else {
2207
0
      int len;
2208
0
      const htmlEntityDesc * ent;
2209
0
      const char *cp;
2210
0
      char nbuf[16];
2211
2212
      /*
2213
       * Try to lookup a predefined HTML entity for it
2214
       */
2215
2216
0
      ent = htmlEntityValueLookup(c);
2217
0
      if (ent == NULL) {
2218
0
        snprintf(nbuf, sizeof(nbuf), "#%u", c);
2219
0
        cp = nbuf;
2220
0
      }
2221
0
      else
2222
0
        cp = ent->name;
2223
0
      len = strlen(cp);
2224
0
      if (out + 2 + len >= outend)
2225
0
    break;
2226
0
      *out++ = '&';
2227
0
      memcpy(out, cp, len);
2228
0
      out += len;
2229
0
      *out++ = ';';
2230
0
  }
2231
0
  processed = in;
2232
0
    }
2233
0
    *outlen = out - outstart;
2234
0
    *inlen = processed - instart;
2235
0
    return(0);
2236
0
}
2237
2238
/**
2239
 * htmlEncodeEntities:
2240
 * @out:  a pointer to an array of bytes to store the result
2241
 * @outlen:  the length of @out
2242
 * @in:  a pointer to an array of UTF-8 chars
2243
 * @inlen:  the length of @in
2244
 * @quoteChar: the quote character to escape (' or ") or zero.
2245
 *
2246
 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2247
 * plus HTML entities block of chars out.
2248
 *
2249
 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2250
 * The value of @inlen after return is the number of octets consumed
2251
 *     as the return value is positive, else unpredictable.
2252
 * The value of @outlen after return is the number of octets consumed.
2253
 */
2254
int
2255
htmlEncodeEntities(unsigned char* out, int *outlen,
2256
0
       const unsigned char* in, int *inlen, int quoteChar) {
2257
0
    const unsigned char* processed = in;
2258
0
    const unsigned char* outend;
2259
0
    const unsigned char* outstart = out;
2260
0
    const unsigned char* instart = in;
2261
0
    const unsigned char* inend;
2262
0
    unsigned int c, d;
2263
0
    int trailing;
2264
2265
0
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL))
2266
0
        return(-1);
2267
0
    outend = out + (*outlen);
2268
0
    inend = in + (*inlen);
2269
0
    while (in < inend) {
2270
0
  d = *in++;
2271
0
  if      (d < 0x80)  { c= d; trailing= 0; }
2272
0
  else if (d < 0xC0) {
2273
      /* trailing byte in leading position */
2274
0
      *outlen = out - outstart;
2275
0
      *inlen = processed - instart;
2276
0
      return(-2);
2277
0
        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2278
0
        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2279
0
        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2280
0
  else {
2281
      /* no chance for this in Ascii */
2282
0
      *outlen = out - outstart;
2283
0
      *inlen = processed - instart;
2284
0
      return(-2);
2285
0
  }
2286
2287
0
  if (inend - in < trailing)
2288
0
      break;
2289
2290
0
  while (trailing--) {
2291
0
      if (((d= *in++) & 0xC0) != 0x80) {
2292
0
    *outlen = out - outstart;
2293
0
    *inlen = processed - instart;
2294
0
    return(-2);
2295
0
      }
2296
0
      c <<= 6;
2297
0
      c |= d & 0x3F;
2298
0
  }
2299
2300
  /* assertion: c is a single UTF-4 value */
2301
0
  if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
2302
0
      (c != '&') && (c != '<') && (c != '>')) {
2303
0
      if (out >= outend)
2304
0
    break;
2305
0
      *out++ = c;
2306
0
  } else {
2307
0
      const htmlEntityDesc * ent;
2308
0
      const char *cp;
2309
0
      char nbuf[16];
2310
0
      int len;
2311
2312
      /*
2313
       * Try to lookup a predefined HTML entity for it
2314
       */
2315
0
      ent = htmlEntityValueLookup(c);
2316
0
      if (ent == NULL) {
2317
0
    snprintf(nbuf, sizeof(nbuf), "#%u", c);
2318
0
    cp = nbuf;
2319
0
      }
2320
0
      else
2321
0
    cp = ent->name;
2322
0
      len = strlen(cp);
2323
0
      if (out + 2 + len > outend)
2324
0
    break;
2325
0
      *out++ = '&';
2326
0
      memcpy(out, cp, len);
2327
0
      out += len;
2328
0
      *out++ = ';';
2329
0
  }
2330
0
  processed = in;
2331
0
    }
2332
0
    *outlen = out - outstart;
2333
0
    *inlen = processed - instart;
2334
0
    return(0);
2335
0
}
2336
2337
/************************************************************************
2338
 *                  *
2339
 *    Commodity functions to handle streams     *
2340
 *                  *
2341
 ************************************************************************/
2342
2343
#ifdef LIBXML_PUSH_ENABLED
2344
/**
2345
 * htmlNewInputStream:
2346
 * @ctxt:  an HTML parser context
2347
 *
2348
 * Create a new input stream structure
2349
 * Returns the new input stream or NULL
2350
 */
2351
static htmlParserInputPtr
2352
0
htmlNewInputStream(htmlParserCtxtPtr ctxt) {
2353
0
    htmlParserInputPtr input;
2354
2355
0
    input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
2356
0
    if (input == NULL) {
2357
0
        htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
2358
0
  return(NULL);
2359
0
    }
2360
0
    memset(input, 0, sizeof(htmlParserInput));
2361
0
    input->filename = NULL;
2362
0
    input->directory = NULL;
2363
0
    input->base = NULL;
2364
0
    input->cur = NULL;
2365
0
    input->buf = NULL;
2366
0
    input->line = 1;
2367
0
    input->col = 1;
2368
0
    input->buf = NULL;
2369
0
    input->free = NULL;
2370
0
    input->version = NULL;
2371
0
    input->consumed = 0;
2372
0
    input->length = 0;
2373
0
    return(input);
2374
0
}
2375
#endif
2376
2377
2378
/************************************************************************
2379
 *                  *
2380
 *    Commodity functions, cleanup needed ?     *
2381
 *                  *
2382
 ************************************************************************/
2383
/*
2384
 * all tags allowing pc data from the html 4.01 loose dtd
2385
 * NOTE: it might be more appropriate to integrate this information
2386
 * into the html40ElementTable array but I don't want to risk any
2387
 * binary incompatibility
2388
 */
2389
static const char *allowPCData[] = {
2390
    "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
2391
    "blockquote", "body", "button", "caption", "center", "cite", "code",
2392
    "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
2393
    "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
2394
    "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
2395
    "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
2396
};
2397
2398
/**
2399
 * areBlanks:
2400
 * @ctxt:  an HTML parser context
2401
 * @str:  a xmlChar *
2402
 * @len:  the size of @str
2403
 *
2404
 * Is this a sequence of blank chars that one can ignore ?
2405
 *
2406
 * Returns 1 if ignorable 0 otherwise.
2407
 */
2408
2409
0
static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
2410
0
    unsigned int i;
2411
0
    int j;
2412
0
    xmlNodePtr lastChild;
2413
0
    xmlDtdPtr dtd;
2414
2415
0
    for (j = 0;j < len;j++)
2416
0
        if (!(IS_BLANK_CH(str[j]))) return(0);
2417
2418
0
    if (CUR == 0) return(1);
2419
0
    if (CUR != '<') return(0);
2420
0
    if (ctxt->name == NULL)
2421
0
  return(1);
2422
0
    if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
2423
0
  return(1);
2424
0
    if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
2425
0
  return(1);
2426
2427
    /* Only strip CDATA children of the body tag for strict HTML DTDs */
2428
0
    if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) {
2429
0
        dtd = xmlGetIntSubset(ctxt->myDoc);
2430
0
        if (dtd != NULL && dtd->ExternalID != NULL) {
2431
0
            if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") ||
2432
0
                    !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN"))
2433
0
                return(1);
2434
0
        }
2435
0
    }
2436
2437
0
    if (ctxt->node == NULL) return(0);
2438
0
    lastChild = xmlGetLastChild(ctxt->node);
2439
0
    while ((lastChild) && (lastChild->type == XML_COMMENT_NODE))
2440
0
  lastChild = lastChild->prev;
2441
0
    if (lastChild == NULL) {
2442
0
        if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2443
0
            (ctxt->node->content != NULL)) return(0);
2444
  /* keep ws in constructs like ...<b> </b>...
2445
     for all tags "b" allowing PCDATA */
2446
0
  for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2447
0
      if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
2448
0
    return(0);
2449
0
      }
2450
0
  }
2451
0
    } else if (xmlNodeIsText(lastChild)) {
2452
0
        return(0);
2453
0
    } else {
2454
  /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
2455
     for all tags "p" allowing PCDATA */
2456
0
  for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
2457
0
      if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
2458
0
    return(0);
2459
0
      }
2460
0
  }
2461
0
    }
2462
0
    return(1);
2463
0
}
2464
2465
/**
2466
 * htmlNewDocNoDtD:
2467
 * @URI:  URI for the dtd, or NULL
2468
 * @ExternalID:  the external ID of the DTD, or NULL
2469
 *
2470
 * Creates a new HTML document without a DTD node if @URI and @ExternalID
2471
 * are NULL
2472
 *
2473
 * Returns a new document, do not initialize the DTD if not provided
2474
 */
2475
htmlDocPtr
2476
0
htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
2477
0
    xmlDocPtr cur;
2478
2479
    /*
2480
     * Allocate a new document and fill the fields.
2481
     */
2482
0
    cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
2483
0
    if (cur == NULL) {
2484
0
  htmlErrMemory(NULL, "HTML document creation failed\n");
2485
0
  return(NULL);
2486
0
    }
2487
0
    memset(cur, 0, sizeof(xmlDoc));
2488
2489
0
    cur->type = XML_HTML_DOCUMENT_NODE;
2490
0
    cur->version = NULL;
2491
0
    cur->intSubset = NULL;
2492
0
    cur->doc = cur;
2493
0
    cur->name = NULL;
2494
0
    cur->children = NULL;
2495
0
    cur->extSubset = NULL;
2496
0
    cur->oldNs = NULL;
2497
0
    cur->encoding = NULL;
2498
0
    cur->standalone = 1;
2499
0
    cur->compression = 0;
2500
0
    cur->ids = NULL;
2501
0
    cur->refs = NULL;
2502
0
    cur->_private = NULL;
2503
0
    cur->charset = XML_CHAR_ENCODING_UTF8;
2504
0
    cur->properties = XML_DOC_HTML | XML_DOC_USERBUILT;
2505
0
    if ((ExternalID != NULL) ||
2506
0
  (URI != NULL))
2507
0
  xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
2508
0
    if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2509
0
  xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
2510
0
    return(cur);
2511
0
}
2512
2513
/**
2514
 * htmlNewDoc:
2515
 * @URI:  URI for the dtd, or NULL
2516
 * @ExternalID:  the external ID of the DTD, or NULL
2517
 *
2518
 * Creates a new HTML document
2519
 *
2520
 * Returns a new document
2521
 */
2522
htmlDocPtr
2523
0
htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
2524
0
    if ((URI == NULL) && (ExternalID == NULL))
2525
0
  return(htmlNewDocNoDtD(
2526
0
        BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
2527
0
        BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
2528
2529
0
    return(htmlNewDocNoDtD(URI, ExternalID));
2530
0
}
2531
2532
2533
/************************************************************************
2534
 *                  *
2535
 *      The parser itself       *
2536
 *  Relates to http://www.w3.org/TR/html40        *
2537
 *                  *
2538
 ************************************************************************/
2539
2540
/************************************************************************
2541
 *                  *
2542
 *      The parser itself       *
2543
 *                  *
2544
 ************************************************************************/
2545
2546
static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
2547
2548
/**
2549
 * htmlParseHTMLName:
2550
 * @ctxt:  an HTML parser context
2551
 *
2552
 * parse an HTML tag or attribute name, note that we convert it to lowercase
2553
 * since HTML names are not case-sensitive.
2554
 *
2555
 * Returns the Tag Name parsed or NULL
2556
 */
2557
2558
static const xmlChar *
2559
0
htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
2560
0
    int i = 0;
2561
0
    xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2562
2563
0
    if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
2564
0
        (CUR != ':') && (CUR != '.')) return(NULL);
2565
2566
0
    while ((i < HTML_PARSER_BUFFER_SIZE) &&
2567
0
           ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) ||
2568
0
     (CUR == ':') || (CUR == '-') || (CUR == '_') ||
2569
0
           (CUR == '.'))) {
2570
0
  if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2571
0
        else loc[i] = CUR;
2572
0
  i++;
2573
2574
0
  NEXT;
2575
0
    }
2576
2577
0
    return(xmlDictLookup(ctxt->dict, loc, i));
2578
0
}
2579
2580
2581
/**
2582
 * htmlParseHTMLName_nonInvasive:
2583
 * @ctxt:  an HTML parser context
2584
 *
2585
 * parse an HTML tag or attribute name, note that we convert it to lowercase
2586
 * since HTML names are not case-sensitive, this doesn't consume the data
2587
 * from the stream, it's a look-ahead
2588
 *
2589
 * Returns the Tag Name parsed or NULL
2590
 */
2591
2592
static const xmlChar *
2593
0
htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) {
2594
0
    int i = 0;
2595
0
    xmlChar loc[HTML_PARSER_BUFFER_SIZE];
2596
2597
0
    if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') &&
2598
0
        (NXT(1) != ':')) return(NULL);
2599
2600
0
    while ((i < HTML_PARSER_BUFFER_SIZE) &&
2601
0
           ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) ||
2602
0
     (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) {
2603
0
  if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20;
2604
0
        else loc[i] = NXT(1+i);
2605
0
  i++;
2606
0
    }
2607
2608
0
    return(xmlDictLookup(ctxt->dict, loc, i));
2609
0
}
2610
2611
2612
/**
2613
 * htmlParseName:
2614
 * @ctxt:  an HTML parser context
2615
 *
2616
 * parse an HTML name, this routine is case sensitive.
2617
 *
2618
 * Returns the Name parsed or NULL
2619
 */
2620
2621
static const xmlChar *
2622
0
htmlParseName(htmlParserCtxtPtr ctxt) {
2623
0
    const xmlChar *in;
2624
0
    const xmlChar *ret;
2625
0
    int count = 0;
2626
2627
0
    GROW;
2628
2629
    /*
2630
     * Accelerator for simple ASCII names
2631
     */
2632
0
    in = ctxt->input->cur;
2633
0
    if (((*in >= 0x61) && (*in <= 0x7A)) ||
2634
0
  ((*in >= 0x41) && (*in <= 0x5A)) ||
2635
0
  (*in == '_') || (*in == ':')) {
2636
0
  in++;
2637
0
  while (((*in >= 0x61) && (*in <= 0x7A)) ||
2638
0
         ((*in >= 0x41) && (*in <= 0x5A)) ||
2639
0
         ((*in >= 0x30) && (*in <= 0x39)) ||
2640
0
         (*in == '_') || (*in == '-') ||
2641
0
         (*in == ':') || (*in == '.'))
2642
0
      in++;
2643
2644
0
  if (in == ctxt->input->end)
2645
0
      return(NULL);
2646
2647
0
  if ((*in > 0) && (*in < 0x80)) {
2648
0
      count = in - ctxt->input->cur;
2649
0
      ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
2650
0
      ctxt->input->cur = in;
2651
0
      ctxt->input->col += count;
2652
0
      return(ret);
2653
0
  }
2654
0
    }
2655
0
    return(htmlParseNameComplex(ctxt));
2656
0
}
2657
2658
static const xmlChar *
2659
0
htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
2660
0
    int len = 0, l;
2661
0
    int c;
2662
0
    int count = 0;
2663
0
    const xmlChar *base = ctxt->input->base;
2664
2665
    /*
2666
     * Handler for more complex cases
2667
     */
2668
0
    GROW;
2669
0
    c = CUR_CHAR(l);
2670
0
    if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2671
0
  (!IS_LETTER(c) && (c != '_') &&
2672
0
         (c != ':'))) {
2673
0
  return(NULL);
2674
0
    }
2675
2676
0
    while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
2677
0
     ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
2678
0
            (c == '.') || (c == '-') ||
2679
0
      (c == '_') || (c == ':') ||
2680
0
      (IS_COMBINING(c)) ||
2681
0
      (IS_EXTENDER(c)))) {
2682
0
  if (count++ > 100) {
2683
0
      count = 0;
2684
0
      GROW;
2685
0
  }
2686
0
  len += l;
2687
0
  NEXTL(l);
2688
0
  c = CUR_CHAR(l);
2689
0
  if (ctxt->input->base != base) {
2690
      /*
2691
       * We changed encoding from an unknown encoding
2692
       * Input buffer changed location, so we better start again
2693
       */
2694
0
      return(htmlParseNameComplex(ctxt));
2695
0
  }
2696
0
    }
2697
2698
0
    if (ctxt->input->cur - ctxt->input->base < len) {
2699
        /* Sanity check */
2700
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
2701
0
                     "unexpected change of input buffer", NULL, NULL);
2702
0
        return (NULL);
2703
0
    }
2704
2705
0
    return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
2706
0
}
2707
2708
2709
/**
2710
 * htmlParseHTMLAttribute:
2711
 * @ctxt:  an HTML parser context
2712
 * @stop:  a char stop value
2713
 *
2714
 * parse an HTML attribute value till the stop (quote), if
2715
 * stop is 0 then it stops at the first space
2716
 *
2717
 * Returns the attribute parsed or NULL
2718
 */
2719
2720
static xmlChar *
2721
0
htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
2722
0
    xmlChar *buffer = NULL;
2723
0
    int buffer_size = 0;
2724
0
    xmlChar *out = NULL;
2725
0
    const xmlChar *name = NULL;
2726
0
    const xmlChar *cur = NULL;
2727
0
    const htmlEntityDesc * ent;
2728
2729
    /*
2730
     * allocate a translation buffer.
2731
     */
2732
0
    buffer_size = HTML_PARSER_BUFFER_SIZE;
2733
0
    buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
2734
0
    if (buffer == NULL) {
2735
0
  htmlErrMemory(ctxt, "buffer allocation failed\n");
2736
0
  return(NULL);
2737
0
    }
2738
0
    out = buffer;
2739
2740
    /*
2741
     * Ok loop until we reach one of the ending chars
2742
     */
2743
0
    while ((CUR != 0) && (CUR != stop)) {
2744
0
  if ((stop == 0) && (CUR == '>')) break;
2745
0
  if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
2746
0
        if (CUR == '&') {
2747
0
      if (NXT(1) == '#') {
2748
0
    unsigned int c;
2749
0
    int bits;
2750
2751
0
    c = htmlParseCharRef(ctxt);
2752
0
    if      (c <    0x80)
2753
0
            { *out++  = c;                bits= -6; }
2754
0
    else if (c <   0x800)
2755
0
            { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2756
0
    else if (c < 0x10000)
2757
0
            { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2758
0
    else
2759
0
            { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2760
2761
0
    for ( ; bits >= 0; bits-= 6) {
2762
0
        *out++  = ((c >> bits) & 0x3F) | 0x80;
2763
0
    }
2764
2765
0
    if (out - buffer > buffer_size - 100) {
2766
0
      int indx = out - buffer;
2767
2768
0
      growBuffer(buffer);
2769
0
      out = &buffer[indx];
2770
0
    }
2771
0
      } else {
2772
0
    ent = htmlParseEntityRef(ctxt, &name);
2773
0
    if (name == NULL) {
2774
0
        *out++ = '&';
2775
0
        if (out - buffer > buffer_size - 100) {
2776
0
      int indx = out - buffer;
2777
2778
0
      growBuffer(buffer);
2779
0
      out = &buffer[indx];
2780
0
        }
2781
0
    } else if (ent == NULL) {
2782
0
        *out++ = '&';
2783
0
        cur = name;
2784
0
        while (*cur != 0) {
2785
0
      if (out - buffer > buffer_size - 100) {
2786
0
          int indx = out - buffer;
2787
2788
0
          growBuffer(buffer);
2789
0
          out = &buffer[indx];
2790
0
      }
2791
0
      *out++ = *cur++;
2792
0
        }
2793
0
    } else {
2794
0
        unsigned int c;
2795
0
        int bits;
2796
2797
0
        if (out - buffer > buffer_size - 100) {
2798
0
      int indx = out - buffer;
2799
2800
0
      growBuffer(buffer);
2801
0
      out = &buffer[indx];
2802
0
        }
2803
0
        c = ent->value;
2804
0
        if      (c <    0x80)
2805
0
      { *out++  = c;                bits= -6; }
2806
0
        else if (c <   0x800)
2807
0
      { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2808
0
        else if (c < 0x10000)
2809
0
      { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2810
0
        else
2811
0
      { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2812
2813
0
        for ( ; bits >= 0; bits-= 6) {
2814
0
      *out++  = ((c >> bits) & 0x3F) | 0x80;
2815
0
        }
2816
0
    }
2817
0
      }
2818
0
  } else {
2819
0
      unsigned int c;
2820
0
      int bits, l;
2821
2822
0
      if (out - buffer > buffer_size - 100) {
2823
0
    int indx = out - buffer;
2824
2825
0
    growBuffer(buffer);
2826
0
    out = &buffer[indx];
2827
0
      }
2828
0
      c = CUR_CHAR(l);
2829
0
      if      (c <    0x80)
2830
0
        { *out++  = c;                bits= -6; }
2831
0
      else if (c <   0x800)
2832
0
        { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2833
0
      else if (c < 0x10000)
2834
0
        { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2835
0
      else
2836
0
        { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2837
2838
0
      for ( ; bits >= 0; bits-= 6) {
2839
0
    *out++  = ((c >> bits) & 0x3F) | 0x80;
2840
0
      }
2841
0
      NEXT;
2842
0
  }
2843
0
    }
2844
0
    *out = 0;
2845
0
    return(buffer);
2846
0
}
2847
2848
/**
2849
 * htmlParseEntityRef:
2850
 * @ctxt:  an HTML parser context
2851
 * @str:  location to store the entity name
2852
 *
2853
 * parse an HTML ENTITY references
2854
 *
2855
 * [68] EntityRef ::= '&' Name ';'
2856
 *
2857
 * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
2858
 *         if non-NULL *str will have to be freed by the caller.
2859
 */
2860
const htmlEntityDesc *
2861
0
htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
2862
0
    const xmlChar *name;
2863
0
    const htmlEntityDesc * ent = NULL;
2864
2865
0
    if (str != NULL) *str = NULL;
2866
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
2867
2868
0
    if (CUR == '&') {
2869
0
        NEXT;
2870
0
        name = htmlParseName(ctxt);
2871
0
  if (name == NULL) {
2872
0
      htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
2873
0
                   "htmlParseEntityRef: no name\n", NULL, NULL);
2874
0
  } else {
2875
0
      GROW;
2876
0
      if (CUR == ';') {
2877
0
          if (str != NULL)
2878
0
        *str = name;
2879
2880
    /*
2881
     * Lookup the entity in the table.
2882
     */
2883
0
    ent = htmlEntityLookup(name);
2884
0
    if (ent != NULL) /* OK that's ugly !!! */
2885
0
        NEXT;
2886
0
      } else {
2887
0
    htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
2888
0
                 "htmlParseEntityRef: expecting ';'\n",
2889
0
           NULL, NULL);
2890
0
          if (str != NULL)
2891
0
        *str = name;
2892
0
      }
2893
0
  }
2894
0
    }
2895
0
    return(ent);
2896
0
}
2897
2898
/**
2899
 * htmlParseAttValue:
2900
 * @ctxt:  an HTML parser context
2901
 *
2902
 * parse a value for an attribute
2903
 * Note: the parser won't do substitution of entities here, this
2904
 * will be handled later in xmlStringGetNodeList, unless it was
2905
 * asked for ctxt->replaceEntities != 0
2906
 *
2907
 * Returns the AttValue parsed or NULL.
2908
 */
2909
2910
static xmlChar *
2911
0
htmlParseAttValue(htmlParserCtxtPtr ctxt) {
2912
0
    xmlChar *ret = NULL;
2913
2914
0
    if (CUR == '"') {
2915
0
        NEXT;
2916
0
  ret = htmlParseHTMLAttribute(ctxt, '"');
2917
0
        if (CUR != '"') {
2918
0
      htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2919
0
                   "AttValue: \" expected\n", NULL, NULL);
2920
0
  } else
2921
0
      NEXT;
2922
0
    } else if (CUR == '\'') {
2923
0
        NEXT;
2924
0
  ret = htmlParseHTMLAttribute(ctxt, '\'');
2925
0
        if (CUR != '\'') {
2926
0
      htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2927
0
                   "AttValue: ' expected\n", NULL, NULL);
2928
0
  } else
2929
0
      NEXT;
2930
0
    } else {
2931
        /*
2932
   * That's an HTMLism, the attribute value may not be quoted
2933
   */
2934
0
  ret = htmlParseHTMLAttribute(ctxt, 0);
2935
0
  if (ret == NULL) {
2936
0
      htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
2937
0
                   "AttValue: no value found\n", NULL, NULL);
2938
0
  }
2939
0
    }
2940
0
    return(ret);
2941
0
}
2942
2943
/**
2944
 * htmlParseSystemLiteral:
2945
 * @ctxt:  an HTML parser context
2946
 *
2947
 * parse an HTML Literal
2948
 *
2949
 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2950
 *
2951
 * Returns the SystemLiteral parsed or NULL
2952
 */
2953
2954
static xmlChar *
2955
0
htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
2956
0
    size_t len = 0, startPosition = 0;
2957
0
    int err = 0;
2958
0
    int quote;
2959
0
    xmlChar *ret = NULL;
2960
2961
0
    if ((CUR != '"') && (CUR != '\'')) {
2962
0
  htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
2963
0
               "SystemLiteral \" or ' expected\n", NULL, NULL);
2964
0
        return(NULL);
2965
0
    }
2966
0
    quote = CUR;
2967
0
    NEXT;
2968
2969
0
    if (CUR_PTR < BASE_PTR)
2970
0
        return(ret);
2971
0
    startPosition = CUR_PTR - BASE_PTR;
2972
2973
0
    while ((CUR != 0) && (CUR != quote)) {
2974
        /* TODO: Handle UTF-8 */
2975
0
        if (!IS_CHAR_CH(CUR)) {
2976
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
2977
0
                            "Invalid char in SystemLiteral 0x%X\n", CUR);
2978
0
            err = 1;
2979
0
        }
2980
0
        NEXT;
2981
0
        len++;
2982
0
    }
2983
0
    if (CUR != quote) {
2984
0
        htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
2985
0
                     "Unfinished SystemLiteral\n", NULL, NULL);
2986
0
    } else {
2987
0
        NEXT;
2988
0
        if (err == 0)
2989
0
            ret = xmlStrndup((BASE_PTR+startPosition), len);
2990
0
    }
2991
2992
0
    return(ret);
2993
0
}
2994
2995
/**
2996
 * htmlParsePubidLiteral:
2997
 * @ctxt:  an HTML parser context
2998
 *
2999
 * parse an HTML public literal
3000
 *
3001
 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3002
 *
3003
 * Returns the PubidLiteral parsed or NULL.
3004
 */
3005
3006
static xmlChar *
3007
0
htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
3008
0
    size_t len = 0, startPosition = 0;
3009
0
    int err = 0;
3010
0
    int quote;
3011
0
    xmlChar *ret = NULL;
3012
3013
0
    if ((CUR != '"') && (CUR != '\'')) {
3014
0
  htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
3015
0
               "PubidLiteral \" or ' expected\n", NULL, NULL);
3016
0
        return(NULL);
3017
0
    }
3018
0
    quote = CUR;
3019
0
    NEXT;
3020
3021
    /*
3022
     * Name ::= (Letter | '_') (NameChar)*
3023
     */
3024
0
    if (CUR_PTR < BASE_PTR)
3025
0
        return(ret);
3026
0
    startPosition = CUR_PTR - BASE_PTR;
3027
3028
0
    while ((CUR != 0) && (CUR != quote)) {
3029
0
        if (!IS_PUBIDCHAR_CH(CUR)) {
3030
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3031
0
                            "Invalid char in PubidLiteral 0x%X\n", CUR);
3032
0
            err = 1;
3033
0
        }
3034
0
        len++;
3035
0
        NEXT;
3036
0
    }
3037
3038
0
    if (CUR != quote) {
3039
0
        htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
3040
0
                     "Unfinished PubidLiteral\n", NULL, NULL);
3041
0
    } else {
3042
0
        NEXT;
3043
0
        if (err == 0)
3044
0
            ret = xmlStrndup((BASE_PTR + startPosition), len);
3045
0
    }
3046
3047
0
    return(ret);
3048
0
}
3049
3050
/**
3051
 * htmlParseScript:
3052
 * @ctxt:  an HTML parser context
3053
 *
3054
 * parse the content of an HTML SCRIPT or STYLE element
3055
 * http://www.w3.org/TR/html4/sgml/dtd.html#Script
3056
 * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
3057
 * http://www.w3.org/TR/html4/types.html#type-script
3058
 * http://www.w3.org/TR/html4/types.html#h-6.15
3059
 * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
3060
 *
3061
 * Script data ( %Script; in the DTD) can be the content of the SCRIPT
3062
 * element and the value of intrinsic event attributes. User agents must
3063
 * not evaluate script data as HTML markup but instead must pass it on as
3064
 * data to a script engine.
3065
 * NOTES:
3066
 * - The content is passed like CDATA
3067
 * - the attributes for style and scripting "onXXX" are also described
3068
 *   as CDATA but SGML allows entities references in attributes so their
3069
 *   processing is identical as other attributes
3070
 */
3071
static void
3072
0
htmlParseScript(htmlParserCtxtPtr ctxt) {
3073
0
    xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
3074
0
    int nbchar = 0;
3075
0
    int cur,l;
3076
3077
0
    SHRINK;
3078
0
    cur = CUR_CHAR(l);
3079
0
    while (cur != 0) {
3080
0
  if ((cur == '<') && (NXT(1) == '/')) {
3081
            /*
3082
             * One should break here, the specification is clear:
3083
             * Authors should therefore escape "</" within the content.
3084
             * Escape mechanisms are specific to each scripting or
3085
             * style sheet language.
3086
             *
3087
             * In recovery mode, only break if end tag match the
3088
             * current tag, effectively ignoring all tags inside the
3089
             * script/style block and treating the entire block as
3090
             * CDATA.
3091
             */
3092
0
            if (ctxt->recovery) {
3093
0
                if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2,
3094
0
           xmlStrlen(ctxt->name)) == 0)
3095
0
                {
3096
0
                    break; /* while */
3097
0
                } else {
3098
0
        htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
3099
0
         "Element %s embeds close tag\n",
3100
0
                     ctxt->name, NULL);
3101
0
    }
3102
0
            } else {
3103
0
                if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
3104
0
                    ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
3105
0
                {
3106
0
                    break; /* while */
3107
0
                }
3108
0
            }
3109
0
  }
3110
0
        if (IS_CHAR(cur)) {
3111
0
      COPY_BUF(l,buf,nbchar,cur);
3112
0
        } else {
3113
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3114
0
                            "Invalid char in CDATA 0x%X\n", cur);
3115
0
        }
3116
0
  if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
3117
0
            buf[nbchar] = 0;
3118
0
      if (ctxt->sax->cdataBlock!= NULL) {
3119
    /*
3120
     * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
3121
     */
3122
0
    ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
3123
0
      } else if (ctxt->sax->characters != NULL) {
3124
0
    ctxt->sax->characters(ctxt->userData, buf, nbchar);
3125
0
      }
3126
0
      nbchar = 0;
3127
0
  }
3128
0
  GROW;
3129
0
  NEXTL(l);
3130
0
  cur = CUR_CHAR(l);
3131
0
    }
3132
3133
0
    if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
3134
0
        buf[nbchar] = 0;
3135
0
  if (ctxt->sax->cdataBlock!= NULL) {
3136
      /*
3137
       * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
3138
       */
3139
0
      ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
3140
0
  } else if (ctxt->sax->characters != NULL) {
3141
0
      ctxt->sax->characters(ctxt->userData, buf, nbchar);
3142
0
  }
3143
0
    }
3144
0
}
3145
3146
3147
/**
3148
 * htmlParseCharDataInternal:
3149
 * @ctxt:  an HTML parser context
3150
 * @readahead: optional read ahead character in ascii range
3151
 *
3152
 * parse a CharData section.
3153
 * if we are within a CDATA section ']]>' marks an end of section.
3154
 *
3155
 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3156
 */
3157
3158
static void
3159
0
htmlParseCharDataInternal(htmlParserCtxtPtr ctxt, int readahead) {
3160
0
    xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 6];
3161
0
    int nbchar = 0;
3162
0
    int cur, l;
3163
0
    int chunk = 0;
3164
3165
0
    if (readahead)
3166
0
        buf[nbchar++] = readahead;
3167
3168
0
    SHRINK;
3169
0
    cur = CUR_CHAR(l);
3170
0
    while (((cur != '<') || (ctxt->token == '<')) &&
3171
0
           ((cur != '&') || (ctxt->token == '&')) &&
3172
0
     (cur != 0)) {
3173
0
  if (!(IS_CHAR(cur))) {
3174
0
      htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3175
0
                  "Invalid char in CDATA 0x%X\n", cur);
3176
0
  } else {
3177
0
      COPY_BUF(l,buf,nbchar,cur);
3178
0
  }
3179
0
  if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
3180
0
            buf[nbchar] = 0;
3181
3182
      /*
3183
       * Ok the segment is to be consumed as chars.
3184
       */
3185
0
      if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
3186
0
    if (areBlanks(ctxt, buf, nbchar)) {
3187
0
        if (ctxt->keepBlanks) {
3188
0
      if (ctxt->sax->characters != NULL)
3189
0
          ctxt->sax->characters(ctxt->userData, buf, nbchar);
3190
0
        } else {
3191
0
      if (ctxt->sax->ignorableWhitespace != NULL)
3192
0
          ctxt->sax->ignorableWhitespace(ctxt->userData,
3193
0
                                         buf, nbchar);
3194
0
        }
3195
0
    } else {
3196
0
        htmlCheckParagraph(ctxt);
3197
0
        if (ctxt->sax->characters != NULL)
3198
0
      ctxt->sax->characters(ctxt->userData, buf, nbchar);
3199
0
    }
3200
0
      }
3201
0
      nbchar = 0;
3202
0
  }
3203
0
  NEXTL(l);
3204
0
        chunk++;
3205
0
        if (chunk > HTML_PARSER_BUFFER_SIZE) {
3206
0
            chunk = 0;
3207
0
            SHRINK;
3208
0
            GROW;
3209
0
        }
3210
0
  cur = CUR_CHAR(l);
3211
0
  if (cur == 0) {
3212
0
      SHRINK;
3213
0
      GROW;
3214
0
      cur = CUR_CHAR(l);
3215
0
  }
3216
0
    }
3217
0
    if (nbchar != 0) {
3218
0
        buf[nbchar] = 0;
3219
3220
  /*
3221
   * Ok the segment is to be consumed as chars.
3222
   */
3223
0
  if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
3224
0
      if (areBlanks(ctxt, buf, nbchar)) {
3225
0
    if (ctxt->keepBlanks) {
3226
0
        if (ctxt->sax->characters != NULL)
3227
0
      ctxt->sax->characters(ctxt->userData, buf, nbchar);
3228
0
    } else {
3229
0
        if (ctxt->sax->ignorableWhitespace != NULL)
3230
0
      ctxt->sax->ignorableWhitespace(ctxt->userData,
3231
0
                                     buf, nbchar);
3232
0
    }
3233
0
      } else {
3234
0
    htmlCheckParagraph(ctxt);
3235
0
    if (ctxt->sax->characters != NULL)
3236
0
        ctxt->sax->characters(ctxt->userData, buf, nbchar);
3237
0
      }
3238
0
  }
3239
0
    } else {
3240
  /*
3241
   * Loop detection
3242
   */
3243
0
  if (cur == 0)
3244
0
      ctxt->instate = XML_PARSER_EOF;
3245
0
    }
3246
0
}
3247
3248
/**
3249
 * htmlParseCharData:
3250
 * @ctxt:  an HTML parser context
3251
 *
3252
 * parse a CharData section.
3253
 * if we are within a CDATA section ']]>' marks an end of section.
3254
 *
3255
 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3256
 */
3257
3258
static void
3259
0
htmlParseCharData(htmlParserCtxtPtr ctxt) {
3260
0
    htmlParseCharDataInternal(ctxt, 0);
3261
0
}
3262
3263
/**
3264
 * htmlParseExternalID:
3265
 * @ctxt:  an HTML parser context
3266
 * @publicID:  a xmlChar** receiving PubidLiteral
3267
 *
3268
 * Parse an External ID or a Public ID
3269
 *
3270
 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3271
 *                   | 'PUBLIC' S PubidLiteral S SystemLiteral
3272
 *
3273
 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3274
 *
3275
 * Returns the function returns SystemLiteral and in the second
3276
 *                case publicID receives PubidLiteral, is strict is off
3277
 *                it is possible to return NULL and have publicID set.
3278
 */
3279
3280
static xmlChar *
3281
0
htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
3282
0
    xmlChar *URI = NULL;
3283
3284
0
    if ((UPPER == 'S') && (UPP(1) == 'Y') &&
3285
0
         (UPP(2) == 'S') && (UPP(3) == 'T') &&
3286
0
   (UPP(4) == 'E') && (UPP(5) == 'M')) {
3287
0
        SKIP(6);
3288
0
  if (!IS_BLANK_CH(CUR)) {
3289
0
      htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3290
0
                   "Space required after 'SYSTEM'\n", NULL, NULL);
3291
0
  }
3292
0
        SKIP_BLANKS;
3293
0
  URI = htmlParseSystemLiteral(ctxt);
3294
0
  if (URI == NULL) {
3295
0
      htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
3296
0
                   "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
3297
0
        }
3298
0
    } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
3299
0
         (UPP(2) == 'B') && (UPP(3) == 'L') &&
3300
0
         (UPP(4) == 'I') && (UPP(5) == 'C')) {
3301
0
        SKIP(6);
3302
0
  if (!IS_BLANK_CH(CUR)) {
3303
0
      htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3304
0
                   "Space required after 'PUBLIC'\n", NULL, NULL);
3305
0
  }
3306
0
        SKIP_BLANKS;
3307
0
  *publicID = htmlParsePubidLiteral(ctxt);
3308
0
  if (*publicID == NULL) {
3309
0
      htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
3310
0
                   "htmlParseExternalID: PUBLIC, no Public Identifier\n",
3311
0
       NULL, NULL);
3312
0
  }
3313
0
        SKIP_BLANKS;
3314
0
        if ((CUR == '"') || (CUR == '\'')) {
3315
0
      URI = htmlParseSystemLiteral(ctxt);
3316
0
  }
3317
0
    }
3318
0
    return(URI);
3319
0
}
3320
3321
/**
3322
 * xmlParsePI:
3323
 * @ctxt:  an XML parser context
3324
 *
3325
 * parse an XML Processing Instruction.
3326
 *
3327
 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3328
 */
3329
static void
3330
0
htmlParsePI(htmlParserCtxtPtr ctxt) {
3331
0
    xmlChar *buf = NULL;
3332
0
    int len = 0;
3333
0
    int size = HTML_PARSER_BUFFER_SIZE;
3334
0
    int cur, l;
3335
0
    const xmlChar *target;
3336
0
    xmlParserInputState state;
3337
0
    int count = 0;
3338
3339
0
    if ((RAW == '<') && (NXT(1) == '?')) {
3340
0
  state = ctxt->instate;
3341
0
        ctxt->instate = XML_PARSER_PI;
3342
  /*
3343
   * this is a Processing Instruction.
3344
   */
3345
0
  SKIP(2);
3346
0
  SHRINK;
3347
3348
  /*
3349
   * Parse the target name and check for special support like
3350
   * namespace.
3351
   */
3352
0
        target = htmlParseName(ctxt);
3353
0
  if (target != NULL) {
3354
0
      if (RAW == '>') {
3355
0
    SKIP(1);
3356
3357
    /*
3358
     * SAX: PI detected.
3359
     */
3360
0
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
3361
0
        (ctxt->sax->processingInstruction != NULL))
3362
0
        ctxt->sax->processingInstruction(ctxt->userData,
3363
0
                                         target, NULL);
3364
0
    ctxt->instate = state;
3365
0
    return;
3366
0
      }
3367
0
      buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
3368
0
      if (buf == NULL) {
3369
0
    htmlErrMemory(ctxt, NULL);
3370
0
    ctxt->instate = state;
3371
0
    return;
3372
0
      }
3373
0
      cur = CUR;
3374
0
      if (!IS_BLANK(cur)) {
3375
0
    htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
3376
0
        "ParsePI: PI %s space expected\n", target, NULL);
3377
0
      }
3378
0
            SKIP_BLANKS;
3379
0
      cur = CUR_CHAR(l);
3380
0
      while ((cur != 0) && (cur != '>')) {
3381
0
    if (len + 5 >= size) {
3382
0
        xmlChar *tmp;
3383
3384
0
        size *= 2;
3385
0
        tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3386
0
        if (tmp == NULL) {
3387
0
      htmlErrMemory(ctxt, NULL);
3388
0
      xmlFree(buf);
3389
0
      ctxt->instate = state;
3390
0
      return;
3391
0
        }
3392
0
        buf = tmp;
3393
0
    }
3394
0
    count++;
3395
0
    if (count > 50) {
3396
0
        GROW;
3397
0
        count = 0;
3398
0
    }
3399
0
                if (IS_CHAR(cur)) {
3400
0
        COPY_BUF(l,buf,len,cur);
3401
0
                } else {
3402
0
                    htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3403
0
                                    "Invalid char in processing instruction "
3404
0
                                    "0x%X\n", cur);
3405
0
                }
3406
0
    NEXTL(l);
3407
0
    cur = CUR_CHAR(l);
3408
0
    if (cur == 0) {
3409
0
        SHRINK;
3410
0
        GROW;
3411
0
        cur = CUR_CHAR(l);
3412
0
    }
3413
0
      }
3414
0
      buf[len] = 0;
3415
0
      if (cur != '>') {
3416
0
    htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
3417
0
          "ParsePI: PI %s never end ...\n", target, NULL);
3418
0
      } else {
3419
0
    SKIP(1);
3420
3421
    /*
3422
     * SAX: PI detected.
3423
     */
3424
0
    if ((ctxt->sax) && (!ctxt->disableSAX) &&
3425
0
        (ctxt->sax->processingInstruction != NULL))
3426
0
        ctxt->sax->processingInstruction(ctxt->userData,
3427
0
                                         target, buf);
3428
0
      }
3429
0
      xmlFree(buf);
3430
0
  } else {
3431
0
      htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
3432
0
                         "PI is not started correctly", NULL, NULL);
3433
0
  }
3434
0
  ctxt->instate = state;
3435
0
    }
3436
0
}
3437
3438
/**
3439
 * htmlParseComment:
3440
 * @ctxt:  an HTML parser context
3441
 *
3442
 * Parse an XML (SGML) comment <!-- .... -->
3443
 *
3444
 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3445
 */
3446
static void
3447
0
htmlParseComment(htmlParserCtxtPtr ctxt) {
3448
0
    xmlChar *buf = NULL;
3449
0
    int len;
3450
0
    int size = HTML_PARSER_BUFFER_SIZE;
3451
0
    int q, ql;
3452
0
    int r, rl;
3453
0
    int cur, l;
3454
0
    int next, nl;
3455
0
    xmlParserInputState state;
3456
3457
    /*
3458
     * Check that there is a comment right here.
3459
     */
3460
0
    if ((RAW != '<') || (NXT(1) != '!') ||
3461
0
        (NXT(2) != '-') || (NXT(3) != '-')) return;
3462
3463
0
    state = ctxt->instate;
3464
0
    ctxt->instate = XML_PARSER_COMMENT;
3465
0
    SHRINK;
3466
0
    SKIP(4);
3467
0
    buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
3468
0
    if (buf == NULL) {
3469
0
        htmlErrMemory(ctxt, "buffer allocation failed\n");
3470
0
  ctxt->instate = state;
3471
0
  return;
3472
0
    }
3473
0
    len = 0;
3474
0
    buf[len] = 0;
3475
0
    q = CUR_CHAR(ql);
3476
0
    if (q == 0)
3477
0
        goto unfinished;
3478
0
    if (q == '>') {
3479
0
        htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL);
3480
0
        cur = '>';
3481
0
        goto finished;
3482
0
    }
3483
0
    NEXTL(ql);
3484
0
    r = CUR_CHAR(rl);
3485
0
    if (r == 0)
3486
0
        goto unfinished;
3487
0
    if (q == '-' && r == '>') {
3488
0
        htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL);
3489
0
        cur = '>';
3490
0
        goto finished;
3491
0
    }
3492
0
    NEXTL(rl);
3493
0
    cur = CUR_CHAR(l);
3494
0
    while ((cur != 0) &&
3495
0
           ((cur != '>') ||
3496
0
      (r != '-') || (q != '-'))) {
3497
0
  NEXTL(l);
3498
0
  next = CUR_CHAR(nl);
3499
0
  if (next == 0) {
3500
0
      SHRINK;
3501
0
      GROW;
3502
0
      next = CUR_CHAR(nl);
3503
0
  }
3504
3505
0
  if ((q == '-') && (r == '-') && (cur == '!') && (next == '>')) {
3506
0
    htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3507
0
           "Comment incorrectly closed by '--!>'", NULL, NULL);
3508
0
    cur = '>';
3509
0
    break;
3510
0
  }
3511
3512
0
  if (len + 5 >= size) {
3513
0
      xmlChar *tmp;
3514
3515
0
      size *= 2;
3516
0
      tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3517
0
      if (tmp == NULL) {
3518
0
          xmlFree(buf);
3519
0
          htmlErrMemory(ctxt, "growing buffer failed\n");
3520
0
    ctxt->instate = state;
3521
0
    return;
3522
0
      }
3523
0
      buf = tmp;
3524
0
  }
3525
0
        if (IS_CHAR(q)) {
3526
0
      COPY_BUF(ql,buf,len,q);
3527
0
        } else {
3528
0
            htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3529
0
                            "Invalid char in comment 0x%X\n", q);
3530
0
        }
3531
3532
0
  q = r;
3533
0
  ql = rl;
3534
0
  r = cur;
3535
0
  rl = l;
3536
0
  cur = next;
3537
0
  l = nl;
3538
0
    }
3539
0
finished:
3540
0
    buf[len] = 0;
3541
0
    if (cur == '>') {
3542
0
        NEXT;
3543
0
  if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3544
0
      (!ctxt->disableSAX))
3545
0
      ctxt->sax->comment(ctxt->userData, buf);
3546
0
  xmlFree(buf);
3547
0
  ctxt->instate = state;
3548
0
  return;
3549
0
    }
3550
3551
0
unfinished:
3552
0
    htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3553
0
     "Comment not terminated \n<!--%.50s\n", buf, NULL);
3554
0
    xmlFree(buf);
3555
0
}
3556
3557
/**
3558
 * htmlParseCharRef:
3559
 * @ctxt:  an HTML parser context
3560
 *
3561
 * parse Reference declarations
3562
 *
3563
 * [66] CharRef ::= '&#' [0-9]+ ';' |
3564
 *                  '&#x' [0-9a-fA-F]+ ';'
3565
 *
3566
 * Returns the value parsed (as an int)
3567
 */
3568
int
3569
0
htmlParseCharRef(htmlParserCtxtPtr ctxt) {
3570
0
    int val = 0;
3571
3572
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
3573
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3574
0
         "htmlParseCharRef: context error\n",
3575
0
         NULL, NULL);
3576
0
        return(0);
3577
0
    }
3578
0
    if ((CUR == '&') && (NXT(1) == '#') &&
3579
0
        ((NXT(2) == 'x') || NXT(2) == 'X')) {
3580
0
  SKIP(3);
3581
0
  while (CUR != ';') {
3582
0
      if ((CUR >= '0') && (CUR <= '9')) {
3583
0
                if (val < 0x110000)
3584
0
              val = val * 16 + (CUR - '0');
3585
0
            } else if ((CUR >= 'a') && (CUR <= 'f')) {
3586
0
                if (val < 0x110000)
3587
0
              val = val * 16 + (CUR - 'a') + 10;
3588
0
            } else if ((CUR >= 'A') && (CUR <= 'F')) {
3589
0
                if (val < 0x110000)
3590
0
              val = val * 16 + (CUR - 'A') + 10;
3591
0
            } else {
3592
0
          htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
3593
0
                 "htmlParseCharRef: missing semicolon\n",
3594
0
           NULL, NULL);
3595
0
    break;
3596
0
      }
3597
0
      NEXT;
3598
0
  }
3599
0
  if (CUR == ';')
3600
0
      NEXT;
3601
0
    } else if  ((CUR == '&') && (NXT(1) == '#')) {
3602
0
  SKIP(2);
3603
0
  while (CUR != ';') {
3604
0
      if ((CUR >= '0') && (CUR <= '9')) {
3605
0
                if (val < 0x110000)
3606
0
              val = val * 10 + (CUR - '0');
3607
0
            } else {
3608
0
          htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
3609
0
                 "htmlParseCharRef: missing semicolon\n",
3610
0
           NULL, NULL);
3611
0
    break;
3612
0
      }
3613
0
      NEXT;
3614
0
  }
3615
0
  if (CUR == ';')
3616
0
      NEXT;
3617
0
    } else {
3618
0
  htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
3619
0
               "htmlParseCharRef: invalid value\n", NULL, NULL);
3620
0
    }
3621
    /*
3622
     * Check the value IS_CHAR ...
3623
     */
3624
0
    if (IS_CHAR(val)) {
3625
0
        return(val);
3626
0
    } else if (val >= 0x110000) {
3627
0
  htmlParseErr(ctxt, XML_ERR_INVALID_CHAR,
3628
0
         "htmlParseCharRef: value too large\n", NULL, NULL);
3629
0
    } else {
3630
0
  htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
3631
0
      "htmlParseCharRef: invalid xmlChar value %d\n",
3632
0
      val);
3633
0
    }
3634
0
    return(0);
3635
0
}
3636
3637
3638
/**
3639
 * htmlParseDocTypeDecl:
3640
 * @ctxt:  an HTML parser context
3641
 *
3642
 * parse a DOCTYPE declaration
3643
 *
3644
 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3645
 *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3646
 */
3647
3648
static void
3649
0
htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
3650
0
    const xmlChar *name;
3651
0
    xmlChar *ExternalID = NULL;
3652
0
    xmlChar *URI = NULL;
3653
3654
    /*
3655
     * We know that '<!DOCTYPE' has been detected.
3656
     */
3657
0
    SKIP(9);
3658
3659
0
    SKIP_BLANKS;
3660
3661
    /*
3662
     * Parse the DOCTYPE name.
3663
     */
3664
0
    name = htmlParseName(ctxt);
3665
0
    if (name == NULL) {
3666
0
  htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3667
0
               "htmlParseDocTypeDecl : no DOCTYPE name !\n",
3668
0
         NULL, NULL);
3669
0
    }
3670
    /*
3671
     * Check that upper(name) == "HTML" !!!!!!!!!!!!!
3672
     */
3673
3674
0
    SKIP_BLANKS;
3675
3676
    /*
3677
     * Check for SystemID and ExternalID
3678
     */
3679
0
    URI = htmlParseExternalID(ctxt, &ExternalID);
3680
0
    SKIP_BLANKS;
3681
3682
    /*
3683
     * We should be at the end of the DOCTYPE declaration.
3684
     */
3685
0
    if (CUR != '>') {
3686
0
  htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
3687
0
               "DOCTYPE improperly terminated\n", NULL, NULL);
3688
        /* Ignore bogus content */
3689
0
        while ((CUR != 0) && (CUR != '>'))
3690
0
            NEXT;
3691
0
    }
3692
0
    if (CUR == '>')
3693
0
        NEXT;
3694
3695
    /*
3696
     * Create or update the document accordingly to the DOCTYPE
3697
     */
3698
0
    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3699
0
  (!ctxt->disableSAX))
3700
0
  ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
3701
3702
    /*
3703
     * Cleanup, since we don't use all those identifiers
3704
     */
3705
0
    if (URI != NULL) xmlFree(URI);
3706
0
    if (ExternalID != NULL) xmlFree(ExternalID);
3707
0
}
3708
3709
/**
3710
 * htmlParseAttribute:
3711
 * @ctxt:  an HTML parser context
3712
 * @value:  a xmlChar ** used to store the value of the attribute
3713
 *
3714
 * parse an attribute
3715
 *
3716
 * [41] Attribute ::= Name Eq AttValue
3717
 *
3718
 * [25] Eq ::= S? '=' S?
3719
 *
3720
 * With namespace:
3721
 *
3722
 * [NS 11] Attribute ::= QName Eq AttValue
3723
 *
3724
 * Also the case QName == xmlns:??? is handled independently as a namespace
3725
 * definition.
3726
 *
3727
 * Returns the attribute name, and the value in *value.
3728
 */
3729
3730
static const xmlChar *
3731
0
htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
3732
0
    const xmlChar *name;
3733
0
    xmlChar *val = NULL;
3734
3735
0
    *value = NULL;
3736
0
    name = htmlParseHTMLName(ctxt);
3737
0
    if (name == NULL) {
3738
0
  htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3739
0
               "error parsing attribute name\n", NULL, NULL);
3740
0
        return(NULL);
3741
0
    }
3742
3743
    /*
3744
     * read the value
3745
     */
3746
0
    SKIP_BLANKS;
3747
0
    if (CUR == '=') {
3748
0
        NEXT;
3749
0
  SKIP_BLANKS;
3750
0
  val = htmlParseAttValue(ctxt);
3751
0
    }
3752
3753
0
    *value = val;
3754
0
    return(name);
3755
0
}
3756
3757
/**
3758
 * htmlCheckEncodingDirect:
3759
 * @ctxt:  an HTML parser context
3760
 * @attvalue: the attribute value
3761
 *
3762
 * Checks an attribute value to detect
3763
 * the encoding
3764
 * If a new encoding is detected the parser is switched to decode
3765
 * it and pass UTF8
3766
 */
3767
static void
3768
0
htmlCheckEncodingDirect(htmlParserCtxtPtr ctxt, const xmlChar *encoding) {
3769
3770
0
    if ((ctxt == NULL) || (encoding == NULL) ||
3771
0
        (ctxt->options & HTML_PARSE_IGNORE_ENC))
3772
0
  return;
3773
3774
    /* do not change encoding */
3775
0
    if (ctxt->input->encoding != NULL)
3776
0
        return;
3777
3778
0
    if (encoding != NULL) {
3779
0
  xmlCharEncoding enc;
3780
0
  xmlCharEncodingHandlerPtr handler;
3781
3782
0
  while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
3783
3784
0
  if (ctxt->input->encoding != NULL)
3785
0
      xmlFree((xmlChar *) ctxt->input->encoding);
3786
0
  ctxt->input->encoding = xmlStrdup(encoding);
3787
3788
0
  enc = xmlParseCharEncoding((const char *) encoding);
3789
  /*
3790
   * registered set of known encodings
3791
   */
3792
0
  if (enc != XML_CHAR_ENCODING_ERROR) {
3793
0
      if (((enc == XML_CHAR_ENCODING_UTF16LE) ||
3794
0
           (enc == XML_CHAR_ENCODING_UTF16BE) ||
3795
0
     (enc == XML_CHAR_ENCODING_UCS4LE) ||
3796
0
     (enc == XML_CHAR_ENCODING_UCS4BE)) &&
3797
0
    (ctxt->input->buf != NULL) &&
3798
0
    (ctxt->input->buf->encoder == NULL)) {
3799
0
    htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3800
0
                 "htmlCheckEncoding: wrong encoding meta\n",
3801
0
           NULL, NULL);
3802
0
      } else {
3803
0
    xmlSwitchEncoding(ctxt, enc);
3804
0
      }
3805
0
      ctxt->charset = XML_CHAR_ENCODING_UTF8;
3806
0
  } else {
3807
      /*
3808
       * fallback for unknown encodings
3809
       */
3810
0
      handler = xmlFindCharEncodingHandler((const char *) encoding);
3811
0
      if (handler != NULL) {
3812
0
    xmlSwitchToEncoding(ctxt, handler);
3813
0
    ctxt->charset = XML_CHAR_ENCODING_UTF8;
3814
0
      } else {
3815
0
    htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
3816
0
                 "htmlCheckEncoding: unknown encoding %s\n",
3817
0
           encoding, NULL);
3818
0
      }
3819
0
  }
3820
3821
0
  if ((ctxt->input->buf != NULL) &&
3822
0
      (ctxt->input->buf->encoder != NULL) &&
3823
0
      (ctxt->input->buf->raw != NULL) &&
3824
0
      (ctxt->input->buf->buffer != NULL)) {
3825
0
      int nbchars;
3826
0
      int processed;
3827
3828
      /*
3829
       * convert as much as possible to the parser reading buffer.
3830
       */
3831
0
      processed = ctxt->input->cur - ctxt->input->base;
3832
0
      xmlBufShrink(ctxt->input->buf->buffer, processed);
3833
0
      nbchars = xmlCharEncInput(ctxt->input->buf, 1);
3834
0
            xmlBufResetInput(ctxt->input->buf->buffer, ctxt->input);
3835
0
      if (nbchars < 0) {
3836
0
    htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
3837
0
                 "htmlCheckEncoding: encoder error\n",
3838
0
           NULL, NULL);
3839
0
      }
3840
0
  }
3841
0
    }
3842
0
}
3843
3844
/**
3845
 * htmlCheckEncoding:
3846
 * @ctxt:  an HTML parser context
3847
 * @attvalue: the attribute value
3848
 *
3849
 * Checks an http-equiv attribute from a Meta tag to detect
3850
 * the encoding
3851
 * If a new encoding is detected the parser is switched to decode
3852
 * it and pass UTF8
3853
 */
3854
static void
3855
0
htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
3856
0
    const xmlChar *encoding;
3857
3858
0
    if (!attvalue)
3859
0
  return;
3860
3861
0
    encoding = xmlStrcasestr(attvalue, BAD_CAST"charset");
3862
0
    if (encoding != NULL) {
3863
0
  encoding += 7;
3864
0
    }
3865
    /*
3866
     * skip blank
3867
     */
3868
0
    if (encoding && IS_BLANK_CH(*encoding))
3869
0
  encoding = xmlStrcasestr(attvalue, BAD_CAST"=");
3870
0
    if (encoding && *encoding == '=') {
3871
0
  encoding ++;
3872
0
  htmlCheckEncodingDirect(ctxt, encoding);
3873
0
    }
3874
0
}
3875
3876
/**
3877
 * htmlCheckMeta:
3878
 * @ctxt:  an HTML parser context
3879
 * @atts:  the attributes values
3880
 *
3881
 * Checks an attributes from a Meta tag
3882
 */
3883
static void
3884
0
htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
3885
0
    int i;
3886
0
    const xmlChar *att, *value;
3887
0
    int http = 0;
3888
0
    const xmlChar *content = NULL;
3889
3890
0
    if ((ctxt == NULL) || (atts == NULL))
3891
0
  return;
3892
3893
0
    i = 0;
3894
0
    att = atts[i++];
3895
0
    while (att != NULL) {
3896
0
  value = atts[i++];
3897
0
  if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
3898
0
   && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
3899
0
      http = 1;
3900
0
  else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"charset")))
3901
0
      htmlCheckEncodingDirect(ctxt, value);
3902
0
  else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
3903
0
      content = value;
3904
0
  att = atts[i++];
3905
0
    }
3906
0
    if ((http) && (content != NULL))
3907
0
  htmlCheckEncoding(ctxt, content);
3908
3909
0
}
3910
3911
/**
3912
 * htmlParseStartTag:
3913
 * @ctxt:  an HTML parser context
3914
 *
3915
 * parse a start of tag either for rule element or
3916
 * EmptyElement. In both case we don't parse the tag closing chars.
3917
 *
3918
 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3919
 *
3920
 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3921
 *
3922
 * With namespace:
3923
 *
3924
 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3925
 *
3926
 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3927
 *
3928
 * Returns 0 in case of success, -1 in case of error and 1 if discarded
3929
 */
3930
3931
static int
3932
0
htmlParseStartTag(htmlParserCtxtPtr ctxt) {
3933
0
    const xmlChar *name;
3934
0
    const xmlChar *attname;
3935
0
    xmlChar *attvalue;
3936
0
    const xmlChar **atts;
3937
0
    int nbatts = 0;
3938
0
    int maxatts;
3939
0
    int meta = 0;
3940
0
    int i;
3941
0
    int discardtag = 0;
3942
3943
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
3944
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
3945
0
         "htmlParseStartTag: context error\n", NULL, NULL);
3946
0
  return -1;
3947
0
    }
3948
0
    if (ctxt->instate == XML_PARSER_EOF)
3949
0
        return(-1);
3950
0
    if (CUR != '<') return -1;
3951
0
    NEXT;
3952
3953
0
    atts = ctxt->atts;
3954
0
    maxatts = ctxt->maxatts;
3955
3956
0
    GROW;
3957
0
    name = htmlParseHTMLName(ctxt);
3958
0
    if (name == NULL) {
3959
0
  htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
3960
0
               "htmlParseStartTag: invalid element name\n",
3961
0
         NULL, NULL);
3962
  /* Dump the bogus tag like browsers do */
3963
0
  while ((CUR != 0) && (CUR != '>') &&
3964
0
               (ctxt->instate != XML_PARSER_EOF))
3965
0
      NEXT;
3966
0
        return -1;
3967
0
    }
3968
0
    if (xmlStrEqual(name, BAD_CAST"meta"))
3969
0
  meta = 1;
3970
3971
    /*
3972
     * Check for auto-closure of HTML elements.
3973
     */
3974
0
    htmlAutoClose(ctxt, name);
3975
3976
    /*
3977
     * Check for implied HTML elements.
3978
     */
3979
0
    htmlCheckImplied(ctxt, name);
3980
3981
    /*
3982
     * Avoid html at any level > 0, head at any level != 1
3983
     * or any attempt to recurse body
3984
     */
3985
0
    if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
3986
0
  htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3987
0
               "htmlParseStartTag: misplaced <html> tag\n",
3988
0
         name, NULL);
3989
0
  discardtag = 1;
3990
0
  ctxt->depth++;
3991
0
    }
3992
0
    if ((ctxt->nameNr != 1) &&
3993
0
  (xmlStrEqual(name, BAD_CAST"head"))) {
3994
0
  htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
3995
0
               "htmlParseStartTag: misplaced <head> tag\n",
3996
0
         name, NULL);
3997
0
  discardtag = 1;
3998
0
  ctxt->depth++;
3999
0
    }
4000
0
    if (xmlStrEqual(name, BAD_CAST"body")) {
4001
0
  int indx;
4002
0
  for (indx = 0;indx < ctxt->nameNr;indx++) {
4003
0
      if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
4004
0
    htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4005
0
                 "htmlParseStartTag: misplaced <body> tag\n",
4006
0
           name, NULL);
4007
0
    discardtag = 1;
4008
0
    ctxt->depth++;
4009
0
      }
4010
0
  }
4011
0
    }
4012
4013
    /*
4014
     * Now parse the attributes, it ends up with the ending
4015
     *
4016
     * (S Attribute)* S?
4017
     */
4018
0
    SKIP_BLANKS;
4019
0
    while ((CUR != 0) &&
4020
0
           (CUR != '>') &&
4021
0
     ((CUR != '/') || (NXT(1) != '>'))) {
4022
0
  GROW;
4023
0
  attname = htmlParseAttribute(ctxt, &attvalue);
4024
0
        if (attname != NULL) {
4025
4026
      /*
4027
       * Well formedness requires at most one declaration of an attribute
4028
       */
4029
0
      for (i = 0; i < nbatts;i += 2) {
4030
0
          if (xmlStrEqual(atts[i], attname)) {
4031
0
        htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
4032
0
                     "Attribute %s redefined\n", attname, NULL);
4033
0
        if (attvalue != NULL)
4034
0
      xmlFree(attvalue);
4035
0
        goto failed;
4036
0
    }
4037
0
      }
4038
4039
      /*
4040
       * Add the pair to atts
4041
       */
4042
0
      if (atts == NULL) {
4043
0
          maxatts = 22; /* allow for 10 attrs by default */
4044
0
          atts = (const xmlChar **)
4045
0
           xmlMalloc(maxatts * sizeof(xmlChar *));
4046
0
    if (atts == NULL) {
4047
0
        htmlErrMemory(ctxt, NULL);
4048
0
        if (attvalue != NULL)
4049
0
      xmlFree(attvalue);
4050
0
        goto failed;
4051
0
    }
4052
0
    ctxt->atts = atts;
4053
0
    ctxt->maxatts = maxatts;
4054
0
      } else if (nbatts + 4 > maxatts) {
4055
0
          const xmlChar **n;
4056
4057
0
          maxatts *= 2;
4058
0
          n = (const xmlChar **) xmlRealloc((void *) atts,
4059
0
               maxatts * sizeof(const xmlChar *));
4060
0
    if (n == NULL) {
4061
0
        htmlErrMemory(ctxt, NULL);
4062
0
        if (attvalue != NULL)
4063
0
      xmlFree(attvalue);
4064
0
        goto failed;
4065
0
    }
4066
0
    atts = n;
4067
0
    ctxt->atts = atts;
4068
0
    ctxt->maxatts = maxatts;
4069
0
      }
4070
0
      atts[nbatts++] = attname;
4071
0
      atts[nbatts++] = attvalue;
4072
0
      atts[nbatts] = NULL;
4073
0
      atts[nbatts + 1] = NULL;
4074
0
  }
4075
0
  else {
4076
0
      if (attvalue != NULL)
4077
0
          xmlFree(attvalue);
4078
      /* Dump the bogus attribute string up to the next blank or
4079
       * the end of the tag. */
4080
0
      while ((CUR != 0) &&
4081
0
             !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
4082
0
       ((CUR != '/') || (NXT(1) != '>')))
4083
0
    NEXT;
4084
0
  }
4085
4086
0
failed:
4087
0
  SKIP_BLANKS;
4088
0
    }
4089
4090
    /*
4091
     * Handle specific association to the META tag
4092
     */
4093
0
    if (meta && (nbatts != 0))
4094
0
  htmlCheckMeta(ctxt, atts);
4095
4096
    /*
4097
     * SAX: Start of Element !
4098
     */
4099
0
    if (!discardtag) {
4100
0
  htmlnamePush(ctxt, name);
4101
0
  if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
4102
0
      if (nbatts != 0)
4103
0
    ctxt->sax->startElement(ctxt->userData, name, atts);
4104
0
      else
4105
0
    ctxt->sax->startElement(ctxt->userData, name, NULL);
4106
0
  }
4107
0
    }
4108
4109
0
    if (atts != NULL) {
4110
0
        for (i = 1;i < nbatts;i += 2) {
4111
0
      if (atts[i] != NULL)
4112
0
    xmlFree((xmlChar *) atts[i]);
4113
0
  }
4114
0
    }
4115
4116
0
    return(discardtag);
4117
0
}
4118
4119
/**
4120
 * htmlParseEndTag:
4121
 * @ctxt:  an HTML parser context
4122
 *
4123
 * parse an end of tag
4124
 *
4125
 * [42] ETag ::= '</' Name S? '>'
4126
 *
4127
 * With namespace
4128
 *
4129
 * [NS 9] ETag ::= '</' QName S? '>'
4130
 *
4131
 * Returns 1 if the current level should be closed.
4132
 */
4133
4134
static int
4135
htmlParseEndTag(htmlParserCtxtPtr ctxt)
4136
0
{
4137
0
    const xmlChar *name;
4138
0
    const xmlChar *oldname;
4139
0
    int i, ret;
4140
4141
0
    if ((CUR != '<') || (NXT(1) != '/')) {
4142
0
        htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
4143
0
               "htmlParseEndTag: '</' not found\n", NULL, NULL);
4144
0
        return (0);
4145
0
    }
4146
0
    SKIP(2);
4147
4148
0
    name = htmlParseHTMLName(ctxt);
4149
0
    if (name == NULL)
4150
0
        return (0);
4151
    /*
4152
     * We should definitely be at the ending "S? '>'" part
4153
     */
4154
0
    SKIP_BLANKS;
4155
0
    if (CUR != '>') {
4156
0
        htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4157
0
               "End tag : expected '>'\n", NULL, NULL);
4158
        /* Skip to next '>' */
4159
0
        while ((CUR != 0) && (CUR != '>'))
4160
0
            NEXT;
4161
0
    }
4162
0
    if (CUR == '>')
4163
0
        NEXT;
4164
4165
    /*
4166
     * if we ignored misplaced tags in htmlParseStartTag don't pop them
4167
     * out now.
4168
     */
4169
0
    if ((ctxt->depth > 0) &&
4170
0
        (xmlStrEqual(name, BAD_CAST "html") ||
4171
0
         xmlStrEqual(name, BAD_CAST "body") ||
4172
0
   xmlStrEqual(name, BAD_CAST "head"))) {
4173
0
  ctxt->depth--;
4174
0
  return (0);
4175
0
    }
4176
4177
    /*
4178
     * If the name read is not one of the element in the parsing stack
4179
     * then return, it's just an error.
4180
     */
4181
0
    for (i = (ctxt->nameNr - 1); i >= 0; i--) {
4182
0
        if (xmlStrEqual(name, ctxt->nameTab[i]))
4183
0
            break;
4184
0
    }
4185
0
    if (i < 0) {
4186
0
        htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
4187
0
               "Unexpected end tag : %s\n", name, NULL);
4188
0
        return (0);
4189
0
    }
4190
4191
4192
    /*
4193
     * Check for auto-closure of HTML elements.
4194
     */
4195
4196
0
    htmlAutoCloseOnClose(ctxt, name);
4197
4198
    /*
4199
     * Well formedness constraints, opening and closing must match.
4200
     * With the exception that the autoclose may have popped stuff out
4201
     * of the stack.
4202
     */
4203
0
    if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
4204
0
        htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
4205
0
                     "Opening and ending tag mismatch: %s and %s\n",
4206
0
                     name, ctxt->name);
4207
0
    }
4208
4209
    /*
4210
     * SAX: End of Tag
4211
     */
4212
0
    oldname = ctxt->name;
4213
0
    if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
4214
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4215
0
            ctxt->sax->endElement(ctxt->userData, name);
4216
0
  htmlNodeInfoPop(ctxt);
4217
0
        htmlnamePop(ctxt);
4218
0
        ret = 1;
4219
0
    } else {
4220
0
        ret = 0;
4221
0
    }
4222
4223
0
    return (ret);
4224
0
}
4225
4226
4227
/**
4228
 * htmlParseReference:
4229
 * @ctxt:  an HTML parser context
4230
 *
4231
 * parse and handle entity references in content,
4232
 * this will end-up in a call to character() since this is either a
4233
 * CharRef, or a predefined entity.
4234
 */
4235
static void
4236
0
htmlParseReference(htmlParserCtxtPtr ctxt) {
4237
0
    const htmlEntityDesc * ent;
4238
0
    xmlChar out[6];
4239
0
    const xmlChar *name;
4240
0
    if (CUR != '&') return;
4241
4242
0
    if (NXT(1) == '#') {
4243
0
  unsigned int c;
4244
0
  int bits, i = 0;
4245
4246
0
  c = htmlParseCharRef(ctxt);
4247
0
  if (c == 0)
4248
0
      return;
4249
4250
0
        if      (c <    0x80) { out[i++]= c;                bits= -6; }
4251
0
        else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
4252
0
        else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
4253
0
        else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
4254
4255
0
        for ( ; bits >= 0; bits-= 6) {
4256
0
            out[i++]= ((c >> bits) & 0x3F) | 0x80;
4257
0
        }
4258
0
  out[i] = 0;
4259
4260
0
  htmlCheckParagraph(ctxt);
4261
0
  if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4262
0
      ctxt->sax->characters(ctxt->userData, out, i);
4263
0
    } else {
4264
0
  ent = htmlParseEntityRef(ctxt, &name);
4265
0
  if (name == NULL) {
4266
0
      htmlCheckParagraph(ctxt);
4267
0
      if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4268
0
          ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
4269
0
      return;
4270
0
  }
4271
0
  if ((ent == NULL) || !(ent->value > 0)) {
4272
0
      htmlCheckParagraph(ctxt);
4273
0
      if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
4274
0
    ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
4275
0
    ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
4276
    /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
4277
0
      }
4278
0
  } else {
4279
0
      unsigned int c;
4280
0
      int bits, i = 0;
4281
4282
0
      c = ent->value;
4283
0
      if      (c <    0x80)
4284
0
              { out[i++]= c;                bits= -6; }
4285
0
      else if (c <   0x800)
4286
0
              { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
4287
0
      else if (c < 0x10000)
4288
0
              { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
4289
0
      else
4290
0
              { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
4291
4292
0
      for ( ; bits >= 0; bits-= 6) {
4293
0
    out[i++]= ((c >> bits) & 0x3F) | 0x80;
4294
0
      }
4295
0
      out[i] = 0;
4296
4297
0
      htmlCheckParagraph(ctxt);
4298
0
      if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4299
0
    ctxt->sax->characters(ctxt->userData, out, i);
4300
0
  }
4301
0
    }
4302
0
}
4303
4304
/**
4305
 * htmlParseContent:
4306
 * @ctxt:  an HTML parser context
4307
 *
4308
 * Parse a content: comment, sub-element, reference or text.
4309
 * Kept for compatibility with old code
4310
 */
4311
4312
static void
4313
0
htmlParseContent(htmlParserCtxtPtr ctxt) {
4314
0
    xmlChar *currentNode;
4315
0
    int depth;
4316
0
    const xmlChar *name;
4317
4318
0
    currentNode = xmlStrdup(ctxt->name);
4319
0
    depth = ctxt->nameNr;
4320
0
    while (1) {
4321
0
        GROW;
4322
4323
0
        if (ctxt->instate == XML_PARSER_EOF)
4324
0
            break;
4325
4326
  /*
4327
   * Our tag or one of it's parent or children is ending.
4328
   */
4329
0
        if ((CUR == '<') && (NXT(1) == '/')) {
4330
0
      if (htmlParseEndTag(ctxt) &&
4331
0
    ((currentNode != NULL) || (ctxt->nameNr == 0))) {
4332
0
    if (currentNode != NULL)
4333
0
        xmlFree(currentNode);
4334
0
    return;
4335
0
      }
4336
0
      continue; /* while */
4337
0
        }
4338
4339
0
  else if ((CUR == '<') &&
4340
0
           ((IS_ASCII_LETTER(NXT(1))) ||
4341
0
      (NXT(1) == '_') || (NXT(1) == ':'))) {
4342
0
      name = htmlParseHTMLName_nonInvasive(ctxt);
4343
0
      if (name == NULL) {
4344
0
          htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
4345
0
       "htmlParseStartTag: invalid element name\n",
4346
0
       NULL, NULL);
4347
          /* Dump the bogus tag like browsers do */
4348
0
                while ((CUR != 0) && (CUR != '>'))
4349
0
              NEXT;
4350
4351
0
          if (currentNode != NULL)
4352
0
              xmlFree(currentNode);
4353
0
          return;
4354
0
      }
4355
4356
0
      if (ctxt->name != NULL) {
4357
0
          if (htmlCheckAutoClose(name, ctxt->name) == 1) {
4358
0
              htmlAutoClose(ctxt, name);
4359
0
              continue;
4360
0
          }
4361
0
      }
4362
0
  }
4363
4364
  /*
4365
   * Has this node been popped out during parsing of
4366
   * the next element
4367
   */
4368
0
        if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
4369
0
      (!xmlStrEqual(currentNode, ctxt->name)))
4370
0
       {
4371
0
      if (currentNode != NULL) xmlFree(currentNode);
4372
0
      return;
4373
0
  }
4374
4375
0
  if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
4376
0
      (xmlStrEqual(currentNode, BAD_CAST"style")))) {
4377
      /*
4378
       * Handle SCRIPT/STYLE separately
4379
       */
4380
0
      htmlParseScript(ctxt);
4381
0
  } else {
4382
      /*
4383
       * Sometimes DOCTYPE arrives in the middle of the document
4384
       */
4385
0
      if ((CUR == '<') && (NXT(1) == '!') &&
4386
0
    (UPP(2) == 'D') && (UPP(3) == 'O') &&
4387
0
    (UPP(4) == 'C') && (UPP(5) == 'T') &&
4388
0
    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4389
0
    (UPP(8) == 'E')) {
4390
0
    htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4391
0
                 "Misplaced DOCTYPE declaration\n",
4392
0
           BAD_CAST "DOCTYPE" , NULL);
4393
0
    htmlParseDocTypeDecl(ctxt);
4394
0
      }
4395
4396
      /*
4397
       * First case :  a comment
4398
       */
4399
0
      if ((CUR == '<') && (NXT(1) == '!') &&
4400
0
    (NXT(2) == '-') && (NXT(3) == '-')) {
4401
0
    htmlParseComment(ctxt);
4402
0
      }
4403
4404
      /*
4405
       * Second case : a Processing Instruction.
4406
       */
4407
0
      else if ((CUR == '<') && (NXT(1) == '?')) {
4408
0
    htmlParsePI(ctxt);
4409
0
      }
4410
4411
      /*
4412
       * Third case :  a sub-element.
4413
       */
4414
0
      else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) {
4415
0
    htmlParseElement(ctxt);
4416
0
      }
4417
0
      else if (CUR == '<') {
4418
0
                if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4419
0
                    (ctxt->sax->characters != NULL))
4420
0
                    ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1);
4421
0
                NEXT;
4422
0
      }
4423
4424
      /*
4425
       * Fourth case : a reference. If if has not been resolved,
4426
       *    parsing returns it's Name, create the node
4427
       */
4428
0
      else if (CUR == '&') {
4429
0
    htmlParseReference(ctxt);
4430
0
      }
4431
4432
      /*
4433
       * Fifth case : end of the resource
4434
       */
4435
0
      else if (CUR == 0) {
4436
0
    htmlAutoCloseOnEnd(ctxt);
4437
0
    break;
4438
0
      }
4439
4440
      /*
4441
       * Last case, text. Note that References are handled directly.
4442
       */
4443
0
      else {
4444
0
    htmlParseCharData(ctxt);
4445
0
      }
4446
0
  }
4447
0
        GROW;
4448
0
    }
4449
0
    if (currentNode != NULL) xmlFree(currentNode);
4450
0
}
4451
4452
/**
4453
 * htmlParseElement:
4454
 * @ctxt:  an HTML parser context
4455
 *
4456
 * parse an HTML element, this is highly recursive
4457
 * this is kept for compatibility with previous code versions
4458
 *
4459
 * [39] element ::= EmptyElemTag | STag content ETag
4460
 *
4461
 * [41] Attribute ::= Name Eq AttValue
4462
 */
4463
4464
void
4465
0
htmlParseElement(htmlParserCtxtPtr ctxt) {
4466
0
    const xmlChar *name;
4467
0
    xmlChar *currentNode = NULL;
4468
0
    const htmlElemDesc * info;
4469
0
    htmlParserNodeInfo node_info;
4470
0
    int failed;
4471
0
    int depth;
4472
0
    const xmlChar *oldptr;
4473
4474
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
4475
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4476
0
         "htmlParseElement: context error\n", NULL, NULL);
4477
0
  return;
4478
0
    }
4479
4480
0
    if (ctxt->instate == XML_PARSER_EOF)
4481
0
        return;
4482
4483
    /* Capture start position */
4484
0
    if (ctxt->record_info) {
4485
0
        node_info.begin_pos = ctxt->input->consumed +
4486
0
                          (CUR_PTR - ctxt->input->base);
4487
0
  node_info.begin_line = ctxt->input->line;
4488
0
    }
4489
4490
0
    failed = htmlParseStartTag(ctxt);
4491
0
    name = ctxt->name;
4492
0
    if ((failed == -1) || (name == NULL)) {
4493
0
  if (CUR == '>')
4494
0
      NEXT;
4495
0
        return;
4496
0
    }
4497
4498
    /*
4499
     * Lookup the info for that element.
4500
     */
4501
0
    info = htmlTagLookup(name);
4502
0
    if (info == NULL) {
4503
0
  htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4504
0
               "Tag %s invalid\n", name, NULL);
4505
0
    }
4506
4507
    /*
4508
     * Check for an Empty Element labeled the XML/SGML way
4509
     */
4510
0
    if ((CUR == '/') && (NXT(1) == '>')) {
4511
0
        SKIP(2);
4512
0
  if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4513
0
      ctxt->sax->endElement(ctxt->userData, name);
4514
0
  htmlnamePop(ctxt);
4515
0
  return;
4516
0
    }
4517
4518
0
    if (CUR == '>') {
4519
0
        NEXT;
4520
0
    } else {
4521
0
  htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4522
0
               "Couldn't find end of Start Tag %s\n", name, NULL);
4523
4524
  /*
4525
   * end of parsing of this node.
4526
   */
4527
0
  if (xmlStrEqual(name, ctxt->name)) {
4528
0
      nodePop(ctxt);
4529
0
      htmlnamePop(ctxt);
4530
0
  }
4531
4532
  /*
4533
   * Capture end position and add node
4534
   */
4535
0
  if (ctxt->record_info) {
4536
0
     node_info.end_pos = ctxt->input->consumed +
4537
0
            (CUR_PTR - ctxt->input->base);
4538
0
     node_info.end_line = ctxt->input->line;
4539
0
     node_info.node = ctxt->node;
4540
0
     xmlParserAddNodeInfo(ctxt, &node_info);
4541
0
  }
4542
0
  return;
4543
0
    }
4544
4545
    /*
4546
     * Check for an Empty Element from DTD definition
4547
     */
4548
0
    if ((info != NULL) && (info->empty)) {
4549
0
  if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4550
0
      ctxt->sax->endElement(ctxt->userData, name);
4551
0
  htmlnamePop(ctxt);
4552
0
  return;
4553
0
    }
4554
4555
    /*
4556
     * Parse the content of the element:
4557
     */
4558
0
    currentNode = xmlStrdup(ctxt->name);
4559
0
    depth = ctxt->nameNr;
4560
0
    while (CUR != 0) {
4561
0
  oldptr = ctxt->input->cur;
4562
0
  htmlParseContent(ctxt);
4563
0
  if (oldptr==ctxt->input->cur) break;
4564
0
  if (ctxt->nameNr < depth) break;
4565
0
    }
4566
4567
    /*
4568
     * Capture end position and add node
4569
     */
4570
0
    if ( currentNode != NULL && ctxt->record_info ) {
4571
0
       node_info.end_pos = ctxt->input->consumed +
4572
0
                          (CUR_PTR - ctxt->input->base);
4573
0
       node_info.end_line = ctxt->input->line;
4574
0
       node_info.node = ctxt->node;
4575
0
       xmlParserAddNodeInfo(ctxt, &node_info);
4576
0
    }
4577
0
    if (CUR == 0) {
4578
0
  htmlAutoCloseOnEnd(ctxt);
4579
0
    }
4580
4581
0
    if (currentNode != NULL)
4582
0
  xmlFree(currentNode);
4583
0
}
4584
4585
static void
4586
0
htmlParserFinishElementParsing(htmlParserCtxtPtr ctxt) {
4587
    /*
4588
     * Capture end position and add node
4589
     */
4590
0
    if ( ctxt->node != NULL && ctxt->record_info ) {
4591
0
       ctxt->nodeInfo->end_pos = ctxt->input->consumed +
4592
0
                                (CUR_PTR - ctxt->input->base);
4593
0
       ctxt->nodeInfo->end_line = ctxt->input->line;
4594
0
       ctxt->nodeInfo->node = ctxt->node;
4595
0
       xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
4596
0
       htmlNodeInfoPop(ctxt);
4597
0
    }
4598
0
    if (CUR == 0) {
4599
0
       htmlAutoCloseOnEnd(ctxt);
4600
0
    }
4601
0
}
4602
4603
/**
4604
 * htmlParseElementInternal:
4605
 * @ctxt:  an HTML parser context
4606
 *
4607
 * parse an HTML element, new version, non recursive
4608
 *
4609
 * [39] element ::= EmptyElemTag | STag content ETag
4610
 *
4611
 * [41] Attribute ::= Name Eq AttValue
4612
 */
4613
4614
static void
4615
0
htmlParseElementInternal(htmlParserCtxtPtr ctxt) {
4616
0
    const xmlChar *name;
4617
0
    const htmlElemDesc * info;
4618
0
    htmlParserNodeInfo node_info = { NULL, 0, 0, 0, 0 };
4619
0
    int failed;
4620
4621
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
4622
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4623
0
         "htmlParseElementInternal: context error\n", NULL, NULL);
4624
0
  return;
4625
0
    }
4626
4627
0
    if (ctxt->instate == XML_PARSER_EOF)
4628
0
        return;
4629
4630
    /* Capture start position */
4631
0
    if (ctxt->record_info) {
4632
0
        node_info.begin_pos = ctxt->input->consumed +
4633
0
                          (CUR_PTR - ctxt->input->base);
4634
0
  node_info.begin_line = ctxt->input->line;
4635
0
    }
4636
4637
0
    failed = htmlParseStartTag(ctxt);
4638
0
    name = ctxt->name;
4639
0
    if ((failed == -1) || (name == NULL)) {
4640
0
  if (CUR == '>')
4641
0
      NEXT;
4642
0
        return;
4643
0
    }
4644
4645
    /*
4646
     * Lookup the info for that element.
4647
     */
4648
0
    info = htmlTagLookup(name);
4649
0
    if (info == NULL) {
4650
0
  htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
4651
0
               "Tag %s invalid\n", name, NULL);
4652
0
    }
4653
4654
    /*
4655
     * Check for an Empty Element labeled the XML/SGML way
4656
     */
4657
0
    if ((CUR == '/') && (NXT(1) == '>')) {
4658
0
        SKIP(2);
4659
0
  if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4660
0
      ctxt->sax->endElement(ctxt->userData, name);
4661
0
  htmlnamePop(ctxt);
4662
0
  return;
4663
0
    }
4664
4665
0
    if (CUR == '>') {
4666
0
        NEXT;
4667
0
    } else {
4668
0
  htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
4669
0
               "Couldn't find end of Start Tag %s\n", name, NULL);
4670
4671
  /*
4672
   * end of parsing of this node.
4673
   */
4674
0
  if (xmlStrEqual(name, ctxt->name)) {
4675
0
      nodePop(ctxt);
4676
0
      htmlnamePop(ctxt);
4677
0
  }
4678
4679
0
        if (ctxt->record_info)
4680
0
            htmlNodeInfoPush(ctxt, &node_info);
4681
0
        htmlParserFinishElementParsing(ctxt);
4682
0
  return;
4683
0
    }
4684
4685
    /*
4686
     * Check for an Empty Element from DTD definition
4687
     */
4688
0
    if ((info != NULL) && (info->empty)) {
4689
0
  if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4690
0
      ctxt->sax->endElement(ctxt->userData, name);
4691
0
  htmlnamePop(ctxt);
4692
0
  return;
4693
0
    }
4694
4695
0
    if (ctxt->record_info)
4696
0
        htmlNodeInfoPush(ctxt, &node_info);
4697
0
}
4698
4699
/**
4700
 * htmlParseContentInternal:
4701
 * @ctxt:  an HTML parser context
4702
 *
4703
 * Parse a content: comment, sub-element, reference or text.
4704
 * New version for non recursive htmlParseElementInternal
4705
 */
4706
4707
static void
4708
0
htmlParseContentInternal(htmlParserCtxtPtr ctxt) {
4709
0
    xmlChar *currentNode;
4710
0
    int depth;
4711
0
    const xmlChar *name;
4712
4713
0
    currentNode = xmlStrdup(ctxt->name);
4714
0
    depth = ctxt->nameNr;
4715
0
    while (1) {
4716
0
        GROW;
4717
4718
0
        if (ctxt->instate == XML_PARSER_EOF)
4719
0
            break;
4720
4721
  /*
4722
   * Our tag or one of it's parent or children is ending.
4723
   */
4724
0
        if ((CUR == '<') && (NXT(1) == '/')) {
4725
0
      if (htmlParseEndTag(ctxt) &&
4726
0
    ((currentNode != NULL) || (ctxt->nameNr == 0))) {
4727
0
    if (currentNode != NULL)
4728
0
        xmlFree(currentNode);
4729
4730
0
          currentNode = xmlStrdup(ctxt->name);
4731
0
          depth = ctxt->nameNr;
4732
0
      }
4733
0
      continue; /* while */
4734
0
        }
4735
4736
0
  else if ((CUR == '<') &&
4737
0
           ((IS_ASCII_LETTER(NXT(1))) ||
4738
0
      (NXT(1) == '_') || (NXT(1) == ':'))) {
4739
0
      name = htmlParseHTMLName_nonInvasive(ctxt);
4740
0
      if (name == NULL) {
4741
0
          htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
4742
0
       "htmlParseStartTag: invalid element name\n",
4743
0
       NULL, NULL);
4744
          /* Dump the bogus tag like browsers do */
4745
0
          while ((CUR == 0) && (CUR != '>'))
4746
0
              NEXT;
4747
4748
0
          htmlParserFinishElementParsing(ctxt);
4749
0
          if (currentNode != NULL)
4750
0
              xmlFree(currentNode);
4751
4752
0
          currentNode = xmlStrdup(ctxt->name);
4753
0
          depth = ctxt->nameNr;
4754
0
          continue;
4755
0
      }
4756
4757
0
      if (ctxt->name != NULL) {
4758
0
          if (htmlCheckAutoClose(name, ctxt->name) == 1) {
4759
0
              htmlAutoClose(ctxt, name);
4760
0
              continue;
4761
0
          }
4762
0
      }
4763
0
  }
4764
4765
  /*
4766
   * Has this node been popped out during parsing of
4767
   * the next element
4768
   */
4769
0
        if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
4770
0
      (!xmlStrEqual(currentNode, ctxt->name)))
4771
0
       {
4772
0
      htmlParserFinishElementParsing(ctxt);
4773
0
      if (currentNode != NULL) xmlFree(currentNode);
4774
4775
0
      currentNode = xmlStrdup(ctxt->name);
4776
0
      depth = ctxt->nameNr;
4777
0
      continue;
4778
0
  }
4779
4780
0
  if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
4781
0
      (xmlStrEqual(currentNode, BAD_CAST"style")))) {
4782
      /*
4783
       * Handle SCRIPT/STYLE separately
4784
       */
4785
0
      htmlParseScript(ctxt);
4786
0
  } else {
4787
      /*
4788
       * Sometimes DOCTYPE arrives in the middle of the document
4789
       */
4790
0
      if ((CUR == '<') && (NXT(1) == '!') &&
4791
0
    (UPP(2) == 'D') && (UPP(3) == 'O') &&
4792
0
    (UPP(4) == 'C') && (UPP(5) == 'T') &&
4793
0
    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4794
0
    (UPP(8) == 'E')) {
4795
0
    htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
4796
0
                 "Misplaced DOCTYPE declaration\n",
4797
0
           BAD_CAST "DOCTYPE" , NULL);
4798
0
    htmlParseDocTypeDecl(ctxt);
4799
0
      }
4800
4801
      /*
4802
       * First case :  a comment
4803
       */
4804
0
      if ((CUR == '<') && (NXT(1) == '!') &&
4805
0
    (NXT(2) == '-') && (NXT(3) == '-')) {
4806
0
    htmlParseComment(ctxt);
4807
0
      }
4808
4809
      /*
4810
       * Second case : a Processing Instruction.
4811
       */
4812
0
      else if ((CUR == '<') && (NXT(1) == '?')) {
4813
0
    htmlParsePI(ctxt);
4814
0
      }
4815
4816
      /*
4817
       * Third case :  a sub-element.
4818
       */
4819
0
      else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) {
4820
0
    htmlParseElementInternal(ctxt);
4821
0
    if (currentNode != NULL) xmlFree(currentNode);
4822
4823
0
    currentNode = xmlStrdup(ctxt->name);
4824
0
    depth = ctxt->nameNr;
4825
0
      }
4826
0
      else if (CUR == '<') {
4827
0
                if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4828
0
                    (ctxt->sax->characters != NULL))
4829
0
                    ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1);
4830
0
                NEXT;
4831
0
            }
4832
4833
      /*
4834
       * Fourth case : a reference. If if has not been resolved,
4835
       *    parsing returns it's Name, create the node
4836
       */
4837
0
      else if (CUR == '&') {
4838
0
    htmlParseReference(ctxt);
4839
0
      }
4840
4841
      /*
4842
       * Fifth case : end of the resource
4843
       */
4844
0
      else if (CUR == 0) {
4845
0
    htmlAutoCloseOnEnd(ctxt);
4846
0
    break;
4847
0
      }
4848
4849
      /*
4850
       * Last case, text. Note that References are handled directly.
4851
       */
4852
0
      else {
4853
0
    htmlParseCharData(ctxt);
4854
0
      }
4855
0
  }
4856
0
        GROW;
4857
0
    }
4858
0
    if (currentNode != NULL) xmlFree(currentNode);
4859
0
}
4860
4861
/**
4862
 * htmlParseContent:
4863
 * @ctxt:  an HTML parser context
4864
 *
4865
 * Parse a content: comment, sub-element, reference or text.
4866
 * This is the entry point when called from parser.c
4867
 */
4868
4869
void
4870
0
__htmlParseContent(void *ctxt) {
4871
0
    if (ctxt != NULL)
4872
0
  htmlParseContentInternal((htmlParserCtxtPtr) ctxt);
4873
0
}
4874
4875
/**
4876
 * htmlParseDocument:
4877
 * @ctxt:  an HTML parser context
4878
 *
4879
 * parse an HTML document (and build a tree if using the standard SAX
4880
 * interface).
4881
 *
4882
 * Returns 0, -1 in case of error. the parser context is augmented
4883
 *                as a result of the parsing.
4884
 */
4885
4886
int
4887
0
htmlParseDocument(htmlParserCtxtPtr ctxt) {
4888
0
    xmlChar start[4];
4889
0
    xmlCharEncoding enc;
4890
0
    xmlDtdPtr dtd;
4891
4892
0
    xmlInitParser();
4893
4894
0
    htmlDefaultSAXHandlerInit();
4895
4896
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
4897
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
4898
0
         "htmlParseDocument: context error\n", NULL, NULL);
4899
0
  return(XML_ERR_INTERNAL_ERROR);
4900
0
    }
4901
0
    ctxt->html = 1;
4902
0
    ctxt->linenumbers = 1;
4903
0
    GROW;
4904
    /*
4905
     * SAX: beginning of the document processing.
4906
     */
4907
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4908
0
        ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4909
4910
0
    if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
4911
0
        ((ctxt->input->end - ctxt->input->cur) >= 4)) {
4912
  /*
4913
   * Get the 4 first bytes and decode the charset
4914
   * if enc != XML_CHAR_ENCODING_NONE
4915
   * plug some encoding conversion routines.
4916
   */
4917
0
  start[0] = RAW;
4918
0
  start[1] = NXT(1);
4919
0
  start[2] = NXT(2);
4920
0
  start[3] = NXT(3);
4921
0
  enc = xmlDetectCharEncoding(&start[0], 4);
4922
0
  if (enc != XML_CHAR_ENCODING_NONE) {
4923
0
      xmlSwitchEncoding(ctxt, enc);
4924
0
  }
4925
0
    }
4926
4927
    /*
4928
     * Wipe out everything which is before the first '<'
4929
     */
4930
0
    SKIP_BLANKS;
4931
0
    if (CUR == 0) {
4932
0
  htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
4933
0
               "Document is empty\n", NULL, NULL);
4934
0
    }
4935
4936
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4937
0
  ctxt->sax->startDocument(ctxt->userData);
4938
4939
4940
    /*
4941
     * Parse possible comments and PIs before any content
4942
     */
4943
0
    while (((CUR == '<') && (NXT(1) == '!') &&
4944
0
            (NXT(2) == '-') && (NXT(3) == '-')) ||
4945
0
     ((CUR == '<') && (NXT(1) == '?'))) {
4946
0
        htmlParseComment(ctxt);
4947
0
        htmlParsePI(ctxt);
4948
0
  SKIP_BLANKS;
4949
0
    }
4950
4951
4952
    /*
4953
     * Then possibly doc type declaration(s) and more Misc
4954
     * (doctypedecl Misc*)?
4955
     */
4956
0
    if ((CUR == '<') && (NXT(1) == '!') &&
4957
0
  (UPP(2) == 'D') && (UPP(3) == 'O') &&
4958
0
  (UPP(4) == 'C') && (UPP(5) == 'T') &&
4959
0
  (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4960
0
  (UPP(8) == 'E')) {
4961
0
  htmlParseDocTypeDecl(ctxt);
4962
0
    }
4963
0
    SKIP_BLANKS;
4964
4965
    /*
4966
     * Parse possible comments and PIs before any content
4967
     */
4968
0
    while (((CUR == '<') && (NXT(1) == '!') &&
4969
0
            (NXT(2) == '-') && (NXT(3) == '-')) ||
4970
0
     ((CUR == '<') && (NXT(1) == '?'))) {
4971
0
        htmlParseComment(ctxt);
4972
0
        htmlParsePI(ctxt);
4973
0
  SKIP_BLANKS;
4974
0
    }
4975
4976
    /*
4977
     * Time to start parsing the tree itself
4978
     */
4979
0
    htmlParseContentInternal(ctxt);
4980
4981
    /*
4982
     * autoclose
4983
     */
4984
0
    if (CUR == 0)
4985
0
  htmlAutoCloseOnEnd(ctxt);
4986
4987
4988
    /*
4989
     * SAX: end of the document processing.
4990
     */
4991
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4992
0
        ctxt->sax->endDocument(ctxt->userData);
4993
4994
0
    if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL)) {
4995
0
  dtd = xmlGetIntSubset(ctxt->myDoc);
4996
0
  if (dtd == NULL)
4997
0
      ctxt->myDoc->intSubset =
4998
0
    xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
4999
0
        BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
5000
0
        BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
5001
0
    }
5002
0
    if (! ctxt->wellFormed) return(-1);
5003
0
    return(0);
5004
0
}
5005
5006
5007
/************************************************************************
5008
 *                  *
5009
 *      Parser contexts handling      *
5010
 *                  *
5011
 ************************************************************************/
5012
5013
/**
5014
 * htmlInitParserCtxt:
5015
 * @ctxt:  an HTML parser context
5016
 *
5017
 * Initialize a parser context
5018
 *
5019
 * Returns 0 in case of success and -1 in case of error
5020
 */
5021
5022
static int
5023
htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
5024
0
{
5025
0
    htmlSAXHandler *sax;
5026
5027
0
    if (ctxt == NULL) return(-1);
5028
0
    memset(ctxt, 0, sizeof(htmlParserCtxt));
5029
5030
0
    ctxt->dict = xmlDictCreate();
5031
0
    if (ctxt->dict == NULL) {
5032
0
        htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
5033
0
  return(-1);
5034
0
    }
5035
0
    sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
5036
0
    if (sax == NULL) {
5037
0
        htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
5038
0
  return(-1);
5039
0
    }
5040
0
    else
5041
0
        memset(sax, 0, sizeof(htmlSAXHandler));
5042
5043
    /* Allocate the Input stack */
5044
0
    ctxt->inputTab = (htmlParserInputPtr *)
5045
0
                      xmlMalloc(5 * sizeof(htmlParserInputPtr));
5046
0
    if (ctxt->inputTab == NULL) {
5047
0
        htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
5048
0
  ctxt->inputNr = 0;
5049
0
  ctxt->inputMax = 0;
5050
0
  ctxt->input = NULL;
5051
0
  return(-1);
5052
0
    }
5053
0
    ctxt->inputNr = 0;
5054
0
    ctxt->inputMax = 5;
5055
0
    ctxt->input = NULL;
5056
0
    ctxt->version = NULL;
5057
0
    ctxt->encoding = NULL;
5058
0
    ctxt->standalone = -1;
5059
0
    ctxt->instate = XML_PARSER_START;
5060
5061
    /* Allocate the Node stack */
5062
0
    ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
5063
0
    if (ctxt->nodeTab == NULL) {
5064
0
        htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
5065
0
  ctxt->nodeNr = 0;
5066
0
  ctxt->nodeMax = 0;
5067
0
  ctxt->node = NULL;
5068
0
  ctxt->inputNr = 0;
5069
0
  ctxt->inputMax = 0;
5070
0
  ctxt->input = NULL;
5071
0
  return(-1);
5072
0
    }
5073
0
    ctxt->nodeNr = 0;
5074
0
    ctxt->nodeMax = 10;
5075
0
    ctxt->node = NULL;
5076
5077
    /* Allocate the Name stack */
5078
0
    ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
5079
0
    if (ctxt->nameTab == NULL) {
5080
0
        htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
5081
0
  ctxt->nameNr = 0;
5082
0
  ctxt->nameMax = 0;
5083
0
  ctxt->name = NULL;
5084
0
  ctxt->nodeNr = 0;
5085
0
  ctxt->nodeMax = 0;
5086
0
  ctxt->node = NULL;
5087
0
  ctxt->inputNr = 0;
5088
0
  ctxt->inputMax = 0;
5089
0
  ctxt->input = NULL;
5090
0
  return(-1);
5091
0
    }
5092
0
    ctxt->nameNr = 0;
5093
0
    ctxt->nameMax = 10;
5094
0
    ctxt->name = NULL;
5095
5096
0
    ctxt->nodeInfoTab = NULL;
5097
0
    ctxt->nodeInfoNr  = 0;
5098
0
    ctxt->nodeInfoMax = 0;
5099
5100
0
    if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler;
5101
0
    else {
5102
0
        ctxt->sax = sax;
5103
0
  memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
5104
0
    }
5105
0
    ctxt->userData = ctxt;
5106
0
    ctxt->myDoc = NULL;
5107
0
    ctxt->wellFormed = 1;
5108
0
    ctxt->replaceEntities = 0;
5109
0
    ctxt->linenumbers = xmlLineNumbersDefaultValue;
5110
0
    ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
5111
0
    ctxt->html = 1;
5112
0
    ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT;
5113
0
    ctxt->vctxt.userData = ctxt;
5114
0
    ctxt->vctxt.error = xmlParserValidityError;
5115
0
    ctxt->vctxt.warning = xmlParserValidityWarning;
5116
0
    ctxt->record_info = 0;
5117
0
    ctxt->validate = 0;
5118
0
    ctxt->checkIndex = 0;
5119
0
    ctxt->catalogs = NULL;
5120
0
    xmlInitNodeInfoSeq(&ctxt->node_seq);
5121
0
    return(0);
5122
0
}
5123
5124
/**
5125
 * htmlFreeParserCtxt:
5126
 * @ctxt:  an HTML parser context
5127
 *
5128
 * Free all the memory used by a parser context. However the parsed
5129
 * document in ctxt->myDoc is not freed.
5130
 */
5131
5132
void
5133
htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
5134
0
{
5135
0
    xmlFreeParserCtxt(ctxt);
5136
0
}
5137
5138
/**
5139
 * htmlNewParserCtxt:
5140
 *
5141
 * Allocate and initialize a new parser context.
5142
 *
5143
 * Returns the htmlParserCtxtPtr or NULL in case of allocation error
5144
 */
5145
5146
htmlParserCtxtPtr
5147
htmlNewParserCtxt(void)
5148
0
{
5149
0
    xmlParserCtxtPtr ctxt;
5150
5151
0
    ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
5152
0
    if (ctxt == NULL) {
5153
0
        htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
5154
0
  return(NULL);
5155
0
    }
5156
0
    memset(ctxt, 0, sizeof(xmlParserCtxt));
5157
0
    if (htmlInitParserCtxt(ctxt) < 0) {
5158
0
        htmlFreeParserCtxt(ctxt);
5159
0
  return(NULL);
5160
0
    }
5161
0
    return(ctxt);
5162
0
}
5163
5164
/**
5165
 * htmlCreateMemoryParserCtxt:
5166
 * @buffer:  a pointer to a char array
5167
 * @size:  the size of the array
5168
 *
5169
 * Create a parser context for an HTML in-memory document.
5170
 *
5171
 * Returns the new parser context or NULL
5172
 */
5173
htmlParserCtxtPtr
5174
0
htmlCreateMemoryParserCtxt(const char *buffer, int size) {
5175
0
    xmlParserCtxtPtr ctxt;
5176
0
    xmlParserInputPtr input;
5177
0
    xmlParserInputBufferPtr buf;
5178
5179
0
    if (buffer == NULL)
5180
0
  return(NULL);
5181
0
    if (size <= 0)
5182
0
  return(NULL);
5183
5184
0
    ctxt = htmlNewParserCtxt();
5185
0
    if (ctxt == NULL)
5186
0
  return(NULL);
5187
5188
0
    buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
5189
0
    if (buf == NULL) return(NULL);
5190
5191
0
    input = xmlNewInputStream(ctxt);
5192
0
    if (input == NULL) {
5193
0
  xmlFreeParserInputBuffer(buf);
5194
0
  xmlFreeParserCtxt(ctxt);
5195
0
  return(NULL);
5196
0
    }
5197
5198
0
    input->filename = NULL;
5199
0
    input->buf = buf;
5200
0
    xmlBufResetInput(buf->buffer, input);
5201
5202
0
    inputPush(ctxt, input);
5203
0
    return(ctxt);
5204
0
}
5205
5206
/**
5207
 * htmlCreateDocParserCtxt:
5208
 * @cur:  a pointer to an array of xmlChar
5209
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
5210
 *
5211
 * Create a parser context for an HTML document.
5212
 *
5213
 * TODO: check the need to add encoding handling there
5214
 *
5215
 * Returns the new parser context or NULL
5216
 */
5217
static htmlParserCtxtPtr
5218
0
htmlCreateDocParserCtxt(const xmlChar *cur, const char *encoding) {
5219
0
    int len;
5220
0
    htmlParserCtxtPtr ctxt;
5221
5222
0
    if (cur == NULL)
5223
0
  return(NULL);
5224
0
    len = xmlStrlen(cur);
5225
0
    ctxt = htmlCreateMemoryParserCtxt((char *)cur, len);
5226
0
    if (ctxt == NULL)
5227
0
  return(NULL);
5228
5229
0
    if (encoding != NULL) {
5230
0
  xmlCharEncoding enc;
5231
0
  xmlCharEncodingHandlerPtr handler;
5232
5233
0
  if (ctxt->input->encoding != NULL)
5234
0
      xmlFree((xmlChar *) ctxt->input->encoding);
5235
0
  ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding);
5236
5237
0
  enc = xmlParseCharEncoding(encoding);
5238
  /*
5239
   * registered set of known encodings
5240
   */
5241
0
  if (enc != XML_CHAR_ENCODING_ERROR) {
5242
0
      xmlSwitchEncoding(ctxt, enc);
5243
0
      if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5244
0
    htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
5245
0
                 "Unsupported encoding %s\n",
5246
0
           (const xmlChar *) encoding, NULL);
5247
0
      }
5248
0
  } else {
5249
      /*
5250
       * fallback for unknown encodings
5251
       */
5252
0
      handler = xmlFindCharEncodingHandler((const char *) encoding);
5253
0
      if (handler != NULL) {
5254
0
    xmlSwitchToEncoding(ctxt, handler);
5255
0
      } else {
5256
0
    htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
5257
0
                 "Unsupported encoding %s\n",
5258
0
           (const xmlChar *) encoding, NULL);
5259
0
      }
5260
0
  }
5261
0
    }
5262
0
    return(ctxt);
5263
0
}
5264
5265
#ifdef LIBXML_PUSH_ENABLED
5266
/************************************************************************
5267
 *                  *
5268
 *  Progressive parsing interfaces        *
5269
 *                  *
5270
 ************************************************************************/
5271
5272
/**
5273
 * htmlParseLookupSequence:
5274
 * @ctxt:  an HTML parser context
5275
 * @first:  the first char to lookup
5276
 * @next:  the next char to lookup or zero
5277
 * @third:  the next char to lookup or zero
5278
 * @ignoreattrval: skip over attribute values
5279
 *
5280
 * Try to find if a sequence (first, next, third) or  just (first next) or
5281
 * (first) is available in the input stream.
5282
 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5283
 * to avoid rescanning sequences of bytes, it DOES change the state of the
5284
 * parser, do not use liberally.
5285
 * This is basically similar to xmlParseLookupSequence()
5286
 *
5287
 * Returns the index to the current parsing point if the full sequence
5288
 *      is available, -1 otherwise.
5289
 */
5290
static int
5291
htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
5292
                        xmlChar next, xmlChar third, int ignoreattrval)
5293
0
{
5294
0
    int base, len;
5295
0
    htmlParserInputPtr in;
5296
0
    const xmlChar *buf;
5297
0
    int invalue = 0;
5298
0
    char valdellim = 0x0;
5299
5300
0
    in = ctxt->input;
5301
0
    if (in == NULL)
5302
0
        return (-1);
5303
5304
0
    base = in->cur - in->base;
5305
0
    if (base < 0)
5306
0
        return (-1);
5307
5308
0
    if (ctxt->checkIndex > base) {
5309
0
        base = ctxt->checkIndex;
5310
        /* Abuse hasPErefs member to restore current state. */
5311
0
        invalue = ctxt->hasPErefs & 1 ? 1 : 0;
5312
0
    }
5313
5314
0
    if (in->buf == NULL) {
5315
0
        buf = in->base;
5316
0
        len = in->length;
5317
0
    } else {
5318
0
        buf = xmlBufContent(in->buf->buffer);
5319
0
        len = xmlBufUse(in->buf->buffer);
5320
0
    }
5321
5322
    /* take into account the sequence length */
5323
0
    if (third)
5324
0
        len -= 2;
5325
0
    else if (next)
5326
0
        len--;
5327
0
    for (; base < len; base++) {
5328
0
        if (ignoreattrval) {
5329
0
            if (buf[base] == '"' || buf[base] == '\'') {
5330
0
                if (invalue) {
5331
0
                    if (buf[base] == valdellim) {
5332
0
                        invalue = 0;
5333
0
                        continue;
5334
0
                    }
5335
0
                } else {
5336
0
                    valdellim = buf[base];
5337
0
                    invalue = 1;
5338
0
                    continue;
5339
0
                }
5340
0
            } else if (invalue) {
5341
0
                continue;
5342
0
            }
5343
0
        }
5344
0
        if (buf[base] == first) {
5345
0
            if (third != 0) {
5346
0
                if ((buf[base + 1] != next) || (buf[base + 2] != third))
5347
0
                    continue;
5348
0
            } else if (next != 0) {
5349
0
                if (buf[base + 1] != next)
5350
0
                    continue;
5351
0
            }
5352
0
            ctxt->checkIndex = 0;
5353
#ifdef DEBUG_PUSH
5354
            if (next == 0)
5355
                xmlGenericError(xmlGenericErrorContext,
5356
                                "HPP: lookup '%c' found at %d\n",
5357
                                first, base);
5358
            else if (third == 0)
5359
                xmlGenericError(xmlGenericErrorContext,
5360
                                "HPP: lookup '%c%c' found at %d\n",
5361
                                first, next, base);
5362
            else
5363
                xmlGenericError(xmlGenericErrorContext,
5364
                                "HPP: lookup '%c%c%c' found at %d\n",
5365
                                first, next, third, base);
5366
#endif
5367
0
            return (base - (in->cur - in->base));
5368
0
        }
5369
0
    }
5370
0
    ctxt->checkIndex = base;
5371
    /* Abuse hasPErefs member to track current state. */
5372
0
    if (invalue)
5373
0
        ctxt->hasPErefs |= 1;
5374
0
    else
5375
0
        ctxt->hasPErefs &= ~1;
5376
#ifdef DEBUG_PUSH
5377
    if (next == 0)
5378
        xmlGenericError(xmlGenericErrorContext,
5379
                        "HPP: lookup '%c' failed\n", first);
5380
    else if (third == 0)
5381
        xmlGenericError(xmlGenericErrorContext,
5382
                        "HPP: lookup '%c%c' failed\n", first, next);
5383
    else
5384
        xmlGenericError(xmlGenericErrorContext,
5385
                        "HPP: lookup '%c%c%c' failed\n", first, next,
5386
                        third);
5387
#endif
5388
0
    return (-1);
5389
0
}
5390
5391
/**
5392
 * htmlParseLookupCommentEnd:
5393
 * @ctxt: an HTML parser context
5394
 *
5395
 * Try to find a comment end tag in the input stream
5396
 * The search includes "-->" as well as WHATWG-recommended incorrectly-closed tags.
5397
 * (See https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment)
5398
 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5399
 * to avoid rescanning sequences of bytes, it DOES change the state of the
5400
 * parser, do not use liberally.
5401
 * This wraps to htmlParseLookupSequence()
5402
 *
5403
 * Returns the index to the current parsing point if the full sequence is available, -1 otherwise.
5404
 */
5405
static int
5406
htmlParseLookupCommentEnd(htmlParserCtxtPtr ctxt)
5407
0
{
5408
0
    int mark = 0;
5409
0
    int cur = CUR_PTR - BASE_PTR;
5410
5411
0
    while (mark >= 0) {
5412
0
  mark = htmlParseLookupSequence(ctxt, '-', '-', 0, 0);
5413
0
  if ((mark < 0) ||
5414
0
      (NXT(mark+2) == '>') ||
5415
0
      ((NXT(mark+2) == '!') && (NXT(mark+3) == '>'))) {
5416
0
      return mark;
5417
0
  }
5418
0
  ctxt->checkIndex = cur + mark + 1;
5419
0
    }
5420
0
    return mark;
5421
0
}
5422
5423
5424
/**
5425
 * htmlParseTryOrFinish:
5426
 * @ctxt:  an HTML parser context
5427
 * @terminate:  last chunk indicator
5428
 *
5429
 * Try to progress on parsing
5430
 *
5431
 * Returns zero if no parsing was possible
5432
 */
5433
static int
5434
0
htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
5435
0
    int ret = 0;
5436
0
    htmlParserInputPtr in;
5437
0
    ptrdiff_t avail = 0;
5438
0
    xmlChar cur, next;
5439
5440
0
    htmlParserNodeInfo node_info;
5441
5442
#ifdef DEBUG_PUSH
5443
    switch (ctxt->instate) {
5444
  case XML_PARSER_EOF:
5445
      xmlGenericError(xmlGenericErrorContext,
5446
        "HPP: try EOF\n"); break;
5447
  case XML_PARSER_START:
5448
      xmlGenericError(xmlGenericErrorContext,
5449
        "HPP: try START\n"); break;
5450
  case XML_PARSER_MISC:
5451
      xmlGenericError(xmlGenericErrorContext,
5452
        "HPP: try MISC\n");break;
5453
  case XML_PARSER_COMMENT:
5454
      xmlGenericError(xmlGenericErrorContext,
5455
        "HPP: try COMMENT\n");break;
5456
  case XML_PARSER_PROLOG:
5457
      xmlGenericError(xmlGenericErrorContext,
5458
        "HPP: try PROLOG\n");break;
5459
  case XML_PARSER_START_TAG:
5460
      xmlGenericError(xmlGenericErrorContext,
5461
        "HPP: try START_TAG\n");break;
5462
  case XML_PARSER_CONTENT:
5463
      xmlGenericError(xmlGenericErrorContext,
5464
        "HPP: try CONTENT\n");break;
5465
  case XML_PARSER_CDATA_SECTION:
5466
      xmlGenericError(xmlGenericErrorContext,
5467
        "HPP: try CDATA_SECTION\n");break;
5468
  case XML_PARSER_END_TAG:
5469
      xmlGenericError(xmlGenericErrorContext,
5470
        "HPP: try END_TAG\n");break;
5471
  case XML_PARSER_ENTITY_DECL:
5472
      xmlGenericError(xmlGenericErrorContext,
5473
        "HPP: try ENTITY_DECL\n");break;
5474
  case XML_PARSER_ENTITY_VALUE:
5475
      xmlGenericError(xmlGenericErrorContext,
5476
        "HPP: try ENTITY_VALUE\n");break;
5477
  case XML_PARSER_ATTRIBUTE_VALUE:
5478
      xmlGenericError(xmlGenericErrorContext,
5479
        "HPP: try ATTRIBUTE_VALUE\n");break;
5480
  case XML_PARSER_DTD:
5481
      xmlGenericError(xmlGenericErrorContext,
5482
        "HPP: try DTD\n");break;
5483
  case XML_PARSER_EPILOG:
5484
      xmlGenericError(xmlGenericErrorContext,
5485
        "HPP: try EPILOG\n");break;
5486
  case XML_PARSER_PI:
5487
      xmlGenericError(xmlGenericErrorContext,
5488
        "HPP: try PI\n");break;
5489
  case XML_PARSER_SYSTEM_LITERAL:
5490
      xmlGenericError(xmlGenericErrorContext,
5491
        "HPP: try SYSTEM_LITERAL\n");break;
5492
    }
5493
#endif
5494
5495
0
    while (1) {
5496
5497
0
  in = ctxt->input;
5498
0
  if (in == NULL) break;
5499
0
  if (in->buf == NULL)
5500
0
      avail = in->length - (in->cur - in->base);
5501
0
  else
5502
0
      avail = (ptrdiff_t)xmlBufUse(in->buf->buffer) -
5503
0
                    (in->cur - in->base);
5504
0
  if ((avail == 0) && (terminate)) {
5505
0
      htmlAutoCloseOnEnd(ctxt);
5506
0
      if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5507
    /*
5508
     * SAX: end of the document processing.
5509
     */
5510
0
    ctxt->instate = XML_PARSER_EOF;
5511
0
    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5512
0
        ctxt->sax->endDocument(ctxt->userData);
5513
0
      }
5514
0
  }
5515
0
        if (avail < 1)
5516
0
      goto done;
5517
        /*
5518
         * This is done to make progress and avoid an infinite loop
5519
         * if a parsing attempt was aborted by hitting a NUL byte. After
5520
         * changing htmlCurrentChar, this probably isn't necessary anymore.
5521
         * We should consider removing this check.
5522
         */
5523
0
  cur = in->cur[0];
5524
0
  if (cur == 0) {
5525
0
      SKIP(1);
5526
0
      continue;
5527
0
  }
5528
5529
0
        switch (ctxt->instate) {
5530
0
            case XML_PARSER_EOF:
5531
          /*
5532
     * Document parsing is done !
5533
     */
5534
0
          goto done;
5535
0
            case XML_PARSER_START:
5536
          /*
5537
     * Very first chars read from the document flow.
5538
     */
5539
0
    cur = in->cur[0];
5540
0
    if (IS_BLANK_CH(cur)) {
5541
0
        SKIP_BLANKS;
5542
0
        if (in->buf == NULL)
5543
0
      avail = in->length - (in->cur - in->base);
5544
0
        else
5545
0
      avail = (ptrdiff_t)xmlBufUse(in->buf->buffer) -
5546
0
                                (in->cur - in->base);
5547
0
    }
5548
0
    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
5549
0
        ctxt->sax->setDocumentLocator(ctxt->userData,
5550
0
              &xmlDefaultSAXLocator);
5551
0
    if ((ctxt->sax) && (ctxt->sax->startDocument) &&
5552
0
              (!ctxt->disableSAX))
5553
0
        ctxt->sax->startDocument(ctxt->userData);
5554
5555
0
    cur = in->cur[0];
5556
0
    next = in->cur[1];
5557
0
    if ((cur == '<') && (next == '!') &&
5558
0
        (UPP(2) == 'D') && (UPP(3) == 'O') &&
5559
0
        (UPP(4) == 'C') && (UPP(5) == 'T') &&
5560
0
        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5561
0
        (UPP(8) == 'E')) {
5562
0
        if ((!terminate) &&
5563
0
            (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
5564
0
      goto done;
5565
#ifdef DEBUG_PUSH
5566
        xmlGenericError(xmlGenericErrorContext,
5567
          "HPP: Parsing internal subset\n");
5568
#endif
5569
0
        htmlParseDocTypeDecl(ctxt);
5570
0
        ctxt->instate = XML_PARSER_PROLOG;
5571
#ifdef DEBUG_PUSH
5572
        xmlGenericError(xmlGenericErrorContext,
5573
          "HPP: entering PROLOG\n");
5574
#endif
5575
0
                } else {
5576
0
        ctxt->instate = XML_PARSER_MISC;
5577
#ifdef DEBUG_PUSH
5578
        xmlGenericError(xmlGenericErrorContext,
5579
          "HPP: entering MISC\n");
5580
#endif
5581
0
    }
5582
0
    break;
5583
0
            case XML_PARSER_MISC:
5584
0
    SKIP_BLANKS;
5585
0
    if (in->buf == NULL)
5586
0
        avail = in->length - (in->cur - in->base);
5587
0
    else
5588
0
        avail = (ptrdiff_t)xmlBufUse(in->buf->buffer) -
5589
0
                            (in->cur - in->base);
5590
    /*
5591
     * no chars in buffer
5592
     */
5593
0
    if (avail < 1)
5594
0
        goto done;
5595
    /*
5596
     * not enough chars in buffer
5597
     */
5598
0
    if (avail < 2) {
5599
0
        if (!terminate)
5600
0
      goto done;
5601
0
        else
5602
0
      next = ' ';
5603
0
    } else {
5604
0
        next = in->cur[1];
5605
0
    }
5606
0
    cur = in->cur[0];
5607
0
          if ((cur == '<') && (next == '!') &&
5608
0
        (in->cur[2] == '-') && (in->cur[3] == '-')) {
5609
0
        if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
5610
0
      goto done;
5611
#ifdef DEBUG_PUSH
5612
        xmlGenericError(xmlGenericErrorContext,
5613
          "HPP: Parsing Comment\n");
5614
#endif
5615
0
        htmlParseComment(ctxt);
5616
0
        ctxt->instate = XML_PARSER_MISC;
5617
0
          } else if ((cur == '<') && (next == '?')) {
5618
0
        if ((!terminate) &&
5619
0
            (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
5620
0
      goto done;
5621
#ifdef DEBUG_PUSH
5622
        xmlGenericError(xmlGenericErrorContext,
5623
          "HPP: Parsing PI\n");
5624
#endif
5625
0
        htmlParsePI(ctxt);
5626
0
        ctxt->instate = XML_PARSER_MISC;
5627
0
    } else if ((cur == '<') && (next == '!') &&
5628
0
        (UPP(2) == 'D') && (UPP(3) == 'O') &&
5629
0
        (UPP(4) == 'C') && (UPP(5) == 'T') &&
5630
0
        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5631
0
        (UPP(8) == 'E')) {
5632
0
        if ((!terminate) &&
5633
0
            (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
5634
0
      goto done;
5635
#ifdef DEBUG_PUSH
5636
        xmlGenericError(xmlGenericErrorContext,
5637
          "HPP: Parsing internal subset\n");
5638
#endif
5639
0
        htmlParseDocTypeDecl(ctxt);
5640
0
        ctxt->instate = XML_PARSER_PROLOG;
5641
#ifdef DEBUG_PUSH
5642
        xmlGenericError(xmlGenericErrorContext,
5643
          "HPP: entering PROLOG\n");
5644
#endif
5645
0
    } else if ((cur == '<') && (next == '!') &&
5646
0
               (avail < 9)) {
5647
0
        goto done;
5648
0
    } else {
5649
0
        ctxt->instate = XML_PARSER_CONTENT;
5650
#ifdef DEBUG_PUSH
5651
        xmlGenericError(xmlGenericErrorContext,
5652
          "HPP: entering START_TAG\n");
5653
#endif
5654
0
    }
5655
0
    break;
5656
0
            case XML_PARSER_PROLOG:
5657
0
    SKIP_BLANKS;
5658
0
    if (in->buf == NULL)
5659
0
        avail = in->length - (in->cur - in->base);
5660
0
    else
5661
0
        avail = (ptrdiff_t)xmlBufUse(in->buf->buffer) -
5662
0
                            (in->cur - in->base);
5663
0
    if (avail < 2)
5664
0
        goto done;
5665
0
    cur = in->cur[0];
5666
0
    next = in->cur[1];
5667
0
    if ((cur == '<') && (next == '!') &&
5668
0
        (in->cur[2] == '-') && (in->cur[3] == '-')) {
5669
0
        if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
5670
0
      goto done;
5671
#ifdef DEBUG_PUSH
5672
        xmlGenericError(xmlGenericErrorContext,
5673
          "HPP: Parsing Comment\n");
5674
#endif
5675
0
        htmlParseComment(ctxt);
5676
0
        ctxt->instate = XML_PARSER_PROLOG;
5677
0
          } else if ((cur == '<') && (next == '?')) {
5678
0
        if ((!terminate) &&
5679
0
            (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
5680
0
      goto done;
5681
#ifdef DEBUG_PUSH
5682
        xmlGenericError(xmlGenericErrorContext,
5683
          "HPP: Parsing PI\n");
5684
#endif
5685
0
        htmlParsePI(ctxt);
5686
0
        ctxt->instate = XML_PARSER_PROLOG;
5687
0
    } else if ((cur == '<') && (next == '!') &&
5688
0
               (avail < 4)) {
5689
0
        goto done;
5690
0
    } else {
5691
0
        ctxt->instate = XML_PARSER_CONTENT;
5692
#ifdef DEBUG_PUSH
5693
        xmlGenericError(xmlGenericErrorContext,
5694
          "HPP: entering START_TAG\n");
5695
#endif
5696
0
    }
5697
0
    break;
5698
0
            case XML_PARSER_EPILOG:
5699
0
    if (in->buf == NULL)
5700
0
        avail = in->length - (in->cur - in->base);
5701
0
    else
5702
0
        avail = (ptrdiff_t)xmlBufUse(in->buf->buffer) -
5703
0
                            (in->cur - in->base);
5704
0
    if (avail < 1)
5705
0
        goto done;
5706
0
    cur = in->cur[0];
5707
0
    if (IS_BLANK_CH(cur)) {
5708
0
        htmlParseCharData(ctxt);
5709
0
        goto done;
5710
0
    }
5711
0
    if (avail < 2)
5712
0
        goto done;
5713
0
    next = in->cur[1];
5714
0
          if ((cur == '<') && (next == '!') &&
5715
0
        (in->cur[2] == '-') && (in->cur[3] == '-')) {
5716
0
        if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
5717
0
      goto done;
5718
#ifdef DEBUG_PUSH
5719
        xmlGenericError(xmlGenericErrorContext,
5720
          "HPP: Parsing Comment\n");
5721
#endif
5722
0
        htmlParseComment(ctxt);
5723
0
        ctxt->instate = XML_PARSER_EPILOG;
5724
0
          } else if ((cur == '<') && (next == '?')) {
5725
0
        if ((!terminate) &&
5726
0
            (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
5727
0
      goto done;
5728
#ifdef DEBUG_PUSH
5729
        xmlGenericError(xmlGenericErrorContext,
5730
          "HPP: Parsing PI\n");
5731
#endif
5732
0
        htmlParsePI(ctxt);
5733
0
        ctxt->instate = XML_PARSER_EPILOG;
5734
0
    } else if ((cur == '<') && (next == '!') &&
5735
0
               (avail < 4)) {
5736
0
        goto done;
5737
0
    } else {
5738
0
        ctxt->errNo = XML_ERR_DOCUMENT_END;
5739
0
        ctxt->wellFormed = 0;
5740
0
        ctxt->instate = XML_PARSER_EOF;
5741
#ifdef DEBUG_PUSH
5742
        xmlGenericError(xmlGenericErrorContext,
5743
          "HPP: entering EOF\n");
5744
#endif
5745
0
        if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5746
0
      ctxt->sax->endDocument(ctxt->userData);
5747
0
        goto done;
5748
0
    }
5749
0
    break;
5750
0
            case XML_PARSER_START_TAG: {
5751
0
          const xmlChar *name;
5752
0
    int failed;
5753
0
    const htmlElemDesc * info;
5754
5755
    /*
5756
     * no chars in buffer
5757
     */
5758
0
    if (avail < 1)
5759
0
        goto done;
5760
    /*
5761
     * not enough chars in buffer
5762
     */
5763
0
    if (avail < 2) {
5764
0
        if (!terminate)
5765
0
      goto done;
5766
0
        else
5767
0
      next = ' ';
5768
0
    } else {
5769
0
        next = in->cur[1];
5770
0
    }
5771
0
    cur = in->cur[0];
5772
0
          if (cur != '<') {
5773
0
        ctxt->instate = XML_PARSER_CONTENT;
5774
#ifdef DEBUG_PUSH
5775
        xmlGenericError(xmlGenericErrorContext,
5776
          "HPP: entering CONTENT\n");
5777
#endif
5778
0
        break;
5779
0
    }
5780
0
    if (next == '/') {
5781
0
        ctxt->instate = XML_PARSER_END_TAG;
5782
0
        ctxt->checkIndex = 0;
5783
#ifdef DEBUG_PUSH
5784
        xmlGenericError(xmlGenericErrorContext,
5785
          "HPP: entering END_TAG\n");
5786
#endif
5787
0
        break;
5788
0
    }
5789
0
    if ((!terminate) &&
5790
0
        (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
5791
0
        goto done;
5792
5793
                /* Capture start position */
5794
0
          if (ctxt->record_info) {
5795
0
               node_info.begin_pos = ctxt->input->consumed +
5796
0
                                  (CUR_PTR - ctxt->input->base);
5797
0
               node_info.begin_line = ctxt->input->line;
5798
0
          }
5799
5800
5801
0
    failed = htmlParseStartTag(ctxt);
5802
0
    name = ctxt->name;
5803
0
    if ((failed == -1) ||
5804
0
        (name == NULL)) {
5805
0
        if (CUR == '>')
5806
0
      NEXT;
5807
0
        break;
5808
0
    }
5809
5810
    /*
5811
     * Lookup the info for that element.
5812
     */
5813
0
    info = htmlTagLookup(name);
5814
0
    if (info == NULL) {
5815
0
        htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
5816
0
                     "Tag %s invalid\n", name, NULL);
5817
0
    }
5818
5819
    /*
5820
     * Check for an Empty Element labeled the XML/SGML way
5821
     */
5822
0
    if ((CUR == '/') && (NXT(1) == '>')) {
5823
0
        SKIP(2);
5824
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5825
0
      ctxt->sax->endElement(ctxt->userData, name);
5826
0
        htmlnamePop(ctxt);
5827
0
        ctxt->instate = XML_PARSER_CONTENT;
5828
#ifdef DEBUG_PUSH
5829
        xmlGenericError(xmlGenericErrorContext,
5830
          "HPP: entering CONTENT\n");
5831
#endif
5832
0
        break;
5833
0
    }
5834
5835
0
    if (CUR == '>') {
5836
0
        NEXT;
5837
0
    } else {
5838
0
        htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
5839
0
                     "Couldn't find end of Start Tag %s\n",
5840
0
         name, NULL);
5841
5842
        /*
5843
         * end of parsing of this node.
5844
         */
5845
0
        if (xmlStrEqual(name, ctxt->name)) {
5846
0
      nodePop(ctxt);
5847
0
      htmlnamePop(ctxt);
5848
0
        }
5849
5850
0
        if (ctxt->record_info)
5851
0
            htmlNodeInfoPush(ctxt, &node_info);
5852
5853
0
        ctxt->instate = XML_PARSER_CONTENT;
5854
#ifdef DEBUG_PUSH
5855
        xmlGenericError(xmlGenericErrorContext,
5856
          "HPP: entering CONTENT\n");
5857
#endif
5858
0
        break;
5859
0
    }
5860
5861
    /*
5862
     * Check for an Empty Element from DTD definition
5863
     */
5864
0
    if ((info != NULL) && (info->empty)) {
5865
0
        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5866
0
      ctxt->sax->endElement(ctxt->userData, name);
5867
0
        htmlnamePop(ctxt);
5868
0
    }
5869
5870
0
                if (ctxt->record_info)
5871
0
              htmlNodeInfoPush(ctxt, &node_info);
5872
5873
0
    ctxt->instate = XML_PARSER_CONTENT;
5874
#ifdef DEBUG_PUSH
5875
    xmlGenericError(xmlGenericErrorContext,
5876
      "HPP: entering CONTENT\n");
5877
#endif
5878
0
                break;
5879
0
      }
5880
0
            case XML_PARSER_CONTENT: {
5881
0
    xmlChar chr[2] = { 0, 0 };
5882
5883
                /*
5884
     * Handle preparsed entities and charRef
5885
     */
5886
0
    if (ctxt->token != 0) {
5887
0
        chr[0] = (xmlChar) ctxt->token;
5888
0
        htmlCheckParagraph(ctxt);
5889
0
        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
5890
0
      ctxt->sax->characters(ctxt->userData, chr, 1);
5891
0
        ctxt->token = 0;
5892
0
        ctxt->checkIndex = 0;
5893
0
    }
5894
0
    if ((avail == 1) && (terminate)) {
5895
0
        cur = in->cur[0];
5896
0
        if ((cur != '<') && (cur != '&')) {
5897
0
      if (ctxt->sax != NULL) {
5898
0
                            chr[0] = cur;
5899
0
          if (IS_BLANK_CH(cur)) {
5900
0
        if (ctxt->keepBlanks) {
5901
0
            if (ctxt->sax->characters != NULL)
5902
0
          ctxt->sax->characters(
5903
0
            ctxt->userData, chr, 1);
5904
0
        } else {
5905
0
            if (ctxt->sax->ignorableWhitespace != NULL)
5906
0
          ctxt->sax->ignorableWhitespace(
5907
0
            ctxt->userData, chr, 1);
5908
0
        }
5909
0
          } else {
5910
0
        htmlCheckParagraph(ctxt);
5911
0
        if (ctxt->sax->characters != NULL)
5912
0
            ctxt->sax->characters(
5913
0
              ctxt->userData, chr, 1);
5914
0
          }
5915
0
      }
5916
0
      ctxt->token = 0;
5917
0
      ctxt->checkIndex = 0;
5918
0
      in->cur++;
5919
0
      break;
5920
0
        }
5921
0
    }
5922
0
    if (avail < 2)
5923
0
        goto done;
5924
0
    cur = in->cur[0];
5925
0
    next = in->cur[1];
5926
0
    if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
5927
0
        (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
5928
        /*
5929
         * Handle SCRIPT/STYLE separately
5930
         */
5931
0
        if (!terminate) {
5932
0
            int idx;
5933
0
      xmlChar val;
5934
5935
0
      idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0);
5936
0
      if (idx < 0)
5937
0
          goto done;
5938
0
            val = in->cur[idx + 2];
5939
0
      if (val == 0) /* bad cut of input */
5940
0
          goto done;
5941
0
        }
5942
0
        htmlParseScript(ctxt);
5943
0
        if ((cur == '<') && (next == '/')) {
5944
0
      ctxt->instate = XML_PARSER_END_TAG;
5945
0
      ctxt->checkIndex = 0;
5946
#ifdef DEBUG_PUSH
5947
      xmlGenericError(xmlGenericErrorContext,
5948
        "HPP: entering END_TAG\n");
5949
#endif
5950
0
      break;
5951
0
        }
5952
0
    } else {
5953
        /*
5954
         * Sometimes DOCTYPE arrives in the middle of the document
5955
         */
5956
0
        if ((cur == '<') && (next == '!') &&
5957
0
      (UPP(2) == 'D') && (UPP(3) == 'O') &&
5958
0
      (UPP(4) == 'C') && (UPP(5) == 'T') &&
5959
0
      (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5960
0
      (UPP(8) == 'E')) {
5961
0
      if ((!terminate) &&
5962
0
          (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
5963
0
          goto done;
5964
0
      htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
5965
0
                   "Misplaced DOCTYPE declaration\n",
5966
0
             BAD_CAST "DOCTYPE" , NULL);
5967
0
      htmlParseDocTypeDecl(ctxt);
5968
0
        } else if ((cur == '<') && (next == '!') &&
5969
0
      (in->cur[2] == '-') && (in->cur[3] == '-')) {
5970
0
      if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
5971
0
          goto done;
5972
#ifdef DEBUG_PUSH
5973
      xmlGenericError(xmlGenericErrorContext,
5974
        "HPP: Parsing Comment\n");
5975
#endif
5976
0
      htmlParseComment(ctxt);
5977
0
      ctxt->instate = XML_PARSER_CONTENT;
5978
0
        } else if ((cur == '<') && (next == '?')) {
5979
0
      if ((!terminate) &&
5980
0
          (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
5981
0
          goto done;
5982
#ifdef DEBUG_PUSH
5983
      xmlGenericError(xmlGenericErrorContext,
5984
        "HPP: Parsing PI\n");
5985
#endif
5986
0
      htmlParsePI(ctxt);
5987
0
      ctxt->instate = XML_PARSER_CONTENT;
5988
0
        } else if ((cur == '<') && (next == '!') && (avail < 4)) {
5989
0
      goto done;
5990
0
        } else if ((cur == '<') && (next == '/')) {
5991
0
      ctxt->instate = XML_PARSER_END_TAG;
5992
0
      ctxt->checkIndex = 0;
5993
#ifdef DEBUG_PUSH
5994
      xmlGenericError(xmlGenericErrorContext,
5995
        "HPP: entering END_TAG\n");
5996
#endif
5997
0
      break;
5998
0
        } else if ((cur == '<') && IS_ASCII_LETTER(next)) {
5999
0
                        if ((!terminate) && (next == 0))
6000
0
                            goto done;
6001
0
                        ctxt->instate = XML_PARSER_START_TAG;
6002
0
                        ctxt->checkIndex = 0;
6003
#ifdef DEBUG_PUSH
6004
                        xmlGenericError(xmlGenericErrorContext,
6005
                                "HPP: entering START_TAG\n");
6006
#endif
6007
0
      break;
6008
0
        } else if (cur == '<') {
6009
0
                        if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
6010
0
                            (ctxt->sax->characters != NULL))
6011
0
          ctxt->sax->characters(ctxt->userData,
6012
0
              BAD_CAST "<", 1);
6013
0
                        NEXT;
6014
0
        } else {
6015
            /*
6016
       * check that the text sequence is complete
6017
       * before handing out the data to the parser
6018
       * to avoid problems with erroneous end of
6019
       * data detection.
6020
       */
6021
0
      if ((!terminate) &&
6022
0
                            (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
6023
0
          goto done;
6024
0
      ctxt->checkIndex = 0;
6025
#ifdef DEBUG_PUSH
6026
      xmlGenericError(xmlGenericErrorContext,
6027
        "HPP: Parsing char data\n");
6028
#endif
6029
0
                        while ((ctxt->instate != XML_PARSER_EOF) &&
6030
0
                               (cur != '<') && (in->cur < in->end)) {
6031
0
                            if (cur == '&') {
6032
0
              htmlParseReference(ctxt);
6033
0
                            } else {
6034
0
              htmlParseCharData(ctxt);
6035
0
                            }
6036
0
                            cur = in->cur[0];
6037
0
                        }
6038
0
        }
6039
0
    }
6040
6041
0
    break;
6042
0
      }
6043
0
            case XML_PARSER_END_TAG:
6044
0
    if (avail < 2)
6045
0
        goto done;
6046
0
    if ((!terminate) &&
6047
0
        (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
6048
0
        goto done;
6049
0
    htmlParseEndTag(ctxt);
6050
0
    if (ctxt->nameNr == 0) {
6051
0
        ctxt->instate = XML_PARSER_EPILOG;
6052
0
    } else {
6053
0
        ctxt->instate = XML_PARSER_CONTENT;
6054
0
    }
6055
0
    ctxt->checkIndex = 0;
6056
#ifdef DEBUG_PUSH
6057
    xmlGenericError(xmlGenericErrorContext,
6058
      "HPP: entering CONTENT\n");
6059
#endif
6060
0
          break;
6061
0
            case XML_PARSER_CDATA_SECTION:
6062
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6063
0
      "HPP: internal error, state == CDATA\n",
6064
0
           NULL, NULL);
6065
0
    ctxt->instate = XML_PARSER_CONTENT;
6066
0
    ctxt->checkIndex = 0;
6067
#ifdef DEBUG_PUSH
6068
    xmlGenericError(xmlGenericErrorContext,
6069
      "HPP: entering CONTENT\n");
6070
#endif
6071
0
    break;
6072
0
            case XML_PARSER_DTD:
6073
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6074
0
      "HPP: internal error, state == DTD\n",
6075
0
           NULL, NULL);
6076
0
    ctxt->instate = XML_PARSER_CONTENT;
6077
0
    ctxt->checkIndex = 0;
6078
#ifdef DEBUG_PUSH
6079
    xmlGenericError(xmlGenericErrorContext,
6080
      "HPP: entering CONTENT\n");
6081
#endif
6082
0
    break;
6083
0
            case XML_PARSER_COMMENT:
6084
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6085
0
      "HPP: internal error, state == COMMENT\n",
6086
0
           NULL, NULL);
6087
0
    ctxt->instate = XML_PARSER_CONTENT;
6088
0
    ctxt->checkIndex = 0;
6089
#ifdef DEBUG_PUSH
6090
    xmlGenericError(xmlGenericErrorContext,
6091
      "HPP: entering CONTENT\n");
6092
#endif
6093
0
    break;
6094
0
            case XML_PARSER_PI:
6095
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6096
0
      "HPP: internal error, state == PI\n",
6097
0
           NULL, NULL);
6098
0
    ctxt->instate = XML_PARSER_CONTENT;
6099
0
    ctxt->checkIndex = 0;
6100
#ifdef DEBUG_PUSH
6101
    xmlGenericError(xmlGenericErrorContext,
6102
      "HPP: entering CONTENT\n");
6103
#endif
6104
0
    break;
6105
0
            case XML_PARSER_ENTITY_DECL:
6106
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6107
0
      "HPP: internal error, state == ENTITY_DECL\n",
6108
0
           NULL, NULL);
6109
0
    ctxt->instate = XML_PARSER_CONTENT;
6110
0
    ctxt->checkIndex = 0;
6111
#ifdef DEBUG_PUSH
6112
    xmlGenericError(xmlGenericErrorContext,
6113
      "HPP: entering CONTENT\n");
6114
#endif
6115
0
    break;
6116
0
            case XML_PARSER_ENTITY_VALUE:
6117
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6118
0
      "HPP: internal error, state == ENTITY_VALUE\n",
6119
0
           NULL, NULL);
6120
0
    ctxt->instate = XML_PARSER_CONTENT;
6121
0
    ctxt->checkIndex = 0;
6122
#ifdef DEBUG_PUSH
6123
    xmlGenericError(xmlGenericErrorContext,
6124
      "HPP: entering DTD\n");
6125
#endif
6126
0
    break;
6127
0
            case XML_PARSER_ATTRIBUTE_VALUE:
6128
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6129
0
      "HPP: internal error, state == ATTRIBUTE_VALUE\n",
6130
0
           NULL, NULL);
6131
0
    ctxt->instate = XML_PARSER_START_TAG;
6132
0
    ctxt->checkIndex = 0;
6133
#ifdef DEBUG_PUSH
6134
    xmlGenericError(xmlGenericErrorContext,
6135
      "HPP: entering START_TAG\n");
6136
#endif
6137
0
    break;
6138
0
      case XML_PARSER_SYSTEM_LITERAL:
6139
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6140
0
        "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n",
6141
0
           NULL, NULL);
6142
0
    ctxt->instate = XML_PARSER_CONTENT;
6143
0
    ctxt->checkIndex = 0;
6144
#ifdef DEBUG_PUSH
6145
    xmlGenericError(xmlGenericErrorContext,
6146
      "HPP: entering CONTENT\n");
6147
#endif
6148
0
    break;
6149
0
      case XML_PARSER_IGNORE:
6150
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6151
0
      "HPP: internal error, state == XML_PARSER_IGNORE\n",
6152
0
           NULL, NULL);
6153
0
    ctxt->instate = XML_PARSER_CONTENT;
6154
0
    ctxt->checkIndex = 0;
6155
#ifdef DEBUG_PUSH
6156
    xmlGenericError(xmlGenericErrorContext,
6157
      "HPP: entering CONTENT\n");
6158
#endif
6159
0
    break;
6160
0
      case XML_PARSER_PUBLIC_LITERAL:
6161
0
    htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6162
0
      "HPP: internal error, state == XML_PARSER_LITERAL\n",
6163
0
           NULL, NULL);
6164
0
    ctxt->instate = XML_PARSER_CONTENT;
6165
0
    ctxt->checkIndex = 0;
6166
#ifdef DEBUG_PUSH
6167
    xmlGenericError(xmlGenericErrorContext,
6168
      "HPP: entering CONTENT\n");
6169
#endif
6170
0
    break;
6171
6172
0
  }
6173
0
    }
6174
0
done:
6175
0
    if ((avail == 0) && (terminate)) {
6176
0
  htmlAutoCloseOnEnd(ctxt);
6177
0
  if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
6178
      /*
6179
       * SAX: end of the document processing.
6180
       */
6181
0
      ctxt->instate = XML_PARSER_EOF;
6182
0
      if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
6183
0
    ctxt->sax->endDocument(ctxt->userData);
6184
0
  }
6185
0
    }
6186
0
    if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL) &&
6187
0
  ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
6188
0
   (ctxt->instate == XML_PARSER_EPILOG))) {
6189
0
  xmlDtdPtr dtd;
6190
0
  dtd = xmlGetIntSubset(ctxt->myDoc);
6191
0
  if (dtd == NULL)
6192
0
      ctxt->myDoc->intSubset =
6193
0
    xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
6194
0
        BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
6195
0
        BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
6196
0
    }
6197
#ifdef DEBUG_PUSH
6198
    xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
6199
#endif
6200
0
    return(ret);
6201
0
}
6202
6203
/**
6204
 * htmlParseChunk:
6205
 * @ctxt:  an HTML parser context
6206
 * @chunk:  an char array
6207
 * @size:  the size in byte of the chunk
6208
 * @terminate:  last chunk indicator
6209
 *
6210
 * Parse a Chunk of memory
6211
 *
6212
 * Returns zero if no error, the xmlParserErrors otherwise.
6213
 */
6214
int
6215
htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
6216
0
              int terminate) {
6217
0
    if ((ctxt == NULL) || (ctxt->input == NULL)) {
6218
0
  htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
6219
0
         "htmlParseChunk: context error\n", NULL, NULL);
6220
0
  return(XML_ERR_INTERNAL_ERROR);
6221
0
    }
6222
0
    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
6223
0
        (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
6224
0
  size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
6225
0
  size_t cur = ctxt->input->cur - ctxt->input->base;
6226
0
  int res;
6227
6228
0
  res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
6229
0
        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
6230
0
  if (res < 0) {
6231
0
      ctxt->errNo = XML_PARSER_EOF;
6232
0
      ctxt->disableSAX = 1;
6233
0
      return (XML_PARSER_EOF);
6234
0
  }
6235
#ifdef DEBUG_PUSH
6236
  xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
6237
#endif
6238
6239
#if 0
6240
  if ((terminate) || (ctxt->input->buf->buffer->use > 80))
6241
      htmlParseTryOrFinish(ctxt, terminate);
6242
#endif
6243
0
    } else if (ctxt->instate != XML_PARSER_EOF) {
6244
0
  if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
6245
0
      xmlParserInputBufferPtr in = ctxt->input->buf;
6246
0
      if ((in->encoder != NULL) && (in->buffer != NULL) &&
6247
0
        (in->raw != NULL)) {
6248
0
    int nbchars;
6249
0
    size_t base = xmlBufGetInputBase(in->buffer, ctxt->input);
6250
0
    size_t current = ctxt->input->cur - ctxt->input->base;
6251
6252
0
    nbchars = xmlCharEncInput(in, terminate);
6253
0
    xmlBufSetInputBaseCur(in->buffer, ctxt->input, base, current);
6254
0
    if (nbchars < 0) {
6255
0
        htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
6256
0
               "encoder error\n", NULL, NULL);
6257
0
        return(XML_ERR_INVALID_ENCODING);
6258
0
    }
6259
0
      }
6260
0
  }
6261
0
    }
6262
0
    htmlParseTryOrFinish(ctxt, terminate);
6263
0
    if (terminate) {
6264
0
  if ((ctxt->instate != XML_PARSER_EOF) &&
6265
0
      (ctxt->instate != XML_PARSER_EPILOG) &&
6266
0
      (ctxt->instate != XML_PARSER_MISC)) {
6267
0
      ctxt->errNo = XML_ERR_DOCUMENT_END;
6268
0
      ctxt->wellFormed = 0;
6269
0
  }
6270
0
  if (ctxt->instate != XML_PARSER_EOF) {
6271
0
      if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
6272
0
    ctxt->sax->endDocument(ctxt->userData);
6273
0
  }
6274
0
  ctxt->instate = XML_PARSER_EOF;
6275
0
    }
6276
0
    return((xmlParserErrors) ctxt->errNo);
6277
0
}
6278
6279
/************************************************************************
6280
 *                  *
6281
 *      User entry points       *
6282
 *                  *
6283
 ************************************************************************/
6284
6285
/**
6286
 * htmlCreatePushParserCtxt:
6287
 * @sax:  a SAX handler
6288
 * @user_data:  The user data returned on SAX callbacks
6289
 * @chunk:  a pointer to an array of chars
6290
 * @size:  number of chars in the array
6291
 * @filename:  an optional file name or URI
6292
 * @enc:  an optional encoding
6293
 *
6294
 * Create a parser context for using the HTML parser in push mode
6295
 * The value of @filename is used for fetching external entities
6296
 * and error/warning reports.
6297
 *
6298
 * Returns the new parser context or NULL
6299
 */
6300
htmlParserCtxtPtr
6301
htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
6302
                         const char *chunk, int size, const char *filename,
6303
0
       xmlCharEncoding enc) {
6304
0
    htmlParserCtxtPtr ctxt;
6305
0
    htmlParserInputPtr inputStream;
6306
0
    xmlParserInputBufferPtr buf;
6307
6308
0
    xmlInitParser();
6309
6310
0
    buf = xmlAllocParserInputBuffer(enc);
6311
0
    if (buf == NULL) return(NULL);
6312
6313
0
    ctxt = htmlNewParserCtxt();
6314
0
    if (ctxt == NULL) {
6315
0
  xmlFreeParserInputBuffer(buf);
6316
0
  return(NULL);
6317
0
    }
6318
0
    if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
6319
0
  ctxt->charset=XML_CHAR_ENCODING_UTF8;
6320
0
    if (sax != NULL) {
6321
0
  if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
6322
0
      xmlFree(ctxt->sax);
6323
0
  ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
6324
0
  if (ctxt->sax == NULL) {
6325
0
      xmlFree(buf);
6326
0
      xmlFree(ctxt);
6327
0
      return(NULL);
6328
0
  }
6329
0
  memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
6330
0
  if (user_data != NULL)
6331
0
      ctxt->userData = user_data;
6332
0
    }
6333
0
    if (filename == NULL) {
6334
0
  ctxt->directory = NULL;
6335
0
    } else {
6336
0
        ctxt->directory = xmlParserGetDirectory(filename);
6337
0
    }
6338
6339
0
    inputStream = htmlNewInputStream(ctxt);
6340
0
    if (inputStream == NULL) {
6341
0
  xmlFreeParserCtxt(ctxt);
6342
0
  xmlFree(buf);
6343
0
  return(NULL);
6344
0
    }
6345
6346
0
    if (filename == NULL)
6347
0
  inputStream->filename = NULL;
6348
0
    else
6349
0
  inputStream->filename = (char *)
6350
0
      xmlCanonicPath((const xmlChar *) filename);
6351
0
    inputStream->buf = buf;
6352
0
    xmlBufResetInput(buf->buffer, inputStream);
6353
6354
0
    inputPush(ctxt, inputStream);
6355
6356
0
    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
6357
0
        (ctxt->input->buf != NULL))  {
6358
0
  size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input);
6359
0
  size_t cur = ctxt->input->cur - ctxt->input->base;
6360
6361
0
  xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
6362
6363
0
        xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur);
6364
#ifdef DEBUG_PUSH
6365
  xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
6366
#endif
6367
0
    }
6368
0
    ctxt->progressive = 1;
6369
6370
0
    return(ctxt);
6371
0
}
6372
#endif /* LIBXML_PUSH_ENABLED */
6373
6374
/**
6375
 * htmlSAXParseDoc:
6376
 * @cur:  a pointer to an array of xmlChar
6377
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
6378
 * @sax:  the SAX handler block
6379
 * @userData: if using SAX, this pointer will be provided on callbacks.
6380
 *
6381
 * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
6382
 * to handle parse events. If sax is NULL, fallback to the default DOM
6383
 * behavior and return a tree.
6384
 *
6385
 * Returns the resulting document tree unless SAX is NULL or the document is
6386
 *     not well formed.
6387
 */
6388
6389
htmlDocPtr
6390
htmlSAXParseDoc(const xmlChar *cur, const char *encoding,
6391
0
                htmlSAXHandlerPtr sax, void *userData) {
6392
0
    htmlDocPtr ret;
6393
0
    htmlParserCtxtPtr ctxt;
6394
6395
0
    xmlInitParser();
6396
6397
0
    if (cur == NULL) return(NULL);
6398
6399
6400
0
    ctxt = htmlCreateDocParserCtxt(cur, encoding);
6401
0
    if (ctxt == NULL) return(NULL);
6402
0
    if (sax != NULL) {
6403
0
        if (ctxt->sax != NULL) xmlFree (ctxt->sax);
6404
0
        ctxt->sax = sax;
6405
0
        ctxt->userData = userData;
6406
0
    }
6407
6408
0
    htmlParseDocument(ctxt);
6409
0
    ret = ctxt->myDoc;
6410
0
    if (sax != NULL) {
6411
0
  ctxt->sax = NULL;
6412
0
  ctxt->userData = NULL;
6413
0
    }
6414
0
    htmlFreeParserCtxt(ctxt);
6415
6416
0
    return(ret);
6417
0
}
6418
6419
/**
6420
 * htmlParseDoc:
6421
 * @cur:  a pointer to an array of xmlChar
6422
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
6423
 *
6424
 * parse an HTML in-memory document and build a tree.
6425
 *
6426
 * Returns the resulting document tree
6427
 */
6428
6429
htmlDocPtr
6430
0
htmlParseDoc(const xmlChar *cur, const char *encoding) {
6431
0
    return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
6432
0
}
6433
6434
6435
/**
6436
 * htmlCreateFileParserCtxt:
6437
 * @filename:  the filename
6438
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
6439
 *
6440
 * Create a parser context for a file content.
6441
 * Automatic support for ZLIB/Compress compressed document is provided
6442
 * by default if found at compile-time.
6443
 *
6444
 * Returns the new parser context or NULL
6445
 */
6446
htmlParserCtxtPtr
6447
htmlCreateFileParserCtxt(const char *filename, const char *encoding)
6448
0
{
6449
0
    htmlParserCtxtPtr ctxt;
6450
0
    htmlParserInputPtr inputStream;
6451
0
    char *canonicFilename;
6452
    /* htmlCharEncoding enc; */
6453
0
    xmlChar *content, *content_line = (xmlChar *) "charset=";
6454
6455
0
    if (filename == NULL)
6456
0
        return(NULL);
6457
6458
0
    ctxt = htmlNewParserCtxt();
6459
0
    if (ctxt == NULL) {
6460
0
  return(NULL);
6461
0
    }
6462
0
    canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
6463
0
    if (canonicFilename == NULL) {
6464
0
#ifdef LIBXML_SAX1_ENABLED
6465
0
  if (xmlDefaultSAXHandler.error != NULL) {
6466
0
      xmlDefaultSAXHandler.error(NULL, "out of memory\n");
6467
0
  }
6468
0
#endif
6469
0
  xmlFreeParserCtxt(ctxt);
6470
0
  return(NULL);
6471
0
    }
6472
6473
0
    inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
6474
0
    xmlFree(canonicFilename);
6475
0
    if (inputStream == NULL) {
6476
0
  xmlFreeParserCtxt(ctxt);
6477
0
  return(NULL);
6478
0
    }
6479
6480
0
    inputPush(ctxt, inputStream);
6481
6482
    /* set encoding */
6483
0
    if (encoding) {
6484
0
        size_t l = strlen(encoding);
6485
6486
0
  if (l < 1000) {
6487
0
      content = xmlMallocAtomic (xmlStrlen(content_line) + l + 1);
6488
0
      if (content) {
6489
0
    strcpy ((char *)content, (char *)content_line);
6490
0
    strcat ((char *)content, (char *)encoding);
6491
0
    htmlCheckEncoding (ctxt, content);
6492
0
    xmlFree (content);
6493
0
      }
6494
0
  }
6495
0
    }
6496
6497
0
    return(ctxt);
6498
0
}
6499
6500
/**
6501
 * htmlSAXParseFile:
6502
 * @filename:  the filename
6503
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
6504
 * @sax:  the SAX handler block
6505
 * @userData: if using SAX, this pointer will be provided on callbacks.
6506
 *
6507
 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6508
 * compressed document is provided by default if found at compile-time.
6509
 * It use the given SAX function block to handle the parsing callback.
6510
 * If sax is NULL, fallback to the default DOM tree building routines.
6511
 *
6512
 * Returns the resulting document tree unless SAX is NULL or the document is
6513
 *     not well formed.
6514
 */
6515
6516
htmlDocPtr
6517
htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
6518
0
                 void *userData) {
6519
0
    htmlDocPtr ret;
6520
0
    htmlParserCtxtPtr ctxt;
6521
0
    htmlSAXHandlerPtr oldsax = NULL;
6522
6523
0
    xmlInitParser();
6524
6525
0
    ctxt = htmlCreateFileParserCtxt(filename, encoding);
6526
0
    if (ctxt == NULL) return(NULL);
6527
0
    if (sax != NULL) {
6528
0
  oldsax = ctxt->sax;
6529
0
        ctxt->sax = sax;
6530
0
        ctxt->userData = userData;
6531
0
    }
6532
6533
0
    htmlParseDocument(ctxt);
6534
6535
0
    ret = ctxt->myDoc;
6536
0
    if (sax != NULL) {
6537
0
        ctxt->sax = oldsax;
6538
0
        ctxt->userData = NULL;
6539
0
    }
6540
0
    htmlFreeParserCtxt(ctxt);
6541
6542
0
    return(ret);
6543
0
}
6544
6545
/**
6546
 * htmlParseFile:
6547
 * @filename:  the filename
6548
 * @encoding:  a free form C string describing the HTML document encoding, or NULL
6549
 *
6550
 * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
6551
 * compressed document is provided by default if found at compile-time.
6552
 *
6553
 * Returns the resulting document tree
6554
 */
6555
6556
htmlDocPtr
6557
0
htmlParseFile(const char *filename, const char *encoding) {
6558
0
    return(htmlSAXParseFile(filename, encoding, NULL, NULL));
6559
0
}
6560
6561
/**
6562
 * htmlHandleOmittedElem:
6563
 * @val:  int 0 or 1
6564
 *
6565
 * Set and return the previous value for handling HTML omitted tags.
6566
 *
6567
 * Returns the last value for 0 for no handling, 1 for auto insertion.
6568
 */
6569
6570
int
6571
0
htmlHandleOmittedElem(int val) {
6572
0
    int old = htmlOmittedDefaultValue;
6573
6574
0
    htmlOmittedDefaultValue = val;
6575
0
    return(old);
6576
0
}
6577
6578
/**
6579
 * htmlElementAllowedHere:
6580
 * @parent: HTML parent element
6581
 * @elt: HTML element
6582
 *
6583
 * Checks whether an HTML element may be a direct child of a parent element.
6584
 * Note - doesn't check for deprecated elements
6585
 *
6586
 * Returns 1 if allowed; 0 otherwise.
6587
 */
6588
int
6589
0
htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
6590
0
  const char** p ;
6591
6592
0
  if ( ! elt || ! parent || ! parent->subelts )
6593
0
  return 0 ;
6594
6595
0
  for ( p = parent->subelts; *p; ++p )
6596
0
    if ( !xmlStrcmp((const xmlChar *)*p, elt) )
6597
0
      return 1 ;
6598
6599
0
  return 0 ;
6600
0
}
6601
/**
6602
 * htmlElementStatusHere:
6603
 * @parent: HTML parent element
6604
 * @elt: HTML element
6605
 *
6606
 * Checks whether an HTML element may be a direct child of a parent element.
6607
 * and if so whether it is valid or deprecated.
6608
 *
6609
 * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6610
 */
6611
htmlStatus
6612
0
htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
6613
0
  if ( ! parent || ! elt )
6614
0
    return HTML_INVALID ;
6615
0
  if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
6616
0
    return HTML_INVALID ;
6617
6618
0
  return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
6619
0
}
6620
/**
6621
 * htmlAttrAllowed:
6622
 * @elt: HTML element
6623
 * @attr: HTML attribute
6624
 * @legacy: whether to allow deprecated attributes
6625
 *
6626
 * Checks whether an attribute is valid for an element
6627
 * Has full knowledge of Required and Deprecated attributes
6628
 *
6629
 * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
6630
 */
6631
htmlStatus
6632
0
htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
6633
0
  const char** p ;
6634
6635
0
  if ( !elt || ! attr )
6636
0
  return HTML_INVALID ;
6637
6638
0
  if ( elt->attrs_req )
6639
0
    for ( p = elt->attrs_req; *p; ++p)
6640
0
      if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6641
0
        return HTML_REQUIRED ;
6642
6643
0
  if ( elt->attrs_opt )
6644
0
    for ( p = elt->attrs_opt; *p; ++p)
6645
0
      if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6646
0
        return HTML_VALID ;
6647
6648
0
  if ( legacy && elt->attrs_depr )
6649
0
    for ( p = elt->attrs_depr; *p; ++p)
6650
0
      if ( !xmlStrcmp((const xmlChar*)*p, attr) )
6651
0
        return HTML_DEPRECATED ;
6652
6653
0
  return HTML_INVALID ;
6654
0
}
6655
/**
6656
 * htmlNodeStatus:
6657
 * @node: an htmlNodePtr in a tree
6658
 * @legacy: whether to allow deprecated elements (YES is faster here
6659
 *  for Element nodes)
6660
 *
6661
 * Checks whether the tree node is valid.  Experimental (the author
6662
 *     only uses the HTML enhancements in a SAX parser)
6663
 *
6664
 * Return: for Element nodes, a return from htmlElementAllowedHere (if
6665
 *  legacy allowed) or htmlElementStatusHere (otherwise).
6666
 *  for Attribute nodes, a return from htmlAttrAllowed
6667
 *  for other nodes, HTML_NA (no checks performed)
6668
 */
6669
htmlStatus
6670
0
htmlNodeStatus(const htmlNodePtr node, int legacy) {
6671
0
  if ( ! node )
6672
0
    return HTML_INVALID ;
6673
6674
0
  switch ( node->type ) {
6675
0
    case XML_ELEMENT_NODE:
6676
0
      return legacy
6677
0
  ? ( htmlElementAllowedHere (
6678
0
    htmlTagLookup(node->parent->name) , node->name
6679
0
    ) ? HTML_VALID : HTML_INVALID )
6680
0
  : htmlElementStatusHere(
6681
0
    htmlTagLookup(node->parent->name) ,
6682
0
    htmlTagLookup(node->name) )
6683
0
  ;
6684
0
    case XML_ATTRIBUTE_NODE:
6685
0
      return htmlAttrAllowed(
6686
0
  htmlTagLookup(node->parent->name) , node->name, legacy) ;
6687
0
    default: return HTML_NA ;
6688
0
  }
6689
0
}
6690
/************************************************************************
6691
 *                  *
6692
 *  New set (2.6.0) of simpler and more flexible APIs   *
6693
 *                  *
6694
 ************************************************************************/
6695
/**
6696
 * DICT_FREE:
6697
 * @str:  a string
6698
 *
6699
 * Free a string if it is not owned by the "dict" dictionary in the
6700
 * current scope
6701
 */
6702
#define DICT_FREE(str)            \
6703
0
  if ((str) && ((!dict) ||       \
6704
0
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
6705
0
      xmlFree((char *)(str));
6706
6707
/**
6708
 * htmlCtxtReset:
6709
 * @ctxt: an HTML parser context
6710
 *
6711
 * Reset a parser context
6712
 */
6713
void
6714
htmlCtxtReset(htmlParserCtxtPtr ctxt)
6715
0
{
6716
0
    xmlParserInputPtr input;
6717
0
    xmlDictPtr dict;
6718
6719
0
    if (ctxt == NULL)
6720
0
        return;
6721
6722
0
    xmlInitParser();
6723
0
    dict = ctxt->dict;
6724
6725
0
    while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
6726
0
        xmlFreeInputStream(input);
6727
0
    }
6728
0
    ctxt->inputNr = 0;
6729
0
    ctxt->input = NULL;
6730
6731
0
    ctxt->spaceNr = 0;
6732
0
    if (ctxt->spaceTab != NULL) {
6733
0
  ctxt->spaceTab[0] = -1;
6734
0
  ctxt->space = &ctxt->spaceTab[0];
6735
0
    } else {
6736
0
  ctxt->space = NULL;
6737
0
    }
6738
6739
6740
0
    ctxt->nodeNr = 0;
6741
0
    ctxt->node = NULL;
6742
6743
0
    ctxt->nameNr = 0;
6744
0
    ctxt->name = NULL;
6745
6746
0
    DICT_FREE(ctxt->version);
6747
0
    ctxt->version = NULL;
6748
0
    DICT_FREE(ctxt->encoding);
6749
0
    ctxt->encoding = NULL;
6750
0
    DICT_FREE(ctxt->directory);
6751
0
    ctxt->directory = NULL;
6752
0
    DICT_FREE(ctxt->extSubURI);
6753
0
    ctxt->extSubURI = NULL;
6754
0
    DICT_FREE(ctxt->extSubSystem);
6755
0
    ctxt->extSubSystem = NULL;
6756
0
    if (ctxt->myDoc != NULL)
6757
0
        xmlFreeDoc(ctxt->myDoc);
6758
0
    ctxt->myDoc = NULL;
6759
6760
0
    ctxt->standalone = -1;
6761
0
    ctxt->hasExternalSubset = 0;
6762
0
    ctxt->hasPErefs = 0;
6763
0
    ctxt->html = 1;
6764
0
    ctxt->external = 0;
6765
0
    ctxt->instate = XML_PARSER_START;
6766
0
    ctxt->token = 0;
6767
6768
0
    ctxt->wellFormed = 1;
6769
0
    ctxt->nsWellFormed = 1;
6770
0
    ctxt->disableSAX = 0;
6771
0
    ctxt->valid = 1;
6772
0
    ctxt->vctxt.userData = ctxt;
6773
0
    ctxt->vctxt.error = xmlParserValidityError;
6774
0
    ctxt->vctxt.warning = xmlParserValidityWarning;
6775
0
    ctxt->record_info = 0;
6776
0
    ctxt->checkIndex = 0;
6777
0
    ctxt->inSubset = 0;
6778
0
    ctxt->errNo = XML_ERR_OK;
6779
0
    ctxt->depth = 0;
6780
0
    ctxt->charset = XML_CHAR_ENCODING_NONE;
6781
0
    ctxt->catalogs = NULL;
6782
0
    xmlInitNodeInfoSeq(&ctxt->node_seq);
6783
6784
0
    if (ctxt->attsDefault != NULL) {
6785
0
        xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
6786
0
        ctxt->attsDefault = NULL;
6787
0
    }
6788
0
    if (ctxt->attsSpecial != NULL) {
6789
0
        xmlHashFree(ctxt->attsSpecial, NULL);
6790
0
        ctxt->attsSpecial = NULL;
6791
0
    }
6792
0
}
6793
6794
/**
6795
 * htmlCtxtUseOptions:
6796
 * @ctxt: an HTML parser context
6797
 * @options:  a combination of htmlParserOption(s)
6798
 *
6799
 * Applies the options to the parser context
6800
 *
6801
 * Returns 0 in case of success, the set of unknown or unimplemented options
6802
 *         in case of error.
6803
 */
6804
int
6805
htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
6806
0
{
6807
0
    if (ctxt == NULL)
6808
0
        return(-1);
6809
6810
0
    if (options & HTML_PARSE_NOWARNING) {
6811
0
        ctxt->sax->warning = NULL;
6812
0
        ctxt->vctxt.warning = NULL;
6813
0
        options -= XML_PARSE_NOWARNING;
6814
0
  ctxt->options |= XML_PARSE_NOWARNING;
6815
0
    }
6816
0
    if (options & HTML_PARSE_NOERROR) {
6817
0
        ctxt->sax->error = NULL;
6818
0
        ctxt->vctxt.error = NULL;
6819
0
        ctxt->sax->fatalError = NULL;
6820
0
        options -= XML_PARSE_NOERROR;
6821
0
  ctxt->options |= XML_PARSE_NOERROR;
6822
0
    }
6823
0
    if (options & HTML_PARSE_PEDANTIC) {
6824
0
        ctxt->pedantic = 1;
6825
0
        options -= XML_PARSE_PEDANTIC;
6826
0
  ctxt->options |= XML_PARSE_PEDANTIC;
6827
0
    } else
6828
0
        ctxt->pedantic = 0;
6829
0
    if (options & XML_PARSE_NOBLANKS) {
6830
0
        ctxt->keepBlanks = 0;
6831
0
        ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
6832
0
        options -= XML_PARSE_NOBLANKS;
6833
0
  ctxt->options |= XML_PARSE_NOBLANKS;
6834
0
    } else
6835
0
        ctxt->keepBlanks = 1;
6836
0
    if (options & HTML_PARSE_RECOVER) {
6837
0
        ctxt->recovery = 1;
6838
0
  options -= HTML_PARSE_RECOVER;
6839
0
    } else
6840
0
        ctxt->recovery = 0;
6841
0
    if (options & HTML_PARSE_COMPACT) {
6842
0
  ctxt->options |= HTML_PARSE_COMPACT;
6843
0
        options -= HTML_PARSE_COMPACT;
6844
0
    }
6845
0
    if (options & XML_PARSE_HUGE) {
6846
0
  ctxt->options |= XML_PARSE_HUGE;
6847
0
        options -= XML_PARSE_HUGE;
6848
0
    }
6849
0
    if (options & HTML_PARSE_NODEFDTD) {
6850
0
  ctxt->options |= HTML_PARSE_NODEFDTD;
6851
0
        options -= HTML_PARSE_NODEFDTD;
6852
0
    }
6853
0
    if (options & HTML_PARSE_IGNORE_ENC) {
6854
0
  ctxt->options |= HTML_PARSE_IGNORE_ENC;
6855
0
        options -= HTML_PARSE_IGNORE_ENC;
6856
0
    }
6857
0
    if (options & HTML_PARSE_NOIMPLIED) {
6858
0
        ctxt->options |= HTML_PARSE_NOIMPLIED;
6859
0
        options -= HTML_PARSE_NOIMPLIED;
6860
0
    }
6861
0
    ctxt->dictNames = 0;
6862
0
    return (options);
6863
0
}
6864
6865
/**
6866
 * htmlDoRead:
6867
 * @ctxt:  an HTML parser context
6868
 * @URL:  the base URL to use for the document
6869
 * @encoding:  the document encoding, or NULL
6870
 * @options:  a combination of htmlParserOption(s)
6871
 * @reuse:  keep the context for reuse
6872
 *
6873
 * Common front-end for the htmlRead functions
6874
 *
6875
 * Returns the resulting document tree or NULL
6876
 */
6877
static htmlDocPtr
6878
htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
6879
          int options, int reuse)
6880
0
{
6881
0
    htmlDocPtr ret;
6882
6883
0
    htmlCtxtUseOptions(ctxt, options);
6884
0
    ctxt->html = 1;
6885
0
    if (encoding != NULL) {
6886
0
        xmlCharEncodingHandlerPtr hdlr;
6887
6888
0
  hdlr = xmlFindCharEncodingHandler(encoding);
6889
0
  if (hdlr != NULL) {
6890
0
      xmlSwitchToEncoding(ctxt, hdlr);
6891
0
      if (ctxt->input->encoding != NULL)
6892
0
        xmlFree((xmlChar *) ctxt->input->encoding);
6893
0
            ctxt->input->encoding = xmlStrdup((xmlChar *)encoding);
6894
0
        }
6895
0
    }
6896
0
    if ((URL != NULL) && (ctxt->input != NULL) &&
6897
0
        (ctxt->input->filename == NULL))
6898
0
        ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
6899
0
    htmlParseDocument(ctxt);
6900
0
    ret = ctxt->myDoc;
6901
0
    ctxt->myDoc = NULL;
6902
0
    if (!reuse) {
6903
0
        if ((ctxt->dictNames) &&
6904
0
      (ret != NULL) &&
6905
0
      (ret->dict == ctxt->dict))
6906
0
      ctxt->dict = NULL;
6907
0
  xmlFreeParserCtxt(ctxt);
6908
0
    }
6909
0
    return (ret);
6910
0
}
6911
6912
/**
6913
 * htmlReadDoc:
6914
 * @cur:  a pointer to a zero terminated string
6915
 * @URL:  the base URL to use for the document
6916
 * @encoding:  the document encoding, or NULL
6917
 * @options:  a combination of htmlParserOption(s)
6918
 *
6919
 * parse an XML in-memory document and build a tree.
6920
 *
6921
 * Returns the resulting document tree
6922
 */
6923
htmlDocPtr
6924
htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
6925
0
{
6926
0
    htmlParserCtxtPtr ctxt;
6927
6928
0
    if (cur == NULL)
6929
0
        return (NULL);
6930
6931
0
    xmlInitParser();
6932
0
    ctxt = htmlCreateDocParserCtxt(cur, NULL);
6933
0
    if (ctxt == NULL)
6934
0
        return (NULL);
6935
0
    return (htmlDoRead(ctxt, URL, encoding, options, 0));
6936
0
}
6937
6938
/**
6939
 * htmlReadFile:
6940
 * @filename:  a file or URL
6941
 * @encoding:  the document encoding, or NULL
6942
 * @options:  a combination of htmlParserOption(s)
6943
 *
6944
 * parse an XML file from the filesystem or the network.
6945
 *
6946
 * Returns the resulting document tree
6947
 */
6948
htmlDocPtr
6949
htmlReadFile(const char *filename, const char *encoding, int options)
6950
0
{
6951
0
    htmlParserCtxtPtr ctxt;
6952
6953
0
    xmlInitParser();
6954
0
    ctxt = htmlCreateFileParserCtxt(filename, encoding);
6955
0
    if (ctxt == NULL)
6956
0
        return (NULL);
6957
0
    return (htmlDoRead(ctxt, NULL, NULL, options, 0));
6958
0
}
6959
6960
/**
6961
 * htmlReadMemory:
6962
 * @buffer:  a pointer to a char array
6963
 * @size:  the size of the array
6964
 * @URL:  the base URL to use for the document
6965
 * @encoding:  the document encoding, or NULL
6966
 * @options:  a combination of htmlParserOption(s)
6967
 *
6968
 * parse an XML in-memory document and build a tree.
6969
 *
6970
 * Returns the resulting document tree
6971
 */
6972
htmlDocPtr
6973
htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
6974
0
{
6975
0
    htmlParserCtxtPtr ctxt;
6976
6977
0
    xmlInitParser();
6978
0
    ctxt = xmlCreateMemoryParserCtxt(buffer, size);
6979
0
    if (ctxt == NULL)
6980
0
        return (NULL);
6981
0
    htmlDefaultSAXHandlerInit();
6982
0
    if (ctxt->sax != NULL)
6983
0
        memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1));
6984
0
    return (htmlDoRead(ctxt, URL, encoding, options, 0));
6985
0
}
6986
6987
/**
6988
 * htmlReadFd:
6989
 * @fd:  an open file descriptor
6990
 * @URL:  the base URL to use for the document
6991
 * @encoding:  the document encoding, or NULL
6992
 * @options:  a combination of htmlParserOption(s)
6993
 *
6994
 * parse an HTML from a file descriptor and build a tree.
6995
 * NOTE that the file descriptor will not be closed when the
6996
 *      reader is closed or reset.
6997
 *
6998
 * Returns the resulting document tree
6999
 */
7000
htmlDocPtr
7001
htmlReadFd(int fd, const char *URL, const char *encoding, int options)
7002
0
{
7003
0
    htmlParserCtxtPtr ctxt;
7004
0
    xmlParserInputBufferPtr input;
7005
0
    htmlParserInputPtr stream;
7006
7007
0
    if (fd < 0)
7008
0
        return (NULL);
7009
7010
0
    xmlInitParser();
7011
0
    input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
7012
0
    if (input == NULL)
7013
0
        return (NULL);
7014
0
    input->closecallback = NULL;
7015
0
    ctxt = htmlNewParserCtxt();
7016
0
    if (ctxt == NULL) {
7017
0
        xmlFreeParserInputBuffer(input);
7018
0
        return (NULL);
7019
0
    }
7020
0
    stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
7021
0
    if (stream == NULL) {
7022
0
        xmlFreeParserInputBuffer(input);
7023
0
  htmlFreeParserCtxt(ctxt);
7024
0
        return (NULL);
7025
0
    }
7026
0
    inputPush(ctxt, stream);
7027
0
    return (htmlDoRead(ctxt, URL, encoding, options, 0));
7028
0
}
7029
7030
/**
7031
 * htmlReadIO:
7032
 * @ioread:  an I/O read function
7033
 * @ioclose:  an I/O close function
7034
 * @ioctx:  an I/O handler
7035
 * @URL:  the base URL to use for the document
7036
 * @encoding:  the document encoding, or NULL
7037
 * @options:  a combination of htmlParserOption(s)
7038
 *
7039
 * parse an HTML document from I/O functions and source and build a tree.
7040
 *
7041
 * Returns the resulting document tree
7042
 */
7043
htmlDocPtr
7044
htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
7045
          void *ioctx, const char *URL, const char *encoding, int options)
7046
0
{
7047
0
    htmlParserCtxtPtr ctxt;
7048
0
    xmlParserInputBufferPtr input;
7049
0
    xmlParserInputPtr stream;
7050
7051
0
    if (ioread == NULL)
7052
0
        return (NULL);
7053
0
    xmlInitParser();
7054
7055
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
7056
0
                                         XML_CHAR_ENCODING_NONE);
7057
0
    if (input == NULL) {
7058
0
        if (ioclose != NULL)
7059
0
            ioclose(ioctx);
7060
0
        return (NULL);
7061
0
    }
7062
0
    ctxt = htmlNewParserCtxt();
7063
0
    if (ctxt == NULL) {
7064
0
        xmlFreeParserInputBuffer(input);
7065
0
        return (NULL);
7066
0
    }
7067
0
    stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
7068
0
    if (stream == NULL) {
7069
0
        xmlFreeParserInputBuffer(input);
7070
0
  xmlFreeParserCtxt(ctxt);
7071
0
        return (NULL);
7072
0
    }
7073
0
    inputPush(ctxt, stream);
7074
0
    return (htmlDoRead(ctxt, URL, encoding, options, 0));
7075
0
}
7076
7077
/**
7078
 * htmlCtxtReadDoc:
7079
 * @ctxt:  an HTML parser context
7080
 * @cur:  a pointer to a zero terminated string
7081
 * @URL:  the base URL to use for the document
7082
 * @encoding:  the document encoding, or NULL
7083
 * @options:  a combination of htmlParserOption(s)
7084
 *
7085
 * parse an XML in-memory document and build a tree.
7086
 * This reuses the existing @ctxt parser context
7087
 *
7088
 * Returns the resulting document tree
7089
 */
7090
htmlDocPtr
7091
htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur,
7092
               const char *URL, const char *encoding, int options)
7093
0
{
7094
0
    xmlParserInputPtr stream;
7095
7096
0
    if (cur == NULL)
7097
0
        return (NULL);
7098
0
    if (ctxt == NULL)
7099
0
        return (NULL);
7100
0
    xmlInitParser();
7101
7102
0
    htmlCtxtReset(ctxt);
7103
7104
0
    stream = xmlNewStringInputStream(ctxt, cur);
7105
0
    if (stream == NULL) {
7106
0
        return (NULL);
7107
0
    }
7108
0
    inputPush(ctxt, stream);
7109
0
    return (htmlDoRead(ctxt, URL, encoding, options, 1));
7110
0
}
7111
7112
/**
7113
 * htmlCtxtReadFile:
7114
 * @ctxt:  an HTML parser context
7115
 * @filename:  a file or URL
7116
 * @encoding:  the document encoding, or NULL
7117
 * @options:  a combination of htmlParserOption(s)
7118
 *
7119
 * parse an XML file from the filesystem or the network.
7120
 * This reuses the existing @ctxt parser context
7121
 *
7122
 * Returns the resulting document tree
7123
 */
7124
htmlDocPtr
7125
htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
7126
                const char *encoding, int options)
7127
0
{
7128
0
    xmlParserInputPtr stream;
7129
7130
0
    if (filename == NULL)
7131
0
        return (NULL);
7132
0
    if (ctxt == NULL)
7133
0
        return (NULL);
7134
0
    xmlInitParser();
7135
7136
0
    htmlCtxtReset(ctxt);
7137
7138
0
    stream = xmlLoadExternalEntity(filename, NULL, ctxt);
7139
0
    if (stream == NULL) {
7140
0
        return (NULL);
7141
0
    }
7142
0
    inputPush(ctxt, stream);
7143
0
    return (htmlDoRead(ctxt, NULL, encoding, options, 1));
7144
0
}
7145
7146
/**
7147
 * htmlCtxtReadMemory:
7148
 * @ctxt:  an HTML parser context
7149
 * @buffer:  a pointer to a char array
7150
 * @size:  the size of the array
7151
 * @URL:  the base URL to use for the document
7152
 * @encoding:  the document encoding, or NULL
7153
 * @options:  a combination of htmlParserOption(s)
7154
 *
7155
 * parse an XML in-memory document and build a tree.
7156
 * This reuses the existing @ctxt parser context
7157
 *
7158
 * Returns the resulting document tree
7159
 */
7160
htmlDocPtr
7161
htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
7162
                  const char *URL, const char *encoding, int options)
7163
0
{
7164
0
    xmlParserInputBufferPtr input;
7165
0
    xmlParserInputPtr stream;
7166
7167
0
    if (ctxt == NULL)
7168
0
        return (NULL);
7169
0
    if (buffer == NULL)
7170
0
        return (NULL);
7171
0
    xmlInitParser();
7172
7173
0
    htmlCtxtReset(ctxt);
7174
7175
0
    input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
7176
0
    if (input == NULL) {
7177
0
  return(NULL);
7178
0
    }
7179
7180
0
    stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
7181
0
    if (stream == NULL) {
7182
0
  xmlFreeParserInputBuffer(input);
7183
0
  return(NULL);
7184
0
    }
7185
7186
0
    inputPush(ctxt, stream);
7187
0
    return (htmlDoRead(ctxt, URL, encoding, options, 1));
7188
0
}
7189
7190
/**
7191
 * htmlCtxtReadFd:
7192
 * @ctxt:  an HTML parser context
7193
 * @fd:  an open file descriptor
7194
 * @URL:  the base URL to use for the document
7195
 * @encoding:  the document encoding, or NULL
7196
 * @options:  a combination of htmlParserOption(s)
7197
 *
7198
 * parse an XML from a file descriptor and build a tree.
7199
 * This reuses the existing @ctxt parser context
7200
 *
7201
 * Returns the resulting document tree
7202
 */
7203
htmlDocPtr
7204
htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
7205
              const char *URL, const char *encoding, int options)
7206
0
{
7207
0
    xmlParserInputBufferPtr input;
7208
0
    xmlParserInputPtr stream;
7209
7210
0
    if (fd < 0)
7211
0
        return (NULL);
7212
0
    if (ctxt == NULL)
7213
0
        return (NULL);
7214
0
    xmlInitParser();
7215
7216
0
    htmlCtxtReset(ctxt);
7217
7218
7219
0
    input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
7220
0
    if (input == NULL)
7221
0
        return (NULL);
7222
0
    stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
7223
0
    if (stream == NULL) {
7224
0
        xmlFreeParserInputBuffer(input);
7225
0
        return (NULL);
7226
0
    }
7227
0
    inputPush(ctxt, stream);
7228
0
    return (htmlDoRead(ctxt, URL, encoding, options, 1));
7229
0
}
7230
7231
/**
7232
 * htmlCtxtReadIO:
7233
 * @ctxt:  an HTML parser context
7234
 * @ioread:  an I/O read function
7235
 * @ioclose:  an I/O close function
7236
 * @ioctx:  an I/O handler
7237
 * @URL:  the base URL to use for the document
7238
 * @encoding:  the document encoding, or NULL
7239
 * @options:  a combination of htmlParserOption(s)
7240
 *
7241
 * parse an HTML document from I/O functions and source and build a tree.
7242
 * This reuses the existing @ctxt parser context
7243
 *
7244
 * Returns the resulting document tree
7245
 */
7246
htmlDocPtr
7247
htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
7248
              xmlInputCloseCallback ioclose, void *ioctx,
7249
        const char *URL,
7250
              const char *encoding, int options)
7251
0
{
7252
0
    xmlParserInputBufferPtr input;
7253
0
    xmlParserInputPtr stream;
7254
7255
0
    if (ioread == NULL)
7256
0
        return (NULL);
7257
0
    if (ctxt == NULL)
7258
0
        return (NULL);
7259
0
    xmlInitParser();
7260
7261
0
    htmlCtxtReset(ctxt);
7262
7263
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
7264
0
                                         XML_CHAR_ENCODING_NONE);
7265
0
    if (input == NULL) {
7266
0
        if (ioclose != NULL)
7267
0
            ioclose(ioctx);
7268
0
        return (NULL);
7269
0
    }
7270
0
    stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
7271
0
    if (stream == NULL) {
7272
0
        xmlFreeParserInputBuffer(input);
7273
0
        return (NULL);
7274
0
    }
7275
0
    inputPush(ctxt, stream);
7276
0
    return (htmlDoRead(ctxt, URL, encoding, options, 1));
7277
0
}
7278
7279
#endif /* LIBXML_HTML_ENABLED */