Coverage Report

Created: 2024-02-11 06:24

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