blob: b5085836b900af69b90eab1d68bd71ed3ba55559 [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) {
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200163 htmlNodePtr cur, meta = NULL, head = NULL;
164 const xmlChar *content = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000165 char newcontent[100];
166
167
168 if (doc == NULL)
169 return(-1);
170
Daniel Veillard74eb54b2009-08-12 15:59:01 +0200171 /* html isn't a real encoding it's just libxml2 way to get entities */
172 if (!xmlStrcasecmp(encoding, BAD_CAST "html"))
173 return(-1);
174
Owen Taylor3473f882001-02-23 17:55:21 +0000175 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000176 snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s",
William M. Brack13dfa872004-09-18 04:52:08 +0000177 (char *)encoding);
Owen Taylor3473f882001-02-23 17:55:21 +0000178 newcontent[sizeof(newcontent) - 1] = 0;
179 }
180
181 cur = doc->children;
182
183 /*
184 * Search the html
185 */
186 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000187 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000188 if (xmlStrcasecmp(cur->name, BAD_CAST"html") == 0)
189 break;
190 if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
191 goto found_head;
192 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
193 goto found_meta;
Owen Taylor3473f882001-02-23 17:55:21 +0000194 }
195 cur = cur->next;
196 }
197 if (cur == NULL)
198 return(-1);
199 cur = cur->children;
200
201 /*
202 * Search the head
203 */
204 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000205 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000206 if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
207 break;
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200208 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
209 head = cur->parent;
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000210 goto found_meta;
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200211 }
Owen Taylor3473f882001-02-23 17:55:21 +0000212 }
213 cur = cur->next;
214 }
215 if (cur == NULL)
216 return(-1);
217found_head:
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200218 head = cur;
219 if (cur->children == NULL)
220 goto create;
Owen Taylor3473f882001-02-23 17:55:21 +0000221 cur = cur->children;
222
223found_meta:
Owen Taylor3473f882001-02-23 17:55:21 +0000224 /*
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200225 * Search and update all the remaining the meta elements carrying
Owen Taylor3473f882001-02-23 17:55:21 +0000226 * encoding informations
227 */
228 while (cur != NULL) {
Daniel Veillard5151c062001-10-23 13:10:19 +0000229 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000230 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000231 xmlAttrPtr attr = cur->properties;
232 int http;
233 const xmlChar *value;
234
235 content = NULL;
236 http = 0;
237 while (attr != NULL) {
238 if ((attr->children != NULL) &&
239 (attr->children->type == XML_TEXT_NODE) &&
240 (attr->children->next == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000241 value = attr->children->content;
Owen Taylor3473f882001-02-23 17:55:21 +0000242 if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
243 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
244 http = 1;
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200245 else
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000246 {
247 if ((value != NULL) &&
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200248 (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
249 content = value;
Daniel Veillard1ed3f882001-04-18 09:45:35 +0000250 }
Daniel Veillard4e0e2972002-03-06 21:39:42 +0000251 if ((http != 0) && (content != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000252 break;
253 }
254 attr = attr->next;
255 }
Daniel Veillard4e0e2972002-03-06 21:39:42 +0000256 if ((http != 0) && (content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000257 meta = cur;
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200258 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000259 }
260
261 }
262 }
263 cur = cur->next;
264 }
Daniel Veillard8d7c1b72009-08-12 23:03:23 +0200265create:
266 if (meta == NULL) {
267 if ((encoding != NULL) && (head != NULL)) {
268 /*
269 * Create a new Meta element with the right attributes
270 */
271
272 meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
273 if (head->children == NULL)
274 xmlAddChild(head, meta);
275 else
276 xmlAddPrevSibling(head->children, meta);
277 xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
278 xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
279 }
280 } else {
281 /* change the document only if there is a real encoding change */
282 if (xmlStrcasestr(content, encoding) == NULL) {
283 xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent);
284 }
285 }
286
287
Owen Taylor3473f882001-02-23 17:55:21 +0000288 return(0);
289}
290
Daniel Veillardc084e472002-08-12 13:27:28 +0000291/**
292 * booleanHTMLAttrs:
293 *
294 * These are the HTML attributes which will be output
295 * in minimized form, i.e. <option selected="selected"> will be
296 * output as <option selected>, as per XSLT 1.0 16.2 "HTML Output Method"
297 *
298 */
299static const char* htmlBooleanAttrs[] = {
300 "checked", "compact", "declare", "defer", "disabled", "ismap",
301 "multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
302 "selected", NULL
303};
304
305
306/**
307 * htmlIsBooleanAttr:
308 * @name: the name of the attribute to check
309 *
310 * Determine if a given attribute is a boolean attribute.
311 *
312 * returns: false if the attribute is not boolean, true otherwise.
313 */
314int
315htmlIsBooleanAttr(const xmlChar *name)
316{
317 int i = 0;
318
319 while (htmlBooleanAttrs[i] != NULL) {
Daniel Veillardabe01742002-09-26 12:40:03 +0000320 if (xmlStrcasecmp((const xmlChar *)htmlBooleanAttrs[i], name) == 0)
Daniel Veillardc084e472002-08-12 13:27:28 +0000321 return 1;
322 i++;
323 }
324 return 0;
325}
326
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000327#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardda3fee42008-09-01 13:08:57 +0000328/*
329 * private routine exported from xmlIO.c
330 */
331xmlOutputBufferPtr
332xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder);
Owen Taylor3473f882001-02-23 17:55:21 +0000333/************************************************************************
334 * *
Daniel Veillarde2238d52003-10-09 13:14:55 +0000335 * Output error handlers *
336 * *
337 ************************************************************************/
338/**
339 * htmlSaveErrMemory:
340 * @extra: extra informations
341 *
342 * Handle an out of memory condition
343 */
344static void
345htmlSaveErrMemory(const char *extra)
346{
347 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
348}
349
350/**
351 * htmlSaveErr:
352 * @code: the error number
353 * @node: the location of the error.
354 * @extra: extra informations
355 *
356 * Handle an out of memory condition
357 */
358static void
359htmlSaveErr(int code, xmlNodePtr node, const char *extra)
360{
361 const char *msg = NULL;
362
363 switch(code) {
364 case XML_SAVE_NOT_UTF8:
Rob Richards417b74d2006-08-15 23:14:24 +0000365 msg = "string is not in UTF-8\n";
Daniel Veillarde2238d52003-10-09 13:14:55 +0000366 break;
367 case XML_SAVE_CHAR_INVALID:
Rob Richards417b74d2006-08-15 23:14:24 +0000368 msg = "invalid character value\n";
Daniel Veillarde2238d52003-10-09 13:14:55 +0000369 break;
370 case XML_SAVE_UNKNOWN_ENCODING:
Rob Richards417b74d2006-08-15 23:14:24 +0000371 msg = "unknown encoding %s\n";
Daniel Veillarde2238d52003-10-09 13:14:55 +0000372 break;
373 case XML_SAVE_NO_DOCTYPE:
Rob Richards417b74d2006-08-15 23:14:24 +0000374 msg = "HTML has no DOCTYPE\n";
Daniel Veillarde2238d52003-10-09 13:14:55 +0000375 break;
376 default:
Rob Richards417b74d2006-08-15 23:14:24 +0000377 msg = "unexpected error number\n";
Daniel Veillarde2238d52003-10-09 13:14:55 +0000378 }
379 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
380}
381
382/************************************************************************
383 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000384 * Dumping HTML tree content to a simple buffer *
385 * *
386 ************************************************************************/
387
Daniel Veillard8db67d22002-11-27 19:39:27 +0000388static int
Daniel Veillard86fd5a72001-12-13 14:55:21 +0000389htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
390 int format);
Owen Taylor3473f882001-02-23 17:55:21 +0000391
392/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000393 * htmlNodeDumpFormat:
Owen Taylor3473f882001-02-23 17:55:21 +0000394 * @buf: the HTML buffer output
395 * @doc: the document
396 * @cur: the current node
Daniel Veillard95d845f2001-06-13 13:48:46 +0000397 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000398 *
399 * Dump an HTML node, recursive behaviour,children are printed too.
Daniel Veillard8db67d22002-11-27 19:39:27 +0000400 *
401 * Returns the number of byte written or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +0000402 */
Daniel Veillard8db67d22002-11-27 19:39:27 +0000403static int
Daniel Veillard95d845f2001-06-13 13:48:46 +0000404htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
405 int format) {
Daniel Veillard8db67d22002-11-27 19:39:27 +0000406 unsigned int use;
407 int ret;
408 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +0000409
410 if (cur == NULL) {
Daniel Veillard8db67d22002-11-27 19:39:27 +0000411 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000412 }
Daniel Veillard8db67d22002-11-27 19:39:27 +0000413 if (buf == NULL) {
414 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000415 }
Daniel Veillard8db67d22002-11-27 19:39:27 +0000416 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
417 if (outbuf == NULL) {
Daniel Veillarde2238d52003-10-09 13:14:55 +0000418 htmlSaveErrMemory("allocating HTML output buffer");
Daniel Veillard8db67d22002-11-27 19:39:27 +0000419 return (-1);
420 }
421 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
422 outbuf->buffer = buf;
423 outbuf->encoder = NULL;
424 outbuf->writecallback = NULL;
425 outbuf->closecallback = NULL;
426 outbuf->context = NULL;
427 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000428
Daniel Veillard8db67d22002-11-27 19:39:27 +0000429 use = buf->use;
430 htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
431 xmlFree(outbuf);
432 ret = buf->use - use;
433 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000434}
435
436/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000437 * htmlNodeDump:
438 * @buf: the HTML buffer output
439 * @doc: the document
440 * @cur: the current node
441 *
442 * Dump an HTML node, recursive behaviour,children are printed too,
443 * and formatting returns are added.
Daniel Veillard8db67d22002-11-27 19:39:27 +0000444 *
445 * Returns the number of byte written or -1 in case of error
Daniel Veillard95d845f2001-06-13 13:48:46 +0000446 */
Daniel Veillard8db67d22002-11-27 19:39:27 +0000447int
Daniel Veillard95d845f2001-06-13 13:48:46 +0000448htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000449 xmlInitParser();
450
Daniel Veillard8db67d22002-11-27 19:39:27 +0000451 return(htmlNodeDumpFormat(buf, doc, cur, 1));
Daniel Veillard95d845f2001-06-13 13:48:46 +0000452}
453
454/**
455 * htmlNodeDumpFileFormat:
456 * @out: the FILE pointer
457 * @doc: the document
458 * @cur: the current node
459 * @encoding: the document encoding
460 * @format: should formatting spaces been added
461 *
462 * Dump an HTML node, recursive behaviour,children are printed too.
463 *
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000464 * TODO: if encoding == NULL try to save in the doc encoding
465 *
466 * returns: the number of byte written or -1 in case of failure.
Daniel Veillard95d845f2001-06-13 13:48:46 +0000467 */
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000468int
469htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
470 xmlNodePtr cur, const char *encoding, int format) {
471 xmlOutputBufferPtr buf;
472 xmlCharEncodingHandlerPtr handler = NULL;
473 int ret;
Daniel Veillard95d845f2001-06-13 13:48:46 +0000474
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000475 xmlInitParser();
476
Daniel Veillardc4f631d2001-06-14 11:11:59 +0000477 if (encoding != NULL) {
478 xmlCharEncoding enc;
479
480 enc = xmlParseCharEncoding(encoding);
481 if (enc != XML_CHAR_ENCODING_UTF8) {
482 handler = xmlFindCharEncodingHandler(encoding);
483 if (handler == NULL)
484 return(-1);
485 }
486 }
487
488 /*
489 * Fallback to HTML or ASCII when the encoding is unspecified
490 */
491 if (handler == NULL)
492 handler = xmlFindCharEncodingHandler("HTML");
493 if (handler == NULL)
494 handler = xmlFindCharEncodingHandler("ascii");
495
496 /*
497 * save the content to a temp buffer.
498 */
499 buf = xmlOutputBufferCreateFile(out, handler);
500 if (buf == NULL) return(0);
501
502 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
503
504 ret = xmlOutputBufferClose(buf);
505 return(ret);
Daniel Veillard95d845f2001-06-13 13:48:46 +0000506}
507
508/**
Owen Taylor3473f882001-02-23 17:55:21 +0000509 * htmlNodeDumpFile:
510 * @out: the FILE pointer
511 * @doc: the document
512 * @cur: the current node
513 *
Daniel Veillard95d845f2001-06-13 13:48:46 +0000514 * Dump an HTML node, recursive behaviour,children are printed too,
515 * and formatting returns are added.
Owen Taylor3473f882001-02-23 17:55:21 +0000516 */
517void
518htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillard95d845f2001-06-13 13:48:46 +0000519 htmlNodeDumpFileFormat(out, doc, cur, NULL, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000520}
521
522/**
Rob Richards77b92ff2005-12-20 15:55:14 +0000523 * htmlDocDumpMemoryFormat:
Owen Taylor3473f882001-02-23 17:55:21 +0000524 * @cur: the document
525 * @mem: OUT: the memory pointer
Daniel Veillard2d703722001-05-30 18:32:34 +0000526 * @size: OUT: the memory length
Rob Richards77b92ff2005-12-20 15:55:14 +0000527 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000528 *
529 * Dump an HTML document in memory and return the xmlChar * and it's size.
530 * It's up to the caller to free the memory.
531 */
532void
Rob Richards77b92ff2005-12-20 15:55:14 +0000533htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
Daniel Veillard2d703722001-05-30 18:32:34 +0000534 xmlOutputBufferPtr buf;
535 xmlCharEncodingHandlerPtr handler = NULL;
536 const char *encoding;
Owen Taylor3473f882001-02-23 17:55:21 +0000537
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000538 xmlInitParser();
539
Daniel Veillardd5cc0f72004-11-06 19:24:28 +0000540 if ((mem == NULL) || (size == NULL))
541 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000542 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000543 *mem = NULL;
544 *size = 0;
545 return;
546 }
Daniel Veillard2d703722001-05-30 18:32:34 +0000547
548 encoding = (const char *) htmlGetMetaEncoding(cur);
549
550 if (encoding != NULL) {
551 xmlCharEncoding enc;
552
553 enc = xmlParseCharEncoding(encoding);
554 if (enc != cur->charset) {
555 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
556 /*
557 * Not supported yet
558 */
559 *mem = NULL;
560 *size = 0;
561 return;
562 }
563
564 handler = xmlFindCharEncodingHandler(encoding);
565 if (handler == NULL) {
566 *mem = NULL;
567 *size = 0;
568 return;
569 }
Daniel Veillardb8c80162005-08-08 13:46:45 +0000570 } else {
571 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard2d703722001-05-30 18:32:34 +0000572 }
573 }
574
575 /*
576 * Fallback to HTML or ASCII when the encoding is unspecified
577 */
578 if (handler == NULL)
579 handler = xmlFindCharEncodingHandler("HTML");
580 if (handler == NULL)
581 handler = xmlFindCharEncodingHandler("ascii");
582
Daniel Veillardda3fee42008-09-01 13:08:57 +0000583 buf = xmlAllocOutputBufferInternal(handler);
Owen Taylor3473f882001-02-23 17:55:21 +0000584 if (buf == NULL) {
585 *mem = NULL;
586 *size = 0;
587 return;
588 }
Daniel Veillard2d703722001-05-30 18:32:34 +0000589
Rob Richards77b92ff2005-12-20 15:55:14 +0000590 htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
591
Daniel Veillard2d703722001-05-30 18:32:34 +0000592 xmlOutputBufferFlush(buf);
593 if (buf->conv != NULL) {
594 *size = buf->conv->use;
595 *mem = xmlStrndup(buf->conv->content, *size);
596 } else {
597 *size = buf->buffer->use;
598 *mem = xmlStrndup(buf->buffer->content, *size);
599 }
600 (void)xmlOutputBufferClose(buf);
Owen Taylor3473f882001-02-23 17:55:21 +0000601}
602
Rob Richards77b92ff2005-12-20 15:55:14 +0000603/**
604 * htmlDocDumpMemory:
605 * @cur: the document
606 * @mem: OUT: the memory pointer
607 * @size: OUT: the memory length
608 *
609 * Dump an HTML document in memory and return the xmlChar * and it's size.
610 * It's up to the caller to free the memory.
611 */
612void
613htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
614 htmlDocDumpMemoryFormat(cur, mem, size, 1);
615}
616
Owen Taylor3473f882001-02-23 17:55:21 +0000617
618/************************************************************************
619 * *
620 * Dumping HTML tree content to an I/O output buffer *
621 * *
622 ************************************************************************/
623
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000624void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
Daniel Veillardc084e472002-08-12 13:27:28 +0000625
Owen Taylor3473f882001-02-23 17:55:21 +0000626/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000627 * htmlDtdDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000628 * @buf: the HTML buffer output
629 * @doc: the document
630 * @encoding: the encoding string
631 *
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000632 * TODO: check whether encoding is needed
633 *
Owen Taylor3473f882001-02-23 17:55:21 +0000634 * Dump the HTML document DTD, if any.
635 */
636static void
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000637htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000638 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000639 xmlDtdPtr cur = doc->intSubset;
640
641 if (cur == NULL) {
Daniel Veillarde2238d52003-10-09 13:14:55 +0000642 htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000643 return;
644 }
645 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
646 xmlOutputBufferWriteString(buf, (const char *)cur->name);
647 if (cur->ExternalID != NULL) {
648 xmlOutputBufferWriteString(buf, " PUBLIC ");
649 xmlBufferWriteQuotedString(buf->buffer, cur->ExternalID);
650 if (cur->SystemID != NULL) {
651 xmlOutputBufferWriteString(buf, " ");
652 xmlBufferWriteQuotedString(buf->buffer, cur->SystemID);
653 }
654 } else if (cur->SystemID != NULL) {
655 xmlOutputBufferWriteString(buf, " SYSTEM ");
656 xmlBufferWriteQuotedString(buf->buffer, cur->SystemID);
657 }
658 xmlOutputBufferWriteString(buf, ">\n");
659}
660
661/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000662 * htmlAttrDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000663 * @buf: the HTML buffer output
664 * @doc: the document
665 * @cur: the attribute pointer
666 * @encoding: the encoding string
667 *
668 * Dump an HTML attribute
669 */
670static void
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000671htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000672 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000673 xmlChar *value;
674
Daniel Veillardeca60d02001-06-13 07:45:41 +0000675 /*
676 * TODO: The html output method should not escape a & character
677 * occurring in an attribute value immediately followed by
678 * a { character (see Section B.7.1 of the HTML 4.0 Recommendation).
679 */
680
Owen Taylor3473f882001-02-23 17:55:21 +0000681 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000682 return;
683 }
684 xmlOutputBufferWriteString(buf, " ");
William M. Brack3a6da762003-09-15 04:58:14 +0000685 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
686 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
687 xmlOutputBufferWriteString(buf, ":");
688 }
Owen Taylor3473f882001-02-23 17:55:21 +0000689 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillardc084e472002-08-12 13:27:28 +0000690 if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000691 value = xmlNodeListGetString(doc, cur->children, 0);
692 if (value) {
693 xmlOutputBufferWriteString(buf, "=");
Daniel Veillardc7e9b192003-03-27 14:08:24 +0000694 if ((cur->ns == NULL) && (cur->parent != NULL) &&
695 (cur->parent->ns == NULL) &&
696 ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) ||
697 (!xmlStrcasecmp(cur->name, BAD_CAST "action")) ||
Daniel Veillardaa9a9832005-03-29 20:30:17 +0000698 (!xmlStrcasecmp(cur->name, BAD_CAST "src")) ||
699 ((!xmlStrcasecmp(cur->name, BAD_CAST "name")) &&
700 (!xmlStrcasecmp(cur->parent->name, BAD_CAST "a"))))) {
Daniel Veillardeb475a32002-04-14 22:00:22 +0000701 xmlChar *escaped;
702 xmlChar *tmp = value;
703
William M. Brack76e95df2003-10-18 16:20:14 +0000704 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillardeb475a32002-04-14 22:00:22 +0000705
Daniel Veillard5f5b7bb2003-05-16 17:19:40 +0000706 escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+");
Daniel Veillardeb475a32002-04-14 22:00:22 +0000707 if (escaped != NULL) {
708 xmlBufferWriteQuotedString(buf->buffer, escaped);
709 xmlFree(escaped);
710 } else {
711 xmlBufferWriteQuotedString(buf->buffer, value);
712 }
713 } else {
714 xmlBufferWriteQuotedString(buf->buffer, value);
715 }
Owen Taylor3473f882001-02-23 17:55:21 +0000716 xmlFree(value);
717 } else {
718 xmlOutputBufferWriteString(buf, "=\"\"");
719 }
720 }
721}
722
723/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000724 * htmlAttrListDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000725 * @buf: the HTML buffer output
726 * @doc: the document
727 * @cur: the first attribute pointer
728 * @encoding: the encoding string
729 *
730 * Dump a list of HTML attributes
731 */
732static void
733htmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) {
734 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000735 return;
736 }
737 while (cur != NULL) {
738 htmlAttrDumpOutput(buf, doc, cur, encoding);
739 cur = cur->next;
740 }
741}
742
743
Owen Taylor3473f882001-02-23 17:55:21 +0000744
745/**
Daniel Veillardeca60d02001-06-13 07:45:41 +0000746 * htmlNodeListDumpOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000747 * @buf: the HTML buffer output
748 * @doc: the document
749 * @cur: the first node
750 * @encoding: the encoding string
Daniel Veillard95d845f2001-06-13 13:48:46 +0000751 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000752 *
753 * Dump an HTML node list, recursive behaviour,children are printed too.
754 */
755static void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000756htmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
757 xmlNodePtr cur, const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +0000758 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000759 return;
760 }
761 while (cur != NULL) {
Daniel Veillard95d845f2001-06-13 13:48:46 +0000762 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +0000763 cur = cur->next;
764 }
765}
766
767/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000768 * htmlNodeDumpFormatOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000769 * @buf: the HTML buffer output
770 * @doc: the document
771 * @cur: the current node
772 * @encoding: the encoding string
Daniel Veillard95d845f2001-06-13 13:48:46 +0000773 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000774 *
775 * Dump an HTML node, recursive behaviour,children are printed too.
776 */
777void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000778htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
779 xmlNodePtr cur, const char *encoding, int format) {
Daniel Veillardbb371292001-08-16 23:26:59 +0000780 const htmlElemDesc * info;
Owen Taylor3473f882001-02-23 17:55:21 +0000781
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000782 xmlInitParser();
783
Daniel Veillardce244ad2004-11-05 10:03:46 +0000784 if ((cur == NULL) || (buf == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000785 return;
786 }
787 /*
788 * Special cases.
789 */
790 if (cur->type == XML_DTD_NODE)
791 return;
Daniel Veillardce244ad2004-11-05 10:03:46 +0000792 if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
793 (cur->type == XML_DOCUMENT_NODE)){
Owen Taylor3473f882001-02-23 17:55:21 +0000794 htmlDocContentDumpOutput(buf, (xmlDocPtr) cur, encoding);
795 return;
796 }
Daniel Veillardfcd02ad2007-06-12 09:49:40 +0000797 if (cur->type == XML_ATTRIBUTE_NODE) {
798 htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur, encoding);
799 return;
800 }
Owen Taylor3473f882001-02-23 17:55:21 +0000801 if (cur->type == HTML_TEXT_NODE) {
802 if (cur->content != NULL) {
Daniel Veillardb44025c2001-10-11 22:55:55 +0000803 if (((cur->name == (const xmlChar *)xmlStringText) ||
804 (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
Daniel Veillard6e93c4a2001-06-05 20:57:42 +0000805 ((cur->parent == NULL) ||
Daniel Veillard44892f72002-10-16 15:23:26 +0000806 ((xmlStrcasecmp(cur->parent->name, BAD_CAST "script")) &&
807 (xmlStrcasecmp(cur->parent->name, BAD_CAST "style"))))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000808 xmlChar *buffer;
809
Owen Taylor3473f882001-02-23 17:55:21 +0000810 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000811 if (buffer != NULL) {
812 xmlOutputBufferWriteString(buf, (const char *)buffer);
813 xmlFree(buffer);
814 }
815 } else {
816 xmlOutputBufferWriteString(buf, (const char *)cur->content);
817 }
818 }
819 return;
820 }
821 if (cur->type == HTML_COMMENT_NODE) {
822 if (cur->content != NULL) {
823 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +0000824 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000825 xmlOutputBufferWriteString(buf, "-->");
826 }
827 return;
828 }
Daniel Veillard7533cc82001-04-24 15:52:00 +0000829 if (cur->type == HTML_PI_NODE) {
Daniel Veillard5146f202001-04-25 10:29:44 +0000830 if (cur->name == NULL)
831 return;
832 xmlOutputBufferWriteString(buf, "<?");
833 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard7533cc82001-04-24 15:52:00 +0000834 if (cur->content != NULL) {
Daniel Veillard5146f202001-04-25 10:29:44 +0000835 xmlOutputBufferWriteString(buf, " ");
Daniel Veillard7533cc82001-04-24 15:52:00 +0000836 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Daniel Veillard7533cc82001-04-24 15:52:00 +0000837 }
Daniel Veillard5146f202001-04-25 10:29:44 +0000838 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7533cc82001-04-24 15:52:00 +0000839 return;
840 }
Owen Taylor3473f882001-02-23 17:55:21 +0000841 if (cur->type == HTML_ENTITY_REF_NODE) {
842 xmlOutputBufferWriteString(buf, "&");
843 xmlOutputBufferWriteString(buf, (const char *)cur->name);
844 xmlOutputBufferWriteString(buf, ";");
845 return;
846 }
847 if (cur->type == HTML_PRESERVE_NODE) {
848 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000849 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000850 }
851 return;
852 }
853
854 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000855 * Get specific HTML info for that node.
Owen Taylor3473f882001-02-23 17:55:21 +0000856 */
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000857 if (cur->ns == NULL)
858 info = htmlTagLookup(cur->name);
859 else
860 info = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000861
862 xmlOutputBufferWriteString(buf, "<");
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000863 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
864 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
865 xmlOutputBufferWriteString(buf, ":");
866 }
Owen Taylor3473f882001-02-23 17:55:21 +0000867 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000868 if (cur->nsDef)
869 xmlNsListDumpOutput(buf, cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +0000870 if (cur->properties != NULL)
871 htmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
872
873 if ((info != NULL) && (info->empty)) {
874 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard02bb1702001-06-13 21:11:59 +0000875 if ((format) && (!info->isinline) && (cur->next != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000876 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillard8a926292001-06-07 11:20:20 +0000877 (cur->next->type != HTML_ENTITY_REF_NODE) &&
878 (cur->parent != NULL) &&
Daniel Veillard42fd4122003-11-04 08:47:48 +0000879 (cur->parent->name != NULL) &&
880 (cur->parent->name[0] != 'p')) /* p, pre, param */
Owen Taylor3473f882001-02-23 17:55:21 +0000881 xmlOutputBufferWriteString(buf, "\n");
882 }
883 return;
884 }
Daniel Veillard7db37732001-07-12 01:20:08 +0000885 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
886 (cur->children == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000887 if ((info != NULL) && (info->saveEndTag != 0) &&
Daniel Veillardeca60d02001-06-13 07:45:41 +0000888 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
889 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000890 xmlOutputBufferWriteString(buf, ">");
891 } else {
892 xmlOutputBufferWriteString(buf, "></");
Daniel Veillard645c6902003-04-10 21:40:49 +0000893 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
894 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
895 xmlOutputBufferWriteString(buf, ":");
896 }
Owen Taylor3473f882001-02-23 17:55:21 +0000897 xmlOutputBufferWriteString(buf, (const char *)cur->name);
898 xmlOutputBufferWriteString(buf, ">");
899 }
Daniel Veillard02bb1702001-06-13 21:11:59 +0000900 if ((format) && (cur->next != NULL) &&
901 (info != NULL) && (!info->isinline)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000902 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillard8a926292001-06-07 11:20:20 +0000903 (cur->next->type != HTML_ENTITY_REF_NODE) &&
904 (cur->parent != NULL) &&
Daniel Veillard42fd4122003-11-04 08:47:48 +0000905 (cur->parent->name != NULL) &&
906 (cur->parent->name[0] != 'p')) /* p, pre, param */
Owen Taylor3473f882001-02-23 17:55:21 +0000907 xmlOutputBufferWriteString(buf, "\n");
908 }
909 return;
910 }
911 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +0000912 if ((cur->type != XML_ELEMENT_NODE) &&
913 (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000914 /*
915 * Uses the OutputBuffer property to automatically convert
916 * invalids to charrefs
917 */
918
Owen Taylor3473f882001-02-23 17:55:21 +0000919 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000920 }
921 if (cur->children != NULL) {
Daniel Veillard02bb1702001-06-13 21:11:59 +0000922 if ((format) && (info != NULL) && (!info->isinline) &&
923 (cur->children->type != HTML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000924 (cur->children->type != HTML_ENTITY_REF_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000925 (cur->children != cur->last) &&
Daniel Veillard42fd4122003-11-04 08:47:48 +0000926 (cur->name != NULL) &&
927 (cur->name[0] != 'p')) /* p, pre, param */
Owen Taylor3473f882001-02-23 17:55:21 +0000928 xmlOutputBufferWriteString(buf, "\n");
Daniel Veillard95d845f2001-06-13 13:48:46 +0000929 htmlNodeListDumpOutput(buf, doc, cur->children, encoding, format);
Daniel Veillard02bb1702001-06-13 21:11:59 +0000930 if ((format) && (info != NULL) && (!info->isinline) &&
931 (cur->last->type != HTML_TEXT_NODE) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000932 (cur->last->type != HTML_ENTITY_REF_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000933 (cur->children != cur->last) &&
Daniel Veillard42fd4122003-11-04 08:47:48 +0000934 (cur->name != NULL) &&
935 (cur->name[0] != 'p')) /* p, pre, param */
Owen Taylor3473f882001-02-23 17:55:21 +0000936 xmlOutputBufferWriteString(buf, "\n");
937 }
Owen Taylor3473f882001-02-23 17:55:21 +0000938 xmlOutputBufferWriteString(buf, "</");
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +0000939 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
940 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
941 xmlOutputBufferWriteString(buf, ":");
942 }
Owen Taylor3473f882001-02-23 17:55:21 +0000943 xmlOutputBufferWriteString(buf, (const char *)cur->name);
944 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard02bb1702001-06-13 21:11:59 +0000945 if ((format) && (info != NULL) && (!info->isinline) &&
946 (cur->next != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000947 if ((cur->next->type != HTML_TEXT_NODE) &&
Daniel Veillardf0c53762001-06-07 16:07:07 +0000948 (cur->next->type != HTML_ENTITY_REF_NODE) &&
949 (cur->parent != NULL) &&
Daniel Veillard42fd4122003-11-04 08:47:48 +0000950 (cur->parent->name != NULL) &&
951 (cur->parent->name[0] != 'p')) /* p, pre, param */
Owen Taylor3473f882001-02-23 17:55:21 +0000952 xmlOutputBufferWriteString(buf, "\n");
953 }
954}
955
956/**
Daniel Veillard95d845f2001-06-13 13:48:46 +0000957 * htmlNodeDumpOutput:
958 * @buf: the HTML buffer output
959 * @doc: the document
960 * @cur: the current node
961 * @encoding: the encoding string
962 *
963 * Dump an HTML node, recursive behaviour,children are printed too,
964 * and formatting returns/spaces are added.
965 */
966void
967htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
968 xmlNodePtr cur, const char *encoding) {
969 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, 1);
970}
971
972/**
973 * htmlDocContentDumpFormatOutput:
Owen Taylor3473f882001-02-23 17:55:21 +0000974 * @buf: the HTML buffer output
975 * @cur: the document
976 * @encoding: the encoding string
Daniel Veillard9d06d302002-01-22 18:15:52 +0000977 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +0000978 *
979 * Dump an HTML document.
980 */
981void
Daniel Veillard95d845f2001-06-13 13:48:46 +0000982htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
983 const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +0000984 int type;
985
Daniel Veillard70bcb0e2003-08-08 14:00:28 +0000986 xmlInitParser();
987
Daniel Veillard3d97e662004-11-04 10:49:00 +0000988 if ((buf == NULL) || (cur == NULL))
989 return;
990
Owen Taylor3473f882001-02-23 17:55:21 +0000991 /*
992 * force to output the stuff as HTML, especially for entities
993 */
994 type = cur->type;
995 cur->type = XML_HTML_DOCUMENT_NODE;
Daniel Veillard4dd93462001-04-02 15:16:19 +0000996 if (cur->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000997 htmlDtdDumpOutput(buf, cur, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000998 }
999 if (cur->children != NULL) {
Daniel Veillard95d845f2001-06-13 13:48:46 +00001000 htmlNodeListDumpOutput(buf, cur, cur->children, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00001001 }
1002 xmlOutputBufferWriteString(buf, "\n");
1003 cur->type = (xmlElementType) type;
1004}
1005
Daniel Veillard95d845f2001-06-13 13:48:46 +00001006/**
1007 * htmlDocContentDumpOutput:
1008 * @buf: the HTML buffer output
1009 * @cur: the document
1010 * @encoding: the encoding string
1011 *
1012 * Dump an HTML document. Formating return/spaces are added.
1013 */
1014void
1015htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
1016 const char *encoding) {
1017 htmlDocContentDumpFormatOutput(buf, cur, encoding, 1);
1018}
1019
Owen Taylor3473f882001-02-23 17:55:21 +00001020/************************************************************************
1021 * *
1022 * Saving functions front-ends *
1023 * *
1024 ************************************************************************/
1025
1026/**
1027 * htmlDocDump:
1028 * @f: the FILE*
1029 * @cur: the document
1030 *
1031 * Dump an HTML document to an open FILE.
1032 *
1033 * returns: the number of byte written or -1 in case of failure.
1034 */
1035int
1036htmlDocDump(FILE *f, xmlDocPtr cur) {
1037 xmlOutputBufferPtr buf;
1038 xmlCharEncodingHandlerPtr handler = NULL;
1039 const char *encoding;
1040 int ret;
1041
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001042 xmlInitParser();
1043
Daniel Veillard3d97e662004-11-04 10:49:00 +00001044 if ((cur == NULL) || (f == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001045 return(-1);
1046 }
1047
1048 encoding = (const char *) htmlGetMetaEncoding(cur);
1049
1050 if (encoding != NULL) {
1051 xmlCharEncoding enc;
1052
1053 enc = xmlParseCharEncoding(encoding);
1054 if (enc != cur->charset) {
1055 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1056 /*
1057 * Not supported yet
1058 */
1059 return(-1);
1060 }
1061
1062 handler = xmlFindCharEncodingHandler(encoding);
1063 if (handler == NULL)
1064 return(-1);
Daniel Veillardb8c80162005-08-08 13:46:45 +00001065 } else {
1066 handler = xmlFindCharEncodingHandler(encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001067 }
1068 }
1069
1070 /*
1071 * Fallback to HTML or ASCII when the encoding is unspecified
1072 */
1073 if (handler == NULL)
1074 handler = xmlFindCharEncodingHandler("HTML");
1075 if (handler == NULL)
1076 handler = xmlFindCharEncodingHandler("ascii");
1077
1078 buf = xmlOutputBufferCreateFile(f, handler);
1079 if (buf == NULL) return(-1);
1080 htmlDocContentDumpOutput(buf, cur, NULL);
1081
1082 ret = xmlOutputBufferClose(buf);
1083 return(ret);
1084}
1085
1086/**
1087 * htmlSaveFile:
1088 * @filename: the filename (or URL)
1089 * @cur: the document
1090 *
1091 * Dump an HTML document to a file. If @filename is "-" the stdout file is
1092 * used.
1093 * returns: the number of byte written or -1 in case of failure.
1094 */
1095int
1096htmlSaveFile(const char *filename, xmlDocPtr cur) {
1097 xmlOutputBufferPtr buf;
1098 xmlCharEncodingHandlerPtr handler = NULL;
1099 const char *encoding;
1100 int ret;
1101
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001102 if ((cur == NULL) || (filename == NULL))
1103 return(-1);
1104
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001105 xmlInitParser();
1106
Owen Taylor3473f882001-02-23 17:55:21 +00001107 encoding = (const char *) htmlGetMetaEncoding(cur);
1108
1109 if (encoding != NULL) {
1110 xmlCharEncoding enc;
1111
1112 enc = xmlParseCharEncoding(encoding);
1113 if (enc != cur->charset) {
1114 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1115 /*
1116 * Not supported yet
1117 */
1118 return(-1);
1119 }
1120
1121 handler = xmlFindCharEncodingHandler(encoding);
1122 if (handler == NULL)
1123 return(-1);
1124 }
1125 }
1126
1127 /*
1128 * Fallback to HTML or ASCII when the encoding is unspecified
1129 */
1130 if (handler == NULL)
1131 handler = xmlFindCharEncodingHandler("HTML");
1132 if (handler == NULL)
1133 handler = xmlFindCharEncodingHandler("ascii");
1134
1135 /*
1136 * save the content to a temp buffer.
1137 */
1138 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
1139 if (buf == NULL) return(0);
1140
1141 htmlDocContentDumpOutput(buf, cur, NULL);
1142
1143 ret = xmlOutputBufferClose(buf);
1144 return(ret);
1145}
1146
1147/**
Daniel Veillard95d845f2001-06-13 13:48:46 +00001148 * htmlSaveFileFormat:
Owen Taylor3473f882001-02-23 17:55:21 +00001149 * @filename: the filename
1150 * @cur: the document
Daniel Veillard95d845f2001-06-13 13:48:46 +00001151 * @format: should formatting spaces been added
1152 * @encoding: the document encoding
Owen Taylor3473f882001-02-23 17:55:21 +00001153 *
1154 * Dump an HTML document to a file using a given encoding.
1155 *
1156 * returns: the number of byte written or -1 in case of failure.
1157 */
1158int
Daniel Veillard95d845f2001-06-13 13:48:46 +00001159htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
1160 const char *encoding, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00001161 xmlOutputBufferPtr buf;
1162 xmlCharEncodingHandlerPtr handler = NULL;
1163 int ret;
1164
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001165 if ((cur == NULL) || (filename == NULL))
1166 return(-1);
Daniel Veillard8d7c1b72009-08-12 23:03:23 +02001167
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00001168 xmlInitParser();
1169
Owen Taylor3473f882001-02-23 17:55:21 +00001170 if (encoding != NULL) {
1171 xmlCharEncoding enc;
1172
1173 enc = xmlParseCharEncoding(encoding);
1174 if (enc != cur->charset) {
1175 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1176 /*
1177 * Not supported yet
1178 */
1179 return(-1);
1180 }
1181
1182 handler = xmlFindCharEncodingHandler(encoding);
1183 if (handler == NULL)
1184 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001185 }
Daniel Veillard8d7c1b72009-08-12 23:03:23 +02001186 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
Daniel Veillard4dd93462001-04-02 15:16:19 +00001187 } else {
1188 htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
Owen Taylor3473f882001-02-23 17:55:21 +00001189 }
1190
1191 /*
1192 * Fallback to HTML or ASCII when the encoding is unspecified
1193 */
1194 if (handler == NULL)
1195 handler = xmlFindCharEncodingHandler("HTML");
1196 if (handler == NULL)
1197 handler = xmlFindCharEncodingHandler("ascii");
1198
1199 /*
1200 * save the content to a temp buffer.
1201 */
1202 buf = xmlOutputBufferCreateFilename(filename, handler, 0);
1203 if (buf == NULL) return(0);
1204
Daniel Veillard95d845f2001-06-13 13:48:46 +00001205 htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00001206
1207 ret = xmlOutputBufferClose(buf);
1208 return(ret);
1209}
Daniel Veillard95d845f2001-06-13 13:48:46 +00001210
1211/**
1212 * htmlSaveFileEnc:
1213 * @filename: the filename
1214 * @cur: the document
1215 * @encoding: the document encoding
1216 *
1217 * Dump an HTML document to a file using a given encoding
1218 * and formatting returns/spaces are added.
1219 *
1220 * returns: the number of byte written or -1 in case of failure.
1221 */
1222int
1223htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
1224 return(htmlSaveFileFormat(filename, cur, encoding, 1));
1225}
1226
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001227#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardc084e472002-08-12 13:27:28 +00001228
Daniel Veillard5d4644e2005-04-01 13:11:58 +00001229#define bottom_HTMLtree
1230#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00001231#endif /* LIBXML_HTML_ENABLED */