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