Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * HTMLtree.c : implemetation of access function for an HTML tree. |
| 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 | |
| 9 | |
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 |
| 12 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 | #ifdef HAVE_CTYPE_H |
| 14 | #include <ctype.h> |
| 15 | #endif |
| 16 | #ifdef HAVE_STDLIB_H |
| 17 | #include <stdlib.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <libxml/xmlmemory.h> |
| 21 | #include <libxml/HTMLparser.h> |
| 22 | #include <libxml/HTMLtree.h> |
| 23 | #include <libxml/entities.h> |
| 24 | #include <libxml/valid.h> |
| 25 | #include <libxml/xmlerror.h> |
| 26 | #include <libxml/parserInternals.h> |
| 27 | |
| 28 | /************************************************************************ |
| 29 | * * |
| 30 | * Getting/Setting encoding meta tags * |
| 31 | * * |
| 32 | ************************************************************************/ |
| 33 | |
| 34 | /** |
| 35 | * htmlGetMetaEncoding: |
| 36 | * @doc: the document |
| 37 | * |
| 38 | * Encoding definition lookup in the Meta tags |
| 39 | * |
| 40 | * Returns the current encoding as flagged in the HTML source |
| 41 | */ |
| 42 | const xmlChar * |
| 43 | htmlGetMetaEncoding(htmlDocPtr doc) { |
| 44 | htmlNodePtr cur; |
| 45 | const xmlChar *content; |
| 46 | const xmlChar *encoding; |
| 47 | |
| 48 | if (doc == NULL) |
| 49 | return(NULL); |
| 50 | cur = doc->children; |
| 51 | |
| 52 | /* |
| 53 | * Search the html |
| 54 | */ |
| 55 | while (cur != NULL) { |
| 56 | if (cur->name != NULL) { |
| 57 | if (xmlStrEqual(cur->name, BAD_CAST"html")) |
| 58 | break; |
| 59 | if (xmlStrEqual(cur->name, BAD_CAST"head")) |
| 60 | goto found_head; |
| 61 | if (xmlStrEqual(cur->name, BAD_CAST"meta")) |
| 62 | goto found_meta; |
| 63 | } |
| 64 | cur = cur->next; |
| 65 | } |
| 66 | if (cur == NULL) |
| 67 | return(NULL); |
| 68 | cur = cur->children; |
| 69 | |
| 70 | /* |
| 71 | * Search the head |
| 72 | */ |
| 73 | while (cur != NULL) { |
| 74 | if (cur->name != NULL) { |
| 75 | if (xmlStrEqual(cur->name, BAD_CAST"head")) |
| 76 | break; |
| 77 | if (xmlStrEqual(cur->name, BAD_CAST"meta")) |
| 78 | goto found_meta; |
| 79 | } |
| 80 | cur = cur->next; |
| 81 | } |
| 82 | if (cur == NULL) |
| 83 | return(NULL); |
| 84 | found_head: |
| 85 | cur = cur->children; |
| 86 | |
| 87 | /* |
| 88 | * Search the meta elements |
| 89 | */ |
| 90 | found_meta: |
| 91 | while (cur != NULL) { |
| 92 | if (cur->name != NULL) { |
| 93 | if (xmlStrEqual(cur->name, BAD_CAST"meta")) { |
| 94 | xmlAttrPtr attr = cur->properties; |
| 95 | int http; |
| 96 | const xmlChar *value; |
| 97 | |
| 98 | content = NULL; |
| 99 | http = 0; |
| 100 | while (attr != NULL) { |
| 101 | if ((attr->children != NULL) && |
| 102 | (attr->children->type == XML_TEXT_NODE) && |
| 103 | (attr->children->next == NULL)) { |
| 104 | #ifndef XML_USE_BUFFER_CONTENT |
| 105 | value = attr->children->content; |
| 106 | #else |
| 107 | value = xmlBufferContent(attr->children->content); |
| 108 | #endif |
| 109 | if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv")) |
| 110 | && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) |
| 111 | http = 1; |
| 112 | else if ((value != NULL) |
| 113 | && (!xmlStrcasecmp(attr->name, BAD_CAST"content"))) |
| 114 | content = value; |
| 115 | if ((http != 0) && (content != NULL)) |
| 116 | goto found_content; |
| 117 | } |
| 118 | attr = attr->next; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | cur = cur->next; |
| 123 | } |
| 124 | return(NULL); |
| 125 | |
| 126 | found_content: |
| 127 | encoding = xmlStrstr(content, BAD_CAST"charset="); |
| 128 | if (encoding == NULL) |
| 129 | encoding = xmlStrstr(content, BAD_CAST"Charset="); |
| 130 | if (encoding == NULL) |
| 131 | encoding = xmlStrstr(content, BAD_CAST"CHARSET="); |
| 132 | if (encoding != NULL) { |
| 133 | encoding += 8; |
| 134 | } else { |
| 135 | encoding = xmlStrstr(content, BAD_CAST"charset ="); |
| 136 | if (encoding == NULL) |
| 137 | encoding = xmlStrstr(content, BAD_CAST"Charset ="); |
| 138 | if (encoding == NULL) |
| 139 | encoding = xmlStrstr(content, BAD_CAST"CHARSET ="); |
| 140 | if (encoding != NULL) |
| 141 | encoding += 9; |
| 142 | } |
| 143 | if (encoding != NULL) { |
| 144 | while ((*encoding == ' ') || (*encoding == '\t')) encoding++; |
| 145 | } |
| 146 | return(encoding); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * htmlSetMetaEncoding: |
| 151 | * @doc: the document |
| 152 | * @encoding: the encoding string |
| 153 | * |
| 154 | * Sets the current encoding in the Meta tags |
| 155 | * NOTE: this will not change the document content encoding, just |
| 156 | * the META flag associated. |
| 157 | * |
| 158 | * Returns 0 in case of success and -1 in case of error |
| 159 | */ |
| 160 | int |
| 161 | htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) { |
| 162 | htmlNodePtr cur, meta; |
| 163 | const xmlChar *content; |
| 164 | char newcontent[100]; |
| 165 | |
| 166 | |
| 167 | if (doc == NULL) |
| 168 | return(-1); |
| 169 | |
| 170 | if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 171 | snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s", |
| 172 | encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 173 | newcontent[sizeof(newcontent) - 1] = 0; |
| 174 | } |
| 175 | |
| 176 | cur = doc->children; |
| 177 | |
| 178 | /* |
| 179 | * Search the html |
| 180 | */ |
| 181 | while (cur != NULL) { |
| 182 | if (cur->name != NULL) { |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 183 | if (xmlStrcasecmp(cur->name, BAD_CAST"html") == 0) |
| 184 | break; |
| 185 | if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0) |
| 186 | goto found_head; |
| 187 | if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) |
| 188 | goto found_meta; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 189 | } |
| 190 | cur = cur->next; |
| 191 | } |
| 192 | if (cur == NULL) |
| 193 | return(-1); |
| 194 | cur = cur->children; |
| 195 | |
| 196 | /* |
| 197 | * Search the head |
| 198 | */ |
| 199 | while (cur != NULL) { |
| 200 | if (cur->name != NULL) { |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 201 | if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0) |
| 202 | break; |
| 203 | if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) |
| 204 | goto found_meta; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 205 | } |
| 206 | cur = cur->next; |
| 207 | } |
| 208 | if (cur == NULL) |
| 209 | return(-1); |
| 210 | found_head: |
| 211 | if (cur->children == NULL) { |
| 212 | if (encoding == NULL) |
| 213 | return(0); |
| 214 | meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL); |
| 215 | xmlAddChild(cur, meta); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 216 | xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent); |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 217 | xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 218 | return(0); |
| 219 | } |
| 220 | cur = cur->children; |
| 221 | |
| 222 | found_meta: |
| 223 | if (encoding != NULL) { |
| 224 | /* |
| 225 | * Create a new Meta element with the right aatributes |
| 226 | */ |
| 227 | |
| 228 | meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL); |
| 229 | xmlAddPrevSibling(cur, meta); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 230 | xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent); |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 231 | xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Search and destroy all the remaining the meta elements carrying |
| 236 | * encoding informations |
| 237 | */ |
| 238 | while (cur != NULL) { |
| 239 | if (cur->name != NULL) { |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 240 | if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 241 | xmlAttrPtr attr = cur->properties; |
| 242 | int http; |
| 243 | const xmlChar *value; |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 244 | int same_charset; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 245 | |
| 246 | content = NULL; |
| 247 | http = 0; |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 248 | same_charset = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 249 | while (attr != NULL) { |
| 250 | if ((attr->children != NULL) && |
| 251 | (attr->children->type == XML_TEXT_NODE) && |
| 252 | (attr->children->next == NULL)) { |
| 253 | #ifndef XML_USE_BUFFER_CONTENT |
| 254 | value = attr->children->content; |
| 255 | #else |
| 256 | value = xmlBufferContent(attr->children->content); |
| 257 | #endif |
| 258 | if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv")) |
| 259 | && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) |
| 260 | http = 1; |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 261 | else |
| 262 | { |
| 263 | if ((value != NULL) && |
| 264 | (!xmlStrcasecmp(attr->name, BAD_CAST"content"))) |
| 265 | content = value; |
| 266 | else |
| 267 | if ((!xmlStrcasecmp(attr->name, BAD_CAST"charset")) |
| 268 | && (!xmlStrcasecmp(value, encoding))) |
| 269 | same_charset = 1; |
| 270 | } |
| 271 | if ((http != 0) && (content != NULL) && (same_charset != 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | attr = attr->next; |
| 275 | } |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 276 | if ((http != 0) && (content != NULL) && (same_charset != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 277 | meta = cur; |
| 278 | cur = cur->next; |
| 279 | xmlUnlinkNode(meta); |
| 280 | xmlFreeNode(meta); |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | } |
| 286 | cur = cur->next; |
| 287 | } |
| 288 | return(0); |
| 289 | } |
| 290 | |
| 291 | /************************************************************************ |
| 292 | * * |
| 293 | * Dumping HTML tree content to a simple buffer * |
| 294 | * * |
| 295 | ************************************************************************/ |
| 296 | |
Daniel Veillard | c4f631d | 2001-06-14 11:11:59 +0000 | [diff] [blame] | 297 | void htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 298 | xmlNodePtr cur, const char *encoding, int format); |
| 299 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 300 | static void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 301 | htmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur, int format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * htmlDtdDump: |
| 305 | * @buf: the HTML buffer output |
| 306 | * @doc: the document |
| 307 | * |
| 308 | * Dump the HTML document DTD, if any. |
| 309 | */ |
| 310 | static void |
| 311 | htmlDtdDump(xmlBufferPtr buf, xmlDocPtr doc) { |
| 312 | xmlDtdPtr cur = doc->intSubset; |
| 313 | |
| 314 | if (cur == NULL) { |
| 315 | xmlGenericError(xmlGenericErrorContext, |
| 316 | "htmlDtdDump : no internal subset\n"); |
| 317 | return; |
| 318 | } |
| 319 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
| 320 | xmlBufferWriteCHAR(buf, cur->name); |
| 321 | if (cur->ExternalID != NULL) { |
| 322 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 323 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 324 | if (cur->SystemID != NULL) { |
| 325 | xmlBufferWriteChar(buf, " "); |
| 326 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
| 327 | } |
| 328 | } else if (cur->SystemID != NULL) { |
| 329 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 330 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
| 331 | } |
| 332 | xmlBufferWriteChar(buf, ">\n"); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * htmlAttrDump: |
| 337 | * @buf: the HTML buffer output |
| 338 | * @doc: the document |
| 339 | * @cur: the attribute pointer |
| 340 | * |
| 341 | * Dump an HTML attribute |
| 342 | */ |
| 343 | static void |
| 344 | htmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
| 345 | xmlChar *value; |
| 346 | |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 347 | /* |
| 348 | * TODO: The html output method should not escape a & character |
| 349 | * occurring in an attribute value immediately followed by |
| 350 | * a { character (see Section B.7.1 of the HTML 4.0 Recommendation). |
| 351 | */ |
| 352 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 353 | if (cur == NULL) { |
| 354 | xmlGenericError(xmlGenericErrorContext, |
| 355 | "htmlAttrDump : property == NULL\n"); |
| 356 | return; |
| 357 | } |
| 358 | xmlBufferWriteChar(buf, " "); |
| 359 | xmlBufferWriteCHAR(buf, cur->name); |
| 360 | if (cur->children != NULL) { |
| 361 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 362 | if (value) { |
| 363 | xmlBufferWriteChar(buf, "="); |
| 364 | xmlBufferWriteQuotedString(buf, value); |
| 365 | xmlFree(value); |
| 366 | } else { |
| 367 | xmlBufferWriteChar(buf, "=\"\""); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * htmlAttrListDump: |
| 374 | * @buf: the HTML buffer output |
| 375 | * @doc: the document |
| 376 | * @cur: the first attribute pointer |
| 377 | * |
| 378 | * Dump a list of HTML attributes |
| 379 | */ |
| 380 | static void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 381 | htmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, int format) { |
| 382 | int i = 0; |
| 383 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 384 | if (cur == NULL) { |
| 385 | xmlGenericError(xmlGenericErrorContext, |
| 386 | "htmlAttrListDump : property == NULL\n"); |
| 387 | return; |
| 388 | } |
| 389 | while (cur != NULL) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 390 | i++; |
| 391 | if ((format) && (i >= 5)) { |
| 392 | i = 0; |
| 393 | xmlBufferWriteChar(buf, "\n"); |
| 394 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 395 | htmlAttrDump(buf, doc, cur); |
| 396 | cur = cur->next; |
| 397 | } |
| 398 | } |
| 399 | |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 400 | static void |
| 401 | htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 402 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 403 | /** |
| 404 | * htmlNodeListDump: |
| 405 | * @buf: the HTML buffer output |
| 406 | * @doc: the document |
| 407 | * @cur: the first node |
| 408 | * |
| 409 | * Dump an HTML node list, recursive behaviour,children are printed too. |
| 410 | */ |
| 411 | static void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 412 | htmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 413 | if (cur == NULL) { |
| 414 | xmlGenericError(xmlGenericErrorContext, |
| 415 | "htmlNodeListDump : node == NULL\n"); |
| 416 | return; |
| 417 | } |
| 418 | while (cur != NULL) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 419 | htmlNodeDumpFormat(buf, doc, cur, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 420 | cur = cur->next; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 425 | * htmlNodeDumpFormat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 426 | * @buf: the HTML buffer output |
| 427 | * @doc: the document |
| 428 | * @cur: the current node |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 429 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 430 | * |
| 431 | * Dump an HTML node, recursive behaviour,children are printed too. |
| 432 | */ |
| 433 | void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 434 | htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 435 | int format) { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 436 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 437 | |
| 438 | if (cur == NULL) { |
| 439 | xmlGenericError(xmlGenericErrorContext, |
| 440 | "htmlNodeDump : node == NULL\n"); |
| 441 | return; |
| 442 | } |
| 443 | /* |
| 444 | * Special cases. |
| 445 | */ |
| 446 | if (cur->type == XML_DTD_NODE) |
| 447 | return; |
| 448 | if (cur->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 449 | htmlDocContentDump(buf, (xmlDocPtr) cur, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | if (cur->type == HTML_TEXT_NODE) { |
| 453 | if (cur->content != NULL) { |
Daniel Veillard | 6e93c4a | 2001-06-05 20:57:42 +0000 | [diff] [blame] | 454 | if (((cur->name == xmlStringText) || |
| 455 | (cur->name != xmlStringTextNoenc)) && |
| 456 | ((cur->parent == NULL) || |
| 457 | (!xmlStrEqual(cur->parent->name, BAD_CAST "script")))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 458 | xmlChar *buffer; |
| 459 | |
| 460 | #ifndef XML_USE_BUFFER_CONTENT |
| 461 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 462 | #else |
| 463 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 464 | xmlBufferContent(cur->content)); |
| 465 | #endif |
| 466 | if (buffer != NULL) { |
| 467 | xmlBufferWriteCHAR(buf, buffer); |
| 468 | xmlFree(buffer); |
| 469 | } |
| 470 | } else { |
| 471 | xmlBufferWriteCHAR(buf, cur->content); |
| 472 | } |
| 473 | } |
| 474 | return; |
| 475 | } |
| 476 | if (cur->type == HTML_COMMENT_NODE) { |
| 477 | if (cur->content != NULL) { |
| 478 | xmlBufferWriteChar(buf, "<!--"); |
| 479 | #ifndef XML_USE_BUFFER_CONTENT |
| 480 | xmlBufferWriteCHAR(buf, cur->content); |
| 481 | #else |
| 482 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 483 | #endif |
| 484 | xmlBufferWriteChar(buf, "-->"); |
| 485 | } |
| 486 | return; |
| 487 | } |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 488 | if (cur->type == HTML_PI_NODE) { |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 489 | if (cur->name == NULL) |
| 490 | return; |
| 491 | xmlBufferWriteChar(buf, "<?"); |
| 492 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 493 | if (cur->content != NULL) { |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 494 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 495 | #ifndef XML_USE_BUFFER_CONTENT |
| 496 | xmlBufferWriteCHAR(buf, cur->content); |
| 497 | #else |
| 498 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 499 | #endif |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 500 | } |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 501 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 502 | return; |
| 503 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 504 | if (cur->type == HTML_ENTITY_REF_NODE) { |
| 505 | xmlBufferWriteChar(buf, "&"); |
| 506 | xmlBufferWriteCHAR(buf, cur->name); |
| 507 | xmlBufferWriteChar(buf, ";"); |
| 508 | return; |
| 509 | } |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 510 | if (cur->type == HTML_PRESERVE_NODE) { |
| 511 | if (cur->content != NULL) { |
| 512 | #ifndef XML_USE_BUFFER_CONTENT |
| 513 | xmlBufferWriteCHAR(buf, cur->content); |
| 514 | #else |
| 515 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 516 | #endif |
| 517 | } |
| 518 | return; |
| 519 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 520 | |
| 521 | /* |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 522 | * Get specific HTML info for taht node. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 523 | */ |
| 524 | info = htmlTagLookup(cur->name); |
| 525 | |
| 526 | xmlBufferWriteChar(buf, "<"); |
| 527 | xmlBufferWriteCHAR(buf, cur->name); |
| 528 | if (cur->properties != NULL) |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 529 | htmlAttrListDump(buf, doc, cur->properties, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 530 | |
| 531 | if ((info != NULL) && (info->empty)) { |
| 532 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 533 | if ((format) && (info != NULL) && (!info->isinline) && |
| 534 | (cur->next != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 535 | if ((cur->next->type != HTML_TEXT_NODE) && |
| 536 | (cur->next->type != HTML_ENTITY_REF_NODE)) |
| 537 | xmlBufferWriteChar(buf, "\n"); |
| 538 | } |
| 539 | return; |
| 540 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 541 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 542 | (cur->children == NULL)) { |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 543 | if ((info != NULL) && (info->saveEndTag != 0) && |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 544 | (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) && |
| 545 | (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 546 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 547 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 548 | xmlBufferWriteChar(buf, "></"); |
| 549 | xmlBufferWriteCHAR(buf, cur->name); |
| 550 | xmlBufferWriteChar(buf, ">"); |
| 551 | } |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 552 | if ((format) && (info != NULL) && (!info->isinline) && |
| 553 | (cur->next != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 554 | if ((cur->next->type != HTML_TEXT_NODE) && |
| 555 | (cur->next->type != HTML_ENTITY_REF_NODE)) |
| 556 | xmlBufferWriteChar(buf, "\n"); |
| 557 | } |
| 558 | return; |
| 559 | } |
| 560 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 561 | if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 562 | xmlChar *buffer; |
| 563 | |
| 564 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 565 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 566 | #else |
Daniel Veillard | 083c266 | 2001-05-08 08:27:14 +0000 | [diff] [blame] | 567 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 568 | xmlBufferContent(cur->content)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 569 | #endif |
| 570 | if (buffer != NULL) { |
| 571 | xmlBufferWriteCHAR(buf, buffer); |
| 572 | xmlFree(buffer); |
| 573 | } |
| 574 | } |
| 575 | if (cur->children != NULL) { |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 576 | if ((format) && (info != NULL) && (!info->isinline) && |
| 577 | (cur->children->type != HTML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 578 | (cur->children->type != HTML_ENTITY_REF_NODE) && |
| 579 | (cur->children != cur->last)) |
| 580 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 581 | htmlNodeListDump(buf, doc, cur->children, format); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 582 | if ((format) && (info != NULL) && (!info->isinline) && |
| 583 | (cur->last->type != HTML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 584 | (cur->last->type != HTML_ENTITY_REF_NODE) && |
| 585 | (cur->children != cur->last)) |
| 586 | xmlBufferWriteChar(buf, "\n"); |
| 587 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 588 | xmlBufferWriteChar(buf, "</"); |
| 589 | xmlBufferWriteCHAR(buf, cur->name); |
| 590 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 591 | if ((format) && (info != NULL) && (!info->isinline) && |
| 592 | (cur->next != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 593 | if ((cur->next->type != HTML_TEXT_NODE) && |
| 594 | (cur->next->type != HTML_ENTITY_REF_NODE)) |
| 595 | xmlBufferWriteChar(buf, "\n"); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 600 | * htmlNodeDump: |
| 601 | * @buf: the HTML buffer output |
| 602 | * @doc: the document |
| 603 | * @cur: the current node |
| 604 | * |
| 605 | * Dump an HTML node, recursive behaviour,children are printed too, |
| 606 | * and formatting returns are added. |
| 607 | */ |
| 608 | void |
| 609 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) { |
| 610 | htmlNodeDumpFormat(buf, doc, cur, 1); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * htmlNodeDumpFileFormat: |
| 615 | * @out: the FILE pointer |
| 616 | * @doc: the document |
| 617 | * @cur: the current node |
| 618 | * @encoding: the document encoding |
| 619 | * @format: should formatting spaces been added |
| 620 | * |
| 621 | * Dump an HTML node, recursive behaviour,children are printed too. |
| 622 | * |
Daniel Veillard | c4f631d | 2001-06-14 11:11:59 +0000 | [diff] [blame] | 623 | * TODO: if encoding == NULL try to save in the doc encoding |
| 624 | * |
| 625 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 626 | */ |
Daniel Veillard | c4f631d | 2001-06-14 11:11:59 +0000 | [diff] [blame] | 627 | int |
| 628 | htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc, |
| 629 | xmlNodePtr cur, const char *encoding, int format) { |
| 630 | xmlOutputBufferPtr buf; |
| 631 | xmlCharEncodingHandlerPtr handler = NULL; |
| 632 | int ret; |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 633 | |
Daniel Veillard | c4f631d | 2001-06-14 11:11:59 +0000 | [diff] [blame] | 634 | if (encoding != NULL) { |
| 635 | xmlCharEncoding enc; |
| 636 | |
| 637 | enc = xmlParseCharEncoding(encoding); |
| 638 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 639 | handler = xmlFindCharEncodingHandler(encoding); |
| 640 | if (handler == NULL) |
| 641 | return(-1); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 647 | */ |
| 648 | if (handler == NULL) |
| 649 | handler = xmlFindCharEncodingHandler("HTML"); |
| 650 | if (handler == NULL) |
| 651 | handler = xmlFindCharEncodingHandler("ascii"); |
| 652 | |
| 653 | /* |
| 654 | * save the content to a temp buffer. |
| 655 | */ |
| 656 | buf = xmlOutputBufferCreateFile(out, handler); |
| 657 | if (buf == NULL) return(0); |
| 658 | |
| 659 | htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format); |
| 660 | |
| 661 | ret = xmlOutputBufferClose(buf); |
| 662 | return(ret); |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 666 | * htmlNodeDumpFile: |
| 667 | * @out: the FILE pointer |
| 668 | * @doc: the document |
| 669 | * @cur: the current node |
| 670 | * |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 671 | * Dump an HTML node, recursive behaviour,children are printed too, |
| 672 | * and formatting returns are added. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 673 | */ |
| 674 | void |
| 675 | htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 676 | htmlNodeDumpFileFormat(out, doc, cur, NULL, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
| 680 | * htmlDocContentDump: |
| 681 | * @buf: the HTML buffer output |
| 682 | * @cur: the document |
| 683 | * |
| 684 | * Dump an HTML document. |
| 685 | */ |
| 686 | static void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 687 | htmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 688 | int type; |
| 689 | |
| 690 | /* |
| 691 | * force to output the stuff as HTML, especially for entities |
| 692 | */ |
| 693 | type = cur->type; |
| 694 | cur->type = XML_HTML_DOCUMENT_NODE; |
| 695 | if (cur->intSubset != NULL) |
| 696 | htmlDtdDump(buf, cur); |
| 697 | else { |
| 698 | /* Default to HTML-4.0 transitionnal @@@@ */ |
| 699 | xmlBufferWriteChar(buf, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">"); |
| 700 | |
| 701 | } |
| 702 | if (cur->children != NULL) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 703 | htmlNodeListDump(buf, cur, cur->children, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 704 | } |
| 705 | xmlBufferWriteChar(buf, "\n"); |
| 706 | cur->type = (xmlElementType) type; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * htmlDocDumpMemory: |
| 711 | * @cur: the document |
| 712 | * @mem: OUT: the memory pointer |
Daniel Veillard | 2d70372 | 2001-05-30 18:32:34 +0000 | [diff] [blame] | 713 | * @size: OUT: the memory length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 714 | * |
| 715 | * Dump an HTML document in memory and return the xmlChar * and it's size. |
| 716 | * It's up to the caller to free the memory. |
| 717 | */ |
| 718 | void |
| 719 | htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 2d70372 | 2001-05-30 18:32:34 +0000 | [diff] [blame] | 720 | xmlOutputBufferPtr buf; |
| 721 | xmlCharEncodingHandlerPtr handler = NULL; |
| 722 | const char *encoding; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 723 | |
| 724 | if (cur == NULL) { |
| 725 | #ifdef DEBUG_TREE |
| 726 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 2d70372 | 2001-05-30 18:32:34 +0000 | [diff] [blame] | 727 | "htmlDocDumpMemory : document == NULL\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 728 | #endif |
| 729 | *mem = NULL; |
| 730 | *size = 0; |
| 731 | return; |
| 732 | } |
Daniel Veillard | 2d70372 | 2001-05-30 18:32:34 +0000 | [diff] [blame] | 733 | |
| 734 | encoding = (const char *) htmlGetMetaEncoding(cur); |
| 735 | |
| 736 | if (encoding != NULL) { |
| 737 | xmlCharEncoding enc; |
| 738 | |
| 739 | enc = xmlParseCharEncoding(encoding); |
| 740 | if (enc != cur->charset) { |
| 741 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 742 | /* |
| 743 | * Not supported yet |
| 744 | */ |
| 745 | *mem = NULL; |
| 746 | *size = 0; |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | handler = xmlFindCharEncodingHandler(encoding); |
| 751 | if (handler == NULL) { |
| 752 | *mem = NULL; |
| 753 | *size = 0; |
| 754 | return; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 761 | */ |
| 762 | if (handler == NULL) |
| 763 | handler = xmlFindCharEncodingHandler("HTML"); |
| 764 | if (handler == NULL) |
| 765 | handler = xmlFindCharEncodingHandler("ascii"); |
| 766 | |
| 767 | buf = xmlAllocOutputBuffer(handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 768 | if (buf == NULL) { |
| 769 | *mem = NULL; |
| 770 | *size = 0; |
| 771 | return; |
| 772 | } |
Daniel Veillard | 2d70372 | 2001-05-30 18:32:34 +0000 | [diff] [blame] | 773 | |
| 774 | htmlDocContentDumpOutput(buf, cur, NULL); |
| 775 | xmlOutputBufferFlush(buf); |
| 776 | if (buf->conv != NULL) { |
| 777 | *size = buf->conv->use; |
| 778 | *mem = xmlStrndup(buf->conv->content, *size); |
| 779 | } else { |
| 780 | *size = buf->buffer->use; |
| 781 | *mem = xmlStrndup(buf->buffer->content, *size); |
| 782 | } |
| 783 | (void)xmlOutputBufferClose(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
| 787 | /************************************************************************ |
| 788 | * * |
| 789 | * Dumping HTML tree content to an I/O output buffer * |
| 790 | * * |
| 791 | ************************************************************************/ |
| 792 | |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 793 | void |
| 794 | htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 795 | const char *encoding, int format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 796 | /** |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 797 | * htmlDtdDumpOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 798 | * @buf: the HTML buffer output |
| 799 | * @doc: the document |
| 800 | * @encoding: the encoding string |
| 801 | * |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 802 | * TODO: check whether encoding is needed |
| 803 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 804 | * Dump the HTML document DTD, if any. |
| 805 | */ |
| 806 | static void |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 807 | htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 808 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 809 | xmlDtdPtr cur = doc->intSubset; |
| 810 | |
| 811 | if (cur == NULL) { |
| 812 | xmlGenericError(xmlGenericErrorContext, |
| 813 | "htmlDtdDump : no internal subset\n"); |
| 814 | return; |
| 815 | } |
| 816 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 817 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 818 | if (cur->ExternalID != NULL) { |
| 819 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 820 | xmlBufferWriteQuotedString(buf->buffer, cur->ExternalID); |
| 821 | if (cur->SystemID != NULL) { |
| 822 | xmlOutputBufferWriteString(buf, " "); |
| 823 | xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); |
| 824 | } |
| 825 | } else if (cur->SystemID != NULL) { |
| 826 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 827 | xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); |
| 828 | } |
| 829 | xmlOutputBufferWriteString(buf, ">\n"); |
| 830 | } |
| 831 | |
| 832 | /** |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 833 | * htmlAttrDumpOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 834 | * @buf: the HTML buffer output |
| 835 | * @doc: the document |
| 836 | * @cur: the attribute pointer |
| 837 | * @encoding: the encoding string |
| 838 | * |
| 839 | * Dump an HTML attribute |
| 840 | */ |
| 841 | static void |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 842 | htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 843 | const char *encoding ATTRIBUTE_UNUSED) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 844 | xmlChar *value; |
| 845 | |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 846 | /* |
| 847 | * TODO: The html output method should not escape a & character |
| 848 | * occurring in an attribute value immediately followed by |
| 849 | * a { character (see Section B.7.1 of the HTML 4.0 Recommendation). |
| 850 | */ |
| 851 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 852 | if (cur == NULL) { |
| 853 | xmlGenericError(xmlGenericErrorContext, |
| 854 | "htmlAttrDump : property == NULL\n"); |
| 855 | return; |
| 856 | } |
| 857 | xmlOutputBufferWriteString(buf, " "); |
| 858 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 859 | if (cur->children != NULL) { |
| 860 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 861 | if (value) { |
| 862 | xmlOutputBufferWriteString(buf, "="); |
| 863 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 864 | xmlFree(value); |
| 865 | } else { |
| 866 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /** |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 872 | * htmlAttrListDumpOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 873 | * @buf: the HTML buffer output |
| 874 | * @doc: the document |
| 875 | * @cur: the first attribute pointer |
| 876 | * @encoding: the encoding string |
| 877 | * |
| 878 | * Dump a list of HTML attributes |
| 879 | */ |
| 880 | static void |
| 881 | htmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) { |
| 882 | if (cur == NULL) { |
| 883 | xmlGenericError(xmlGenericErrorContext, |
| 884 | "htmlAttrListDump : property == NULL\n"); |
| 885 | return; |
| 886 | } |
| 887 | while (cur != NULL) { |
| 888 | htmlAttrDumpOutput(buf, doc, cur, encoding); |
| 889 | cur = cur->next; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | |
| 894 | void htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 895 | xmlNodePtr cur, const char *encoding); |
| 896 | |
| 897 | /** |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 898 | * htmlNodeListDumpOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 899 | * @buf: the HTML buffer output |
| 900 | * @doc: the document |
| 901 | * @cur: the first node |
| 902 | * @encoding: the encoding string |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 903 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 904 | * |
| 905 | * Dump an HTML node list, recursive behaviour,children are printed too. |
| 906 | */ |
| 907 | static void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 908 | htmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 909 | xmlNodePtr cur, const char *encoding, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 910 | if (cur == NULL) { |
| 911 | xmlGenericError(xmlGenericErrorContext, |
| 912 | "htmlNodeListDump : node == NULL\n"); |
| 913 | return; |
| 914 | } |
| 915 | while (cur != NULL) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 916 | htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 917 | cur = cur->next; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 922 | * htmlNodeDumpFormatOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 923 | * @buf: the HTML buffer output |
| 924 | * @doc: the document |
| 925 | * @cur: the current node |
| 926 | * @encoding: the encoding string |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 927 | * @format: should formatting spaces been added |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 928 | * |
| 929 | * Dump an HTML node, recursive behaviour,children are printed too. |
| 930 | */ |
| 931 | void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 932 | htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 933 | xmlNodePtr cur, const char *encoding, int format) { |
Daniel Veillard | bb37129 | 2001-08-16 23:26:59 +0000 | [diff] [blame] | 934 | const htmlElemDesc * info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 935 | |
| 936 | if (cur == NULL) { |
| 937 | xmlGenericError(xmlGenericErrorContext, |
| 938 | "htmlNodeDump : node == NULL\n"); |
| 939 | return; |
| 940 | } |
| 941 | /* |
| 942 | * Special cases. |
| 943 | */ |
| 944 | if (cur->type == XML_DTD_NODE) |
| 945 | return; |
| 946 | if (cur->type == XML_HTML_DOCUMENT_NODE) { |
| 947 | htmlDocContentDumpOutput(buf, (xmlDocPtr) cur, encoding); |
| 948 | return; |
| 949 | } |
| 950 | if (cur->type == HTML_TEXT_NODE) { |
| 951 | if (cur->content != NULL) { |
Daniel Veillard | 6e93c4a | 2001-06-05 20:57:42 +0000 | [diff] [blame] | 952 | if (((cur->name == xmlStringText) || |
| 953 | (cur->name != xmlStringTextNoenc)) && |
| 954 | ((cur->parent == NULL) || |
| 955 | (!xmlStrEqual(cur->parent->name, BAD_CAST "script")))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 956 | xmlChar *buffer; |
| 957 | |
| 958 | #ifndef XML_USE_BUFFER_CONTENT |
| 959 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 960 | #else |
| 961 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 962 | xmlBufferContent(cur->content)); |
| 963 | #endif |
| 964 | if (buffer != NULL) { |
| 965 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 966 | xmlFree(buffer); |
| 967 | } |
| 968 | } else { |
| 969 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 970 | } |
| 971 | } |
| 972 | return; |
| 973 | } |
| 974 | if (cur->type == HTML_COMMENT_NODE) { |
| 975 | if (cur->content != NULL) { |
| 976 | xmlOutputBufferWriteString(buf, "<!--"); |
| 977 | #ifndef XML_USE_BUFFER_CONTENT |
| 978 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 979 | #else |
| 980 | xmlOutputBufferWriteString(buf, (const char *) |
| 981 | xmlBufferContent(cur->content)); |
| 982 | #endif |
| 983 | xmlOutputBufferWriteString(buf, "-->"); |
| 984 | } |
| 985 | return; |
| 986 | } |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 987 | if (cur->type == HTML_PI_NODE) { |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 988 | if (cur->name == NULL) |
| 989 | return; |
| 990 | xmlOutputBufferWriteString(buf, "<?"); |
| 991 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 992 | if (cur->content != NULL) { |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 993 | xmlOutputBufferWriteString(buf, " "); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 994 | #ifndef XML_USE_BUFFER_CONTENT |
| 995 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 996 | #else |
| 997 | xmlOutputBufferWriteString(buf, (const char *) |
| 998 | xmlBufferContent(cur->content)); |
| 999 | #endif |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 1000 | } |
Daniel Veillard | 5146f20 | 2001-04-25 10:29:44 +0000 | [diff] [blame] | 1001 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7533cc8 | 2001-04-24 15:52:00 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1004 | if (cur->type == HTML_ENTITY_REF_NODE) { |
| 1005 | xmlOutputBufferWriteString(buf, "&"); |
| 1006 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1007 | xmlOutputBufferWriteString(buf, ";"); |
| 1008 | return; |
| 1009 | } |
| 1010 | if (cur->type == HTML_PRESERVE_NODE) { |
| 1011 | if (cur->content != NULL) { |
| 1012 | #ifndef XML_USE_BUFFER_CONTENT |
| 1013 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 1014 | #else |
| 1015 | xmlOutputBufferWriteString(buf, (const char *) |
| 1016 | xmlBufferContent(cur->content)); |
| 1017 | #endif |
| 1018 | } |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
Daniel Veillard | 1ed3f88 | 2001-04-18 09:45:35 +0000 | [diff] [blame] | 1023 | * Get specific HTML info for taht node. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1024 | */ |
| 1025 | info = htmlTagLookup(cur->name); |
| 1026 | |
| 1027 | xmlOutputBufferWriteString(buf, "<"); |
| 1028 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1029 | if (cur->properties != NULL) |
| 1030 | htmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 1031 | |
| 1032 | if ((info != NULL) && (info->empty)) { |
| 1033 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 1034 | if ((format) && (!info->isinline) && (cur->next != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1035 | if ((cur->next->type != HTML_TEXT_NODE) && |
Daniel Veillard | 8a92629 | 2001-06-07 11:20:20 +0000 | [diff] [blame] | 1036 | (cur->next->type != HTML_ENTITY_REF_NODE) && |
| 1037 | (cur->parent != NULL) && |
| 1038 | (!xmlStrEqual(cur->parent->name, BAD_CAST "pre"))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1039 | xmlOutputBufferWriteString(buf, "\n"); |
| 1040 | } |
| 1041 | return; |
| 1042 | } |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1043 | if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && |
| 1044 | (cur->children == NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1045 | if ((info != NULL) && (info->saveEndTag != 0) && |
Daniel Veillard | eca60d0 | 2001-06-13 07:45:41 +0000 | [diff] [blame] | 1046 | (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) && |
| 1047 | (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1048 | xmlOutputBufferWriteString(buf, ">"); |
| 1049 | } else { |
| 1050 | xmlOutputBufferWriteString(buf, "></"); |
| 1051 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1052 | xmlOutputBufferWriteString(buf, ">"); |
| 1053 | } |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 1054 | if ((format) && (cur->next != NULL) && |
| 1055 | (info != NULL) && (!info->isinline)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1056 | if ((cur->next->type != HTML_TEXT_NODE) && |
Daniel Veillard | 8a92629 | 2001-06-07 11:20:20 +0000 | [diff] [blame] | 1057 | (cur->next->type != HTML_ENTITY_REF_NODE) && |
| 1058 | (cur->parent != NULL) && |
| 1059 | (!xmlStrEqual(cur->parent->name, BAD_CAST "pre"))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1060 | xmlOutputBufferWriteString(buf, "\n"); |
| 1061 | } |
| 1062 | return; |
| 1063 | } |
| 1064 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 1065 | if ((cur->type != XML_ELEMENT_NODE) && |
| 1066 | (cur->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1067 | /* |
| 1068 | * Uses the OutputBuffer property to automatically convert |
| 1069 | * invalids to charrefs |
| 1070 | */ |
| 1071 | |
| 1072 | #ifndef XML_USE_BUFFER_CONTENT |
| 1073 | xmlOutputBufferWriteString(buf, (const char *) cur->content); |
| 1074 | #else |
| 1075 | xmlOutputBufferWriteString(buf, |
| 1076 | (const char *) xmlBufferContent(cur->content)); |
| 1077 | #endif |
| 1078 | } |
| 1079 | if (cur->children != NULL) { |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 1080 | if ((format) && (info != NULL) && (!info->isinline) && |
| 1081 | (cur->children->type != HTML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1082 | (cur->children->type != HTML_ENTITY_REF_NODE) && |
Daniel Veillard | f0c5376 | 2001-06-07 16:07:07 +0000 | [diff] [blame] | 1083 | (cur->children != cur->last) && |
| 1084 | (!xmlStrEqual(cur->name, BAD_CAST "pre"))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1085 | xmlOutputBufferWriteString(buf, "\n"); |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1086 | htmlNodeListDumpOutput(buf, doc, cur->children, encoding, format); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 1087 | if ((format) && (info != NULL) && (!info->isinline) && |
| 1088 | (cur->last->type != HTML_TEXT_NODE) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1089 | (cur->last->type != HTML_ENTITY_REF_NODE) && |
Daniel Veillard | f0c5376 | 2001-06-07 16:07:07 +0000 | [diff] [blame] | 1090 | (cur->children != cur->last) && |
| 1091 | (!xmlStrEqual(cur->name, BAD_CAST "pre"))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | xmlOutputBufferWriteString(buf, "\n"); |
| 1093 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1094 | xmlOutputBufferWriteString(buf, "</"); |
| 1095 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 1096 | xmlOutputBufferWriteString(buf, ">"); |
Daniel Veillard | 02bb170 | 2001-06-13 21:11:59 +0000 | [diff] [blame] | 1097 | if ((format) && (info != NULL) && (!info->isinline) && |
| 1098 | (cur->next != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1099 | if ((cur->next->type != HTML_TEXT_NODE) && |
Daniel Veillard | f0c5376 | 2001-06-07 16:07:07 +0000 | [diff] [blame] | 1100 | (cur->next->type != HTML_ENTITY_REF_NODE) && |
| 1101 | (cur->parent != NULL) && |
| 1102 | (!xmlStrEqual(cur->parent->name, BAD_CAST "pre"))) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1103 | xmlOutputBufferWriteString(buf, "\n"); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | /** |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1108 | * htmlNodeDumpOutput: |
| 1109 | * @buf: the HTML buffer output |
| 1110 | * @doc: the document |
| 1111 | * @cur: the current node |
| 1112 | * @encoding: the encoding string |
| 1113 | * |
| 1114 | * Dump an HTML node, recursive behaviour,children are printed too, |
| 1115 | * and formatting returns/spaces are added. |
| 1116 | */ |
| 1117 | void |
| 1118 | htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 1119 | xmlNodePtr cur, const char *encoding) { |
| 1120 | htmlNodeDumpFormatOutput(buf, doc, cur, encoding, 1); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * htmlDocContentDumpFormatOutput: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1125 | * @buf: the HTML buffer output |
| 1126 | * @cur: the document |
| 1127 | * @encoding: the encoding string |
| 1128 | * |
| 1129 | * Dump an HTML document. |
| 1130 | */ |
| 1131 | void |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1132 | htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 1133 | const char *encoding, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1134 | int type; |
| 1135 | |
| 1136 | /* |
| 1137 | * force to output the stuff as HTML, especially for entities |
| 1138 | */ |
| 1139 | type = cur->type; |
| 1140 | cur->type = XML_HTML_DOCUMENT_NODE; |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 1141 | if (cur->intSubset != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1142 | htmlDtdDumpOutput(buf, cur, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1143 | } |
| 1144 | if (cur->children != NULL) { |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1145 | htmlNodeListDumpOutput(buf, cur, cur->children, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1146 | } |
| 1147 | xmlOutputBufferWriteString(buf, "\n"); |
| 1148 | cur->type = (xmlElementType) type; |
| 1149 | } |
| 1150 | |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1151 | /** |
| 1152 | * htmlDocContentDumpOutput: |
| 1153 | * @buf: the HTML buffer output |
| 1154 | * @cur: the document |
| 1155 | * @encoding: the encoding string |
| 1156 | * |
| 1157 | * Dump an HTML document. Formating return/spaces are added. |
| 1158 | */ |
| 1159 | void |
| 1160 | htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 1161 | const char *encoding) { |
| 1162 | htmlDocContentDumpFormatOutput(buf, cur, encoding, 1); |
| 1163 | } |
| 1164 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1165 | /************************************************************************ |
| 1166 | * * |
| 1167 | * Saving functions front-ends * |
| 1168 | * * |
| 1169 | ************************************************************************/ |
| 1170 | |
| 1171 | /** |
| 1172 | * htmlDocDump: |
| 1173 | * @f: the FILE* |
| 1174 | * @cur: the document |
| 1175 | * |
| 1176 | * Dump an HTML document to an open FILE. |
| 1177 | * |
| 1178 | * returns: the number of byte written or -1 in case of failure. |
| 1179 | */ |
| 1180 | int |
| 1181 | htmlDocDump(FILE *f, xmlDocPtr cur) { |
| 1182 | xmlOutputBufferPtr buf; |
| 1183 | xmlCharEncodingHandlerPtr handler = NULL; |
| 1184 | const char *encoding; |
| 1185 | int ret; |
| 1186 | |
| 1187 | if (cur == NULL) { |
| 1188 | #ifdef DEBUG_TREE |
| 1189 | xmlGenericError(xmlGenericErrorContext, |
| 1190 | "htmlDocDump : document == NULL\n"); |
| 1191 | #endif |
| 1192 | return(-1); |
| 1193 | } |
| 1194 | |
| 1195 | encoding = (const char *) htmlGetMetaEncoding(cur); |
| 1196 | |
| 1197 | if (encoding != NULL) { |
| 1198 | xmlCharEncoding enc; |
| 1199 | |
| 1200 | enc = xmlParseCharEncoding(encoding); |
| 1201 | if (enc != cur->charset) { |
| 1202 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 1203 | /* |
| 1204 | * Not supported yet |
| 1205 | */ |
| 1206 | return(-1); |
| 1207 | } |
| 1208 | |
| 1209 | handler = xmlFindCharEncodingHandler(encoding); |
| 1210 | if (handler == NULL) |
| 1211 | return(-1); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 1217 | */ |
| 1218 | if (handler == NULL) |
| 1219 | handler = xmlFindCharEncodingHandler("HTML"); |
| 1220 | if (handler == NULL) |
| 1221 | handler = xmlFindCharEncodingHandler("ascii"); |
| 1222 | |
| 1223 | buf = xmlOutputBufferCreateFile(f, handler); |
| 1224 | if (buf == NULL) return(-1); |
| 1225 | htmlDocContentDumpOutput(buf, cur, NULL); |
| 1226 | |
| 1227 | ret = xmlOutputBufferClose(buf); |
| 1228 | return(ret); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * htmlSaveFile: |
| 1233 | * @filename: the filename (or URL) |
| 1234 | * @cur: the document |
| 1235 | * |
| 1236 | * Dump an HTML document to a file. If @filename is "-" the stdout file is |
| 1237 | * used. |
| 1238 | * returns: the number of byte written or -1 in case of failure. |
| 1239 | */ |
| 1240 | int |
| 1241 | htmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 1242 | xmlOutputBufferPtr buf; |
| 1243 | xmlCharEncodingHandlerPtr handler = NULL; |
| 1244 | const char *encoding; |
| 1245 | int ret; |
| 1246 | |
| 1247 | encoding = (const char *) htmlGetMetaEncoding(cur); |
| 1248 | |
| 1249 | if (encoding != NULL) { |
| 1250 | xmlCharEncoding enc; |
| 1251 | |
| 1252 | enc = xmlParseCharEncoding(encoding); |
| 1253 | if (enc != cur->charset) { |
| 1254 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 1255 | /* |
| 1256 | * Not supported yet |
| 1257 | */ |
| 1258 | return(-1); |
| 1259 | } |
| 1260 | |
| 1261 | handler = xmlFindCharEncodingHandler(encoding); |
| 1262 | if (handler == NULL) |
| 1263 | return(-1); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 1269 | */ |
| 1270 | if (handler == NULL) |
| 1271 | handler = xmlFindCharEncodingHandler("HTML"); |
| 1272 | if (handler == NULL) |
| 1273 | handler = xmlFindCharEncodingHandler("ascii"); |
| 1274 | |
| 1275 | /* |
| 1276 | * save the content to a temp buffer. |
| 1277 | */ |
| 1278 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 1279 | if (buf == NULL) return(0); |
| 1280 | |
| 1281 | htmlDocContentDumpOutput(buf, cur, NULL); |
| 1282 | |
| 1283 | ret = xmlOutputBufferClose(buf); |
| 1284 | return(ret); |
| 1285 | } |
| 1286 | |
| 1287 | /** |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1288 | * htmlSaveFileFormat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1289 | * @filename: the filename |
| 1290 | * @cur: the document |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1291 | * @format: should formatting spaces been added |
| 1292 | * @encoding: the document encoding |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1293 | * |
| 1294 | * Dump an HTML document to a file using a given encoding. |
| 1295 | * |
| 1296 | * returns: the number of byte written or -1 in case of failure. |
| 1297 | */ |
| 1298 | int |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1299 | htmlSaveFileFormat(const char *filename, xmlDocPtr cur, |
| 1300 | const char *encoding, int format) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1301 | xmlOutputBufferPtr buf; |
| 1302 | xmlCharEncodingHandlerPtr handler = NULL; |
| 1303 | int ret; |
| 1304 | |
| 1305 | if (encoding != NULL) { |
| 1306 | xmlCharEncoding enc; |
| 1307 | |
| 1308 | enc = xmlParseCharEncoding(encoding); |
| 1309 | if (enc != cur->charset) { |
| 1310 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
| 1311 | /* |
| 1312 | * Not supported yet |
| 1313 | */ |
| 1314 | return(-1); |
| 1315 | } |
| 1316 | |
| 1317 | handler = xmlFindCharEncodingHandler(encoding); |
| 1318 | if (handler == NULL) |
| 1319 | return(-1); |
| 1320 | htmlSetMetaEncoding(cur, (const xmlChar *) encoding); |
| 1321 | } |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 1322 | } else { |
| 1323 | htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * Fallback to HTML or ASCII when the encoding is unspecified |
| 1328 | */ |
| 1329 | if (handler == NULL) |
| 1330 | handler = xmlFindCharEncodingHandler("HTML"); |
| 1331 | if (handler == NULL) |
| 1332 | handler = xmlFindCharEncodingHandler("ascii"); |
| 1333 | |
| 1334 | /* |
| 1335 | * save the content to a temp buffer. |
| 1336 | */ |
| 1337 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
| 1338 | if (buf == NULL) return(0); |
| 1339 | |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1340 | htmlDocContentDumpFormatOutput(buf, cur, encoding, format); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1341 | |
| 1342 | ret = xmlOutputBufferClose(buf); |
| 1343 | return(ret); |
| 1344 | } |
Daniel Veillard | 95d845f | 2001-06-13 13:48:46 +0000 | [diff] [blame] | 1345 | |
| 1346 | /** |
| 1347 | * htmlSaveFileEnc: |
| 1348 | * @filename: the filename |
| 1349 | * @cur: the document |
| 1350 | * @encoding: the document encoding |
| 1351 | * |
| 1352 | * Dump an HTML document to a file using a given encoding |
| 1353 | * and formatting returns/spaces are added. |
| 1354 | * |
| 1355 | * returns: the number of byte written or -1 in case of failure. |
| 1356 | */ |
| 1357 | int |
| 1358 | htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 1359 | return(htmlSaveFileFormat(filename, cur, encoding, 1)); |
| 1360 | } |
| 1361 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1362 | #endif /* LIBXML_HTML_ENABLED */ |