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 | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 62 | * Some factorized error routines * |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 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 | * * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 150 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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 | { |
Daniel Veillard | 029a04d | 2009-08-24 12:50:23 +0200 | [diff] [blame] | 166 | if ((ctxt->html < 3) && (xmlStrEqual(value, BAD_CAST "head"))) |
| 167 | ctxt->html = 3; |
| 168 | if ((ctxt->html < 10) && (xmlStrEqual(value, BAD_CAST "body"))) |
| 169 | ctxt->html = 10; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 170 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 171 | ctxt->nameMax *= 2; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 172 | ctxt->nameTab = (const xmlChar * *) |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 173 | xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 174 | ctxt->nameMax * |
| 175 | sizeof(ctxt->nameTab[0])); |
| 176 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 177 | htmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 178 | return (0); |
| 179 | } |
| 180 | } |
| 181 | ctxt->nameTab[ctxt->nameNr] = value; |
| 182 | ctxt->name = value; |
| 183 | return (ctxt->nameNr++); |
| 184 | } |
| 185 | /** |
| 186 | * htmlnamePop: |
| 187 | * @ctxt: an HTML parser context |
| 188 | * |
| 189 | * Pops the top element name from the name stack |
| 190 | * |
| 191 | * Returns the name just removed |
| 192 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 193 | static const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 194 | htmlnamePop(htmlParserCtxtPtr ctxt) |
| 195 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 196 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 197 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 198 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 199 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 200 | ctxt->nameNr--; |
| 201 | if (ctxt->nameNr < 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 202 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 203 | if (ctxt->nameNr > 0) |
| 204 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 205 | else |
| 206 | ctxt->name = NULL; |
| 207 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 208 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 209 | return (ret); |
| 210 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * Macros for accessing the content. Those should be used only by the parser, |
| 214 | * and not exported. |
| 215 | * |
| 216 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 217 | * |
| 218 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 219 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 220 | * in ISO-Latin or UTF-8, and the current 16 bit value if compiled |
| 221 | * in UNICODE mode. This should be used internally by the parser |
| 222 | * only to compare to ASCII values otherwise it would break when |
| 223 | * running with UTF-8 encoding. |
| 224 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 225 | * to compare on ASCII based substring. |
| 226 | * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR |
| 227 | * it should be used only to compare on ASCII based substring. |
| 228 | * 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] | 229 | * strings without newlines within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 230 | * |
| 231 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 232 | * |
| 233 | * CURRENT Returns the current char value, with the full decoding of |
| 234 | * UTF-8 if we are using this mode. It returns an int. |
| 235 | * NEXT Skip to the next character, this does the proper decoding |
| 236 | * 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] | 237 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 238 | * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly |
| 239 | */ |
| 240 | |
| 241 | #define UPPER (toupper(*ctxt->input->cur)) |
| 242 | |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 243 | #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] | 244 | |
| 245 | #define NXT(val) ctxt->input->cur[(val)] |
| 246 | |
| 247 | #define UPP(val) (toupper(ctxt->input->cur[(val)])) |
| 248 | |
| 249 | #define CUR_PTR ctxt->input->cur |
| 250 | |
Daniel Veillard | 652f9aa | 2003-10-28 22:04:45 +0000 | [diff] [blame] | 251 | #define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 252 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
| 253 | xmlParserInputShrink(ctxt->input) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 254 | |
Daniel Veillard | 652f9aa | 2003-10-28 22:04:45 +0000 | [diff] [blame] | 255 | #define GROW if ((ctxt->progressive == 0) && \ |
| 256 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
| 257 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 258 | |
| 259 | #define CURRENT ((int) (*ctxt->input->cur)) |
| 260 | |
| 261 | #define SKIP_BLANKS htmlSkipBlankChars(ctxt) |
| 262 | |
| 263 | /* Inported from XML */ |
| 264 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 265 | /* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */ |
| 266 | #define CUR ((int) (*ctxt->input->cur)) |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 267 | #define NEXT xmlNextChar(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 268 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 269 | #define RAW (ctxt->token ? -1 : (*ctxt->input->cur)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | #define NEXTL(l) do { \ |
| 273 | if (*(ctxt->input->cur) == '\n') { \ |
| 274 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 275 | } else ctxt->input->col++; \ |
| 276 | ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++; \ |
| 277 | } while (0) |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 278 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 279 | /************ |
| 280 | \ |
| 281 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 282 | if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt); |
| 283 | ************/ |
| 284 | |
| 285 | #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l) |
| 286 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 287 | |
| 288 | #define COPY_BUF(l,b,i,v) \ |
| 289 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 290 | else i += xmlCopyChar(l,&b[i],v) |
| 291 | |
| 292 | /** |
Daniel Veillard | 533ec0e | 2009-08-12 20:13:38 +0200 | [diff] [blame] | 293 | * htmlFindEncoding: |
| 294 | * @the HTML parser context |
| 295 | * |
| 296 | * Ty to find and encoding in the current data available in the input |
| 297 | * buffer this is needed to try to switch to the proper encoding when |
| 298 | * one face a character error. |
| 299 | * That's an heuristic, since it's operating outside of parsing it could |
| 300 | * try to use a meta which had been commented out, that's the reason it |
| 301 | * should only be used in case of error, not as a default. |
| 302 | * |
| 303 | * Returns an encoding string or NULL if not found, the string need to |
| 304 | * be freed |
| 305 | */ |
| 306 | static xmlChar * |
| 307 | htmlFindEncoding(xmlParserCtxtPtr ctxt) { |
| 308 | const xmlChar *start, *cur, *end; |
| 309 | |
| 310 | if ((ctxt == NULL) || (ctxt->input == NULL) || |
| 311 | (ctxt->input->encoding != NULL) || (ctxt->input->buf == NULL) || |
| 312 | (ctxt->input->buf->encoder != NULL)) |
| 313 | return(NULL); |
| 314 | if ((ctxt->input->cur == NULL) || (ctxt->input->end == NULL)) |
| 315 | return(NULL); |
| 316 | |
| 317 | start = ctxt->input->cur; |
| 318 | end = ctxt->input->end; |
| 319 | /* we also expect the input buffer to be zero terminated */ |
| 320 | if (*end != 0) |
| 321 | return(NULL); |
| 322 | |
| 323 | cur = xmlStrcasestr(start, BAD_CAST "HTTP-EQUIV"); |
| 324 | if (cur == NULL) |
| 325 | return(NULL); |
| 326 | cur = xmlStrcasestr(cur, BAD_CAST "CONTENT"); |
| 327 | if (cur == NULL) |
| 328 | return(NULL); |
| 329 | cur = xmlStrcasestr(cur, BAD_CAST "CHARSET="); |
| 330 | if (cur == NULL) |
| 331 | return(NULL); |
| 332 | cur += 8; |
| 333 | start = cur; |
| 334 | while (((*cur >= 'A') && (*cur <= 'Z')) || |
| 335 | ((*cur >= 'a') && (*cur <= 'z')) || |
| 336 | ((*cur >= '0') && (*cur <= '9')) || |
| 337 | (*cur == '-') || (*cur == '_') || (*cur == ':') || (*cur == '/')) |
| 338 | cur++; |
| 339 | if (cur == start) |
| 340 | return(NULL); |
| 341 | return(xmlStrndup(start, cur - start)); |
| 342 | } |
| 343 | |
| 344 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 345 | * htmlCurrentChar: |
| 346 | * @ctxt: the HTML parser context |
| 347 | * @len: pointer to the length of the char read |
| 348 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 349 | * 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] | 350 | * bytes in the input buffer. Implement the end of line normalization: |
| 351 | * 2.11 End-of-Line Handling |
| 352 | * If the encoding is unspecified, in the case we find an ISO-Latin-1 |
| 353 | * char, then the encoding converter is plugged in automatically. |
| 354 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 355 | * Returns the current char value and its length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 356 | */ |
| 357 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 358 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 359 | htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) { |
| 360 | if (ctxt->instate == XML_PARSER_EOF) |
| 361 | return(0); |
| 362 | |
| 363 | if (ctxt->token != 0) { |
| 364 | *len = 0; |
| 365 | return(ctxt->token); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 366 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 367 | if (ctxt->charset == XML_CHAR_ENCODING_UTF8) { |
| 368 | /* |
| 369 | * We are supposed to handle UTF8, check it's valid |
| 370 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 371 | * |
| 372 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 373 | * 0000 0000-0000 007F 0xxxxxxx |
| 374 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 375 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 376 | * |
| 377 | * Check for the 0x110000 limit too |
| 378 | */ |
| 379 | const unsigned char *cur = ctxt->input->cur; |
| 380 | unsigned char c; |
| 381 | unsigned int val; |
| 382 | |
| 383 | c = *cur; |
| 384 | if (c & 0x80) { |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 385 | if (cur[1] == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 386 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 387 | cur = ctxt->input->cur; |
| 388 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 389 | if ((cur[1] & 0xc0) != 0x80) |
| 390 | goto encoding_error; |
| 391 | if ((c & 0xe0) == 0xe0) { |
| 392 | |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 393 | if (cur[2] == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 394 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 395 | cur = ctxt->input->cur; |
| 396 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 397 | if ((cur[2] & 0xc0) != 0x80) |
| 398 | goto encoding_error; |
| 399 | if ((c & 0xf0) == 0xf0) { |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 400 | if (cur[3] == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 401 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Adiel Mittmann | 8a10379 | 2009-08-25 11:27:13 +0200 | [diff] [blame] | 402 | cur = ctxt->input->cur; |
| 403 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 404 | if (((c & 0xf8) != 0xf0) || |
| 405 | ((cur[3] & 0xc0) != 0x80)) |
| 406 | goto encoding_error; |
| 407 | /* 4-byte code */ |
| 408 | *len = 4; |
| 409 | val = (cur[0] & 0x7) << 18; |
| 410 | val |= (cur[1] & 0x3f) << 12; |
| 411 | val |= (cur[2] & 0x3f) << 6; |
| 412 | val |= cur[3] & 0x3f; |
| 413 | } else { |
| 414 | /* 3-byte code */ |
| 415 | *len = 3; |
| 416 | val = (cur[0] & 0xf) << 12; |
| 417 | val |= (cur[1] & 0x3f) << 6; |
| 418 | val |= cur[2] & 0x3f; |
| 419 | } |
| 420 | } else { |
| 421 | /* 2-byte code */ |
| 422 | *len = 2; |
| 423 | val = (cur[0] & 0x1f) << 6; |
| 424 | val |= cur[1] & 0x3f; |
| 425 | } |
| 426 | if (!IS_CHAR(val)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 427 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 428 | "Char 0x%X out of allowed range\n", val); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 430 | return(val); |
| 431 | } else { |
Daniel Veillard | 856c668 | 2009-08-24 18:16:56 +0200 | [diff] [blame] | 432 | if ((*ctxt->input->cur == 0) && |
| 433 | (ctxt->input->cur < ctxt->input->end)) { |
| 434 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 435 | "Char 0x%X out of allowed range\n", 0); |
| 436 | *len = 1; |
| 437 | return(' '); |
| 438 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 439 | /* 1-byte code */ |
| 440 | *len = 1; |
| 441 | return((int) *ctxt->input->cur); |
| 442 | } |
| 443 | } |
| 444 | /* |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 445 | * Assume it's a fixed length encoding (1) with |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 446 | * a compatible encoding for the ASCII set, since |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 447 | * XML constructs only use < 128 chars |
| 448 | */ |
| 449 | *len = 1; |
| 450 | if ((int) *ctxt->input->cur < 0x80) |
| 451 | return((int) *ctxt->input->cur); |
| 452 | |
| 453 | /* |
| 454 | * Humm this is bad, do an automatic flow conversion |
| 455 | */ |
Daniel Veillard | 533ec0e | 2009-08-12 20:13:38 +0200 | [diff] [blame] | 456 | { |
| 457 | xmlChar * guess; |
| 458 | xmlCharEncodingHandlerPtr handler; |
| 459 | |
| 460 | guess = htmlFindEncoding(ctxt); |
| 461 | if (guess == NULL) { |
| 462 | xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1); |
| 463 | } else { |
| 464 | if (ctxt->input->encoding != NULL) |
| 465 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 466 | ctxt->input->encoding = guess; |
| 467 | handler = xmlFindCharEncodingHandler((const char *) guess); |
| 468 | if (handler != NULL) { |
| 469 | xmlSwitchToEncoding(ctxt, handler); |
| 470 | } else { |
| 471 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 472 | "Unsupported encoding %s", guess, NULL); |
| 473 | } |
| 474 | } |
| 475 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 476 | } |
| 477 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 478 | return(xmlCurrentChar(ctxt, len)); |
| 479 | |
| 480 | encoding_error: |
| 481 | /* |
| 482 | * If we detect an UTF8 error that probably mean that the |
| 483 | * input encoding didn't get properly advertized in the |
| 484 | * declaration header. Report the error and switch the encoding |
| 485 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 486 | * encoding !) |
| 487 | */ |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 488 | { |
| 489 | char buffer[150]; |
| 490 | |
Daniel Veillard | 861101d | 2007-06-12 08:38:57 +0000 | [diff] [blame] | 491 | if (ctxt->input->end - ctxt->input->cur >= 4) { |
| 492 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 493 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 494 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 495 | } else { |
| 496 | snprintf(buffer, 149, "Bytes: 0x%02X\n", ctxt->input->cur[0]); |
| 497 | } |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 498 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 499 | "Input is not proper UTF-8, indicate encoding !\n", |
| 500 | BAD_CAST buffer, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 503 | ctxt->charset = XML_CHAR_ENCODING_8859_1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 504 | *len = 1; |
| 505 | return((int) *ctxt->input->cur); |
| 506 | } |
| 507 | |
| 508 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 509 | * htmlSkipBlankChars: |
| 510 | * @ctxt: the HTML parser context |
| 511 | * |
| 512 | * skip all blanks character found at that point in the input streams. |
| 513 | * |
| 514 | * Returns the number of space chars skipped |
| 515 | */ |
| 516 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 517 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 518 | htmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
| 519 | int res = 0; |
| 520 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 521 | while (IS_BLANK_CH(*(ctxt->input->cur))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | if ((*ctxt->input->cur == 0) && |
| 523 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) { |
| 524 | xmlPopInput(ctxt); |
| 525 | } else { |
| 526 | if (*(ctxt->input->cur) == '\n') { |
| 527 | ctxt->input->line++; ctxt->input->col = 1; |
| 528 | } else ctxt->input->col++; |
| 529 | ctxt->input->cur++; |
| 530 | ctxt->nbChars++; |
| 531 | if (*ctxt->input->cur == 0) |
| 532 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 533 | } |
| 534 | res++; |
| 535 | } |
| 536 | return(res); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | |
| 541 | /************************************************************************ |
| 542 | * * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 543 | * The list of HTML elements and their properties * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | * * |
| 545 | ************************************************************************/ |
| 546 | |
| 547 | /* |
| 548 | * Start Tag: 1 means the start tag can be ommited |
| 549 | * End Tag: 1 means the end tag can be ommited |
| 550 | * 2 means it's forbidden (empty elements) |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 551 | * 3 means the tag is stylistic and should be closed easily |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 552 | * Depr: this element is deprecated |
| 553 | * DTD: 1 means that this element is valid only in the Loose DTD |
| 554 | * 2 means that this element is valid only in the Frameset DTD |
| 555 | * |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 556 | * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 557 | , subElements , impliedsubelt , Attributes, userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 558 | */ |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 559 | |
| 560 | /* Definitions and a couple of vars for HTML Elements */ |
| 561 | |
| 562 | #define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 563 | #define NB_FONTSTYLE 8 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 564 | #define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 565 | #define NB_PHRASE 10 |
Daniel Veillard | 491e58e | 2007-05-02 16:15:18 +0000 | [diff] [blame] | 566 | #define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe" |
| 567 | #define NB_SPECIAL 16 |
Eugene Pimenov | 4b41f15 | 2010-01-20 14:25:59 +0100 | [diff] [blame] | 568 | #define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 569 | #define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL |
Eugene Pimenov | 4b41f15 | 2010-01-20 14:25:59 +0100 | [diff] [blame] | 570 | #define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 571 | #define NB_BLOCK NB_HEADING + NB_LIST + 14 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 572 | #define FORMCTRL "input", "select", "textarea", "label", "button" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 573 | #define NB_FORMCTRL 5 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 574 | #define PCDATA |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 575 | #define NB_PCDATA 0 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 576 | #define HEADING "h1", "h2", "h3", "h4", "h5", "h6" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 577 | #define NB_HEADING 6 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 578 | #define LIST "ul", "ol", "dir", "menu" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 579 | #define NB_LIST 4 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 580 | #define MODIFIER |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 581 | #define NB_MODIFIER 0 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 582 | #define FLOW BLOCK,INLINE |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 583 | #define NB_FLOW NB_BLOCK + NB_INLINE |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 584 | #define EMPTY NULL |
| 585 | |
| 586 | |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 587 | static const char* const html_flow[] = { FLOW, NULL } ; |
| 588 | static const char* const html_inline[] = { INLINE, NULL } ; |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 589 | |
| 590 | /* placeholders: elts with content but no subelements */ |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 591 | static const char* const html_pcdata[] = { NULL } ; |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 592 | #define html_cdata html_pcdata |
| 593 | |
| 594 | |
| 595 | /* ... and for HTML Attributes */ |
| 596 | |
| 597 | #define COREATTRS "id", "class", "style", "title" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 598 | #define NB_COREATTRS 4 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 599 | #define I18N "lang", "dir" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 600 | #define NB_I18N 2 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 601 | #define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 602 | #define NB_EVENTS 9 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 603 | #define ATTRS COREATTRS,I18N,EVENTS |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 604 | #define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 605 | #define CELLHALIGN "align", "char", "charoff" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 606 | #define NB_CELLHALIGN 3 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 607 | #define CELLVALIGN "valign" |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 608 | #define NB_CELLVALIGN 1 |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 609 | |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 610 | static const char* const html_attrs[] = { ATTRS, NULL } ; |
| 611 | static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ; |
| 612 | static const char* const core_attrs[] = { COREATTRS, NULL } ; |
| 613 | static const char* const i18n_attrs[] = { I18N, NULL } ; |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 614 | |
| 615 | |
| 616 | /* Other declarations that should go inline ... */ |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 617 | static const char* const a_attrs[] = { ATTRS, "charset", "type", "name", |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 618 | "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords", |
| 619 | "tabindex", "onfocus", "onblur", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 620 | static const char* const target_attr[] = { "target", NULL } ; |
| 621 | static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ; |
| 622 | static const char* const alt_attr[] = { "alt", NULL } ; |
| 623 | static const char* const src_alt_attrs[] = { "src", "alt", NULL } ; |
| 624 | static const char* const href_attrs[] = { "href", NULL } ; |
| 625 | static const char* const clear_attrs[] = { "clear", NULL } ; |
| 626 | static const char* const inline_p[] = { INLINE, "p", NULL } ; |
| 627 | |
| 628 | static const char* const flow_param[] = { FLOW, "param", NULL } ; |
| 629 | static const char* const applet_attrs[] = { COREATTRS , "codebase", |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 630 | "archive", "alt", "name", "height", "width", "align", |
| 631 | "hspace", "vspace", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 632 | static const char* const area_attrs[] = { "shape", "coords", "href", "nohref", |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 633 | "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 634 | static const char* const basefont_attrs[] = |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 635 | { "id", "size", "color", "face", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 636 | static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ; |
| 637 | static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ; |
| 638 | static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ; |
| 639 | static const char* const body_depr[] = { "background", "bgcolor", "text", |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 640 | "link", "vlink", "alink", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 641 | static const char* const button_attrs[] = { ATTRS, "name", "value", "type", |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 642 | "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ; |
| 643 | |
| 644 | |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 645 | static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ; |
| 646 | static const char* const col_elt[] = { "col", NULL } ; |
| 647 | static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ; |
| 648 | static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ; |
| 649 | static const char* const dl_contents[] = { "dt", "dd", NULL } ; |
| 650 | static const char* const compact_attr[] = { "compact", NULL } ; |
| 651 | static const char* const label_attr[] = { "label", NULL } ; |
| 652 | static const char* const fieldset_contents[] = { FLOW, "legend" } ; |
| 653 | static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ; |
| 654 | static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ; |
| 655 | static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ; |
| 656 | static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ; |
| 657 | static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ; |
| 658 | static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ; |
| 659 | static const char* const head_attrs[] = { I18N, "profile", NULL } ; |
| 660 | static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ; |
| 661 | static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ; |
| 662 | static const char* const version_attr[] = { "version", NULL } ; |
| 663 | static const char* const html_content[] = { "head", "body", "frameset", NULL } ; |
| 664 | static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ; |
| 665 | static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ; |
Daniel Veillard | 491e58e | 2007-05-02 16:15:18 +0000 | [diff] [blame] | 666 | static const char* const embed_attrs[] = { COREATTRS, "align", "alt", "border", "code", "codebase", "frameborder", "height", "hidden", "hspace", "name", "palette", "pluginspace", "pluginurl", "src", "type", "units", "vspace", "width", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 667 | static const char* const input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ; |
| 668 | static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ; |
| 669 | static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ; |
| 670 | static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ; |
| 671 | static const char* const align_attr[] = { "align", NULL } ; |
| 672 | static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ; |
| 673 | static const char* const map_contents[] = { BLOCK, "area", NULL } ; |
| 674 | static const char* const name_attr[] = { "name", NULL } ; |
| 675 | static const char* const action_attr[] = { "action", NULL } ; |
| 676 | static const char* const blockli_elt[] = { BLOCK, "li", NULL } ; |
| 677 | static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ; |
| 678 | static const char* const content_attr[] = { "content", NULL } ; |
| 679 | static const char* const type_attr[] = { "type", NULL } ; |
| 680 | static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ; |
| 681 | static const char* const object_contents[] = { FLOW, "param", NULL } ; |
| 682 | static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ; |
| 683 | static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ; |
| 684 | static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ; |
| 685 | static const char* const option_elt[] = { "option", NULL } ; |
| 686 | static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ; |
| 687 | static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ; |
| 688 | static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ; |
| 689 | static const char* const width_attr[] = { "width", NULL } ; |
| 690 | static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ; |
| 691 | static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ; |
| 692 | static const char* const language_attr[] = { "language", NULL } ; |
| 693 | static const char* const select_content[] = { "optgroup", "option", NULL } ; |
| 694 | static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ; |
| 695 | static const char* const style_attrs[] = { I18N, "media", "title", NULL } ; |
Roland Steiner | 04f8eef | 2009-05-12 09:16:16 +0200 | [diff] [blame] | 696 | static const char* const table_attrs[] = { ATTRS, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ; |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 697 | static const char* const table_depr[] = { "align", "bgcolor", NULL } ; |
| 698 | static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ; |
| 699 | static const char* const tr_elt[] = { "tr", NULL } ; |
| 700 | static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ; |
| 701 | static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ; |
| 702 | static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ; |
| 703 | static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ; |
| 704 | static const char* const tr_contents[] = { "th", "td", NULL } ; |
| 705 | static const char* const bgcolor_attr[] = { "bgcolor", NULL } ; |
| 706 | static const char* const li_elt[] = { "li", NULL } ; |
| 707 | static const char* const ul_depr[] = { "type", "compact", NULL} ; |
| 708 | static const char* const dir_attr[] = { "dir", NULL} ; |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 709 | |
| 710 | #define DECL (const char**) |
| 711 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 712 | static const htmlElemDesc |
| 713 | html40ElementTable[] = { |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 714 | { "a", 0, 0, 0, 0, 0, 0, 1, "anchor ", |
| 715 | DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL |
| 716 | }, |
| 717 | { "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form", |
| 718 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 719 | }, |
| 720 | { "acronym", 0, 0, 0, 0, 0, 0, 1, "", |
| 721 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 722 | }, |
| 723 | { "address", 0, 0, 0, 0, 0, 0, 0, "information on author ", |
| 724 | DECL inline_p , NULL , DECL html_attrs, NULL, NULL |
| 725 | }, |
| 726 | { "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ", |
| 727 | DECL flow_param , NULL , NULL , DECL applet_attrs, NULL |
| 728 | }, |
| 729 | { "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ", |
| 730 | EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr |
| 731 | }, |
| 732 | { "b", 0, 3, 0, 0, 0, 0, 1, "bold text style", |
| 733 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 734 | }, |
| 735 | { "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ", |
| 736 | EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs |
| 737 | }, |
| 738 | { "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " , |
| 739 | EMPTY , NULL , NULL, DECL basefont_attrs, NULL |
| 740 | }, |
| 741 | { "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ", |
| 742 | DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr |
| 743 | }, |
| 744 | { "big", 0, 3, 0, 0, 0, 0, 1, "large text style", |
| 745 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 746 | }, |
| 747 | { "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ", |
| 748 | DECL html_flow , NULL , DECL quote_attrs , NULL, NULL |
| 749 | }, |
| 750 | { "body", 1, 1, 0, 0, 0, 0, 0, "document body ", |
| 751 | DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL |
| 752 | }, |
| 753 | { "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ", |
| 754 | EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL |
| 755 | }, |
| 756 | { "button", 0, 0, 0, 0, 0, 0, 2, "push button ", |
| 757 | DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL |
| 758 | }, |
| 759 | { "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ", |
| 760 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 761 | }, |
| 762 | { "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ", |
| 763 | DECL html_flow , NULL , NULL, DECL html_attrs, NULL |
| 764 | }, |
| 765 | { "cite", 0, 0, 0, 0, 0, 0, 1, "citation", |
| 766 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 767 | }, |
| 768 | { "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment", |
| 769 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 770 | }, |
| 771 | { "col", 0, 2, 2, 1, 0, 0, 0, "table column ", |
| 772 | EMPTY , NULL , DECL col_attrs , NULL, NULL |
| 773 | }, |
| 774 | { "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ", |
| 775 | DECL col_elt , "col" , DECL col_attrs , NULL, NULL |
| 776 | }, |
| 777 | { "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ", |
| 778 | DECL html_flow , NULL , DECL html_attrs, NULL, NULL |
| 779 | }, |
| 780 | { "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ", |
| 781 | DECL html_flow , NULL , DECL edit_attrs , NULL, NULL |
| 782 | }, |
| 783 | { "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition", |
| 784 | DECL html_inline , NULL , DECL html_attrs, NULL, NULL |
| 785 | }, |
| 786 | { "dir", 0, 0, 0, 0, 1, 1, 0, "directory list", |
| 787 | DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL |
| 788 | }, |
| 789 | { "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container", |
| 790 | DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL |
| 791 | }, |
| 792 | { "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ", |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 793 | DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 794 | }, |
| 795 | { "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ", |
| 796 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 797 | }, |
| 798 | { "em", 0, 3, 0, 0, 0, 0, 1, "emphasis", |
| 799 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 800 | }, |
Daniel Veillard | 640f89e | 2008-01-11 06:24:09 +0000 | [diff] [blame] | 801 | { "embed", 0, 1, 0, 0, 1, 1, 1, "generic embedded object ", |
Daniel Veillard | 491e58e | 2007-05-02 16:15:18 +0000 | [diff] [blame] | 802 | EMPTY, NULL, DECL embed_attrs, NULL, NULL |
| 803 | }, |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 804 | { "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ", |
| 805 | DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL |
| 806 | }, |
| 807 | { "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ", |
| 808 | DECL html_inline, NULL, NULL, DECL font_attrs, NULL |
| 809 | }, |
| 810 | { "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ", |
| 811 | DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr |
| 812 | }, |
| 813 | { "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " , |
| 814 | EMPTY, NULL, NULL, DECL frame_attrs, NULL |
| 815 | }, |
| 816 | { "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" , |
| 817 | DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL |
| 818 | }, |
| 819 | { "h1", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 820 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 821 | }, |
| 822 | { "h2", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 823 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 824 | }, |
| 825 | { "h3", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 826 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 827 | }, |
| 828 | { "h4", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 829 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 830 | }, |
| 831 | { "h5", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 832 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 833 | }, |
| 834 | { "h6", 0, 0, 0, 0, 0, 0, 0, "heading ", |
| 835 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 836 | }, |
| 837 | { "head", 1, 1, 0, 0, 0, 0, 0, "document head ", |
| 838 | DECL head_contents, NULL, DECL head_attrs, NULL, NULL |
| 839 | }, |
| 840 | { "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " , |
| 841 | EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL |
| 842 | }, |
| 843 | { "html", 1, 1, 0, 0, 0, 0, 0, "document root element ", |
| 844 | DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL |
| 845 | }, |
| 846 | { "i", 0, 3, 0, 0, 0, 0, 1, "italic text style", |
| 847 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 848 | }, |
| 849 | { "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ", |
| 850 | DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL |
| 851 | }, |
| 852 | { "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ", |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 853 | EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 854 | }, |
| 855 | { "input", 0, 2, 2, 1, 0, 0, 1, "form control ", |
| 856 | EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL |
| 857 | }, |
| 858 | { "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text", |
| 859 | DECL html_flow, NULL, DECL edit_attrs, NULL, NULL |
| 860 | }, |
| 861 | { "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ", |
| 862 | EMPTY, NULL, NULL, DECL prompt_attrs, NULL |
| 863 | }, |
| 864 | { "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user", |
| 865 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 866 | }, |
| 867 | { "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ", |
| 868 | DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL |
| 869 | }, |
| 870 | { "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ", |
| 871 | DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL |
| 872 | }, |
| 873 | { "li", 0, 1, 1, 0, 0, 0, 0, "list item ", |
| 874 | DECL html_flow, NULL, DECL html_attrs, NULL, NULL |
| 875 | }, |
| 876 | { "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ", |
| 877 | EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL |
| 878 | }, |
| 879 | { "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ", |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 880 | DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 881 | }, |
| 882 | { "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ", |
| 883 | DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL |
| 884 | }, |
| 885 | { "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ", |
| 886 | EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr |
| 887 | }, |
| 888 | { "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ", |
| 889 | DECL noframes_content, "body" , DECL html_attrs, NULL, NULL |
| 890 | }, |
| 891 | { "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ", |
| 892 | DECL html_flow, "div", DECL html_attrs, NULL, NULL |
| 893 | }, |
| 894 | { "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ", |
| 895 | DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL |
| 896 | }, |
| 897 | { "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ", |
| 898 | DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL |
| 899 | }, |
| 900 | { "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ", |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 901 | DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 902 | }, |
| 903 | { "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " , |
| 904 | DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL |
| 905 | }, |
| 906 | { "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ", |
| 907 | DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL |
| 908 | }, |
| 909 | { "param", 0, 2, 2, 1, 0, 0, 0, "named property value ", |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 910 | EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 911 | }, |
| 912 | { "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ", |
| 913 | DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL |
| 914 | }, |
| 915 | { "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ", |
| 916 | DECL html_inline, NULL, DECL quote_attrs, NULL, NULL |
| 917 | }, |
| 918 | { "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style", |
| 919 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 920 | }, |
| 921 | { "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.", |
| 922 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 923 | }, |
| 924 | { "script", 0, 0, 0, 0, 0, 0, 2, "script statements ", |
| 925 | DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr |
| 926 | }, |
| 927 | { "select", 0, 0, 0, 0, 0, 0, 1, "option selector ", |
| 928 | DECL select_content, NULL, DECL select_attrs, NULL, NULL |
| 929 | }, |
| 930 | { "small", 0, 3, 0, 0, 0, 0, 1, "small text style", |
| 931 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 932 | }, |
| 933 | { "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ", |
| 934 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 935 | }, |
| 936 | { "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text", |
| 937 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 938 | }, |
| 939 | { "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis", |
| 940 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 941 | }, |
| 942 | { "style", 0, 0, 0, 0, 0, 0, 0, "style info ", |
| 943 | DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr |
| 944 | }, |
| 945 | { "sub", 0, 3, 0, 0, 0, 0, 1, "subscript", |
| 946 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 947 | }, |
| 948 | { "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ", |
| 949 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 950 | }, |
| 951 | { "table", 0, 0, 0, 0, 0, 0, 0, "", |
| 952 | DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL |
| 953 | }, |
| 954 | { "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ", |
| 955 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 956 | }, |
| 957 | { "td", 0, 0, 0, 0, 0, 0, 0, "table data cell", |
| 958 | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
| 959 | }, |
| 960 | { "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ", |
| 961 | DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr |
| 962 | }, |
| 963 | { "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ", |
| 964 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 965 | }, |
| 966 | { "th", 0, 1, 0, 0, 0, 0, 0, "table header cell", |
| 967 | DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL |
| 968 | }, |
| 969 | { "thead", 0, 1, 0, 0, 0, 0, 0, "table header ", |
| 970 | DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL |
| 971 | }, |
| 972 | { "title", 0, 0, 0, 0, 0, 0, 0, "document title ", |
| 973 | DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL |
| 974 | }, |
| 975 | { "tr", 0, 0, 0, 0, 0, 0, 0, "table row ", |
| 976 | DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL |
| 977 | }, |
| 978 | { "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style", |
| 979 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 980 | }, |
| 981 | { "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style", |
| 982 | DECL html_inline, NULL, NULL, DECL html_attrs, NULL |
| 983 | }, |
| 984 | { "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ", |
| 985 | DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL |
| 986 | }, |
| 987 | { "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument", |
| 988 | DECL html_inline, NULL, DECL html_attrs, NULL, NULL |
| 989 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 990 | }; |
| 991 | |
| 992 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | * start tags that imply the end of current element |
| 994 | */ |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 995 | static const char * const htmlStartClose[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 996 | "form", "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6", |
| 997 | "dl", "ul", "ol", "menu", "dir", "address", "pre", |
| 998 | "listing", "xmp", "head", NULL, |
| 999 | "head", "p", NULL, |
| 1000 | "title", "p", NULL, |
| 1001 | "body", "head", "style", "link", "title", "p", NULL, |
Daniel Veillard | 25d5d9a | 2004-04-05 07:08:42 +0000 | [diff] [blame] | 1002 | "frameset", "head", "style", "link", "title", "p", NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1003 | "li", "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address", |
| 1004 | "pre", "listing", "xmp", "head", "li", NULL, |
| 1005 | "hr", "p", "head", NULL, |
| 1006 | "h1", "p", "head", NULL, |
| 1007 | "h2", "p", "head", NULL, |
| 1008 | "h3", "p", "head", NULL, |
| 1009 | "h4", "p", "head", NULL, |
| 1010 | "h5", "p", "head", NULL, |
| 1011 | "h6", "p", "head", NULL, |
| 1012 | "dir", "p", "head", NULL, |
| 1013 | "address", "p", "head", "ul", NULL, |
| 1014 | "pre", "p", "head", "ul", NULL, |
| 1015 | "listing", "p", "head", NULL, |
| 1016 | "xmp", "p", "head", NULL, |
| 1017 | "blockquote", "p", "head", NULL, |
| 1018 | "dl", "p", "dt", "menu", "dir", "address", "pre", "listing", |
| 1019 | "xmp", "head", NULL, |
| 1020 | "dt", "p", "menu", "dir", "address", "pre", "listing", "xmp", |
| 1021 | "head", "dd", NULL, |
| 1022 | "dd", "p", "menu", "dir", "address", "pre", "listing", "xmp", |
| 1023 | "head", "dt", NULL, |
| 1024 | "ul", "p", "head", "ol", "menu", "dir", "address", "pre", |
| 1025 | "listing", "xmp", NULL, |
| 1026 | "ol", "p", "head", "ul", NULL, |
| 1027 | "menu", "p", "head", "ul", NULL, |
Daniel Veillard | 6339c1a | 2009-08-24 11:59:51 +0200 | [diff] [blame] | 1028 | "p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", FONTSTYLE, NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1029 | "div", "p", "head", NULL, |
| 1030 | "noscript", "p", "head", NULL, |
| 1031 | "center", "font", "b", "i", "p", "head", NULL, |
| 1032 | "a", "a", NULL, |
| 1033 | "caption", "p", NULL, |
| 1034 | "colgroup", "caption", "colgroup", "col", "p", NULL, |
| 1035 | "col", "caption", "col", "p", NULL, |
| 1036 | "table", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre", |
| 1037 | "listing", "xmp", "a", NULL, |
Daniel Veillard | 43dadeb | 2001-04-24 11:23:35 +0000 | [diff] [blame] | 1038 | "th", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL, |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1039 | "td", "th", "td", "p", "span", "font", "a", "b", "i", "u", NULL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1040 | "tr", "th", "td", "tr", "caption", "col", "colgroup", "p", NULL, |
| 1041 | "thead", "caption", "col", "colgroup", NULL, |
| 1042 | "tfoot", "th", "td", "tr", "caption", "col", "colgroup", "thead", |
| 1043 | "tbody", "p", NULL, |
| 1044 | "tbody", "th", "td", "tr", "caption", "col", "colgroup", "thead", |
| 1045 | "tfoot", "tbody", "p", NULL, |
| 1046 | "optgroup", "option", NULL, |
| 1047 | "option", "option", NULL, |
| 1048 | "fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", |
| 1049 | "pre", "listing", "xmp", "a", NULL, |
| 1050 | NULL |
| 1051 | }; |
| 1052 | |
| 1053 | /* |
| 1054 | * The list of HTML elements which are supposed not to have |
| 1055 | * CDATA content and where a p element will be implied |
| 1056 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1057 | * TODO: extend that list by reading the HTML SGML DTD on |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1058 | * implied paragraph |
| 1059 | */ |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 1060 | static const char *const htmlNoContentElements[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1061 | "html", |
| 1062 | "head", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1063 | NULL |
| 1064 | }; |
| 1065 | |
| 1066 | /* |
| 1067 | * The list of HTML attributes which are of content %Script; |
| 1068 | * NOTE: when adding ones, check htmlIsScriptAttribute() since |
| 1069 | * it assumes the name starts with 'on' |
| 1070 | */ |
Daniel Veillard | 065abe8 | 2006-07-03 08:55:04 +0000 | [diff] [blame] | 1071 | static const char *const htmlScriptAttributes[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1072 | "onclick", |
| 1073 | "ondblclick", |
| 1074 | "onmousedown", |
| 1075 | "onmouseup", |
| 1076 | "onmouseover", |
| 1077 | "onmousemove", |
| 1078 | "onmouseout", |
| 1079 | "onkeypress", |
| 1080 | "onkeydown", |
| 1081 | "onkeyup", |
| 1082 | "onload", |
| 1083 | "onunload", |
| 1084 | "onfocus", |
| 1085 | "onblur", |
| 1086 | "onsubmit", |
| 1087 | "onrest", |
| 1088 | "onchange", |
| 1089 | "onselect" |
| 1090 | }; |
| 1091 | |
Daniel Veillard | a2bc368 | 2001-05-03 08:27:20 +0000 | [diff] [blame] | 1092 | /* |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1093 | * This table is used by the htmlparser to know what to do with |
| 1094 | * broken html pages. By assigning different priorities to different |
| 1095 | * elements the parser can decide how to handle extra endtags. |
| 1096 | * Endtags are only allowed to close elements with lower or equal |
| 1097 | * priority. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1098 | */ |
Daniel Veillard | a2bc368 | 2001-05-03 08:27:20 +0000 | [diff] [blame] | 1099 | |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1100 | typedef struct { |
| 1101 | const char *name; |
| 1102 | int priority; |
| 1103 | } elementPriority; |
| 1104 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 1105 | static const elementPriority htmlEndPriority[] = { |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1106 | {"div", 150}, |
| 1107 | {"td", 160}, |
| 1108 | {"th", 160}, |
| 1109 | {"tr", 170}, |
| 1110 | {"thead", 180}, |
| 1111 | {"tbody", 180}, |
| 1112 | {"tfoot", 180}, |
| 1113 | {"table", 190}, |
| 1114 | {"head", 200}, |
| 1115 | {"body", 200}, |
| 1116 | {"html", 220}, |
| 1117 | {NULL, 100} /* Default priority */ |
| 1118 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1119 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1120 | static const char** htmlStartCloseIndex[100]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1121 | static int htmlStartCloseIndexinitialized = 0; |
| 1122 | |
| 1123 | /************************************************************************ |
| 1124 | * * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1125 | * functions to handle HTML specific data * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1126 | * * |
| 1127 | ************************************************************************/ |
| 1128 | |
| 1129 | /** |
| 1130 | * htmlInitAutoClose: |
| 1131 | * |
| 1132 | * Initialize the htmlStartCloseIndex for fast lookup of closing tags names. |
| 1133 | * This is not reentrant. Call xmlInitParser() once before processing in |
| 1134 | * case of use in multithreaded programs. |
| 1135 | */ |
| 1136 | void |
| 1137 | htmlInitAutoClose(void) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1138 | int indx, i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (htmlStartCloseIndexinitialized) return; |
| 1141 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1142 | for (indx = 0;indx < 100;indx ++) htmlStartCloseIndex[indx] = NULL; |
| 1143 | indx = 0; |
| 1144 | while ((htmlStartClose[i] != NULL) && (indx < 100 - 1)) { |
Daniel Veillard | 28aac0b | 2006-10-16 08:31:18 +0000 | [diff] [blame] | 1145 | htmlStartCloseIndex[indx++] = (const char**) &htmlStartClose[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1146 | while (htmlStartClose[i] != NULL) i++; |
| 1147 | i++; |
| 1148 | } |
| 1149 | htmlStartCloseIndexinitialized = 1; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * htmlTagLookup: |
| 1154 | * @tag: The tag name in lowercase |
| 1155 | * |
| 1156 | * Lookup the HTML tag in the ElementTable |
| 1157 | * |
| 1158 | * Returns the related htmlElemDescPtr or NULL if not found. |
| 1159 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1160 | const htmlElemDesc * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1161 | htmlTagLookup(const xmlChar *tag) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1162 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1163 | |
| 1164 | for (i = 0; i < (sizeof(html40ElementTable) / |
| 1165 | sizeof(html40ElementTable[0]));i++) { |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 1166 | if (!xmlStrcasecmp(tag, BAD_CAST html40ElementTable[i].name)) |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1167 | return((htmlElemDescPtr) &html40ElementTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1168 | } |
| 1169 | return(NULL); |
| 1170 | } |
| 1171 | |
| 1172 | /** |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1173 | * htmlGetEndPriority: |
| 1174 | * @name: The name of the element to look up the priority for. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1175 | * |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1176 | * Return value: The "endtag" priority. |
| 1177 | **/ |
| 1178 | static int |
| 1179 | htmlGetEndPriority (const xmlChar *name) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1180 | int i = 0; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1181 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1182 | while ((htmlEndPriority[i].name != NULL) && |
| 1183 | (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name))) |
| 1184 | i++; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1185 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1186 | return(htmlEndPriority[i].priority); |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1189 | |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1190 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1191 | * htmlCheckAutoClose: |
| 1192 | * @newtag: The new tag name |
| 1193 | * @oldtag: The old tag name |
| 1194 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1195 | * Checks whether the new tag is one of the registered valid tags for |
| 1196 | * closing old. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1197 | * Initialize the htmlStartCloseIndex for fast lookup of closing tags names. |
| 1198 | * |
| 1199 | * Returns 0 if no, 1 if yes. |
| 1200 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1201 | static int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1202 | htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag) |
| 1203 | { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1204 | int i, indx; |
| 1205 | const char **closed = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1207 | if (htmlStartCloseIndexinitialized == 0) |
| 1208 | htmlInitAutoClose(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* inefficient, but not a big deal */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1211 | for (indx = 0; indx < 100; indx++) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1212 | closed = htmlStartCloseIndex[indx]; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1213 | if (closed == NULL) |
| 1214 | return (0); |
| 1215 | if (xmlStrEqual(BAD_CAST * closed, newtag)) |
| 1216 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1219 | i = closed - htmlStartClose; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | i++; |
| 1221 | while (htmlStartClose[i] != NULL) { |
| 1222 | if (xmlStrEqual(BAD_CAST htmlStartClose[i], oldtag)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1223 | return (1); |
| 1224 | } |
| 1225 | i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1227 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * htmlAutoCloseOnClose: |
| 1232 | * @ctxt: an HTML parser context |
| 1233 | * @newtag: The new tag name |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1234 | * @force: force the tag closure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1235 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1236 | * The HTML DTD allows an ending tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1237 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1238 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1239 | htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
| 1240 | { |
| 1241 | const htmlElemDesc *info; |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1242 | int i, priority; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1243 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1244 | priority = htmlGetEndPriority(newtag); |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1245 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1246 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
Daniel Veillard | 0a2a163 | 2001-05-11 14:18:03 +0000 | [diff] [blame] | 1247 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1248 | if (xmlStrEqual(newtag, ctxt->nameTab[i])) |
| 1249 | break; |
| 1250 | /* |
| 1251 | * A missplaced endtag can only close elements with lower |
| 1252 | * or equal priority, so if we find an element with higher |
| 1253 | * priority before we find an element with |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1254 | * matching name, we just ignore this endtag |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1255 | */ |
| 1256 | if (htmlGetEndPriority(ctxt->nameTab[i]) > priority) |
| 1257 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1258 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1259 | if (i < 0) |
| 1260 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1261 | |
| 1262 | while (!xmlStrEqual(newtag, ctxt->name)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1263 | info = htmlTagLookup(ctxt->name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1264 | if ((info != NULL) && (info->endTag == 3)) { |
| 1265 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 1266 | "Opening and ending tag mismatch: %s and %s\n", |
Daniel Veillard | 05bcb7e | 2003-10-19 14:26:34 +0000 | [diff] [blame] | 1267 | newtag, ctxt->name); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1268 | } |
| 1269 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1270 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1271 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /** |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1276 | * htmlAutoCloseOnEnd: |
| 1277 | * @ctxt: an HTML parser context |
| 1278 | * |
| 1279 | * Close all remaining tags at the end of the stream |
| 1280 | */ |
| 1281 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1282 | htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt) |
| 1283 | { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1284 | int i; |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1285 | |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1286 | if (ctxt->nameNr == 0) |
| 1287 | return; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1288 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1289 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1290 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1291 | htmlnamePop(ctxt); |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1296 | * htmlAutoClose: |
| 1297 | * @ctxt: an HTML parser context |
| 1298 | * @newtag: The new tag name or NULL |
| 1299 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1300 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1301 | * The list is kept in htmlStartClose array. This function is |
| 1302 | * called when a new tag has been detected and generates the |
| 1303 | * appropriates closes if possible/needed. |
| 1304 | * If newtag is NULL this mean we are at the end of the resource |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1305 | * and we should check |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1306 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1307 | static void |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1308 | htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag) |
| 1309 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1310 | while ((newtag != NULL) && (ctxt->name != NULL) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1311 | (htmlCheckAutoClose(newtag, ctxt->name))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1312 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1313 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1314 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1315 | } |
| 1316 | if (newtag == NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1317 | htmlAutoCloseOnEnd(ctxt); |
| 1318 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1319 | } |
| 1320 | while ((newtag == NULL) && (ctxt->name != NULL) && |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1321 | ((xmlStrEqual(ctxt->name, BAD_CAST "head")) || |
| 1322 | (xmlStrEqual(ctxt->name, BAD_CAST "body")) || |
| 1323 | (xmlStrEqual(ctxt->name, BAD_CAST "html")))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1324 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 1325 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 1326 | htmlnamePop(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1327 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * htmlAutoCloseTag: |
| 1332 | * @doc: the HTML document |
| 1333 | * @name: The tag name |
| 1334 | * @elem: the HTML element |
| 1335 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1336 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1337 | * The list is kept in htmlStartClose array. This function checks |
| 1338 | * if the element or one of it's children would autoclose the |
| 1339 | * given tag. |
| 1340 | * |
| 1341 | * Returns 1 if autoclose, 0 otherwise |
| 1342 | */ |
| 1343 | int |
| 1344 | htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) { |
| 1345 | htmlNodePtr child; |
| 1346 | |
| 1347 | if (elem == NULL) return(1); |
| 1348 | if (xmlStrEqual(name, elem->name)) return(0); |
| 1349 | if (htmlCheckAutoClose(elem->name, name)) return(1); |
| 1350 | child = elem->children; |
| 1351 | while (child != NULL) { |
| 1352 | if (htmlAutoCloseTag(doc, name, child)) return(1); |
| 1353 | child = child->next; |
| 1354 | } |
| 1355 | return(0); |
| 1356 | } |
| 1357 | |
| 1358 | /** |
| 1359 | * htmlIsAutoClosed: |
| 1360 | * @doc: the HTML document |
| 1361 | * @elem: the HTML element |
| 1362 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1363 | * The HTML DTD allows a tag to implicitly close other tags. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1364 | * The list is kept in htmlStartClose array. This function checks |
| 1365 | * if a tag is autoclosed by one of it's child |
| 1366 | * |
| 1367 | * Returns 1 if autoclosed, 0 otherwise |
| 1368 | */ |
| 1369 | int |
| 1370 | htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) { |
| 1371 | htmlNodePtr child; |
| 1372 | |
| 1373 | if (elem == NULL) return(1); |
| 1374 | child = elem->children; |
| 1375 | while (child != NULL) { |
| 1376 | if (htmlAutoCloseTag(doc, elem->name, child)) return(1); |
| 1377 | child = child->next; |
| 1378 | } |
| 1379 | return(0); |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * htmlCheckImplied: |
| 1384 | * @ctxt: an HTML parser context |
| 1385 | * @newtag: The new tag name |
| 1386 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1387 | * The HTML DTD allows a tag to exists only implicitly |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1388 | * called when a new tag has been detected and generates the |
| 1389 | * appropriates implicit tags if missing |
| 1390 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1391 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1392 | htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) { |
Daniel Veillard | b468f74 | 2009-08-24 18:45:33 +0200 | [diff] [blame] | 1393 | int i; |
| 1394 | |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 1395 | if (ctxt->options & HTML_PARSE_NOIMPLIED) |
| 1396 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1397 | if (!htmlOmittedDefaultValue) |
| 1398 | return; |
| 1399 | if (xmlStrEqual(newtag, BAD_CAST"html")) |
| 1400 | return; |
| 1401 | if (ctxt->nameNr <= 0) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1402 | htmlnamePush(ctxt, BAD_CAST"html"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1403 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1404 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL); |
| 1405 | } |
| 1406 | if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head"))) |
| 1407 | return; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1408 | if ((ctxt->nameNr <= 1) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1409 | ((xmlStrEqual(newtag, BAD_CAST"script")) || |
| 1410 | (xmlStrEqual(newtag, BAD_CAST"style")) || |
| 1411 | (xmlStrEqual(newtag, BAD_CAST"meta")) || |
| 1412 | (xmlStrEqual(newtag, BAD_CAST"link")) || |
| 1413 | (xmlStrEqual(newtag, BAD_CAST"title")) || |
| 1414 | (xmlStrEqual(newtag, BAD_CAST"base")))) { |
Daniel Veillard | 029a04d | 2009-08-24 12:50:23 +0200 | [diff] [blame] | 1415 | if (ctxt->html >= 3) { |
| 1416 | /* we already saw or generated an <head> before */ |
| 1417 | return; |
| 1418 | } |
| 1419 | /* |
| 1420 | * dropped OBJECT ... i you put it first BODY will be |
| 1421 | * assumed ! |
| 1422 | */ |
| 1423 | htmlnamePush(ctxt, BAD_CAST"head"); |
| 1424 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1425 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1426 | } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) && |
| 1427 | (!xmlStrEqual(newtag, BAD_CAST"frame")) && |
| 1428 | (!xmlStrEqual(newtag, BAD_CAST"frameset"))) { |
Daniel Veillard | 029a04d | 2009-08-24 12:50:23 +0200 | [diff] [blame] | 1429 | if (ctxt->html >= 10) { |
| 1430 | /* we already saw or generated a <body> before */ |
| 1431 | return; |
| 1432 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | for (i = 0;i < ctxt->nameNr;i++) { |
| 1434 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) { |
| 1435 | return; |
| 1436 | } |
| 1437 | if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) { |
| 1438 | return; |
| 1439 | } |
| 1440 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1441 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1442 | htmlnamePush(ctxt, BAD_CAST"body"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1443 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1444 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * htmlCheckParagraph |
| 1450 | * @ctxt: an HTML parser context |
| 1451 | * |
| 1452 | * Check whether a p element need to be implied before inserting |
| 1453 | * characters in the current element. |
| 1454 | * |
| 1455 | * Returns 1 if a paragraph has been inserted, 0 if not and -1 |
| 1456 | * in case of error. |
| 1457 | */ |
| 1458 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1459 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1460 | htmlCheckParagraph(htmlParserCtxtPtr ctxt) { |
| 1461 | const xmlChar *tag; |
| 1462 | int i; |
| 1463 | |
| 1464 | if (ctxt == NULL) |
| 1465 | return(-1); |
| 1466 | tag = ctxt->name; |
| 1467 | if (tag == NULL) { |
| 1468 | htmlAutoClose(ctxt, BAD_CAST"p"); |
| 1469 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1470 | htmlnamePush(ctxt, BAD_CAST"p"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1471 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1472 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
| 1473 | return(1); |
| 1474 | } |
| 1475 | if (!htmlOmittedDefaultValue) |
| 1476 | return(0); |
| 1477 | for (i = 0; htmlNoContentElements[i] != NULL; i++) { |
| 1478 | if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1479 | htmlAutoClose(ctxt, BAD_CAST"p"); |
| 1480 | htmlCheckImplied(ctxt, BAD_CAST"p"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1481 | htmlnamePush(ctxt, BAD_CAST"p"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1482 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) |
| 1483 | ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL); |
| 1484 | return(1); |
| 1485 | } |
| 1486 | } |
| 1487 | return(0); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * htmlIsScriptAttribute: |
| 1492 | * @name: an attribute name |
| 1493 | * |
| 1494 | * Check if an attribute is of content type Script |
| 1495 | * |
| 1496 | * Returns 1 is the attribute is a script 0 otherwise |
| 1497 | */ |
| 1498 | int |
| 1499 | htmlIsScriptAttribute(const xmlChar *name) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1500 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1501 | |
| 1502 | if (name == NULL) |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1503 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | /* |
| 1505 | * all script attributes start with 'on' |
| 1506 | */ |
| 1507 | if ((name[0] != 'o') || (name[1] != 'n')) |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1508 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1509 | for (i = 0; |
| 1510 | i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]); |
| 1511 | i++) { |
| 1512 | if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i])) |
| 1513 | return(1); |
| 1514 | } |
| 1515 | return(0); |
| 1516 | } |
| 1517 | |
| 1518 | /************************************************************************ |
| 1519 | * * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1520 | * The list of HTML predefined entities * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | * * |
| 1522 | ************************************************************************/ |
| 1523 | |
| 1524 | |
Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 1525 | static const htmlEntityDesc html40EntitiesTable[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1526 | /* |
| 1527 | * the 4 absolute ones, plus apostrophe. |
| 1528 | */ |
| 1529 | { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" }, |
| 1530 | { 38, "amp", "ampersand, U+0026 ISOnum" }, |
| 1531 | { 39, "apos", "single quote" }, |
| 1532 | { 60, "lt", "less-than sign, U+003C ISOnum" }, |
| 1533 | { 62, "gt", "greater-than sign, U+003E ISOnum" }, |
| 1534 | |
| 1535 | /* |
| 1536 | * A bunch still in the 128-255 range |
| 1537 | * Replacing them depend really on the charset used. |
| 1538 | */ |
| 1539 | { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" }, |
| 1540 | { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" }, |
| 1541 | { 162, "cent", "cent sign, U+00A2 ISOnum" }, |
| 1542 | { 163, "pound","pound sign, U+00A3 ISOnum" }, |
| 1543 | { 164, "curren","currency sign, U+00A4 ISOnum" }, |
| 1544 | { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" }, |
| 1545 | { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" }, |
| 1546 | { 167, "sect", "section sign, U+00A7 ISOnum" }, |
| 1547 | { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" }, |
| 1548 | { 169, "copy", "copyright sign, U+00A9 ISOnum" }, |
| 1549 | { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" }, |
| 1550 | { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" }, |
| 1551 | { 172, "not", "not sign, U+00AC ISOnum" }, |
| 1552 | { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" }, |
| 1553 | { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" }, |
| 1554 | { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" }, |
| 1555 | { 176, "deg", "degree sign, U+00B0 ISOnum" }, |
| 1556 | { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" }, |
| 1557 | { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" }, |
| 1558 | { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" }, |
| 1559 | { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" }, |
| 1560 | { 181, "micro","micro sign, U+00B5 ISOnum" }, |
| 1561 | { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" }, |
| 1562 | { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" }, |
| 1563 | { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" }, |
| 1564 | { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" }, |
| 1565 | { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" }, |
| 1566 | { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" }, |
| 1567 | { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" }, |
| 1568 | { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" }, |
| 1569 | { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" }, |
| 1570 | { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" }, |
| 1571 | { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" }, |
| 1572 | { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" }, |
| 1573 | { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" }, |
| 1574 | { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" }, |
| 1575 | { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" }, |
| 1576 | { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" }, |
| 1577 | { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" }, |
| 1578 | { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" }, |
| 1579 | { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" }, |
| 1580 | { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" }, |
| 1581 | { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" }, |
| 1582 | { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" }, |
| 1583 | { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" }, |
| 1584 | { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" }, |
| 1585 | { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" }, |
| 1586 | { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" }, |
| 1587 | { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" }, |
| 1588 | { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" }, |
| 1589 | { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" }, |
| 1590 | { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" }, |
| 1591 | { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" }, |
| 1592 | { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" }, |
| 1593 | { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" }, |
| 1594 | { 215, "times","multiplication sign, U+00D7 ISOnum" }, |
| 1595 | { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" }, |
| 1596 | { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" }, |
| 1597 | { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" }, |
| 1598 | { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" }, |
| 1599 | { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" }, |
| 1600 | { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" }, |
| 1601 | { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" }, |
| 1602 | { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" }, |
| 1603 | { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" }, |
| 1604 | { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" }, |
| 1605 | { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" }, |
| 1606 | { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" }, |
| 1607 | { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" }, |
| 1608 | { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" }, |
| 1609 | { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" }, |
| 1610 | { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" }, |
| 1611 | { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" }, |
| 1612 | { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" }, |
| 1613 | { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" }, |
| 1614 | { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" }, |
| 1615 | { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" }, |
| 1616 | { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" }, |
| 1617 | { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" }, |
| 1618 | { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" }, |
| 1619 | { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" }, |
| 1620 | { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" }, |
| 1621 | { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" }, |
| 1622 | { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" }, |
| 1623 | { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" }, |
| 1624 | { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" }, |
| 1625 | { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" }, |
| 1626 | { 247, "divide","division sign, U+00F7 ISOnum" }, |
| 1627 | { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" }, |
| 1628 | { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" }, |
| 1629 | { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" }, |
| 1630 | { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" }, |
| 1631 | { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" }, |
| 1632 | { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" }, |
| 1633 | { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" }, |
| 1634 | { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" }, |
| 1635 | |
| 1636 | { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" }, |
| 1637 | { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" }, |
| 1638 | { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" }, |
| 1639 | { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" }, |
| 1640 | { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" }, |
| 1641 | |
| 1642 | /* |
| 1643 | * Anything below should really be kept as entities references |
| 1644 | */ |
| 1645 | { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" }, |
| 1646 | |
| 1647 | { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" }, |
| 1648 | { 732, "tilde","small tilde, U+02DC ISOdia" }, |
| 1649 | |
| 1650 | { 913, "Alpha","greek capital letter alpha, U+0391" }, |
| 1651 | { 914, "Beta", "greek capital letter beta, U+0392" }, |
| 1652 | { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" }, |
| 1653 | { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" }, |
| 1654 | { 917, "Epsilon","greek capital letter epsilon, U+0395" }, |
| 1655 | { 918, "Zeta", "greek capital letter zeta, U+0396" }, |
| 1656 | { 919, "Eta", "greek capital letter eta, U+0397" }, |
| 1657 | { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" }, |
| 1658 | { 921, "Iota", "greek capital letter iota, U+0399" }, |
| 1659 | { 922, "Kappa","greek capital letter kappa, U+039A" }, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1660 | { 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" }, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1661 | { 924, "Mu", "greek capital letter mu, U+039C" }, |
| 1662 | { 925, "Nu", "greek capital letter nu, U+039D" }, |
| 1663 | { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" }, |
| 1664 | { 927, "Omicron","greek capital letter omicron, U+039F" }, |
| 1665 | { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" }, |
| 1666 | { 929, "Rho", "greek capital letter rho, U+03A1" }, |
| 1667 | { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" }, |
| 1668 | { 932, "Tau", "greek capital letter tau, U+03A4" }, |
| 1669 | { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" }, |
| 1670 | { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" }, |
| 1671 | { 935, "Chi", "greek capital letter chi, U+03A7" }, |
| 1672 | { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" }, |
| 1673 | { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" }, |
| 1674 | |
| 1675 | { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" }, |
| 1676 | { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" }, |
| 1677 | { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" }, |
| 1678 | { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" }, |
| 1679 | { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" }, |
| 1680 | { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" }, |
| 1681 | { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" }, |
| 1682 | { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" }, |
| 1683 | { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" }, |
| 1684 | { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" }, |
| 1685 | { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" }, |
| 1686 | { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" }, |
| 1687 | { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" }, |
| 1688 | { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" }, |
| 1689 | { 959, "omicron","greek small letter omicron, U+03BF NEW" }, |
| 1690 | { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" }, |
| 1691 | { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" }, |
| 1692 | { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" }, |
| 1693 | { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" }, |
| 1694 | { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" }, |
| 1695 | { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" }, |
| 1696 | { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" }, |
| 1697 | { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" }, |
| 1698 | { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" }, |
| 1699 | { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" }, |
| 1700 | { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" }, |
| 1701 | { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" }, |
| 1702 | { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" }, |
| 1703 | |
| 1704 | { 8194, "ensp", "en space, U+2002 ISOpub" }, |
| 1705 | { 8195, "emsp", "em space, U+2003 ISOpub" }, |
| 1706 | { 8201, "thinsp","thin space, U+2009 ISOpub" }, |
| 1707 | { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" }, |
| 1708 | { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" }, |
| 1709 | { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" }, |
| 1710 | { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" }, |
| 1711 | { 8211, "ndash","en dash, U+2013 ISOpub" }, |
| 1712 | { 8212, "mdash","em dash, U+2014 ISOpub" }, |
| 1713 | { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" }, |
| 1714 | { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" }, |
| 1715 | { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" }, |
| 1716 | { 8220, "ldquo","left double quotation mark, U+201C ISOnum" }, |
| 1717 | { 8221, "rdquo","right double quotation mark, U+201D ISOnum" }, |
| 1718 | { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" }, |
| 1719 | { 8224, "dagger","dagger, U+2020 ISOpub" }, |
| 1720 | { 8225, "Dagger","double dagger, U+2021 ISOpub" }, |
| 1721 | |
| 1722 | { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" }, |
| 1723 | { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" }, |
| 1724 | |
| 1725 | { 8240, "permil","per mille sign, U+2030 ISOtech" }, |
| 1726 | |
| 1727 | { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" }, |
| 1728 | { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" }, |
| 1729 | |
| 1730 | { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" }, |
| 1731 | { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" }, |
| 1732 | |
| 1733 | { 8254, "oline","overline = spacing overscore, U+203E NEW" }, |
| 1734 | { 8260, "frasl","fraction slash, U+2044 NEW" }, |
| 1735 | |
| 1736 | { 8364, "euro", "euro sign, U+20AC NEW" }, |
| 1737 | |
| 1738 | { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" }, |
| 1739 | { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" }, |
| 1740 | { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" }, |
| 1741 | { 8482, "trade","trade mark sign, U+2122 ISOnum" }, |
| 1742 | { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" }, |
| 1743 | { 8592, "larr", "leftwards arrow, U+2190 ISOnum" }, |
| 1744 | { 8593, "uarr", "upwards arrow, U+2191 ISOnum" }, |
| 1745 | { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" }, |
| 1746 | { 8595, "darr", "downwards arrow, U+2193 ISOnum" }, |
| 1747 | { 8596, "harr", "left right arrow, U+2194 ISOamsa" }, |
| 1748 | { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" }, |
| 1749 | { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" }, |
| 1750 | { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" }, |
| 1751 | { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" }, |
| 1752 | { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" }, |
| 1753 | { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" }, |
| 1754 | |
| 1755 | { 8704, "forall","for all, U+2200 ISOtech" }, |
| 1756 | { 8706, "part", "partial differential, U+2202 ISOtech" }, |
| 1757 | { 8707, "exist","there exists, U+2203 ISOtech" }, |
| 1758 | { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" }, |
| 1759 | { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" }, |
| 1760 | { 8712, "isin", "element of, U+2208 ISOtech" }, |
| 1761 | { 8713, "notin","not an element of, U+2209 ISOtech" }, |
| 1762 | { 8715, "ni", "contains as member, U+220B ISOtech" }, |
| 1763 | { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" }, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1764 | { 8721, "sum", "n-ary summation, U+2211 ISOamsb" }, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1765 | { 8722, "minus","minus sign, U+2212 ISOtech" }, |
| 1766 | { 8727, "lowast","asterisk operator, U+2217 ISOtech" }, |
| 1767 | { 8730, "radic","square root = radical sign, U+221A ISOtech" }, |
| 1768 | { 8733, "prop", "proportional to, U+221D ISOtech" }, |
| 1769 | { 8734, "infin","infinity, U+221E ISOtech" }, |
| 1770 | { 8736, "ang", "angle, U+2220 ISOamso" }, |
| 1771 | { 8743, "and", "logical and = wedge, U+2227 ISOtech" }, |
| 1772 | { 8744, "or", "logical or = vee, U+2228 ISOtech" }, |
| 1773 | { 8745, "cap", "intersection = cap, U+2229 ISOtech" }, |
| 1774 | { 8746, "cup", "union = cup, U+222A ISOtech" }, |
| 1775 | { 8747, "int", "integral, U+222B ISOtech" }, |
| 1776 | { 8756, "there4","therefore, U+2234 ISOtech" }, |
| 1777 | { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" }, |
| 1778 | { 8773, "cong", "approximately equal to, U+2245 ISOtech" }, |
| 1779 | { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" }, |
| 1780 | { 8800, "ne", "not equal to, U+2260 ISOtech" }, |
| 1781 | { 8801, "equiv","identical to, U+2261 ISOtech" }, |
| 1782 | { 8804, "le", "less-than or equal to, U+2264 ISOtech" }, |
| 1783 | { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" }, |
| 1784 | { 8834, "sub", "subset of, U+2282 ISOtech" }, |
| 1785 | { 8835, "sup", "superset of, U+2283 ISOtech" }, |
| 1786 | { 8836, "nsub", "not a subset of, U+2284 ISOamsn" }, |
| 1787 | { 8838, "sube", "subset of or equal to, U+2286 ISOtech" }, |
| 1788 | { 8839, "supe", "superset of or equal to, U+2287 ISOtech" }, |
| 1789 | { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" }, |
| 1790 | { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" }, |
| 1791 | { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" }, |
| 1792 | { 8901, "sdot", "dot operator, U+22C5 ISOamsb" }, |
| 1793 | { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" }, |
| 1794 | { 8969, "rceil","right ceiling, U+2309 ISOamsc" }, |
| 1795 | { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" }, |
| 1796 | { 8971, "rfloor","right floor, U+230B ISOamsc" }, |
| 1797 | { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" }, |
| 1798 | { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" }, |
| 1799 | { 9674, "loz", "lozenge, U+25CA ISOpub" }, |
| 1800 | |
| 1801 | { 9824, "spades","black spade suit, U+2660 ISOpub" }, |
| 1802 | { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" }, |
| 1803 | { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" }, |
| 1804 | { 9830, "diams","black diamond suit, U+2666 ISOpub" }, |
| 1805 | |
| 1806 | }; |
| 1807 | |
| 1808 | /************************************************************************ |
| 1809 | * * |
| 1810 | * Commodity functions to handle entities * |
| 1811 | * * |
| 1812 | ************************************************************************/ |
| 1813 | |
| 1814 | /* |
| 1815 | * Macro used to grow the current buffer. |
| 1816 | */ |
| 1817 | #define growBuffer(buffer) { \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1818 | xmlChar *tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1819 | buffer##_size *= 2; \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1820 | tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
| 1821 | if (tmp == NULL) { \ |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1822 | htmlErrMemory(ctxt, "growing buffer\n"); \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1823 | xmlFree(buffer); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1824 | return(NULL); \ |
| 1825 | } \ |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1826 | buffer = tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | /** |
| 1830 | * htmlEntityLookup: |
| 1831 | * @name: the entity name |
| 1832 | * |
| 1833 | * Lookup the given entity in EntitiesTable |
| 1834 | * |
| 1835 | * TODO: the linear scan is really ugly, an hash table is really needed. |
| 1836 | * |
| 1837 | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
| 1838 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1839 | const htmlEntityDesc * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1840 | htmlEntityLookup(const xmlChar *name) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1841 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1842 | |
| 1843 | for (i = 0;i < (sizeof(html40EntitiesTable)/ |
| 1844 | sizeof(html40EntitiesTable[0]));i++) { |
| 1845 | if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1846 | return((htmlEntityDescPtr) &html40EntitiesTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | return(NULL); |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * htmlEntityValueLookup: |
| 1854 | * @value: the entity's unicode value |
| 1855 | * |
| 1856 | * Lookup the given entity in EntitiesTable |
| 1857 | * |
| 1858 | * TODO: the linear scan is really ugly, an hash table is really needed. |
| 1859 | * |
| 1860 | * Returns the associated htmlEntityDescPtr if found, NULL otherwise. |
| 1861 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1862 | const htmlEntityDesc * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1863 | htmlEntityValueLookup(unsigned int value) { |
| 1864 | unsigned int i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1865 | |
| 1866 | for (i = 0;i < (sizeof(html40EntitiesTable)/ |
| 1867 | sizeof(html40EntitiesTable[0]));i++) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1868 | if (html40EntitiesTable[i].value >= value) { |
| 1869 | if (html40EntitiesTable[i].value > value) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1870 | break; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1871 | return((htmlEntityDescPtr) &html40EntitiesTable[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1872 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | } |
| 1874 | return(NULL); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * UTF8ToHtml: |
| 1879 | * @out: a pointer to an array of bytes to store the result |
| 1880 | * @outlen: the length of @out |
| 1881 | * @in: a pointer to an array of UTF-8 chars |
| 1882 | * @inlen: the length of @in |
| 1883 | * |
| 1884 | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
| 1885 | * plus HTML entities block of chars out. |
| 1886 | * |
| 1887 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
| 1888 | * The value of @inlen after return is the number of octets consumed |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1889 | * as the return value is positive, else unpredictable. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1890 | * The value of @outlen after return is the number of octets consumed. |
| 1891 | */ |
| 1892 | int |
| 1893 | UTF8ToHtml(unsigned char* out, int *outlen, |
| 1894 | const unsigned char* in, int *inlen) { |
| 1895 | const unsigned char* processed = in; |
| 1896 | const unsigned char* outend; |
| 1897 | const unsigned char* outstart = out; |
| 1898 | const unsigned char* instart = in; |
| 1899 | const unsigned char* inend; |
| 1900 | unsigned int c, d; |
| 1901 | int trailing; |
| 1902 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 1903 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1904 | if (in == NULL) { |
| 1905 | /* |
| 1906 | * initialization nothing to do |
| 1907 | */ |
| 1908 | *outlen = 0; |
| 1909 | *inlen = 0; |
| 1910 | return(0); |
| 1911 | } |
| 1912 | inend = in + (*inlen); |
| 1913 | outend = out + (*outlen); |
| 1914 | while (in < inend) { |
| 1915 | d = *in++; |
| 1916 | if (d < 0x80) { c= d; trailing= 0; } |
| 1917 | else if (d < 0xC0) { |
| 1918 | /* trailing byte in leading position */ |
| 1919 | *outlen = out - outstart; |
| 1920 | *inlen = processed - instart; |
| 1921 | return(-2); |
| 1922 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 1923 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 1924 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 1925 | else { |
| 1926 | /* no chance for this in Ascii */ |
| 1927 | *outlen = out - outstart; |
| 1928 | *inlen = processed - instart; |
| 1929 | return(-2); |
| 1930 | } |
| 1931 | |
| 1932 | if (inend - in < trailing) { |
| 1933 | break; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 1934 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1935 | |
| 1936 | for ( ; trailing; trailing--) { |
| 1937 | if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80)) |
| 1938 | break; |
| 1939 | c <<= 6; |
| 1940 | c |= d & 0x3F; |
| 1941 | } |
| 1942 | |
| 1943 | /* assertion: c is a single UTF-4 value */ |
| 1944 | if (c < 0x80) { |
| 1945 | if (out + 1 >= outend) |
| 1946 | break; |
| 1947 | *out++ = c; |
| 1948 | } else { |
| 1949 | int len; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 1950 | const htmlEntityDesc * ent; |
Daniel Veillard | 1032ac4 | 2006-11-23 16:18:30 +0000 | [diff] [blame] | 1951 | const char *cp; |
| 1952 | char nbuf[16]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * Try to lookup a predefined HTML entity for it |
| 1956 | */ |
| 1957 | |
| 1958 | ent = htmlEntityValueLookup(c); |
| 1959 | if (ent == NULL) { |
Daniel Veillard | 1032ac4 | 2006-11-23 16:18:30 +0000 | [diff] [blame] | 1960 | snprintf(nbuf, sizeof(nbuf), "#%u", c); |
| 1961 | cp = nbuf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1962 | } |
Daniel Veillard | 1032ac4 | 2006-11-23 16:18:30 +0000 | [diff] [blame] | 1963 | else |
| 1964 | cp = ent->name; |
| 1965 | len = strlen(cp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | if (out + 2 + len >= outend) |
| 1967 | break; |
| 1968 | *out++ = '&'; |
Daniel Veillard | 1032ac4 | 2006-11-23 16:18:30 +0000 | [diff] [blame] | 1969 | memcpy(out, cp, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1970 | out += len; |
| 1971 | *out++ = ';'; |
| 1972 | } |
| 1973 | processed = in; |
| 1974 | } |
| 1975 | *outlen = out - outstart; |
| 1976 | *inlen = processed - instart; |
| 1977 | return(0); |
| 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * htmlEncodeEntities: |
| 1982 | * @out: a pointer to an array of bytes to store the result |
| 1983 | * @outlen: the length of @out |
| 1984 | * @in: a pointer to an array of UTF-8 chars |
| 1985 | * @inlen: the length of @in |
| 1986 | * @quoteChar: the quote character to escape (' or ") or zero. |
| 1987 | * |
| 1988 | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
| 1989 | * plus HTML entities block of chars out. |
| 1990 | * |
| 1991 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
| 1992 | * The value of @inlen after return is the number of octets consumed |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1993 | * as the return value is positive, else unpredictable. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1994 | * The value of @outlen after return is the number of octets consumed. |
| 1995 | */ |
| 1996 | int |
| 1997 | htmlEncodeEntities(unsigned char* out, int *outlen, |
| 1998 | const unsigned char* in, int *inlen, int quoteChar) { |
| 1999 | const unsigned char* processed = in; |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2000 | const unsigned char* outend; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2001 | const unsigned char* outstart = out; |
| 2002 | const unsigned char* instart = in; |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2003 | const unsigned char* inend; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2004 | unsigned int c, d; |
| 2005 | int trailing; |
| 2006 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 2007 | if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) |
| 2008 | return(-1); |
| 2009 | outend = out + (*outlen); |
| 2010 | inend = in + (*inlen); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2011 | while (in < inend) { |
| 2012 | d = *in++; |
| 2013 | if (d < 0x80) { c= d; trailing= 0; } |
| 2014 | else if (d < 0xC0) { |
| 2015 | /* trailing byte in leading position */ |
| 2016 | *outlen = out - outstart; |
| 2017 | *inlen = processed - instart; |
| 2018 | return(-2); |
| 2019 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 2020 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 2021 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 2022 | else { |
| 2023 | /* no chance for this in Ascii */ |
| 2024 | *outlen = out - outstart; |
| 2025 | *inlen = processed - instart; |
| 2026 | return(-2); |
| 2027 | } |
| 2028 | |
| 2029 | if (inend - in < trailing) |
| 2030 | break; |
| 2031 | |
| 2032 | while (trailing--) { |
| 2033 | if (((d= *in++) & 0xC0) != 0x80) { |
| 2034 | *outlen = out - outstart; |
| 2035 | *inlen = processed - instart; |
| 2036 | return(-2); |
| 2037 | } |
| 2038 | c <<= 6; |
| 2039 | c |= d & 0x3F; |
| 2040 | } |
| 2041 | |
| 2042 | /* assertion: c is a single UTF-4 value */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2043 | if ((c < 0x80) && (c != (unsigned int) quoteChar) && |
| 2044 | (c != '&') && (c != '<') && (c != '>')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | if (out >= outend) |
| 2046 | break; |
| 2047 | *out++ = c; |
| 2048 | } else { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2049 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2050 | const char *cp; |
| 2051 | char nbuf[16]; |
| 2052 | int len; |
| 2053 | |
| 2054 | /* |
| 2055 | * Try to lookup a predefined HTML entity for it |
| 2056 | */ |
| 2057 | ent = htmlEntityValueLookup(c); |
| 2058 | if (ent == NULL) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 2059 | snprintf(nbuf, sizeof(nbuf), "#%u", c); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2060 | cp = nbuf; |
| 2061 | } |
| 2062 | else |
| 2063 | cp = ent->name; |
| 2064 | len = strlen(cp); |
| 2065 | if (out + 2 + len > outend) |
| 2066 | break; |
| 2067 | *out++ = '&'; |
| 2068 | memcpy(out, cp, len); |
| 2069 | out += len; |
| 2070 | *out++ = ';'; |
| 2071 | } |
| 2072 | processed = in; |
| 2073 | } |
| 2074 | *outlen = out - outstart; |
| 2075 | *inlen = processed - instart; |
| 2076 | return(0); |
| 2077 | } |
| 2078 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2079 | /************************************************************************ |
| 2080 | * * |
| 2081 | * Commodity functions to handle streams * |
| 2082 | * * |
| 2083 | ************************************************************************/ |
| 2084 | |
| 2085 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | * htmlNewInputStream: |
| 2087 | * @ctxt: an HTML parser context |
| 2088 | * |
| 2089 | * Create a new input stream structure |
| 2090 | * Returns the new input stream or NULL |
| 2091 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2092 | static htmlParserInputPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2093 | htmlNewInputStream(htmlParserCtxtPtr ctxt) { |
| 2094 | htmlParserInputPtr input; |
| 2095 | |
| 2096 | input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput)); |
| 2097 | if (input == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2098 | htmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2099 | return(NULL); |
| 2100 | } |
| 2101 | memset(input, 0, sizeof(htmlParserInput)); |
| 2102 | input->filename = NULL; |
| 2103 | input->directory = NULL; |
| 2104 | input->base = NULL; |
| 2105 | input->cur = NULL; |
| 2106 | input->buf = NULL; |
| 2107 | input->line = 1; |
| 2108 | input->col = 1; |
| 2109 | input->buf = NULL; |
| 2110 | input->free = NULL; |
| 2111 | input->version = NULL; |
| 2112 | input->consumed = 0; |
| 2113 | input->length = 0; |
| 2114 | return(input); |
| 2115 | } |
| 2116 | |
| 2117 | |
| 2118 | /************************************************************************ |
| 2119 | * * |
| 2120 | * Commodity functions, cleanup needed ? * |
| 2121 | * * |
| 2122 | ************************************************************************/ |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2123 | /* |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2124 | * all tags allowing pc data from the html 4.01 loose dtd |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2125 | * NOTE: it might be more apropriate to integrate this information |
| 2126 | * into the html40ElementTable array but I don't want to risk any |
| 2127 | * binary incomptibility |
| 2128 | */ |
| 2129 | static const char *allowPCData[] = { |
| 2130 | "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big", |
| 2131 | "blockquote", "body", "button", "caption", "center", "cite", "code", |
| 2132 | "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2", |
| 2133 | "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend", |
| 2134 | "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp", |
| 2135 | "small", "span", "strike", "strong", "td", "th", "tt", "u", "var" |
| 2136 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2137 | |
| 2138 | /** |
| 2139 | * areBlanks: |
| 2140 | * @ctxt: an HTML parser context |
| 2141 | * @str: a xmlChar * |
| 2142 | * @len: the size of @str |
| 2143 | * |
| 2144 | * Is this a sequence of blank chars that one can ignore ? |
| 2145 | * |
| 2146 | * Returns 1 if ignorable 0 otherwise. |
| 2147 | */ |
| 2148 | |
| 2149 | static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) { |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2150 | unsigned int i; |
| 2151 | int j; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2152 | xmlNodePtr lastChild; |
Daniel Veillard | 36d7340 | 2005-09-01 09:52:30 +0000 | [diff] [blame] | 2153 | xmlDtdPtr dtd; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2154 | |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2155 | for (j = 0;j < len;j++) |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2156 | if (!(IS_BLANK_CH(str[j]))) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2157 | |
| 2158 | if (CUR == 0) return(1); |
| 2159 | if (CUR != '<') return(0); |
| 2160 | if (ctxt->name == NULL) |
| 2161 | return(1); |
| 2162 | if (xmlStrEqual(ctxt->name, BAD_CAST"html")) |
| 2163 | return(1); |
| 2164 | if (xmlStrEqual(ctxt->name, BAD_CAST"head")) |
| 2165 | return(1); |
Daniel Veillard | 36d7340 | 2005-09-01 09:52:30 +0000 | [diff] [blame] | 2166 | |
| 2167 | /* Only strip CDATA children of the body tag for strict HTML DTDs */ |
| 2168 | if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) { |
| 2169 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 2170 | if (dtd != NULL && dtd->ExternalID != NULL) { |
| 2171 | if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") || |
| 2172 | !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN")) |
| 2173 | return(1); |
| 2174 | } |
| 2175 | } |
| 2176 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2177 | if (ctxt->node == NULL) return(0); |
| 2178 | lastChild = xmlGetLastChild(ctxt->node); |
Daniel Veillard | 18a6509 | 2004-05-11 15:57:42 +0000 | [diff] [blame] | 2179 | while ((lastChild) && (lastChild->type == XML_COMMENT_NODE)) |
| 2180 | lastChild = lastChild->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2181 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2182 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2183 | (ctxt->node->content != NULL)) return(0); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2184 | /* keep ws in constructs like ...<b> </b>... |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2185 | for all tags "b" allowing PCDATA */ |
| 2186 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
| 2187 | if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) { |
| 2188 | return(0); |
| 2189 | } |
| 2190 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2191 | } else if (xmlNodeIsText(lastChild)) { |
| 2192 | return(0); |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2193 | } else { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2194 | /* keep ws in constructs like <p><b>xy</b> <i>z</i><p> |
Daniel Veillard | 8c9872c | 2002-07-05 18:17:10 +0000 | [diff] [blame] | 2195 | for all tags "p" allowing PCDATA */ |
| 2196 | for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) { |
| 2197 | if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) { |
| 2198 | return(0); |
| 2199 | } |
| 2200 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2201 | } |
| 2202 | return(1); |
| 2203 | } |
| 2204 | |
| 2205 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2206 | * htmlNewDocNoDtD: |
| 2207 | * @URI: URI for the dtd, or NULL |
| 2208 | * @ExternalID: the external ID of the DTD, or NULL |
| 2209 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2210 | * Creates a new HTML document without a DTD node if @URI and @ExternalID |
| 2211 | * are NULL |
| 2212 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2213 | * Returns a new document, do not initialize the DTD if not provided |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2214 | */ |
| 2215 | htmlDocPtr |
| 2216 | htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) { |
| 2217 | xmlDocPtr cur; |
| 2218 | |
| 2219 | /* |
| 2220 | * Allocate a new document and fill the fields. |
| 2221 | */ |
| 2222 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
| 2223 | if (cur == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2224 | htmlErrMemory(NULL, "HTML document creation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2225 | return(NULL); |
| 2226 | } |
| 2227 | memset(cur, 0, sizeof(xmlDoc)); |
| 2228 | |
| 2229 | cur->type = XML_HTML_DOCUMENT_NODE; |
| 2230 | cur->version = NULL; |
| 2231 | cur->intSubset = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2232 | cur->doc = cur; |
| 2233 | cur->name = NULL; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2234 | cur->children = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2235 | cur->extSubset = NULL; |
| 2236 | cur->oldNs = NULL; |
| 2237 | cur->encoding = NULL; |
| 2238 | cur->standalone = 1; |
| 2239 | cur->compression = 0; |
| 2240 | cur->ids = NULL; |
| 2241 | cur->refs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2242 | cur->_private = NULL; |
Daniel Veillard | 7cc2357 | 2004-07-29 11:20:30 +0000 | [diff] [blame] | 2243 | cur->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2244 | cur->properties = XML_DOC_HTML | XML_DOC_USERBUILT; |
Daniel Veillard | b6b0fd8 | 2001-10-22 12:31:11 +0000 | [diff] [blame] | 2245 | if ((ExternalID != NULL) || |
| 2246 | (URI != NULL)) |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 2247 | xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2248 | return(cur); |
| 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * htmlNewDoc: |
| 2253 | * @URI: URI for the dtd, or NULL |
| 2254 | * @ExternalID: the external ID of the DTD, or NULL |
| 2255 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2256 | * Creates a new HTML document |
| 2257 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2258 | * Returns a new document |
| 2259 | */ |
| 2260 | htmlDocPtr |
| 2261 | htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) { |
| 2262 | if ((URI == NULL) && (ExternalID == NULL)) |
| 2263 | return(htmlNewDocNoDtD( |
Daniel Veillard | 6426935 | 2001-05-04 17:52:34 +0000 | [diff] [blame] | 2264 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd", |
| 2265 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2266 | |
| 2267 | return(htmlNewDocNoDtD(URI, ExternalID)); |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | /************************************************************************ |
| 2272 | * * |
| 2273 | * The parser itself * |
| 2274 | * Relates to http://www.w3.org/TR/html40 * |
| 2275 | * * |
| 2276 | ************************************************************************/ |
| 2277 | |
| 2278 | /************************************************************************ |
| 2279 | * * |
| 2280 | * The parser itself * |
| 2281 | * * |
| 2282 | ************************************************************************/ |
| 2283 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2284 | static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt); |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2285 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2286 | /** |
| 2287 | * htmlParseHTMLName: |
| 2288 | * @ctxt: an HTML parser context |
| 2289 | * |
| 2290 | * parse an HTML tag or attribute name, note that we convert it to lowercase |
| 2291 | * since HTML names are not case-sensitive. |
| 2292 | * |
| 2293 | * Returns the Tag Name parsed or NULL |
| 2294 | */ |
| 2295 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2296 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2297 | htmlParseHTMLName(htmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | int i = 0; |
| 2299 | xmlChar loc[HTML_PARSER_BUFFER_SIZE]; |
| 2300 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 2301 | if (!IS_ASCII_LETTER(CUR) && (CUR != '_') && |
Daniel Veillard | 7459c59 | 2009-08-13 10:10:29 +0200 | [diff] [blame] | 2302 | (CUR != ':') && (CUR != '.')) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | |
| 2304 | while ((i < HTML_PARSER_BUFFER_SIZE) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 2305 | ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) || |
Daniel Veillard | 7459c59 | 2009-08-13 10:10:29 +0200 | [diff] [blame] | 2306 | (CUR == ':') || (CUR == '-') || (CUR == '_') || |
| 2307 | (CUR == '.'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20; |
| 2309 | else loc[i] = CUR; |
| 2310 | i++; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2311 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2312 | NEXT; |
| 2313 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2314 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2315 | return(xmlDictLookup(ctxt->dict, loc, i)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 2318 | |
| 2319 | /** |
| 2320 | * htmlParseHTMLName_nonInvasive: |
| 2321 | * @ctxt: an HTML parser context |
| 2322 | * |
| 2323 | * parse an HTML tag or attribute name, note that we convert it to lowercase |
| 2324 | * since HTML names are not case-sensitive, this doesn't consume the data |
| 2325 | * from the stream, it's a look-ahead |
| 2326 | * |
| 2327 | * Returns the Tag Name parsed or NULL |
| 2328 | */ |
| 2329 | |
| 2330 | static const xmlChar * |
| 2331 | htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) { |
| 2332 | int i = 0; |
| 2333 | xmlChar loc[HTML_PARSER_BUFFER_SIZE]; |
| 2334 | |
| 2335 | if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') && |
| 2336 | (NXT(1) != ':')) return(NULL); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2337 | |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 2338 | while ((i < HTML_PARSER_BUFFER_SIZE) && |
| 2339 | ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) || |
| 2340 | (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) { |
| 2341 | if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20; |
| 2342 | else loc[i] = NXT(1+i); |
| 2343 | i++; |
| 2344 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2345 | |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 2346 | return(xmlDictLookup(ctxt->dict, loc, i)); |
| 2347 | } |
| 2348 | |
| 2349 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2350 | /** |
| 2351 | * htmlParseName: |
| 2352 | * @ctxt: an HTML parser context |
| 2353 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2354 | * parse an HTML name, this routine is case sensitive. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2355 | * |
| 2356 | * Returns the Name parsed or NULL |
| 2357 | */ |
| 2358 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2359 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | htmlParseName(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2361 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2362 | const xmlChar *ret; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2363 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | |
| 2365 | GROW; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2366 | |
| 2367 | /* |
| 2368 | * Accelerator for simple ASCII names |
| 2369 | */ |
| 2370 | in = ctxt->input->cur; |
| 2371 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2372 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2373 | (*in == '_') || (*in == ':')) { |
| 2374 | in++; |
| 2375 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2376 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2377 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 2378 | (*in == '_') || (*in == '-') || |
| 2379 | (*in == ':') || (*in == '.')) |
| 2380 | in++; |
| 2381 | if ((*in > 0) && (*in < 0x80)) { |
| 2382 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2383 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2384 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2385 | ctxt->nbChars += count; |
| 2386 | ctxt->input->col += count; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2387 | return(ret); |
| 2388 | } |
| 2389 | } |
| 2390 | return(htmlParseNameComplex(ctxt)); |
| 2391 | } |
| 2392 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2393 | static const xmlChar * |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2394 | htmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2395 | int len = 0, l; |
| 2396 | int c; |
| 2397 | int count = 0; |
| 2398 | |
| 2399 | /* |
| 2400 | * Handler for more complex cases |
| 2401 | */ |
| 2402 | GROW; |
| 2403 | c = CUR_CHAR(l); |
| 2404 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 2405 | (!IS_LETTER(c) && (c != '_') && |
| 2406 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2407 | return(NULL); |
| 2408 | } |
| 2409 | |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2410 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 2411 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 2412 | (c == '.') || (c == '-') || |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2413 | (c == '_') || (c == ':') || |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2414 | (IS_COMBINING(c)) || |
| 2415 | (IS_EXTENDER(c)))) { |
| 2416 | if (count++ > 100) { |
| 2417 | count = 0; |
| 2418 | GROW; |
| 2419 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2420 | len += l; |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2421 | NEXTL(l); |
| 2422 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2423 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2424 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Daniel Veillard | e55e8e4 | 2003-01-10 12:50:02 +0000 | [diff] [blame] | 2427 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2428 | /** |
| 2429 | * htmlParseHTMLAttribute: |
| 2430 | * @ctxt: an HTML parser context |
| 2431 | * @stop: a char stop value |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2432 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2433 | * parse an HTML attribute value till the stop (quote), if |
| 2434 | * stop is 0 then it stops at the first space |
| 2435 | * |
| 2436 | * Returns the attribute parsed or NULL |
| 2437 | */ |
| 2438 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2439 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2440 | htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) { |
| 2441 | xmlChar *buffer = NULL; |
| 2442 | int buffer_size = 0; |
| 2443 | xmlChar *out = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2444 | const xmlChar *name = NULL; |
| 2445 | const xmlChar *cur = NULL; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2446 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* |
| 2449 | * allocate a translation buffer. |
| 2450 | */ |
| 2451 | buffer_size = HTML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2452 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2453 | if (buffer == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2454 | htmlErrMemory(ctxt, "buffer allocation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2455 | return(NULL); |
| 2456 | } |
| 2457 | out = buffer; |
| 2458 | |
| 2459 | /* |
| 2460 | * Ok loop until we reach one of the ending chars |
| 2461 | */ |
Daniel Veillard | 957fdcf | 2001-11-06 22:50:19 +0000 | [diff] [blame] | 2462 | while ((CUR != 0) && (CUR != stop)) { |
| 2463 | if ((stop == 0) && (CUR == '>')) break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2464 | if ((stop == 0) && (IS_BLANK_CH(CUR))) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2465 | if (CUR == '&') { |
| 2466 | if (NXT(1) == '#') { |
| 2467 | unsigned int c; |
| 2468 | int bits; |
| 2469 | |
| 2470 | c = htmlParseCharRef(ctxt); |
| 2471 | if (c < 0x80) |
| 2472 | { *out++ = c; bits= -6; } |
| 2473 | else if (c < 0x800) |
| 2474 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2475 | else if (c < 0x10000) |
| 2476 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2477 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2478 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2479 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2480 | for ( ; bits >= 0; bits-= 6) { |
| 2481 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2482 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2483 | |
Daniel Veillard | ce02dbc | 2002-10-22 19:14:58 +0000 | [diff] [blame] | 2484 | if (out - buffer > buffer_size - 100) { |
| 2485 | int indx = out - buffer; |
| 2486 | |
| 2487 | growBuffer(buffer); |
| 2488 | out = &buffer[indx]; |
| 2489 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | } else { |
| 2491 | ent = htmlParseEntityRef(ctxt, &name); |
| 2492 | if (name == NULL) { |
| 2493 | *out++ = '&'; |
| 2494 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2495 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2496 | |
| 2497 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2498 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } else if (ent == NULL) { |
| 2501 | *out++ = '&'; |
| 2502 | cur = name; |
| 2503 | while (*cur != 0) { |
| 2504 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2505 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2506 | |
| 2507 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2508 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | } |
| 2510 | *out++ = *cur++; |
| 2511 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | } else { |
| 2513 | unsigned int c; |
| 2514 | int bits; |
| 2515 | |
| 2516 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2517 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2518 | |
| 2519 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2520 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | } |
Daniel Veillard | 4851909 | 2006-10-17 15:56:35 +0000 | [diff] [blame] | 2522 | c = ent->value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2523 | if (c < 0x80) |
| 2524 | { *out++ = c; bits= -6; } |
| 2525 | else if (c < 0x800) |
| 2526 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2527 | else if (c < 0x10000) |
| 2528 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2529 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2530 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2531 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2532 | for ( ; bits >= 0; bits-= 6) { |
| 2533 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2534 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | } else { |
| 2538 | unsigned int c; |
| 2539 | int bits, l; |
| 2540 | |
| 2541 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2542 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2543 | |
| 2544 | growBuffer(buffer); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2545 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2546 | } |
| 2547 | c = CUR_CHAR(l); |
| 2548 | if (c < 0x80) |
| 2549 | { *out++ = c; bits= -6; } |
| 2550 | else if (c < 0x800) |
| 2551 | { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 2552 | else if (c < 0x10000) |
| 2553 | { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2554 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2555 | { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2556 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2557 | for ( ; bits >= 0; bits-= 6) { |
| 2558 | *out++ = ((c >> bits) & 0x3F) | 0x80; |
| 2559 | } |
| 2560 | NEXT; |
| 2561 | } |
| 2562 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2563 | *out = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2564 | return(buffer); |
| 2565 | } |
| 2566 | |
| 2567 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2568 | * htmlParseEntityRef: |
| 2569 | * @ctxt: an HTML parser context |
| 2570 | * @str: location to store the entity name |
| 2571 | * |
| 2572 | * parse an HTML ENTITY references |
| 2573 | * |
| 2574 | * [68] EntityRef ::= '&' Name ';' |
| 2575 | * |
| 2576 | * Returns the associated htmlEntityDescPtr if found, or NULL otherwise, |
| 2577 | * if non-NULL *str will have to be freed by the caller. |
| 2578 | */ |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2579 | const htmlEntityDesc * |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2580 | htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2581 | const xmlChar *name; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 2582 | const htmlEntityDesc * ent = NULL; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2583 | |
| 2584 | if (str != NULL) *str = NULL; |
| 2585 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2586 | |
| 2587 | if (CUR == '&') { |
| 2588 | NEXT; |
| 2589 | name = htmlParseName(ctxt); |
| 2590 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2591 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 2592 | "htmlParseEntityRef: no name\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2593 | } else { |
| 2594 | GROW; |
| 2595 | if (CUR == ';') { |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2596 | if (str != NULL) |
| 2597 | *str = name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2598 | |
| 2599 | /* |
| 2600 | * Lookup the entity in the table. |
| 2601 | */ |
| 2602 | ent = htmlEntityLookup(name); |
| 2603 | if (ent != NULL) /* OK that's ugly !!! */ |
| 2604 | NEXT; |
| 2605 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2606 | htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, |
| 2607 | "htmlParseEntityRef: expecting ';'\n", |
| 2608 | NULL, NULL); |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 2609 | if (str != NULL) |
| 2610 | *str = name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | } |
| 2614 | return(ent); |
| 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * htmlParseAttValue: |
| 2619 | * @ctxt: an HTML parser context |
| 2620 | * |
| 2621 | * parse a value for an attribute |
| 2622 | * Note: the parser won't do substitution of entities here, this |
| 2623 | * will be handled later in xmlStringGetNodeList, unless it was |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2624 | * asked for ctxt->replaceEntities != 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2625 | * |
| 2626 | * Returns the AttValue parsed or NULL. |
| 2627 | */ |
| 2628 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2629 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2630 | htmlParseAttValue(htmlParserCtxtPtr ctxt) { |
| 2631 | xmlChar *ret = NULL; |
| 2632 | |
| 2633 | if (CUR == '"') { |
| 2634 | NEXT; |
| 2635 | ret = htmlParseHTMLAttribute(ctxt, '"'); |
| 2636 | if (CUR != '"') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2637 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2638 | "AttValue: \" expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 | } else |
| 2640 | NEXT; |
| 2641 | } else if (CUR == '\'') { |
| 2642 | NEXT; |
| 2643 | ret = htmlParseHTMLAttribute(ctxt, '\''); |
| 2644 | if (CUR != '\'') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2645 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2646 | "AttValue: ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2647 | } else |
| 2648 | NEXT; |
| 2649 | } else { |
| 2650 | /* |
| 2651 | * That's an HTMLism, the attribute value may not be quoted |
| 2652 | */ |
| 2653 | ret = htmlParseHTMLAttribute(ctxt, 0); |
| 2654 | if (ret == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2655 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 2656 | "AttValue: no value found\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | return(ret); |
| 2660 | } |
| 2661 | |
| 2662 | /** |
| 2663 | * htmlParseSystemLiteral: |
| 2664 | * @ctxt: an HTML parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2665 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2666 | * parse an HTML Literal |
| 2667 | * |
| 2668 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 2669 | * |
| 2670 | * Returns the SystemLiteral parsed or NULL |
| 2671 | */ |
| 2672 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2673 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) { |
| 2675 | const xmlChar *q; |
| 2676 | xmlChar *ret = NULL; |
| 2677 | |
| 2678 | if (CUR == '"') { |
| 2679 | NEXT; |
| 2680 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2681 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2682 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2683 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2684 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2685 | "Unfinished SystemLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2686 | } else { |
| 2687 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2688 | NEXT; |
| 2689 | } |
| 2690 | } else if (CUR == '\'') { |
| 2691 | NEXT; |
| 2692 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2693 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2694 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2695 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2696 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2697 | "Unfinished SystemLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2698 | } else { |
| 2699 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2700 | NEXT; |
| 2701 | } |
| 2702 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2703 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
| 2704 | " or ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2706 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2707 | return(ret); |
| 2708 | } |
| 2709 | |
| 2710 | /** |
| 2711 | * htmlParsePubidLiteral: |
| 2712 | * @ctxt: an HTML parser context |
| 2713 | * |
| 2714 | * parse an HTML public literal |
| 2715 | * |
| 2716 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 2717 | * |
| 2718 | * Returns the PubidLiteral parsed or NULL. |
| 2719 | */ |
| 2720 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2721 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2722 | htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) { |
| 2723 | const xmlChar *q; |
| 2724 | xmlChar *ret = NULL; |
| 2725 | /* |
| 2726 | * Name ::= (Letter | '_') (NameChar)* |
| 2727 | */ |
| 2728 | if (CUR == '"') { |
| 2729 | NEXT; |
| 2730 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2731 | while (IS_PUBIDCHAR_CH(CUR)) NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2732 | if (CUR != '"') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2733 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2734 | "Unfinished PubidLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2735 | } else { |
| 2736 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2737 | NEXT; |
| 2738 | } |
| 2739 | } else if (CUR == '\'') { |
| 2740 | NEXT; |
| 2741 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2742 | while ((IS_PUBIDCHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2743 | NEXT; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 2744 | if (CUR != '\'') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2745 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, |
| 2746 | "Unfinished PubidLiteral\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | } else { |
| 2748 | ret = xmlStrndup(q, CUR_PTR - q); |
| 2749 | NEXT; |
| 2750 | } |
| 2751 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2752 | htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, |
| 2753 | "PubidLiteral \" or ' expected\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2754 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2755 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2756 | return(ret); |
| 2757 | } |
| 2758 | |
| 2759 | /** |
| 2760 | * htmlParseScript: |
| 2761 | * @ctxt: an HTML parser context |
| 2762 | * |
| 2763 | * parse the content of an HTML SCRIPT or STYLE element |
| 2764 | * http://www.w3.org/TR/html4/sgml/dtd.html#Script |
| 2765 | * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet |
| 2766 | * http://www.w3.org/TR/html4/types.html#type-script |
| 2767 | * http://www.w3.org/TR/html4/types.html#h-6.15 |
| 2768 | * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1 |
| 2769 | * |
| 2770 | * Script data ( %Script; in the DTD) can be the content of the SCRIPT |
| 2771 | * element and the value of intrinsic event attributes. User agents must |
| 2772 | * not evaluate script data as HTML markup but instead must pass it on as |
| 2773 | * data to a script engine. |
| 2774 | * NOTES: |
| 2775 | * - The content is passed like CDATA |
| 2776 | * - the attributes for style and scripting "onXXX" are also described |
| 2777 | * as CDATA but SGML allows entities references in attributes so their |
| 2778 | * processing is identical as other attributes |
| 2779 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2780 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2781 | htmlParseScript(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 7d2b323 | 2005-07-14 08:57:39 +0000 | [diff] [blame] | 2782 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | int nbchar = 0; |
Daniel Veillard | 358fef4 | 2005-07-13 16:37:38 +0000 | [diff] [blame] | 2784 | int cur,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2785 | |
| 2786 | SHRINK; |
Daniel Veillard | 358fef4 | 2005-07-13 16:37:38 +0000 | [diff] [blame] | 2787 | cur = CUR_CHAR(l); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2788 | while (IS_CHAR_CH(cur)) { |
Daniel Veillard | 4272024 | 2007-04-16 07:02:31 +0000 | [diff] [blame] | 2789 | if ((cur == '<') && (NXT(1) == '/')) { |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 2790 | /* |
| 2791 | * One should break here, the specification is clear: |
| 2792 | * Authors should therefore escape "</" within the content. |
| 2793 | * Escape mechanisms are specific to each scripting or |
| 2794 | * style sheet language. |
| 2795 | * |
| 2796 | * In recovery mode, only break if end tag match the |
| 2797 | * current tag, effectively ignoring all tags inside the |
| 2798 | * script/style block and treating the entire block as |
| 2799 | * CDATA. |
| 2800 | */ |
| 2801 | if (ctxt->recovery) { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2802 | if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2, |
| 2803 | xmlStrlen(ctxt->name)) == 0) |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 2804 | { |
| 2805 | break; /* while */ |
| 2806 | } else { |
| 2807 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 2cf36a1 | 2005-10-25 12:21:29 +0000 | [diff] [blame] | 2808 | "Element %s embeds close tag\n", |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 2809 | ctxt->name, NULL); |
| 2810 | } |
| 2811 | } else { |
| 2812 | if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) || |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2813 | ((NXT(2) >= 'a') && (NXT(2) <= 'z'))) |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 2814 | { |
| 2815 | break; /* while */ |
| 2816 | } |
| 2817 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2818 | } |
Daniel Veillard | 358fef4 | 2005-07-13 16:37:38 +0000 | [diff] [blame] | 2819 | COPY_BUF(l,buf,nbchar,cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2820 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
| 2821 | if (ctxt->sax->cdataBlock!= NULL) { |
| 2822 | /* |
| 2823 | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
| 2824 | */ |
| 2825 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 2826 | } else if (ctxt->sax->characters != NULL) { |
| 2827 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2828 | } |
| 2829 | nbchar = 0; |
| 2830 | } |
Daniel Veillard | b990008 | 2005-10-25 12:36:29 +0000 | [diff] [blame] | 2831 | GROW; |
Daniel Veillard | 358fef4 | 2005-07-13 16:37:38 +0000 | [diff] [blame] | 2832 | NEXTL(l); |
| 2833 | cur = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2834 | } |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 2835 | |
Daniel Veillard | 68716a7 | 2006-10-16 09:32:17 +0000 | [diff] [blame] | 2836 | if ((!(IS_CHAR_CH(cur))) && (!((cur == 0) && (ctxt->progressive)))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2837 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2838 | "Invalid char in CDATA 0x%X\n", cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2839 | NEXT; |
| 2840 | } |
| 2841 | |
| 2842 | if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2843 | if (ctxt->sax->cdataBlock!= NULL) { |
| 2844 | /* |
| 2845 | * Insert as CDATA, which is the same as HTML_PRESERVE_NODE |
| 2846 | */ |
| 2847 | ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 2848 | } else if (ctxt->sax->characters != NULL) { |
| 2849 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | |
| 2855 | /** |
| 2856 | * htmlParseCharData: |
| 2857 | * @ctxt: an HTML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2858 | * |
| 2859 | * parse a CharData section. |
| 2860 | * if we are within a CDATA section ']]>' marks an end of section. |
| 2861 | * |
| 2862 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 2863 | */ |
| 2864 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2865 | static void |
| 2866 | htmlParseCharData(htmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2867 | xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 2868 | int nbchar = 0; |
| 2869 | int cur, l; |
Daniel Veillard | a57ba4c | 2008-09-25 16:06:18 +0000 | [diff] [blame] | 2870 | int chunk = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2871 | |
| 2872 | SHRINK; |
| 2873 | cur = CUR_CHAR(l); |
| 2874 | while (((cur != '<') || (ctxt->token == '<')) && |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 2875 | ((cur != '&') || (ctxt->token == '&')) && |
Daniel Veillard | c5b43cc | 2008-01-11 07:41:39 +0000 | [diff] [blame] | 2876 | (cur != 0)) { |
| 2877 | if (!(IS_CHAR(cur))) { |
| 2878 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2879 | "Invalid char in CDATA 0x%X\n", cur); |
| 2880 | } else { |
| 2881 | COPY_BUF(l,buf,nbchar,cur); |
| 2882 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2883 | if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) { |
| 2884 | /* |
| 2885 | * Ok the segment is to be consumed as chars. |
| 2886 | */ |
| 2887 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2888 | if (areBlanks(ctxt, buf, nbchar)) { |
| 2889 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 2890 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 2891 | buf, nbchar); |
| 2892 | } else { |
| 2893 | htmlCheckParagraph(ctxt); |
| 2894 | if (ctxt->sax->characters != NULL) |
| 2895 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 2896 | } |
| 2897 | } |
| 2898 | nbchar = 0; |
| 2899 | } |
| 2900 | NEXTL(l); |
Daniel Veillard | a57ba4c | 2008-09-25 16:06:18 +0000 | [diff] [blame] | 2901 | chunk++; |
| 2902 | if (chunk > HTML_PARSER_BUFFER_SIZE) { |
| 2903 | chunk = 0; |
| 2904 | SHRINK; |
| 2905 | GROW; |
| 2906 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2907 | cur = CUR_CHAR(l); |
Daniel Veillard | 358a989 | 2003-02-04 15:22:32 +0000 | [diff] [blame] | 2908 | if (cur == 0) { |
| 2909 | SHRINK; |
| 2910 | GROW; |
| 2911 | cur = CUR_CHAR(l); |
| 2912 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2913 | } |
| 2914 | if (nbchar != 0) { |
Daniel Veillard | d2755a8 | 2005-08-07 23:42:39 +0000 | [diff] [blame] | 2915 | buf[nbchar] = 0; |
| 2916 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2917 | /* |
| 2918 | * Ok the segment is to be consumed as chars. |
| 2919 | */ |
| 2920 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 2921 | if (areBlanks(ctxt, buf, nbchar)) { |
| 2922 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 2923 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 2924 | } else { |
| 2925 | htmlCheckParagraph(ctxt); |
| 2926 | if (ctxt->sax->characters != NULL) |
| 2927 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 2928 | } |
| 2929 | } |
Daniel Veillard | 7cc95c0 | 2001-10-17 15:45:12 +0000 | [diff] [blame] | 2930 | } else { |
| 2931 | /* |
| 2932 | * Loop detection |
| 2933 | */ |
| 2934 | if (cur == 0) |
| 2935 | ctxt->instate = XML_PARSER_EOF; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | /** |
| 2940 | * htmlParseExternalID: |
| 2941 | * @ctxt: an HTML parser context |
| 2942 | * @publicID: a xmlChar** receiving PubidLiteral |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2943 | * |
| 2944 | * Parse an External ID or a Public ID |
| 2945 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2946 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 2947 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 2948 | * |
| 2949 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 2950 | * |
| 2951 | * Returns the function returns SystemLiteral and in the second |
| 2952 | * case publicID receives PubidLiteral, is strict is off |
| 2953 | * it is possible to return NULL and have publicID set. |
| 2954 | */ |
| 2955 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2956 | static xmlChar * |
| 2957 | htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2958 | xmlChar *URI = NULL; |
| 2959 | |
| 2960 | if ((UPPER == 'S') && (UPP(1) == 'Y') && |
| 2961 | (UPP(2) == 'S') && (UPP(3) == 'T') && |
| 2962 | (UPP(4) == 'E') && (UPP(5) == 'M')) { |
| 2963 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2964 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2965 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 2966 | "Space required after 'SYSTEM'\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2967 | } |
| 2968 | SKIP_BLANKS; |
| 2969 | URI = htmlParseSystemLiteral(ctxt); |
| 2970 | if (URI == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2971 | htmlParseErr(ctxt, XML_ERR_URI_REQUIRED, |
| 2972 | "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2973 | } |
| 2974 | } else if ((UPPER == 'P') && (UPP(1) == 'U') && |
| 2975 | (UPP(2) == 'B') && (UPP(3) == 'L') && |
| 2976 | (UPP(4) == 'I') && (UPP(5) == 'C')) { |
| 2977 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2978 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2979 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 2980 | "Space required after 'PUBLIC'\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2981 | } |
| 2982 | SKIP_BLANKS; |
| 2983 | *publicID = htmlParsePubidLiteral(ctxt); |
| 2984 | if (*publicID == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2985 | htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED, |
| 2986 | "htmlParseExternalID: PUBLIC, no Public Identifier\n", |
| 2987 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2988 | } |
| 2989 | SKIP_BLANKS; |
| 2990 | if ((CUR == '"') || (CUR == '\'')) { |
| 2991 | URI = htmlParseSystemLiteral(ctxt); |
| 2992 | } |
| 2993 | } |
| 2994 | return(URI); |
| 2995 | } |
| 2996 | |
| 2997 | /** |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 2998 | * xmlParsePI: |
| 2999 | * @ctxt: an XML parser context |
| 3000 | * |
| 3001 | * parse an XML Processing Instruction. |
| 3002 | * |
| 3003 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 3004 | */ |
| 3005 | static void |
| 3006 | htmlParsePI(htmlParserCtxtPtr ctxt) { |
| 3007 | xmlChar *buf = NULL; |
| 3008 | int len = 0; |
| 3009 | int size = HTML_PARSER_BUFFER_SIZE; |
| 3010 | int cur, l; |
| 3011 | const xmlChar *target; |
| 3012 | xmlParserInputState state; |
| 3013 | int count = 0; |
| 3014 | |
| 3015 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 3016 | state = ctxt->instate; |
| 3017 | ctxt->instate = XML_PARSER_PI; |
| 3018 | /* |
| 3019 | * this is a Processing Instruction. |
| 3020 | */ |
| 3021 | SKIP(2); |
| 3022 | SHRINK; |
| 3023 | |
| 3024 | /* |
| 3025 | * Parse the target name and check for special support like |
| 3026 | * namespace. |
| 3027 | */ |
| 3028 | target = htmlParseName(ctxt); |
| 3029 | if (target != NULL) { |
| 3030 | if (RAW == '>') { |
| 3031 | SKIP(1); |
| 3032 | |
| 3033 | /* |
| 3034 | * SAX: PI detected. |
| 3035 | */ |
| 3036 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3037 | (ctxt->sax->processingInstruction != NULL)) |
| 3038 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3039 | target, NULL); |
| 3040 | ctxt->instate = state; |
| 3041 | return; |
| 3042 | } |
| 3043 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 3044 | if (buf == NULL) { |
| 3045 | htmlErrMemory(ctxt, NULL); |
| 3046 | ctxt->instate = state; |
| 3047 | return; |
| 3048 | } |
| 3049 | cur = CUR; |
| 3050 | if (!IS_BLANK(cur)) { |
| 3051 | htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3052 | "ParsePI: PI %s space expected\n", target, NULL); |
| 3053 | } |
| 3054 | SKIP_BLANKS; |
| 3055 | cur = CUR_CHAR(l); |
| 3056 | while (IS_CHAR(cur) && (cur != '>')) { |
| 3057 | if (len + 5 >= size) { |
| 3058 | xmlChar *tmp; |
| 3059 | |
| 3060 | size *= 2; |
| 3061 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3062 | if (tmp == NULL) { |
| 3063 | htmlErrMemory(ctxt, NULL); |
| 3064 | xmlFree(buf); |
| 3065 | ctxt->instate = state; |
| 3066 | return; |
| 3067 | } |
| 3068 | buf = tmp; |
| 3069 | } |
| 3070 | count++; |
| 3071 | if (count > 50) { |
| 3072 | GROW; |
| 3073 | count = 0; |
| 3074 | } |
| 3075 | COPY_BUF(l,buf,len,cur); |
| 3076 | NEXTL(l); |
| 3077 | cur = CUR_CHAR(l); |
| 3078 | if (cur == 0) { |
| 3079 | SHRINK; |
| 3080 | GROW; |
| 3081 | cur = CUR_CHAR(l); |
| 3082 | } |
| 3083 | } |
| 3084 | buf[len] = 0; |
| 3085 | if (cur != '>') { |
| 3086 | htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 3087 | "ParsePI: PI %s never end ...\n", target, NULL); |
| 3088 | } else { |
| 3089 | SKIP(1); |
| 3090 | |
| 3091 | /* |
| 3092 | * SAX: PI detected. |
| 3093 | */ |
| 3094 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3095 | (ctxt->sax->processingInstruction != NULL)) |
| 3096 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3097 | target, buf); |
| 3098 | } |
| 3099 | xmlFree(buf); |
| 3100 | } else { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3101 | htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED, |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 3102 | "PI is not started correctly", NULL, NULL); |
| 3103 | } |
| 3104 | ctxt->instate = state; |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3109 | * htmlParseComment: |
| 3110 | * @ctxt: an HTML parser context |
| 3111 | * |
| 3112 | * Parse an XML (SGML) comment <!-- .... --> |
| 3113 | * |
| 3114 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 3115 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3116 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3117 | htmlParseComment(htmlParserCtxtPtr ctxt) { |
| 3118 | xmlChar *buf = NULL; |
| 3119 | int len; |
| 3120 | int size = HTML_PARSER_BUFFER_SIZE; |
| 3121 | int q, ql; |
| 3122 | int r, rl; |
| 3123 | int cur, l; |
| 3124 | xmlParserInputState state; |
| 3125 | |
| 3126 | /* |
| 3127 | * Check that there is a comment right here. |
| 3128 | */ |
| 3129 | if ((RAW != '<') || (NXT(1) != '!') || |
| 3130 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
| 3131 | |
| 3132 | state = ctxt->instate; |
| 3133 | ctxt->instate = XML_PARSER_COMMENT; |
| 3134 | SHRINK; |
| 3135 | SKIP(4); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3136 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3137 | if (buf == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3138 | htmlErrMemory(ctxt, "buffer allocation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3139 | ctxt->instate = state; |
| 3140 | return; |
| 3141 | } |
| 3142 | q = CUR_CHAR(ql); |
| 3143 | NEXTL(ql); |
| 3144 | r = CUR_CHAR(rl); |
| 3145 | NEXTL(rl); |
| 3146 | cur = CUR_CHAR(l); |
| 3147 | len = 0; |
| 3148 | while (IS_CHAR(cur) && |
| 3149 | ((cur != '>') || |
| 3150 | (r != '-') || (q != '-'))) { |
| 3151 | if (len + 5 >= size) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3152 | xmlChar *tmp; |
| 3153 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3154 | size *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3155 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3156 | if (tmp == NULL) { |
| 3157 | xmlFree(buf); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3158 | htmlErrMemory(ctxt, "growing buffer failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3159 | ctxt->instate = state; |
| 3160 | return; |
| 3161 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3162 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3163 | } |
| 3164 | COPY_BUF(ql,buf,len,q); |
| 3165 | q = r; |
| 3166 | ql = rl; |
| 3167 | r = cur; |
| 3168 | rl = l; |
| 3169 | NEXTL(l); |
| 3170 | cur = CUR_CHAR(l); |
| 3171 | if (cur == 0) { |
| 3172 | SHRINK; |
| 3173 | GROW; |
| 3174 | cur = CUR_CHAR(l); |
| 3175 | } |
| 3176 | } |
| 3177 | buf[len] = 0; |
| 3178 | if (!IS_CHAR(cur)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3179 | htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3180 | "Comment not terminated \n<!--%.50s\n", buf, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3181 | xmlFree(buf); |
| 3182 | } else { |
| 3183 | NEXT; |
| 3184 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3185 | (!ctxt->disableSAX)) |
| 3186 | ctxt->sax->comment(ctxt->userData, buf); |
| 3187 | xmlFree(buf); |
| 3188 | } |
| 3189 | ctxt->instate = state; |
| 3190 | } |
| 3191 | |
| 3192 | /** |
| 3193 | * htmlParseCharRef: |
| 3194 | * @ctxt: an HTML parser context |
| 3195 | * |
| 3196 | * parse Reference declarations |
| 3197 | * |
| 3198 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 3199 | * '&#x' [0-9a-fA-F]+ ';' |
| 3200 | * |
| 3201 | * Returns the value parsed (as an int) |
| 3202 | */ |
| 3203 | int |
| 3204 | htmlParseCharRef(htmlParserCtxtPtr ctxt) { |
| 3205 | int val = 0; |
| 3206 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3207 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3208 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3209 | "htmlParseCharRef: context error\n", |
| 3210 | NULL, NULL); |
| 3211 | return(0); |
| 3212 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3213 | if ((CUR == '&') && (NXT(1) == '#') && |
Daniel Veillard | c59d826 | 2003-11-20 21:59:12 +0000 | [diff] [blame] | 3214 | ((NXT(2) == 'x') || NXT(2) == 'X')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3215 | SKIP(3); |
| 3216 | while (CUR != ';') { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3217 | if ((CUR >= '0') && (CUR <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3218 | val = val * 16 + (CUR - '0'); |
| 3219 | else if ((CUR >= 'a') && (CUR <= 'f')) |
| 3220 | val = val * 16 + (CUR - 'a') + 10; |
| 3221 | else if ((CUR >= 'A') && (CUR <= 'F')) |
| 3222 | val = val * 16 + (CUR - 'A') + 10; |
| 3223 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3224 | htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, |
Daniel Veillard | 36de63e | 2008-04-03 09:05:05 +0000 | [diff] [blame] | 3225 | "htmlParseCharRef: missing semicolumn\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3226 | NULL, NULL); |
Daniel Veillard | 36de63e | 2008-04-03 09:05:05 +0000 | [diff] [blame] | 3227 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3228 | } |
| 3229 | NEXT; |
| 3230 | } |
| 3231 | if (CUR == ';') |
| 3232 | NEXT; |
| 3233 | } else if ((CUR == '&') && (NXT(1) == '#')) { |
| 3234 | SKIP(2); |
| 3235 | while (CUR != ';') { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3236 | if ((CUR >= '0') && (CUR <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3237 | val = val * 10 + (CUR - '0'); |
| 3238 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3239 | htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, |
Daniel Veillard | 36de63e | 2008-04-03 09:05:05 +0000 | [diff] [blame] | 3240 | "htmlParseCharRef: missing semicolumn\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3241 | NULL, NULL); |
Daniel Veillard | 36de63e | 2008-04-03 09:05:05 +0000 | [diff] [blame] | 3242 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3243 | } |
| 3244 | NEXT; |
| 3245 | } |
| 3246 | if (CUR == ';') |
| 3247 | NEXT; |
| 3248 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3249 | htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF, |
| 3250 | "htmlParseCharRef: invalid value\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3251 | } |
| 3252 | /* |
| 3253 | * Check the value IS_CHAR ... |
| 3254 | */ |
| 3255 | if (IS_CHAR(val)) { |
| 3256 | return(val); |
| 3257 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3258 | htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR, |
| 3259 | "htmlParseCharRef: invalid xmlChar value %d\n", |
| 3260 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3261 | } |
| 3262 | return(0); |
| 3263 | } |
| 3264 | |
| 3265 | |
| 3266 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3267 | * htmlParseDocTypeDecl: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3268 | * @ctxt: an HTML parser context |
| 3269 | * |
| 3270 | * parse a DOCTYPE declaration |
| 3271 | * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3272 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3273 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 3274 | */ |
| 3275 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3276 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3277 | htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3278 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3279 | xmlChar *ExternalID = NULL; |
| 3280 | xmlChar *URI = NULL; |
| 3281 | |
| 3282 | /* |
| 3283 | * We know that '<!DOCTYPE' has been detected. |
| 3284 | */ |
| 3285 | SKIP(9); |
| 3286 | |
| 3287 | SKIP_BLANKS; |
| 3288 | |
| 3289 | /* |
| 3290 | * Parse the DOCTYPE name. |
| 3291 | */ |
| 3292 | name = htmlParseName(ctxt); |
| 3293 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3294 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3295 | "htmlParseDocTypeDecl : no DOCTYPE name !\n", |
| 3296 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3297 | } |
| 3298 | /* |
| 3299 | * Check that upper(name) == "HTML" !!!!!!!!!!!!! |
| 3300 | */ |
| 3301 | |
| 3302 | SKIP_BLANKS; |
| 3303 | |
| 3304 | /* |
| 3305 | * Check for SystemID and ExternalID |
| 3306 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3307 | URI = htmlParseExternalID(ctxt, &ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3308 | SKIP_BLANKS; |
| 3309 | |
| 3310 | /* |
| 3311 | * We should be at the end of the DOCTYPE declaration. |
| 3312 | */ |
| 3313 | if (CUR != '>') { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3314 | htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, |
| 3315 | "DOCTYPE improperly terminated\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3316 | /* We shouldn't try to resynchronize ... */ |
| 3317 | } |
| 3318 | NEXT; |
| 3319 | |
| 3320 | /* |
| 3321 | * Create or update the document accordingly to the DOCTYPE |
| 3322 | */ |
| 3323 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 3324 | (!ctxt->disableSAX)) |
| 3325 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 3326 | |
| 3327 | /* |
| 3328 | * Cleanup, since we don't use all those identifiers |
| 3329 | */ |
| 3330 | if (URI != NULL) xmlFree(URI); |
| 3331 | if (ExternalID != NULL) xmlFree(ExternalID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | /** |
| 3335 | * htmlParseAttribute: |
| 3336 | * @ctxt: an HTML parser context |
| 3337 | * @value: a xmlChar ** used to store the value of the attribute |
| 3338 | * |
| 3339 | * parse an attribute |
| 3340 | * |
| 3341 | * [41] Attribute ::= Name Eq AttValue |
| 3342 | * |
| 3343 | * [25] Eq ::= S? '=' S? |
| 3344 | * |
| 3345 | * With namespace: |
| 3346 | * |
| 3347 | * [NS 11] Attribute ::= QName Eq AttValue |
| 3348 | * |
| 3349 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 3350 | * definition. |
| 3351 | * |
| 3352 | * Returns the attribute name, and the value in *value. |
| 3353 | */ |
| 3354 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3355 | static const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3356 | htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3357 | const xmlChar *name; |
| 3358 | xmlChar *val = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3359 | |
| 3360 | *value = NULL; |
| 3361 | name = htmlParseHTMLName(ctxt); |
| 3362 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3363 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3364 | "error parsing attribute name\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3365 | return(NULL); |
| 3366 | } |
| 3367 | |
| 3368 | /* |
| 3369 | * read the value |
| 3370 | */ |
| 3371 | SKIP_BLANKS; |
| 3372 | if (CUR == '=') { |
| 3373 | NEXT; |
| 3374 | SKIP_BLANKS; |
| 3375 | val = htmlParseAttValue(ctxt); |
Daniel Veillard | c47d263 | 2006-10-17 16:13:27 +0000 | [diff] [blame] | 3376 | } else if (htmlIsBooleanAttr(name)) { |
| 3377 | /* |
| 3378 | * assume a minimized attribute |
| 3379 | */ |
| 3380 | val = xmlStrdup(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | *value = val; |
| 3384 | return(name); |
| 3385 | } |
| 3386 | |
| 3387 | /** |
| 3388 | * htmlCheckEncoding: |
| 3389 | * @ctxt: an HTML parser context |
| 3390 | * @attvalue: the attribute value |
| 3391 | * |
| 3392 | * Checks an http-equiv attribute from a Meta tag to detect |
| 3393 | * the encoding |
| 3394 | * If a new encoding is detected the parser is switched to decode |
| 3395 | * it and pass UTF8 |
| 3396 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3397 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3398 | htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { |
| 3399 | const xmlChar *encoding; |
| 3400 | |
| 3401 | if ((ctxt == NULL) || (attvalue == NULL)) |
| 3402 | return; |
| 3403 | |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3404 | /* do not change encoding */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3405 | if (ctxt->input->encoding != NULL) |
| 3406 | return; |
| 3407 | |
| 3408 | encoding = xmlStrcasestr(attvalue, BAD_CAST"charset="); |
| 3409 | if (encoding != NULL) { |
| 3410 | encoding += 8; |
| 3411 | } else { |
| 3412 | encoding = xmlStrcasestr(attvalue, BAD_CAST"charset ="); |
| 3413 | if (encoding != NULL) |
| 3414 | encoding += 9; |
| 3415 | } |
| 3416 | if (encoding != NULL) { |
| 3417 | xmlCharEncoding enc; |
| 3418 | xmlCharEncodingHandlerPtr handler; |
| 3419 | |
| 3420 | while ((*encoding == ' ') || (*encoding == '\t')) encoding++; |
| 3421 | |
| 3422 | if (ctxt->input->encoding != NULL) |
| 3423 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 3424 | ctxt->input->encoding = xmlStrdup(encoding); |
| 3425 | |
| 3426 | enc = xmlParseCharEncoding((const char *) encoding); |
| 3427 | /* |
| 3428 | * registered set of known encodings |
| 3429 | */ |
| 3430 | if (enc != XML_CHAR_ENCODING_ERROR) { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3431 | if (((enc == XML_CHAR_ENCODING_UTF16LE) || |
Daniel Veillard | 7e30356 | 2006-10-16 13:14:55 +0000 | [diff] [blame] | 3432 | (enc == XML_CHAR_ENCODING_UTF16BE) || |
| 3433 | (enc == XML_CHAR_ENCODING_UCS4LE) || |
| 3434 | (enc == XML_CHAR_ENCODING_UCS4BE)) && |
| 3435 | (ctxt->input->buf != NULL) && |
| 3436 | (ctxt->input->buf->encoder == NULL)) { |
| 3437 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 3438 | "htmlCheckEncoding: wrong encoding meta\n", |
| 3439 | NULL, NULL); |
| 3440 | } else { |
| 3441 | xmlSwitchEncoding(ctxt, enc); |
| 3442 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3443 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 3444 | } else { |
| 3445 | /* |
| 3446 | * fallback for unknown encodings |
| 3447 | */ |
| 3448 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 3449 | if (handler != NULL) { |
| 3450 | xmlSwitchToEncoding(ctxt, handler); |
| 3451 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 3452 | } else { |
| 3453 | ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING; |
| 3454 | } |
| 3455 | } |
| 3456 | |
| 3457 | if ((ctxt->input->buf != NULL) && |
| 3458 | (ctxt->input->buf->encoder != NULL) && |
| 3459 | (ctxt->input->buf->raw != NULL) && |
| 3460 | (ctxt->input->buf->buffer != NULL)) { |
| 3461 | int nbchars; |
| 3462 | int processed; |
| 3463 | |
| 3464 | /* |
| 3465 | * convert as much as possible to the parser reading buffer. |
| 3466 | */ |
| 3467 | processed = ctxt->input->cur - ctxt->input->base; |
| 3468 | xmlBufferShrink(ctxt->input->buf->buffer, processed); |
| 3469 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 3470 | ctxt->input->buf->buffer, |
| 3471 | ctxt->input->buf->raw); |
| 3472 | if (nbchars < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3473 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 3474 | "htmlCheckEncoding: encoder error\n", |
| 3475 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3476 | } |
| 3477 | ctxt->input->base = |
| 3478 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
Eugene Pimenov | 1e60fbc | 2010-03-10 18:10:49 +0100 | [diff] [blame] | 3479 | ctxt->input->end = |
| 3480 | &ctxt->input->base[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | /** |
| 3486 | * htmlCheckMeta: |
| 3487 | * @ctxt: an HTML parser context |
| 3488 | * @atts: the attributes values |
| 3489 | * |
| 3490 | * Checks an attributes from a Meta tag |
| 3491 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3492 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3493 | htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) { |
| 3494 | int i; |
| 3495 | const xmlChar *att, *value; |
| 3496 | int http = 0; |
| 3497 | const xmlChar *content = NULL; |
| 3498 | |
| 3499 | if ((ctxt == NULL) || (atts == NULL)) |
| 3500 | return; |
| 3501 | |
| 3502 | i = 0; |
| 3503 | att = atts[i++]; |
| 3504 | while (att != NULL) { |
| 3505 | value = atts[i++]; |
| 3506 | if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv")) |
| 3507 | && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) |
| 3508 | http = 1; |
| 3509 | else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content"))) |
| 3510 | content = value; |
| 3511 | att = atts[i++]; |
| 3512 | } |
| 3513 | if ((http) && (content != NULL)) |
| 3514 | htmlCheckEncoding(ctxt, content); |
| 3515 | |
| 3516 | } |
| 3517 | |
| 3518 | /** |
| 3519 | * htmlParseStartTag: |
| 3520 | * @ctxt: an HTML parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3521 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3522 | * parse a start of tag either for rule element or |
| 3523 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 3524 | * |
| 3525 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 3526 | * |
| 3527 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 3528 | * |
| 3529 | * With namespace: |
| 3530 | * |
| 3531 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 3532 | * |
| 3533 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 3534 | * |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3535 | * Returns 0 in case of success, -1 in case of error and 1 if discarded |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | */ |
| 3537 | |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 3538 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3539 | htmlParseStartTag(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3540 | const xmlChar *name; |
| 3541 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3542 | xmlChar *attvalue; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 3543 | const xmlChar **atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3544 | int nbatts = 0; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 3545 | int maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3546 | int meta = 0; |
| 3547 | int i; |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3548 | int discardtag = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3549 | |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3550 | if (ctxt->instate == XML_PARSER_EOF) |
| 3551 | return(-1); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3552 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 3553 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3554 | "htmlParseStartTag: context error\n", NULL, NULL); |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 3555 | return -1; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 3556 | } |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 3557 | if (CUR != '<') return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3558 | NEXT; |
| 3559 | |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 3560 | atts = ctxt->atts; |
| 3561 | maxatts = ctxt->maxatts; |
| 3562 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3563 | GROW; |
| 3564 | name = htmlParseHTMLName(ctxt); |
| 3565 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3566 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3567 | "htmlParseStartTag: invalid element name\n", |
| 3568 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3569 | /* Dump the bogus tag like browsers do */ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3570 | while ((IS_CHAR_CH(CUR)) && (CUR != '>') && |
| 3571 | (ctxt->instate != XML_PARSER_EOF)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3572 | NEXT; |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 3573 | return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3574 | } |
| 3575 | if (xmlStrEqual(name, BAD_CAST"meta")) |
| 3576 | meta = 1; |
| 3577 | |
| 3578 | /* |
| 3579 | * Check for auto-closure of HTML elements. |
| 3580 | */ |
| 3581 | htmlAutoClose(ctxt, name); |
| 3582 | |
| 3583 | /* |
| 3584 | * Check for implied HTML elements. |
| 3585 | */ |
| 3586 | htmlCheckImplied(ctxt, name); |
| 3587 | |
| 3588 | /* |
| 3589 | * Avoid html at any level > 0, head at any level != 1 |
| 3590 | * or any attempt to recurse body |
| 3591 | */ |
| 3592 | if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3593 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3594 | "htmlParseStartTag: misplaced <html> tag\n", |
| 3595 | name, NULL); |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3596 | discardtag = 1; |
Daniel Veillard | ed86dc2 | 2008-04-24 11:58:41 +0000 | [diff] [blame] | 3597 | ctxt->depth++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3598 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3599 | if ((ctxt->nameNr != 1) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3600 | (xmlStrEqual(name, BAD_CAST"head"))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3601 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3602 | "htmlParseStartTag: misplaced <head> tag\n", |
| 3603 | name, NULL); |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3604 | discardtag = 1; |
Daniel Veillard | ed86dc2 | 2008-04-24 11:58:41 +0000 | [diff] [blame] | 3605 | ctxt->depth++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3606 | } |
| 3607 | if (xmlStrEqual(name, BAD_CAST"body")) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3608 | int indx; |
| 3609 | for (indx = 0;indx < ctxt->nameNr;indx++) { |
| 3610 | if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3611 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 3612 | "htmlParseStartTag: misplaced <body> tag\n", |
| 3613 | name, NULL); |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3614 | discardtag = 1; |
Daniel Veillard | ed86dc2 | 2008-04-24 11:58:41 +0000 | [diff] [blame] | 3615 | ctxt->depth++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * Now parse the attributes, it ends up with the ending |
| 3622 | * |
| 3623 | * (S Attribute)* S? |
| 3624 | */ |
| 3625 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3626 | while ((IS_CHAR_CH(CUR)) && |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3627 | (CUR != '>') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3628 | ((CUR != '/') || (NXT(1) != '>'))) { |
| 3629 | long cons = ctxt->nbChars; |
| 3630 | |
| 3631 | GROW; |
| 3632 | attname = htmlParseAttribute(ctxt, &attvalue); |
| 3633 | if (attname != NULL) { |
| 3634 | |
| 3635 | /* |
| 3636 | * Well formedness requires at most one declaration of an attribute |
| 3637 | */ |
| 3638 | for (i = 0; i < nbatts;i += 2) { |
| 3639 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3640 | htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED, |
| 3641 | "Attribute %s redefined\n", attname, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3642 | if (attvalue != NULL) |
| 3643 | xmlFree(attvalue); |
| 3644 | goto failed; |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | /* |
| 3649 | * Add the pair to atts |
| 3650 | */ |
| 3651 | if (atts == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3652 | maxatts = 22; /* allow for 10 attrs by default */ |
| 3653 | atts = (const xmlChar **) |
| 3654 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3655 | if (atts == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3656 | htmlErrMemory(ctxt, NULL); |
| 3657 | if (attvalue != NULL) |
| 3658 | xmlFree(attvalue); |
| 3659 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3660 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3661 | ctxt->atts = atts; |
| 3662 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3663 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3664 | const xmlChar **n; |
| 3665 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3666 | maxatts *= 2; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3667 | n = (const xmlChar **) xmlRealloc((void *) atts, |
| 3668 | maxatts * sizeof(const xmlChar *)); |
| 3669 | if (n == NULL) { |
| 3670 | htmlErrMemory(ctxt, NULL); |
| 3671 | if (attvalue != NULL) |
| 3672 | xmlFree(attvalue); |
| 3673 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3674 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3675 | atts = n; |
| 3676 | ctxt->atts = atts; |
| 3677 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3678 | } |
| 3679 | atts[nbatts++] = attname; |
| 3680 | atts[nbatts++] = attvalue; |
| 3681 | atts[nbatts] = NULL; |
| 3682 | atts[nbatts + 1] = NULL; |
| 3683 | } |
| 3684 | else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3685 | if (attvalue != NULL) |
| 3686 | xmlFree(attvalue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3687 | /* Dump the bogus attribute string up to the next blank or |
| 3688 | * the end of the tag. */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3689 | while ((IS_CHAR_CH(CUR)) && |
| 3690 | !(IS_BLANK_CH(CUR)) && (CUR != '>') && |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 3691 | ((CUR != '/') || (NXT(1) != '>'))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3692 | NEXT; |
| 3693 | } |
| 3694 | |
| 3695 | failed: |
| 3696 | SKIP_BLANKS; |
| 3697 | if (cons == ctxt->nbChars) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3698 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 3699 | "htmlParseStartTag: problem parsing attributes\n", |
| 3700 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3701 | break; |
| 3702 | } |
| 3703 | } |
| 3704 | |
| 3705 | /* |
| 3706 | * Handle specific association to the META tag |
| 3707 | */ |
William M. Brack | e978ae2 | 2007-03-21 06:16:02 +0000 | [diff] [blame] | 3708 | if (meta && (nbatts != 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3709 | htmlCheckMeta(ctxt, atts); |
| 3710 | |
| 3711 | /* |
| 3712 | * SAX: Start of Element ! |
| 3713 | */ |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3714 | if (!discardtag) { |
| 3715 | htmlnamePush(ctxt, name); |
| 3716 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) { |
| 3717 | if (nbatts != 0) |
| 3718 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 3719 | else |
| 3720 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 3721 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3722 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3723 | |
| 3724 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3725 | for (i = 1;i < nbatts;i += 2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3726 | if (atts[i] != NULL) |
| 3727 | xmlFree((xmlChar *) atts[i]); |
| 3728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3729 | } |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 3730 | |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 3731 | return(discardtag); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | /** |
| 3735 | * htmlParseEndTag: |
| 3736 | * @ctxt: an HTML parser context |
| 3737 | * |
| 3738 | * parse an end of tag |
| 3739 | * |
| 3740 | * [42] ETag ::= '</' Name S? '>' |
| 3741 | * |
| 3742 | * With namespace |
| 3743 | * |
| 3744 | * [NS 9] ETag ::= '</' QName S? '>' |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3745 | * |
| 3746 | * Returns 1 if the current level should be closed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3747 | */ |
| 3748 | |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3749 | static int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3750 | htmlParseEndTag(htmlParserCtxtPtr ctxt) |
| 3751 | { |
| 3752 | const xmlChar *name; |
| 3753 | const xmlChar *oldname; |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3754 | int i, ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3755 | |
| 3756 | if ((CUR != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3757 | htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED, |
| 3758 | "htmlParseEndTag: '</' not found\n", NULL, NULL); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3759 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3760 | } |
| 3761 | SKIP(2); |
| 3762 | |
| 3763 | name = htmlParseHTMLName(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3764 | if (name == NULL) |
| 3765 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3766 | /* |
| 3767 | * We should definitely be at the ending "S? '>'" part |
| 3768 | */ |
| 3769 | SKIP_BLANKS; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3770 | if ((!IS_CHAR_CH(CUR)) || (CUR != '>')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3771 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 3772 | "End tag : expected '>'\n", NULL, NULL); |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 3773 | if (ctxt->recovery) { |
| 3774 | /* |
| 3775 | * We're not at the ending > !! |
| 3776 | * Error, unless in recover mode where we search forwards |
| 3777 | * until we find a > |
| 3778 | */ |
| 3779 | while (CUR != '\0' && CUR != '>') NEXT; |
| 3780 | NEXT; |
| 3781 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3782 | } else |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3783 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3784 | |
| 3785 | /* |
Daniel Veillard | ed86dc2 | 2008-04-24 11:58:41 +0000 | [diff] [blame] | 3786 | * if we ignored misplaced tags in htmlParseStartTag don't pop them |
| 3787 | * out now. |
| 3788 | */ |
| 3789 | if ((ctxt->depth > 0) && |
| 3790 | (xmlStrEqual(name, BAD_CAST "html") || |
| 3791 | xmlStrEqual(name, BAD_CAST "body") || |
| 3792 | xmlStrEqual(name, BAD_CAST "head"))) { |
| 3793 | ctxt->depth--; |
| 3794 | return (0); |
| 3795 | } |
| 3796 | |
| 3797 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3798 | * If the name read is not one of the element in the parsing stack |
| 3799 | * then return, it's just an error. |
| 3800 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3801 | for (i = (ctxt->nameNr - 1); i >= 0; i--) { |
| 3802 | if (xmlStrEqual(name, ctxt->nameTab[i])) |
| 3803 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3804 | } |
| 3805 | if (i < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3806 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 3807 | "Unexpected end tag : %s\n", name, NULL); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3808 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | |
| 3812 | /* |
| 3813 | * Check for auto-closure of HTML elements. |
| 3814 | */ |
| 3815 | |
| 3816 | htmlAutoCloseOnClose(ctxt, name); |
| 3817 | |
| 3818 | /* |
| 3819 | * Well formedness constraints, opening and closing must match. |
| 3820 | * With the exception that the autoclose may have popped stuff out |
| 3821 | * of the stack. |
| 3822 | */ |
| 3823 | if (!xmlStrEqual(name, ctxt->name)) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3824 | if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3825 | htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
| 3826 | "Opening and ending tag mismatch: %s and %s\n", |
| 3827 | name, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
| 3831 | /* |
| 3832 | * SAX: End of Tag |
| 3833 | */ |
| 3834 | oldname = ctxt->name; |
| 3835 | if ((oldname != NULL) && (xmlStrEqual(oldname, name))) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3836 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 3837 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3838 | htmlnamePop(ctxt); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3839 | ret = 1; |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3840 | } else { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3841 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3844 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | |
| 3848 | /** |
| 3849 | * htmlParseReference: |
| 3850 | * @ctxt: an HTML parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3851 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3852 | * parse and handle entity references in content, |
| 3853 | * this will end-up in a call to character() since this is either a |
| 3854 | * CharRef, or a predefined entity. |
| 3855 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3856 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3857 | htmlParseReference(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 3858 | const htmlEntityDesc * ent; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3859 | xmlChar out[6]; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3860 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | if (CUR != '&') return; |
| 3862 | |
| 3863 | if (NXT(1) == '#') { |
| 3864 | unsigned int c; |
| 3865 | int bits, i = 0; |
| 3866 | |
| 3867 | c = htmlParseCharRef(ctxt); |
| 3868 | if (c == 0) |
| 3869 | return; |
| 3870 | |
| 3871 | if (c < 0x80) { out[i++]= c; bits= -6; } |
| 3872 | else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 3873 | else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 3874 | else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3875 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | for ( ; bits >= 0; bits-= 6) { |
| 3877 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
| 3878 | } |
| 3879 | out[i] = 0; |
| 3880 | |
| 3881 | htmlCheckParagraph(ctxt); |
| 3882 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3883 | ctxt->sax->characters(ctxt->userData, out, i); |
| 3884 | } else { |
| 3885 | ent = htmlParseEntityRef(ctxt, &name); |
| 3886 | if (name == NULL) { |
| 3887 | htmlCheckParagraph(ctxt); |
| 3888 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3889 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
| 3890 | return; |
| 3891 | } |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 3892 | if ((ent == NULL) || !(ent->value > 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3893 | htmlCheckParagraph(ctxt); |
| 3894 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) { |
| 3895 | ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1); |
| 3896 | ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name)); |
| 3897 | /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */ |
| 3898 | } |
| 3899 | } else { |
| 3900 | unsigned int c; |
| 3901 | int bits, i = 0; |
| 3902 | |
| 3903 | c = ent->value; |
| 3904 | if (c < 0x80) |
| 3905 | { out[i++]= c; bits= -6; } |
| 3906 | else if (c < 0x800) |
| 3907 | { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 3908 | else if (c < 0x10000) |
| 3909 | { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3910 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3911 | { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3912 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3913 | for ( ; bits >= 0; bits-= 6) { |
| 3914 | out[i++]= ((c >> bits) & 0x3F) | 0x80; |
| 3915 | } |
| 3916 | out[i] = 0; |
| 3917 | |
| 3918 | htmlCheckParagraph(ctxt); |
| 3919 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 3920 | ctxt->sax->characters(ctxt->userData, out, i); |
| 3921 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | /** |
| 3926 | * htmlParseContent: |
| 3927 | * @ctxt: an HTML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3928 | * |
| 3929 | * Parse a content: comment, sub-element, reference or text. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3930 | */ |
| 3931 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3932 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3933 | htmlParseContent(htmlParserCtxtPtr ctxt) { |
| 3934 | xmlChar *currentNode; |
| 3935 | int depth; |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 3936 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | |
| 3938 | currentNode = xmlStrdup(ctxt->name); |
| 3939 | depth = ctxt->nameNr; |
| 3940 | while (1) { |
| 3941 | long cons = ctxt->nbChars; |
| 3942 | |
| 3943 | GROW; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (ctxt->instate == XML_PARSER_EOF) |
| 3946 | break; |
| 3947 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3948 | /* |
| 3949 | * Our tag or one of it's parent or children is ending. |
| 3950 | */ |
| 3951 | if ((CUR == '<') && (NXT(1) == '/')) { |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3952 | if (htmlParseEndTag(ctxt) && |
| 3953 | ((currentNode != NULL) || (ctxt->nameNr == 0))) { |
| 3954 | if (currentNode != NULL) |
| 3955 | xmlFree(currentNode); |
| 3956 | return; |
| 3957 | } |
| 3958 | continue; /* while */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 3961 | else if ((CUR == '<') && |
| 3962 | ((IS_ASCII_LETTER(NXT(1))) || |
| 3963 | (NXT(1) == '_') || (NXT(1) == ':'))) { |
| 3964 | name = htmlParseHTMLName_nonInvasive(ctxt); |
| 3965 | if (name == NULL) { |
| 3966 | htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED, |
| 3967 | "htmlParseStartTag: invalid element name\n", |
| 3968 | NULL, NULL); |
| 3969 | /* Dump the bogus tag like browsers do */ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3970 | while ((IS_CHAR_CH(CUR)) && (CUR != '>')) |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 3971 | NEXT; |
| 3972 | |
| 3973 | if (currentNode != NULL) |
| 3974 | xmlFree(currentNode); |
| 3975 | return; |
| 3976 | } |
| 3977 | |
| 3978 | if (ctxt->name != NULL) { |
| 3979 | if (htmlCheckAutoClose(name, ctxt->name) == 1) { |
| 3980 | htmlAutoClose(ctxt, name); |
| 3981 | continue; |
| 3982 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 3983 | } |
Daniel Veillard | 890fd9f | 2006-10-27 12:53:28 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3986 | /* |
| 3987 | * Has this node been popped out during parsing of |
| 3988 | * the next element |
| 3989 | */ |
Daniel Veillard | f420ac5 | 2001-07-04 16:04:09 +0000 | [diff] [blame] | 3990 | if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) && |
| 3991 | (!xmlStrEqual(currentNode, ctxt->name))) |
| 3992 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3993 | if (currentNode != NULL) xmlFree(currentNode); |
| 3994 | return; |
| 3995 | } |
| 3996 | |
Daniel Veillard | f9533d1 | 2001-03-03 10:04:57 +0000 | [diff] [blame] | 3997 | if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) || |
| 3998 | (xmlStrEqual(currentNode, BAD_CAST"style")))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3999 | /* |
| 4000 | * Handle SCRIPT/STYLE separately |
| 4001 | */ |
| 4002 | htmlParseScript(ctxt); |
| 4003 | } else { |
| 4004 | /* |
| 4005 | * Sometimes DOCTYPE arrives in the middle of the document |
| 4006 | */ |
| 4007 | if ((CUR == '<') && (NXT(1) == '!') && |
| 4008 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4009 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4010 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4011 | (UPP(8) == 'E')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4012 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 4013 | "Misplaced DOCTYPE declaration\n", |
| 4014 | BAD_CAST "DOCTYPE" , NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4015 | htmlParseDocTypeDecl(ctxt); |
| 4016 | } |
| 4017 | |
| 4018 | /* |
| 4019 | * First case : a comment |
| 4020 | */ |
| 4021 | if ((CUR == '<') && (NXT(1) == '!') && |
| 4022 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 4023 | htmlParseComment(ctxt); |
| 4024 | } |
| 4025 | |
| 4026 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4027 | * Second case : a Processing Instruction. |
| 4028 | */ |
| 4029 | else if ((CUR == '<') && (NXT(1) == '?')) { |
| 4030 | htmlParsePI(ctxt); |
| 4031 | } |
| 4032 | |
| 4033 | /* |
| 4034 | * Third case : a sub-element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | */ |
| 4036 | else if (CUR == '<') { |
| 4037 | htmlParseElement(ctxt); |
| 4038 | } |
| 4039 | |
| 4040 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4041 | * Fourth case : a reference. If if has not been resolved, |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4042 | * parsing returns it's Name, create the node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4043 | */ |
| 4044 | else if (CUR == '&') { |
| 4045 | htmlParseReference(ctxt); |
| 4046 | } |
| 4047 | |
| 4048 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4049 | * Fifth case : end of the resource |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4050 | */ |
| 4051 | else if (CUR == 0) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4052 | htmlAutoCloseOnEnd(ctxt); |
| 4053 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | /* |
| 4057 | * Last case, text. Note that References are handled directly. |
| 4058 | */ |
| 4059 | else { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4060 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | if (cons == ctxt->nbChars) { |
| 4064 | if (ctxt->node != NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4065 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4066 | "detected an error in element content\n", |
| 4067 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4068 | } |
| 4069 | break; |
| 4070 | } |
| 4071 | } |
| 4072 | GROW; |
| 4073 | } |
| 4074 | if (currentNode != NULL) xmlFree(currentNode); |
| 4075 | } |
| 4076 | |
| 4077 | /** |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 4078 | * htmlParseContent: |
| 4079 | * @ctxt: an HTML parser context |
| 4080 | * |
| 4081 | * Parse a content: comment, sub-element, reference or text. |
| 4082 | */ |
| 4083 | |
| 4084 | void |
| 4085 | __htmlParseContent(void *ctxt) { |
| 4086 | if (ctxt != NULL) |
| 4087 | htmlParseContent((htmlParserCtxtPtr) ctxt); |
| 4088 | } |
| 4089 | |
| 4090 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4091 | * htmlParseElement: |
| 4092 | * @ctxt: an HTML parser context |
| 4093 | * |
| 4094 | * parse an HTML element, this is highly recursive |
| 4095 | * |
| 4096 | * [39] element ::= EmptyElemTag | STag content ETag |
| 4097 | * |
| 4098 | * [41] Attribute ::= Name Eq AttValue |
| 4099 | */ |
| 4100 | |
| 4101 | void |
| 4102 | htmlParseElement(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4103 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4104 | xmlChar *currentNode = NULL; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 4105 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4106 | htmlParserNodeInfo node_info; |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 4107 | int failed; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 4108 | int depth; |
Daniel Veillard | 3fbe8e3 | 2001-10-06 13:30:33 +0000 | [diff] [blame] | 4109 | const xmlChar *oldptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4110 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 4111 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 4112 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 4113 | "htmlParseElement: context error\n", NULL, NULL); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 4114 | return; |
| 4115 | } |
Daniel Veillard | db4ac22 | 2009-08-22 17:58:31 +0200 | [diff] [blame] | 4116 | |
| 4117 | if (ctxt->instate == XML_PARSER_EOF) |
| 4118 | return; |
| 4119 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4120 | /* Capture start position */ |
| 4121 | if (ctxt->record_info) { |
| 4122 | node_info.begin_pos = ctxt->input->consumed + |
| 4123 | (CUR_PTR - ctxt->input->base); |
| 4124 | node_info.begin_line = ctxt->input->line; |
| 4125 | } |
| 4126 | |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 4127 | failed = htmlParseStartTag(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4128 | name = ctxt->name; |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 4129 | if ((failed == -1) || (name == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | if (CUR == '>') |
| 4131 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | return; |
| 4133 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* |
| 4136 | * Lookup the info for that element. |
| 4137 | */ |
| 4138 | info = htmlTagLookup(name); |
| 4139 | if (info == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4140 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
| 4141 | "Tag %s invalid\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4145 | * Check for an Empty Element labeled the XML/SGML way |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4146 | */ |
| 4147 | if ((CUR == '/') && (NXT(1) == '>')) { |
| 4148 | SKIP(2); |
| 4149 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 4150 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4151 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4152 | return; |
| 4153 | } |
| 4154 | |
| 4155 | if (CUR == '>') { |
| 4156 | NEXT; |
| 4157 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4158 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 4159 | "Couldn't find end of Start Tag %s\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4160 | |
| 4161 | /* |
| 4162 | * end of parsing of this node. |
| 4163 | */ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4164 | if (xmlStrEqual(name, ctxt->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4165 | nodePop(ctxt); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4166 | htmlnamePop(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4167 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * Capture end position and add node |
| 4171 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 4172 | if (ctxt->record_info) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4173 | node_info.end_pos = ctxt->input->consumed + |
| 4174 | (CUR_PTR - ctxt->input->base); |
| 4175 | node_info.end_line = ctxt->input->line; |
| 4176 | node_info.node = ctxt->node; |
| 4177 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 4178 | } |
| 4179 | return; |
| 4180 | } |
| 4181 | |
| 4182 | /* |
| 4183 | * Check for an Empty Element from DTD definition |
| 4184 | */ |
| 4185 | if ((info != NULL) && (info->empty)) { |
| 4186 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 4187 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4188 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4189 | return; |
| 4190 | } |
| 4191 | |
| 4192 | /* |
| 4193 | * Parse the content of the element: |
| 4194 | */ |
| 4195 | currentNode = xmlStrdup(ctxt->name); |
| 4196 | depth = ctxt->nameNr; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4197 | while (IS_CHAR_CH(CUR)) { |
William M. Brack | d28e48a | 2001-09-23 01:55:08 +0000 | [diff] [blame] | 4198 | oldptr = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4199 | htmlParseContent(ctxt); |
William M. Brack | d28e48a | 2001-09-23 01:55:08 +0000 | [diff] [blame] | 4200 | if (oldptr==ctxt->input->cur) break; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4201 | if (ctxt->nameNr < depth) break; |
| 4202 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4203 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4204 | /* |
| 4205 | * Capture end position and add node |
| 4206 | */ |
| 4207 | if ( currentNode != NULL && ctxt->record_info ) { |
| 4208 | node_info.end_pos = ctxt->input->consumed + |
| 4209 | (CUR_PTR - ctxt->input->base); |
| 4210 | node_info.end_line = ctxt->input->line; |
| 4211 | node_info.node = ctxt->node; |
| 4212 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 4213 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4214 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4215 | htmlAutoCloseOnEnd(ctxt); |
| 4216 | } |
| 4217 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4218 | if (currentNode != NULL) |
| 4219 | xmlFree(currentNode); |
| 4220 | } |
| 4221 | |
| 4222 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4223 | * htmlParseDocument: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4224 | * @ctxt: an HTML parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4225 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4226 | * parse an HTML document (and build a tree if using the standard SAX |
| 4227 | * interface). |
| 4228 | * |
| 4229 | * Returns 0, -1 in case of error. the parser context is augmented |
| 4230 | * as a result of the parsing. |
| 4231 | */ |
| 4232 | |
Daniel Veillard | 1b31e4a | 2002-05-27 14:44:50 +0000 | [diff] [blame] | 4233 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4234 | htmlParseDocument(htmlParserCtxtPtr ctxt) { |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 4235 | xmlChar start[4]; |
| 4236 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4237 | xmlDtdPtr dtd; |
| 4238 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 4239 | xmlInitParser(); |
| 4240 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4241 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4242 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 4243 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 4244 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4245 | "htmlParseDocument: context error\n", NULL, NULL); |
| 4246 | return(XML_ERR_INTERNAL_ERROR); |
| 4247 | } |
| 4248 | ctxt->html = 1; |
Daniel Veillard | 4d3e2da | 2009-05-15 17:55:45 +0200 | [diff] [blame] | 4249 | ctxt->linenumbers = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4250 | GROW; |
| 4251 | /* |
| 4252 | * SAX: beginning of the document processing. |
| 4253 | */ |
| 4254 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 4255 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 4256 | |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 4257 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 4258 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
| 4259 | /* |
| 4260 | * Get the 4 first bytes and decode the charset |
| 4261 | * if enc != XML_CHAR_ENCODING_NONE |
| 4262 | * plug some encoding conversion routines. |
| 4263 | */ |
| 4264 | start[0] = RAW; |
| 4265 | start[1] = NXT(1); |
| 4266 | start[2] = NXT(2); |
| 4267 | start[3] = NXT(3); |
| 4268 | enc = xmlDetectCharEncoding(&start[0], 4); |
| 4269 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 4270 | xmlSwitchEncoding(ctxt, enc); |
| 4271 | } |
| 4272 | } |
| 4273 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4274 | /* |
| 4275 | * Wipe out everything which is before the first '<' |
| 4276 | */ |
| 4277 | SKIP_BLANKS; |
| 4278 | if (CUR == 0) { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4279 | htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4280 | "Document is empty\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 4284 | ctxt->sax->startDocument(ctxt->userData); |
| 4285 | |
| 4286 | |
| 4287 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4288 | * Parse possible comments and PIs before any content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4289 | */ |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4290 | while (((CUR == '<') && (NXT(1) == '!') && |
| 4291 | (NXT(2) == '-') && (NXT(3) == '-')) || |
| 4292 | ((CUR == '<') && (NXT(1) == '?'))) { |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 4293 | htmlParseComment(ctxt); |
| 4294 | htmlParsePI(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4295 | SKIP_BLANKS; |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 4296 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4297 | |
| 4298 | |
| 4299 | /* |
| 4300 | * Then possibly doc type declaration(s) and more Misc |
| 4301 | * (doctypedecl Misc*)? |
| 4302 | */ |
| 4303 | if ((CUR == '<') && (NXT(1) == '!') && |
| 4304 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4305 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4306 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4307 | (UPP(8) == 'E')) { |
| 4308 | htmlParseDocTypeDecl(ctxt); |
| 4309 | } |
| 4310 | SKIP_BLANKS; |
| 4311 | |
| 4312 | /* |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4313 | * Parse possible comments and PIs before any content |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4314 | */ |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4315 | while (((CUR == '<') && (NXT(1) == '!') && |
| 4316 | (NXT(2) == '-') && (NXT(3) == '-')) || |
| 4317 | ((CUR == '<') && (NXT(1) == '?'))) { |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4318 | htmlParseComment(ctxt); |
| 4319 | htmlParsePI(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4320 | SKIP_BLANKS; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4321 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4322 | |
| 4323 | /* |
| 4324 | * Time to start parsing the tree itself |
| 4325 | */ |
| 4326 | htmlParseContent(ctxt); |
| 4327 | |
| 4328 | /* |
| 4329 | * autoclose |
| 4330 | */ |
| 4331 | if (CUR == 0) |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4332 | htmlAutoCloseOnEnd(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4333 | |
| 4334 | |
| 4335 | /* |
| 4336 | * SAX: end of the document processing. |
| 4337 | */ |
| 4338 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 4339 | ctxt->sax->endDocument(ctxt->userData); |
| 4340 | |
| 4341 | if (ctxt->myDoc != NULL) { |
| 4342 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 4343 | if (dtd == NULL) |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4344 | ctxt->myDoc->intSubset = |
| 4345 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4346 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
| 4347 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
| 4348 | } |
| 4349 | if (! ctxt->wellFormed) return(-1); |
| 4350 | return(0); |
| 4351 | } |
| 4352 | |
| 4353 | |
| 4354 | /************************************************************************ |
| 4355 | * * |
| 4356 | * Parser contexts handling * |
| 4357 | * * |
| 4358 | ************************************************************************/ |
| 4359 | |
| 4360 | /** |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 4361 | * htmlInitParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4362 | * @ctxt: an HTML parser context |
| 4363 | * |
| 4364 | * Initialize a parser context |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4365 | * |
| 4366 | * Returns 0 in case of success and -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4367 | */ |
| 4368 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4369 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | htmlInitParserCtxt(htmlParserCtxtPtr ctxt) |
| 4371 | { |
| 4372 | htmlSAXHandler *sax; |
| 4373 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4374 | if (ctxt == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4375 | memset(ctxt, 0, sizeof(htmlParserCtxt)); |
| 4376 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4377 | ctxt->dict = xmlDictCreate(); |
| 4378 | if (ctxt->dict == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4379 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
| 4380 | return(-1); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4381 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4382 | sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler)); |
| 4383 | if (sax == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4384 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
| 4385 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4386 | } |
| 4387 | else |
| 4388 | memset(sax, 0, sizeof(htmlSAXHandler)); |
| 4389 | |
| 4390 | /* Allocate the Input stack */ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4391 | ctxt->inputTab = (htmlParserInputPtr *) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4392 | xmlMalloc(5 * sizeof(htmlParserInputPtr)); |
| 4393 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4394 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4395 | ctxt->inputNr = 0; |
| 4396 | ctxt->inputMax = 0; |
| 4397 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4398 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4399 | } |
| 4400 | ctxt->inputNr = 0; |
| 4401 | ctxt->inputMax = 5; |
| 4402 | ctxt->input = NULL; |
| 4403 | ctxt->version = NULL; |
| 4404 | ctxt->encoding = NULL; |
| 4405 | ctxt->standalone = -1; |
| 4406 | ctxt->instate = XML_PARSER_START; |
| 4407 | |
| 4408 | /* Allocate the Node stack */ |
| 4409 | ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr)); |
| 4410 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4411 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4412 | ctxt->nodeNr = 0; |
| 4413 | ctxt->nodeMax = 0; |
| 4414 | ctxt->node = NULL; |
| 4415 | ctxt->inputNr = 0; |
| 4416 | ctxt->inputMax = 0; |
| 4417 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4418 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4419 | } |
| 4420 | ctxt->nodeNr = 0; |
| 4421 | ctxt->nodeMax = 10; |
| 4422 | ctxt->node = NULL; |
| 4423 | |
| 4424 | /* Allocate the Name stack */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4425 | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4427 | htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4428 | ctxt->nameNr = 0; |
Eugene Pimenov | ef9c636 | 2010-03-15 11:37:48 +0100 | [diff] [blame^] | 4429 | ctxt->nameMax = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4430 | ctxt->name = NULL; |
| 4431 | ctxt->nodeNr = 0; |
| 4432 | ctxt->nodeMax = 0; |
| 4433 | ctxt->node = NULL; |
| 4434 | ctxt->inputNr = 0; |
| 4435 | ctxt->inputMax = 0; |
| 4436 | ctxt->input = NULL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4437 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4438 | } |
| 4439 | ctxt->nameNr = 0; |
| 4440 | ctxt->nameMax = 10; |
| 4441 | ctxt->name = NULL; |
| 4442 | |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4443 | if (sax == NULL) ctxt->sax = (xmlSAXHandlerPtr) &htmlDefaultSAXHandler; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4444 | else { |
| 4445 | ctxt->sax = sax; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4446 | memcpy(sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | } |
| 4448 | ctxt->userData = ctxt; |
| 4449 | ctxt->myDoc = NULL; |
| 4450 | ctxt->wellFormed = 1; |
| 4451 | ctxt->replaceEntities = 0; |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 4452 | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4453 | ctxt->html = 1; |
Daniel Veillard | eff45a9 | 2004-10-29 12:10:55 +0000 | [diff] [blame] | 4454 | ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0; |
William M. Brack | edb65a7 | 2004-02-06 07:36:04 +0000 | [diff] [blame] | 4455 | ctxt->vctxt.userData = ctxt; |
| 4456 | ctxt->vctxt.error = xmlParserValidityError; |
| 4457 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4458 | ctxt->record_info = 0; |
| 4459 | ctxt->validate = 0; |
| 4460 | ctxt->nbChars = 0; |
| 4461 | ctxt->checkIndex = 0; |
Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 4462 | ctxt->catalogs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4463 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4464 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | /** |
| 4468 | * htmlFreeParserCtxt: |
| 4469 | * @ctxt: an HTML parser context |
| 4470 | * |
| 4471 | * Free all the memory used by a parser context. However the parsed |
| 4472 | * document in ctxt->myDoc is not freed. |
| 4473 | */ |
| 4474 | |
| 4475 | void |
| 4476 | htmlFreeParserCtxt(htmlParserCtxtPtr ctxt) |
| 4477 | { |
| 4478 | xmlFreeParserCtxt(ctxt); |
| 4479 | } |
| 4480 | |
| 4481 | /** |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4482 | * htmlNewParserCtxt: |
| 4483 | * |
| 4484 | * Allocate and initialize a new parser context. |
| 4485 | * |
Daniel Veillard | 34c647c | 2006-09-21 06:53:59 +0000 | [diff] [blame] | 4486 | * Returns the htmlParserCtxtPtr or NULL in case of allocation error |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4487 | */ |
| 4488 | |
Daniel Veillard | 34c647c | 2006-09-21 06:53:59 +0000 | [diff] [blame] | 4489 | htmlParserCtxtPtr |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4490 | htmlNewParserCtxt(void) |
| 4491 | { |
| 4492 | xmlParserCtxtPtr ctxt; |
| 4493 | |
| 4494 | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
| 4495 | if (ctxt == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4496 | htmlErrMemory(NULL, "NewParserCtxt: out of memory\n"); |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4497 | return(NULL); |
| 4498 | } |
| 4499 | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4500 | if (htmlInitParserCtxt(ctxt) < 0) { |
| 4501 | htmlFreeParserCtxt(ctxt); |
| 4502 | return(NULL); |
| 4503 | } |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4504 | return(ctxt); |
| 4505 | } |
| 4506 | |
| 4507 | /** |
| 4508 | * htmlCreateMemoryParserCtxt: |
| 4509 | * @buffer: a pointer to a char array |
| 4510 | * @size: the size of the array |
| 4511 | * |
| 4512 | * Create a parser context for an HTML in-memory document. |
| 4513 | * |
| 4514 | * Returns the new parser context or NULL |
| 4515 | */ |
Daniel Veillard | 02ea141 | 2003-04-09 12:08:47 +0000 | [diff] [blame] | 4516 | htmlParserCtxtPtr |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4517 | htmlCreateMemoryParserCtxt(const char *buffer, int size) { |
| 4518 | xmlParserCtxtPtr ctxt; |
| 4519 | xmlParserInputPtr input; |
| 4520 | xmlParserInputBufferPtr buf; |
| 4521 | |
| 4522 | if (buffer == NULL) |
| 4523 | return(NULL); |
| 4524 | if (size <= 0) |
| 4525 | return(NULL); |
| 4526 | |
| 4527 | ctxt = htmlNewParserCtxt(); |
| 4528 | if (ctxt == NULL) |
| 4529 | return(NULL); |
| 4530 | |
| 4531 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 4532 | if (buf == NULL) return(NULL); |
| 4533 | |
| 4534 | input = xmlNewInputStream(ctxt); |
| 4535 | if (input == NULL) { |
| 4536 | xmlFreeParserCtxt(ctxt); |
| 4537 | return(NULL); |
| 4538 | } |
| 4539 | |
| 4540 | input->filename = NULL; |
| 4541 | input->buf = buf; |
| 4542 | input->base = input->buf->buffer->content; |
| 4543 | input->cur = input->buf->buffer->content; |
| 4544 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
| 4545 | |
| 4546 | inputPush(ctxt, input); |
| 4547 | return(ctxt); |
| 4548 | } |
| 4549 | |
| 4550 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4551 | * htmlCreateDocParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4552 | * @cur: a pointer to an array of xmlChar |
| 4553 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 4554 | * |
| 4555 | * Create a parser context for an HTML document. |
| 4556 | * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4557 | * TODO: check the need to add encoding handling there |
| 4558 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4559 | * Returns the new parser context or NULL |
| 4560 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4561 | static htmlParserCtxtPtr |
Daniel Veillard | 4d1320f | 2007-04-26 08:55:33 +0000 | [diff] [blame] | 4562 | htmlCreateDocParserCtxt(const xmlChar *cur, const char *encoding) { |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4563 | int len; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4564 | htmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4565 | |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4566 | if (cur == NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4567 | return(NULL); |
Daniel Veillard | 1d99527 | 2002-07-22 16:43:32 +0000 | [diff] [blame] | 4568 | len = xmlStrlen(cur); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4569 | ctxt = htmlCreateMemoryParserCtxt((char *)cur, len); |
Daniel Veillard | 739e9d0 | 2007-04-27 09:33:58 +0000 | [diff] [blame] | 4570 | if (ctxt == NULL) |
| 4571 | return(NULL); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4572 | |
| 4573 | if (encoding != NULL) { |
| 4574 | xmlCharEncoding enc; |
| 4575 | xmlCharEncodingHandlerPtr handler; |
| 4576 | |
| 4577 | if (ctxt->input->encoding != NULL) |
| 4578 | xmlFree((xmlChar *) ctxt->input->encoding); |
Daniel Veillard | e8ed620 | 2003-08-14 23:39:01 +0000 | [diff] [blame] | 4579 | ctxt->input->encoding = xmlStrdup((const xmlChar *) encoding); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4580 | |
| 4581 | enc = xmlParseCharEncoding(encoding); |
| 4582 | /* |
| 4583 | * registered set of known encodings |
| 4584 | */ |
| 4585 | if (enc != XML_CHAR_ENCODING_ERROR) { |
| 4586 | xmlSwitchEncoding(ctxt, enc); |
| 4587 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4588 | htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4589 | "Unsupported encoding %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4590 | (const xmlChar *) encoding, NULL); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4591 | } |
| 4592 | } else { |
| 4593 | /* |
| 4594 | * fallback for unknown encodings |
| 4595 | */ |
| 4596 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 4597 | if (handler != NULL) { |
| 4598 | xmlSwitchToEncoding(ctxt, handler); |
| 4599 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4600 | htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 4601 | "Unsupported encoding %s\n", |
| 4602 | (const xmlChar *) encoding, NULL); |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | } |
| 4606 | return(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 4609 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4610 | /************************************************************************ |
| 4611 | * * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4612 | * Progressive parsing interfaces * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4613 | * * |
| 4614 | ************************************************************************/ |
| 4615 | |
| 4616 | /** |
| 4617 | * htmlParseLookupSequence: |
| 4618 | * @ctxt: an HTML parser context |
| 4619 | * @first: the first char to lookup |
| 4620 | * @next: the next char to lookup or zero |
| 4621 | * @third: the next char to lookup or zero |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 4622 | * @comment: flag to force checking inside comments |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4623 | * |
| 4624 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 4625 | * (first) is available in the input stream. |
| 4626 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 4627 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 4628 | * parser, do not use liberally. |
| 4629 | * This is basically similar to xmlParseLookupSequence() |
| 4630 | * |
| 4631 | * Returns the index to the current parsing point if the full sequence |
| 4632 | * is available, -1 otherwise. |
| 4633 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4634 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4635 | htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first, |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 4636 | xmlChar next, xmlChar third, int iscomment, |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4637 | int ignoreattrval) |
| 4638 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4639 | int base, len; |
| 4640 | htmlParserInputPtr in; |
| 4641 | const xmlChar *buf; |
Daniel Veillard | c1f7834 | 2001-11-10 11:43:05 +0000 | [diff] [blame] | 4642 | int incomment = 0; |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 4643 | int invalue = 0; |
| 4644 | char valdellim = 0x0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4645 | |
| 4646 | in = ctxt->input; |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4647 | if (in == NULL) |
| 4648 | return (-1); |
| 4649 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4650 | base = in->cur - in->base; |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4651 | if (base < 0) |
| 4652 | return (-1); |
| 4653 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4654 | if (ctxt->checkIndex > base) |
| 4655 | base = ctxt->checkIndex; |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4656 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4657 | if (in->buf == NULL) { |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4658 | buf = in->base; |
| 4659 | len = in->length; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4660 | } else { |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4661 | buf = in->buf->buffer->content; |
| 4662 | len = in->buf->buffer->use; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4663 | } |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4664 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4665 | /* take into account the sequence length */ |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4666 | if (third) |
| 4667 | len -= 2; |
| 4668 | else if (next) |
| 4669 | len--; |
| 4670 | for (; base < len; base++) { |
| 4671 | if ((!incomment) && (base + 4 < len) && (!iscomment)) { |
| 4672 | if ((buf[base] == '<') && (buf[base + 1] == '!') && |
| 4673 | (buf[base + 2] == '-') && (buf[base + 3] == '-')) { |
| 4674 | incomment = 1; |
| 4675 | /* do not increment past <! - some people use <!--> */ |
| 4676 | base += 2; |
| 4677 | } |
| 4678 | } |
| 4679 | if (ignoreattrval) { |
| 4680 | if (buf[base] == '"' || buf[base] == '\'') { |
| 4681 | if (invalue) { |
| 4682 | if (buf[base] == valdellim) { |
| 4683 | invalue = 0; |
| 4684 | continue; |
| 4685 | } |
| 4686 | } else { |
| 4687 | valdellim = buf[base]; |
| 4688 | invalue = 1; |
| 4689 | continue; |
| 4690 | } |
| 4691 | } else if (invalue) { |
| 4692 | continue; |
| 4693 | } |
| 4694 | } |
| 4695 | if (incomment) { |
| 4696 | if (base + 3 > len) |
| 4697 | return (-1); |
| 4698 | if ((buf[base] == '-') && (buf[base + 1] == '-') && |
| 4699 | (buf[base + 2] == '>')) { |
| 4700 | incomment = 0; |
| 4701 | base += 2; |
| 4702 | } |
| 4703 | continue; |
| 4704 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4705 | if (buf[base] == first) { |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4706 | if (third != 0) { |
| 4707 | if ((buf[base + 1] != next) || (buf[base + 2] != third)) |
| 4708 | continue; |
| 4709 | } else if (next != 0) { |
| 4710 | if (buf[base + 1] != next) |
| 4711 | continue; |
| 4712 | } |
| 4713 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4714 | #ifdef DEBUG_PUSH |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4715 | if (next == 0) |
| 4716 | xmlGenericError(xmlGenericErrorContext, |
| 4717 | "HPP: lookup '%c' found at %d\n", |
| 4718 | first, base); |
| 4719 | else if (third == 0) |
| 4720 | xmlGenericError(xmlGenericErrorContext, |
| 4721 | "HPP: lookup '%c%c' found at %d\n", |
| 4722 | first, next, base); |
| 4723 | else |
| 4724 | xmlGenericError(xmlGenericErrorContext, |
| 4725 | "HPP: lookup '%c%c%c' found at %d\n", |
| 4726 | first, next, third, base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4727 | #endif |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4728 | return (base - (in->cur - in->base)); |
| 4729 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | } |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4731 | if ((!incomment) && (!invalue)) |
| 4732 | ctxt->checkIndex = base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4733 | #ifdef DEBUG_PUSH |
| 4734 | if (next == 0) |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4735 | xmlGenericError(xmlGenericErrorContext, |
| 4736 | "HPP: lookup '%c' failed\n", first); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4737 | else if (third == 0) |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4738 | xmlGenericError(xmlGenericErrorContext, |
| 4739 | "HPP: lookup '%c%c' failed\n", first, next); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4740 | else |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4741 | xmlGenericError(xmlGenericErrorContext, |
| 4742 | "HPP: lookup '%c%c%c' failed\n", first, next, |
| 4743 | third); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4744 | #endif |
Daniel Veillard | eeb9932 | 2009-08-25 14:42:16 +0200 | [diff] [blame] | 4745 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | /** |
Markus Kull | 56a0303 | 2009-08-24 19:00:23 +0200 | [diff] [blame] | 4749 | * htmlParseLookupChars: |
| 4750 | * @ctxt: an HTML parser context |
| 4751 | * @stop: Array of chars, which stop the lookup. |
| 4752 | * @stopLen: Length of stop-Array |
| 4753 | * |
| 4754 | * Try to find if any char of the stop-Array is available in the input |
| 4755 | * stream. |
| 4756 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 4757 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 4758 | * parser, do not use liberally. |
| 4759 | * |
| 4760 | * Returns the index to the current parsing point if a stopChar |
| 4761 | * is available, -1 otherwise. |
| 4762 | */ |
| 4763 | static int |
| 4764 | htmlParseLookupChars(htmlParserCtxtPtr ctxt, const xmlChar * stop, |
| 4765 | int stopLen) |
| 4766 | { |
| 4767 | int base, len; |
| 4768 | htmlParserInputPtr in; |
| 4769 | const xmlChar *buf; |
| 4770 | int incomment = 0; |
| 4771 | int i; |
| 4772 | |
| 4773 | in = ctxt->input; |
| 4774 | if (in == NULL) |
| 4775 | return (-1); |
| 4776 | |
| 4777 | base = in->cur - in->base; |
| 4778 | if (base < 0) |
| 4779 | return (-1); |
| 4780 | |
| 4781 | if (ctxt->checkIndex > base) |
| 4782 | base = ctxt->checkIndex; |
| 4783 | |
| 4784 | if (in->buf == NULL) { |
| 4785 | buf = in->base; |
| 4786 | len = in->length; |
| 4787 | } else { |
| 4788 | buf = in->buf->buffer->content; |
| 4789 | len = in->buf->buffer->use; |
| 4790 | } |
| 4791 | |
| 4792 | for (; base < len; base++) { |
| 4793 | if (!incomment && (base + 4 < len)) { |
| 4794 | if ((buf[base] == '<') && (buf[base + 1] == '!') && |
| 4795 | (buf[base + 2] == '-') && (buf[base + 3] == '-')) { |
| 4796 | incomment = 1; |
| 4797 | /* do not increment past <! - some people use <!--> */ |
| 4798 | base += 2; |
| 4799 | } |
| 4800 | } |
| 4801 | if (incomment) { |
| 4802 | if (base + 3 > len) |
| 4803 | return (-1); |
| 4804 | if ((buf[base] == '-') && (buf[base + 1] == '-') && |
| 4805 | (buf[base + 2] == '>')) { |
| 4806 | incomment = 0; |
| 4807 | base += 2; |
| 4808 | } |
| 4809 | continue; |
| 4810 | } |
| 4811 | for (i = 0; i < stopLen; ++i) { |
| 4812 | if (buf[base] == stop[i]) { |
| 4813 | ctxt->checkIndex = 0; |
| 4814 | return (base - (in->cur - in->base)); |
| 4815 | } |
| 4816 | } |
| 4817 | } |
| 4818 | ctxt->checkIndex = base; |
| 4819 | return (-1); |
| 4820 | } |
| 4821 | |
| 4822 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4823 | * htmlParseTryOrFinish: |
| 4824 | * @ctxt: an HTML parser context |
| 4825 | * @terminate: last chunk indicator |
| 4826 | * |
| 4827 | * Try to progress on parsing |
| 4828 | * |
| 4829 | * Returns zero if no parsing was possible |
| 4830 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4831 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4832 | htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) { |
| 4833 | int ret = 0; |
| 4834 | htmlParserInputPtr in; |
| 4835 | int avail = 0; |
| 4836 | xmlChar cur, next; |
| 4837 | |
| 4838 | #ifdef DEBUG_PUSH |
| 4839 | switch (ctxt->instate) { |
| 4840 | case XML_PARSER_EOF: |
| 4841 | xmlGenericError(xmlGenericErrorContext, |
| 4842 | "HPP: try EOF\n"); break; |
| 4843 | case XML_PARSER_START: |
| 4844 | xmlGenericError(xmlGenericErrorContext, |
| 4845 | "HPP: try START\n"); break; |
| 4846 | case XML_PARSER_MISC: |
| 4847 | xmlGenericError(xmlGenericErrorContext, |
| 4848 | "HPP: try MISC\n");break; |
| 4849 | case XML_PARSER_COMMENT: |
| 4850 | xmlGenericError(xmlGenericErrorContext, |
| 4851 | "HPP: try COMMENT\n");break; |
| 4852 | case XML_PARSER_PROLOG: |
| 4853 | xmlGenericError(xmlGenericErrorContext, |
| 4854 | "HPP: try PROLOG\n");break; |
| 4855 | case XML_PARSER_START_TAG: |
| 4856 | xmlGenericError(xmlGenericErrorContext, |
| 4857 | "HPP: try START_TAG\n");break; |
| 4858 | case XML_PARSER_CONTENT: |
| 4859 | xmlGenericError(xmlGenericErrorContext, |
| 4860 | "HPP: try CONTENT\n");break; |
| 4861 | case XML_PARSER_CDATA_SECTION: |
| 4862 | xmlGenericError(xmlGenericErrorContext, |
| 4863 | "HPP: try CDATA_SECTION\n");break; |
| 4864 | case XML_PARSER_END_TAG: |
| 4865 | xmlGenericError(xmlGenericErrorContext, |
| 4866 | "HPP: try END_TAG\n");break; |
| 4867 | case XML_PARSER_ENTITY_DECL: |
| 4868 | xmlGenericError(xmlGenericErrorContext, |
| 4869 | "HPP: try ENTITY_DECL\n");break; |
| 4870 | case XML_PARSER_ENTITY_VALUE: |
| 4871 | xmlGenericError(xmlGenericErrorContext, |
| 4872 | "HPP: try ENTITY_VALUE\n");break; |
| 4873 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 4874 | xmlGenericError(xmlGenericErrorContext, |
| 4875 | "HPP: try ATTRIBUTE_VALUE\n");break; |
| 4876 | case XML_PARSER_DTD: |
| 4877 | xmlGenericError(xmlGenericErrorContext, |
| 4878 | "HPP: try DTD\n");break; |
| 4879 | case XML_PARSER_EPILOG: |
| 4880 | xmlGenericError(xmlGenericErrorContext, |
| 4881 | "HPP: try EPILOG\n");break; |
| 4882 | case XML_PARSER_PI: |
| 4883 | xmlGenericError(xmlGenericErrorContext, |
| 4884 | "HPP: try PI\n");break; |
| 4885 | case XML_PARSER_SYSTEM_LITERAL: |
| 4886 | xmlGenericError(xmlGenericErrorContext, |
| 4887 | "HPP: try SYSTEM_LITERAL\n");break; |
| 4888 | } |
| 4889 | #endif |
| 4890 | |
| 4891 | while (1) { |
| 4892 | |
| 4893 | in = ctxt->input; |
| 4894 | if (in == NULL) break; |
| 4895 | if (in->buf == NULL) |
| 4896 | avail = in->length - (in->cur - in->base); |
| 4897 | else |
| 4898 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4899 | if ((avail == 0) && (terminate)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 4900 | htmlAutoCloseOnEnd(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 4901 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4902 | /* |
| 4903 | * SAX: end of the document processing. |
| 4904 | */ |
| 4905 | ctxt->instate = XML_PARSER_EOF; |
| 4906 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 4907 | ctxt->sax->endDocument(ctxt->userData); |
| 4908 | } |
| 4909 | } |
| 4910 | if (avail < 1) |
| 4911 | goto done; |
Daniel Veillard | 45269b8 | 2003-04-22 13:21:57 +0000 | [diff] [blame] | 4912 | cur = in->cur[0]; |
| 4913 | if (cur == 0) { |
| 4914 | SKIP(1); |
| 4915 | continue; |
| 4916 | } |
| 4917 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4918 | switch (ctxt->instate) { |
| 4919 | case XML_PARSER_EOF: |
| 4920 | /* |
| 4921 | * Document parsing is done ! |
| 4922 | */ |
| 4923 | goto done; |
| 4924 | case XML_PARSER_START: |
| 4925 | /* |
| 4926 | * Very first chars read from the document flow. |
| 4927 | */ |
| 4928 | cur = in->cur[0]; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4929 | if (IS_BLANK_CH(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4930 | SKIP_BLANKS; |
| 4931 | if (in->buf == NULL) |
| 4932 | avail = in->length - (in->cur - in->base); |
| 4933 | else |
| 4934 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4935 | } |
| 4936 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 4937 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 4938 | &xmlDefaultSAXLocator); |
| 4939 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 4940 | (!ctxt->disableSAX)) |
| 4941 | ctxt->sax->startDocument(ctxt->userData); |
| 4942 | |
| 4943 | cur = in->cur[0]; |
| 4944 | next = in->cur[1]; |
| 4945 | if ((cur == '<') && (next == '!') && |
| 4946 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 4947 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 4948 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 4949 | (UPP(8) == 'E')) { |
| 4950 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 4951 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4952 | goto done; |
| 4953 | #ifdef DEBUG_PUSH |
| 4954 | xmlGenericError(xmlGenericErrorContext, |
| 4955 | "HPP: Parsing internal subset\n"); |
| 4956 | #endif |
| 4957 | htmlParseDocTypeDecl(ctxt); |
| 4958 | ctxt->instate = XML_PARSER_PROLOG; |
| 4959 | #ifdef DEBUG_PUSH |
| 4960 | xmlGenericError(xmlGenericErrorContext, |
| 4961 | "HPP: entering PROLOG\n"); |
| 4962 | #endif |
| 4963 | } else { |
| 4964 | ctxt->instate = XML_PARSER_MISC; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4965 | #ifdef DEBUG_PUSH |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 4966 | xmlGenericError(xmlGenericErrorContext, |
| 4967 | "HPP: entering MISC\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4968 | #endif |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 4969 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | case XML_PARSER_MISC: |
| 4972 | SKIP_BLANKS; |
| 4973 | if (in->buf == NULL) |
| 4974 | avail = in->length - (in->cur - in->base); |
| 4975 | else |
| 4976 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 4977 | if (avail < 2) |
| 4978 | goto done; |
| 4979 | cur = in->cur[0]; |
| 4980 | next = in->cur[1]; |
| 4981 | if ((cur == '<') && (next == '!') && |
| 4982 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 4983 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 4984 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4985 | goto done; |
| 4986 | #ifdef DEBUG_PUSH |
| 4987 | xmlGenericError(xmlGenericErrorContext, |
| 4988 | "HPP: Parsing Comment\n"); |
| 4989 | #endif |
| 4990 | htmlParseComment(ctxt); |
| 4991 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4992 | } else if ((cur == '<') && (next == '?')) { |
| 4993 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 4994 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 4995 | goto done; |
| 4996 | #ifdef DEBUG_PUSH |
| 4997 | xmlGenericError(xmlGenericErrorContext, |
| 4998 | "HPP: Parsing PI\n"); |
| 4999 | #endif |
| 5000 | htmlParsePI(ctxt); |
| 5001 | ctxt->instate = XML_PARSER_MISC; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5002 | } else if ((cur == '<') && (next == '!') && |
| 5003 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 5004 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 5005 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 5006 | (UPP(8) == 'E')) { |
| 5007 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5008 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5009 | goto done; |
| 5010 | #ifdef DEBUG_PUSH |
| 5011 | xmlGenericError(xmlGenericErrorContext, |
| 5012 | "HPP: Parsing internal subset\n"); |
| 5013 | #endif |
| 5014 | htmlParseDocTypeDecl(ctxt); |
| 5015 | ctxt->instate = XML_PARSER_PROLOG; |
| 5016 | #ifdef DEBUG_PUSH |
| 5017 | xmlGenericError(xmlGenericErrorContext, |
| 5018 | "HPP: entering PROLOG\n"); |
| 5019 | #endif |
| 5020 | } else if ((cur == '<') && (next == '!') && |
| 5021 | (avail < 9)) { |
| 5022 | goto done; |
| 5023 | } else { |
| 5024 | ctxt->instate = XML_PARSER_START_TAG; |
| 5025 | #ifdef DEBUG_PUSH |
| 5026 | xmlGenericError(xmlGenericErrorContext, |
| 5027 | "HPP: entering START_TAG\n"); |
| 5028 | #endif |
| 5029 | } |
| 5030 | break; |
| 5031 | case XML_PARSER_PROLOG: |
| 5032 | SKIP_BLANKS; |
| 5033 | if (in->buf == NULL) |
| 5034 | avail = in->length - (in->cur - in->base); |
| 5035 | else |
| 5036 | avail = in->buf->buffer->use - (in->cur - in->base); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5037 | if (avail < 2) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5038 | goto done; |
| 5039 | cur = in->cur[0]; |
| 5040 | next = in->cur[1]; |
| 5041 | if ((cur == '<') && (next == '!') && |
| 5042 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 5043 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5044 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | goto done; |
| 5046 | #ifdef DEBUG_PUSH |
| 5047 | xmlGenericError(xmlGenericErrorContext, |
| 5048 | "HPP: Parsing Comment\n"); |
| 5049 | #endif |
| 5050 | htmlParseComment(ctxt); |
| 5051 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5052 | } else if ((cur == '<') && (next == '?')) { |
| 5053 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5054 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5055 | goto done; |
| 5056 | #ifdef DEBUG_PUSH |
| 5057 | xmlGenericError(xmlGenericErrorContext, |
| 5058 | "HPP: Parsing PI\n"); |
| 5059 | #endif |
| 5060 | htmlParsePI(ctxt); |
| 5061 | ctxt->instate = XML_PARSER_PROLOG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5062 | } else if ((cur == '<') && (next == '!') && |
| 5063 | (avail < 4)) { |
| 5064 | goto done; |
| 5065 | } else { |
| 5066 | ctxt->instate = XML_PARSER_START_TAG; |
| 5067 | #ifdef DEBUG_PUSH |
| 5068 | xmlGenericError(xmlGenericErrorContext, |
| 5069 | "HPP: entering START_TAG\n"); |
| 5070 | #endif |
| 5071 | } |
| 5072 | break; |
| 5073 | case XML_PARSER_EPILOG: |
| 5074 | if (in->buf == NULL) |
| 5075 | avail = in->length - (in->cur - in->base); |
| 5076 | else |
| 5077 | avail = in->buf->buffer->use - (in->cur - in->base); |
| 5078 | if (avail < 1) |
| 5079 | goto done; |
| 5080 | cur = in->cur[0]; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5081 | if (IS_BLANK_CH(cur)) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5082 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5083 | goto done; |
| 5084 | } |
| 5085 | if (avail < 2) |
| 5086 | goto done; |
| 5087 | next = in->cur[1]; |
| 5088 | if ((cur == '<') && (next == '!') && |
| 5089 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 5090 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5091 | (htmlParseLookupSequence(ctxt, '-', '-', '>', 1, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5092 | goto done; |
| 5093 | #ifdef DEBUG_PUSH |
| 5094 | xmlGenericError(xmlGenericErrorContext, |
| 5095 | "HPP: Parsing Comment\n"); |
| 5096 | #endif |
| 5097 | htmlParseComment(ctxt); |
| 5098 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5099 | } else if ((cur == '<') && (next == '?')) { |
| 5100 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5101 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5102 | goto done; |
| 5103 | #ifdef DEBUG_PUSH |
| 5104 | xmlGenericError(xmlGenericErrorContext, |
| 5105 | "HPP: Parsing PI\n"); |
| 5106 | #endif |
| 5107 | htmlParsePI(ctxt); |
| 5108 | ctxt->instate = XML_PARSER_EPILOG; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5109 | } else if ((cur == '<') && (next == '!') && |
| 5110 | (avail < 4)) { |
| 5111 | goto done; |
| 5112 | } else { |
| 5113 | ctxt->errNo = XML_ERR_DOCUMENT_END; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5114 | ctxt->wellFormed = 0; |
| 5115 | ctxt->instate = XML_PARSER_EOF; |
| 5116 | #ifdef DEBUG_PUSH |
| 5117 | xmlGenericError(xmlGenericErrorContext, |
| 5118 | "HPP: entering EOF\n"); |
| 5119 | #endif |
| 5120 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 5121 | ctxt->sax->endDocument(ctxt->userData); |
| 5122 | goto done; |
| 5123 | } |
| 5124 | break; |
| 5125 | case XML_PARSER_START_TAG: { |
Daniel Veillard | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 5126 | const xmlChar *name; |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 5127 | int failed; |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 5128 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5129 | |
| 5130 | if (avail < 2) |
| 5131 | goto done; |
| 5132 | cur = in->cur[0]; |
| 5133 | if (cur != '<') { |
| 5134 | ctxt->instate = XML_PARSER_CONTENT; |
| 5135 | #ifdef DEBUG_PUSH |
| 5136 | xmlGenericError(xmlGenericErrorContext, |
| 5137 | "HPP: entering CONTENT\n"); |
| 5138 | #endif |
| 5139 | break; |
| 5140 | } |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 5141 | if (in->cur[1] == '/') { |
| 5142 | ctxt->instate = XML_PARSER_END_TAG; |
| 5143 | ctxt->checkIndex = 0; |
| 5144 | #ifdef DEBUG_PUSH |
| 5145 | xmlGenericError(xmlGenericErrorContext, |
| 5146 | "HPP: entering END_TAG\n"); |
| 5147 | #endif |
| 5148 | break; |
| 5149 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5150 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5151 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5152 | goto done; |
| 5153 | |
Daniel Veillard | 597f1c1 | 2005-07-03 23:00:18 +0000 | [diff] [blame] | 5154 | failed = htmlParseStartTag(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5155 | name = ctxt->name; |
Daniel Veillard | 35fcbb8 | 2008-03-12 21:43:39 +0000 | [diff] [blame] | 5156 | if ((failed == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5157 | (name == NULL)) { |
| 5158 | if (CUR == '>') |
| 5159 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5160 | break; |
| 5161 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5162 | |
| 5163 | /* |
| 5164 | * Lookup the info for that element. |
| 5165 | */ |
| 5166 | info = htmlTagLookup(name); |
| 5167 | if (info == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5168 | htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG, |
| 5169 | "Tag %s invalid\n", name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5173 | * Check for an Empty Element labeled the XML/SGML way |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5174 | */ |
| 5175 | if ((CUR == '/') && (NXT(1) == '>')) { |
| 5176 | SKIP(2); |
| 5177 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 5178 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 5179 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5180 | ctxt->instate = XML_PARSER_CONTENT; |
| 5181 | #ifdef DEBUG_PUSH |
| 5182 | xmlGenericError(xmlGenericErrorContext, |
| 5183 | "HPP: entering CONTENT\n"); |
| 5184 | #endif |
| 5185 | break; |
| 5186 | } |
| 5187 | |
| 5188 | if (CUR == '>') { |
| 5189 | NEXT; |
| 5190 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5191 | htmlParseErr(ctxt, XML_ERR_GT_REQUIRED, |
| 5192 | "Couldn't find end of Start Tag %s\n", |
| 5193 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5194 | |
| 5195 | /* |
| 5196 | * end of parsing of this node. |
| 5197 | */ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5198 | if (xmlStrEqual(name, ctxt->name)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5199 | nodePop(ctxt); |
Daniel Veillard | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 5200 | htmlnamePop(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5201 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | |
| 5203 | ctxt->instate = XML_PARSER_CONTENT; |
| 5204 | #ifdef DEBUG_PUSH |
| 5205 | xmlGenericError(xmlGenericErrorContext, |
| 5206 | "HPP: entering CONTENT\n"); |
| 5207 | #endif |
| 5208 | break; |
| 5209 | } |
| 5210 | |
| 5211 | /* |
| 5212 | * Check for an Empty Element from DTD definition |
| 5213 | */ |
| 5214 | if ((info != NULL) && (info->empty)) { |
| 5215 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) |
| 5216 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 5217 | htmlnamePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5218 | } |
| 5219 | ctxt->instate = XML_PARSER_CONTENT; |
| 5220 | #ifdef DEBUG_PUSH |
| 5221 | xmlGenericError(xmlGenericErrorContext, |
| 5222 | "HPP: entering CONTENT\n"); |
| 5223 | #endif |
| 5224 | break; |
| 5225 | } |
| 5226 | case XML_PARSER_CONTENT: { |
| 5227 | long cons; |
| 5228 | /* |
| 5229 | * Handle preparsed entities and charRef |
| 5230 | */ |
| 5231 | if (ctxt->token != 0) { |
| 5232 | xmlChar chr[2] = { 0 , 0 } ; |
| 5233 | |
| 5234 | chr[0] = (xmlChar) ctxt->token; |
| 5235 | htmlCheckParagraph(ctxt); |
| 5236 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) |
| 5237 | ctxt->sax->characters(ctxt->userData, chr, 1); |
| 5238 | ctxt->token = 0; |
| 5239 | ctxt->checkIndex = 0; |
| 5240 | } |
| 5241 | if ((avail == 1) && (terminate)) { |
| 5242 | cur = in->cur[0]; |
| 5243 | if ((cur != '<') && (cur != '&')) { |
| 5244 | if (ctxt->sax != NULL) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5245 | if (IS_BLANK_CH(cur)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5246 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 5247 | ctxt->sax->ignorableWhitespace( |
| 5248 | ctxt->userData, &cur, 1); |
| 5249 | } else { |
| 5250 | htmlCheckParagraph(ctxt); |
| 5251 | if (ctxt->sax->characters != NULL) |
| 5252 | ctxt->sax->characters( |
| 5253 | ctxt->userData, &cur, 1); |
| 5254 | } |
| 5255 | } |
| 5256 | ctxt->token = 0; |
| 5257 | ctxt->checkIndex = 0; |
Daniel Veillard | bc6e1a3 | 2002-11-18 15:07:25 +0000 | [diff] [blame] | 5258 | in->cur++; |
William M. Brack | 1633d18 | 2001-10-05 15:41:19 +0000 | [diff] [blame] | 5259 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5260 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | } |
| 5262 | if (avail < 2) |
| 5263 | goto done; |
| 5264 | cur = in->cur[0]; |
| 5265 | next = in->cur[1]; |
| 5266 | cons = ctxt->nbChars; |
| 5267 | if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) || |
| 5268 | (xmlStrEqual(ctxt->name, BAD_CAST"style"))) { |
| 5269 | /* |
| 5270 | * Handle SCRIPT/STYLE separately |
| 5271 | */ |
Daniel Veillard | 68716a7 | 2006-10-16 09:32:17 +0000 | [diff] [blame] | 5272 | if (!terminate) { |
| 5273 | int idx; |
| 5274 | xmlChar val; |
| 5275 | |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5276 | idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0, 1); |
Daniel Veillard | 68716a7 | 2006-10-16 09:32:17 +0000 | [diff] [blame] | 5277 | if (idx < 0) |
| 5278 | goto done; |
| 5279 | val = in->cur[idx + 2]; |
| 5280 | if (val == 0) /* bad cut of input */ |
| 5281 | goto done; |
| 5282 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5283 | htmlParseScript(ctxt); |
| 5284 | if ((cur == '<') && (next == '/')) { |
| 5285 | ctxt->instate = XML_PARSER_END_TAG; |
| 5286 | ctxt->checkIndex = 0; |
| 5287 | #ifdef DEBUG_PUSH |
| 5288 | xmlGenericError(xmlGenericErrorContext, |
| 5289 | "HPP: entering END_TAG\n"); |
| 5290 | #endif |
| 5291 | break; |
| 5292 | } |
| 5293 | } else { |
| 5294 | /* |
| 5295 | * Sometimes DOCTYPE arrives in the middle of the document |
| 5296 | */ |
| 5297 | if ((cur == '<') && (next == '!') && |
| 5298 | (UPP(2) == 'D') && (UPP(3) == 'O') && |
| 5299 | (UPP(4) == 'C') && (UPP(5) == 'T') && |
| 5300 | (UPP(6) == 'Y') && (UPP(7) == 'P') && |
| 5301 | (UPP(8) == 'E')) { |
| 5302 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5303 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5304 | goto done; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5305 | htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR, |
| 5306 | "Misplaced DOCTYPE declaration\n", |
| 5307 | BAD_CAST "DOCTYPE" , NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5308 | htmlParseDocTypeDecl(ctxt); |
| 5309 | } else if ((cur == '<') && (next == '!') && |
| 5310 | (in->cur[2] == '-') && (in->cur[3] == '-')) { |
| 5311 | if ((!terminate) && |
Daniel Veillard | 97e0188 | 2003-07-30 18:59:19 +0000 | [diff] [blame] | 5312 | (htmlParseLookupSequence( |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5313 | ctxt, '-', '-', '>', 1, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5314 | goto done; |
| 5315 | #ifdef DEBUG_PUSH |
| 5316 | xmlGenericError(xmlGenericErrorContext, |
| 5317 | "HPP: Parsing Comment\n"); |
| 5318 | #endif |
| 5319 | htmlParseComment(ctxt); |
| 5320 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5321 | } else if ((cur == '<') && (next == '?')) { |
| 5322 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5323 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Daniel Veillard | fc484dd | 2004-10-22 14:34:23 +0000 | [diff] [blame] | 5324 | goto done; |
| 5325 | #ifdef DEBUG_PUSH |
| 5326 | xmlGenericError(xmlGenericErrorContext, |
| 5327 | "HPP: Parsing PI\n"); |
| 5328 | #endif |
| 5329 | htmlParsePI(ctxt); |
| 5330 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | } else if ((cur == '<') && (next == '!') && (avail < 4)) { |
| 5332 | goto done; |
| 5333 | } else if ((cur == '<') && (next == '/')) { |
| 5334 | ctxt->instate = XML_PARSER_END_TAG; |
| 5335 | ctxt->checkIndex = 0; |
| 5336 | #ifdef DEBUG_PUSH |
| 5337 | xmlGenericError(xmlGenericErrorContext, |
| 5338 | "HPP: entering END_TAG\n"); |
| 5339 | #endif |
| 5340 | break; |
| 5341 | } else if (cur == '<') { |
| 5342 | ctxt->instate = XML_PARSER_START_TAG; |
| 5343 | ctxt->checkIndex = 0; |
| 5344 | #ifdef DEBUG_PUSH |
| 5345 | xmlGenericError(xmlGenericErrorContext, |
| 5346 | "HPP: entering START_TAG\n"); |
| 5347 | #endif |
| 5348 | break; |
| 5349 | } else if (cur == '&') { |
| 5350 | if ((!terminate) && |
Markus Kull | 56a0303 | 2009-08-24 19:00:23 +0200 | [diff] [blame] | 5351 | (htmlParseLookupChars(ctxt, |
| 5352 | BAD_CAST "; >/", 4) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5353 | goto done; |
| 5354 | #ifdef DEBUG_PUSH |
| 5355 | xmlGenericError(xmlGenericErrorContext, |
| 5356 | "HPP: Parsing Reference\n"); |
| 5357 | #endif |
| 5358 | /* TODO: check generation of subtrees if noent !!! */ |
| 5359 | htmlParseReference(ctxt); |
| 5360 | } else { |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5361 | /* |
| 5362 | * check that the text sequence is complete |
| 5363 | * before handing out the data to the parser |
| 5364 | * to avoid problems with erroneous end of |
| 5365 | * data detection. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5366 | */ |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5367 | if ((!terminate) && |
Markus Kull | 56a0303 | 2009-08-24 19:00:23 +0200 | [diff] [blame] | 5368 | (htmlParseLookupChars(ctxt, BAD_CAST "<&", 2) < 0)) |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5369 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5370 | ctxt->checkIndex = 0; |
| 5371 | #ifdef DEBUG_PUSH |
| 5372 | xmlGenericError(xmlGenericErrorContext, |
| 5373 | "HPP: Parsing char data\n"); |
| 5374 | #endif |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5375 | htmlParseCharData(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5376 | } |
| 5377 | } |
| 5378 | if (cons == ctxt->nbChars) { |
| 5379 | if (ctxt->node != NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5380 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5381 | "detected an error in element content\n", |
| 5382 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5383 | } |
| 5384 | NEXT; |
| 5385 | break; |
| 5386 | } |
| 5387 | |
| 5388 | break; |
| 5389 | } |
| 5390 | case XML_PARSER_END_TAG: |
| 5391 | if (avail < 2) |
| 5392 | goto done; |
| 5393 | if ((!terminate) && |
Jiri Netolicky | 446e126 | 2009-08-07 17:05:36 +0200 | [diff] [blame] | 5394 | (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5395 | goto done; |
| 5396 | htmlParseEndTag(ctxt); |
| 5397 | if (ctxt->nameNr == 0) { |
| 5398 | ctxt->instate = XML_PARSER_EPILOG; |
| 5399 | } else { |
| 5400 | ctxt->instate = XML_PARSER_CONTENT; |
| 5401 | } |
| 5402 | ctxt->checkIndex = 0; |
| 5403 | #ifdef DEBUG_PUSH |
| 5404 | xmlGenericError(xmlGenericErrorContext, |
| 5405 | "HPP: entering CONTENT\n"); |
| 5406 | #endif |
| 5407 | break; |
| 5408 | case XML_PARSER_CDATA_SECTION: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5409 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5410 | "HPP: internal error, state == CDATA\n", |
| 5411 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5412 | ctxt->instate = XML_PARSER_CONTENT; |
| 5413 | ctxt->checkIndex = 0; |
| 5414 | #ifdef DEBUG_PUSH |
| 5415 | xmlGenericError(xmlGenericErrorContext, |
| 5416 | "HPP: entering CONTENT\n"); |
| 5417 | #endif |
| 5418 | break; |
| 5419 | case XML_PARSER_DTD: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5420 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5421 | "HPP: internal error, state == DTD\n", |
| 5422 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5423 | ctxt->instate = XML_PARSER_CONTENT; |
| 5424 | ctxt->checkIndex = 0; |
| 5425 | #ifdef DEBUG_PUSH |
| 5426 | xmlGenericError(xmlGenericErrorContext, |
| 5427 | "HPP: entering CONTENT\n"); |
| 5428 | #endif |
| 5429 | break; |
| 5430 | case XML_PARSER_COMMENT: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5431 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5432 | "HPP: internal error, state == COMMENT\n", |
| 5433 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | ctxt->instate = XML_PARSER_CONTENT; |
| 5435 | ctxt->checkIndex = 0; |
| 5436 | #ifdef DEBUG_PUSH |
| 5437 | xmlGenericError(xmlGenericErrorContext, |
| 5438 | "HPP: entering CONTENT\n"); |
| 5439 | #endif |
| 5440 | break; |
| 5441 | case XML_PARSER_PI: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5442 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5443 | "HPP: internal error, state == PI\n", |
| 5444 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5445 | ctxt->instate = XML_PARSER_CONTENT; |
| 5446 | ctxt->checkIndex = 0; |
| 5447 | #ifdef DEBUG_PUSH |
| 5448 | xmlGenericError(xmlGenericErrorContext, |
| 5449 | "HPP: entering CONTENT\n"); |
| 5450 | #endif |
| 5451 | break; |
| 5452 | case XML_PARSER_ENTITY_DECL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5453 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5454 | "HPP: internal error, state == ENTITY_DECL\n", |
| 5455 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5456 | ctxt->instate = XML_PARSER_CONTENT; |
| 5457 | ctxt->checkIndex = 0; |
| 5458 | #ifdef DEBUG_PUSH |
| 5459 | xmlGenericError(xmlGenericErrorContext, |
| 5460 | "HPP: entering CONTENT\n"); |
| 5461 | #endif |
| 5462 | break; |
| 5463 | case XML_PARSER_ENTITY_VALUE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5464 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5465 | "HPP: internal error, state == ENTITY_VALUE\n", |
| 5466 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5467 | ctxt->instate = XML_PARSER_CONTENT; |
| 5468 | ctxt->checkIndex = 0; |
| 5469 | #ifdef DEBUG_PUSH |
| 5470 | xmlGenericError(xmlGenericErrorContext, |
| 5471 | "HPP: entering DTD\n"); |
| 5472 | #endif |
| 5473 | break; |
| 5474 | case XML_PARSER_ATTRIBUTE_VALUE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5475 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5476 | "HPP: internal error, state == ATTRIBUTE_VALUE\n", |
| 5477 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5478 | ctxt->instate = XML_PARSER_START_TAG; |
| 5479 | ctxt->checkIndex = 0; |
| 5480 | #ifdef DEBUG_PUSH |
| 5481 | xmlGenericError(xmlGenericErrorContext, |
| 5482 | "HPP: entering START_TAG\n"); |
| 5483 | #endif |
| 5484 | break; |
| 5485 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5486 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5487 | "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n", |
| 5488 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5489 | ctxt->instate = XML_PARSER_CONTENT; |
| 5490 | ctxt->checkIndex = 0; |
| 5491 | #ifdef DEBUG_PUSH |
| 5492 | xmlGenericError(xmlGenericErrorContext, |
| 5493 | "HPP: entering CONTENT\n"); |
| 5494 | #endif |
| 5495 | break; |
| 5496 | case XML_PARSER_IGNORE: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5497 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5498 | "HPP: internal error, state == XML_PARSER_IGNORE\n", |
| 5499 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | ctxt->instate = XML_PARSER_CONTENT; |
| 5501 | ctxt->checkIndex = 0; |
| 5502 | #ifdef DEBUG_PUSH |
| 5503 | xmlGenericError(xmlGenericErrorContext, |
| 5504 | "HPP: entering CONTENT\n"); |
| 5505 | #endif |
| 5506 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5507 | case XML_PARSER_PUBLIC_LITERAL: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5508 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5509 | "HPP: internal error, state == XML_PARSER_LITERAL\n", |
| 5510 | NULL, NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5511 | ctxt->instate = XML_PARSER_CONTENT; |
| 5512 | ctxt->checkIndex = 0; |
| 5513 | #ifdef DEBUG_PUSH |
| 5514 | xmlGenericError(xmlGenericErrorContext, |
| 5515 | "HPP: entering CONTENT\n"); |
| 5516 | #endif |
| 5517 | break; |
| 5518 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5519 | } |
| 5520 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5521 | done: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5522 | if ((avail == 0) && (terminate)) { |
Daniel Veillard | a3bfca5 | 2001-04-12 15:42:58 +0000 | [diff] [blame] | 5523 | htmlAutoCloseOnEnd(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5524 | if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5525 | /* |
| 5526 | * SAX: end of the document processing. |
| 5527 | */ |
| 5528 | ctxt->instate = XML_PARSER_EOF; |
| 5529 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 5530 | ctxt->sax->endDocument(ctxt->userData); |
| 5531 | } |
| 5532 | } |
| 5533 | if ((ctxt->myDoc != NULL) && |
| 5534 | ((terminate) || (ctxt->instate == XML_PARSER_EOF) || |
| 5535 | (ctxt->instate == XML_PARSER_EPILOG))) { |
| 5536 | xmlDtdPtr dtd; |
| 5537 | dtd = xmlGetIntSubset(ctxt->myDoc); |
| 5538 | if (dtd == NULL) |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5539 | ctxt->myDoc->intSubset = |
| 5540 | xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5541 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
| 5542 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
| 5543 | } |
| 5544 | #ifdef DEBUG_PUSH |
| 5545 | xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret); |
| 5546 | #endif |
| 5547 | return(ret); |
| 5548 | } |
| 5549 | |
| 5550 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5551 | * htmlParseChunk: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5552 | * @ctxt: an HTML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5553 | * @chunk: an char array |
| 5554 | * @size: the size in byte of the chunk |
| 5555 | * @terminate: last chunk indicator |
| 5556 | * |
| 5557 | * Parse a Chunk of memory |
| 5558 | * |
| 5559 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 5560 | */ |
| 5561 | int |
| 5562 | htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 5563 | int terminate) { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5564 | if ((ctxt == NULL) || (ctxt->input == NULL)) { |
| 5565 | htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5566 | "htmlParseChunk: context error\n", NULL, NULL); |
| 5567 | return(XML_ERR_INTERNAL_ERROR); |
| 5568 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5569 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 5570 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 5571 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 5572 | int cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | d2755a8 | 2005-08-07 23:42:39 +0000 | [diff] [blame] | 5573 | int res; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5574 | |
| 5575 | res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | d2755a8 | 2005-08-07 23:42:39 +0000 | [diff] [blame] | 5576 | if (res < 0) { |
| 5577 | ctxt->errNo = XML_PARSER_EOF; |
| 5578 | ctxt->disableSAX = 1; |
| 5579 | return (XML_PARSER_EOF); |
| 5580 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5581 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 5582 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | d2755a8 | 2005-08-07 23:42:39 +0000 | [diff] [blame] | 5583 | ctxt->input->end = |
| 5584 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5585 | #ifdef DEBUG_PUSH |
| 5586 | xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); |
| 5587 | #endif |
| 5588 | |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5589 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5590 | if ((terminate) || (ctxt->input->buf->buffer->use > 80)) |
| 5591 | htmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5592 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5593 | } else if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5594 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 5595 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 5596 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 5597 | (in->raw != NULL)) { |
| 5598 | int nbchars; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5599 | |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5600 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 5601 | if (nbchars < 0) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5602 | htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, |
| 5603 | "encoder error\n", NULL, NULL); |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5604 | return(XML_ERR_INVALID_ENCODING); |
| 5605 | } |
| 5606 | } |
| 5607 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5608 | } |
Daniel Veillard | 14f752c | 2003-08-09 11:44:50 +0000 | [diff] [blame] | 5609 | htmlParseTryOrFinish(ctxt, terminate); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5610 | if (terminate) { |
| 5611 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 5612 | (ctxt->instate != XML_PARSER_EPILOG) && |
| 5613 | (ctxt->instate != XML_PARSER_MISC)) { |
| 5614 | ctxt->errNo = XML_ERR_DOCUMENT_END; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5615 | ctxt->wellFormed = 0; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5616 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5617 | if (ctxt->instate != XML_PARSER_EOF) { |
| 5618 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 5619 | ctxt->sax->endDocument(ctxt->userData); |
| 5620 | } |
| 5621 | ctxt->instate = XML_PARSER_EOF; |
| 5622 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5623 | return((xmlParserErrors) ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | /************************************************************************ |
| 5627 | * * |
| 5628 | * User entry points * |
| 5629 | * * |
| 5630 | ************************************************************************/ |
| 5631 | |
| 5632 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5633 | * htmlCreatePushParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5634 | * @sax: a SAX handler |
| 5635 | * @user_data: The user data returned on SAX callbacks |
| 5636 | * @chunk: a pointer to an array of chars |
| 5637 | * @size: number of chars in the array |
| 5638 | * @filename: an optional file name or URI |
| 5639 | * @enc: an optional encoding |
| 5640 | * |
| 5641 | * Create a parser context for using the HTML parser in push mode |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5642 | * The value of @filename is used for fetching external entities |
| 5643 | * and error/warning reports. |
| 5644 | * |
| 5645 | * Returns the new parser context or NULL |
| 5646 | */ |
| 5647 | htmlParserCtxtPtr |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5648 | htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5649 | const char *chunk, int size, const char *filename, |
| 5650 | xmlCharEncoding enc) { |
| 5651 | htmlParserCtxtPtr ctxt; |
| 5652 | htmlParserInputPtr inputStream; |
| 5653 | xmlParserInputBufferPtr buf; |
| 5654 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5655 | xmlInitParser(); |
| 5656 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | buf = xmlAllocParserInputBuffer(enc); |
| 5658 | if (buf == NULL) return(NULL); |
| 5659 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5660 | ctxt = htmlNewParserCtxt(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5661 | if (ctxt == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5662 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | return(NULL); |
| 5664 | } |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 5665 | if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder) |
| 5666 | ctxt->charset=XML_CHAR_ENCODING_UTF8; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5667 | if (sax != NULL) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 5668 | if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | xmlFree(ctxt->sax); |
| 5670 | ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler)); |
| 5671 | if (ctxt->sax == NULL) { |
| 5672 | xmlFree(buf); |
| 5673 | xmlFree(ctxt); |
| 5674 | return(NULL); |
| 5675 | } |
| 5676 | memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler)); |
| 5677 | if (user_data != NULL) |
| 5678 | ctxt->userData = user_data; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5679 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5680 | if (filename == NULL) { |
| 5681 | ctxt->directory = NULL; |
| 5682 | } else { |
| 5683 | ctxt->directory = xmlParserGetDirectory(filename); |
| 5684 | } |
| 5685 | |
| 5686 | inputStream = htmlNewInputStream(ctxt); |
| 5687 | if (inputStream == NULL) { |
| 5688 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 5689 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5690 | return(NULL); |
| 5691 | } |
| 5692 | |
| 5693 | if (filename == NULL) |
| 5694 | inputStream->filename = NULL; |
| 5695 | else |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5696 | inputStream->filename = (char *) |
| 5697 | xmlCanonicPath((const xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5698 | inputStream->buf = buf; |
| 5699 | inputStream->base = inputStream->buf->buffer->content; |
| 5700 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5701 | inputStream->end = |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5702 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5703 | |
| 5704 | inputPush(ctxt, inputStream); |
| 5705 | |
| 5706 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5707 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5708 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 5709 | int cur = ctxt->input->cur - ctxt->input->base; |
| 5710 | |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5711 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 5712 | |
| 5713 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 5714 | ctxt->input->cur = ctxt->input->base + cur; |
| 5715 | ctxt->input->end = |
| 5716 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5717 | #ifdef DEBUG_PUSH |
| 5718 | xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); |
| 5719 | #endif |
| 5720 | } |
Daniel Veillard | 68716a7 | 2006-10-16 09:32:17 +0000 | [diff] [blame] | 5721 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5722 | |
| 5723 | return(ctxt); |
| 5724 | } |
William M. Brack | 21e4ef2 | 2005-01-02 09:53:13 +0000 | [diff] [blame] | 5725 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5726 | |
| 5727 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5728 | * htmlSAXParseDoc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5729 | * @cur: a pointer to an array of xmlChar |
| 5730 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5731 | * @sax: the SAX handler block |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5732 | * @userData: if using SAX, this pointer will be provided on callbacks. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5733 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5734 | * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks |
| 5735 | * to handle parse events. If sax is NULL, fallback to the default DOM |
| 5736 | * behavior and return a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5737 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5738 | * Returns the resulting document tree unless SAX is NULL or the document is |
| 5739 | * not well formed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5740 | */ |
| 5741 | |
| 5742 | htmlDocPtr |
| 5743 | htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) { |
| 5744 | htmlDocPtr ret; |
| 5745 | htmlParserCtxtPtr ctxt; |
| 5746 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5747 | xmlInitParser(); |
| 5748 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5749 | if (cur == NULL) return(NULL); |
| 5750 | |
| 5751 | |
| 5752 | ctxt = htmlCreateDocParserCtxt(cur, encoding); |
| 5753 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5754 | if (sax != NULL) { |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 5755 | if (ctxt->sax != NULL) xmlFree (ctxt->sax); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5756 | ctxt->sax = sax; |
| 5757 | ctxt->userData = userData; |
| 5758 | } |
| 5759 | |
| 5760 | htmlParseDocument(ctxt); |
| 5761 | ret = ctxt->myDoc; |
| 5762 | if (sax != NULL) { |
| 5763 | ctxt->sax = NULL; |
| 5764 | ctxt->userData = NULL; |
| 5765 | } |
| 5766 | htmlFreeParserCtxt(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5767 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5768 | return(ret); |
| 5769 | } |
| 5770 | |
| 5771 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5772 | * htmlParseDoc: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5773 | * @cur: a pointer to an array of xmlChar |
| 5774 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5775 | * |
| 5776 | * parse an HTML in-memory document and build a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5777 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5778 | * Returns the resulting document tree |
| 5779 | */ |
| 5780 | |
| 5781 | htmlDocPtr |
| 5782 | htmlParseDoc(xmlChar *cur, const char *encoding) { |
| 5783 | return(htmlSAXParseDoc(cur, encoding, NULL, NULL)); |
| 5784 | } |
| 5785 | |
| 5786 | |
| 5787 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5788 | * htmlCreateFileParserCtxt: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | * @filename: the filename |
| 5790 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5791 | * |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5792 | * Create a parser context for a file content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5793 | * Automatic support for ZLIB/Compress compressed document is provided |
| 5794 | * by default if found at compile-time. |
| 5795 | * |
| 5796 | * Returns the new parser context or NULL |
| 5797 | */ |
| 5798 | htmlParserCtxtPtr |
| 5799 | htmlCreateFileParserCtxt(const char *filename, const char *encoding) |
| 5800 | { |
| 5801 | htmlParserCtxtPtr ctxt; |
| 5802 | htmlParserInputPtr inputStream; |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5803 | char *canonicFilename; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5804 | /* htmlCharEncoding enc; */ |
| 5805 | xmlChar *content, *content_line = (xmlChar *) "charset="; |
| 5806 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 5807 | if (filename == NULL) |
| 5808 | return(NULL); |
| 5809 | |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5810 | ctxt = htmlNewParserCtxt(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5811 | if (ctxt == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5812 | return(NULL); |
| 5813 | } |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5814 | canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename); |
| 5815 | if (canonicFilename == NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 5816 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5817 | if (xmlDefaultSAXHandler.error != NULL) { |
| 5818 | xmlDefaultSAXHandler.error(NULL, "out of memory\n"); |
| 5819 | } |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 5820 | #endif |
Daniel Veillard | 104caa3 | 2003-05-13 22:54:05 +0000 | [diff] [blame] | 5821 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5822 | return(NULL); |
| 5823 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5824 | |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5825 | inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt); |
| 5826 | xmlFree(canonicFilename); |
| 5827 | if (inputStream == NULL) { |
| 5828 | xmlFreeParserCtxt(ctxt); |
| 5829 | return(NULL); |
| 5830 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5831 | |
| 5832 | inputPush(ctxt, inputStream); |
Daniel Veillard | e8b09e4 | 2003-05-13 22:14:13 +0000 | [diff] [blame] | 5833 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | /* set encoding */ |
| 5835 | if (encoding) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5836 | content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5837 | if (content) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5838 | strcpy ((char *)content, (char *)content_line); |
| 5839 | strcat ((char *)content, (char *)encoding); |
| 5840 | htmlCheckEncoding (ctxt, content); |
| 5841 | xmlFree (content); |
| 5842 | } |
| 5843 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5844 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | return(ctxt); |
| 5846 | } |
| 5847 | |
| 5848 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5849 | * htmlSAXParseFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5850 | * @filename: the filename |
| 5851 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5852 | * @sax: the SAX handler block |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5853 | * @userData: if using SAX, this pointer will be provided on callbacks. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5854 | * |
| 5855 | * parse an HTML file and build a tree. Automatic support for ZLIB/Compress |
| 5856 | * compressed document is provided by default if found at compile-time. |
| 5857 | * It use the given SAX function block to handle the parsing callback. |
| 5858 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 5859 | * |
Daniel Veillard | 4d65a1c | 2001-07-04 22:06:23 +0000 | [diff] [blame] | 5860 | * Returns the resulting document tree unless SAX is NULL or the document is |
| 5861 | * not well formed. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5862 | */ |
| 5863 | |
| 5864 | htmlDocPtr |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5865 | htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5866 | void *userData) { |
| 5867 | htmlDocPtr ret; |
| 5868 | htmlParserCtxtPtr ctxt; |
| 5869 | htmlSAXHandlerPtr oldsax = NULL; |
| 5870 | |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 5871 | xmlInitParser(); |
| 5872 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5873 | ctxt = htmlCreateFileParserCtxt(filename, encoding); |
| 5874 | if (ctxt == NULL) return(NULL); |
| 5875 | if (sax != NULL) { |
| 5876 | oldsax = ctxt->sax; |
| 5877 | ctxt->sax = sax; |
| 5878 | ctxt->userData = userData; |
| 5879 | } |
| 5880 | |
| 5881 | htmlParseDocument(ctxt); |
| 5882 | |
| 5883 | ret = ctxt->myDoc; |
| 5884 | if (sax != NULL) { |
| 5885 | ctxt->sax = oldsax; |
| 5886 | ctxt->userData = NULL; |
| 5887 | } |
| 5888 | htmlFreeParserCtxt(ctxt); |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5889 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5890 | return(ret); |
| 5891 | } |
| 5892 | |
| 5893 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 5894 | * htmlParseFile: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5895 | * @filename: the filename |
| 5896 | * @encoding: a free form C string describing the HTML document encoding, or NULL |
| 5897 | * |
| 5898 | * parse an HTML file and build a tree. Automatic support for ZLIB/Compress |
| 5899 | * compressed document is provided by default if found at compile-time. |
| 5900 | * |
| 5901 | * Returns the resulting document tree |
| 5902 | */ |
| 5903 | |
| 5904 | htmlDocPtr |
| 5905 | htmlParseFile(const char *filename, const char *encoding) { |
| 5906 | return(htmlSAXParseFile(filename, encoding, NULL, NULL)); |
| 5907 | } |
| 5908 | |
| 5909 | /** |
| 5910 | * htmlHandleOmittedElem: |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 5911 | * @val: int 0 or 1 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5912 | * |
| 5913 | * Set and return the previous value for handling HTML omitted tags. |
| 5914 | * |
| 5915 | * Returns the last value for 0 for no handling, 1 for auto insertion. |
| 5916 | */ |
| 5917 | |
| 5918 | int |
| 5919 | htmlHandleOmittedElem(int val) { |
| 5920 | int old = htmlOmittedDefaultValue; |
| 5921 | |
| 5922 | htmlOmittedDefaultValue = val; |
| 5923 | return(old); |
| 5924 | } |
| 5925 | |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 5926 | /** |
| 5927 | * htmlElementAllowedHere: |
| 5928 | * @parent: HTML parent element |
| 5929 | * @elt: HTML element |
| 5930 | * |
| 5931 | * Checks whether an HTML element may be a direct child of a parent element. |
| 5932 | * Note - doesn't check for deprecated elements |
| 5933 | * |
| 5934 | * Returns 1 if allowed; 0 otherwise. |
| 5935 | */ |
| 5936 | int |
| 5937 | htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) { |
| 5938 | const char** p ; |
| 5939 | |
| 5940 | if ( ! elt || ! parent || ! parent->subelts ) |
| 5941 | return 0 ; |
| 5942 | |
| 5943 | for ( p = parent->subelts; *p; ++p ) |
| 5944 | if ( !xmlStrcmp((const xmlChar *)*p, elt) ) |
| 5945 | return 1 ; |
| 5946 | |
| 5947 | return 0 ; |
| 5948 | } |
| 5949 | /** |
| 5950 | * htmlElementStatusHere: |
| 5951 | * @parent: HTML parent element |
| 5952 | * @elt: HTML element |
| 5953 | * |
| 5954 | * Checks whether an HTML element may be a direct child of a parent element. |
| 5955 | * and if so whether it is valid or deprecated. |
| 5956 | * |
| 5957 | * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
| 5958 | */ |
| 5959 | htmlStatus |
| 5960 | htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) { |
| 5961 | if ( ! parent || ! elt ) |
| 5962 | return HTML_INVALID ; |
| 5963 | if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) ) |
| 5964 | return HTML_INVALID ; |
| 5965 | |
| 5966 | return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ; |
| 5967 | } |
| 5968 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 5969 | * htmlAttrAllowed: |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 5970 | * @elt: HTML element |
| 5971 | * @attr: HTML attribute |
| 5972 | * @legacy: whether to allow deprecated attributes |
| 5973 | * |
| 5974 | * Checks whether an attribute is valid for an element |
| 5975 | * Has full knowledge of Required and Deprecated attributes |
| 5976 | * |
| 5977 | * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID |
| 5978 | */ |
| 5979 | htmlStatus |
| 5980 | htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) { |
| 5981 | const char** p ; |
| 5982 | |
| 5983 | if ( !elt || ! attr ) |
| 5984 | return HTML_INVALID ; |
| 5985 | |
| 5986 | if ( elt->attrs_req ) |
| 5987 | for ( p = elt->attrs_req; *p; ++p) |
| 5988 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5989 | return HTML_REQUIRED ; |
| 5990 | |
| 5991 | if ( elt->attrs_opt ) |
| 5992 | for ( p = elt->attrs_opt; *p; ++p) |
| 5993 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5994 | return HTML_VALID ; |
| 5995 | |
| 5996 | if ( legacy && elt->attrs_depr ) |
| 5997 | for ( p = elt->attrs_depr; *p; ++p) |
| 5998 | if ( !xmlStrcmp((const xmlChar*)*p, attr) ) |
| 5999 | return HTML_DEPRECATED ; |
| 6000 | |
| 6001 | return HTML_INVALID ; |
| 6002 | } |
| 6003 | /** |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 6004 | * htmlNodeStatus: |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6005 | * @node: an htmlNodePtr in a tree |
| 6006 | * @legacy: whether to allow deprecated elements (YES is faster here |
Daniel Veillard | 930dfb6 | 2003-02-05 10:17:38 +0000 | [diff] [blame] | 6007 | * for Element nodes) |
| 6008 | * |
| 6009 | * Checks whether the tree node is valid. Experimental (the author |
| 6010 | * only uses the HTML enhancements in a SAX parser) |
| 6011 | * |
| 6012 | * Return: for Element nodes, a return from htmlElementAllowedHere (if |
| 6013 | * legacy allowed) or htmlElementStatusHere (otherwise). |
| 6014 | * for Attribute nodes, a return from htmlAttrAllowed |
| 6015 | * for other nodes, HTML_NA (no checks performed) |
| 6016 | */ |
| 6017 | htmlStatus |
| 6018 | htmlNodeStatus(const htmlNodePtr node, int legacy) { |
| 6019 | if ( ! node ) |
| 6020 | return HTML_INVALID ; |
| 6021 | |
| 6022 | switch ( node->type ) { |
| 6023 | case XML_ELEMENT_NODE: |
| 6024 | return legacy |
| 6025 | ? ( htmlElementAllowedHere ( |
| 6026 | htmlTagLookup(node->parent->name) , node->name |
| 6027 | ) ? HTML_VALID : HTML_INVALID ) |
| 6028 | : htmlElementStatusHere( |
| 6029 | htmlTagLookup(node->parent->name) , |
| 6030 | htmlTagLookup(node->name) ) |
| 6031 | ; |
| 6032 | case XML_ATTRIBUTE_NODE: |
| 6033 | return htmlAttrAllowed( |
| 6034 | htmlTagLookup(node->parent->name) , node->name, legacy) ; |
| 6035 | default: return HTML_NA ; |
| 6036 | } |
| 6037 | } |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6038 | /************************************************************************ |
| 6039 | * * |
| 6040 | * New set (2.6.0) of simpler and more flexible APIs * |
| 6041 | * * |
| 6042 | ************************************************************************/ |
| 6043 | /** |
| 6044 | * DICT_FREE: |
| 6045 | * @str: a string |
| 6046 | * |
| 6047 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 6048 | * current scope |
| 6049 | */ |
| 6050 | #define DICT_FREE(str) \ |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6051 | if ((str) && ((!dict) || \ |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6052 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 6053 | xmlFree((char *)(str)); |
| 6054 | |
| 6055 | /** |
| 6056 | * htmlCtxtReset: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6057 | * @ctxt: an HTML parser context |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6058 | * |
| 6059 | * Reset a parser context |
| 6060 | */ |
| 6061 | void |
| 6062 | htmlCtxtReset(htmlParserCtxtPtr ctxt) |
| 6063 | { |
| 6064 | xmlParserInputPtr input; |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 6065 | xmlDictPtr dict; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6066 | |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 6067 | if (ctxt == NULL) |
| 6068 | return; |
| 6069 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6070 | xmlInitParser(); |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 6071 | dict = ctxt->dict; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6072 | |
| 6073 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 6074 | xmlFreeInputStream(input); |
| 6075 | } |
| 6076 | ctxt->inputNr = 0; |
| 6077 | ctxt->input = NULL; |
| 6078 | |
| 6079 | ctxt->spaceNr = 0; |
Daniel Veillard | a521d28 | 2004-11-09 14:59:59 +0000 | [diff] [blame] | 6080 | if (ctxt->spaceTab != NULL) { |
| 6081 | ctxt->spaceTab[0] = -1; |
| 6082 | ctxt->space = &ctxt->spaceTab[0]; |
| 6083 | } else { |
| 6084 | ctxt->space = NULL; |
| 6085 | } |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6086 | |
| 6087 | |
| 6088 | ctxt->nodeNr = 0; |
| 6089 | ctxt->node = NULL; |
| 6090 | |
| 6091 | ctxt->nameNr = 0; |
| 6092 | ctxt->name = NULL; |
| 6093 | |
| 6094 | DICT_FREE(ctxt->version); |
| 6095 | ctxt->version = NULL; |
| 6096 | DICT_FREE(ctxt->encoding); |
| 6097 | ctxt->encoding = NULL; |
| 6098 | DICT_FREE(ctxt->directory); |
| 6099 | ctxt->directory = NULL; |
| 6100 | DICT_FREE(ctxt->extSubURI); |
| 6101 | ctxt->extSubURI = NULL; |
| 6102 | DICT_FREE(ctxt->extSubSystem); |
| 6103 | ctxt->extSubSystem = NULL; |
| 6104 | if (ctxt->myDoc != NULL) |
| 6105 | xmlFreeDoc(ctxt->myDoc); |
| 6106 | ctxt->myDoc = NULL; |
| 6107 | |
| 6108 | ctxt->standalone = -1; |
| 6109 | ctxt->hasExternalSubset = 0; |
| 6110 | ctxt->hasPErefs = 0; |
| 6111 | ctxt->html = 1; |
| 6112 | ctxt->external = 0; |
| 6113 | ctxt->instate = XML_PARSER_START; |
| 6114 | ctxt->token = 0; |
| 6115 | |
| 6116 | ctxt->wellFormed = 1; |
| 6117 | ctxt->nsWellFormed = 1; |
| 6118 | ctxt->valid = 1; |
| 6119 | ctxt->vctxt.userData = ctxt; |
| 6120 | ctxt->vctxt.error = xmlParserValidityError; |
| 6121 | ctxt->vctxt.warning = xmlParserValidityWarning; |
| 6122 | ctxt->record_info = 0; |
| 6123 | ctxt->nbChars = 0; |
| 6124 | ctxt->checkIndex = 0; |
| 6125 | ctxt->inSubset = 0; |
| 6126 | ctxt->errNo = XML_ERR_OK; |
| 6127 | ctxt->depth = 0; |
Daniel Veillard | 772869f | 2006-11-08 09:16:56 +0000 | [diff] [blame] | 6128 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6129 | ctxt->catalogs = NULL; |
| 6130 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 6131 | |
| 6132 | if (ctxt->attsDefault != NULL) { |
| 6133 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 6134 | ctxt->attsDefault = NULL; |
| 6135 | } |
| 6136 | if (ctxt->attsSpecial != NULL) { |
| 6137 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 6138 | ctxt->attsSpecial = NULL; |
| 6139 | } |
| 6140 | } |
| 6141 | |
| 6142 | /** |
| 6143 | * htmlCtxtUseOptions: |
| 6144 | * @ctxt: an HTML parser context |
| 6145 | * @options: a combination of htmlParserOption(s) |
| 6146 | * |
| 6147 | * Applies the options to the parser context |
| 6148 | * |
| 6149 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 6150 | * in case of error. |
| 6151 | */ |
| 6152 | int |
| 6153 | htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options) |
| 6154 | { |
Daniel Veillard | a03e365 | 2004-11-02 18:45:30 +0000 | [diff] [blame] | 6155 | if (ctxt == NULL) |
| 6156 | return(-1); |
| 6157 | |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6158 | if (options & HTML_PARSE_NOWARNING) { |
| 6159 | ctxt->sax->warning = NULL; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6160 | ctxt->vctxt.warning = NULL; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6161 | options -= XML_PARSE_NOWARNING; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6162 | ctxt->options |= XML_PARSE_NOWARNING; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6163 | } |
| 6164 | if (options & HTML_PARSE_NOERROR) { |
| 6165 | ctxt->sax->error = NULL; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6166 | ctxt->vctxt.error = NULL; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6167 | ctxt->sax->fatalError = NULL; |
| 6168 | options -= XML_PARSE_NOERROR; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6169 | ctxt->options |= XML_PARSE_NOERROR; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6170 | } |
| 6171 | if (options & HTML_PARSE_PEDANTIC) { |
| 6172 | ctxt->pedantic = 1; |
| 6173 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6174 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6175 | } else |
| 6176 | ctxt->pedantic = 0; |
| 6177 | if (options & XML_PARSE_NOBLANKS) { |
| 6178 | ctxt->keepBlanks = 0; |
| 6179 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 6180 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | d3669b2 | 2004-02-25 12:34:55 +0000 | [diff] [blame] | 6181 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6182 | } else |
| 6183 | ctxt->keepBlanks = 1; |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 6184 | if (options & HTML_PARSE_RECOVER) { |
| 6185 | ctxt->recovery = 1; |
Daniel Veillard | af616a7 | 2006-10-17 20:18:39 +0000 | [diff] [blame] | 6186 | options -= HTML_PARSE_RECOVER; |
Daniel Veillard | ea4b0ba | 2005-08-23 16:06:08 +0000 | [diff] [blame] | 6187 | } else |
| 6188 | ctxt->recovery = 0; |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 6189 | if (options & HTML_PARSE_COMPACT) { |
| 6190 | ctxt->options |= HTML_PARSE_COMPACT; |
| 6191 | options -= HTML_PARSE_COMPACT; |
| 6192 | } |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6193 | if (options & XML_PARSE_HUGE) { |
| 6194 | ctxt->options |= XML_PARSE_HUGE; |
| 6195 | options -= XML_PARSE_HUGE; |
| 6196 | } |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6197 | ctxt->dictNames = 0; |
| 6198 | return (options); |
| 6199 | } |
| 6200 | |
| 6201 | /** |
| 6202 | * htmlDoRead: |
| 6203 | * @ctxt: an HTML parser context |
| 6204 | * @URL: the base URL to use for the document |
| 6205 | * @encoding: the document encoding, or NULL |
| 6206 | * @options: a combination of htmlParserOption(s) |
| 6207 | * @reuse: keep the context for reuse |
| 6208 | * |
| 6209 | * Common front-end for the htmlRead functions |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6210 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6211 | * Returns the resulting document tree or NULL |
| 6212 | */ |
| 6213 | static htmlDocPtr |
| 6214 | htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 6215 | int options, int reuse) |
| 6216 | { |
| 6217 | htmlDocPtr ret; |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6218 | |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6219 | htmlCtxtUseOptions(ctxt, options); |
| 6220 | ctxt->html = 1; |
| 6221 | if (encoding != NULL) { |
| 6222 | xmlCharEncodingHandlerPtr hdlr; |
| 6223 | |
| 6224 | hdlr = xmlFindCharEncodingHandler(encoding); |
Daniel Veillard | 4cc67bb | 2008-08-29 19:58:23 +0000 | [diff] [blame] | 6225 | if (hdlr != NULL) { |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6226 | xmlSwitchToEncoding(ctxt, hdlr); |
Daniel Veillard | 4cc67bb | 2008-08-29 19:58:23 +0000 | [diff] [blame] | 6227 | if (ctxt->input->encoding != NULL) |
| 6228 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 6229 | ctxt->input->encoding = xmlStrdup((xmlChar *)encoding); |
| 6230 | } |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6231 | } |
| 6232 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 6233 | (ctxt->input->filename == NULL)) |
| 6234 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
| 6235 | htmlParseDocument(ctxt); |
| 6236 | ret = ctxt->myDoc; |
| 6237 | ctxt->myDoc = NULL; |
| 6238 | if (!reuse) { |
| 6239 | if ((ctxt->dictNames) && |
| 6240 | (ret != NULL) && |
| 6241 | (ret->dict == ctxt->dict)) |
| 6242 | ctxt->dict = NULL; |
| 6243 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6244 | } |
| 6245 | return (ret); |
| 6246 | } |
| 6247 | |
| 6248 | /** |
| 6249 | * htmlReadDoc: |
| 6250 | * @cur: a pointer to a zero terminated string |
| 6251 | * @URL: the base URL to use for the document |
| 6252 | * @encoding: the document encoding, or NULL |
| 6253 | * @options: a combination of htmlParserOption(s) |
| 6254 | * |
| 6255 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6256 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6257 | * Returns the resulting document tree |
| 6258 | */ |
| 6259 | htmlDocPtr |
| 6260 | htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
| 6261 | { |
| 6262 | htmlParserCtxtPtr ctxt; |
| 6263 | |
| 6264 | if (cur == NULL) |
| 6265 | return (NULL); |
| 6266 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6267 | xmlInitParser(); |
Daniel Veillard | 8a82ae1 | 2006-10-17 20:04:10 +0000 | [diff] [blame] | 6268 | ctxt = htmlCreateDocParserCtxt(cur, NULL); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6269 | if (ctxt == NULL) |
| 6270 | return (NULL); |
| 6271 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 6272 | } |
| 6273 | |
| 6274 | /** |
| 6275 | * htmlReadFile: |
| 6276 | * @filename: a file or URL |
| 6277 | * @encoding: the document encoding, or NULL |
| 6278 | * @options: a combination of htmlParserOption(s) |
| 6279 | * |
| 6280 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6281 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6282 | * Returns the resulting document tree |
| 6283 | */ |
| 6284 | htmlDocPtr |
| 6285 | htmlReadFile(const char *filename, const char *encoding, int options) |
| 6286 | { |
| 6287 | htmlParserCtxtPtr ctxt; |
| 6288 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6289 | xmlInitParser(); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6290 | ctxt = htmlCreateFileParserCtxt(filename, encoding); |
| 6291 | if (ctxt == NULL) |
| 6292 | return (NULL); |
| 6293 | return (htmlDoRead(ctxt, NULL, NULL, options, 0)); |
| 6294 | } |
| 6295 | |
| 6296 | /** |
| 6297 | * htmlReadMemory: |
| 6298 | * @buffer: a pointer to a char array |
| 6299 | * @size: the size of the array |
| 6300 | * @URL: the base URL to use for the document |
| 6301 | * @encoding: the document encoding, or NULL |
| 6302 | * @options: a combination of htmlParserOption(s) |
| 6303 | * |
| 6304 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6305 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6306 | * Returns the resulting document tree |
| 6307 | */ |
| 6308 | htmlDocPtr |
| 6309 | htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options) |
| 6310 | { |
| 6311 | htmlParserCtxtPtr ctxt; |
| 6312 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6313 | xmlInitParser(); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6314 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 6315 | if (ctxt == NULL) |
| 6316 | return (NULL); |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6317 | htmlDefaultSAXHandlerInit(); |
William M. Brack | d43cdcd | 2004-08-03 15:13:29 +0000 | [diff] [blame] | 6318 | if (ctxt->sax != NULL) |
| 6319 | memcpy(ctxt->sax, &htmlDefaultSAXHandler, sizeof(xmlSAXHandlerV1)); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6320 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 6321 | } |
| 6322 | |
| 6323 | /** |
| 6324 | * htmlReadFd: |
| 6325 | * @fd: an open file descriptor |
| 6326 | * @URL: the base URL to use for the document |
| 6327 | * @encoding: the document encoding, or NULL |
| 6328 | * @options: a combination of htmlParserOption(s) |
| 6329 | * |
| 6330 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6331 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6332 | * Returns the resulting document tree |
| 6333 | */ |
| 6334 | htmlDocPtr |
| 6335 | htmlReadFd(int fd, const char *URL, const char *encoding, int options) |
| 6336 | { |
| 6337 | htmlParserCtxtPtr ctxt; |
| 6338 | xmlParserInputBufferPtr input; |
| 6339 | xmlParserInputPtr stream; |
| 6340 | |
| 6341 | if (fd < 0) |
| 6342 | return (NULL); |
| 6343 | |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6344 | xmlInitParser(); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6345 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 6346 | if (input == NULL) |
| 6347 | return (NULL); |
| 6348 | ctxt = xmlNewParserCtxt(); |
| 6349 | if (ctxt == NULL) { |
| 6350 | xmlFreeParserInputBuffer(input); |
| 6351 | return (NULL); |
| 6352 | } |
| 6353 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6354 | if (stream == NULL) { |
| 6355 | xmlFreeParserInputBuffer(input); |
| 6356 | xmlFreeParserCtxt(ctxt); |
| 6357 | return (NULL); |
| 6358 | } |
| 6359 | inputPush(ctxt, stream); |
| 6360 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 6361 | } |
| 6362 | |
| 6363 | /** |
| 6364 | * htmlReadIO: |
| 6365 | * @ioread: an I/O read function |
| 6366 | * @ioclose: an I/O close function |
| 6367 | * @ioctx: an I/O handler |
| 6368 | * @URL: the base URL to use for the document |
| 6369 | * @encoding: the document encoding, or NULL |
| 6370 | * @options: a combination of htmlParserOption(s) |
| 6371 | * |
| 6372 | * parse an HTML document from I/O functions and source and build a tree. |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6373 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6374 | * Returns the resulting document tree |
| 6375 | */ |
| 6376 | htmlDocPtr |
| 6377 | htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 6378 | void *ioctx, const char *URL, const char *encoding, int options) |
| 6379 | { |
| 6380 | htmlParserCtxtPtr ctxt; |
| 6381 | xmlParserInputBufferPtr input; |
| 6382 | xmlParserInputPtr stream; |
| 6383 | |
| 6384 | if (ioread == NULL) |
| 6385 | return (NULL); |
Daniel Veillard | f1a27c6 | 2006-10-13 22:33:03 +0000 | [diff] [blame] | 6386 | xmlInitParser(); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6387 | |
| 6388 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 6389 | XML_CHAR_ENCODING_NONE); |
| 6390 | if (input == NULL) |
| 6391 | return (NULL); |
Daniel Veillard | 8a82ae1 | 2006-10-17 20:04:10 +0000 | [diff] [blame] | 6392 | ctxt = htmlNewParserCtxt(); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6393 | if (ctxt == NULL) { |
| 6394 | xmlFreeParserInputBuffer(input); |
| 6395 | return (NULL); |
| 6396 | } |
| 6397 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6398 | if (stream == NULL) { |
| 6399 | xmlFreeParserInputBuffer(input); |
| 6400 | xmlFreeParserCtxt(ctxt); |
| 6401 | return (NULL); |
| 6402 | } |
| 6403 | inputPush(ctxt, stream); |
| 6404 | return (htmlDoRead(ctxt, URL, encoding, options, 0)); |
| 6405 | } |
| 6406 | |
| 6407 | /** |
| 6408 | * htmlCtxtReadDoc: |
| 6409 | * @ctxt: an HTML parser context |
| 6410 | * @cur: a pointer to a zero terminated string |
| 6411 | * @URL: the base URL to use for the document |
| 6412 | * @encoding: the document encoding, or NULL |
| 6413 | * @options: a combination of htmlParserOption(s) |
| 6414 | * |
| 6415 | * parse an XML in-memory document and build a tree. |
| 6416 | * This reuses the existing @ctxt parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6417 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6418 | * Returns the resulting document tree |
| 6419 | */ |
| 6420 | htmlDocPtr |
| 6421 | htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar * cur, |
| 6422 | const char *URL, const char *encoding, int options) |
| 6423 | { |
| 6424 | xmlParserInputPtr stream; |
| 6425 | |
| 6426 | if (cur == NULL) |
| 6427 | return (NULL); |
| 6428 | if (ctxt == NULL) |
| 6429 | return (NULL); |
| 6430 | |
| 6431 | htmlCtxtReset(ctxt); |
| 6432 | |
| 6433 | stream = xmlNewStringInputStream(ctxt, cur); |
| 6434 | if (stream == NULL) { |
| 6435 | return (NULL); |
| 6436 | } |
| 6437 | inputPush(ctxt, stream); |
| 6438 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6439 | } |
| 6440 | |
| 6441 | /** |
| 6442 | * htmlCtxtReadFile: |
| 6443 | * @ctxt: an HTML parser context |
| 6444 | * @filename: a file or URL |
| 6445 | * @encoding: the document encoding, or NULL |
| 6446 | * @options: a combination of htmlParserOption(s) |
| 6447 | * |
| 6448 | * parse an XML file from the filesystem or the network. |
| 6449 | * This reuses the existing @ctxt parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6450 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6451 | * Returns the resulting document tree |
| 6452 | */ |
| 6453 | htmlDocPtr |
| 6454 | htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename, |
| 6455 | const char *encoding, int options) |
| 6456 | { |
| 6457 | xmlParserInputPtr stream; |
| 6458 | |
| 6459 | if (filename == NULL) |
| 6460 | return (NULL); |
| 6461 | if (ctxt == NULL) |
| 6462 | return (NULL); |
| 6463 | |
| 6464 | htmlCtxtReset(ctxt); |
| 6465 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 6466 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6467 | if (stream == NULL) { |
| 6468 | return (NULL); |
| 6469 | } |
| 6470 | inputPush(ctxt, stream); |
| 6471 | return (htmlDoRead(ctxt, NULL, encoding, options, 1)); |
| 6472 | } |
| 6473 | |
| 6474 | /** |
| 6475 | * htmlCtxtReadMemory: |
| 6476 | * @ctxt: an HTML parser context |
| 6477 | * @buffer: a pointer to a char array |
| 6478 | * @size: the size of the array |
| 6479 | * @URL: the base URL to use for the document |
| 6480 | * @encoding: the document encoding, or NULL |
| 6481 | * @options: a combination of htmlParserOption(s) |
| 6482 | * |
| 6483 | * parse an XML in-memory document and build a tree. |
| 6484 | * This reuses the existing @ctxt parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6485 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6486 | * Returns the resulting document tree |
| 6487 | */ |
| 6488 | htmlDocPtr |
| 6489 | htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size, |
| 6490 | const char *URL, const char *encoding, int options) |
| 6491 | { |
| 6492 | xmlParserInputBufferPtr input; |
| 6493 | xmlParserInputPtr stream; |
| 6494 | |
| 6495 | if (ctxt == NULL) |
| 6496 | return (NULL); |
| 6497 | if (buffer == NULL) |
| 6498 | return (NULL); |
| 6499 | |
| 6500 | htmlCtxtReset(ctxt); |
| 6501 | |
| 6502 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 6503 | if (input == NULL) { |
| 6504 | return(NULL); |
| 6505 | } |
| 6506 | |
| 6507 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6508 | if (stream == NULL) { |
| 6509 | xmlFreeParserInputBuffer(input); |
| 6510 | return(NULL); |
| 6511 | } |
| 6512 | |
| 6513 | inputPush(ctxt, stream); |
| 6514 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6515 | } |
| 6516 | |
| 6517 | /** |
| 6518 | * htmlCtxtReadFd: |
| 6519 | * @ctxt: an HTML parser context |
| 6520 | * @fd: an open file descriptor |
| 6521 | * @URL: the base URL to use for the document |
| 6522 | * @encoding: the document encoding, or NULL |
| 6523 | * @options: a combination of htmlParserOption(s) |
| 6524 | * |
| 6525 | * parse an XML from a file descriptor and build a tree. |
| 6526 | * This reuses the existing @ctxt parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6527 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6528 | * Returns the resulting document tree |
| 6529 | */ |
| 6530 | htmlDocPtr |
| 6531 | htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd, |
| 6532 | const char *URL, const char *encoding, int options) |
| 6533 | { |
| 6534 | xmlParserInputBufferPtr input; |
| 6535 | xmlParserInputPtr stream; |
| 6536 | |
| 6537 | if (fd < 0) |
| 6538 | return (NULL); |
| 6539 | if (ctxt == NULL) |
| 6540 | return (NULL); |
| 6541 | |
| 6542 | htmlCtxtReset(ctxt); |
| 6543 | |
| 6544 | |
| 6545 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 6546 | if (input == NULL) |
| 6547 | return (NULL); |
| 6548 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6549 | if (stream == NULL) { |
| 6550 | xmlFreeParserInputBuffer(input); |
| 6551 | return (NULL); |
| 6552 | } |
| 6553 | inputPush(ctxt, stream); |
| 6554 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6555 | } |
| 6556 | |
| 6557 | /** |
| 6558 | * htmlCtxtReadIO: |
| 6559 | * @ctxt: an HTML parser context |
| 6560 | * @ioread: an I/O read function |
| 6561 | * @ioclose: an I/O close function |
| 6562 | * @ioctx: an I/O handler |
| 6563 | * @URL: the base URL to use for the document |
| 6564 | * @encoding: the document encoding, or NULL |
| 6565 | * @options: a combination of htmlParserOption(s) |
| 6566 | * |
| 6567 | * parse an HTML document from I/O functions and source and build a tree. |
| 6568 | * This reuses the existing @ctxt parser context |
Daniel Veillard | e77db16 | 2009-08-22 11:32:38 +0200 | [diff] [blame] | 6569 | * |
Daniel Veillard | 9475a35 | 2003-09-26 12:47:50 +0000 | [diff] [blame] | 6570 | * Returns the resulting document tree |
| 6571 | */ |
| 6572 | htmlDocPtr |
| 6573 | htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 6574 | xmlInputCloseCallback ioclose, void *ioctx, |
| 6575 | const char *URL, |
| 6576 | const char *encoding, int options) |
| 6577 | { |
| 6578 | xmlParserInputBufferPtr input; |
| 6579 | xmlParserInputPtr stream; |
| 6580 | |
| 6581 | if (ioread == NULL) |
| 6582 | return (NULL); |
| 6583 | if (ctxt == NULL) |
| 6584 | return (NULL); |
| 6585 | |
| 6586 | htmlCtxtReset(ctxt); |
| 6587 | |
| 6588 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 6589 | XML_CHAR_ENCODING_NONE); |
| 6590 | if (input == NULL) |
| 6591 | return (NULL); |
| 6592 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 6593 | if (stream == NULL) { |
| 6594 | xmlFreeParserInputBuffer(input); |
| 6595 | return (NULL); |
| 6596 | } |
| 6597 | inputPush(ctxt, stream); |
| 6598 | return (htmlDoRead(ctxt, URL, encoding, options, 1)); |
| 6599 | } |
| 6600 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 6601 | #define bottom_HTMLparser |
| 6602 | #include "elfgcchack.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6603 | #endif /* LIBXML_HTML_ENABLED */ |