blob: 9a39255671ebae8b0bb3755e55e01117ad1a430a [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002 * HTMLtree.c : implementation of access function for an HTML tree.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#ifdef LIBXML_HTML_ENABLED
13
Daniel Veillard8db67d22002-11-27 19:39:27 +000014#include <string.h> /* for memset() only ! */
15
Owen Taylor3473f882001-02-23 17:55:21 +000016#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 Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillardeb475a32002-04-14 22:00:22 +000031#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032
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 */
47const xmlChar *
48htmlGetMetaEncoding(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 Veillard5151c062001-10-23 13:10:19 +000061 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +000062 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 Veillard5151c062001-10-23 13:10:19 +000079 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +000080 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);
89found_head:
90 cur = cur->children;
91
92 /*
93 * Search the meta elements
94 */
95found_meta:
96 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +000097 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +000098 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 Taylor3473f882001-02-23 17:55:21 +0000109 value = attr->children->content;
Owen Taylor3473f882001-02-23 17:55:21 +0000110 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
127found_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 */
161int
162htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
163 htmlNodePtr cur, meta;
164 const xmlChar *content;
165 char newcontent[100];
166
167
168 if (doc == NULL)
169 return(-1);
170
171 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000172 snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s",
173 encoding);
Owen Taylor3473f882001-02-23 17:55:21 +0000174 newcontent[sizeof(newcontent) - 1] = 0;
175 }
176
177 cur = doc->children;
178
179 /*
180 * Search the html
181 */
182 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000183 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000184 if (xmlStrcasecmp(cur->name, BAD_CAST"html") == 0)
185 break;
186 if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
187 goto found_head;
188 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
189 goto found_meta;
Owen Taylor3473f882001-02-23 17:55:21 +0000190 }
191 cur = cur->next;
192 }
193 if (cur == NULL)
194 return(-1);
195 cur = cur->children;
196
197 /*
198 * Search the head
199 */
200 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000201 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000202 if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
203 break;
204 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
205 goto found_meta;
Owen Taylor3473f882001-02-23 17:55:21 +0000206 }
207 cur = cur->next;
208 }
209 if (cur == NULL)
210 return(-1);
211found_head:
212 if (cur->children == NULL) {
213 if (encoding == NULL)
214 return(0);
215 meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
216 xmlAddChild(cur, meta);
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000217 xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
Daniel Veillard3a42f3f2002-07-17 17:57:34 +0000218 xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
Owen Taylor3473f882001-02-23 17:55:21 +0000219 return(0);
220 }
221 cur = cur->children;
222
223found_meta:
224 if (encoding != NULL) {
225 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000226 * Create a new Meta element with the right attributes
Owen Taylor3473f882001-02-23 17:55:21 +0000227 */
228
229 meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
230 xmlAddPrevSibling(cur, meta);
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000231 xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
Daniel Veillard3a42f3f2002-07-17 17:57:34 +0000232 xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
Owen Taylor3473f882001-02-23 17:55:21 +0000233 }
234
235 /*
236 * Search and destroy all the remaining the meta elements carrying
237 * encoding informations
238 */
239 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000240 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000241 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000242 xmlAttrPtr attr = cur->properties;
243 int http;
244 const xmlChar *value;
245
246 content = NULL;
247 http = 0;
248 while (attr != NULL) {
249 if ((attr->children != NULL) &&
250 (attr->children->type == XML_TEXT_NODE) &&
251 (attr->children->next == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000252 value = attr->children->content;
Owen Taylor3473f882001-02-23 17:55:21 +0000253 if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
254 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
255 http = 1;
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000256 else
257 {
258 if ((value != NULL) &&
259 (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
260 content = value;
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000261 }
Daniel Veillard4e0e2972002-03-06 21:39:42 +0000262 if ((http != 0) && (content != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000263 break;
264 }
265 attr = attr->next;
266 }
Daniel Veillard4e0e2972002-03-06 21:39:42 +0000267 if ((http != 0) && (content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000268 meta = cur;
269 cur = cur->next;
270 xmlUnlinkNode(meta);
271 xmlFreeNode(meta);
272 continue;
273 }
274
275 }
276 }
277 cur = cur->next;
278 }
279 return(0);
280}
281
Daniel Veillardc084e472002-08-12 13:27:28 +0000282/**
283 * booleanHTMLAttrs:
284 *
285 * These are the HTML attributes which will be output
286 * in minimized form, i.e. <option selected="selected"> will be
287 * output as <option selected>, as per XSLT 1.0 16.2 "HTML Output Method"
288 *
289 */
290static const char* htmlBooleanAttrs[] = {
291 "checked", "compact", "declare", "defer", "disabled", "ismap",
292 "multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
293 "selected", NULL
294};
295
296
297/**
298 * htmlIsBooleanAttr:
299 * @name: the name of the attribute to check
300 *
301 * Determine if a given attribute is a boolean attribute.
302 *
303 * returns: false if the attribute is not boolean, true otherwise.
304 */
305int
306htmlIsBooleanAttr(const xmlChar *name)
307{
308 int i = 0;
309
310 while (htmlBooleanAttrs[i] != NULL) {
Daniel Veillardabe01742002-09-26 12:40:03 +0000311 if (xmlStrcasecmp((const xmlChar *)htmlBooleanAttrs[i], name) == 0)
Daniel Veillardc084e472002-08-12 13:27:28 +0000312 return 1;
313 i++;
314 }
315 return 0;
316}
317
Owen Taylor3473f882001-02-23 17:55:21 +0000318/************************************************************************
319 * *
320 * Dumping HTML tree content to a simple buffer *
321 * *
322 ************************************************************************/
323
Daniel Veillard8db67d22002-11-27 19:39:27 +0000324static int
Daniel Veillard86fd5a72001-12-13 14:55:21 +0000325htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
326 int format);
Owen Taylor3473f882001-02-23 17:55:21 +0000327
328/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000329 * htmlNodeDumpFormat:
Owen Taylor3473f882001-02-23 17:55:21 +0000330 * @buf: the HTML buffer output
331 * @doc: the document
332 * @cur: the current node
Daniel Veillard95d845f2001-06-13 13:48:46 +0000333 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000334 *
335 * Dump an HTML node, recursive behaviour,children are printed too.
Daniel Veillard8db67d22002-11-27 19:39:27 +0000336 *
337 * Returns the number of byte written or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +0000338 */
Daniel Veillard8db67d22002-11-27 19:39:27 +0000339static int
Daniel Veillard95d845f2001-06-13 13:48:46 +0000340htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
341 int format) {
Daniel Veillard8db67d22002-11-27 19:39:27 +0000342 unsigned int use;
343 int ret;
344 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +0000345
346 if (cur == NULL) {
Daniel Veillard8db67d22002-11-27 19:39:27 +0000347 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000348 }
Daniel Veillard8db67d22002-11-27 19:39:27 +0000349 if (buf == NULL) {
350 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000351 }
Daniel Veillard8db67d22002-11-27 19:39:27 +0000352 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
353 if (outbuf == NULL) {
354 xmlGenericError(xmlGenericErrorContext,
355 "htmlNodeDumpFormat: out of memory!\n");
356 return (-1);
357 }
358 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
359 outbuf->buffer = buf;
360 outbuf->encoder = NULL;
361 outbuf->writecallback = NULL;
362 outbuf->closecallback = NULL;
363 outbuf->context = NULL;
364 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000365
Daniel Veillard8db67d22002-11-27 19:39:27 +0000366 use = buf->use;
367 htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
368 xmlFree(outbuf);
369 ret = buf->use - use;
370 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000371}
372
373/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000374 * htmlNodeDump:
375 * @buf: the HTML buffer output
376 * @doc: the document
377 * @cur: the current node
378 *
379 * Dump an HTML node, recursive behaviour,children are printed too,
380 * and formatting returns are added.
Daniel Veillard8db67d22002-11-27 19:39:27 +0000381 *
382 * Returns the number of byte written or -1 in case of error
Daniel Veillard95d845f2001-06-13 13:48:46 +0000383 */
Daniel Veillard8db67d22002-11-27 19:39:27 +0000384int
Daniel Veillard95d845f2001-06-13 13:48:46 +0000385htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillard8db67d22002-11-27 19:39:27 +0000386 return(htmlNodeDumpFormat(buf, doc, cur, 1));
Daniel Veillard95d845f2001-06-13 13:48:46 +0000387}
388
389/**
390 * htmlNodeDumpFileFormat:
391 * @out: the FILE pointer
392 * @doc: the document
393 * @cur: the current node
394 * @encoding: the document encoding
395 * @format: should formatting spaces been added
396 *
397 * Dump an HTML node, recursive behaviour,children are printed too.
398 *
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000399 * TODO: if encoding == NULL try to save in the doc encoding
400 *
401 * returns: the number of byte written or -1 in case of failure.
Daniel Veillard95d845f2001-06-13 13:48:46 +0000402 */
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000403int
404htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
405 xmlNodePtr cur, const char *encoding, int format) {
406 xmlOutputBufferPtr buf;
407 xmlCharEncodingHandlerPtr handler = NULL;
408 int ret;
Daniel Veillard95d845f2001-06-13 13:48:46 +0000409
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000410 if (encoding != NULL) {
411 xmlCharEncoding enc;
412
413 enc = xmlParseCharEncoding(encoding);
414 if (enc != XML_CHAR_ENCODING_UTF8) {
415 handler = xmlFindCharEncodingHandler(encoding);
416 if (handler == NULL)
417 return(-1);
418 }
419 }
420
421 /*
422 * Fallback to HTML or ASCII when the encoding is unspecified
423 */
424 if (handler == NULL)
425 handler = xmlFindCharEncodingHandler("HTML");
426 if (handler == NULL)
427 handler = xmlFindCharEncodingHandler("ascii");
428
429 /*
430 * save the content to a temp buffer.
431 */
432 buf = xmlOutputBufferCreateFile(out, handler);
433 if (buf == NULL) return(0);
434
435 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
436
437 ret = xmlOutputBufferClose(buf);
438 return(ret);
Daniel Veillard95d845f2001-06-13 13:48:46 +0000439}
440
441/**
Owen Taylor3473f882001-02-23 17:55:21 +0000442 * htmlNodeDumpFile:
443 * @out: the FILE pointer
444 * @doc: the document
445 * @cur: the current node
446 *
Daniel Veillard95d845f2001-06-13 13:48:46 +0000447 * Dump an HTML node, recursive behaviour,children are printed too,
448 * and formatting returns are added.
Owen Taylor3473f882001-02-23 17:55:21 +0000449 */
450void
451htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillard95d845f2001-06-13 13:48:46 +0000452 htmlNodeDumpFileFormat(out, doc, cur, NULL, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000453}
454
455/**
Owen Taylor3473f882001-02-23 17:55:21 +0000456 * htmlDocDumpMemory:
457 * @cur: the document
458 * @mem: OUT: the memory pointer
Daniel Veillard2d703722001-05-30 18:32:34 +0000459 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +0000460 *
461 * Dump an HTML document in memory and return the xmlChar * and it's size.
462 * It's up to the caller to free the memory.
463 */
464void
465htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
Daniel Veillard2d703722001-05-30 18:32:34 +0000466 xmlOutputBufferPtr buf;
467 xmlCharEncodingHandlerPtr handler = NULL;
468 const char *encoding;
Owen Taylor3473f882001-02-23 17:55:21 +0000469
470 if (cur == NULL) {
471#ifdef DEBUG_TREE
472 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard2d703722001-05-30 18:32:34 +0000473 "htmlDocDumpMemory : document == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000474#endif
475 *mem = NULL;
476 *size = 0;
477 return;
478 }
Daniel Veillard2d703722001-05-30 18:32:34 +0000479
480 encoding = (const char *) htmlGetMetaEncoding(cur);
481
482 if (encoding != NULL) {
483 xmlCharEncoding enc;
484
485 enc = xmlParseCharEncoding(encoding);
486 if (enc != cur->charset) {
487 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
488 /*
489 * Not supported yet
490 */
491 *mem = NULL;
492 *size = 0;
493 return;
494 }
495
496 handler = xmlFindCharEncodingHandler(encoding);
497 if (handler == NULL) {
498 *mem = NULL;
499 *size = 0;
500 return;
501 }
502 }
503 }
504
505 /*
506 * Fallback to HTML or ASCII when the encoding is unspecified
507 */
508 if (handler == NULL)
509 handler = xmlFindCharEncodingHandler("HTML");
510 if (handler == NULL)
511 handler = xmlFindCharEncodingHandler("ascii");
512
513 buf = xmlAllocOutputBuffer(handler);
Owen Taylor3473f882001-02-23 17:55:21 +0000514 if (buf == NULL) {
515 *mem = NULL;
516 *size = 0;
517 return;
518 }
Daniel Veillard2d703722001-05-30 18:32:34 +0000519
520 htmlDocContentDumpOutput(buf, cur, NULL);
521 xmlOutputBufferFlush(buf);
522 if (buf->conv != NULL) {
523 *size = buf->conv->use;
524 *mem = xmlStrndup(buf->conv->content, *size);
525 } else {
526 *size = buf->buffer->use;
527 *mem = xmlStrndup(buf->buffer->content, *size);
528 }
529 (void)xmlOutputBufferClose(buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000530}
531
532
533/************************************************************************
534 * *
535 * Dumping HTML tree content to an I/O output buffer *
536 * *
537 ************************************************************************/
538
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000539void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillardc084e472002-08-12 13:27:28 +0000540
Owen Taylor3473f882001-02-23 17:55:21 +0000541/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000542 * htmlDtdDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000543 * @buf: the HTML buffer output
544 * @doc: the document
545 * @encoding: the encoding string
546 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000547 * TODO: check whether encoding is needed
548 *
Owen Taylor3473f882001-02-23 17:55:21 +0000549 * Dump the HTML document DTD, if any.
550 */
551static void
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000552htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000553 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000554 xmlDtdPtr cur = doc->intSubset;
555
556 if (cur == NULL) {
557 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000558 "htmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000559 return;
560 }
561 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
562 xmlOutputBufferWriteString(buf, (const char *)cur->name);
563 if (cur->ExternalID != NULL) {
564 xmlOutputBufferWriteString(buf, " PUBLIC ");
565 xmlBufferWriteQuotedString(buf->buffer, cur->ExternalID);
566 if (cur->SystemID != NULL) {
567 xmlOutputBufferWriteString(buf, " ");
568 xmlBufferWriteQuotedString(buf->buffer, cur->SystemID);
569 }
570 } else if (cur->SystemID != NULL) {
571 xmlOutputBufferWriteString(buf, " SYSTEM ");
572 xmlBufferWriteQuotedString(buf->buffer, cur->SystemID);
573 }
574 xmlOutputBufferWriteString(buf, ">\n");
575}
576
577/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000578 * htmlAttrDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000579 * @buf: the HTML buffer output
580 * @doc: the document
581 * @cur: the attribute pointer
582 * @encoding: the encoding string
583 *
584 * Dump an HTML attribute
585 */
586static void
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000587htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000588 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000589 xmlChar *value;
590
Daniel Veillardeca60d02001-06-13 07:45:41 +0000591 /*
592 * TODO: The html output method should not escape a & character
593 * occurring in an attribute value immediately followed by
594 * a { character (see Section B.7.1 of the HTML 4.0 Recommendation).
595 */
596
Owen Taylor3473f882001-02-23 17:55:21 +0000597 if (cur == NULL) {
598 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000599 "htmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000600 return;
601 }
602 xmlOutputBufferWriteString(buf, " ");
603 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillardc084e472002-08-12 13:27:28 +0000604 if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000605 value = xmlNodeListGetString(doc, cur->children, 0);
606 if (value) {
607 xmlOutputBufferWriteString(buf, "=");
Daniel Veillardc7e9b192003-03-27 14:08:24 +0000608 if ((cur->ns == NULL) && (cur->parent != NULL) &&
609 (cur->parent->ns == NULL) &&
610 ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) ||
611 (!xmlStrcasecmp(cur->name, BAD_CAST "action")) ||
612 (!xmlStrcasecmp(cur->name, BAD_CAST "src")))) {
Daniel Veillardeb475a32002-04-14 22:00:22 +0000613 xmlChar *escaped;
614 xmlChar *tmp = value;
615
616 while (IS_BLANK(*tmp)) tmp++;
617
Daniel Veillard04ee2f22003-03-23 20:31:46 +0000618 escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,");
Daniel Veillardeb475a32002-04-14 22:00:22 +0000619 if (escaped != NULL) {
620 xmlBufferWriteQuotedString(buf->buffer, escaped);
621 xmlFree(escaped);
622 } else {
623 xmlBufferWriteQuotedString(buf->buffer, value);
624 }
625 } else {
626 xmlBufferWriteQuotedString(buf->buffer, value);
627 }
Owen Taylor3473f882001-02-23 17:55:21 +0000628 xmlFree(value);
629 } else {
630 xmlOutputBufferWriteString(buf, "=\"\"");
631 }
632 }
633}
634
635/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000636 * htmlAttrListDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000637 * @buf: the HTML buffer output
638 * @doc: the document
639 * @cur: the first attribute pointer
640 * @encoding: the encoding string
641 *
642 * Dump a list of HTML attributes
643 */
644static void
645htmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) {
646 if (cur == NULL) {
647 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000648 "htmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000649 return;
650 }
651 while (cur != NULL) {
652 htmlAttrDumpOutput(buf, doc, cur, encoding);
653 cur = cur->next;
654 }
655}
656
657
Owen Taylor3473f882001-02-23 17:55:21 +0000658
659/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000660 * htmlNodeListDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000661 * @buf: the HTML buffer output
662 * @doc: the document
663 * @cur: the first node
664 * @encoding: the encoding string
Daniel Veillard95d845f2001-06-13 13:48:46 +0000665 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000666 *
667 * Dump an HTML node list, recursive behaviour,children are printed too.
668 */
669static void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000670htmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
671 xmlNodePtr cur, const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +0000672 if (cur == NULL) {
673 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000674 "htmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000675 return;
676 }
677 while (cur != NULL) {
Daniel Veillard95d845f2001-06-13 13:48:46 +0000678 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +0000679 cur = cur->next;
680 }
681}
682
683/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000684 * htmlNodeDumpFormatOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000685 * @buf: the HTML buffer output
686 * @doc: the document
687 * @cur: the current node
688 * @encoding: the encoding string
Daniel Veillard95d845f2001-06-13 13:48:46 +0000689 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000690 *
691 * Dump an HTML node, recursive behaviour,children are printed too.
692 */
693void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000694htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
695 xmlNodePtr cur, const char *encoding, int format) {
Daniel Veillardbb371292001-08-16 23:26:59 +0000696 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +0000697
698 if (cur == NULL) {
699 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000700 "htmlNodeDumpFormatOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000701 return;
702 }
703 /*
704 * Special cases.
705 */
706 if (cur->type == XML_DTD_NODE)
707 return;
708 if (cur->type == XML_HTML_DOCUMENT_NODE) {
709 htmlDocContentDumpOutput(buf, (xmlDocPtr) cur, encoding);
710 return;
711 }
712 if (cur->type == HTML_TEXT_NODE) {
713 if (cur->content != NULL) {
Daniel Veillardb44025c2001-10-11 22:55:55 +0000714 if (((cur->name == (const xmlChar *)xmlStringText) ||
715 (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
Daniel Veillard6e93c4a2001-06-05 20:57:42 +0000716 ((cur->parent == NULL) ||
Daniel Veillard44892f72002-10-16 15:23:26 +0000717 ((xmlStrcasecmp(cur->parent->name, BAD_CAST "script")) &&
718 (xmlStrcasecmp(cur->parent->name, BAD_CAST "style"))))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000719 xmlChar *buffer;
720
Owen Taylor3473f882001-02-23 17:55:21 +0000721 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000722 if (buffer != NULL) {
723 xmlOutputBufferWriteString(buf, (const char *)buffer);
724 xmlFree(buffer);
725 }
726 } else {
727 xmlOutputBufferWriteString(buf, (const char *)cur->content);
728 }
729 }
730 return;
731 }
732 if (cur->type == HTML_COMMENT_NODE) {
733 if (cur->content != NULL) {
734 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +0000735 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000736 xmlOutputBufferWriteString(buf, "-->");
737 }
738 return;
739 }
Daniel Veillard7533cc82001-04-24 15:52:00 +0000740 if (cur->type == HTML_PI_NODE) {
Daniel Veillard5146f202001-04-25 10:29:44 +0000741 if (cur->name == NULL)
742 return;
743 xmlOutputBufferWriteString(buf, "<?");
744 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7533cc82001-04-24 15:52:00 +0000745 if (cur->content != NULL) {
Daniel Veillard5146f202001-04-25 10:29:44 +0000746 xmlOutputBufferWriteString(buf, " ");
Daniel Veillard7533cc82001-04-24 15:52:00 +0000747 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7533cc82001-04-24 15:52:00 +0000748 }
Daniel Veillard5146f202001-04-25 10:29:44 +0000749 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7533cc82001-04-24 15:52:00 +0000750 return;
751 }
Owen Taylor3473f882001-02-23 17:55:21 +0000752 if (cur->type == HTML_ENTITY_REF_NODE) {
753 xmlOutputBufferWriteString(buf, "&");
754 xmlOutputBufferWriteString(buf, (const char *)cur->name);
755 xmlOutputBufferWriteString(buf, ";");
756 return;
757 }
758 if (cur->type == HTML_PRESERVE_NODE) {
759 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000760 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000761 }
762 return;
763 }
764
765 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000766 * Get specific HTML info for that node.
Owen Taylor3473f882001-02-23 17:55:21 +0000767 */
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000768 if (cur->ns == NULL)
769 info = htmlTagLookup(cur->name);
770 else
771 info = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000772
773 xmlOutputBufferWriteString(buf, "<");
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000774 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
775 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
776 xmlOutputBufferWriteString(buf, ":");
777 }
Owen Taylor3473f882001-02-23 17:55:21 +0000778 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000779 if (cur->nsDef)
780 xmlNsListDumpOutput(buf, cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +0000781 if (cur->properties != NULL)
782 htmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
783
784 if ((info != NULL) && (info->empty)) {
785 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard02bb1702001-06-13 21:11:59 +0000786 if ((format) && (!info->isinline) && (cur->next != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000787 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillard8a926292001-06-07 11:20:20 +0000788 (cur->next->type != HTML_ENTITY_REF_NODE) &&
789 (cur->parent != NULL) &&
790 (!xmlStrEqual(cur->parent->name, BAD_CAST "pre")))
Owen Taylor3473f882001-02-23 17:55:21 +0000791 xmlOutputBufferWriteString(buf, "\n");
792 }
793 return;
794 }
Daniel Veillard7db37732001-07-12 01:20:08 +0000795 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
796 (cur->children == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000797 if ((info != NULL) && (info->saveEndTag != 0) &&
Daniel Veillardeca60d02001-06-13 07:45:41 +0000798 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
799 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000800 xmlOutputBufferWriteString(buf, ">");
801 } else {
802 xmlOutputBufferWriteString(buf, "></");
Daniel Veillard645c6902003-04-10 21:40:49 +0000803 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
804 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
805 xmlOutputBufferWriteString(buf, ":");
806 }
Owen Taylor3473f882001-02-23 17:55:21 +0000807 xmlOutputBufferWriteString(buf, (const char *)cur->name);
808 xmlOutputBufferWriteString(buf, ">");
809 }
Daniel Veillard02bb1702001-06-13 21:11:59 +0000810 if ((format) && (cur->next != NULL) &&
811 (info != NULL) && (!info->isinline)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000812 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillard8a926292001-06-07 11:20:20 +0000813 (cur->next->type != HTML_ENTITY_REF_NODE) &&
814 (cur->parent != NULL) &&
815 (!xmlStrEqual(cur->parent->name, BAD_CAST "pre")))
Owen Taylor3473f882001-02-23 17:55:21 +0000816 xmlOutputBufferWriteString(buf, "\n");
817 }
818 return;
819 }
820 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +0000821 if ((cur->type != XML_ELEMENT_NODE) &&
822 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000823 /*
824 * Uses the OutputBuffer property to automatically convert
825 * invalids to charrefs
826 */
827
Owen Taylor3473f882001-02-23 17:55:21 +0000828 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000829 }
830 if (cur->children != NULL) {
Daniel Veillard02bb1702001-06-13 21:11:59 +0000831 if ((format) && (info != NULL) && (!info->isinline) &&
832 (cur->children->type != HTML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000833 (cur->children->type != HTML_ENTITY_REF_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000834 (cur->children != cur->last) &&
835 (!xmlStrEqual(cur->name, BAD_CAST "pre")))
Owen Taylor3473f882001-02-23 17:55:21 +0000836 xmlOutputBufferWriteString(buf, "\n");
Daniel Veillard95d845f2001-06-13 13:48:46 +0000837 htmlNodeListDumpOutput(buf, doc, cur->children, encoding, format);
Daniel Veillard02bb1702001-06-13 21:11:59 +0000838 if ((format) && (info != NULL) && (!info->isinline) &&
839 (cur->last->type != HTML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000840 (cur->last->type != HTML_ENTITY_REF_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000841 (cur->children != cur->last) &&
842 (!xmlStrEqual(cur->name, BAD_CAST "pre")))
Owen Taylor3473f882001-02-23 17:55:21 +0000843 xmlOutputBufferWriteString(buf, "\n");
844 }
Owen Taylor3473f882001-02-23 17:55:21 +0000845 xmlOutputBufferWriteString(buf, "</");
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000846 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
847 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
848 xmlOutputBufferWriteString(buf, ":");
849 }
Owen Taylor3473f882001-02-23 17:55:21 +0000850 xmlOutputBufferWriteString(buf, (const char *)cur->name);
851 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard02bb1702001-06-13 21:11:59 +0000852 if ((format) && (info != NULL) && (!info->isinline) &&
853 (cur->next != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000854 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000855 (cur->next->type != HTML_ENTITY_REF_NODE) &&
856 (cur->parent != NULL) &&
857 (!xmlStrEqual(cur->parent->name, BAD_CAST "pre")))
Owen Taylor3473f882001-02-23 17:55:21 +0000858 xmlOutputBufferWriteString(buf, "\n");
859 }
860}
861
862/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000863 * htmlNodeDumpOutput:
864 * @buf: the HTML buffer output
865 * @doc: the document
866 * @cur: the current node
867 * @encoding: the encoding string
868 *
869 * Dump an HTML node, recursive behaviour,children are printed too,
870 * and formatting returns/spaces are added.
871 */
872void
873htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
874 xmlNodePtr cur, const char *encoding) {
875 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, 1);
876}
877
878/**
879 * htmlDocContentDumpFormatOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000880 * @buf: the HTML buffer output
881 * @cur: the document
882 * @encoding: the encoding string
Daniel Veillard9d06d302002-01-22 18:15:52 +0000883 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000884 *
885 * Dump an HTML document.
886 */
887void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000888htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
889 const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +0000890 int type;
891
892 /*
893 * force to output the stuff as HTML, especially for entities
894 */
895 type = cur->type;
896 cur->type = XML_HTML_DOCUMENT_NODE;
Daniel Veillard4dd93462001-04-02 15:16:19 +0000897 if (cur->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000898 htmlDtdDumpOutput(buf, cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000899 }
900 if (cur->children != NULL) {
Daniel Veillard95d845f2001-06-13 13:48:46 +0000901 htmlNodeListDumpOutput(buf, cur, cur->children, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +0000902 }
903 xmlOutputBufferWriteString(buf, "\n");
904 cur->type = (xmlElementType) type;
905}
906
Daniel Veillard95d845f2001-06-13 13:48:46 +0000907/**
908 * htmlDocContentDumpOutput:
909 * @buf: the HTML buffer output
910 * @cur: the document
911 * @encoding: the encoding string
912 *
913 * Dump an HTML document. Formating return/spaces are added.
914 */
915void
916htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
917 const char *encoding) {
918 htmlDocContentDumpFormatOutput(buf, cur, encoding, 1);
919}
920
Owen Taylor3473f882001-02-23 17:55:21 +0000921/************************************************************************
922 * *
923 * Saving functions front-ends *
924 * *
925 ************************************************************************/
926
927/**
928 * htmlDocDump:
929 * @f: the FILE*
930 * @cur: the document
931 *
932 * Dump an HTML document to an open FILE.
933 *
934 * returns: the number of byte written or -1 in case of failure.
935 */
936int
937htmlDocDump(FILE *f, xmlDocPtr cur) {
938 xmlOutputBufferPtr buf;
939 xmlCharEncodingHandlerPtr handler = NULL;
940 const char *encoding;
941 int ret;
942
943 if (cur == NULL) {
944#ifdef DEBUG_TREE
945 xmlGenericError(xmlGenericErrorContext,
946 "htmlDocDump : document == NULL\n");
947#endif
948 return(-1);
949 }
950
951 encoding = (const char *) htmlGetMetaEncoding(cur);
952
953 if (encoding != NULL) {
954 xmlCharEncoding enc;
955
956 enc = xmlParseCharEncoding(encoding);
957 if (enc != cur->charset) {
958 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
959 /*
960 * Not supported yet
961 */
962 return(-1);
963 }
964
965 handler = xmlFindCharEncodingHandler(encoding);
966 if (handler == NULL)
967 return(-1);
968 }
969 }
970
971 /*
972 * Fallback to HTML or ASCII when the encoding is unspecified
973 */
974 if (handler == NULL)
975 handler = xmlFindCharEncodingHandler("HTML");
976 if (handler == NULL)
977 handler = xmlFindCharEncodingHandler("ascii");
978
979 buf = xmlOutputBufferCreateFile(f, handler);
980 if (buf == NULL) return(-1);
981 htmlDocContentDumpOutput(buf, cur, NULL);
982
983 ret = xmlOutputBufferClose(buf);
984 return(ret);
985}
986
987/**
988 * htmlSaveFile:
989 * @filename: the filename (or URL)
990 * @cur: the document
991 *
992 * Dump an HTML document to a file. If @filename is "-" the stdout file is
993 * used.
994 * returns: the number of byte written or -1 in case of failure.
995 */
996int
997htmlSaveFile(const char *filename, xmlDocPtr cur) {
998 xmlOutputBufferPtr buf;
999 xmlCharEncodingHandlerPtr handler = NULL;
1000 const char *encoding;
1001 int ret;
1002
1003 encoding = (const char *) htmlGetMetaEncoding(cur);
1004
1005 if (encoding != NULL) {
1006 xmlCharEncoding enc;
1007
1008 enc = xmlParseCharEncoding(encoding);
1009 if (enc != cur->charset) {
1010 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1011 /*
1012 * Not supported yet
1013 */
1014 return(-1);
1015 }
1016
1017 handler = xmlFindCharEncodingHandler(encoding);
1018 if (handler == NULL)
1019 return(-1);
1020 }
1021 }
1022
1023 /*
1024 * Fallback to HTML or ASCII when the encoding is unspecified
1025 */
1026 if (handler == NULL)
1027 handler = xmlFindCharEncodingHandler("HTML");
1028 if (handler == NULL)
1029 handler = xmlFindCharEncodingHandler("ascii");
1030
1031 /*
1032 * save the content to a temp buffer.
1033 */
1034 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
1035 if (buf == NULL) return(0);
1036
1037 htmlDocContentDumpOutput(buf, cur, NULL);
1038
1039 ret = xmlOutputBufferClose(buf);
1040 return(ret);
1041}
1042
1043/**
Daniel Veillard95d845f2001-06-13 13:48:46 +00001044 * htmlSaveFileFormat:
Owen Taylor3473f882001-02-23 17:55:21 +00001045 * @filename: the filename
1046 * @cur: the document
Daniel Veillard95d845f2001-06-13 13:48:46 +00001047 * @format: should formatting spaces been added
1048 * @encoding: the document encoding
Owen Taylor3473f882001-02-23 17:55:21 +00001049 *
1050 * Dump an HTML document to a file using a given encoding.
1051 *
1052 * returns: the number of byte written or -1 in case of failure.
1053 */
1054int
Daniel Veillard95d845f2001-06-13 13:48:46 +00001055htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
1056 const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00001057 xmlOutputBufferPtr buf;
1058 xmlCharEncodingHandlerPtr handler = NULL;
1059 int ret;
1060
1061 if (encoding != NULL) {
1062 xmlCharEncoding enc;
1063
1064 enc = xmlParseCharEncoding(encoding);
1065 if (enc != cur->charset) {
1066 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1067 /*
1068 * Not supported yet
1069 */
1070 return(-1);
1071 }
1072
1073 handler = xmlFindCharEncodingHandler(encoding);
1074 if (handler == NULL)
1075 return(-1);
1076 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
1077 }
Daniel Veillard4dd93462001-04-02 15:16:19 +00001078 } else {
1079 htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
Owen Taylor3473f882001-02-23 17:55:21 +00001080 }
1081
1082 /*
1083 * Fallback to HTML or ASCII when the encoding is unspecified
1084 */
1085 if (handler == NULL)
1086 handler = xmlFindCharEncodingHandler("HTML");
1087 if (handler == NULL)
1088 handler = xmlFindCharEncodingHandler("ascii");
1089
1090 /*
1091 * save the content to a temp buffer.
1092 */
1093 buf = xmlOutputBufferCreateFilename(filename, handler, 0);
1094 if (buf == NULL) return(0);
1095
Daniel Veillard95d845f2001-06-13 13:48:46 +00001096 htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00001097
1098 ret = xmlOutputBufferClose(buf);
1099 return(ret);
1100}
Daniel Veillard95d845f2001-06-13 13:48:46 +00001101
1102/**
1103 * htmlSaveFileEnc:
1104 * @filename: the filename
1105 * @cur: the document
1106 * @encoding: the document encoding
1107 *
1108 * Dump an HTML document to a file using a given encoding
1109 * and formatting returns/spaces are added.
1110 *
1111 * returns: the number of byte written or -1 in case of failure.
1112 */
1113int
1114htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
1115 return(htmlSaveFileFormat(filename, cur, encoding, 1));
1116}
1117
Daniel Veillardc084e472002-08-12 13:27:28 +00001118
1119
Owen Taylor3473f882001-02-23 17:55:21 +00001120#endif /* LIBXML_HTML_ENABLED */