Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * HTMLparser.c : an HTML 4.0 non-verifying parser |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 9 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | #ifdef LIBXML_HTML_ENABLED |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #ifdef HAVE_CTYPE_H |
| 15 | #include <ctype.h> |
| 16 | #endif |
| 17 | #ifdef HAVE_STDLIB_H |
| 18 | #include <stdlib.h> |
| 19 | #endif |
| 20 | #ifdef HAVE_SYS_STAT_H |
| 21 | #include <sys/stat.h> |
| 22 | #endif |
| 23 | #ifdef HAVE_FCNTL_H |
| 24 | #include <fcntl.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_UNISTD_H |
| 27 | #include <unistd.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_ZLIB_H |
| 30 | #include <zlib.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <libxml/xmlmemory.h> |
| 34 | #include <libxml/tree.h> |
| 35 | #include <libxml/parser.h> |
| 36 | #include <libxml/parserInternals.h> |
| 37 | #include <libxml/xmlerror.h> |
| 38 | #include <libxml/HTMLparser.h> |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 39 | #include <libxml/HTMLtree.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 40 | #include <libxml/entities.h> |
| 41 | #include <libxml/encoding.h> |
| 42 | #include <libxml/valid.h> |
| 43 | #include <libxml/xmlIO.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 44 | #include <libxml/globals.h> |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 45 | #include <libxml/uri.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | |
| 47 | #define HTML_MAX_NAMELEN 1000 |
| 48 | #define HTML_PARSER_BIG_BUFFER_SIZE 1000 |
| 49 | #define HTML_PARSER_BUFFER_SIZE 100 |
| 50 | |
| 51 | /* #define DEBUG */ |
| 52 | /* #define DEBUG_PUSH */ |
| 53 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 54 | static int htmlOmittedDefaultValue = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 56 | xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len, |
| 57 | xmlChar end, xmlChar end2, xmlChar end3); |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 58 | static void htmlParseComment(htmlParserCtxtPtr ctxt); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 59 | |
| 60 | /************************************************************************ |
| 61 | * * |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 62 | * Some factorized error routines * |
| 63 | * * |
| 64 | ************************************************************************/ |
| 65 | |
| 66 | /** |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 67 | * htmlErrMemory: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 68 | * @ctxt: an HTML parser context |
| 69 | * @extra: extra informations |
| 70 | * |
| 71 | * Handle a redefinition of attribute error |
| 72 | */ |
| 73 | static void |
| 74 | htmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra) |
| 75 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 76 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 77 | (ctxt->instate == XML_PARSER_EOF)) |
| 78 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 79 | if (ctxt != NULL) { |
| 80 | ctxt->errNo = XML_ERR_NO_MEMORY; |
| 81 | ctxt->instate = XML_PARSER_EOF; |
| 82 | ctxt->disableSAX = 1; |
| 83 | } |
| 84 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 85 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 86 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 87 | NULL, NULL, 0, 0, |
| 88 | "Memory allocation failed : %s\n", extra); |
| 89 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 90 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 91 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 92 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * htmlParseErr: |
| 97 | * @ctxt: an HTML parser context |
| 98 | * @error: the error number |
| 99 | * @msg: the error message |
| 100 | * @str1: string infor |
| 101 | * @str2: string infor |
| 102 | * |
| 103 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 104 | */ |
| 105 | static void |
| 106 | htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 107 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 108 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 109 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 110 | (ctxt->instate == XML_PARSER_EOF)) |
| 111 | return; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 112 | if (ctxt != NULL) |
| 113 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 114 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 115 | XML_ERR_ERROR, NULL, 0, |
| 116 | (const char *) str1, (const char *) str2, |
| 117 | NULL, 0, 0, |
| 118 | msg, str1, str2); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 119 | if (ctxt != NULL) |
| 120 | ctxt->wellFormed = 0; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * htmlParseErrInt: |
| 125 | * @ctxt: an HTML parser context |
| 126 | * @error: the error number |
| 127 | * @msg: the error message |
| 128 | * @val: integer info |
| 129 | * |
| 130 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 131 | */ |
| 132 | static void |
| 133 | htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 134 | const char *msg, int val) |
| 135 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 136 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 137 | (ctxt->instate == XML_PARSER_EOF)) |
| 138 | return; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 139 | if (ctxt != NULL) |
| 140 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 141 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 142 | XML_ERR_ERROR, NULL, 0, NULL, NULL, |
| 143 | NULL, val, 0, msg, val); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 144 | if (ctxt != NULL) |
| 145 | ctxt->wellFormed = 0; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /************************************************************************ |
| 149 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 150 | * Parser stacks related functions and macros * |
| 151 | * * |
| 152 | ************************************************************************/ |
| 153 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 154 | /** |
| 155 | * htmlnamePush: |
| 156 | * @ctxt: an HTML parser context |
| 157 | * @value: the element name |
| 158 | * |
| 159 | * Pushes a new element name on top of the name stack |
| 160 | * |
| 161 | * Returns 0 in case of error, the index in the stack otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 162 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 163 | static int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 164 | htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 165 | { |
| 166 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 167 | ctxt->nameMax *= 2; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 168 | ctxt->nameTab = (const xmlChar * *) |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 169 | xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 170 | ctxt->nameMax * |
| 171 | sizeof(ctxt->nameTab[0])); |
| 172 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 173 | htmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 174 | return (0); |
| 175 | } |
| 176 | } |
| 177 | ctxt->nameTab[ctxt->nameNr] = value; |
| 178 | ctxt->name = value; |
| 179 | return (ctxt->nameNr++); |
| 180 | } |
| 181 | /** |
| 182 | * htmlnamePop: |
| 183 | * @ctxt: an HTML parser context |
| 184 | * |
| 185 | * Pops the top element name from the name stack |
| 186 | * |
| 187 | * Returns the name just removed |
| 188 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 189 | static const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 190 | htmlnamePop(htmlParserCtxtPtr ctxt) |
| 191 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 192 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 193 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 194 | if (ctxt->nameNr <= 0) |
| 195 | return (0); |
| 196 | ctxt->nameNr--; |
| 197 | if (ctxt->nameNr < 0) |
| 198 | return (0); |
| 199 | if (ctxt->nameNr > 0) |
| 200 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 201 | else |
| 202 | ctxt->name = NULL; |
| 203 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 204 | ctxt->nameTab[ctxt->nameNr] = 0; |
| 205 | return (ret); |
| 206 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Macros for accessing the content. Those should be used only by the parser, |
| 210 | * and not exported. |
| 211 | * |
| 212 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 213 | * |
| 214 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 215 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 216 | * in ISO-Latin or UTF-8, and the current 16 bit value if compiled |
| 217 | * in UNICODE mode. This should be used internally by the parser |
| 218 | * only to compare to ASCII values otherwise it would break when |
| 219 | * running with UTF-8 encoding. |
| 220 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 221 | * to compare on ASCII based substring. |
| 222 | * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR |
| 223 | * it should be used only to compare on ASCII based substring. |
| 224 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 225 | * strings without newlines within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 226 | * |
| 227 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 228 | * |
| 229 | * CURRENT Returns the current char value, with the full decoding of |
| 230 | * UTF-8 if we are using this mode. It returns an int. |
| 231 | * NEXT Skip to the next character, this does the proper decoding |
| 232 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 233 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 234 | * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly |
| 235 | */ |
| 236 | |
| 237 | #define UPPER (toupper(*ctxt->input->cur)) |
| 238 | |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 239 | #define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 240 | |
| 241 | #define NXT(val) ctxt->input->cur[(val)] |
| 242 | |
| 243 | #define UPP(val) (toupper(ctxt->input->cur[(val)])) |
| 244 | |
| 245 | #define CUR_PTR ctxt->input->cur |
| 246 | |
Daniel Veillard | 652f9aa | 2003-10-28 22:04:45 +0000 | [diff] [blame] | 247 | #define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 248 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
| 249 | xmlParserInputShrink(ctxt->input) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 250 | |
Daniel Veillard | 652f9aa | 2003-10-28 22:04:45 +0000 | [diff] [blame] | 251 | #define GROW if ((ctxt->progressive == 0) && \ |
| 252 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
| 253 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 254 | |
| 255 | #define CURRENT ((int) (*ctxt->input->cur)) |
| 256 | |
| 257 | #define SKIP_BLANKS htmlSkipBlankChars(ctxt) |
| 258 | |
| 259 | /* Inported from XML */ |
| 260 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 261 | /* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */ |
| 262 | #define CUR ((int) (*ctxt->input->cur)) |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 263 | #define NEXT xmlNextChar(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 264 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 265 | #define RAW (ctxt->token ? -1 : (*ctxt->input->cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 266 | #define NXT(val) ctxt->input->cur[(val)] |
| 267 | #define CUR_PTR ctxt->input->cur |
| 268 | |
| 269 | |
| 270 | #define NEXTL(l) do { \ |
| 271 | if (*(ctxt->input->cur) == '\n') { \ |
| 272 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 273 | } else ctxt->input->col++; \ |
| 274 | ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \ |
| 275 | } while (0) |
| 276 | |
| 277 | /************ |
| 278 | \ |
| 279 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 280 | if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt); |
| 281 | ************/ |
| 282 | |
| 283 | #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l) |
| 284 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 285 | |
| 286 | #define COPY_BUF(l,b,i,v) \ |
| 287 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 288 | else i += xmlCopyChar(l,&b[i],v) |
| 289 | |
| 290 | /** |
| 291 | * htmlCurrentChar: |
| 292 | * @ctxt: the HTML parser context |
| 293 | * @len: pointer to the length of the char read |
| 294 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 295 | * The current char value, if using UTF-8 this may actually span multiple |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 296 | * bytes in the input buffer. Implement the end of line normalization: |
| 297 | * 2.11 End-of-Line Handling |
| 298 | * If the encoding is unspecified, in the case we find an ISO-Latin-1 |
| 299 | * char, then the encoding converter is plugged in automatically. |
| 300 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 301 | * Returns the current char value and its length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 302 | */ |
| 303 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 304 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 305 | htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) { |
| 306 | if (ctxt->instate == XML_PARSER_EOF) |
| 307 | return(0); |
| 308 | |
| 309 | if (ctxt->token != 0) { |
| 310 | *len = 0; |
| 311 | return(ctxt->token); |
| 312 | } |
| 313 | if (ctxt->charset == XML_CHAR_ENCODING_UTF8) { |
| 314 | /* |
| 315 | * We are supposed to handle UTF8, check it's valid |
| 316 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 317 | * |
| 318 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 319 | * 0000 0000-0000 007F 0xxxxxxx |
| 320 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 321 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 322 | * |
| 323 | * Check for the 0x110000 limit too |
| 324 | */ |
| 325 | const unsigned char *cur = ctxt->input->cur; |
| 326 | unsigned char c; |
| 327 | unsigned int val; |
| 328 | |
| 329 | c = *cur; |
| 330 | if (c & 0x80) { |
| 331 | if (cur[1] == 0) |
| 332 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 333 | if ((cur[1] & 0xc0) != 0x80) |
| 334 | goto encoding_error; |
| 335 | if ((c & 0xe0) == 0xe0) { |
| 336 | |
| 337 | if (cur[2] == 0) |
| 338 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 339 | if ((cur[2] & 0xc0) != 0x80) |
| 340 | goto encoding_error; |
| 341 | if ((c & 0xf0) == 0xf0) { |
| 342 | if (cur[3] == 0) |
| 343 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 344 | if (((c & 0xf8) != 0xf0) || |
| 345 | ((cur[3] & 0xc0) != 0x80)) |
| 346 | goto encoding_error; |
| 347 | /* 4-byte code */ |
| 348 | *len = 4; |
| 349 | val = (cur[0] & 0x7) << 18; |
| 350 | val |= (cur[1] & 0x3f) << 12; |
| 351 | val |= (cur[2] & 0x3f) << 6; |
| 352 | val |= cur[3] & 0x3f; |
| 353 | } else { |
| 354 | /* 3-byte code */ |
| 355 | *len = 3; |
| 356 | val = (cur[0] & 0xf) << 12; |
| 357 | val |= (cur[1] & 0x3f) << 6; |
| 358 | val |= cur[2] & 0x3f; |
| 359 | } |
| 360 | } else { |
| 361 | /* 2-byte code */ |
| 362 | *len = 2; |
| 363 | val = (cur[0] & 0x1f) << 6; |
| 364 | val |= cur[1] & 0x3f; |
| 365 | } |
| 366 | if (!IS_CHAR(val)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 367 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 368 | "Char 0x%X out of allowed range\n", val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 369 | } |
| 370 | return(val); |
| 371 | } else { |
| 372 | /* 1-byte code */ |
| 373 | *len = 1; |
| 374 | return((int) *ctxt->input->cur); |
| 375 | } |
| 376 | } |
| 377 | /* |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 378 | * Assume it's a fixed length encoding (1) with |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 379 | * a compatible encoding for the ASCII set, since |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 380 | * XML constructs only use < 128 chars |
| 381 | */ |
| 382 | *len = 1; |
| 383 | if ((int) *ctxt->input->cur < 0x80) |
| 384 | return((int) *ctxt->input->cur); |
| 385 | |
| 386 | /* |
| 387 | * Humm this is bad, do an automatic flow conversion |
| 388 | */ |
| 389 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1); |
| 390 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 391 | return(xmlCurrentChar(ctxt, len)); |
| 392 | |
| 393 | encoding_error: |
| 394 | /* |
| 395 | * If we detect an UTF8 error that probably mean that the |
| 396 | * input encoding didn't get properly advertized in the |
| 397 | * declaration header. Report the error and switch the encoding |
| 398 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 399 | * encoding !) |
| 400 | */ |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 401 | { |
| 402 | char buffer[150]; |
| 403 | |
| 404 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 405 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 406 | ctxt->input->cur[2], ctxt->input->cur[3]); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 407 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 408 | "Input is not proper UTF-8, indicate encoding !\n", |
| 409 | BAD_CAST buffer, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | ctxt->charset = XML_CHAR_ENCODING_8859_1; |
| 413 | *len = 1; |
| 414 | return((int) *ctxt->input->cur); |
| 415 | } |
| 416 | |
| 417 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 418 | * htmlSkipBlankChars: |
| 419 | * @ctxt: the HTML parser context |
| 420 | * |
| 421 | * skip all blanks character found at that point in the input streams. |
| 422 | * |
| 423 | * Returns the number of space chars skipped |
| 424 | */ |
| 425 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 426 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | htmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
| 428 | int res = 0; |
| 429 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 430 | while (IS_BLANK_CH(*(ctxt->input->cur))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 431 | if ((*ctxt->input->cur == 0) && |
| 432 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) { |
| 433 | xmlPopInput(ctxt); |
| 434 | } else { |
| 435 | if (*(ctxt->input->cur) == '\n') { |
| 436 | ctxt->input->line++; ctxt->input->col = 1; |
| 437 | } else ctxt->input->col++; |
| 438 | ctxt->input->cur++; |
| 439 | ctxt->nbChars++; |
| 440 | if (*ctxt->input->cur == 0) |
| 441 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 442 | } |
| 443 | res++; |
| 444 | } |
| 445 | return(res); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | /************************************************************************ |
| 451 | * * |
| 452 | * The list of HTML elements and their properties * |
| 453 | * * |
| 454 | ************************************************************************/ |
| 455 | |
| 456 | /* |
| 457 | * Start Tag: 1 means the start tag can be ommited |
| 458 | * End Tag: 1 means the end tag can be ommited |
| 459 | * 2 means it's forbidden (empty elements) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 460 | * 3 means the tag is stylistic and should be closed easily |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 461 | * Depr: this element is deprecated |
| 462 | * DTD: 1 means that this element is valid only in the Loose DTD |
| 463 | * 2 means that this element is valid only in the Frameset DTD |
| 464 | * |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 465 | * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 466 | , subElements , impliedsubelt , Attributes, userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 467 | */ |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 468 | |
| 469 | /* Definitions and a couple of vars for HTML Elements */ |
| 470 | |
| 471 | #define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 472 | #define NB_FONTSTYLE 8 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 473 | #define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 474 | #define NB_PHRASE 10 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 475 | #define SPECIAL "a", "img", "applet", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 476 | #define NB_SPECIAL 15 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 477 | #define INLINE PCDATA FONTSTYLE PHRASE SPECIAL FORMCTRL |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 478 | #define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL |
| 479 | #define BLOCK HEADING, LIST "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address" |
| 480 | #define NB_BLOCK NB_HEADING + NB_LIST + 14 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 481 | #define FORMCTRL "input", "select", "textarea", "label", "button" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 482 | #define NB_FORMCTRL 5 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 483 | #define PCDATA |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 484 | #define NB_PCDATA 0 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 485 | #define HEADING "h1", "h2", "h3", "h4", "h5", "h6" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 486 | #define NB_HEADING 6 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 487 | #define LIST "ul", "ol", "dir", "menu" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 488 | #define NB_LIST 4 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 489 | #define MODIFIER |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 490 | #define NB_MODIFIER 0 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 491 | #define FLOW BLOCK,INLINE |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 492 | #define NB_FLOW NB_BLOCK + NB_INLINE |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 493 | #define EMPTY NULL |
| 494 | |
| 495 | |
| 496 | static const char* html_flow[] = { FLOW, NULL } ; |
| 497 | static const char* html_inline[] = { INLINE, NULL } ; |
| 498 | |
| 499 | /* placeholders: elts with content but no subelements */ |
| 500 | static const char* html_pcdata[] = { NULL } ; |
| 501 | #define html_cdata html_pcdata |
| 502 | |
| 503 | |
| 504 | /* ... and for HTML Attributes */ |
| 505 | |
| 506 | #define COREATTRS "id", "class", "style", "title" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 507 | #define NB_COREATTRS 4 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 508 | #define I18N "lang", "dir" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 509 | #define NB_I18N 2 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 510 | #define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 511 | #define NB_EVENTS 9 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 512 | #define ATTRS COREATTRS,I18N,EVENTS |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 513 | #define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 514 | #define CELLHALIGN "align", "char", "charoff" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 515 | #define NB_CELLHALIGN 3 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 516 | #define CELLVALIGN "valign" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 517 | #define NB_CELLVALIGN 1 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 518 | |
| 519 | static const char* html_attrs[] = { ATTRS, NULL } ; |
| 520 | static const char* core_i18n_attrs[] = { COREATTRS, I18N, NULL } ; |
| 521 | static const char* core_attrs[] = { COREATTRS, NULL } ; |
| 522 | static const char* i18n_attrs[] = { I18N, NULL } ; |
| 523 | |
| 524 | |
| 525 | /* Other declarations that should go inline ... */ |
| 526 | static const char* a_attrs[] = { ATTRS, "charset", "type", "name", |
| 527 | "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords", |
| 528 | "tabindex", "onfocus", "onblur", NULL } ; |
| 529 | static const char* target_attr[] = { "target", NULL } ; |
| 530 | static const char* rows_cols_attr[] = { "rows", "cols", NULL } ; |
| 531 | static const char* alt_attr[] = { "alt", NULL } ; |
| 532 | static const char* src_alt_attrs[] = { "src", "alt", NULL } ; |
| 533 | static const char* href_attrs[] = { "href", NULL } ; |
| 534 | static const char* clear_attrs[] = { "clear", NULL } ; |
| 535 | static const char* inline_p[] = { INLINE, "p", NULL } ; |
| 536 | static const char* flow_param[] = { FLOW, "param", NULL } ; |
| 537 | static const char* applet_attrs[] = { COREATTRS , "codebase", |
| 538 | "archive", "alt", "name", "height", "width", "align", |
| 539 | "hspace", "vspace", NULL } ; |
| 540 | static const char* area_attrs[] = { "shape", "coords", "href", "nohref", |
| 541 | "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
| 542 | static const char* basefont_attrs[] = |
| 543 | { "id", "size", "color", "face", NULL } ; |
| 544 | static const char* quote_attrs[] = { ATTRS, "cite", NULL } ; |
| 545 | static const char* body_contents[] = { FLOW, "ins", "del", NULL } ; |
| 546 | static const char* body_attrs[] = { ATTRS, "onload", "onunload", NULL } ; |
| 547 | static const char* body_depr[] = { "background", "bgcolor", "text", |
| 548 | "link", "vlink", "alink", NULL } ; |
| 549 | static const char* button_attrs[] = { ATTRS, "name", "value", "type", |
| 550 | "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
| 551 | |
| 552 | |
| 553 | static const char* col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ; |
| 554 | static const char* col_elt[] = { "col", NULL } ; |
| 555 | static const char* edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ; |
| 556 | static const char* compact_attrs[] = { ATTRS, "compact", NULL } ; |
| 557 | static const char* dl_contents[] = { "dt", "dd", NULL } ; |
| 558 | static const char* compact_attr[] = { "compact", NULL } ; |
| 559 | static const char* label_attr[] = { "label", NULL } ; |
| 560 | static const char* fieldset_contents[] = { FLOW, "legend" } ; |
| 561 | static const char* font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ; |
| 562 | static const char* form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ; |
| 563 | static const char* form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ; |
| 564 | static const char* frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ; |
| 565 | static const char* frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ; |
| 566 | static const char* frameset_contents[] = { "frameset", "frame", "noframes", NULL } ; |
| 567 | static const char* head_attrs[] = { I18N, "profile", NULL } ; |
| 568 | static const char* head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ; |
| 569 | static const char* hr_depr[] = { "align", "noshade", "size", "width", NULL } ; |
| 570 | static const char* version_attr[] = { "version", NULL } ; |
| 571 | static const char* html_content[] = { "head", "body", "frameset", NULL } ; |
| 572 | static const char* iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ; |
| 573 | static const char* img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ; |
| 574 | static const char* input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ; |
| 575 | static const char* prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ; |
| 576 | static const char* label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ; |
| 577 | static const char* legend_attrs[] = { ATTRS, "accesskey", NULL } ; |
| 578 | static const char* align_attr[] = { "align", NULL } ; |
| 579 | static const char* link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ; |
| 580 | static const char* map_contents[] = { BLOCK, "area", NULL } ; |
| 581 | static const char* name_attr[] = { "name", NULL } ; |
| 582 | static const char* action_attr[] = { "action", NULL } ; |
| 583 | static const char* blockli_elt[] = { BLOCK, "li", NULL } ; |
| 584 | static const char* meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ; |
| 585 | static const char* content_attr[] = { "content", NULL } ; |
| 586 | static const char* type_attr[] = { "type", NULL } ; |
| 587 | static const char* noframes_content[] = { "body", FLOW MODIFIER, NULL } ; |
| 588 | static const char* object_contents[] = { FLOW, "param", NULL } ; |
| 589 | static const char* object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ; |
| 590 | static const char* object_depr[] = { "align", "border", "hspace", "vspace", NULL } ; |
| 591 | static const char* ol_attrs[] = { "type", "compact", "start", NULL} ; |
| 592 | static const char* option_elt[] = { "option", NULL } ; |
| 593 | static const char* optgroup_attrs[] = { ATTRS, "disabled", NULL } ; |
| 594 | static const char* option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ; |
| 595 | static const char* param_attrs[] = { "id", "value", "valuetype", "type", NULL } ; |
| 596 | static const char* width_attr[] = { "width", NULL } ; |
| 597 | static const char* pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ; |
| 598 | static const char* script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ; |
| 599 | static const char* language_attr[] = { "language", NULL } ; |
| 600 | static const char* select_content[] = { "optgroup", "option", NULL } ; |
| 601 | static const char* select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ; |
| 602 | static const char* style_attrs[] = { I18N, "media", "title", NULL } ; |
| 603 | static const char* table_attrs[] = { ATTRS "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ; |
| 604 | static const char* table_depr[] = { "align", "bgcolor", NULL } ; |
| 605 | static const char* table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ; |
| 606 | static const char* tr_elt[] = { "tr", NULL } ; |
| 607 | static const char* talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ; |
| 608 | static const char* th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ; |
| 609 | static const char* th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ; |
| 610 | static const char* textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ; |
| 611 | static const char* tr_contents[] = { "th", "td", NULL } ; |
| 612 | static const char* bgcolor_attr[] = { "bgcolor", NULL } ; |
| 613 | static const char* li_elt[] = { "li", NULL } ; |
| 614 | static const char* ul_depr[] = { "type", "compact", NULL} ; |
| 615 | static const char* dir_attr[] = { "dir", NULL} ; |
| 616 | |
| 617 | #define DECL (const char**) |
| 618 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 619 | static const htmlElemDesc |
| 620 | html40ElementTable[] = { |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 621 | { "a", 0, 0, 0, 0, 0, 0, 1, "anchor ", |
| 622 | DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL |
| 623 | }, |
| 624 | { "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form", |
| 625 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 626 | }, |
| 627 | { "acronym", 0, 0, 0, 0, 0, 0, 1, "", |
| 628 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 629 | }, |
| 630 | { "address", 0, 0, 0, 0, 0, 0, 0, "information on author ", |
| 631 | DECL inline_p , NULL , DECL html_attrs, NULL, NULL |
| 632 | }, |
| 633 | { "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ", |
| 634 | DECL flow_param , NULL , NULL , DECL applet_attrs, NULL |
| 635 | }, |
| 636 | { "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ", |
| 637 | EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr |
| 638 | }, |
| 639 | { "b", 0, 3, 0, 0, 0, 0, 1, "bold text style", |
| 640 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 641 | }, |
| 642 | { "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ", |
| 643 | EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs |
| 644 | }, |
| 645 | { "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " , |
| 646 | EMPTY , NULL , NULL, DECL basefont_attrs, NULL |
| 647 | }, |
| 648 | { "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ", |
| 649 | DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr |
| 650 | }, |
| 651 | { "big", 0, 3, 0, 0, 0, 0, 1, "large text style", |
| 652 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 653 | }, |
| 654 | { "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ", |
| 655 | DECL html_flow , NULL , DECL quote_attrs , NULL, NULL |
| 656 | }, |
| 657 | { "body", 1, 1, 0, 0, 0, 0, 0, "document body ", |
| 658 | DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL |
| 659 | }, |
| 660 | { "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ", |
| 661 | EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL |
| 662 | }, |
| 663 | { "button", 0, 0, 0, 0, 0, 0, 2, "push button ", |
| 664 | DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL |
| 665 | }, |
| 666 | { "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ", |
| 667 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 668 | }, |
| 669 | { "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ", |
| 670 | DECL html_flow , NULL , NULL, DECL html_attrs, NULL |
| 671 | }, |
| 672 | { "cite", 0, 0, 0, 0, 0, 0, 1, "citation", |
| 673 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 674 | }, |
| 675 | { "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment", |
| 676 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 677 | }, |
| 678 | { "col", 0, 2, 2, 1, 0, 0, 0, "table column ", |
| 679 | EMPTY , NULL , DECL col_attrs , NULL, NULL |
| 680 | }, |
| 681 | { "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ", |
| 682 | DECL col_elt , "col" , DECL col_attrs , NULL, NULL |
| 683 | }, |
| 684 | { "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ", |
| 685 | DECL html_flow , NULL , DECL html_attrs, NULL, NULL |
| 686 | }, |
| 687 | { "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ", |
| 688 | DECL html_flow , NULL , DECL edit_attrs , NULL, NULL |
| 689 | }, |
| 690 | { "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition", |
| 691 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 692 | }, |
| 693 | { "dir", 0, 0, 0, 0, 1, 1, 0, "directory list", |
| 694 | DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL |
| 695 | }, |
| 696 | { "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container", |
| 697 | DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL |
| 698 | }, |
| 699 | { "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ", |
| 700 | DECL dl_contents , "dd" , html_attrs, DECL compact_attr, NULL |
| 701 | }, |
| 702 | { "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ", |
| 703 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 704 | }, |
| 705 | { "em", 0, 3, 0, 0, 0, 0, 1, "emphasis", |
| 706 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 707 | }, |
| 708 | { "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ", |
| 709 | DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL |
| 710 | }, |
| 711 | { "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ", |
| 712 | DECL html_inline, NULL, NULL, DECL font_attrs, NULL |
| 713 | }, |
| 714 | { "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ", |
| 715 | DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr |
| 716 | }, |
| 717 | { "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " , |
| 718 | EMPTY, NULL, NULL, DECL frame_attrs, NULL |
| 719 | }, |
| 720 | { "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" , |
| 721 | DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL |
| 722 | }, |
| 723 | { "h1", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 724 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 725 | }, |
| 726 | { "h2", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 727 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 728 | }, |
| 729 | { "h3", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 730 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 731 | }, |
| 732 | { "h4", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 733 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 734 | }, |
| 735 | { "h5", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 736 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 737 | }, |
| 738 | { "h6", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 739 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 740 | }, |
| 741 | { "head", 1, 1, 0, 0, 0, 0, 0, "document head ", |
| 742 | DECL head_contents, NULL, DECL head_attrs, NULL, NULL |
| 743 | }, |
| 744 | { "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " , |
| 745 | EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL |
| 746 | }, |
| 747 | { "html", 1, 1, 0, 0, 0, 0, 0, "document root element ", |
| 748 | DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL |
| 749 | }, |
| 750 | { "i", 0, 3, 0, 0, 0, 0, 1, "italic text style", |
| 751 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 752 | }, |
| 753 | { "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ", |
| 754 | DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL |
| 755 | }, |
| 756 | { "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ", |
| 757 | EMPTY, NULL, DECL img_attrs, DECL align_attr, src_alt_attrs |
| 758 | }, |
| 759 | { "input", 0, 2, 2, 1, 0, 0, 1, "form control ", |
| 760 | EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL |
| 761 | }, |
| 762 | { "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text", |
| 763 | DECL html_flow, NULL, DECL edit_attrs, NULL, NULL |
| 764 | }, |
| 765 | { "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ", |
| 766 | EMPTY, NULL, NULL, DECL prompt_attrs, NULL |
| 767 | }, |
| 768 | { "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user", |
| 769 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 770 | }, |
| 771 | { "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ", |
| 772 | DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL |
| 773 | }, |
| 774 | { "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ", |
| 775 | DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL |
| 776 | }, |
| 777 | { "li", 0, 1, 1, 0, 0, 0, 0, "list item ", |
| 778 | DECL html_flow, NULL, DECL html_attrs, NULL, NULL |
| 779 | }, |
| 780 | { "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ", |
| 781 | EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL |
| 782 | }, |
| 783 | { "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ", |
| 784 | DECL map_contents , NULL, DECL html_attrs , NULL, name_attr |
| 785 | }, |
| 786 | { "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ", |
| 787 | DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL |
| 788 | }, |
| 789 | { "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ", |
| 790 | EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr |
| 791 | }, |
| 792 | { "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ", |
| 793 | DECL noframes_content, "body" , DECL html_attrs, NULL, NULL |
| 794 | }, |
| 795 | { "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ", |
| 796 | DECL html_flow, "div", DECL html_attrs, NULL, NULL |
| 797 | }, |
| 798 | { "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ", |
| 799 | DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL |
| 800 | }, |
| 801 | { "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ", |
| 802 | DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL |
| 803 | }, |
| 804 | { "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ", |
| 805 | option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr |
| 806 | }, |
| 807 | { "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " , |
| 808 | DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL |
| 809 | }, |
| 810 | { "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ", |
| 811 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 812 | }, |
| 813 | { "param", 0, 2, 2, 1, 0, 0, 0, "named property value ", |
| 814 | EMPTY, NULL, DECL param_attrs, NULL, name_attr |
| 815 | }, |
| 816 | { "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ", |
| 817 | DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL |
| 818 | }, |
| 819 | { "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ", |
| 820 | DECL html_inline, NULL, DECL quote_attrs, NULL, NULL |
| 821 | }, |
| 822 | { "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style", |
| 823 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 824 | }, |
| 825 | { "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.", |
| 826 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 827 | }, |
| 828 | { "script", 0, 0, 0, 0, 0, 0, 2, "script statements ", |
| 829 | DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr |
| 830 | }, |
| 831 | { "select", 0, 0, 0, 0, 0, 0, 1, "option selector ", |
| 832 | DECL select_content, NULL, DECL select_attrs, NULL, NULL |
| 833 | }, |
| 834 | { "small", 0, 3, 0, 0, 0, 0, 1, "small text style", |
| 835 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 836 | }, |
| 837 | { "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ", |
| 838 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 839 | }, |
| 840 | { "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text", |
| 841 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 842 | }, |
| 843 | { "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis", |
| 844 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 845 | }, |
| 846 | { "style", 0, 0, 0, 0, 0, 0, 0, "style info ", |
| 847 | DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr |
| 848 | }, |
| 849 | { "sub", 0, 3, 0, 0, 0, 0, 1, "subscript", |
| 850 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 851 | }, |
| 852 | { "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ", |
| 853 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 854 | }, |
| 855 | { "table", 0, 0, 0, 0, 0, 0, 0, "", |
| 856 | DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL |
| 857 | }, |
| 858 | { "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ", |
| 859 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 860 | }, |
| 861 | { "td", 0, 0, 0, 0, 0, 0, 0, "table data cell", |
| 862 | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
| 863 | }, |
| 864 | { "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ", |
| 865 | DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr |
| 866 | }, |
| 867 | { "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ", |
| 868 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 869 | }, |
| 870 | { "th", 0, 1, 0, 0, 0, 0, 0, "table header cell", |
| 871 | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
| 872 | }, |
| 873 | { "thead", 0, 1, 0, 0, 0, 0, 0, "table header ", |
| 874 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 875 | }, |
| 876 | { "title", 0, 0, 0, 0, 0, 0, 0, "document title ", |
| 877 | DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL |
| 878 | }, |
| 879 | { "tr", 0, 0, 0, 0, 0, 0, 0, "table row ", |
| 880 | DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL |
| 881 | }, |
| 882 | { "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style", |
| 883 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 884 | }, |
| 885 | { "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style", |
| 886 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 887 | }, |
| 888 | { "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ", |
| 889 | DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL |
| 890 | }, |
| 891 | { "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument", |
| 892 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 893 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 894 | }; |
| 895 | |
| 896 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 897 | * start tags that imply the end of current element |
| 898 | */ |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 899 | static const char *htmlStartClose[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | "form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6", |
| 901 | "dl", "ul", "ol", "menu", "dir", "address", "pre", |
| 902 | "listing", "xmp", "head", NULL, |
| 903 | "head", "p", NULL, |
| 904 | "title", "p", NULL, |
| 905 | "body", "head", "style", "link", "title", "p", NULL, |
Daniel Veillard | 25d5d9a | 2004-04-05 07:08:42 +0000 | [diff] [blame] | 906 | "frameset", "head", "style", "link", "title", "p", NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 907 | "li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address", |
| 908 | "pre", "listing", "xmp", "head", "li", NULL, |
| 909 | "hr", "p", "head", NULL, |
| 910 | "h1", "p", "head", NULL, |
| 911 | "h2", "p", "head", NULL, |
| 912 | "h3", "p", "head", NULL, |
| 913 | "h4", "p", "head", NULL, |
| 914 | "h5", "p", "head", NULL, |
| 915 | "h6", "p", "head", NULL, |
| 916 | "dir", "p", "head", NULL, |
| 917 | "address", "p", "head", "ul", NULL, |
| 918 | "pre", "p", "head", "ul", NULL, |
| 919 | "listing", "p", "head", NULL, |
| 920 | "xmp", "p", "head", NULL, |
| 921 | "blockquote", "p", "head", NULL, |
| 922 | "dl", "p", "dt", "menu", "dir", "address", "pre", "listing", |
| 923 | "xmp", "head", NULL, |
| 924 | "dt", "p", "menu", "dir", "address", "pre", "listing", "xmp", |
| 925 | "head", "dd", NULL, |
| 926 | "dd", "p", "menu", "dir", "address", "pre", "listing", "xmp", |
| 927 | "head", "dt", NULL, |
| 928 | "ul", "p", "head", "ol", "menu", "dir", "address", "pre", |
| 929 | "listing", "xmp", NULL, |
| 930 | "ol", "p", "head", "ul", NULL, |
| 931 | "menu", "p", "head", "ul", NULL, |
| 932 | "p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL, |
| 933 | "div", "p", "head", NULL, |
| 934 | "noscript", "p", "head", NULL, |
| 935 | "center", "font", "b", "i", "p", "head", NULL, |
| 936 | "a", "a", NULL, |
| 937 | "caption", "p", NULL, |
| 938 | "colgroup", "caption", "colgroup", "col", "p", NULL, |
| 939 | "col", "caption", "col", "p", NULL, |
| 940 | "table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre", |
| 941 | "listing", "xmp", "a", NULL, |
Daniel Veillard | 43dadeb | 2001-04-24 11:23:35 +0000 | [diff] [blame] | 942 | "th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL, |
| 943 | "td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 944 | "tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL, |
| 945 | "thead", "caption", "col", "colgroup", NULL, |
| 946 | "tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead", |
| 947 | "tbody", "p", NULL, |
| 948 | "tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead", |
| 949 | "tfoot", "tbody", "p", NULL, |
| 950 | "optgroup", "option", NULL, |
| 951 | "option", "option", NULL, |
| 952 | "fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", |
| 953 | "pre", "listing", "xmp", "a", NULL, |
| 954 | NULL |
| 955 | }; |
| 956 | |
| 957 | /* |
| 958 | * The list of HTML elements which are supposed not to have |
| 959 | * CDATA content and where a p element will be implied |
| 960 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 961 | * TODO: extend that list by reading the HTML SGML DTD on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 962 | * implied paragraph |
| 963 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 964 | static const char *htmlNoContentElements[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 965 | "html", |
| 966 | "head", |
| 967 | "body", |
| 968 | NULL |
| 969 | }; |
| 970 | |
| 971 | /* |
| 972 | * The list of HTML attributes which are of content %Script; |
| 973 | * NOTE: when adding ones, check htmlIsScriptAttribute() since |
| 974 | * it assumes the name starts with 'on' |
| 975 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 976 | static const char *htmlScriptAttributes[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 977 | "onclick", |
| 978 | "ondblclick", |
| 979 | "onmousedown", |
| 980 | "onmouseup", |
| 981 | "onmouseover", |
| 982 | "onmousemove", |
| 983 | "onmouseout", |
| 984 | "onkeypress", |
| 985 | "onkeydown", |
| 986 | "onkeyup", |
| 987 | "onload", |
| 988 | "onunload", |
| 989 | "onfocus", |
| 990 | "onblur", |
| 991 | "onsubmit", |
| 992 | "onrest", |
| 993 | "onchange", |
| 994 | "onselect" |
| 995 | }; |
| 996 | |
Daniel Veillard | a2bc368 | 2001-05-03 08:27:20 +0000 | [diff] [blame] | 997 | /* |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 998 | * This table is used by the htmlparser to know what to do with |
| 999 | * broken html pages. By assigning different priorities to different |
| 1000 | * elements the parser can decide how to handle extra endtags. |
| 1001 | * Endtags are only allowed to close elements with lower or equal |
| 1002 | * priority. |
| 1003 | */ |
Daniel Veillard | a2bc368 | 2001-05-03 08:27:20 +0000 | [diff] [blame] | 1004 | |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1005 | typedef struct { |
| 1006 | const char *name; |
| 1007 | int priority; |
| 1008 | } elementPriority; |
| 1009 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 1010 | static const elementPriority htmlEndPriority[] = { |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1011 | {"div", 150}, |
| 1012 | {"td", 160}, |
| 1013 | {"th", 160}, |
| 1014 | {"tr", 170}, |
| 1015 | {"thead", 180}, |
| 1016 | {"tbody", 180}, |
| 1017 | {"tfoot", 180}, |
| 1018 | {"table", 190}, |
| 1019 | {"head", 200}, |
| 1020 | {"body", 200}, |
| 1021 | {"html", 220}, |
| 1022 | {NULL, 100} /* Default priority */ |
| 1023 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1024 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1025 | static const char** htmlStartCloseIndex[100]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1026 | static int htmlStartCloseIndexinitialized = 0; |
| 1027 | |
| 1028 | /************************************************************************ |
| 1029 | * * |
| 1030 | * functions to handle HTML specific data * |
| 1031 | * * |
| 1032 | ************************************************************************/ |
| 1033 | |
| 1034 | /** |
| 1035 | * htmlInitAutoClose: |
| 1036 | * |
| 1037 | * Initialize the htmlStartCloseIndex for fast lookup of closing tags names. |
| 1038 | * This is not reentrant. Call xmlInitParser() once before processing in |
| 1039 | * case of use in multithreaded programs. |
| 1040 | */ |
| 1041 | void |
| 1042 | htmlInitAutoClose(void) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1043 | int indx, i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (htmlStartCloseIndexinitialized) return; |
| 1046 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1047 | for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL; |
| 1048 | indx = 0; |
| 1049 | while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) { |
| 1050 | htmlStartCloseIndex[indx++] = &htmlStartClose[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1051 | while (htmlStartClose[i] != NULL) i++; |
| 1052 | i++; |
| 1053 | } |
| 1054 | htmlStartCloseIndexinitialized = 1; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * htmlTagLookup: |
| 1059 | * @tag: The tag name in lowercase |
| 1060 | * |
| 1061 | * Lookup the HTML tag in the ElementTable |
| 1062 | * |
| 1063 | * Returns the related htmlElemDescPtr or NULL if not found. |
| 1064 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1065 | const htmlElemDesc * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1066 | htmlTagLookup(const xmlChar *tag) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1067 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | |
| 1069 | for (i = 0; i < (sizeof(html40ElementTable) / |
| 1070 | sizeof(html40ElementTable[0]));i++) { |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 1071 | if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name)) |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1072 | return((htmlElemDescPtr) &html40ElementTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1073 | } |
| 1074 | return(NULL); |
| 1075 | } |
| 1076 | |
| 1077 | /** |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1078 | * htmlGetEndPriority: |
| 1079 | * @name: The name of the element to look up the priority for. |
| 1080 | * |
| 1081 | * Return value: The "endtag" priority. |
| 1082 | **/ |
| 1083 | static int |
| 1084 | htmlGetEndPriority (const xmlChar *name) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1085 | int i = 0; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1086 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1087 | while ((htmlEndPriority[i].name != NULL) && |
| 1088 | (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name))) |
| 1089 | i++; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1090 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1091 | return(htmlEndPriority[i].priority); |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1094 | |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1095 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1096 | * htmlCheckAutoClose: |
| 1097 | * @newtag: The new tag name |
| 1098 | * @oldtag: The old tag name |
| 1099 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1100 | * Checks whether the new tag is one of the registered valid tags for |
| 1101 | * closing old. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1102 | * Initialize the htmlStartCloseIndex for fast lookup of closing tags names. |
| 1103 | * |
| 1104 | * Returns 0 if no, 1 if yes. |
| 1105 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1106 | static int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1107 | htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag) |
| 1108 | { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1109 | int i, indx; |
| 1110 | const char **closed = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1111 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1112 | if (htmlStartCloseIndexinitialized == 0) |
| 1113 | htmlInitAutoClose(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* inefficient, but not a big deal */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1116 | for (indx = 0; indx < 100; indx++) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1117 | closed = htmlStartCloseIndex[indx]; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1118 | if (closed == NULL) |
| 1119 | return (0); |
| 1120 | if (xmlStrEqual(BAD_CAST * closed, newtag)) |
| 1121 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1124 | i = closed - htmlStartClose; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1125 | i++; |
| 1126 | while (htmlStartClose[i] != NULL) { |
| 1127 | if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1128 | return (1); |
| 1129 | } |
| 1130 | i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1131 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1132 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * htmlAutoCloseOnClose: |
| 1137 | * @ctxt: an HTML parser context |
| 1138 | * @newtag: The new tag name |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1139 | * @force: force the tag closure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1140 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1141 | * The HTML DTD allows an ending tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1143 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1144 | htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
| 1145 | { |
| 1146 | const htmlElemDesc *info; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1147 | int i, priority; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1148 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1149 | priority = htmlGetEndPriority(newtag); |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1150 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1151 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1152 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1153 | if (xmlStrEqual(newtag, ctxt->nameTab[i])) |
| 1154 | break; |
| 1155 | /* |
| 1156 | * A missplaced endtag can only close elements with lower |
| 1157 | * or equal priority, so if we find an element with higher |
| 1158 | * priority before we find an element with |
| 1159 | * matching name, we just ignore this endtag |
| 1160 | */ |
| 1161 | if (htmlGetEndPriority(ctxt->nameTab[i]) > priority) |
| 1162 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1163 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1164 | if (i < 0) |
| 1165 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1166 | |
| 1167 | while (!xmlStrEqual(newtag, ctxt->name)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1168 | info = htmlTagLookup(ctxt->name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1169 | if ((info != NULL) && (info->endTag == 3)) { |
| 1170 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 1171 | "Opening and ending tag mismatch: %s and %s\n", |
Daniel Veillard | 05bcb7e | 2003-10-19 14:26:34 +0000 | [diff] [blame] | 1172 | newtag, ctxt->name); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1173 | } |
| 1174 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1175 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1176 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /** |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1181 | * htmlAutoCloseOnEnd: |
| 1182 | * @ctxt: an HTML parser context |
| 1183 | * |
| 1184 | * Close all remaining tags at the end of the stream |
| 1185 | */ |
| 1186 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1187 | htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt) |
| 1188 | { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1189 | int i; |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1190 | |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1191 | if (ctxt->nameNr == 0) |
| 1192 | return; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1193 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1194 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1195 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1196 | htmlnamePop(ctxt); |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1201 | * htmlAutoClose: |
| 1202 | * @ctxt: an HTML parser context |
| 1203 | * @newtag: The new tag name or NULL |
| 1204 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1205 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | * The list is kept in htmlStartClose array. This function is |
| 1207 | * called when a new tag has been detected and generates the |
| 1208 | * appropriates closes if possible/needed. |
| 1209 | * If newtag is NULL this mean we are at the end of the resource |
| 1210 | * and we should check |
| 1211 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1212 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1213 | htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
| 1214 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1215 | while ((newtag != NULL) && (ctxt->name != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1216 | (htmlCheckAutoClose(newtag, ctxt->name))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1217 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1218 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1219 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | } |
| 1221 | if (newtag == NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1222 | htmlAutoCloseOnEnd(ctxt); |
| 1223 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1224 | } |
| 1225 | while ((newtag == NULL) && (ctxt->name != NULL) && |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1226 | ((xmlStrEqual(ctxt->name, BAD_CAST "head")) || |
| 1227 | (xmlStrEqual(ctxt->name, BAD_CAST "body")) || |
| 1228 | (xmlStrEqual(ctxt->name, BAD_CAST "html")))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1229 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1230 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1231 | htmlnamePop(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1232 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * htmlAutoCloseTag: |
| 1237 | * @doc: the HTML document |
| 1238 | * @name: The tag name |
| 1239 | * @elem: the HTML element |
| 1240 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1241 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1242 | * The list is kept in htmlStartClose array. This function checks |
| 1243 | * if the element or one of it's children would autoclose the |
| 1244 | * given tag. |
| 1245 | * |
| 1246 | * Returns 1 if autoclose, 0 otherwise |
| 1247 | */ |
| 1248 | int |
| 1249 | htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) { |
| 1250 | htmlNodePtr child; |
| 1251 | |
| 1252 | if (elem == NULL) return(1); |
| 1253 | if (xmlStrEqual(name, elem->name)) return(0); |
| 1254 | if (htmlCheckAutoClose(elem->name, name)) return(1); |
| 1255 | child = elem->children; |
| 1256 | while (child != NULL) { |
| 1257 | if (htmlAutoCloseTag(doc, name, child)) return(1); |
| 1258 | child = child->next; |
| 1259 | } |
| 1260 | return(0); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * htmlIsAutoClosed: |
| 1265 | * @doc: the HTML document |
| 1266 | * @elem: the HTML element |
| 1267 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1268 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1269 | * The list is kept in htmlStartClose array. This function checks |
| 1270 | * if a tag is autoclosed by one of it's child |
| 1271 | * |
| 1272 | * Returns 1 if autoclosed, 0 otherwise |
| 1273 | */ |
| 1274 | int |
| 1275 | htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) { |
| 1276 | htmlNodePtr child; |
| 1277 | |
| 1278 | if (elem == NULL) return(1); |
| 1279 | child = elem->children; |
| 1280 | while (child != NULL) { |
| 1281 | if (htmlAutoCloseTag(doc, elem->name, child)) return(1); |
| 1282 | child = child->next; |
| 1283 | } |
| 1284 | return(0); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * htmlCheckImplied: |
| 1289 | * @ctxt: an HTML parser context |
| 1290 | * @newtag: The new tag name |
| 1291 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1292 | * The HTML DTD allows a tag to exists only implicitly |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1293 | * called when a new tag has been detected and generates the |
| 1294 | * appropriates implicit tags if missing |
| 1295 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1296 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1297 | htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) { |
| 1298 | if (!htmlOmittedDefaultValue) |
| 1299 | return; |
| 1300 | if (xmlStrEqual(newtag, BAD_CAST"html")) |
| 1301 | return; |
| 1302 | if (ctxt->nameNr <= 0) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1303 | htmlnamePush(ctxt, BAD_CAST"html"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1304 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1305 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL); |
| 1306 | } |
| 1307 | if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head"))) |
| 1308 | return; |
| 1309 | if ((ctxt->nameNr <= 1) && |
| 1310 | ((xmlStrEqual(newtag, BAD_CAST"script")) || |
| 1311 | (xmlStrEqual(newtag, BAD_CAST"style")) || |
| 1312 | (xmlStrEqual(newtag, BAD_CAST"meta")) || |
| 1313 | (xmlStrEqual(newtag, BAD_CAST"link")) || |
| 1314 | (xmlStrEqual(newtag, BAD_CAST"title")) || |
| 1315 | (xmlStrEqual(newtag, BAD_CAST"base")))) { |
| 1316 | /* |
| 1317 | * dropped OBJECT ... i you put it first BODY will be |
| 1318 | * assumed ! |
| 1319 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1320 | htmlnamePush(ctxt, BAD_CAST"head"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1321 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1322 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL); |
| 1323 | } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) && |
| 1324 | (!xmlStrEqual(newtag, BAD_CAST"frame")) && |
| 1325 | (!xmlStrEqual(newtag, BAD_CAST"frameset"))) { |
| 1326 | int i; |
| 1327 | for (i = 0;i < ctxt->nameNr;i++) { |
| 1328 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) { |
| 1329 | return; |
| 1330 | } |
| 1331 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) { |
| 1332 | return; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1336 | htmlnamePush(ctxt, BAD_CAST"body"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1337 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1338 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL); |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * htmlCheckParagraph |
| 1344 | * @ctxt: an HTML parser context |
| 1345 | * |
| 1346 | * Check whether a p element need to be implied before inserting |
| 1347 | * characters in the current element. |
| 1348 | * |
| 1349 | * Returns 1 if a paragraph has been inserted, 0 if not and -1 |
| 1350 | * in case of error. |
| 1351 | */ |
| 1352 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1353 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1354 | htmlCheckParagraph(htmlParserCtxtPtr ctxt) { |
| 1355 | const xmlChar *tag; |
| 1356 | int i; |
| 1357 | |
| 1358 | if (ctxt == NULL) |
| 1359 | return(-1); |
| 1360 | tag = ctxt->name; |
| 1361 | if (tag == NULL) { |
| 1362 | htmlAutoClose(ctxt, BAD_CAST"p"); |
| 1363 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1364 | htmlnamePush(ctxt, BAD_CAST"p"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1365 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1366 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
| 1367 | return(1); |
| 1368 | } |
| 1369 | if (!htmlOmittedDefaultValue) |
| 1370 | return(0); |
| 1371 | for (i = 0; htmlNoContentElements[i] != NULL; i++) { |
| 1372 | if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1373 | htmlAutoClose(ctxt, BAD_CAST"p"); |
| 1374 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1375 | htmlnamePush(ctxt, BAD_CAST"p"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1376 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1377 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
| 1378 | return(1); |
| 1379 | } |
| 1380 | } |
| 1381 | return(0); |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * htmlIsScriptAttribute: |
| 1386 | * @name: an attribute name |
| 1387 | * |
| 1388 | * Check if an attribute is of content type Script |
| 1389 | * |
| 1390 | * Returns 1 is the attribute is a script 0 otherwise |
| 1391 | */ |
| 1392 | int |
| 1393 | htmlIsScriptAttribute(const xmlChar *name) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1394 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (name == NULL) |
| 1397 | return(0); |
| 1398 | /* |
| 1399 | * all script attributes start with 'on' |
| 1400 | */ |
| 1401 | if ((name[0] != 'o') || (name[1] != 'n')) |
| 1402 | return(0); |
| 1403 | for (i = 0; |
| 1404 | i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]); |
| 1405 | i++) { |
| 1406 | if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i])) |
| 1407 | return(1); |
| 1408 | } |
| 1409 | return(0); |
| 1410 | } |
| 1411 | |
| 1412 | /************************************************************************ |
| 1413 | * * |
| 1414 | * The list of HTML predefined entities * |
| 1415 | * * |
| 1416 | ************************************************************************/ |
| 1417 | |
| 1418 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 1419 | static const htmlEntityDesc html40EntitiesTable[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1420 | /* |
| 1421 | * the 4 absolute ones, plus apostrophe. |
| 1422 | */ |
| 1423 | { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" }, |
| 1424 | { 38, "amp", "ampersand, U+0026 ISOnum" }, |
| 1425 | { 39, "apos", "single quote" }, |
| 1426 | { 60, "lt", "less-than sign, U+003C ISOnum" }, |
| 1427 | { 62, "gt", "greater-than sign, U+003E ISOnum" }, |
| 1428 | |
| 1429 | /* |
| 1430 | * A bunch still in the 128-255 range |
| 1431 | * Replacing them depend really on the charset used. |
| 1432 | */ |
| 1433 | { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" }, |
| 1434 | { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" }, |
| 1435 | { 162, "cent", "cent sign, U+00A2 ISOnum" }, |
| 1436 | { 163, "pound","pound sign, U+00A3 ISOnum" }, |
| 1437 | { 164, "curren","currency sign, U+00A4 ISOnum" }, |
| 1438 | { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" }, |
| 1439 | { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" }, |
| 1440 | { 167, "sect", "section sign, U+00A7 ISOnum" }, |
| 1441 | { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" }, |
| 1442 | { 169, "copy", "copyright sign, U+00A9 ISOnum" }, |
| 1443 | { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" }, |
| 1444 | { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" }, |
| 1445 | { 172, "not", "not sign, U+00AC ISOnum" }, |
| 1446 | { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" }, |
| 1447 | { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" }, |
| 1448 | { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" }, |
| 1449 | { 176, "deg", "degree sign, U+00B0 ISOnum" }, |
| 1450 | { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" }, |
| 1451 | { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" }, |
| 1452 | { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" }, |
| 1453 | { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" }, |
| 1454 | { 181, "micro","micro sign, U+00B5 ISOnum" }, |
| 1455 | { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" }, |
| 1456 | { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" }, |
| 1457 | { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" }, |
| 1458 | { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" }, |
| 1459 | { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" }, |
| 1460 | { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" }, |
| 1461 | { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" }, |
| 1462 | { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" }, |
| 1463 | { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" }, |
| 1464 | { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" }, |
| 1465 | { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" }, |
| 1466 | { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" }, |
| 1467 | { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" }, |
| 1468 | { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" }, |
| 1469 | { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" }, |
| 1470 | { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" }, |
| 1471 | { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" }, |
| 1472 | { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" }, |
| 1473 | { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" }, |
| 1474 | { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" }, |
| 1475 | { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" }, |
| 1476 | { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" }, |
| 1477 | { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" }, |
| 1478 | { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" }, |
| 1479 | { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" }, |
| 1480 | { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" }, |
| 1481 | { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" }, |
| 1482 | { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" }, |
| 1483 | { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" }, |
| 1484 | { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" }, |
| 1485 | { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" }, |
| 1486 | { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" }, |
| 1487 | { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" }, |
| 1488 | { 215, "times","multiplication sign, U+00D7 ISOnum" }, |
| 1489 | { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" }, |
| 1490 | { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" }, |
| 1491 | { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" }, |
| 1492 | { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" }, |
| 1493 | { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" }, |
| 1494 | { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" }, |
| 1495 | { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" }, |
| 1496 | { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" }, |
| 1497 | { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" }, |
| 1498 | { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" }, |
| 1499 | { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" }, |
| 1500 | { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" }, |
| 1501 | { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" }, |
| 1502 | { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" }, |
| 1503 | { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" }, |
| 1504 | { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" }, |
| 1505 | { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" }, |
| 1506 | { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" }, |
| 1507 | { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" }, |
| 1508 | { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" }, |
| 1509 | { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" }, |
| 1510 | { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" }, |
| 1511 | { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" }, |
| 1512 | { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" }, |
| 1513 | { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" }, |
| 1514 | { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" }, |
| 1515 | { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" }, |
| 1516 | { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" }, |
| 1517 | { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" }, |
| 1518 | { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" }, |
| 1519 | { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" }, |
| 1520 | { 247, "divide","division sign, U+00F7 ISOnum" }, |
| 1521 | { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" }, |
| 1522 | { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" }, |
| 1523 | { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" }, |
| 1524 | { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" }, |
| 1525 | { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" }, |
| 1526 | { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" }, |
| 1527 | { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" }, |
| 1528 | { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" }, |
| 1529 | |
| 1530 | { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" }, |
| 1531 | { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" }, |
| 1532 | { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" }, |
| 1533 | { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" }, |
| 1534 | { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" }, |
| 1535 | |
| 1536 | /* |
| 1537 | * Anything below should really be kept as entities references |
| 1538 | */ |
| 1539 | { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" }, |
| 1540 | |
| 1541 | { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" }, |
| 1542 | { 732, "tilde","small tilde, U+02DC ISOdia" }, |
| 1543 | |
| 1544 | { 913, "Alpha","greek capital letter alpha, U+0391" }, |
| 1545 | { 914, "Beta", "greek capital letter beta, U+0392" }, |
| 1546 | { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" }, |
| 1547 | { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" }, |
| 1548 | { 917, "Epsilon","greek capital letter epsilon, U+0395" }, |
| 1549 | { 918, "Zeta", "greek capital letter zeta, U+0396" }, |
| 1550 | { 919, "Eta", "greek capital letter eta, U+0397" }, |
| 1551 | { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" }, |
| 1552 | { 921, "Iota", "greek capital letter iota, U+0399" }, |
| 1553 | { 922, "Kappa","greek capital letter kappa, U+039A" }, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1554 | { 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" }, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1555 | { 924, "Mu", "greek capital letter mu, U+039C" }, |
| 1556 | { 925, "Nu", "greek capital letter nu, U+039D" }, |
| 1557 | { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" }, |
| 1558 | { 927, "Omicron","greek capital letter omicron, U+039F" }, |
| 1559 | { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" }, |
| 1560 | { 929, "Rho", "greek capital letter rho, U+03A1" }, |
| 1561 | { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" }, |
| 1562 | { 932, "Tau", "greek capital letter tau, U+03A4" }, |
| 1563 | { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" }, |
| 1564 | { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" }, |
| 1565 | { 935, "Chi", "greek capital letter chi, U+03A7" }, |
| 1566 | { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" }, |
| 1567 | { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" }, |
| 1568 | |
| 1569 | { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" }, |
| 1570 | { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" }, |
| 1571 | { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" }, |
| 1572 | { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" }, |
| 1573 | { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" }, |
| 1574 | { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" }, |
| 1575 | { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" }, |
| 1576 | { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" }, |
| 1577 | { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" }, |
| 1578 | { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" }, |
| 1579 | { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" }, |
| 1580 | { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" }, |
| 1581 | { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" }, |
| 1582 | { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" }, |
| 1583 | { 959, "omicron","greek small letter omicron, U+03BF NEW" }, |
| 1584 | { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" }, |
| 1585 | { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" }, |
| 1586 | { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" }, |
| 1587 | { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" }, |
| 1588 | { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" }, |
| 1589 | { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" }, |
| 1590 | { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" }, |
| 1591 | { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" }, |
| 1592 | { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" }, |
| 1593 | { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" }, |
| 1594 | { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" }, |
| 1595 | { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" }, |
| 1596 | { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" }, |
| 1597 | |
| 1598 | { 8194, "ensp", "en space, U+2002 ISOpub" }, |
| 1599 | { 8195, "emsp", "em space, U+2003 ISOpub" }, |
| 1600 | { 8201, "thinsp","thin space, U+2009 ISOpub" }, |
| 1601 | { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" }, |
| 1602 | { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" }, |
| 1603 | { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" }, |
| 1604 | { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" }, |
| 1605 | { 8211, "ndash","en dash, U+2013 ISOpub" }, |
| 1606 | { 8212, "mdash","em dash, U+2014 ISOpub" }, |
| 1607 | { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" }, |
| 1608 | { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" }, |
| 1609 | { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" }, |
| 1610 | { 8220, "ldquo","left double quotation mark, U+201C ISOnum" }, |
| 1611 | { 8221, "rdquo","right double quotation mark, U+201D ISOnum" }, |
| 1612 | { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" }, |
| 1613 | { 8224, "dagger","dagger, U+2020 ISOpub" }, |
| 1614 | { 8225, "Dagger","double dagger, U+2021 ISOpub" }, |
| 1615 | |
| 1616 | { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" }, |
| 1617 | { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" }, |
| 1618 | |
| 1619 | { 8240, "permil","per mille sign, U+2030 ISOtech" }, |
| 1620 | |
| 1621 | { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" }, |
| 1622 | { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" }, |
| 1623 | |
| 1624 | { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" }, |
| 1625 | { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" }, |
| 1626 | |
| 1627 | { 8254, "oline","overline = spacing overscore, U+203E NEW" }, |
| 1628 | { 8260, "frasl","fraction slash, U+2044 NEW" }, |
| 1629 | |
| 1630 | { 8364, "euro", "euro sign, U+20AC NEW" }, |
| 1631 | |
| 1632 | { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" }, |
| 1633 | { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" }, |
| 1634 | { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" }, |
| 1635 | { 8482, "trade","trade mark sign, U+2122 ISOnum" }, |
| 1636 | { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" }, |
| 1637 | { 8592, "larr", "leftwards arrow, U+2190 ISOnum" }, |
| 1638 | { 8593, "uarr", "upwards arrow, U+2191 ISOnum" }, |
| 1639 | { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" }, |
| 1640 | { 8595, "darr", "downwards arrow, U+2193 ISOnum" }, |
| 1641 | { 8596, "harr", "left right arrow, U+2194 ISOamsa" }, |
| 1642 | { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" }, |
| 1643 | { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" }, |
| 1644 | { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" }, |
| 1645 | { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" }, |
| 1646 | { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" }, |
| 1647 | { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" }, |
| 1648 | |
| 1649 | { 8704, "forall","for all, U+2200 ISOtech" }, |
| 1650 | { 8706, "part", "partial differential, U+2202 ISOtech" }, |
| 1651 | { 8707, "exist","there exists, U+2203 ISOtech" }, |
| 1652 | { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" }, |
| 1653 | { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" }, |
| 1654 | { 8712, "isin", "element of, U+2208 ISOtech" }, |
| 1655 | { 8713, "notin","not an element of, U+2209 ISOtech" }, |
| 1656 | { 8715, "ni", "contains as member, U+220B ISOtech" }, |
| 1657 | { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" }, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1658 | { 8721, "sum", "n-ary summation, U+2211 ISOamsb" }, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1659 | { 8722, "minus","minus sign, U+2212 ISOtech" }, |
| 1660 | { 8727, "lowast","asterisk operator, U+2217 ISOtech" }, |
| 1661 | { 8730, "radic","square root = radical sign, U+221A ISOtech" }, |
| 1662 | { 8733, "prop", "proportional to, U+221D ISOtech" }, |
| 1663 | { 8734, "infin","infinity, U+221E ISOtech" }, |
| 1664 | { 8736, "ang", "angle, U+2220 ISOamso" }, |
| 1665 | { 8743, "and", "logical and = wedge, U+2227 ISOtech" }, |
| 1666 | { 8744, "or", "logical or = vee, U+2228 ISOtech" }, |
| 1667 | { 8745, "cap", "intersection = cap, U+2229 ISOtech" }, |
| 1668 | { 8746, "cup", "union = cup, U+222A ISOtech" }, |
| 1669 | { 8747, "int", "integral, U+222B ISOtech" }, |
| 1670 | { 8756, "there4","therefore, U+2234 ISOtech" }, |
| 1671 | { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" }, |
| 1672 | { 8773, "cong", "approximately equal to, U+2245 ISOtech" }, |
| 1673 | { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" }, |
| 1674 | { 8800, "ne", "not equal to, U+2260 ISOtech" }, |
| 1675 | { 8801, "equiv","identical to, U+2261 ISOtech" }, |
| 1676 | { 8804, "le", "less-than or equal to, U+2264 ISOtech" }, |
| 1677 | { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" }, |
| 1678 | { 8834, "sub", "subset of, U+2282 ISOtech" }, |
| 1679 | { 8835, "sup", "superset of, U+2283 ISOtech" }, |
| 1680 | { 8836, "nsub", "not a subset of, U+2284 ISOamsn" }, |
| 1681 | { 8838, "sube", "subset of or equal to, U+2286 ISOtech" }, |
| 1682 | { 8839, "supe", "superset of or equal to, U+2287 ISOtech" }, |
| 1683 | { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" }, |
| 1684 | { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" }, |
| 1685 | { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" }, |
| 1686 | { 8901, "sdot", "dot operator, U+22C5 ISOamsb" }, |
| 1687 | { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" }, |
| 1688 | { 8969, "rceil","right ceiling, U+2309 ISOamsc" }, |
| 1689 | { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" }, |
| 1690 | { 8971, "rfloor","right floor, U+230B ISOamsc" }, |
| 1691 | { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" }, |
| 1692 | { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" }, |
| 1693 | { 9674, "loz", "lozenge, U+25CA ISOpub" }, |
| 1694 | |
| 1695 | { 9824, "spades","black spade suit, U+2660 ISOpub" }, |
| 1696 | { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" }, |
| 1697 | { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" }, |
| 1698 | { 9830, "diams","black diamond suit, U+2666 ISOpub" }, |
| 1699 | |
| 1700 | }; |
| 1701 | |
| 1702 | /************************************************************************ |
| 1703 | * * |
| 1704 | * Commodity functions to handle entities * |
| 1705 | * * |
| 1706 | ************************************************************************/ |
| 1707 | |
| 1708 | /* |
| 1709 | * Macro used to grow the current buffer. |
| 1710 | */ |
| 1711 | #define growBuffer(buffer) { \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1712 | xmlChar *tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1713 | buffer##_size *= 2; \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1714 | tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
| 1715 | if (tmp == NULL) { \ |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1716 | htmlErrMemory(ctxt, "growing buffer\n"); \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1717 | xmlFree(buffer); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | return(NULL); \ |
| 1719 | } \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1720 | buffer = tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * htmlEntityLookup: |
| 1725 | * @name: the entity name |
| 1726 | * |
| 1727 | * Lookup the given entity in EntitiesTable |
| 1728 | * |
| 1729 | * TODO: the linear scan is really ugly, an hash table is really needed. |
| 1730 | * |
| 1731 | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
| 1732 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1733 | const htmlEntityDesc * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1734 | htmlEntityLookup(const xmlChar *name) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1735 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1736 | |
| 1737 | for (i = 0;i < (sizeof(html40EntitiesTable)/ |
| 1738 | sizeof(html40EntitiesTable[0]));i++) { |
| 1739 | if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1740 | return((htmlEntityDescPtr) &html40EntitiesTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | return(NULL); |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * htmlEntityValueLookup: |
| 1748 | * @value: the entity's unicode value |
| 1749 | * |
| 1750 | * Lookup the given entity in EntitiesTable |
| 1751 | * |
| 1752 | * TODO: the linear scan is really ugly, an hash table is really needed. |
| 1753 | * |
| 1754 | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
| 1755 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1756 | const htmlEntityDesc * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1757 | htmlEntityValueLookup(unsigned int value) { |
| 1758 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1759 | |
| 1760 | for (i = 0;i < (sizeof(html40EntitiesTable)/ |
| 1761 | sizeof(html40EntitiesTable[0]));i++) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1762 | if (html40EntitiesTable[i].value >= value) { |
| 1763 | if (html40EntitiesTable[i].value > value) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | break; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1765 | return((htmlEntityDescPtr) &html40EntitiesTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1766 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1767 | } |
| 1768 | return(NULL); |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * UTF8ToHtml: |
| 1773 | * @out: a pointer to an array of bytes to store the result |
| 1774 | * @outlen: the length of @out |
| 1775 | * @in: a pointer to an array of UTF-8 chars |
| 1776 | * @inlen: the length of @in |
| 1777 | * |
| 1778 | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
| 1779 | * plus HTML entities block of chars out. |
| 1780 | * |
| 1781 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
| 1782 | * The value of @inlen after return is the number of octets consumed |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1783 | * as the return value is positive, else unpredictable. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1784 | * The value of @outlen after return is the number of octets consumed. |
| 1785 | */ |
| 1786 | int |
| 1787 | UTF8ToHtml(unsigned char* out, int *outlen, |
| 1788 | const unsigned char* in, int *inlen) { |
| 1789 | const unsigned char* processed = in; |
| 1790 | const unsigned char* outend; |
| 1791 | const unsigned char* outstart = out; |
| 1792 | const unsigned char* instart = in; |
| 1793 | const unsigned char* inend; |
| 1794 | unsigned int c, d; |
| 1795 | int trailing; |
| 1796 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1797 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1798 | if (in == NULL) { |
| 1799 | /* |
| 1800 | * initialization nothing to do |
| 1801 | */ |
| 1802 | *outlen = 0; |
| 1803 | *inlen = 0; |
| 1804 | return(0); |
| 1805 | } |
| 1806 | inend = in + (*inlen); |
| 1807 | outend = out + (*outlen); |
| 1808 | while (in < inend) { |
| 1809 | d = *in++; |
| 1810 | if (d < 0x80) { c= d; trailing= 0; } |
| 1811 | else if (d < 0xC0) { |
| 1812 | /* trailing byte in leading position */ |
| 1813 | *outlen = out - outstart; |
| 1814 | *inlen = processed - instart; |
| 1815 | return(-2); |
| 1816 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 1817 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 1818 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 1819 | else { |
| 1820 | /* no chance for this in Ascii */ |
| 1821 | *outlen = out - outstart; |
| 1822 | *inlen = processed - instart; |
| 1823 | return(-2); |
| 1824 | } |
| 1825 | |
| 1826 | if (inend - in < trailing) { |
| 1827 | break; |
| 1828 | } |
| 1829 | |
| 1830 | for ( ; trailing; trailing--) { |
| 1831 | if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80)) |
| 1832 | break; |
| 1833 | c <<= 6; |
| 1834 | c |= d & 0x3F; |
| 1835 | } |
| 1836 | |
| 1837 | /* assertion: c is a single UTF-4 value */ |
| 1838 | if (c < 0x80) { |
| 1839 | if (out + 1 >= outend) |
| 1840 | break; |
| 1841 | *out++ = c; |
| 1842 | } else { |
| 1843 | int len; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1844 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * Try to lookup a predefined HTML entity for it |
| 1848 | */ |
| 1849 | |
| 1850 | ent = htmlEntityValueLookup(c); |
| 1851 | if (ent == NULL) { |
| 1852 | /* no chance for this in Ascii */ |
| 1853 | *outlen = out - outstart; |
| 1854 | *inlen = processed - instart; |
| 1855 | return(-2); |
| 1856 | } |
| 1857 | len = strlen(ent->name); |
| 1858 | if (out + 2 + len >= outend) |
| 1859 | break; |
| 1860 | *out++ = '&'; |
| 1861 | memcpy(out, ent->name, len); |
| 1862 | out += len; |
| 1863 | *out++ = ';'; |
| 1864 | } |
| 1865 | processed = in; |
| 1866 | } |
| 1867 | *outlen = out - outstart; |
| 1868 | *inlen = processed - instart; |
| 1869 | return(0); |
| 1870 | } |
| 1871 | |
| 1872 | /** |
| 1873 | * htmlEncodeEntities: |
| 1874 | * @out: a pointer to an array of bytes to store the result |
| 1875 | * @outlen: the length of @out |
| 1876 | * @in: a pointer to an array of UTF-8 chars |
| 1877 | * @inlen: the length of @in |
| 1878 | * @quoteChar: the quote character to escape (' or ") or zero. |
| 1879 | * |
| 1880 | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
| 1881 | * plus HTML entities block of chars out. |
| 1882 | * |
| 1883 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
| 1884 | * The value of @inlen after return is the number of octets consumed |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1885 | * as the return value is positive, else unpredictable. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | * The value of @outlen after return is the number of octets consumed. |
| 1887 | */ |
| 1888 | int |
| 1889 | htmlEncodeEntities(unsigned char* out, int *outlen, |
| 1890 | const unsigned char* in, int *inlen, int quoteChar) { |
| 1891 | const unsigned char* processed = in; |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1892 | const unsigned char* outend; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1893 | const unsigned char* outstart = out; |
| 1894 | const unsigned char* instart = in; |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1895 | const unsigned char* inend; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1896 | unsigned int c, d; |
| 1897 | int trailing; |
| 1898 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1899 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) |
| 1900 | return(-1); |
| 1901 | outend = out + (*outlen); |
| 1902 | inend = in + (*inlen); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | while (in < inend) { |
| 1904 | d = *in++; |
| 1905 | if (d < 0x80) { c= d; trailing= 0; } |
| 1906 | else if (d < 0xC0) { |
| 1907 | /* trailing byte in leading position */ |
| 1908 | *outlen = out - outstart; |
| 1909 | *inlen = processed - instart; |
| 1910 | return(-2); |
| 1911 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 1912 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 1913 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 1914 | else { |
| 1915 | /* no chance for this in Ascii */ |
| 1916 | *outlen = out - outstart; |
| 1917 | *inlen = processed - instart; |
| 1918 | return(-2); |
| 1919 | } |
| 1920 | |
| 1921 | if (inend - in < trailing) |
| 1922 | break; |
| 1923 | |
| 1924 | while (trailing--) { |
| 1925 | if (((d= *in++) & 0xC0) != 0x80) { |
| 1926 | *outlen = out - outstart; |
| 1927 | *inlen = processed - instart; |
| 1928 | return(-2); |
| 1929 | } |
| 1930 | c <<= 6; |
| 1931 | c |= d & 0x3F; |
| 1932 | } |
| 1933 | |
| 1934 | /* assertion: c is a single UTF-4 value */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1935 | if ((c < 0x80) && (c != (unsigned int) quoteChar) && |
| 1936 | (c != '&') && (c != '<') && (c != '>')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1937 | if (out >= outend) |
| 1938 | break; |
| 1939 | *out++ = c; |
| 1940 | } else { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1941 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1942 | const char *cp; |
| 1943 | char nbuf[16]; |
| 1944 | int len; |
| 1945 | |
| 1946 | /* |
| 1947 | * Try to lookup a predefined HTML entity for it |
| 1948 | */ |
| 1949 | ent = htmlEntityValueLookup(c); |
| 1950 | if (ent == NULL) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1951 | snprintf(nbuf, sizeof(nbuf), "#%u", c); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1952 | cp = nbuf; |
| 1953 | } |
| 1954 | else |
| 1955 | cp = ent->name; |
| 1956 | len = strlen(cp); |
| 1957 | if (out + 2 + len > outend) |
| 1958 | break; |
| 1959 | *out++ = '&'; |
| 1960 | memcpy(out, cp, len); |
| 1961 | out += len; |
| 1962 | *out++ = ';'; |
| 1963 | } |
| 1964 | processed = in; |
| 1965 | } |
| 1966 | *outlen = out - outstart; |
| 1967 | *inlen = processed - instart; |
| 1968 | return(0); |
| 1969 | } |
| 1970 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1971 | /************************************************************************ |
| 1972 | * * |
| 1973 | * Commodity functions to handle streams * |
| 1974 | * * |
| 1975 | ************************************************************************/ |
| 1976 | |
| 1977 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1978 | * htmlNewInputStream: |
| 1979 | * @ctxt: an HTML parser context |
| 1980 | * |
| 1981 | * Create a new input stream structure |
| 1982 | * Returns the new input stream or NULL |
| 1983 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1984 | static htmlParserInputPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1985 | htmlNewInputStream(htmlParserCtxtPtr ctxt) { |
| 1986 | htmlParserInputPtr input; |
| 1987 | |
| 1988 | input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput)); |
| 1989 | if (input == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1990 | htmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1991 | return(NULL); |
| 1992 | } |
| 1993 | memset(input, 0, sizeof(htmlParserInput)); |
| 1994 | input->filename = NULL; |
| 1995 | input->directory = NULL; |
| 1996 | input->base = NULL; |
| 1997 | input->cur = NULL; |
| 1998 | input->buf = NULL; |
| 1999 | input->line = 1; |
| 2000 | input->col = 1; |
| 2001 | input->buf = NULL; |
| 2002 | input->free = NULL; |
| 2003 | input->version = NULL; |
| 2004 | input->consumed = 0; |
| 2005 | input->length = 0; |
| 2006 | return(input); |
| 2007 | } |
| 2008 | |
| 2009 | |
| 2010 | /************************************************************************ |
| 2011 | * * |
| 2012 | * Commodity functions, cleanup needed ? * |
| 2013 | * * |
| 2014 | ************************************************************************/ |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2015 | /* |
| 2016 | * all tags allowing pc data from the html 4.01 loose dtd |
| 2017 | * NOTE: it might be more apropriate to integrate this information |
| 2018 | * into the html40ElementTable array but I don't want to risk any |
| 2019 | * binary incomptibility |
| 2020 | */ |
| 2021 | static const char *allowPCData[] = { |
| 2022 | "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big", |
| 2023 | "blockquote", "body", "button", "caption", "center", "cite", "code", |
| 2024 | "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2", |
| 2025 | "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend", |
| 2026 | "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp", |
| 2027 | "small", "span", "strike", "strong", "td", "th", "tt", "u", "var" |
| 2028 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2029 | |
| 2030 | /** |
| 2031 | * areBlanks: |
| 2032 | * @ctxt: an HTML parser context |
| 2033 | * @str: a xmlChar * |
| 2034 | * @len: the size of @str |
| 2035 | * |
| 2036 | * Is this a sequence of blank chars that one can ignore ? |
| 2037 | * |
| 2038 | * Returns 1 if ignorable 0 otherwise. |
| 2039 | */ |
| 2040 | |
| 2041 | static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) { |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2042 | unsigned int i; |
| 2043 | int j; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2044 | xmlNodePtr lastChild; |
| 2045 | |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2046 | for (j = 0;j < len;j++) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2047 | if (!(IS_BLANK_CH(str[j]))) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (CUR == 0) return(1); |
| 2050 | if (CUR != '<') return(0); |
| 2051 | if (ctxt->name == NULL) |
| 2052 | return(1); |
| 2053 | if (xmlStrEqual(ctxt->name, BAD_CAST"html")) |
| 2054 | return(1); |
| 2055 | if (xmlStrEqual(ctxt->name, BAD_CAST"head")) |
| 2056 | return(1); |
| 2057 | if (xmlStrEqual(ctxt->name, BAD_CAST"body")) |
| 2058 | return(1); |
| 2059 | if (ctxt->node == NULL) return(0); |
| 2060 | lastChild = xmlGetLastChild(ctxt->node); |
Daniel Veillard | 18a6509 | 2004-05-11 15:57:42 +0000 | [diff] [blame] | 2061 | while ((lastChild) && (lastChild->type == XML_COMMENT_NODE)) |
| 2062 | lastChild = lastChild->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2063 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2064 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2065 | (ctxt->node->content != NULL)) return(0); |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2066 | /* keep ws in constructs like ...<b> </b>... |
| 2067 | for all tags "b" allowing PCDATA */ |
| 2068 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
| 2069 | if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) { |
| 2070 | return(0); |
| 2071 | } |
| 2072 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2073 | } else if (xmlNodeIsText(lastChild)) { |
| 2074 | return(0); |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2075 | } else { |
| 2076 | /* keep ws in constructs like <p><b>xy</b> <i>z</i><p> |
| 2077 | for all tags "p" allowing PCDATA */ |
| 2078 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
| 2079 | if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) { |
| 2080 | return(0); |
| 2081 | } |
| 2082 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2083 | } |
| 2084 | return(1); |
| 2085 | } |
| 2086 | |
| 2087 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | * htmlNewDocNoDtD: |
| 2089 | * @URI: URI for the dtd, or NULL |
| 2090 | * @ExternalID: the external ID of the DTD, or NULL |
| 2091 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2092 | * Creates a new HTML document without a DTD node if @URI and @ExternalID |
| 2093 | * are NULL |
| 2094 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2095 | * Returns a new document, do not initialize the DTD if not provided |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2096 | */ |
| 2097 | htmlDocPtr |
| 2098 | htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) { |
| 2099 | xmlDocPtr cur; |
| 2100 | |
| 2101 | /* |
| 2102 | * Allocate a new document and fill the fields. |
| 2103 | */ |
| 2104 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 2105 | if (cur == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2106 | htmlErrMemory(NULL, "HTML document creation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2107 | return(NULL); |
| 2108 | } |
| 2109 | memset(cur, 0, sizeof(xmlDoc)); |
| 2110 | |
| 2111 | cur->type = XML_HTML_DOCUMENT_NODE; |
| 2112 | cur->version = NULL; |
| 2113 | cur->intSubset = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2114 | cur->doc = cur; |
| 2115 | cur->name = NULL; |
| 2116 | cur->children = NULL; |
| 2117 | cur->extSubset = NULL; |
| 2118 | cur->oldNs = NULL; |
| 2119 | cur->encoding = NULL; |
| 2120 | cur->standalone = 1; |
| 2121 | cur->compression = 0; |
| 2122 | cur->ids = NULL; |
| 2123 | cur->refs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2124 | cur->_private = NULL; |
Daniel Veillard | 7cc2357 | 2004-07-29 11:20:30 +0000 | [diff] [blame] | 2125 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | b6b0fd8 | 2001-10-22 12:31:11 +0000 | [diff] [blame] | 2126 | if ((ExternalID != NULL) || |
| 2127 | (URI != NULL)) |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 2128 | xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2129 | return(cur); |
| 2130 | } |
| 2131 | |
| 2132 | /** |
| 2133 | * htmlNewDoc: |
| 2134 | * @URI: URI for the dtd, or NULL |
| 2135 | * @ExternalID: the external ID of the DTD, or NULL |
| 2136 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2137 | * Creates a new HTML document |
| 2138 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2139 | * Returns a new document |
| 2140 | */ |
| 2141 | htmlDocPtr |
| 2142 | htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) { |
| 2143 | if ((URI == NULL) && (ExternalID == NULL)) |
| 2144 | return(htmlNewDocNoDtD( |
Daniel Veillard | 6426935 | 2001-05-04 17:52:34 +0000 | [diff] [blame] | 2145 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd", |
| 2146 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2147 | |
| 2148 | return(htmlNewDocNoDtD(URI, ExternalID)); |
| 2149 | } |
| 2150 | |
| 2151 | |
| 2152 | /************************************************************************ |
| 2153 | * * |
| 2154 | * The parser itself * |
| 2155 | * Relates to http://www.w3.org/TR/html40 * |
| 2156 | * * |
| 2157 | ************************************************************************/ |
| 2158 | |
| 2159 | /************************************************************************ |
| 2160 | * * |
| 2161 | * The parser itself * |
| 2162 | * * |
| 2163 | ************************************************************************/ |
| 2164 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2165 | static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt); |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2166 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2167 | /** |
| 2168 | * htmlParseHTMLName: |
| 2169 | * @ctxt: an HTML parser context |
| 2170 | * |
| 2171 | * parse an HTML tag or attribute name, note that we convert it to lowercase |
| 2172 | * since HTML names are not case-sensitive. |
| 2173 | * |
| 2174 | * Returns the Tag Name parsed or NULL |
| 2175 | */ |
| 2176 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2177 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2178 | htmlParseHTMLName(htmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | int i = 0; |
| 2180 | xmlChar loc[HTML_PARSER_BUFFER_SIZE]; |
| 2181 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 2182 | if (!IS_ASCII_LETTER(CUR) && (CUR != '_') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2183 | (CUR != ':')) return(NULL); |
| 2184 | |
| 2185 | while ((i < HTML_PARSER_BUFFER_SIZE) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 2186 | ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2187 | (CUR == ':') || (CUR == '-') || (CUR == '_'))) { |
| 2188 | if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20; |
| 2189 | else loc[i] = CUR; |
| 2190 | i++; |
| 2191 | |
| 2192 | NEXT; |
| 2193 | } |
| 2194 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2195 | return(xmlDictLookup(ctxt->dict, loc, i)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /** |
| 2199 | * htmlParseName: |
| 2200 | * @ctxt: an HTML parser context |
| 2201 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2202 | * parse an HTML name, this routine is case sensitive. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2203 | * |
| 2204 | * Returns the Name parsed or NULL |
| 2205 | */ |
| 2206 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2207 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2208 | htmlParseName(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2209 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2210 | const xmlChar *ret; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2211 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2212 | |
| 2213 | GROW; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2214 | |
| 2215 | /* |
| 2216 | * Accelerator for simple ASCII names |
| 2217 | */ |
| 2218 | in = ctxt->input->cur; |
| 2219 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2220 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2221 | (*in == '_') || (*in == ':')) { |
| 2222 | in++; |
| 2223 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2224 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2225 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 2226 | (*in == '_') || (*in == '-') || |
| 2227 | (*in == ':') || (*in == '.')) |
| 2228 | in++; |
| 2229 | if ((*in > 0) && (*in < 0x80)) { |
| 2230 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2231 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2232 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2233 | ctxt->nbChars += count; |
| 2234 | ctxt->input->col += count; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2235 | return(ret); |
| 2236 | } |
| 2237 | } |
| 2238 | return(htmlParseNameComplex(ctxt)); |
| 2239 | } |
| 2240 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2241 | static const xmlChar * |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2242 | htmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2243 | int len = 0, l; |
| 2244 | int c; |
| 2245 | int count = 0; |
| 2246 | |
| 2247 | /* |
| 2248 | * Handler for more complex cases |
| 2249 | */ |
| 2250 | GROW; |
| 2251 | c = CUR_CHAR(l); |
| 2252 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 2253 | (!IS_LETTER(c) && (c != '_') && |
| 2254 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2255 | return(NULL); |
| 2256 | } |
| 2257 | |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2258 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 2259 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 2260 | (c == '.') || (c == '-') || |
| 2261 | (c == '_') || (c == ':') || |
| 2262 | (IS_COMBINING(c)) || |
| 2263 | (IS_EXTENDER(c)))) { |
| 2264 | if (count++ > 100) { |
| 2265 | count = 0; |
| 2266 | GROW; |
| 2267 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2268 | len += l; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2269 | NEXTL(l); |
| 2270 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2271 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2272 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2275 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | /** |
| 2277 | * htmlParseHTMLAttribute: |
| 2278 | * @ctxt: an HTML parser context |
| 2279 | * @stop: a char stop value |
| 2280 | * |
| 2281 | * parse an HTML attribute value till the stop (quote), if |
| 2282 | * stop is 0 then it stops at the first space |
| 2283 | * |
| 2284 | * Returns the attribute parsed or NULL |
| 2285 | */ |
| 2286 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2287 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) { |
| 2289 | xmlChar *buffer = NULL; |
| 2290 | int buffer_size = 0; |
| 2291 | xmlChar *out = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2292 | const xmlChar *name = NULL; |
| 2293 | const xmlChar *cur = NULL; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2294 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2295 | |
| 2296 | /* |
| 2297 | * allocate a translation buffer. |
| 2298 | */ |
| 2299 | buffer_size = HTML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2300 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2301 | if (buffer == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2302 | htmlErrMemory(ctxt, "buffer allocation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | return(NULL); |
| 2304 | } |
| 2305 | out = buffer; |
| 2306 | |
| 2307 | /* |
| 2308 | * Ok loop until we reach one of the ending chars |
| 2309 | */ |
Daniel Veillard | 957fdcf | 2001-11-06 22:50:19 +0000 | [diff] [blame] | 2310 | while ((CUR != 0) && (CUR != stop)) { |
| 2311 | if ((stop == 0) && (CUR == '>')) break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2312 | if ((stop == 0) && (IS_BLANK_CH(CUR))) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | if (CUR == '&') { |
| 2314 | if (NXT(1) == '#') { |
| 2315 | unsigned int c; |
| 2316 | int bits; |
| 2317 | |
| 2318 | c = htmlParseCharRef(ctxt); |
| 2319 | if (c < 0x80) |
| 2320 | { *out++ = c; bits= -6; } |
| 2321 | else if (c < 0x800) |
| 2322 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2323 | else if (c < 0x10000) |
| 2324 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 2325 | else |
| 2326 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 2327 | |
| 2328 | for ( ; bits >= 0; bits-= 6) { |
| 2329 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2330 | } |
Daniel Veillard | ce02dbc | 2002-10-22 19:14:58 +0000 | [diff] [blame] | 2331 | |
| 2332 | if (out - buffer > buffer_size - 100) { |
| 2333 | int indx = out - buffer; |
| 2334 | |
| 2335 | growBuffer(buffer); |
| 2336 | out = &buffer[indx]; |
| 2337 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2338 | } else { |
| 2339 | ent = htmlParseEntityRef(ctxt, &name); |
| 2340 | if (name == NULL) { |
| 2341 | *out++ = '&'; |
| 2342 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2343 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2344 | |
| 2345 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2346 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2347 | } |
| 2348 | } else if (ent == NULL) { |
| 2349 | *out++ = '&'; |
| 2350 | cur = name; |
| 2351 | while (*cur != 0) { |
| 2352 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2353 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2354 | |
| 2355 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2356 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2357 | } |
| 2358 | *out++ = *cur++; |
| 2359 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | } else { |
| 2361 | unsigned int c; |
| 2362 | int bits; |
| 2363 | |
| 2364 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2365 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2366 | |
| 2367 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2368 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | } |
| 2370 | c = (xmlChar)ent->value; |
| 2371 | if (c < 0x80) |
| 2372 | { *out++ = c; bits= -6; } |
| 2373 | else if (c < 0x800) |
| 2374 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2375 | else if (c < 0x10000) |
| 2376 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 2377 | else |
| 2378 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 2379 | |
| 2380 | for ( ; bits >= 0; bits-= 6) { |
| 2381 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2382 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | } else { |
| 2386 | unsigned int c; |
| 2387 | int bits, l; |
| 2388 | |
| 2389 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2390 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | |
| 2392 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2393 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2394 | } |
| 2395 | c = CUR_CHAR(l); |
| 2396 | if (c < 0x80) |
| 2397 | { *out++ = c; bits= -6; } |
| 2398 | else if (c < 0x800) |
| 2399 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2400 | else if (c < 0x10000) |
| 2401 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 2402 | else |
| 2403 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 2404 | |
| 2405 | for ( ; bits >= 0; bits-= 6) { |
| 2406 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2407 | } |
| 2408 | NEXT; |
| 2409 | } |
| 2410 | } |
| 2411 | *out++ = 0; |
| 2412 | return(buffer); |
| 2413 | } |
| 2414 | |
| 2415 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2416 | * htmlParseEntityRef: |
| 2417 | * @ctxt: an HTML parser context |
| 2418 | * @str: location to store the entity name |
| 2419 | * |
| 2420 | * parse an HTML ENTITY references |
| 2421 | * |
| 2422 | * [68] EntityRef ::= '&' Name ';' |
| 2423 | * |
| 2424 | * Returns the associated htmlEntityDescPtr if found, or NULL otherwise, |
| 2425 | * if non-NULL *str will have to be freed by the caller. |
| 2426 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2427 | const htmlEntityDesc * |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2428 | htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2429 | const xmlChar *name; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2430 | const htmlEntityDesc * ent = NULL; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2431 | |
| 2432 | if (str != NULL) *str = NULL; |
| 2433 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2434 | |
| 2435 | if (CUR == '&') { |
| 2436 | NEXT; |
| 2437 | name = htmlParseName(ctxt); |
| 2438 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2439 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 2440 | "htmlParseEntityRef: no name\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2441 | } else { |
| 2442 | GROW; |
| 2443 | if (CUR == ';') { |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2444 | if (str != NULL) |
| 2445 | *str = name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2446 | |
| 2447 | /* |
| 2448 | * Lookup the entity in the table. |
| 2449 | */ |
| 2450 | ent = htmlEntityLookup(name); |
| 2451 | if (ent != NULL) /* OK that's ugly !!! */ |
| 2452 | NEXT; |
| 2453 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2454 | htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, |
| 2455 | "htmlParseEntityRef: expecting ';'\n", |
| 2456 | NULL, NULL); |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2457 | if (str != NULL) |
| 2458 | *str = name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | return(ent); |
| 2463 | } |
| 2464 | |
| 2465 | /** |
| 2466 | * htmlParseAttValue: |
| 2467 | * @ctxt: an HTML parser context |
| 2468 | * |
| 2469 | * parse a value for an attribute |
| 2470 | * Note: the parser won't do substitution of entities here, this |
| 2471 | * will be handled later in xmlStringGetNodeList, unless it was |
| 2472 | * asked for ctxt->replaceEntities != 0 |
| 2473 | * |
| 2474 | * Returns the AttValue parsed or NULL. |
| 2475 | */ |
| 2476 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2477 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2478 | htmlParseAttValue(htmlParserCtxtPtr ctxt) { |
| 2479 | xmlChar *ret = NULL; |
| 2480 | |
| 2481 | if (CUR == '"') { |
| 2482 | NEXT; |
| 2483 | ret = htmlParseHTMLAttribute(ctxt, '"'); |
| 2484 | if (CUR != '"') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2485 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2486 | "AttValue: \" expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2487 | } else |
| 2488 | NEXT; |
| 2489 | } else if (CUR == '\'') { |
| 2490 | NEXT; |
| 2491 | ret = htmlParseHTMLAttribute(ctxt, '\''); |
| 2492 | if (CUR != '\'') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2493 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2494 | "AttValue: ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2495 | } else |
| 2496 | NEXT; |
| 2497 | } else { |
| 2498 | /* |
| 2499 | * That's an HTMLism, the attribute value may not be quoted |
| 2500 | */ |
| 2501 | ret = htmlParseHTMLAttribute(ctxt, 0); |
| 2502 | if (ret == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2503 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 2504 | "AttValue: no value found\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | return(ret); |
| 2508 | } |
| 2509 | |
| 2510 | /** |
| 2511 | * htmlParseSystemLiteral: |
| 2512 | * @ctxt: an HTML parser context |
| 2513 | * |
| 2514 | * parse an HTML Literal |
| 2515 | * |
| 2516 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 2517 | * |
| 2518 | * Returns the SystemLiteral parsed or NULL |
| 2519 | */ |
| 2520 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2521 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2522 | htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) { |
| 2523 | const xmlChar *q; |
| 2524 | xmlChar *ret = NULL; |
| 2525 | |
| 2526 | if (CUR == '"') { |
| 2527 | NEXT; |
| 2528 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2529 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2530 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2531 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2532 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2533 | "Unfinished SystemLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2534 | } else { |
| 2535 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2536 | NEXT; |
| 2537 | } |
| 2538 | } else if (CUR == '\'') { |
| 2539 | NEXT; |
| 2540 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2541 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2542 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2543 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2544 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2545 | "Unfinished SystemLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2546 | } else { |
| 2547 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2548 | NEXT; |
| 2549 | } |
| 2550 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2551 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
| 2552 | " or ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | return(ret); |
| 2556 | } |
| 2557 | |
| 2558 | /** |
| 2559 | * htmlParsePubidLiteral: |
| 2560 | * @ctxt: an HTML parser context |
| 2561 | * |
| 2562 | * parse an HTML public literal |
| 2563 | * |
| 2564 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 2565 | * |
| 2566 | * Returns the PubidLiteral parsed or NULL. |
| 2567 | */ |
| 2568 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2569 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2570 | htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) { |
| 2571 | const xmlChar *q; |
| 2572 | xmlChar *ret = NULL; |
| 2573 | /* |
| 2574 | * Name ::= (Letter | '_') (NameChar)* |
| 2575 | */ |
| 2576 | if (CUR == '"') { |
| 2577 | NEXT; |
| 2578 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2579 | while (IS_PUBIDCHAR_CH(CUR)) NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2580 | if (CUR != '"') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2581 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2582 | "Unfinished PubidLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | } else { |
| 2584 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2585 | NEXT; |
| 2586 | } |
| 2587 | } else if (CUR == '\'') { |
| 2588 | NEXT; |
| 2589 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2590 | while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2591 | NEXT; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 2592 | if (CUR != '\'') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2593 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2594 | "Unfinished PubidLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2595 | } else { |
| 2596 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2597 | NEXT; |
| 2598 | } |
| 2599 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2600 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
| 2601 | "PubidLiteral \" or ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | return(ret); |
| 2605 | } |
| 2606 | |
| 2607 | /** |
| 2608 | * htmlParseScript: |
| 2609 | * @ctxt: an HTML parser context |
| 2610 | * |
| 2611 | * parse the content of an HTML SCRIPT or STYLE element |
| 2612 | * http://www.w3.org/TR/html4/sgml/dtd.html#Script |
| 2613 | * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet |
| 2614 | * http://www.w3.org/TR/html4/types.html#type-script |
| 2615 | * http://www.w3.org/TR/html4/types.html#h-6.15 |
| 2616 | * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1 |
| 2617 | * |
| 2618 | * Script data ( %Script; in the DTD) can be the content of the SCRIPT |
| 2619 | * element and the value of intrinsic event attributes. User agents must |
| 2620 | * not evaluate script data as HTML markup but instead must pass it on as |
| 2621 | * data to a script engine. |
| 2622 | * NOTES: |
| 2623 | * - The content is passed like CDATA |
| 2624 | * - the attributes for style and scripting "onXXX" are also described |
| 2625 | * as CDATA but SGML allows entities references in attributes so their |
| 2626 | * processing is identical as other attributes |
| 2627 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2628 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2629 | htmlParseScript(htmlParserCtxtPtr ctxt) { |
| 2630 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 1]; |
| 2631 | int nbchar = 0; |
| 2632 | xmlChar cur; |
| 2633 | |
| 2634 | SHRINK; |
| 2635 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2636 | while (IS_CHAR_CH(cur)) { |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 2637 | if ((cur == '<') && (NXT(1) == '!') && (NXT(2) == '-') && |
| 2638 | (NXT(3) == '-')) { |
| 2639 | if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2640 | if (ctxt->sax->cdataBlock!= NULL) { |
| 2641 | /* |
| 2642 | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
| 2643 | */ |
| 2644 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 2645 | } else if (ctxt->sax->characters != NULL) { |
| 2646 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | nbchar = 0; |
| 2650 | htmlParseComment(ctxt); |
| 2651 | cur = CUR; |
| 2652 | continue; |
| 2653 | } else if ((cur == '<') && (NXT(1) == '/')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2654 | /* |
| 2655 | * One should break here, the specification is clear: |
| 2656 | * Authors should therefore escape "</" within the content. |
| 2657 | * Escape mechanisms are specific to each scripting or |
| 2658 | * style sheet language. |
| 2659 | */ |
| 2660 | if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) || |
| 2661 | ((NXT(2) >= 'a') && (NXT(2) <= 'z'))) |
| 2662 | break; /* while */ |
| 2663 | } |
| 2664 | buf[nbchar++] = cur; |
| 2665 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
| 2666 | if (ctxt->sax->cdataBlock!= NULL) { |
| 2667 | /* |
| 2668 | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
| 2669 | */ |
| 2670 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 2671 | } else if (ctxt->sax->characters != NULL) { |
| 2672 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2673 | } |
| 2674 | nbchar = 0; |
| 2675 | } |
| 2676 | NEXT; |
| 2677 | cur = CUR; |
| 2678 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2679 | if (!(IS_CHAR_CH(cur))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2680 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2681 | "Invalid char in CDATA 0x%X\n", cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2682 | NEXT; |
| 2683 | } |
| 2684 | |
| 2685 | if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2686 | if (ctxt->sax->cdataBlock!= NULL) { |
| 2687 | /* |
| 2688 | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
| 2689 | */ |
| 2690 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 2691 | } else if (ctxt->sax->characters != NULL) { |
| 2692 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | |
| 2698 | /** |
| 2699 | * htmlParseCharData: |
| 2700 | * @ctxt: an HTML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | * |
| 2702 | * parse a CharData section. |
| 2703 | * if we are within a CDATA section ']]>' marks an end of section. |
| 2704 | * |
| 2705 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 2706 | */ |
| 2707 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2708 | static void |
| 2709 | htmlParseCharData(htmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 2711 | int nbchar = 0; |
| 2712 | int cur, l; |
| 2713 | |
| 2714 | SHRINK; |
| 2715 | cur = CUR_CHAR(l); |
| 2716 | while (((cur != '<') || (ctxt->token == '<')) && |
| 2717 | ((cur != '&') || (ctxt->token == '&')) && |
| 2718 | (IS_CHAR(cur))) { |
| 2719 | COPY_BUF(l,buf,nbchar,cur); |
| 2720 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
| 2721 | /* |
| 2722 | * Ok the segment is to be consumed as chars. |
| 2723 | */ |
| 2724 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2725 | if (areBlanks(ctxt, buf, nbchar)) { |
| 2726 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 2727 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 2728 | buf, nbchar); |
| 2729 | } else { |
| 2730 | htmlCheckParagraph(ctxt); |
| 2731 | if (ctxt->sax->characters != NULL) |
| 2732 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 2733 | } |
| 2734 | } |
| 2735 | nbchar = 0; |
| 2736 | } |
| 2737 | NEXTL(l); |
| 2738 | cur = CUR_CHAR(l); |
Daniel Veillard | 358a989 | 2003-02-04 15:22:32 +0000 | [diff] [blame] | 2739 | if (cur == 0) { |
| 2740 | SHRINK; |
| 2741 | GROW; |
| 2742 | cur = CUR_CHAR(l); |
| 2743 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2744 | } |
| 2745 | if (nbchar != 0) { |
| 2746 | /* |
| 2747 | * Ok the segment is to be consumed as chars. |
| 2748 | */ |
| 2749 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2750 | if (areBlanks(ctxt, buf, nbchar)) { |
| 2751 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 2752 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 2753 | } else { |
| 2754 | htmlCheckParagraph(ctxt); |
| 2755 | if (ctxt->sax->characters != NULL) |
| 2756 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 2757 | } |
| 2758 | } |
Daniel Veillard | 7cc95c0 | 2001-10-17 15:45:12 +0000 | [diff] [blame] | 2759 | } else { |
| 2760 | /* |
| 2761 | * Loop detection |
| 2762 | */ |
| 2763 | if (cur == 0) |
| 2764 | ctxt->instate = XML_PARSER_EOF; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | /** |
| 2769 | * htmlParseExternalID: |
| 2770 | * @ctxt: an HTML parser context |
| 2771 | * @publicID: a xmlChar** receiving PubidLiteral |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2772 | * |
| 2773 | * Parse an External ID or a Public ID |
| 2774 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2775 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 2776 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 2777 | * |
| 2778 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 2779 | * |
| 2780 | * Returns the function returns SystemLiteral and in the second |
| 2781 | * case publicID receives PubidLiteral, is strict is off |
| 2782 | * it is possible to return NULL and have publicID set. |
| 2783 | */ |
| 2784 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2785 | static xmlChar * |
| 2786 | htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2787 | xmlChar *URI = NULL; |
| 2788 | |
| 2789 | if ((UPPER == 'S') && (UPP(1) == 'Y') && |
| 2790 | (UPP(2) == 'S') && (UPP(3) == 'T') && |
| 2791 | (UPP(4) == 'E') && (UPP(5) == 'M')) { |
| 2792 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2793 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2794 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 2795 | "Space required after 'SYSTEM'\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2796 | } |
| 2797 | SKIP_BLANKS; |
| 2798 | URI = htmlParseSystemLiteral(ctxt); |
| 2799 | if (URI == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2800 | htmlParseErr(ctxt, XML_ERR_URI_REQUIRED, |
| 2801 | "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } else if ((UPPER == 'P') && (UPP(1) == 'U') && |
| 2804 | (UPP(2) == 'B') && (UPP(3) == 'L') && |
| 2805 | (UPP(4) == 'I') && (UPP(5) == 'C')) { |
| 2806 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2807 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2808 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 2809 | "Space required after 'PUBLIC'\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2810 | } |
| 2811 | SKIP_BLANKS; |
| 2812 | *publicID = htmlParsePubidLiteral(ctxt); |
| 2813 | if (*publicID == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2814 | htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED, |
| 2815 | "htmlParseExternalID: PUBLIC, no Public Identifier\n", |
| 2816 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | } |
| 2818 | SKIP_BLANKS; |
| 2819 | if ((CUR == '"') || (CUR == '\'')) { |
| 2820 | URI = htmlParseSystemLiteral(ctxt); |
| 2821 | } |
| 2822 | } |
| 2823 | return(URI); |
| 2824 | } |
| 2825 | |
| 2826 | /** |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 2827 | * xmlParsePI: |
| 2828 | * @ctxt: an XML parser context |
| 2829 | * |
| 2830 | * parse an XML Processing Instruction. |
| 2831 | * |
| 2832 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 2833 | */ |
| 2834 | static void |
| 2835 | htmlParsePI(htmlParserCtxtPtr ctxt) { |
| 2836 | xmlChar *buf = NULL; |
| 2837 | int len = 0; |
| 2838 | int size = HTML_PARSER_BUFFER_SIZE; |
| 2839 | int cur, l; |
| 2840 | const xmlChar *target; |
| 2841 | xmlParserInputState state; |
| 2842 | int count = 0; |
| 2843 | |
| 2844 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 2845 | state = ctxt->instate; |
| 2846 | ctxt->instate = XML_PARSER_PI; |
| 2847 | /* |
| 2848 | * this is a Processing Instruction. |
| 2849 | */ |
| 2850 | SKIP(2); |
| 2851 | SHRINK; |
| 2852 | |
| 2853 | /* |
| 2854 | * Parse the target name and check for special support like |
| 2855 | * namespace. |
| 2856 | */ |
| 2857 | target = htmlParseName(ctxt); |
| 2858 | if (target != NULL) { |
| 2859 | if (RAW == '>') { |
| 2860 | SKIP(1); |
| 2861 | |
| 2862 | /* |
| 2863 | * SAX: PI detected. |
| 2864 | */ |
| 2865 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 2866 | (ctxt->sax->processingInstruction != NULL)) |
| 2867 | ctxt->sax->processingInstruction(ctxt->userData, |
| 2868 | target, NULL); |
| 2869 | ctxt->instate = state; |
| 2870 | return; |
| 2871 | } |
| 2872 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 2873 | if (buf == NULL) { |
| 2874 | htmlErrMemory(ctxt, NULL); |
| 2875 | ctxt->instate = state; |
| 2876 | return; |
| 2877 | } |
| 2878 | cur = CUR; |
| 2879 | if (!IS_BLANK(cur)) { |
| 2880 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 2881 | "ParsePI: PI %s space expected\n", target, NULL); |
| 2882 | } |
| 2883 | SKIP_BLANKS; |
| 2884 | cur = CUR_CHAR(l); |
| 2885 | while (IS_CHAR(cur) && (cur != '>')) { |
| 2886 | if (len + 5 >= size) { |
| 2887 | xmlChar *tmp; |
| 2888 | |
| 2889 | size *= 2; |
| 2890 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 2891 | if (tmp == NULL) { |
| 2892 | htmlErrMemory(ctxt, NULL); |
| 2893 | xmlFree(buf); |
| 2894 | ctxt->instate = state; |
| 2895 | return; |
| 2896 | } |
| 2897 | buf = tmp; |
| 2898 | } |
| 2899 | count++; |
| 2900 | if (count > 50) { |
| 2901 | GROW; |
| 2902 | count = 0; |
| 2903 | } |
| 2904 | COPY_BUF(l,buf,len,cur); |
| 2905 | NEXTL(l); |
| 2906 | cur = CUR_CHAR(l); |
| 2907 | if (cur == 0) { |
| 2908 | SHRINK; |
| 2909 | GROW; |
| 2910 | cur = CUR_CHAR(l); |
| 2911 | } |
| 2912 | } |
| 2913 | buf[len] = 0; |
| 2914 | if (cur != '>') { |
| 2915 | htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 2916 | "ParsePI: PI %s never end ...\n", target, NULL); |
| 2917 | } else { |
| 2918 | SKIP(1); |
| 2919 | |
| 2920 | /* |
| 2921 | * SAX: PI detected. |
| 2922 | */ |
| 2923 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 2924 | (ctxt->sax->processingInstruction != NULL)) |
| 2925 | ctxt->sax->processingInstruction(ctxt->userData, |
| 2926 | target, buf); |
| 2927 | } |
| 2928 | xmlFree(buf); |
| 2929 | } else { |
| 2930 | htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED, |
| 2931 | "PI is not started correctly", NULL, NULL); |
| 2932 | } |
| 2933 | ctxt->instate = state; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2938 | * htmlParseComment: |
| 2939 | * @ctxt: an HTML parser context |
| 2940 | * |
| 2941 | * Parse an XML (SGML) comment <!-- .... --> |
| 2942 | * |
| 2943 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 2944 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2945 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2946 | htmlParseComment(htmlParserCtxtPtr ctxt) { |
| 2947 | xmlChar *buf = NULL; |
| 2948 | int len; |
| 2949 | int size = HTML_PARSER_BUFFER_SIZE; |
| 2950 | int q, ql; |
| 2951 | int r, rl; |
| 2952 | int cur, l; |
| 2953 | xmlParserInputState state; |
| 2954 | |
| 2955 | /* |
| 2956 | * Check that there is a comment right here. |
| 2957 | */ |
| 2958 | if ((RAW != '<') || (NXT(1) != '!') || |
| 2959 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
| 2960 | |
| 2961 | state = ctxt->instate; |
| 2962 | ctxt->instate = XML_PARSER_COMMENT; |
| 2963 | SHRINK; |
| 2964 | SKIP(4); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2965 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2966 | if (buf == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2967 | htmlErrMemory(ctxt, "buffer allocation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2968 | ctxt->instate = state; |
| 2969 | return; |
| 2970 | } |
| 2971 | q = CUR_CHAR(ql); |
| 2972 | NEXTL(ql); |
| 2973 | r = CUR_CHAR(rl); |
| 2974 | NEXTL(rl); |
| 2975 | cur = CUR_CHAR(l); |
| 2976 | len = 0; |
| 2977 | while (IS_CHAR(cur) && |
| 2978 | ((cur != '>') || |
| 2979 | (r != '-') || (q != '-'))) { |
| 2980 | if (len + 5 >= size) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 2981 | xmlChar *tmp; |
| 2982 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2983 | size *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 2984 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 2985 | if (tmp == NULL) { |
| 2986 | xmlFree(buf); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2987 | htmlErrMemory(ctxt, "growing buffer failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2988 | ctxt->instate = state; |
| 2989 | return; |
| 2990 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 2991 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2992 | } |
| 2993 | COPY_BUF(ql,buf,len,q); |
| 2994 | q = r; |
| 2995 | ql = rl; |
| 2996 | r = cur; |
| 2997 | rl = l; |
| 2998 | NEXTL(l); |
| 2999 | cur = CUR_CHAR(l); |
| 3000 | if (cur == 0) { |
| 3001 | SHRINK; |
| 3002 | GROW; |
| 3003 | cur = CUR_CHAR(l); |
| 3004 | } |
| 3005 | } |
| 3006 | buf[len] = 0; |
| 3007 | if (!IS_CHAR(cur)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3008 | htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3009 | "Comment not terminated \n<!--%.50s\n", buf, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3010 | xmlFree(buf); |
| 3011 | } else { |
| 3012 | NEXT; |
| 3013 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3014 | (!ctxt->disableSAX)) |
| 3015 | ctxt->sax->comment(ctxt->userData, buf); |
| 3016 | xmlFree(buf); |
| 3017 | } |
| 3018 | ctxt->instate = state; |
| 3019 | } |
| 3020 | |
| 3021 | /** |
| 3022 | * htmlParseCharRef: |
| 3023 | * @ctxt: an HTML parser context |
| 3024 | * |
| 3025 | * parse Reference declarations |
| 3026 | * |
| 3027 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 3028 | * '&#x' [0-9a-fA-F]+ ';' |
| 3029 | * |
| 3030 | * Returns the value parsed (as an int) |
| 3031 | */ |
| 3032 | int |
| 3033 | htmlParseCharRef(htmlParserCtxtPtr ctxt) { |
| 3034 | int val = 0; |
| 3035 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3036 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3037 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3038 | "htmlParseCharRef: context error\n", |
| 3039 | NULL, NULL); |
| 3040 | return(0); |
| 3041 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3042 | if ((CUR == '&') && (NXT(1) == '#') && |
Daniel Veillard | c59d826 | 2003-11-20 21:59:12 +0000 | [diff] [blame] | 3043 | ((NXT(2) == 'x') || NXT(2) == 'X')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | SKIP(3); |
| 3045 | while (CUR != ';') { |
| 3046 | if ((CUR >= '0') && (CUR <= '9')) |
| 3047 | val = val * 16 + (CUR - '0'); |
| 3048 | else if ((CUR >= 'a') && (CUR <= 'f')) |
| 3049 | val = val * 16 + (CUR - 'a') + 10; |
| 3050 | else if ((CUR >= 'A') && (CUR <= 'F')) |
| 3051 | val = val * 16 + (CUR - 'A') + 10; |
| 3052 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3053 | htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, |
| 3054 | "htmlParseCharRef: invalid hexadecimal value\n", |
| 3055 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | return(0); |
| 3057 | } |
| 3058 | NEXT; |
| 3059 | } |
| 3060 | if (CUR == ';') |
| 3061 | NEXT; |
| 3062 | } else if ((CUR == '&') && (NXT(1) == '#')) { |
| 3063 | SKIP(2); |
| 3064 | while (CUR != ';') { |
| 3065 | if ((CUR >= '0') && (CUR <= '9')) |
| 3066 | val = val * 10 + (CUR - '0'); |
| 3067 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3068 | htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, |
| 3069 | "htmlParseCharRef: invalid decimal value\n", |
| 3070 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3071 | return(0); |
| 3072 | } |
| 3073 | NEXT; |
| 3074 | } |
| 3075 | if (CUR == ';') |
| 3076 | NEXT; |
| 3077 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3078 | htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF, |
| 3079 | "htmlParseCharRef: invalid value\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3080 | } |
| 3081 | /* |
| 3082 | * Check the value IS_CHAR ... |
| 3083 | */ |
| 3084 | if (IS_CHAR(val)) { |
| 3085 | return(val); |
| 3086 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3087 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 3088 | "htmlParseCharRef: invalid xmlChar value %d\n", |
| 3089 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3090 | } |
| 3091 | return(0); |
| 3092 | } |
| 3093 | |
| 3094 | |
| 3095 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3096 | * htmlParseDocTypeDecl: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3097 | * @ctxt: an HTML parser context |
| 3098 | * |
| 3099 | * parse a DOCTYPE declaration |
| 3100 | * |
| 3101 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 3102 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 3103 | */ |
| 3104 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3105 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3106 | htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3107 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3108 | xmlChar *ExternalID = NULL; |
| 3109 | xmlChar *URI = NULL; |
| 3110 | |
| 3111 | /* |
| 3112 | * We know that '<!DOCTYPE' has been detected. |
| 3113 | */ |
| 3114 | SKIP(9); |
| 3115 | |
| 3116 | SKIP_BLANKS; |
| 3117 | |
| 3118 | /* |
| 3119 | * Parse the DOCTYPE name. |
| 3120 | */ |
| 3121 | name = htmlParseName(ctxt); |
| 3122 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3123 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3124 | "htmlParseDocTypeDecl : no DOCTYPE name !\n", |
| 3125 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3126 | } |
| 3127 | /* |
| 3128 | * Check that upper(name) == "HTML" !!!!!!!!!!!!! |
| 3129 | */ |
| 3130 | |
| 3131 | SKIP_BLANKS; |
| 3132 | |
| 3133 | /* |
| 3134 | * Check for SystemID and ExternalID |
| 3135 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3136 | URI = htmlParseExternalID(ctxt, &ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3137 | SKIP_BLANKS; |
| 3138 | |
| 3139 | /* |
| 3140 | * We should be at the end of the DOCTYPE declaration. |
| 3141 | */ |
| 3142 | if (CUR != '>') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3143 | htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, |
| 3144 | "DOCTYPE improperly terminated\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3145 | /* We shouldn't try to resynchronize ... */ |
| 3146 | } |
| 3147 | NEXT; |
| 3148 | |
| 3149 | /* |
| 3150 | * Create or update the document accordingly to the DOCTYPE |
| 3151 | */ |
| 3152 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 3153 | (!ctxt->disableSAX)) |
| 3154 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 3155 | |
| 3156 | /* |
| 3157 | * Cleanup, since we don't use all those identifiers |
| 3158 | */ |
| 3159 | if (URI != NULL) xmlFree(URI); |
| 3160 | if (ExternalID != NULL) xmlFree(ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | /** |
| 3164 | * htmlParseAttribute: |
| 3165 | * @ctxt: an HTML parser context |
| 3166 | * @value: a xmlChar ** used to store the value of the attribute |
| 3167 | * |
| 3168 | * parse an attribute |
| 3169 | * |
| 3170 | * [41] Attribute ::= Name Eq AttValue |
| 3171 | * |
| 3172 | * [25] Eq ::= S? '=' S? |
| 3173 | * |
| 3174 | * With namespace: |
| 3175 | * |
| 3176 | * [NS 11] Attribute ::= QName Eq AttValue |
| 3177 | * |
| 3178 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 3179 | * definition. |
| 3180 | * |
| 3181 | * Returns the attribute name, and the value in *value. |
| 3182 | */ |
| 3183 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3184 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3185 | htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3186 | const xmlChar *name; |
| 3187 | xmlChar *val = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3188 | |
| 3189 | *value = NULL; |
| 3190 | name = htmlParseHTMLName(ctxt); |
| 3191 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3192 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3193 | "error parsing attribute name\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3194 | return(NULL); |
| 3195 | } |
| 3196 | |
| 3197 | /* |
| 3198 | * read the value |
| 3199 | */ |
| 3200 | SKIP_BLANKS; |
| 3201 | if (CUR == '=') { |
| 3202 | NEXT; |
| 3203 | SKIP_BLANKS; |
| 3204 | val = htmlParseAttValue(ctxt); |
| 3205 | /****** |
| 3206 | } else { |
| 3207 | * TODO : some attribute must have values, some may not |
| 3208 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 3209 | ctxt->sax->warning(ctxt->userData, |
| 3210 | "No value for attribute %s\n", name); */ |
| 3211 | } |
| 3212 | |
| 3213 | *value = val; |
| 3214 | return(name); |
| 3215 | } |
| 3216 | |
| 3217 | /** |
| 3218 | * htmlCheckEncoding: |
| 3219 | * @ctxt: an HTML parser context |
| 3220 | * @attvalue: the attribute value |
| 3221 | * |
| 3222 | * Checks an http-equiv attribute from a Meta tag to detect |
| 3223 | * the encoding |
| 3224 | * If a new encoding is detected the parser is switched to decode |
| 3225 | * it and pass UTF8 |
| 3226 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3227 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3228 | htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { |
| 3229 | const xmlChar *encoding; |
| 3230 | |
| 3231 | if ((ctxt == NULL) || (attvalue == NULL)) |
| 3232 | return; |
| 3233 | |
| 3234 | /* do not change encoding */ |
| 3235 | if (ctxt->input->encoding != NULL) |
| 3236 | return; |
| 3237 | |
| 3238 | encoding = xmlStrcasestr(attvalue, BAD_CAST"charset="); |
| 3239 | if (encoding != NULL) { |
| 3240 | encoding += 8; |
| 3241 | } else { |
| 3242 | encoding = xmlStrcasestr(attvalue, BAD_CAST"charset ="); |
| 3243 | if (encoding != NULL) |
| 3244 | encoding += 9; |
| 3245 | } |
| 3246 | if (encoding != NULL) { |
| 3247 | xmlCharEncoding enc; |
| 3248 | xmlCharEncodingHandlerPtr handler; |
| 3249 | |
| 3250 | while ((*encoding == ' ') || (*encoding == '\t')) encoding++; |
| 3251 | |
| 3252 | if (ctxt->input->encoding != NULL) |
| 3253 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 3254 | ctxt->input->encoding = xmlStrdup(encoding); |
| 3255 | |
| 3256 | enc = xmlParseCharEncoding((const char *) encoding); |
| 3257 | /* |
| 3258 | * registered set of known encodings |
| 3259 | */ |
| 3260 | if (enc != XML_CHAR_ENCODING_ERROR) { |
| 3261 | xmlSwitchEncoding(ctxt, enc); |
| 3262 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 3263 | } else { |
| 3264 | /* |
| 3265 | * fallback for unknown encodings |
| 3266 | */ |
| 3267 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 3268 | if (handler != NULL) { |
| 3269 | xmlSwitchToEncoding(ctxt, handler); |
| 3270 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 3271 | } else { |
| 3272 | ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | if ((ctxt->input->buf != NULL) && |
| 3277 | (ctxt->input->buf->encoder != NULL) && |
| 3278 | (ctxt->input->buf->raw != NULL) && |
| 3279 | (ctxt->input->buf->buffer != NULL)) { |
| 3280 | int nbchars; |
| 3281 | int processed; |
| 3282 | |
| 3283 | /* |
| 3284 | * convert as much as possible to the parser reading buffer. |
| 3285 | */ |
| 3286 | processed = ctxt->input->cur - ctxt->input->base; |
| 3287 | xmlBufferShrink(ctxt->input->buf->buffer, processed); |
| 3288 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 3289 | ctxt->input->buf->buffer, |
| 3290 | ctxt->input->buf->raw); |
| 3291 | if (nbchars < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3292 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 3293 | "htmlCheckEncoding: encoder error\n", |
| 3294 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3295 | } |
| 3296 | ctxt->input->base = |
| 3297 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * htmlCheckMeta: |
| 3304 | * @ctxt: an HTML parser context |
| 3305 | * @atts: the attributes values |
| 3306 | * |
| 3307 | * Checks an attributes from a Meta tag |
| 3308 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3309 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3310 | htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) { |
| 3311 | int i; |
| 3312 | const xmlChar *att, *value; |
| 3313 | int http = 0; |
| 3314 | const xmlChar *content = NULL; |
| 3315 | |
| 3316 | if ((ctxt == NULL) || (atts == NULL)) |
| 3317 | return; |
| 3318 | |
| 3319 | i = 0; |
| 3320 | att = atts[i++]; |
| 3321 | while (att != NULL) { |
| 3322 | value = atts[i++]; |
| 3323 | if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv")) |
| 3324 | && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) |
| 3325 | http = 1; |
| 3326 | else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content"))) |
| 3327 | content = value; |
| 3328 | att = atts[i++]; |
| 3329 | } |
| 3330 | if ((http) && (content != NULL)) |
| 3331 | htmlCheckEncoding(ctxt, content); |
| 3332 | |
| 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * htmlParseStartTag: |
| 3337 | * @ctxt: an HTML parser context |
| 3338 | * |
| 3339 | * parse a start of tag either for rule element or |
| 3340 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 3341 | * |
| 3342 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 3343 | * |
| 3344 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 3345 | * |
| 3346 | * With namespace: |
| 3347 | * |
| 3348 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 3349 | * |
| 3350 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 3351 | * |
| 3352 | */ |
| 3353 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3354 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3355 | htmlParseStartTag(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3356 | const xmlChar *name; |
| 3357 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3358 | xmlChar *attvalue; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3359 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3360 | int nbatts = 0; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3361 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3362 | int meta = 0; |
| 3363 | int i; |
| 3364 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3365 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3366 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3367 | "htmlParseStartTag: context error\n", NULL, NULL); |
| 3368 | return; |
| 3369 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3370 | if (CUR != '<') return; |
| 3371 | NEXT; |
| 3372 | |
| 3373 | GROW; |
| 3374 | name = htmlParseHTMLName(ctxt); |
| 3375 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3376 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3377 | "htmlParseStartTag: invalid element name\n", |
| 3378 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3379 | /* Dump the bogus tag like browsers do */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3380 | while ((IS_CHAR_CH(CUR)) && (CUR != '>')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3381 | NEXT; |
| 3382 | return; |
| 3383 | } |
| 3384 | if (xmlStrEqual(name, BAD_CAST"meta")) |
| 3385 | meta = 1; |
| 3386 | |
| 3387 | /* |
| 3388 | * Check for auto-closure of HTML elements. |
| 3389 | */ |
| 3390 | htmlAutoClose(ctxt, name); |
| 3391 | |
| 3392 | /* |
| 3393 | * Check for implied HTML elements. |
| 3394 | */ |
| 3395 | htmlCheckImplied(ctxt, name); |
| 3396 | |
| 3397 | /* |
| 3398 | * Avoid html at any level > 0, head at any level != 1 |
| 3399 | * or any attempt to recurse body |
| 3400 | */ |
| 3401 | if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3402 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3403 | "htmlParseStartTag: misplaced <html> tag\n", |
| 3404 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3405 | return; |
| 3406 | } |
| 3407 | if ((ctxt->nameNr != 1) && |
| 3408 | (xmlStrEqual(name, BAD_CAST"head"))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3409 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3410 | "htmlParseStartTag: misplaced <head> tag\n", |
| 3411 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3412 | return; |
| 3413 | } |
| 3414 | if (xmlStrEqual(name, BAD_CAST"body")) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3415 | int indx; |
| 3416 | for (indx = 0;indx < ctxt->nameNr;indx++) { |
| 3417 | if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3418 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3419 | "htmlParseStartTag: misplaced <body> tag\n", |
| 3420 | name, NULL); |
Daniel Veillard | c59d826 | 2003-11-20 21:59:12 +0000 | [diff] [blame] | 3421 | while ((IS_CHAR_CH(CUR)) && (CUR != '>')) |
| 3422 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3423 | return; |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | /* |
| 3429 | * Now parse the attributes, it ends up with the ending |
| 3430 | * |
| 3431 | * (S Attribute)* S? |
| 3432 | */ |
| 3433 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3434 | while ((IS_CHAR_CH(CUR)) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3435 | (CUR != '>') && |
| 3436 | ((CUR != '/') || (NXT(1) != '>'))) { |
| 3437 | long cons = ctxt->nbChars; |
| 3438 | |
| 3439 | GROW; |
| 3440 | attname = htmlParseAttribute(ctxt, &attvalue); |
| 3441 | if (attname != NULL) { |
| 3442 | |
| 3443 | /* |
| 3444 | * Well formedness requires at most one declaration of an attribute |
| 3445 | */ |
| 3446 | for (i = 0; i < nbatts;i += 2) { |
| 3447 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3448 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED, |
| 3449 | "Attribute %s redefined\n", attname, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3450 | if (attvalue != NULL) |
| 3451 | xmlFree(attvalue); |
| 3452 | goto failed; |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | /* |
| 3457 | * Add the pair to atts |
| 3458 | */ |
| 3459 | if (atts == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3460 | maxatts = 22; /* allow for 10 attrs by default */ |
| 3461 | atts = (const xmlChar **) |
| 3462 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3463 | if (atts == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3464 | htmlErrMemory(ctxt, NULL); |
| 3465 | if (attvalue != NULL) |
| 3466 | xmlFree(attvalue); |
| 3467 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3468 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3469 | ctxt->atts = atts; |
| 3470 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3471 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3472 | const xmlChar **n; |
| 3473 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3474 | maxatts *= 2; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3475 | n = (const xmlChar **) xmlRealloc((void *) atts, |
| 3476 | maxatts * sizeof(const xmlChar *)); |
| 3477 | if (n == NULL) { |
| 3478 | htmlErrMemory(ctxt, NULL); |
| 3479 | if (attvalue != NULL) |
| 3480 | xmlFree(attvalue); |
| 3481 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3482 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3483 | atts = n; |
| 3484 | ctxt->atts = atts; |
| 3485 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3486 | } |
| 3487 | atts[nbatts++] = attname; |
| 3488 | atts[nbatts++] = attvalue; |
| 3489 | atts[nbatts] = NULL; |
| 3490 | atts[nbatts + 1] = NULL; |
| 3491 | } |
| 3492 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3493 | if (attvalue != NULL) |
| 3494 | xmlFree(attvalue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3495 | /* Dump the bogus attribute string up to the next blank or |
| 3496 | * the end of the tag. */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3497 | while ((IS_CHAR_CH(CUR)) && |
| 3498 | !(IS_BLANK_CH(CUR)) && (CUR != '>') && |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 3499 | ((CUR != '/') || (NXT(1) != '>'))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3500 | NEXT; |
| 3501 | } |
| 3502 | |
| 3503 | failed: |
| 3504 | SKIP_BLANKS; |
| 3505 | if (cons == ctxt->nbChars) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3506 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3507 | "htmlParseStartTag: problem parsing attributes\n", |
| 3508 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3509 | break; |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | /* |
| 3514 | * Handle specific association to the META tag |
| 3515 | */ |
| 3516 | if (meta) |
| 3517 | htmlCheckMeta(ctxt, atts); |
| 3518 | |
| 3519 | /* |
| 3520 | * SAX: Start of Element ! |
| 3521 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3522 | htmlnamePush(ctxt, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3523 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) { |
| 3524 | if (nbatts != 0) |
| 3525 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 3526 | else |
| 3527 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 3528 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3529 | |
| 3530 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3531 | for (i = 1;i < nbatts;i += 2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3532 | if (atts[i] != NULL) |
| 3533 | xmlFree((xmlChar *) atts[i]); |
| 3534 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3535 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | /** |
| 3539 | * htmlParseEndTag: |
| 3540 | * @ctxt: an HTML parser context |
| 3541 | * |
| 3542 | * parse an end of tag |
| 3543 | * |
| 3544 | * [42] ETag ::= '</' Name S? '>' |
| 3545 | * |
| 3546 | * With namespace |
| 3547 | * |
| 3548 | * [NS 9] ETag ::= '</' QName S? '>' |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3549 | * |
| 3550 | * Returns 1 if the current level should be closed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3551 | */ |
| 3552 | |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3553 | static int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3554 | htmlParseEndTag(htmlParserCtxtPtr ctxt) |
| 3555 | { |
| 3556 | const xmlChar *name; |
| 3557 | const xmlChar *oldname; |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3558 | int i, ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | |
| 3560 | if ((CUR != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3561 | htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED, |
| 3562 | "htmlParseEndTag: '</' not found\n", NULL, NULL); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3563 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3564 | } |
| 3565 | SKIP(2); |
| 3566 | |
| 3567 | name = htmlParseHTMLName(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3568 | if (name == NULL) |
| 3569 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3570 | |
| 3571 | /* |
| 3572 | * We should definitely be at the ending "S? '>'" part |
| 3573 | */ |
| 3574 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3575 | if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3576 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 3577 | "End tag : expected '>'\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3578 | } else |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3579 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3580 | |
| 3581 | /* |
| 3582 | * If the name read is not one of the element in the parsing stack |
| 3583 | * then return, it's just an error. |
| 3584 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3585 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
| 3586 | if (xmlStrEqual(name, ctxt->nameTab[i])) |
| 3587 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3588 | } |
| 3589 | if (i < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3590 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 3591 | "Unexpected end tag : %s\n", name, NULL); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3592 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | |
| 3596 | /* |
| 3597 | * Check for auto-closure of HTML elements. |
| 3598 | */ |
| 3599 | |
| 3600 | htmlAutoCloseOnClose(ctxt, name); |
| 3601 | |
| 3602 | /* |
| 3603 | * Well formedness constraints, opening and closing must match. |
| 3604 | * With the exception that the autoclose may have popped stuff out |
| 3605 | * of the stack. |
| 3606 | */ |
| 3607 | if (!xmlStrEqual(name, ctxt->name)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3608 | if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3609 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 3610 | "Opening and ending tag mismatch: %s and %s\n", |
| 3611 | name, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | /* |
| 3616 | * SAX: End of Tag |
| 3617 | */ |
| 3618 | oldname = ctxt->name; |
| 3619 | if ((oldname != NULL) && (xmlStrEqual(oldname, name))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3620 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 3621 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3622 | htmlnamePop(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3623 | ret = 1; |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3624 | } else { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3625 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3628 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | /** |
| 3633 | * htmlParseReference: |
| 3634 | * @ctxt: an HTML parser context |
| 3635 | * |
| 3636 | * parse and handle entity references in content, |
| 3637 | * this will end-up in a call to character() since this is either a |
| 3638 | * CharRef, or a predefined entity. |
| 3639 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3640 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3641 | htmlParseReference(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 3642 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3643 | xmlChar out[6]; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3644 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3645 | if (CUR != '&') return; |
| 3646 | |
| 3647 | if (NXT(1) == '#') { |
| 3648 | unsigned int c; |
| 3649 | int bits, i = 0; |
| 3650 | |
| 3651 | c = htmlParseCharRef(ctxt); |
| 3652 | if (c == 0) |
| 3653 | return; |
| 3654 | |
| 3655 | if (c < 0x80) { out[i++]= c; bits= -6; } |
| 3656 | else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 3657 | else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 3658 | else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 3659 | |
| 3660 | for ( ; bits >= 0; bits-= 6) { |
| 3661 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
| 3662 | } |
| 3663 | out[i] = 0; |
| 3664 | |
| 3665 | htmlCheckParagraph(ctxt); |
| 3666 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3667 | ctxt->sax->characters(ctxt->userData, out, i); |
| 3668 | } else { |
| 3669 | ent = htmlParseEntityRef(ctxt, &name); |
| 3670 | if (name == NULL) { |
| 3671 | htmlCheckParagraph(ctxt); |
| 3672 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3673 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
| 3674 | return; |
| 3675 | } |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 3676 | if ((ent == NULL) || !(ent->value > 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3677 | htmlCheckParagraph(ctxt); |
| 3678 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) { |
| 3679 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
| 3680 | ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name)); |
| 3681 | /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */ |
| 3682 | } |
| 3683 | } else { |
| 3684 | unsigned int c; |
| 3685 | int bits, i = 0; |
| 3686 | |
| 3687 | c = ent->value; |
| 3688 | if (c < 0x80) |
| 3689 | { out[i++]= c; bits= -6; } |
| 3690 | else if (c < 0x800) |
| 3691 | { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 3692 | else if (c < 0x10000) |
| 3693 | { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 3694 | else |
| 3695 | { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 3696 | |
| 3697 | for ( ; bits >= 0; bits-= 6) { |
| 3698 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
| 3699 | } |
| 3700 | out[i] = 0; |
| 3701 | |
| 3702 | htmlCheckParagraph(ctxt); |
| 3703 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3704 | ctxt->sax->characters(ctxt->userData, out, i); |
| 3705 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | /** |
| 3710 | * htmlParseContent: |
| 3711 | * @ctxt: an HTML parser context |
| 3712 | * @name: the node name |
| 3713 | * |
| 3714 | * Parse a content: comment, sub-element, reference or text. |
| 3715 | * |
| 3716 | */ |
| 3717 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3718 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3719 | htmlParseContent(htmlParserCtxtPtr ctxt) { |
| 3720 | xmlChar *currentNode; |
| 3721 | int depth; |
| 3722 | |
| 3723 | currentNode = xmlStrdup(ctxt->name); |
| 3724 | depth = ctxt->nameNr; |
| 3725 | while (1) { |
| 3726 | long cons = ctxt->nbChars; |
| 3727 | |
| 3728 | GROW; |
| 3729 | /* |
| 3730 | * Our tag or one of it's parent or children is ending. |
| 3731 | */ |
| 3732 | if ((CUR == '<') && (NXT(1) == '/')) { |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3733 | if (htmlParseEndTag(ctxt) && |
| 3734 | ((currentNode != NULL) || (ctxt->nameNr == 0))) { |
| 3735 | if (currentNode != NULL) |
| 3736 | xmlFree(currentNode); |
| 3737 | return; |
| 3738 | } |
| 3739 | continue; /* while */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | /* |
| 3743 | * Has this node been popped out during parsing of |
| 3744 | * the next element |
| 3745 | */ |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3746 | if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) && |
| 3747 | (!xmlStrEqual(currentNode, ctxt->name))) |
| 3748 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3749 | if (currentNode != NULL) xmlFree(currentNode); |
| 3750 | return; |
| 3751 | } |
| 3752 | |
Daniel Veillard | f9533d1 | 2001-03-03 10:04:57 +0000 | [diff] [blame] | 3753 | if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) || |
| 3754 | (xmlStrEqual(currentNode, BAD_CAST"style")))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3755 | /* |
| 3756 | * Handle SCRIPT/STYLE separately |
| 3757 | */ |
| 3758 | htmlParseScript(ctxt); |
| 3759 | } else { |
| 3760 | /* |
| 3761 | * Sometimes DOCTYPE arrives in the middle of the document |
| 3762 | */ |
| 3763 | if ((CUR == '<') && (NXT(1) == '!') && |
| 3764 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 3765 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 3766 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 3767 | (UPP(8) == 'E')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3768 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3769 | "Misplaced DOCTYPE declaration\n", |
| 3770 | BAD_CAST "DOCTYPE" , NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3771 | htmlParseDocTypeDecl(ctxt); |
| 3772 | } |
| 3773 | |
| 3774 | /* |
| 3775 | * First case : a comment |
| 3776 | */ |
| 3777 | if ((CUR == '<') && (NXT(1) == '!') && |
| 3778 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 3779 | htmlParseComment(ctxt); |
| 3780 | } |
| 3781 | |
| 3782 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 3783 | * Second case : a Processing Instruction. |
| 3784 | */ |
| 3785 | else if ((CUR == '<') && (NXT(1) == '?')) { |
| 3786 | htmlParsePI(ctxt); |
| 3787 | } |
| 3788 | |
| 3789 | /* |
| 3790 | * Third case : a sub-element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3791 | */ |
| 3792 | else if (CUR == '<') { |
| 3793 | htmlParseElement(ctxt); |
| 3794 | } |
| 3795 | |
| 3796 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 3797 | * Fourth case : a reference. If if has not been resolved, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3798 | * parsing returns it's Name, create the node |
| 3799 | */ |
| 3800 | else if (CUR == '&') { |
| 3801 | htmlParseReference(ctxt); |
| 3802 | } |
| 3803 | |
| 3804 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 3805 | * Fifth case : end of the resource |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | */ |
| 3807 | else if (CUR == 0) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 3808 | htmlAutoCloseOnEnd(ctxt); |
| 3809 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /* |
| 3813 | * Last case, text. Note that References are handled directly. |
| 3814 | */ |
| 3815 | else { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3816 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | if (cons == ctxt->nbChars) { |
| 3820 | if (ctxt->node != NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3821 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3822 | "detected an error in element content\n", |
| 3823 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3824 | } |
| 3825 | break; |
| 3826 | } |
| 3827 | } |
| 3828 | GROW; |
| 3829 | } |
| 3830 | if (currentNode != NULL) xmlFree(currentNode); |
| 3831 | } |
| 3832 | |
| 3833 | /** |
| 3834 | * htmlParseElement: |
| 3835 | * @ctxt: an HTML parser context |
| 3836 | * |
| 3837 | * parse an HTML element, this is highly recursive |
| 3838 | * |
| 3839 | * [39] element ::= EmptyElemTag | STag content ETag |
| 3840 | * |
| 3841 | * [41] Attribute ::= Name Eq AttValue |
| 3842 | */ |
| 3843 | |
| 3844 | void |
| 3845 | htmlParseElement(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3846 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3847 | xmlChar *currentNode = NULL; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 3848 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3849 | htmlParserNodeInfo node_info; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3850 | const xmlChar *oldname; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3851 | int depth; |
Daniel Veillard | 3fbe8e3 | 2001-10-06 13:30:33 +0000 | [diff] [blame] | 3852 | const xmlChar *oldptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3853 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3854 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3855 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3856 | "htmlParseStartTag: context error\n", NULL, NULL); |
| 3857 | return; |
| 3858 | } |
| 3859 | depth = ctxt->nameNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3860 | /* Capture start position */ |
| 3861 | if (ctxt->record_info) { |
| 3862 | node_info.begin_pos = ctxt->input->consumed + |
| 3863 | (CUR_PTR - ctxt->input->base); |
| 3864 | node_info.begin_line = ctxt->input->line; |
| 3865 | } |
| 3866 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3867 | oldname = ctxt->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3868 | htmlParseStartTag(ctxt); |
| 3869 | name = ctxt->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3870 | if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) || |
| 3871 | (name == NULL)) { |
| 3872 | if (CUR == '>') |
| 3873 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | |
| 3877 | /* |
| 3878 | * Lookup the info for that element. |
| 3879 | */ |
| 3880 | info = htmlTagLookup(name); |
| 3881 | if (info == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3882 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
| 3883 | "Tag %s invalid\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3887 | * Check for an Empty Element labeled the XML/SGML way |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3888 | */ |
| 3889 | if ((CUR == '/') && (NXT(1) == '>')) { |
| 3890 | SKIP(2); |
| 3891 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 3892 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3893 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | return; |
| 3895 | } |
| 3896 | |
| 3897 | if (CUR == '>') { |
| 3898 | NEXT; |
| 3899 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3900 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 3901 | "Couldn't find end of Start Tag %s\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3902 | |
| 3903 | /* |
| 3904 | * end of parsing of this node. |
| 3905 | */ |
| 3906 | if (xmlStrEqual(name, ctxt->name)) { |
| 3907 | nodePop(ctxt); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3908 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | /* |
| 3912 | * Capture end position and add node |
| 3913 | */ |
| 3914 | if ( currentNode != NULL && ctxt->record_info ) { |
| 3915 | node_info.end_pos = ctxt->input->consumed + |
| 3916 | (CUR_PTR - ctxt->input->base); |
| 3917 | node_info.end_line = ctxt->input->line; |
| 3918 | node_info.node = ctxt->node; |
| 3919 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 3920 | } |
| 3921 | return; |
| 3922 | } |
| 3923 | |
| 3924 | /* |
| 3925 | * Check for an Empty Element from DTD definition |
| 3926 | */ |
| 3927 | if ((info != NULL) && (info->empty)) { |
| 3928 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 3929 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3930 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3931 | return; |
| 3932 | } |
| 3933 | |
| 3934 | /* |
| 3935 | * Parse the content of the element: |
| 3936 | */ |
| 3937 | currentNode = xmlStrdup(ctxt->name); |
| 3938 | depth = ctxt->nameNr; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3939 | while (IS_CHAR_CH(CUR)) { |
William M. Brack | d28e48a | 2001-09-23 01:55:08 +0000 | [diff] [blame] | 3940 | oldptr = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3941 | htmlParseContent(ctxt); |
William M. Brack | d28e48a | 2001-09-23 01:55:08 +0000 | [diff] [blame] | 3942 | if (oldptr==ctxt->input->cur) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3943 | if (ctxt->nameNr < depth) break; |
| 3944 | } |
| 3945 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3946 | /* |
| 3947 | * Capture end position and add node |
| 3948 | */ |
| 3949 | if ( currentNode != NULL && ctxt->record_info ) { |
| 3950 | node_info.end_pos = ctxt->input->consumed + |
| 3951 | (CUR_PTR - ctxt->input->base); |
| 3952 | node_info.end_line = ctxt->input->line; |
| 3953 | node_info.node = ctxt->node; |
| 3954 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 3955 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3956 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 3957 | htmlAutoCloseOnEnd(ctxt); |
| 3958 | } |
| 3959 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3960 | if (currentNode != NULL) |
| 3961 | xmlFree(currentNode); |
| 3962 | } |
| 3963 | |
| 3964 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3965 | * htmlParseDocument: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3966 | * @ctxt: an HTML parser context |
| 3967 | * |
| 3968 | * parse an HTML document (and build a tree if using the standard SAX |
| 3969 | * interface). |
| 3970 | * |
| 3971 | * Returns 0, -1 in case of error. the parser context is augmented |
| 3972 | * as a result of the parsing. |
| 3973 | */ |
| 3974 | |
Daniel Veillard | 1b31e4a | 2002-05-27 14:44:50 +0000 | [diff] [blame] | 3975 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3976 | htmlParseDocument(htmlParserCtxtPtr ctxt) { |
| 3977 | xmlDtdPtr dtd; |
| 3978 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 3979 | xmlInitParser(); |
| 3980 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3981 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3982 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3983 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3984 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3985 | "htmlParseDocument: context error\n", NULL, NULL); |
| 3986 | return(XML_ERR_INTERNAL_ERROR); |
| 3987 | } |
| 3988 | ctxt->html = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3989 | GROW; |
| 3990 | /* |
| 3991 | * SAX: beginning of the document processing. |
| 3992 | */ |
| 3993 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 3994 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 3995 | |
| 3996 | /* |
| 3997 | * Wipe out everything which is before the first '<' |
| 3998 | */ |
| 3999 | SKIP_BLANKS; |
| 4000 | if (CUR == 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4001 | htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 4002 | "Document is empty\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 4006 | ctxt->sax->startDocument(ctxt->userData); |
| 4007 | |
| 4008 | |
| 4009 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4010 | * Parse possible comments and PIs before any content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4011 | */ |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4012 | while (((CUR == '<') && (NXT(1) == '!') && |
| 4013 | (NXT(2) == '-') && (NXT(3) == '-')) || |
| 4014 | ((CUR == '<') && (NXT(1) == '?'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4015 | htmlParseComment(ctxt); |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4016 | htmlParsePI(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4017 | SKIP_BLANKS; |
| 4018 | } |
| 4019 | |
| 4020 | |
| 4021 | /* |
| 4022 | * Then possibly doc type declaration(s) and more Misc |
| 4023 | * (doctypedecl Misc*)? |
| 4024 | */ |
| 4025 | if ((CUR == '<') && (NXT(1) == '!') && |
| 4026 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4027 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4028 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4029 | (UPP(8) == 'E')) { |
| 4030 | htmlParseDocTypeDecl(ctxt); |
| 4031 | } |
| 4032 | SKIP_BLANKS; |
| 4033 | |
| 4034 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4035 | * Parse possible comments and PIs before any content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4036 | */ |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4037 | while (((CUR == '<') && (NXT(1) == '!') && |
| 4038 | (NXT(2) == '-') && (NXT(3) == '-')) || |
| 4039 | ((CUR == '<') && (NXT(1) == '?'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4040 | htmlParseComment(ctxt); |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4041 | htmlParsePI(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4042 | SKIP_BLANKS; |
| 4043 | } |
| 4044 | |
| 4045 | /* |
| 4046 | * Time to start parsing the tree itself |
| 4047 | */ |
| 4048 | htmlParseContent(ctxt); |
| 4049 | |
| 4050 | /* |
| 4051 | * autoclose |
| 4052 | */ |
| 4053 | if (CUR == 0) |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4054 | htmlAutoCloseOnEnd(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4055 | |
| 4056 | |
| 4057 | /* |
| 4058 | * SAX: end of the document processing. |
| 4059 | */ |
| 4060 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 4061 | ctxt->sax->endDocument(ctxt->userData); |
| 4062 | |
| 4063 | if (ctxt->myDoc != NULL) { |
| 4064 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 4065 | if (dtd == NULL) |
| 4066 | ctxt->myDoc->intSubset = |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4067 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4068 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
| 4069 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
| 4070 | } |
| 4071 | if (! ctxt->wellFormed) return(-1); |
| 4072 | return(0); |
| 4073 | } |
| 4074 | |
| 4075 | |
| 4076 | /************************************************************************ |
| 4077 | * * |
| 4078 | * Parser contexts handling * |
| 4079 | * * |
| 4080 | ************************************************************************/ |
| 4081 | |
| 4082 | /** |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 4083 | * htmlInitParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4084 | * @ctxt: an HTML parser context |
| 4085 | * |
| 4086 | * Initialize a parser context |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4087 | * |
| 4088 | * Returns 0 in case of success and -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4089 | */ |
| 4090 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4091 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | htmlInitParserCtxt(htmlParserCtxtPtr ctxt) |
| 4093 | { |
| 4094 | htmlSAXHandler *sax; |
| 4095 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4096 | if (ctxt == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4097 | memset(ctxt, 0, sizeof(htmlParserCtxt)); |
| 4098 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4099 | ctxt->dict = xmlDictCreate(); |
| 4100 | if (ctxt->dict == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4101 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
| 4102 | return(-1); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4104 | sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler)); |
| 4105 | if (sax == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4106 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
| 4107 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4108 | } |
| 4109 | else |
| 4110 | memset(sax, 0, sizeof(htmlSAXHandler)); |
| 4111 | |
| 4112 | /* Allocate the Input stack */ |
| 4113 | ctxt->inputTab = (htmlParserInputPtr *) |
| 4114 | xmlMalloc(5 * sizeof(htmlParserInputPtr)); |
| 4115 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4116 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4117 | ctxt->inputNr = 0; |
| 4118 | ctxt->inputMax = 0; |
| 4119 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4120 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4121 | } |
| 4122 | ctxt->inputNr = 0; |
| 4123 | ctxt->inputMax = 5; |
| 4124 | ctxt->input = NULL; |
| 4125 | ctxt->version = NULL; |
| 4126 | ctxt->encoding = NULL; |
| 4127 | ctxt->standalone = -1; |
| 4128 | ctxt->instate = XML_PARSER_START; |
| 4129 | |
| 4130 | /* Allocate the Node stack */ |
| 4131 | ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr)); |
| 4132 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4133 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4134 | ctxt->nodeNr = 0; |
| 4135 | ctxt->nodeMax = 0; |
| 4136 | ctxt->node = NULL; |
| 4137 | ctxt->inputNr = 0; |
| 4138 | ctxt->inputMax = 0; |
| 4139 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4140 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4141 | } |
| 4142 | ctxt->nodeNr = 0; |
| 4143 | ctxt->nodeMax = 10; |
| 4144 | ctxt->node = NULL; |
| 4145 | |
| 4146 | /* Allocate the Name stack */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4147 | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4148 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4149 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4150 | ctxt->nameNr = 0; |
| 4151 | ctxt->nameMax = 10; |
| 4152 | ctxt->name = NULL; |
| 4153 | ctxt->nodeNr = 0; |
| 4154 | ctxt->nodeMax = 0; |
| 4155 | ctxt->node = NULL; |
| 4156 | ctxt->inputNr = 0; |
| 4157 | ctxt->inputMax = 0; |
| 4158 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4159 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4160 | } |
| 4161 | ctxt->nameNr = 0; |
| 4162 | ctxt->nameMax = 10; |
| 4163 | ctxt->name = NULL; |
| 4164 | |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4165 | if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4166 | else { |
| 4167 | ctxt->sax = sax; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4168 | memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4169 | } |
| 4170 | ctxt->userData = ctxt; |
| 4171 | ctxt->myDoc = NULL; |
| 4172 | ctxt->wellFormed = 1; |
| 4173 | ctxt->replaceEntities = 0; |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 4174 | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4175 | ctxt->html = 1; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 4176 | ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0; |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 4177 | ctxt->vctxt.userData = ctxt; |
| 4178 | ctxt->vctxt.error = xmlParserValidityError; |
| 4179 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4180 | ctxt->record_info = 0; |
| 4181 | ctxt->validate = 0; |
| 4182 | ctxt->nbChars = 0; |
| 4183 | ctxt->checkIndex = 0; |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 4184 | ctxt->catalogs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4185 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4186 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | /** |
| 4190 | * htmlFreeParserCtxt: |
| 4191 | * @ctxt: an HTML parser context |
| 4192 | * |
| 4193 | * Free all the memory used by a parser context. However the parsed |
| 4194 | * document in ctxt->myDoc is not freed. |
| 4195 | */ |
| 4196 | |
| 4197 | void |
| 4198 | htmlFreeParserCtxt(htmlParserCtxtPtr ctxt) |
| 4199 | { |
| 4200 | xmlFreeParserCtxt(ctxt); |
| 4201 | } |
| 4202 | |
| 4203 | /** |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4204 | * htmlNewParserCtxt: |
| 4205 | * |
| 4206 | * Allocate and initialize a new parser context. |
| 4207 | * |
| 4208 | * Returns the xmlParserCtxtPtr or NULL |
| 4209 | */ |
| 4210 | |
| 4211 | static htmlParserCtxtPtr |
| 4212 | htmlNewParserCtxt(void) |
| 4213 | { |
| 4214 | xmlParserCtxtPtr ctxt; |
| 4215 | |
| 4216 | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
| 4217 | if (ctxt == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4218 | htmlErrMemory(NULL, "NewParserCtxt: out of memory\n"); |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4219 | return(NULL); |
| 4220 | } |
| 4221 | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4222 | if (htmlInitParserCtxt(ctxt) < 0) { |
| 4223 | htmlFreeParserCtxt(ctxt); |
| 4224 | return(NULL); |
| 4225 | } |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4226 | return(ctxt); |
| 4227 | } |
| 4228 | |
| 4229 | /** |
| 4230 | * htmlCreateMemoryParserCtxt: |
| 4231 | * @buffer: a pointer to a char array |
| 4232 | * @size: the size of the array |
| 4233 | * |
| 4234 | * Create a parser context for an HTML in-memory document. |
| 4235 | * |
| 4236 | * Returns the new parser context or NULL |
| 4237 | */ |
Daniel Veillard | 02ea141 | 2003-04-09 12:08:47 +0000 | [diff] [blame] | 4238 | htmlParserCtxtPtr |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4239 | htmlCreateMemoryParserCtxt(const char *buffer, int size) { |
| 4240 | xmlParserCtxtPtr ctxt; |
| 4241 | xmlParserInputPtr input; |
| 4242 | xmlParserInputBufferPtr buf; |
| 4243 | |
| 4244 | if (buffer == NULL) |
| 4245 | return(NULL); |
| 4246 | if (size <= 0) |
| 4247 | return(NULL); |
| 4248 | |
| 4249 | ctxt = htmlNewParserCtxt(); |
| 4250 | if (ctxt == NULL) |
| 4251 | return(NULL); |
| 4252 | |
| 4253 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 4254 | if (buf == NULL) return(NULL); |
| 4255 | |
| 4256 | input = xmlNewInputStream(ctxt); |
| 4257 | if (input == NULL) { |
| 4258 | xmlFreeParserCtxt(ctxt); |
| 4259 | return(NULL); |
| 4260 | } |
| 4261 | |
| 4262 | input->filename = NULL; |
| 4263 | input->buf = buf; |
| 4264 | input->base = input->buf->buffer->content; |
| 4265 | input->cur = input->buf->buffer->content; |
| 4266 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
| 4267 | |
| 4268 | inputPush(ctxt, input); |
| 4269 | return(ctxt); |
| 4270 | } |
| 4271 | |
| 4272 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4273 | * htmlCreateDocParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4274 | * @cur: a pointer to an array of xmlChar |
| 4275 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 4276 | * |
| 4277 | * Create a parser context for an HTML document. |
| 4278 | * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4279 | * TODO: check the need to add encoding handling there |
| 4280 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4281 | * Returns the new parser context or NULL |
| 4282 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4283 | static htmlParserCtxtPtr |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 4284 | htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) { |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4285 | int len; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4286 | htmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4287 | |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4288 | if (cur == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4289 | return(NULL); |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4290 | len = xmlStrlen(cur); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4291 | ctxt = htmlCreateMemoryParserCtxt((char *)cur, len); |
| 4292 | |
| 4293 | if (encoding != NULL) { |
| 4294 | xmlCharEncoding enc; |
| 4295 | xmlCharEncodingHandlerPtr handler; |
| 4296 | |
| 4297 | if (ctxt->input->encoding != NULL) |
| 4298 | xmlFree((xmlChar *) ctxt->input->encoding); |
Daniel Veillard | e8ed620 | 2003-08-14 23:39:01 +0000 | [diff] [blame] | 4299 | ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4300 | |
| 4301 | enc = xmlParseCharEncoding(encoding); |
| 4302 | /* |
| 4303 | * registered set of known encodings |
| 4304 | */ |
| 4305 | if (enc != XML_CHAR_ENCODING_ERROR) { |
| 4306 | xmlSwitchEncoding(ctxt, enc); |
| 4307 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4308 | htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 4309 | "Unsupported encoding %s\n", |
| 4310 | (const xmlChar *) encoding, NULL); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4311 | } |
| 4312 | } else { |
| 4313 | /* |
| 4314 | * fallback for unknown encodings |
| 4315 | */ |
| 4316 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 4317 | if (handler != NULL) { |
| 4318 | xmlSwitchToEncoding(ctxt, handler); |
| 4319 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4320 | htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 4321 | "Unsupported encoding %s\n", |
| 4322 | (const xmlChar *) encoding, NULL); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | } |
| 4326 | return(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 4329 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | /************************************************************************ |
| 4331 | * * |
| 4332 | * Progressive parsing interfaces * |
| 4333 | * * |
| 4334 | ************************************************************************/ |
| 4335 | |
| 4336 | /** |
| 4337 | * htmlParseLookupSequence: |
| 4338 | * @ctxt: an HTML parser context |
| 4339 | * @first: the first char to lookup |
| 4340 | * @next: the next char to lookup or zero |
| 4341 | * @third: the next char to lookup or zero |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 4342 | * @comment: flag to force checking inside comments |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4343 | * |
| 4344 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 4345 | * (first) is available in the input stream. |
| 4346 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 4347 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 4348 | * parser, do not use liberally. |
| 4349 | * This is basically similar to xmlParseLookupSequence() |
| 4350 | * |
| 4351 | * Returns the index to the current parsing point if the full sequence |
| 4352 | * is available, -1 otherwise. |
| 4353 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4354 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4355 | htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first, |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 4356 | xmlChar next, xmlChar third, int iscomment) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4357 | int base, len; |
| 4358 | htmlParserInputPtr in; |
| 4359 | const xmlChar *buf; |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4360 | int incomment = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4361 | |
| 4362 | in = ctxt->input; |
| 4363 | if (in == NULL) return(-1); |
| 4364 | base = in->cur - in->base; |
| 4365 | if (base < 0) return(-1); |
| 4366 | if (ctxt->checkIndex > base) |
| 4367 | base = ctxt->checkIndex; |
| 4368 | if (in->buf == NULL) { |
| 4369 | buf = in->base; |
| 4370 | len = in->length; |
| 4371 | } else { |
| 4372 | buf = in->buf->buffer->content; |
| 4373 | len = in->buf->buffer->use; |
| 4374 | } |
| 4375 | /* take into account the sequence length */ |
| 4376 | if (third) len -= 2; |
| 4377 | else if (next) len --; |
| 4378 | for (;base < len;base++) { |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 4379 | if (!incomment && (base + 4 < len) && !iscomment) { |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4380 | if ((buf[base] == '<') && (buf[base + 1] == '!') && |
| 4381 | (buf[base + 2] == '-') && (buf[base + 3] == '-')) { |
| 4382 | incomment = 1; |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4383 | /* do not increment past <! - some people use <!--> */ |
| 4384 | base += 2; |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4385 | } |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4386 | } |
| 4387 | if (incomment) { |
William M. Brack | 4a557d9 | 2003-07-29 04:28:04 +0000 | [diff] [blame] | 4388 | if (base + 3 > len) |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4389 | return(-1); |
| 4390 | if ((buf[base] == '-') && (buf[base + 1] == '-') && |
| 4391 | (buf[base + 2] == '>')) { |
| 4392 | incomment = 0; |
| 4393 | base += 2; |
| 4394 | } |
| 4395 | continue; |
| 4396 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4397 | if (buf[base] == first) { |
| 4398 | if (third != 0) { |
| 4399 | if ((buf[base + 1] != next) || |
| 4400 | (buf[base + 2] != third)) continue; |
| 4401 | } else if (next != 0) { |
| 4402 | if (buf[base + 1] != next) continue; |
| 4403 | } |
| 4404 | ctxt->checkIndex = 0; |
| 4405 | #ifdef DEBUG_PUSH |
| 4406 | if (next == 0) |
| 4407 | xmlGenericError(xmlGenericErrorContext, |
| 4408 | "HPP: lookup '%c' found at %d\n", |
| 4409 | first, base); |
| 4410 | else if (third == 0) |
| 4411 | xmlGenericError(xmlGenericErrorContext, |
| 4412 | "HPP: lookup '%c%c' found at %d\n", |
| 4413 | first, next, base); |
| 4414 | else |
| 4415 | xmlGenericError(xmlGenericErrorContext, |
| 4416 | "HPP: lookup '%c%c%c' found at %d\n", |
| 4417 | first, next, third, base); |
| 4418 | #endif |
| 4419 | return(base - (in->cur - in->base)); |
| 4420 | } |
| 4421 | } |
| 4422 | ctxt->checkIndex = base; |
| 4423 | #ifdef DEBUG_PUSH |
| 4424 | if (next == 0) |
| 4425 | xmlGenericError(xmlGenericErrorContext, |
| 4426 | "HPP: lookup '%c' failed\n", first); |
| 4427 | else if (third == 0) |
| 4428 | xmlGenericError(xmlGenericErrorContext, |
| 4429 | "HPP: lookup '%c%c' failed\n", first, next); |
| 4430 | else |
| 4431 | xmlGenericError(xmlGenericErrorContext, |
| 4432 | "HPP: lookup '%c%c%c' failed\n", first, next, third); |
| 4433 | #endif |
| 4434 | return(-1); |
| 4435 | } |
| 4436 | |
| 4437 | /** |
| 4438 | * htmlParseTryOrFinish: |
| 4439 | * @ctxt: an HTML parser context |
| 4440 | * @terminate: last chunk indicator |
| 4441 | * |
| 4442 | * Try to progress on parsing |
| 4443 | * |
| 4444 | * Returns zero if no parsing was possible |
| 4445 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4446 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) { |
| 4448 | int ret = 0; |
| 4449 | htmlParserInputPtr in; |
| 4450 | int avail = 0; |
| 4451 | xmlChar cur, next; |
| 4452 | |
| 4453 | #ifdef DEBUG_PUSH |
| 4454 | switch (ctxt->instate) { |
| 4455 | case XML_PARSER_EOF: |
| 4456 | xmlGenericError(xmlGenericErrorContext, |
| 4457 | "HPP: try EOF\n"); break; |
| 4458 | case XML_PARSER_START: |
| 4459 | xmlGenericError(xmlGenericErrorContext, |
| 4460 | "HPP: try START\n"); break; |
| 4461 | case XML_PARSER_MISC: |
| 4462 | xmlGenericError(xmlGenericErrorContext, |
| 4463 | "HPP: try MISC\n");break; |
| 4464 | case XML_PARSER_COMMENT: |
| 4465 | xmlGenericError(xmlGenericErrorContext, |
| 4466 | "HPP: try COMMENT\n");break; |
| 4467 | case XML_PARSER_PROLOG: |
| 4468 | xmlGenericError(xmlGenericErrorContext, |
| 4469 | "HPP: try PROLOG\n");break; |
| 4470 | case XML_PARSER_START_TAG: |
| 4471 | xmlGenericError(xmlGenericErrorContext, |
| 4472 | "HPP: try START_TAG\n");break; |
| 4473 | case XML_PARSER_CONTENT: |
| 4474 | xmlGenericError(xmlGenericErrorContext, |
| 4475 | "HPP: try CONTENT\n");break; |
| 4476 | case XML_PARSER_CDATA_SECTION: |
| 4477 | xmlGenericError(xmlGenericErrorContext, |
| 4478 | "HPP: try CDATA_SECTION\n");break; |
| 4479 | case XML_PARSER_END_TAG: |
| 4480 | xmlGenericError(xmlGenericErrorContext, |
| 4481 | "HPP: try END_TAG\n");break; |
| 4482 | case XML_PARSER_ENTITY_DECL: |
| 4483 | xmlGenericError(xmlGenericErrorContext, |
| 4484 | "HPP: try ENTITY_DECL\n");break; |
| 4485 | case XML_PARSER_ENTITY_VALUE: |
| 4486 | xmlGenericError(xmlGenericErrorContext, |
| 4487 | "HPP: try ENTITY_VALUE\n");break; |
| 4488 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 4489 | xmlGenericError(xmlGenericErrorContext, |
| 4490 | "HPP: try ATTRIBUTE_VALUE\n");break; |
| 4491 | case XML_PARSER_DTD: |
| 4492 | xmlGenericError(xmlGenericErrorContext, |
| 4493 | "HPP: try DTD\n");break; |
| 4494 | case XML_PARSER_EPILOG: |
| 4495 | xmlGenericError(xmlGenericErrorContext, |
| 4496 | "HPP: try EPILOG\n");break; |
| 4497 | case XML_PARSER_PI: |
| 4498 | xmlGenericError(xmlGenericErrorContext, |
| 4499 | "HPP: try PI\n");break; |
| 4500 | case XML_PARSER_SYSTEM_LITERAL: |
| 4501 | xmlGenericError(xmlGenericErrorContext, |
| 4502 | "HPP: try SYSTEM_LITERAL\n");break; |
| 4503 | } |
| 4504 | #endif |
| 4505 | |
| 4506 | while (1) { |
| 4507 | |
| 4508 | in = ctxt->input; |
| 4509 | if (in == NULL) break; |
| 4510 | if (in->buf == NULL) |
| 4511 | avail = in->length - (in->cur - in->base); |
| 4512 | else |
| 4513 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4514 | if ((avail == 0) && (terminate)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4515 | htmlAutoCloseOnEnd(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4516 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
| 4517 | /* |
| 4518 | * SAX: end of the document processing. |
| 4519 | */ |
| 4520 | ctxt->instate = XML_PARSER_EOF; |
| 4521 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 4522 | ctxt->sax->endDocument(ctxt->userData); |
| 4523 | } |
| 4524 | } |
| 4525 | if (avail < 1) |
| 4526 | goto done; |
Daniel Veillard | 45269b8 | 2003-04-22 13:21:57 +0000 | [diff] [blame] | 4527 | cur = in->cur[0]; |
| 4528 | if (cur == 0) { |
| 4529 | SKIP(1); |
| 4530 | continue; |
| 4531 | } |
| 4532 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4533 | switch (ctxt->instate) { |
| 4534 | case XML_PARSER_EOF: |
| 4535 | /* |
| 4536 | * Document parsing is done ! |
| 4537 | */ |
| 4538 | goto done; |
| 4539 | case XML_PARSER_START: |
| 4540 | /* |
| 4541 | * Very first chars read from the document flow. |
| 4542 | */ |
| 4543 | cur = in->cur[0]; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4544 | if (IS_BLANK_CH(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4545 | SKIP_BLANKS; |
| 4546 | if (in->buf == NULL) |
| 4547 | avail = in->length - (in->cur - in->base); |
| 4548 | else |
| 4549 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4550 | } |
| 4551 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 4552 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 4553 | &xmlDefaultSAXLocator); |
| 4554 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 4555 | (!ctxt->disableSAX)) |
| 4556 | ctxt->sax->startDocument(ctxt->userData); |
| 4557 | |
| 4558 | cur = in->cur[0]; |
| 4559 | next = in->cur[1]; |
| 4560 | if ((cur == '<') && (next == '!') && |
| 4561 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4562 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4563 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4564 | (UPP(8) == 'E')) { |
| 4565 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4566 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4567 | goto done; |
| 4568 | #ifdef DEBUG_PUSH |
| 4569 | xmlGenericError(xmlGenericErrorContext, |
| 4570 | "HPP: Parsing internal subset\n"); |
| 4571 | #endif |
| 4572 | htmlParseDocTypeDecl(ctxt); |
| 4573 | ctxt->instate = XML_PARSER_PROLOG; |
| 4574 | #ifdef DEBUG_PUSH |
| 4575 | xmlGenericError(xmlGenericErrorContext, |
| 4576 | "HPP: entering PROLOG\n"); |
| 4577 | #endif |
| 4578 | } else { |
| 4579 | ctxt->instate = XML_PARSER_MISC; |
| 4580 | } |
| 4581 | #ifdef DEBUG_PUSH |
| 4582 | xmlGenericError(xmlGenericErrorContext, |
| 4583 | "HPP: entering MISC\n"); |
| 4584 | #endif |
| 4585 | break; |
| 4586 | case XML_PARSER_MISC: |
| 4587 | SKIP_BLANKS; |
| 4588 | if (in->buf == NULL) |
| 4589 | avail = in->length - (in->cur - in->base); |
| 4590 | else |
| 4591 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4592 | if (avail < 2) |
| 4593 | goto done; |
| 4594 | cur = in->cur[0]; |
| 4595 | next = in->cur[1]; |
| 4596 | if ((cur == '<') && (next == '!') && |
| 4597 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 4598 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4599 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | goto done; |
| 4601 | #ifdef DEBUG_PUSH |
| 4602 | xmlGenericError(xmlGenericErrorContext, |
| 4603 | "HPP: Parsing Comment\n"); |
| 4604 | #endif |
| 4605 | htmlParseComment(ctxt); |
| 4606 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4607 | } else if ((cur == '<') && (next == '?')) { |
| 4608 | if ((!terminate) && |
| 4609 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
| 4610 | goto done; |
| 4611 | #ifdef DEBUG_PUSH |
| 4612 | xmlGenericError(xmlGenericErrorContext, |
| 4613 | "HPP: Parsing PI\n"); |
| 4614 | #endif |
| 4615 | htmlParsePI(ctxt); |
| 4616 | ctxt->instate = XML_PARSER_MISC; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4617 | } else if ((cur == '<') && (next == '!') && |
| 4618 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4619 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4620 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4621 | (UPP(8) == 'E')) { |
| 4622 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4623 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4624 | goto done; |
| 4625 | #ifdef DEBUG_PUSH |
| 4626 | xmlGenericError(xmlGenericErrorContext, |
| 4627 | "HPP: Parsing internal subset\n"); |
| 4628 | #endif |
| 4629 | htmlParseDocTypeDecl(ctxt); |
| 4630 | ctxt->instate = XML_PARSER_PROLOG; |
| 4631 | #ifdef DEBUG_PUSH |
| 4632 | xmlGenericError(xmlGenericErrorContext, |
| 4633 | "HPP: entering PROLOG\n"); |
| 4634 | #endif |
| 4635 | } else if ((cur == '<') && (next == '!') && |
| 4636 | (avail < 9)) { |
| 4637 | goto done; |
| 4638 | } else { |
| 4639 | ctxt->instate = XML_PARSER_START_TAG; |
| 4640 | #ifdef DEBUG_PUSH |
| 4641 | xmlGenericError(xmlGenericErrorContext, |
| 4642 | "HPP: entering START_TAG\n"); |
| 4643 | #endif |
| 4644 | } |
| 4645 | break; |
| 4646 | case XML_PARSER_PROLOG: |
| 4647 | SKIP_BLANKS; |
| 4648 | if (in->buf == NULL) |
| 4649 | avail = in->length - (in->cur - in->base); |
| 4650 | else |
| 4651 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4652 | if (avail < 2) |
| 4653 | goto done; |
| 4654 | cur = in->cur[0]; |
| 4655 | next = in->cur[1]; |
| 4656 | if ((cur == '<') && (next == '!') && |
| 4657 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 4658 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4659 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4660 | goto done; |
| 4661 | #ifdef DEBUG_PUSH |
| 4662 | xmlGenericError(xmlGenericErrorContext, |
| 4663 | "HPP: Parsing Comment\n"); |
| 4664 | #endif |
| 4665 | htmlParseComment(ctxt); |
| 4666 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4667 | } else if ((cur == '<') && (next == '?')) { |
| 4668 | if ((!terminate) && |
| 4669 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
| 4670 | goto done; |
| 4671 | #ifdef DEBUG_PUSH |
| 4672 | xmlGenericError(xmlGenericErrorContext, |
| 4673 | "HPP: Parsing PI\n"); |
| 4674 | #endif |
| 4675 | htmlParsePI(ctxt); |
| 4676 | ctxt->instate = XML_PARSER_PROLOG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4677 | } else if ((cur == '<') && (next == '!') && |
| 4678 | (avail < 4)) { |
| 4679 | goto done; |
| 4680 | } else { |
| 4681 | ctxt->instate = XML_PARSER_START_TAG; |
| 4682 | #ifdef DEBUG_PUSH |
| 4683 | xmlGenericError(xmlGenericErrorContext, |
| 4684 | "HPP: entering START_TAG\n"); |
| 4685 | #endif |
| 4686 | } |
| 4687 | break; |
| 4688 | case XML_PARSER_EPILOG: |
| 4689 | if (in->buf == NULL) |
| 4690 | avail = in->length - (in->cur - in->base); |
| 4691 | else |
| 4692 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4693 | if (avail < 1) |
| 4694 | goto done; |
| 4695 | cur = in->cur[0]; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4696 | if (IS_BLANK_CH(cur)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4697 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4698 | goto done; |
| 4699 | } |
| 4700 | if (avail < 2) |
| 4701 | goto done; |
| 4702 | next = in->cur[1]; |
| 4703 | if ((cur == '<') && (next == '!') && |
| 4704 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 4705 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4706 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4707 | goto done; |
| 4708 | #ifdef DEBUG_PUSH |
| 4709 | xmlGenericError(xmlGenericErrorContext, |
| 4710 | "HPP: Parsing Comment\n"); |
| 4711 | #endif |
| 4712 | htmlParseComment(ctxt); |
| 4713 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4714 | } else if ((cur == '<') && (next == '?')) { |
| 4715 | if ((!terminate) && |
| 4716 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
| 4717 | goto done; |
| 4718 | #ifdef DEBUG_PUSH |
| 4719 | xmlGenericError(xmlGenericErrorContext, |
| 4720 | "HPP: Parsing PI\n"); |
| 4721 | #endif |
| 4722 | htmlParsePI(ctxt); |
| 4723 | ctxt->instate = XML_PARSER_EPILOG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | } else if ((cur == '<') && (next == '!') && |
| 4725 | (avail < 4)) { |
| 4726 | goto done; |
| 4727 | } else { |
| 4728 | ctxt->errNo = XML_ERR_DOCUMENT_END; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4729 | ctxt->wellFormed = 0; |
| 4730 | ctxt->instate = XML_PARSER_EOF; |
| 4731 | #ifdef DEBUG_PUSH |
| 4732 | xmlGenericError(xmlGenericErrorContext, |
| 4733 | "HPP: entering EOF\n"); |
| 4734 | #endif |
| 4735 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 4736 | ctxt->sax->endDocument(ctxt->userData); |
| 4737 | goto done; |
| 4738 | } |
| 4739 | break; |
| 4740 | case XML_PARSER_START_TAG: { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4741 | const xmlChar *name, *oldname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4742 | int depth = ctxt->nameNr; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 4743 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4744 | |
| 4745 | if (avail < 2) |
| 4746 | goto done; |
| 4747 | cur = in->cur[0]; |
| 4748 | if (cur != '<') { |
| 4749 | ctxt->instate = XML_PARSER_CONTENT; |
| 4750 | #ifdef DEBUG_PUSH |
| 4751 | xmlGenericError(xmlGenericErrorContext, |
| 4752 | "HPP: entering CONTENT\n"); |
| 4753 | #endif |
| 4754 | break; |
| 4755 | } |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 4756 | if (in->cur[1] == '/') { |
| 4757 | ctxt->instate = XML_PARSER_END_TAG; |
| 4758 | ctxt->checkIndex = 0; |
| 4759 | #ifdef DEBUG_PUSH |
| 4760 | xmlGenericError(xmlGenericErrorContext, |
| 4761 | "HPP: entering END_TAG\n"); |
| 4762 | #endif |
| 4763 | break; |
| 4764 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4765 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4766 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4767 | goto done; |
| 4768 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4769 | oldname = ctxt->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4770 | htmlParseStartTag(ctxt); |
| 4771 | name = ctxt->name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4772 | if (((depth == ctxt->nameNr) && |
| 4773 | (xmlStrEqual(oldname, ctxt->name))) || |
| 4774 | (name == NULL)) { |
| 4775 | if (CUR == '>') |
| 4776 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4777 | break; |
| 4778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | |
| 4780 | /* |
| 4781 | * Lookup the info for that element. |
| 4782 | */ |
| 4783 | info = htmlTagLookup(name); |
| 4784 | if (info == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4785 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
| 4786 | "Tag %s invalid\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4790 | * Check for an Empty Element labeled the XML/SGML way |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4791 | */ |
| 4792 | if ((CUR == '/') && (NXT(1) == '>')) { |
| 4793 | SKIP(2); |
| 4794 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 4795 | ctxt->sax->endElement(ctxt->userData, name); |
| 4796 | oldname = htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4797 | ctxt->instate = XML_PARSER_CONTENT; |
| 4798 | #ifdef DEBUG_PUSH |
| 4799 | xmlGenericError(xmlGenericErrorContext, |
| 4800 | "HPP: entering CONTENT\n"); |
| 4801 | #endif |
| 4802 | break; |
| 4803 | } |
| 4804 | |
| 4805 | if (CUR == '>') { |
| 4806 | NEXT; |
| 4807 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4808 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 4809 | "Couldn't find end of Start Tag %s\n", |
| 4810 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4811 | |
| 4812 | /* |
| 4813 | * end of parsing of this node. |
| 4814 | */ |
| 4815 | if (xmlStrEqual(name, ctxt->name)) { |
| 4816 | nodePop(ctxt); |
| 4817 | oldname = htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | ctxt->instate = XML_PARSER_CONTENT; |
| 4821 | #ifdef DEBUG_PUSH |
| 4822 | xmlGenericError(xmlGenericErrorContext, |
| 4823 | "HPP: entering CONTENT\n"); |
| 4824 | #endif |
| 4825 | break; |
| 4826 | } |
| 4827 | |
| 4828 | /* |
| 4829 | * Check for an Empty Element from DTD definition |
| 4830 | */ |
| 4831 | if ((info != NULL) && (info->empty)) { |
| 4832 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 4833 | ctxt->sax->endElement(ctxt->userData, name); |
| 4834 | oldname = htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4835 | } |
| 4836 | ctxt->instate = XML_PARSER_CONTENT; |
| 4837 | #ifdef DEBUG_PUSH |
| 4838 | xmlGenericError(xmlGenericErrorContext, |
| 4839 | "HPP: entering CONTENT\n"); |
| 4840 | #endif |
| 4841 | break; |
| 4842 | } |
| 4843 | case XML_PARSER_CONTENT: { |
| 4844 | long cons; |
| 4845 | /* |
| 4846 | * Handle preparsed entities and charRef |
| 4847 | */ |
| 4848 | if (ctxt->token != 0) { |
| 4849 | xmlChar chr[2] = { 0 , 0 } ; |
| 4850 | |
| 4851 | chr[0] = (xmlChar) ctxt->token; |
| 4852 | htmlCheckParagraph(ctxt); |
| 4853 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 4854 | ctxt->sax->characters(ctxt->userData, chr, 1); |
| 4855 | ctxt->token = 0; |
| 4856 | ctxt->checkIndex = 0; |
| 4857 | } |
| 4858 | if ((avail == 1) && (terminate)) { |
| 4859 | cur = in->cur[0]; |
| 4860 | if ((cur != '<') && (cur != '&')) { |
| 4861 | if (ctxt->sax != NULL) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4862 | if (IS_BLANK_CH(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4864 | ctxt->sax->ignorableWhitespace( |
| 4865 | ctxt->userData, &cur, 1); |
| 4866 | } else { |
| 4867 | htmlCheckParagraph(ctxt); |
| 4868 | if (ctxt->sax->characters != NULL) |
| 4869 | ctxt->sax->characters( |
| 4870 | ctxt->userData, &cur, 1); |
| 4871 | } |
| 4872 | } |
| 4873 | ctxt->token = 0; |
| 4874 | ctxt->checkIndex = 0; |
Daniel Veillard | bc6e1a3 | 2002-11-18 15:07:25 +0000 | [diff] [blame] | 4875 | in->cur++; |
William M. Brack | 1633d18 | 2001-10-05 15:41:19 +0000 | [diff] [blame] | 4876 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4877 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4878 | } |
| 4879 | if (avail < 2) |
| 4880 | goto done; |
| 4881 | cur = in->cur[0]; |
| 4882 | next = in->cur[1]; |
| 4883 | cons = ctxt->nbChars; |
| 4884 | if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) || |
| 4885 | (xmlStrEqual(ctxt->name, BAD_CAST"style"))) { |
| 4886 | /* |
| 4887 | * Handle SCRIPT/STYLE separately |
| 4888 | */ |
| 4889 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4890 | (htmlParseLookupSequence(ctxt, '<', '/', 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4891 | goto done; |
| 4892 | htmlParseScript(ctxt); |
| 4893 | if ((cur == '<') && (next == '/')) { |
| 4894 | ctxt->instate = XML_PARSER_END_TAG; |
| 4895 | ctxt->checkIndex = 0; |
| 4896 | #ifdef DEBUG_PUSH |
| 4897 | xmlGenericError(xmlGenericErrorContext, |
| 4898 | "HPP: entering END_TAG\n"); |
| 4899 | #endif |
| 4900 | break; |
| 4901 | } |
| 4902 | } else { |
| 4903 | /* |
| 4904 | * Sometimes DOCTYPE arrives in the middle of the document |
| 4905 | */ |
| 4906 | if ((cur == '<') && (next == '!') && |
| 4907 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4908 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4909 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4910 | (UPP(8) == 'E')) { |
| 4911 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4912 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4913 | goto done; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4914 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 4915 | "Misplaced DOCTYPE declaration\n", |
| 4916 | BAD_CAST "DOCTYPE" , NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4917 | htmlParseDocTypeDecl(ctxt); |
| 4918 | } else if ((cur == '<') && (next == '!') && |
| 4919 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 4920 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4921 | (htmlParseLookupSequence( |
| 4922 | ctxt, '-', '-', '>', 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4923 | goto done; |
| 4924 | #ifdef DEBUG_PUSH |
| 4925 | xmlGenericError(xmlGenericErrorContext, |
| 4926 | "HPP: Parsing Comment\n"); |
| 4927 | #endif |
| 4928 | htmlParseComment(ctxt); |
| 4929 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4930 | } else if ((cur == '<') && (next == '?')) { |
| 4931 | if ((!terminate) && |
| 4932 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
| 4933 | goto done; |
| 4934 | #ifdef DEBUG_PUSH |
| 4935 | xmlGenericError(xmlGenericErrorContext, |
| 4936 | "HPP: Parsing PI\n"); |
| 4937 | #endif |
| 4938 | htmlParsePI(ctxt); |
| 4939 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4940 | } else if ((cur == '<') && (next == '!') && (avail < 4)) { |
| 4941 | goto done; |
| 4942 | } else if ((cur == '<') && (next == '/')) { |
| 4943 | ctxt->instate = XML_PARSER_END_TAG; |
| 4944 | ctxt->checkIndex = 0; |
| 4945 | #ifdef DEBUG_PUSH |
| 4946 | xmlGenericError(xmlGenericErrorContext, |
| 4947 | "HPP: entering END_TAG\n"); |
| 4948 | #endif |
| 4949 | break; |
| 4950 | } else if (cur == '<') { |
| 4951 | ctxt->instate = XML_PARSER_START_TAG; |
| 4952 | ctxt->checkIndex = 0; |
| 4953 | #ifdef DEBUG_PUSH |
| 4954 | xmlGenericError(xmlGenericErrorContext, |
| 4955 | "HPP: entering START_TAG\n"); |
| 4956 | #endif |
| 4957 | break; |
| 4958 | } else if (cur == '&') { |
| 4959 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 4960 | (htmlParseLookupSequence(ctxt, ';', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4961 | goto done; |
| 4962 | #ifdef DEBUG_PUSH |
| 4963 | xmlGenericError(xmlGenericErrorContext, |
| 4964 | "HPP: Parsing Reference\n"); |
| 4965 | #endif |
| 4966 | /* TODO: check generation of subtrees if noent !!! */ |
| 4967 | htmlParseReference(ctxt); |
| 4968 | } else { |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 4969 | /* |
| 4970 | * check that the text sequence is complete |
| 4971 | * before handing out the data to the parser |
| 4972 | * to avoid problems with erroneous end of |
| 4973 | * data detection. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4974 | */ |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 4975 | if ((!terminate) && |
| 4976 | (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0)) |
| 4977 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4978 | ctxt->checkIndex = 0; |
| 4979 | #ifdef DEBUG_PUSH |
| 4980 | xmlGenericError(xmlGenericErrorContext, |
| 4981 | "HPP: Parsing char data\n"); |
| 4982 | #endif |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4983 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4984 | } |
| 4985 | } |
| 4986 | if (cons == ctxt->nbChars) { |
| 4987 | if (ctxt->node != NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4988 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4989 | "detected an error in element content\n", |
| 4990 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4991 | } |
| 4992 | NEXT; |
| 4993 | break; |
| 4994 | } |
| 4995 | |
| 4996 | break; |
| 4997 | } |
| 4998 | case XML_PARSER_END_TAG: |
| 4999 | if (avail < 2) |
| 5000 | goto done; |
| 5001 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 5002 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5003 | goto done; |
| 5004 | htmlParseEndTag(ctxt); |
| 5005 | if (ctxt->nameNr == 0) { |
| 5006 | ctxt->instate = XML_PARSER_EPILOG; |
| 5007 | } else { |
| 5008 | ctxt->instate = XML_PARSER_CONTENT; |
| 5009 | } |
| 5010 | ctxt->checkIndex = 0; |
| 5011 | #ifdef DEBUG_PUSH |
| 5012 | xmlGenericError(xmlGenericErrorContext, |
| 5013 | "HPP: entering CONTENT\n"); |
| 5014 | #endif |
| 5015 | break; |
| 5016 | case XML_PARSER_CDATA_SECTION: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5017 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5018 | "HPP: internal error, state == CDATA\n", |
| 5019 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5020 | ctxt->instate = XML_PARSER_CONTENT; |
| 5021 | ctxt->checkIndex = 0; |
| 5022 | #ifdef DEBUG_PUSH |
| 5023 | xmlGenericError(xmlGenericErrorContext, |
| 5024 | "HPP: entering CONTENT\n"); |
| 5025 | #endif |
| 5026 | break; |
| 5027 | case XML_PARSER_DTD: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5028 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5029 | "HPP: internal error, state == DTD\n", |
| 5030 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5031 | ctxt->instate = XML_PARSER_CONTENT; |
| 5032 | ctxt->checkIndex = 0; |
| 5033 | #ifdef DEBUG_PUSH |
| 5034 | xmlGenericError(xmlGenericErrorContext, |
| 5035 | "HPP: entering CONTENT\n"); |
| 5036 | #endif |
| 5037 | break; |
| 5038 | case XML_PARSER_COMMENT: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5039 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5040 | "HPP: internal error, state == COMMENT\n", |
| 5041 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5042 | ctxt->instate = XML_PARSER_CONTENT; |
| 5043 | ctxt->checkIndex = 0; |
| 5044 | #ifdef DEBUG_PUSH |
| 5045 | xmlGenericError(xmlGenericErrorContext, |
| 5046 | "HPP: entering CONTENT\n"); |
| 5047 | #endif |
| 5048 | break; |
| 5049 | case XML_PARSER_PI: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5050 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5051 | "HPP: internal error, state == PI\n", |
| 5052 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | ctxt->instate = XML_PARSER_CONTENT; |
| 5054 | ctxt->checkIndex = 0; |
| 5055 | #ifdef DEBUG_PUSH |
| 5056 | xmlGenericError(xmlGenericErrorContext, |
| 5057 | "HPP: entering CONTENT\n"); |
| 5058 | #endif |
| 5059 | break; |
| 5060 | case XML_PARSER_ENTITY_DECL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5061 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5062 | "HPP: internal error, state == ENTITY_DECL\n", |
| 5063 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5064 | ctxt->instate = XML_PARSER_CONTENT; |
| 5065 | ctxt->checkIndex = 0; |
| 5066 | #ifdef DEBUG_PUSH |
| 5067 | xmlGenericError(xmlGenericErrorContext, |
| 5068 | "HPP: entering CONTENT\n"); |
| 5069 | #endif |
| 5070 | break; |
| 5071 | case XML_PARSER_ENTITY_VALUE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5072 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5073 | "HPP: internal error, state == ENTITY_VALUE\n", |
| 5074 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5075 | ctxt->instate = XML_PARSER_CONTENT; |
| 5076 | ctxt->checkIndex = 0; |
| 5077 | #ifdef DEBUG_PUSH |
| 5078 | xmlGenericError(xmlGenericErrorContext, |
| 5079 | "HPP: entering DTD\n"); |
| 5080 | #endif |
| 5081 | break; |
| 5082 | case XML_PARSER_ATTRIBUTE_VALUE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5083 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5084 | "HPP: internal error, state == ATTRIBUTE_VALUE\n", |
| 5085 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5086 | ctxt->instate = XML_PARSER_START_TAG; |
| 5087 | ctxt->checkIndex = 0; |
| 5088 | #ifdef DEBUG_PUSH |
| 5089 | xmlGenericError(xmlGenericErrorContext, |
| 5090 | "HPP: entering START_TAG\n"); |
| 5091 | #endif |
| 5092 | break; |
| 5093 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5094 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5095 | "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n", |
| 5096 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5097 | ctxt->instate = XML_PARSER_CONTENT; |
| 5098 | ctxt->checkIndex = 0; |
| 5099 | #ifdef DEBUG_PUSH |
| 5100 | xmlGenericError(xmlGenericErrorContext, |
| 5101 | "HPP: entering CONTENT\n"); |
| 5102 | #endif |
| 5103 | break; |
| 5104 | case XML_PARSER_IGNORE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5105 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5106 | "HPP: internal error, state == XML_PARSER_IGNORE\n", |
| 5107 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5108 | ctxt->instate = XML_PARSER_CONTENT; |
| 5109 | ctxt->checkIndex = 0; |
| 5110 | #ifdef DEBUG_PUSH |
| 5111 | xmlGenericError(xmlGenericErrorContext, |
| 5112 | "HPP: entering CONTENT\n"); |
| 5113 | #endif |
| 5114 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5115 | case XML_PARSER_PUBLIC_LITERAL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5116 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5117 | "HPP: internal error, state == XML_PARSER_LITERAL\n", |
| 5118 | NULL, NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5119 | ctxt->instate = XML_PARSER_CONTENT; |
| 5120 | ctxt->checkIndex = 0; |
| 5121 | #ifdef DEBUG_PUSH |
| 5122 | xmlGenericError(xmlGenericErrorContext, |
| 5123 | "HPP: entering CONTENT\n"); |
| 5124 | #endif |
| 5125 | break; |
| 5126 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | } |
| 5128 | } |
| 5129 | done: |
| 5130 | if ((avail == 0) && (terminate)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 5131 | htmlAutoCloseOnEnd(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5132 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
| 5133 | /* |
| 5134 | * SAX: end of the document processing. |
| 5135 | */ |
| 5136 | ctxt->instate = XML_PARSER_EOF; |
| 5137 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 5138 | ctxt->sax->endDocument(ctxt->userData); |
| 5139 | } |
| 5140 | } |
| 5141 | if ((ctxt->myDoc != NULL) && |
| 5142 | ((terminate) || (ctxt->instate == XML_PARSER_EOF) || |
| 5143 | (ctxt->instate == XML_PARSER_EPILOG))) { |
| 5144 | xmlDtdPtr dtd; |
| 5145 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 5146 | if (dtd == NULL) |
| 5147 | ctxt->myDoc->intSubset = |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 5148 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5149 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
| 5150 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
| 5151 | } |
| 5152 | #ifdef DEBUG_PUSH |
| 5153 | xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret); |
| 5154 | #endif |
| 5155 | return(ret); |
| 5156 | } |
| 5157 | |
| 5158 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5159 | * htmlParseChunk: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5160 | * @ctxt: an HTML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5161 | * @chunk: an char array |
| 5162 | * @size: the size in byte of the chunk |
| 5163 | * @terminate: last chunk indicator |
| 5164 | * |
| 5165 | * Parse a Chunk of memory |
| 5166 | * |
| 5167 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 5168 | */ |
| 5169 | int |
| 5170 | htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 5171 | int terminate) { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5172 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 5173 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5174 | "htmlParseChunk: context error\n", NULL, NULL); |
| 5175 | return(XML_ERR_INTERNAL_ERROR); |
| 5176 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 5178 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 5179 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 5180 | int cur = ctxt->input->cur - ctxt->input->base; |
| 5181 | |
| 5182 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 5183 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 5184 | ctxt->input->cur = ctxt->input->base + cur; |
| 5185 | #ifdef DEBUG_PUSH |
| 5186 | xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); |
| 5187 | #endif |
| 5188 | |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5189 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | if ((terminate) || (ctxt->input->buf->buffer->use > 80)) |
| 5191 | htmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5192 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5193 | } else if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5194 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 5195 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 5196 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 5197 | (in->raw != NULL)) { |
| 5198 | int nbchars; |
| 5199 | |
| 5200 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 5201 | if (nbchars < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5202 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 5203 | "encoder error\n", NULL, NULL); |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5204 | return(XML_ERR_INVALID_ENCODING); |
| 5205 | } |
| 5206 | } |
| 5207 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5208 | } |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5209 | htmlParseTryOrFinish(ctxt, terminate); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5210 | if (terminate) { |
| 5211 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 5212 | (ctxt->instate != XML_PARSER_EPILOG) && |
| 5213 | (ctxt->instate != XML_PARSER_MISC)) { |
| 5214 | ctxt->errNo = XML_ERR_DOCUMENT_END; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5215 | ctxt->wellFormed = 0; |
| 5216 | } |
| 5217 | if (ctxt->instate != XML_PARSER_EOF) { |
| 5218 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 5219 | ctxt->sax->endDocument(ctxt->userData); |
| 5220 | } |
| 5221 | ctxt->instate = XML_PARSER_EOF; |
| 5222 | } |
| 5223 | return((xmlParserErrors) ctxt->errNo); |
| 5224 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 5225 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5226 | |
| 5227 | /************************************************************************ |
| 5228 | * * |
| 5229 | * User entry points * |
| 5230 | * * |
| 5231 | ************************************************************************/ |
| 5232 | |
| 5233 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5234 | * htmlCreatePushParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5235 | * @sax: a SAX handler |
| 5236 | * @user_data: The user data returned on SAX callbacks |
| 5237 | * @chunk: a pointer to an array of chars |
| 5238 | * @size: number of chars in the array |
| 5239 | * @filename: an optional file name or URI |
| 5240 | * @enc: an optional encoding |
| 5241 | * |
| 5242 | * Create a parser context for using the HTML parser in push mode |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5243 | * The value of @filename is used for fetching external entities |
| 5244 | * and error/warning reports. |
| 5245 | * |
| 5246 | * Returns the new parser context or NULL |
| 5247 | */ |
| 5248 | htmlParserCtxtPtr |
| 5249 | htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, |
| 5250 | const char *chunk, int size, const char *filename, |
| 5251 | xmlCharEncoding enc) { |
| 5252 | htmlParserCtxtPtr ctxt; |
| 5253 | htmlParserInputPtr inputStream; |
| 5254 | xmlParserInputBufferPtr buf; |
| 5255 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5256 | xmlInitParser(); |
| 5257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5258 | buf = xmlAllocParserInputBuffer(enc); |
| 5259 | if (buf == NULL) return(NULL); |
| 5260 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5261 | ctxt = htmlNewParserCtxt(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | if (ctxt == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5263 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5264 | return(NULL); |
| 5265 | } |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 5266 | if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder) |
| 5267 | ctxt->charset=XML_CHAR_ENCODING_UTF8; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5268 | if (sax != NULL) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 5269 | if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5270 | xmlFree(ctxt->sax); |
| 5271 | ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler)); |
| 5272 | if (ctxt->sax == NULL) { |
| 5273 | xmlFree(buf); |
| 5274 | xmlFree(ctxt); |
| 5275 | return(NULL); |
| 5276 | } |
| 5277 | memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler)); |
| 5278 | if (user_data != NULL) |
| 5279 | ctxt->userData = user_data; |
| 5280 | } |
| 5281 | if (filename == NULL) { |
| 5282 | ctxt->directory = NULL; |
| 5283 | } else { |
| 5284 | ctxt->directory = xmlParserGetDirectory(filename); |
| 5285 | } |
| 5286 | |
| 5287 | inputStream = htmlNewInputStream(ctxt); |
| 5288 | if (inputStream == NULL) { |
| 5289 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 5290 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5291 | return(NULL); |
| 5292 | } |
| 5293 | |
| 5294 | if (filename == NULL) |
| 5295 | inputStream->filename = NULL; |
| 5296 | else |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5297 | inputStream->filename = (char *) |
| 5298 | xmlCanonicPath((const xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5299 | inputStream->buf = buf; |
| 5300 | inputStream->base = inputStream->buf->buffer->content; |
| 5301 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5302 | inputStream->end = |
| 5303 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5304 | |
| 5305 | inputPush(ctxt, inputStream); |
| 5306 | |
| 5307 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 5308 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5309 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 5310 | int cur = ctxt->input->cur - ctxt->input->base; |
| 5311 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5312 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5313 | |
| 5314 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 5315 | ctxt->input->cur = ctxt->input->base + cur; |
| 5316 | ctxt->input->end = |
| 5317 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5318 | #ifdef DEBUG_PUSH |
| 5319 | xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); |
| 5320 | #endif |
| 5321 | } |
| 5322 | |
| 5323 | return(ctxt); |
| 5324 | } |
| 5325 | |
| 5326 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5327 | * htmlSAXParseDoc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5328 | * @cur: a pointer to an array of xmlChar |
| 5329 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5330 | * @sax: the SAX handler block |
| 5331 | * @userData: if using SAX, this pointer will be provided on callbacks. |
| 5332 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5333 | * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks |
| 5334 | * to handle parse events. If sax is NULL, fallback to the default DOM |
| 5335 | * behavior and return a tree. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5336 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5337 | * Returns the resulting document tree unless SAX is NULL or the document is |
| 5338 | * not well formed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5339 | */ |
| 5340 | |
| 5341 | htmlDocPtr |
| 5342 | htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) { |
| 5343 | htmlDocPtr ret; |
| 5344 | htmlParserCtxtPtr ctxt; |
| 5345 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5346 | xmlInitParser(); |
| 5347 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5348 | if (cur == NULL) return(NULL); |
| 5349 | |
| 5350 | |
| 5351 | ctxt = htmlCreateDocParserCtxt(cur, encoding); |
| 5352 | if (ctxt == NULL) return(NULL); |
| 5353 | if (sax != NULL) { |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 5354 | if (ctxt->sax != NULL) xmlFree (ctxt->sax); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5355 | ctxt->sax = sax; |
| 5356 | ctxt->userData = userData; |
| 5357 | } |
| 5358 | |
| 5359 | htmlParseDocument(ctxt); |
| 5360 | ret = ctxt->myDoc; |
| 5361 | if (sax != NULL) { |
| 5362 | ctxt->sax = NULL; |
| 5363 | ctxt->userData = NULL; |
| 5364 | } |
| 5365 | htmlFreeParserCtxt(ctxt); |
| 5366 | |
| 5367 | return(ret); |
| 5368 | } |
| 5369 | |
| 5370 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5371 | * htmlParseDoc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5372 | * @cur: a pointer to an array of xmlChar |
| 5373 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5374 | * |
| 5375 | * parse an HTML in-memory document and build a tree. |
| 5376 | * |
| 5377 | * Returns the resulting document tree |
| 5378 | */ |
| 5379 | |
| 5380 | htmlDocPtr |
| 5381 | htmlParseDoc(xmlChar *cur, const char *encoding) { |
| 5382 | return(htmlSAXParseDoc(cur, encoding, NULL, NULL)); |
| 5383 | } |
| 5384 | |
| 5385 | |
| 5386 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5387 | * htmlCreateFileParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5388 | * @filename: the filename |
| 5389 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5390 | * |
| 5391 | * Create a parser context for a file content. |
| 5392 | * Automatic support for ZLIB/Compress compressed document is provided |
| 5393 | * by default if found at compile-time. |
| 5394 | * |
| 5395 | * Returns the new parser context or NULL |
| 5396 | */ |
| 5397 | htmlParserCtxtPtr |
| 5398 | htmlCreateFileParserCtxt(const char *filename, const char *encoding) |
| 5399 | { |
| 5400 | htmlParserCtxtPtr ctxt; |
| 5401 | htmlParserInputPtr inputStream; |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5402 | char *canonicFilename; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5403 | /* htmlCharEncoding enc; */ |
| 5404 | xmlChar *content, *content_line = (xmlChar *) "charset="; |
| 5405 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5406 | if (filename == NULL) |
| 5407 | return(NULL); |
| 5408 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5409 | ctxt = htmlNewParserCtxt(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5410 | if (ctxt == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5411 | return(NULL); |
| 5412 | } |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5413 | canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename); |
| 5414 | if (canonicFilename == NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 5415 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5416 | if (xmlDefaultSAXHandler.error != NULL) { |
| 5417 | xmlDefaultSAXHandler.error(NULL, "out of memory\n"); |
| 5418 | } |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 5419 | #endif |
Daniel Veillard | 104caa3 | 2003-05-13 22:54:05 +0000 | [diff] [blame] | 5420 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5421 | return(NULL); |
| 5422 | } |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5423 | |
| 5424 | inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt); |
| 5425 | xmlFree(canonicFilename); |
| 5426 | if (inputStream == NULL) { |
| 5427 | xmlFreeParserCtxt(ctxt); |
| 5428 | return(NULL); |
| 5429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5430 | |
| 5431 | inputPush(ctxt, inputStream); |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5432 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5433 | /* set encoding */ |
| 5434 | if (encoding) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5435 | content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5436 | if (content) { |
| 5437 | strcpy ((char *)content, (char *)content_line); |
| 5438 | strcat ((char *)content, (char *)encoding); |
| 5439 | htmlCheckEncoding (ctxt, content); |
| 5440 | xmlFree (content); |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | return(ctxt); |
| 5445 | } |
| 5446 | |
| 5447 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5448 | * htmlSAXParseFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5449 | * @filename: the filename |
| 5450 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5451 | * @sax: the SAX handler block |
| 5452 | * @userData: if using SAX, this pointer will be provided on callbacks. |
| 5453 | * |
| 5454 | * parse an HTML file and build a tree. Automatic support for ZLIB/Compress |
| 5455 | * compressed document is provided by default if found at compile-time. |
| 5456 | * It use the given SAX function block to handle the parsing callback. |
| 5457 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 5458 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5459 | * Returns the resulting document tree unless SAX is NULL or the document is |
| 5460 | * not well formed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5461 | */ |
| 5462 | |
| 5463 | htmlDocPtr |
| 5464 | htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, |
| 5465 | void *userData) { |
| 5466 | htmlDocPtr ret; |
| 5467 | htmlParserCtxtPtr ctxt; |
| 5468 | htmlSAXHandlerPtr oldsax = NULL; |
| 5469 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5470 | xmlInitParser(); |
| 5471 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5472 | ctxt = htmlCreateFileParserCtxt(filename, encoding); |
| 5473 | if (ctxt == NULL) return(NULL); |
| 5474 | if (sax != NULL) { |
| 5475 | oldsax = ctxt->sax; |
| 5476 | ctxt->sax = sax; |
| 5477 | ctxt->userData = userData; |
| 5478 | } |
| 5479 | |
| 5480 | htmlParseDocument(ctxt); |
| 5481 | |
| 5482 | ret = ctxt->myDoc; |
| 5483 | if (sax != NULL) { |
| 5484 | ctxt->sax = oldsax; |
| 5485 | ctxt->userData = NULL; |
| 5486 | } |
| 5487 | htmlFreeParserCtxt(ctxt); |
| 5488 | |
| 5489 | return(ret); |
| 5490 | } |
| 5491 | |
| 5492 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5493 | * htmlParseFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5494 | * @filename: the filename |
| 5495 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5496 | * |
| 5497 | * parse an HTML file and build a tree. Automatic support for ZLIB/Compress |
| 5498 | * compressed document is provided by default if found at compile-time. |
| 5499 | * |
| 5500 | * Returns the resulting document tree |
| 5501 | */ |
| 5502 | |
| 5503 | htmlDocPtr |
| 5504 | htmlParseFile(const char *filename, const char *encoding) { |
| 5505 | return(htmlSAXParseFile(filename, encoding, NULL, NULL)); |
| 5506 | } |
| 5507 | |
| 5508 | /** |
| 5509 | * htmlHandleOmittedElem: |
| 5510 | * @val: int 0 or 1 |
| 5511 | * |
| 5512 | * Set and return the previous value for handling HTML omitted tags. |
| 5513 | * |
| 5514 | * Returns the last value for 0 for no handling, 1 for auto insertion. |
| 5515 | */ |
| 5516 | |
| 5517 | int |
| 5518 | htmlHandleOmittedElem(int val) { |
| 5519 | int old = htmlOmittedDefaultValue; |
| 5520 | |
| 5521 | htmlOmittedDefaultValue = val; |
| 5522 | return(old); |
| 5523 | } |
| 5524 | |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 5525 | /** |
| 5526 | * htmlElementAllowedHere: |
| 5527 | * @parent: HTML parent element |
| 5528 | * @elt: HTML element |
| 5529 | * |
| 5530 | * Checks whether an HTML element may be a direct child of a parent element. |
| 5531 | * Note - doesn't check for deprecated elements |
| 5532 | * |
| 5533 | * Returns 1 if allowed; 0 otherwise. |
| 5534 | */ |
| 5535 | int |
| 5536 | htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) { |
| 5537 | const char** p ; |
| 5538 | |
| 5539 | if ( ! elt || ! parent || ! parent->subelts ) |
| 5540 | return 0 ; |
| 5541 | |
| 5542 | for ( p = parent->subelts; *p; ++p ) |
| 5543 | if ( !xmlStrcmp((const xmlChar *)*p, elt) ) |
| 5544 | return 1 ; |
| 5545 | |
| 5546 | return 0 ; |
| 5547 | } |
| 5548 | /** |
| 5549 | * htmlElementStatusHere: |
| 5550 | * @parent: HTML parent element |
| 5551 | * @elt: HTML element |
| 5552 | * |
| 5553 | * Checks whether an HTML element may be a direct child of a parent element. |
| 5554 | * and if so whether it is valid or deprecated. |
| 5555 | * |
| 5556 | * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
| 5557 | */ |
| 5558 | htmlStatus |
| 5559 | htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) { |
| 5560 | if ( ! parent || ! elt ) |
| 5561 | return HTML_INVALID ; |
| 5562 | if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) ) |
| 5563 | return HTML_INVALID ; |
| 5564 | |
| 5565 | return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ; |
| 5566 | } |
| 5567 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5568 | * htmlAttrAllowed: |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 5569 | * @elt: HTML element |
| 5570 | * @attr: HTML attribute |
| 5571 | * @legacy: whether to allow deprecated attributes |
| 5572 | * |
| 5573 | * Checks whether an attribute is valid for an element |
| 5574 | * Has full knowledge of Required and Deprecated attributes |
| 5575 | * |
| 5576 | * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
| 5577 | */ |
| 5578 | htmlStatus |
| 5579 | htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) { |
| 5580 | const char** p ; |
| 5581 | |
| 5582 | if ( !elt || ! attr ) |
| 5583 | return HTML_INVALID ; |
| 5584 | |
| 5585 | if ( elt->attrs_req ) |
| 5586 | for ( p = elt->attrs_req; *p; ++p) |
| 5587 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5588 | return HTML_REQUIRED ; |
| 5589 | |
| 5590 | if ( elt->attrs_opt ) |
| 5591 | for ( p = elt->attrs_opt; *p; ++p) |
| 5592 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5593 | return HTML_VALID ; |
| 5594 | |
| 5595 | if ( legacy && elt->attrs_depr ) |
| 5596 | for ( p = elt->attrs_depr; *p; ++p) |
| 5597 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5598 | return HTML_DEPRECATED ; |
| 5599 | |
| 5600 | return HTML_INVALID ; |
| 5601 | } |
| 5602 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5603 | * htmlNodeStatus: |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5604 | * @node: an htmlNodePtr in a tree |
| 5605 | * @legacy: whether to allow deprecated elements (YES is faster here |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 5606 | * for Element nodes) |
| 5607 | * |
| 5608 | * Checks whether the tree node is valid. Experimental (the author |
| 5609 | * only uses the HTML enhancements in a SAX parser) |
| 5610 | * |
| 5611 | * Return: for Element nodes, a return from htmlElementAllowedHere (if |
| 5612 | * legacy allowed) or htmlElementStatusHere (otherwise). |
| 5613 | * for Attribute nodes, a return from htmlAttrAllowed |
| 5614 | * for other nodes, HTML_NA (no checks performed) |
| 5615 | */ |
| 5616 | htmlStatus |
| 5617 | htmlNodeStatus(const htmlNodePtr node, int legacy) { |
| 5618 | if ( ! node ) |
| 5619 | return HTML_INVALID ; |
| 5620 | |
| 5621 | switch ( node->type ) { |
| 5622 | case XML_ELEMENT_NODE: |
| 5623 | return legacy |
| 5624 | ? ( htmlElementAllowedHere ( |
| 5625 | htmlTagLookup(node->parent->name) , node->name |
| 5626 | ) ? HTML_VALID : HTML_INVALID ) |
| 5627 | : htmlElementStatusHere( |
| 5628 | htmlTagLookup(node->parent->name) , |
| 5629 | htmlTagLookup(node->name) ) |
| 5630 | ; |
| 5631 | case XML_ATTRIBUTE_NODE: |
| 5632 | return htmlAttrAllowed( |
| 5633 | htmlTagLookup(node->parent->name) , node->name, legacy) ; |
| 5634 | default: return HTML_NA ; |
| 5635 | } |
| 5636 | } |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5637 | /************************************************************************ |
| 5638 | * * |
| 5639 | * New set (2.6.0) of simpler and more flexible APIs * |
| 5640 | * * |
| 5641 | ************************************************************************/ |
| 5642 | /** |
| 5643 | * DICT_FREE: |
| 5644 | * @str: a string |
| 5645 | * |
| 5646 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 5647 | * current scope |
| 5648 | */ |
| 5649 | #define DICT_FREE(str) \ |
| 5650 | if ((str) && ((!dict) || \ |
| 5651 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 5652 | xmlFree((char *)(str)); |
| 5653 | |
| 5654 | /** |
| 5655 | * htmlCtxtReset: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5656 | * @ctxt: an HTML parser context |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5657 | * |
| 5658 | * Reset a parser context |
| 5659 | */ |
| 5660 | void |
| 5661 | htmlCtxtReset(htmlParserCtxtPtr ctxt) |
| 5662 | { |
| 5663 | xmlParserInputPtr input; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5664 | xmlDictPtr dict; |
| 5665 | |
| 5666 | if (ctxt == NULL) |
| 5667 | return; |
| 5668 | |
| 5669 | dict = ctxt->dict; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5670 | |
| 5671 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 5672 | xmlFreeInputStream(input); |
| 5673 | } |
| 5674 | ctxt->inputNr = 0; |
| 5675 | ctxt->input = NULL; |
| 5676 | |
| 5677 | ctxt->spaceNr = 0; |
| 5678 | ctxt->spaceTab[0] = -1; |
| 5679 | ctxt->space = &ctxt->spaceTab[0]; |
| 5680 | |
| 5681 | |
| 5682 | ctxt->nodeNr = 0; |
| 5683 | ctxt->node = NULL; |
| 5684 | |
| 5685 | ctxt->nameNr = 0; |
| 5686 | ctxt->name = NULL; |
| 5687 | |
| 5688 | DICT_FREE(ctxt->version); |
| 5689 | ctxt->version = NULL; |
| 5690 | DICT_FREE(ctxt->encoding); |
| 5691 | ctxt->encoding = NULL; |
| 5692 | DICT_FREE(ctxt->directory); |
| 5693 | ctxt->directory = NULL; |
| 5694 | DICT_FREE(ctxt->extSubURI); |
| 5695 | ctxt->extSubURI = NULL; |
| 5696 | DICT_FREE(ctxt->extSubSystem); |
| 5697 | ctxt->extSubSystem = NULL; |
| 5698 | if (ctxt->myDoc != NULL) |
| 5699 | xmlFreeDoc(ctxt->myDoc); |
| 5700 | ctxt->myDoc = NULL; |
| 5701 | |
| 5702 | ctxt->standalone = -1; |
| 5703 | ctxt->hasExternalSubset = 0; |
| 5704 | ctxt->hasPErefs = 0; |
| 5705 | ctxt->html = 1; |
| 5706 | ctxt->external = 0; |
| 5707 | ctxt->instate = XML_PARSER_START; |
| 5708 | ctxt->token = 0; |
| 5709 | |
| 5710 | ctxt->wellFormed = 1; |
| 5711 | ctxt->nsWellFormed = 1; |
| 5712 | ctxt->valid = 1; |
| 5713 | ctxt->vctxt.userData = ctxt; |
| 5714 | ctxt->vctxt.error = xmlParserValidityError; |
| 5715 | ctxt->vctxt.warning = xmlParserValidityWarning; |
| 5716 | ctxt->record_info = 0; |
| 5717 | ctxt->nbChars = 0; |
| 5718 | ctxt->checkIndex = 0; |
| 5719 | ctxt->inSubset = 0; |
| 5720 | ctxt->errNo = XML_ERR_OK; |
| 5721 | ctxt->depth = 0; |
| 5722 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 5723 | ctxt->catalogs = NULL; |
| 5724 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 5725 | |
| 5726 | if (ctxt->attsDefault != NULL) { |
| 5727 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 5728 | ctxt->attsDefault = NULL; |
| 5729 | } |
| 5730 | if (ctxt->attsSpecial != NULL) { |
| 5731 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 5732 | ctxt->attsSpecial = NULL; |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | /** |
| 5737 | * htmlCtxtUseOptions: |
| 5738 | * @ctxt: an HTML parser context |
| 5739 | * @options: a combination of htmlParserOption(s) |
| 5740 | * |
| 5741 | * Applies the options to the parser context |
| 5742 | * |
| 5743 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 5744 | * in case of error. |
| 5745 | */ |
| 5746 | int |
| 5747 | htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options) |
| 5748 | { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5749 | if (ctxt == NULL) |
| 5750 | return(-1); |
| 5751 | |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5752 | if (options & HTML_PARSE_NOWARNING) { |
| 5753 | ctxt->sax->warning = NULL; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5754 | ctxt->vctxt.warning = NULL; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5755 | options -= XML_PARSE_NOWARNING; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5756 | ctxt->options |= XML_PARSE_NOWARNING; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5757 | } |
| 5758 | if (options & HTML_PARSE_NOERROR) { |
| 5759 | ctxt->sax->error = NULL; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5760 | ctxt->vctxt.error = NULL; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5761 | ctxt->sax->fatalError = NULL; |
| 5762 | options -= XML_PARSE_NOERROR; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5763 | ctxt->options |= XML_PARSE_NOERROR; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5764 | } |
| 5765 | if (options & HTML_PARSE_PEDANTIC) { |
| 5766 | ctxt->pedantic = 1; |
| 5767 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5768 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5769 | } else |
| 5770 | ctxt->pedantic = 0; |
| 5771 | if (options & XML_PARSE_NOBLANKS) { |
| 5772 | ctxt->keepBlanks = 0; |
| 5773 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 5774 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 5775 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5776 | } else |
| 5777 | ctxt->keepBlanks = 1; |
| 5778 | ctxt->dictNames = 0; |
| 5779 | return (options); |
| 5780 | } |
| 5781 | |
| 5782 | /** |
| 5783 | * htmlDoRead: |
| 5784 | * @ctxt: an HTML parser context |
| 5785 | * @URL: the base URL to use for the document |
| 5786 | * @encoding: the document encoding, or NULL |
| 5787 | * @options: a combination of htmlParserOption(s) |
| 5788 | * @reuse: keep the context for reuse |
| 5789 | * |
| 5790 | * Common front-end for the htmlRead functions |
| 5791 | * |
| 5792 | * Returns the resulting document tree or NULL |
| 5793 | */ |
| 5794 | static htmlDocPtr |
| 5795 | htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 5796 | int options, int reuse) |
| 5797 | { |
| 5798 | htmlDocPtr ret; |
| 5799 | |
| 5800 | htmlCtxtUseOptions(ctxt, options); |
| 5801 | ctxt->html = 1; |
| 5802 | if (encoding != NULL) { |
| 5803 | xmlCharEncodingHandlerPtr hdlr; |
| 5804 | |
| 5805 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 5806 | if (hdlr != NULL) |
| 5807 | xmlSwitchToEncoding(ctxt, hdlr); |
| 5808 | } |
| 5809 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 5810 | (ctxt->input->filename == NULL)) |
| 5811 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
| 5812 | htmlParseDocument(ctxt); |
| 5813 | ret = ctxt->myDoc; |
| 5814 | ctxt->myDoc = NULL; |
| 5815 | if (!reuse) { |
| 5816 | if ((ctxt->dictNames) && |
| 5817 | (ret != NULL) && |
| 5818 | (ret->dict == ctxt->dict)) |
| 5819 | ctxt->dict = NULL; |
| 5820 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5821 | } |
| 5822 | return (ret); |
| 5823 | } |
| 5824 | |
| 5825 | /** |
| 5826 | * htmlReadDoc: |
| 5827 | * @cur: a pointer to a zero terminated string |
| 5828 | * @URL: the base URL to use for the document |
| 5829 | * @encoding: the document encoding, or NULL |
| 5830 | * @options: a combination of htmlParserOption(s) |
| 5831 | * |
| 5832 | * parse an XML in-memory document and build a tree. |
| 5833 | * |
| 5834 | * Returns the resulting document tree |
| 5835 | */ |
| 5836 | htmlDocPtr |
| 5837 | htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
| 5838 | { |
| 5839 | htmlParserCtxtPtr ctxt; |
| 5840 | |
| 5841 | if (cur == NULL) |
| 5842 | return (NULL); |
| 5843 | |
| 5844 | ctxt = xmlCreateDocParserCtxt(cur); |
| 5845 | if (ctxt == NULL) |
| 5846 | return (NULL); |
| 5847 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 5848 | } |
| 5849 | |
| 5850 | /** |
| 5851 | * htmlReadFile: |
| 5852 | * @filename: a file or URL |
| 5853 | * @encoding: the document encoding, or NULL |
| 5854 | * @options: a combination of htmlParserOption(s) |
| 5855 | * |
| 5856 | * parse an XML file from the filesystem or the network. |
| 5857 | * |
| 5858 | * Returns the resulting document tree |
| 5859 | */ |
| 5860 | htmlDocPtr |
| 5861 | htmlReadFile(const char *filename, const char *encoding, int options) |
| 5862 | { |
| 5863 | htmlParserCtxtPtr ctxt; |
| 5864 | |
| 5865 | ctxt = htmlCreateFileParserCtxt(filename, encoding); |
| 5866 | if (ctxt == NULL) |
| 5867 | return (NULL); |
| 5868 | return (htmlDoRead(ctxt, NULL, NULL, options, 0)); |
| 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * htmlReadMemory: |
| 5873 | * @buffer: a pointer to a char array |
| 5874 | * @size: the size of the array |
| 5875 | * @URL: the base URL to use for the document |
| 5876 | * @encoding: the document encoding, or NULL |
| 5877 | * @options: a combination of htmlParserOption(s) |
| 5878 | * |
| 5879 | * parse an XML in-memory document and build a tree. |
| 5880 | * |
| 5881 | * Returns the resulting document tree |
| 5882 | */ |
| 5883 | htmlDocPtr |
| 5884 | htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options) |
| 5885 | { |
| 5886 | htmlParserCtxtPtr ctxt; |
| 5887 | |
| 5888 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 5889 | if (ctxt == NULL) |
| 5890 | return (NULL); |
William M. Brack | d43cdcd | 2004-08-03 15:13:29 +0000 | [diff] [blame] | 5891 | if (ctxt->sax != NULL) |
| 5892 | memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1)); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 5893 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 5894 | } |
| 5895 | |
| 5896 | /** |
| 5897 | * htmlReadFd: |
| 5898 | * @fd: an open file descriptor |
| 5899 | * @URL: the base URL to use for the document |
| 5900 | * @encoding: the document encoding, or NULL |
| 5901 | * @options: a combination of htmlParserOption(s) |
| 5902 | * |
| 5903 | * parse an XML from a file descriptor and build a tree. |
| 5904 | * |
| 5905 | * Returns the resulting document tree |
| 5906 | */ |
| 5907 | htmlDocPtr |
| 5908 | htmlReadFd(int fd, const char *URL, const char *encoding, int options) |
| 5909 | { |
| 5910 | htmlParserCtxtPtr ctxt; |
| 5911 | xmlParserInputBufferPtr input; |
| 5912 | xmlParserInputPtr stream; |
| 5913 | |
| 5914 | if (fd < 0) |
| 5915 | return (NULL); |
| 5916 | |
| 5917 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 5918 | if (input == NULL) |
| 5919 | return (NULL); |
| 5920 | ctxt = xmlNewParserCtxt(); |
| 5921 | if (ctxt == NULL) { |
| 5922 | xmlFreeParserInputBuffer(input); |
| 5923 | return (NULL); |
| 5924 | } |
| 5925 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 5926 | if (stream == NULL) { |
| 5927 | xmlFreeParserInputBuffer(input); |
| 5928 | xmlFreeParserCtxt(ctxt); |
| 5929 | return (NULL); |
| 5930 | } |
| 5931 | inputPush(ctxt, stream); |
| 5932 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 5933 | } |
| 5934 | |
| 5935 | /** |
| 5936 | * htmlReadIO: |
| 5937 | * @ioread: an I/O read function |
| 5938 | * @ioclose: an I/O close function |
| 5939 | * @ioctx: an I/O handler |
| 5940 | * @URL: the base URL to use for the document |
| 5941 | * @encoding: the document encoding, or NULL |
| 5942 | * @options: a combination of htmlParserOption(s) |
| 5943 | * |
| 5944 | * parse an HTML document from I/O functions and source and build a tree. |
| 5945 | * |
| 5946 | * Returns the resulting document tree |
| 5947 | */ |
| 5948 | htmlDocPtr |
| 5949 | htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 5950 | void *ioctx, const char *URL, const char *encoding, int options) |
| 5951 | { |
| 5952 | htmlParserCtxtPtr ctxt; |
| 5953 | xmlParserInputBufferPtr input; |
| 5954 | xmlParserInputPtr stream; |
| 5955 | |
| 5956 | if (ioread == NULL) |
| 5957 | return (NULL); |
| 5958 | |
| 5959 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 5960 | XML_CHAR_ENCODING_NONE); |
| 5961 | if (input == NULL) |
| 5962 | return (NULL); |
| 5963 | ctxt = xmlNewParserCtxt(); |
| 5964 | if (ctxt == NULL) { |
| 5965 | xmlFreeParserInputBuffer(input); |
| 5966 | return (NULL); |
| 5967 | } |
| 5968 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 5969 | if (stream == NULL) { |
| 5970 | xmlFreeParserInputBuffer(input); |
| 5971 | xmlFreeParserCtxt(ctxt); |
| 5972 | return (NULL); |
| 5973 | } |
| 5974 | inputPush(ctxt, stream); |
| 5975 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 5976 | } |
| 5977 | |
| 5978 | /** |
| 5979 | * htmlCtxtReadDoc: |
| 5980 | * @ctxt: an HTML parser context |
| 5981 | * @cur: a pointer to a zero terminated string |
| 5982 | * @URL: the base URL to use for the document |
| 5983 | * @encoding: the document encoding, or NULL |
| 5984 | * @options: a combination of htmlParserOption(s) |
| 5985 | * |
| 5986 | * parse an XML in-memory document and build a tree. |
| 5987 | * This reuses the existing @ctxt parser context |
| 5988 | * |
| 5989 | * Returns the resulting document tree |
| 5990 | */ |
| 5991 | htmlDocPtr |
| 5992 | htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur, |
| 5993 | const char *URL, const char *encoding, int options) |
| 5994 | { |
| 5995 | xmlParserInputPtr stream; |
| 5996 | |
| 5997 | if (cur == NULL) |
| 5998 | return (NULL); |
| 5999 | if (ctxt == NULL) |
| 6000 | return (NULL); |
| 6001 | |
| 6002 | htmlCtxtReset(ctxt); |
| 6003 | |
| 6004 | stream = xmlNewStringInputStream(ctxt, cur); |
| 6005 | if (stream == NULL) { |
| 6006 | return (NULL); |
| 6007 | } |
| 6008 | inputPush(ctxt, stream); |
| 6009 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6010 | } |
| 6011 | |
| 6012 | /** |
| 6013 | * htmlCtxtReadFile: |
| 6014 | * @ctxt: an HTML parser context |
| 6015 | * @filename: a file or URL |
| 6016 | * @encoding: the document encoding, or NULL |
| 6017 | * @options: a combination of htmlParserOption(s) |
| 6018 | * |
| 6019 | * parse an XML file from the filesystem or the network. |
| 6020 | * This reuses the existing @ctxt parser context |
| 6021 | * |
| 6022 | * Returns the resulting document tree |
| 6023 | */ |
| 6024 | htmlDocPtr |
| 6025 | htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename, |
| 6026 | const char *encoding, int options) |
| 6027 | { |
| 6028 | xmlParserInputPtr stream; |
| 6029 | |
| 6030 | if (filename == NULL) |
| 6031 | return (NULL); |
| 6032 | if (ctxt == NULL) |
| 6033 | return (NULL); |
| 6034 | |
| 6035 | htmlCtxtReset(ctxt); |
| 6036 | |
| 6037 | stream = xmlNewInputFromFile(ctxt, filename); |
| 6038 | if (stream == NULL) { |
| 6039 | return (NULL); |
| 6040 | } |
| 6041 | inputPush(ctxt, stream); |
| 6042 | return (htmlDoRead(ctxt, NULL, encoding, options, 1)); |
| 6043 | } |
| 6044 | |
| 6045 | /** |
| 6046 | * htmlCtxtReadMemory: |
| 6047 | * @ctxt: an HTML parser context |
| 6048 | * @buffer: a pointer to a char array |
| 6049 | * @size: the size of the array |
| 6050 | * @URL: the base URL to use for the document |
| 6051 | * @encoding: the document encoding, or NULL |
| 6052 | * @options: a combination of htmlParserOption(s) |
| 6053 | * |
| 6054 | * parse an XML in-memory document and build a tree. |
| 6055 | * This reuses the existing @ctxt parser context |
| 6056 | * |
| 6057 | * Returns the resulting document tree |
| 6058 | */ |
| 6059 | htmlDocPtr |
| 6060 | htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size, |
| 6061 | const char *URL, const char *encoding, int options) |
| 6062 | { |
| 6063 | xmlParserInputBufferPtr input; |
| 6064 | xmlParserInputPtr stream; |
| 6065 | |
| 6066 | if (ctxt == NULL) |
| 6067 | return (NULL); |
| 6068 | if (buffer == NULL) |
| 6069 | return (NULL); |
| 6070 | |
| 6071 | htmlCtxtReset(ctxt); |
| 6072 | |
| 6073 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 6074 | if (input == NULL) { |
| 6075 | return(NULL); |
| 6076 | } |
| 6077 | |
| 6078 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6079 | if (stream == NULL) { |
| 6080 | xmlFreeParserInputBuffer(input); |
| 6081 | return(NULL); |
| 6082 | } |
| 6083 | |
| 6084 | inputPush(ctxt, stream); |
| 6085 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6086 | } |
| 6087 | |
| 6088 | /** |
| 6089 | * htmlCtxtReadFd: |
| 6090 | * @ctxt: an HTML parser context |
| 6091 | * @fd: an open file descriptor |
| 6092 | * @URL: the base URL to use for the document |
| 6093 | * @encoding: the document encoding, or NULL |
| 6094 | * @options: a combination of htmlParserOption(s) |
| 6095 | * |
| 6096 | * parse an XML from a file descriptor and build a tree. |
| 6097 | * This reuses the existing @ctxt parser context |
| 6098 | * |
| 6099 | * Returns the resulting document tree |
| 6100 | */ |
| 6101 | htmlDocPtr |
| 6102 | htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd, |
| 6103 | const char *URL, const char *encoding, int options) |
| 6104 | { |
| 6105 | xmlParserInputBufferPtr input; |
| 6106 | xmlParserInputPtr stream; |
| 6107 | |
| 6108 | if (fd < 0) |
| 6109 | return (NULL); |
| 6110 | if (ctxt == NULL) |
| 6111 | return (NULL); |
| 6112 | |
| 6113 | htmlCtxtReset(ctxt); |
| 6114 | |
| 6115 | |
| 6116 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 6117 | if (input == NULL) |
| 6118 | return (NULL); |
| 6119 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6120 | if (stream == NULL) { |
| 6121 | xmlFreeParserInputBuffer(input); |
| 6122 | return (NULL); |
| 6123 | } |
| 6124 | inputPush(ctxt, stream); |
| 6125 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6126 | } |
| 6127 | |
| 6128 | /** |
| 6129 | * htmlCtxtReadIO: |
| 6130 | * @ctxt: an HTML parser context |
| 6131 | * @ioread: an I/O read function |
| 6132 | * @ioclose: an I/O close function |
| 6133 | * @ioctx: an I/O handler |
| 6134 | * @URL: the base URL to use for the document |
| 6135 | * @encoding: the document encoding, or NULL |
| 6136 | * @options: a combination of htmlParserOption(s) |
| 6137 | * |
| 6138 | * parse an HTML document from I/O functions and source and build a tree. |
| 6139 | * This reuses the existing @ctxt parser context |
| 6140 | * |
| 6141 | * Returns the resulting document tree |
| 6142 | */ |
| 6143 | htmlDocPtr |
| 6144 | htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 6145 | xmlInputCloseCallback ioclose, void *ioctx, |
| 6146 | const char *URL, |
| 6147 | const char *encoding, int options) |
| 6148 | { |
| 6149 | xmlParserInputBufferPtr input; |
| 6150 | xmlParserInputPtr stream; |
| 6151 | |
| 6152 | if (ioread == NULL) |
| 6153 | return (NULL); |
| 6154 | if (ctxt == NULL) |
| 6155 | return (NULL); |
| 6156 | |
| 6157 | htmlCtxtReset(ctxt); |
| 6158 | |
| 6159 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 6160 | XML_CHAR_ENCODING_NONE); |
| 6161 | if (input == NULL) |
| 6162 | return (NULL); |
| 6163 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6164 | if (stream == NULL) { |
| 6165 | xmlFreeParserInputBuffer(input); |
| 6166 | return (NULL); |
| 6167 | } |
| 6168 | inputPush(ctxt, stream); |
| 6169 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6170 | } |
| 6171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | #endif /* LIBXML_HTML_ENABLED */ |