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