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