Don't preload rarely seen large images
[chromium-blink-merge.git] / third_party / libxml / src / HTMLtree.c
blob5c57fc572bc3236cc2ab538779cb58b7107ac7e9
1 /*
2 * HTMLtree.c : implementation of access function for an HTML tree.
4 * See Copyright for the status of this software.
6 * daniel@veillard.com
7 */
10 #define IN_LIBXML
11 #include "libxml.h"
12 #ifdef LIBXML_HTML_ENABLED
14 #include <string.h> /* for memset() only ! */
16 #ifdef HAVE_CTYPE_H
17 #include <ctype.h>
18 #endif
19 #ifdef HAVE_STDLIB_H
20 #include <stdlib.h>
21 #endif
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>
30 #include <libxml/globals.h>
31 #include <libxml/uri.h>
33 #include "buf.h"
35 /************************************************************************
36 * *
37 * Getting/Setting encoding meta tags *
38 * *
39 ************************************************************************/
41 /**
42 * htmlGetMetaEncoding:
43 * @doc: the document
45 * Encoding definition lookup in the Meta tags
47 * Returns the current encoding as flagged in the HTML source
49 const xmlChar *
50 htmlGetMetaEncoding(htmlDocPtr doc) {
51 htmlNodePtr cur;
52 const xmlChar *content;
53 const xmlChar *encoding;
55 if (doc == NULL)
56 return(NULL);
57 cur = doc->children;
60 * Search the html
62 while (cur != NULL) {
63 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
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;
71 cur = cur->next;
73 if (cur == NULL)
74 return(NULL);
75 cur = cur->children;
78 * Search the head
80 while (cur != NULL) {
81 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
82 if (xmlStrEqual(cur->name, BAD_CAST"head"))
83 break;
84 if (xmlStrEqual(cur->name, BAD_CAST"meta"))
85 goto found_meta;
87 cur = cur->next;
89 if (cur == NULL)
90 return(NULL);
91 found_head:
92 cur = cur->children;
95 * Search the meta elements
97 found_meta:
98 while (cur != NULL) {
99 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
100 if (xmlStrEqual(cur->name, BAD_CAST"meta")) {
101 xmlAttrPtr attr = cur->properties;
102 int http;
103 const xmlChar *value;
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)) {
111 value = attr->children->content;
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;
121 attr = attr->next;
125 cur = cur->next;
127 return(NULL);
129 found_content:
130 encoding = xmlStrstr(content, BAD_CAST"charset=");
131 if (encoding == NULL)
132 encoding = xmlStrstr(content, BAD_CAST"Charset=");
133 if (encoding == NULL)
134 encoding = xmlStrstr(content, BAD_CAST"CHARSET=");
135 if (encoding != NULL) {
136 encoding += 8;
137 } else {
138 encoding = xmlStrstr(content, BAD_CAST"charset =");
139 if (encoding == NULL)
140 encoding = xmlStrstr(content, BAD_CAST"Charset =");
141 if (encoding == NULL)
142 encoding = xmlStrstr(content, BAD_CAST"CHARSET =");
143 if (encoding != NULL)
144 encoding += 9;
146 if (encoding != NULL) {
147 while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
149 return(encoding);
153 * htmlSetMetaEncoding:
154 * @doc: the document
155 * @encoding: the encoding string
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.
161 * Returns 0 in case of success and -1 in case of error
164 htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
165 htmlNodePtr cur, meta = NULL, head = NULL;
166 const xmlChar *content = NULL;
167 char newcontent[100];
169 newcontent[0] = 0;
171 if (doc == NULL)
172 return(-1);
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);
178 if (encoding != NULL) {
179 snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s",
180 (char *)encoding);
181 newcontent[sizeof(newcontent) - 1] = 0;
184 cur = doc->children;
187 * Search the html
189 while (cur != NULL) {
190 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
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;
198 cur = cur->next;
200 if (cur == NULL)
201 return(-1);
202 cur = cur->children;
205 * Search the head
207 while (cur != NULL) {
208 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
209 if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
210 break;
211 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
212 head = cur->parent;
213 goto found_meta;
216 cur = cur->next;
218 if (cur == NULL)
219 return(-1);
220 found_head:
221 head = cur;
222 if (cur->children == NULL)
223 goto create;
224 cur = cur->children;
226 found_meta:
228 * Search and update all the remaining the meta elements carrying
229 * encoding informations
231 while (cur != NULL) {
232 if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
233 if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
234 xmlAttrPtr attr = cur->properties;
235 int http;
236 const xmlChar *value;
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)) {
244 value = attr->children->content;
245 if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
246 && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
247 http = 1;
248 else
250 if ((value != NULL) &&
251 (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
252 content = value;
254 if ((http != 0) && (content != NULL))
255 break;
257 attr = attr->next;
259 if ((http != 0) && (content != NULL)) {
260 meta = cur;
261 break;
266 cur = cur->next;
268 create:
269 if (meta == NULL) {
270 if ((encoding != NULL) && (head != NULL)) {
272 * Create a new Meta element with the right attributes
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);
283 } else {
284 /* remove the meta tag if NULL is passed */
285 if (encoding == NULL) {
286 xmlUnlinkNode(meta);
287 xmlFreeNode(meta);
289 /* change the document only if there is a real encoding change */
290 else if (xmlStrcasestr(content, encoding) == NULL) {
291 xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent);
296 return(0);
300 * booleanHTMLAttrs:
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"
307 static const char* htmlBooleanAttrs[] = {
308 "checked", "compact", "declare", "defer", "disabled", "ismap",
309 "multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
310 "selected", NULL
315 * htmlIsBooleanAttr:
316 * @name: the name of the attribute to check
318 * Determine if a given attribute is a boolean attribute.
320 * returns: false if the attribute is not boolean, true otherwise.
323 htmlIsBooleanAttr(const xmlChar *name)
325 int i = 0;
327 while (htmlBooleanAttrs[i] != NULL) {
328 if (xmlStrcasecmp((const xmlChar *)htmlBooleanAttrs[i], name) == 0)
329 return 1;
330 i++;
332 return 0;
335 #ifdef LIBXML_OUTPUT_ENABLED
337 * private routine exported from xmlIO.c
339 xmlOutputBufferPtr
340 xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder);
341 /************************************************************************
343 * Output error handlers *
345 ************************************************************************/
347 * htmlSaveErrMemory:
348 * @extra: extra informations
350 * Handle an out of memory condition
352 static void
353 htmlSaveErrMemory(const char *extra)
355 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
359 * htmlSaveErr:
360 * @code: the error number
361 * @node: the location of the error.
362 * @extra: extra informations
364 * Handle an out of memory condition
366 static void
367 htmlSaveErr(int code, xmlNodePtr node, const char *extra)
369 const char *msg = NULL;
371 switch(code) {
372 case XML_SAVE_NOT_UTF8:
373 msg = "string is not in UTF-8\n";
374 break;
375 case XML_SAVE_CHAR_INVALID:
376 msg = "invalid character value\n";
377 break;
378 case XML_SAVE_UNKNOWN_ENCODING:
379 msg = "unknown encoding %s\n";
380 break;
381 case XML_SAVE_NO_DOCTYPE:
382 msg = "HTML has no DOCTYPE\n";
383 break;
384 default:
385 msg = "unexpected error number\n";
387 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
390 /************************************************************************
392 * Dumping HTML tree content to a simple buffer *
394 ************************************************************************/
397 * htmlBufNodeDumpFormat:
398 * @buf: the xmlBufPtr output
399 * @doc: the document
400 * @cur: the current node
401 * @format: should formatting spaces been added
403 * Dump an HTML node, recursive behaviour,children are printed too.
405 * Returns the number of byte written or -1 in case of error
407 static size_t
408 htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur,
409 int format) {
410 size_t use;
411 int ret;
412 xmlOutputBufferPtr outbuf;
414 if (cur == NULL) {
415 return (-1);
417 if (buf == NULL) {
418 return (-1);
420 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
421 if (outbuf == NULL) {
422 htmlSaveErrMemory("allocating HTML output buffer");
423 return (-1);
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;
433 use = xmlBufUse(buf);
434 htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
435 xmlFree(outbuf);
436 ret = xmlBufUse(buf) - use;
437 return (ret);
441 * htmlNodeDump:
442 * @buf: the HTML buffer output
443 * @doc: the document
444 * @cur: the current node
446 * Dump an HTML node, recursive behaviour,children are printed too,
447 * and formatting returns are added.
449 * Returns the number of byte written or -1 in case of error
452 htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
453 xmlBufPtr buffer;
454 size_t ret;
456 if ((buf == NULL) || (cur == NULL))
457 return(-1);
459 xmlInitParser();
460 buffer = xmlBufFromBuffer(buf);
461 if (buffer == NULL)
462 return(-1);
464 ret = htmlBufNodeDumpFormat(buffer, doc, cur, 1);
466 xmlBufBackToBuffer(buffer);
468 if (ret > INT_MAX)
469 return(-1);
470 return((int) ret);
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
481 * Dump an HTML node, recursive behaviour,children are printed too.
483 * TODO: if encoding == NULL try to save in the doc encoding
485 * returns: the number of byte written or -1 in case of failure.
488 htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
489 xmlNodePtr cur, const char *encoding, int format) {
490 xmlOutputBufferPtr buf;
491 xmlCharEncodingHandlerPtr handler = NULL;
492 int ret;
494 xmlInitParser();
496 if (encoding != NULL) {
497 xmlCharEncoding enc;
499 enc = xmlParseCharEncoding(encoding);
500 if (enc != XML_CHAR_ENCODING_UTF8) {
501 handler = xmlFindCharEncodingHandler(encoding);
502 if (handler == NULL)
503 htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
508 * Fallback to HTML or ASCII when the encoding is unspecified
510 if (handler == NULL)
511 handler = xmlFindCharEncodingHandler("HTML");
512 if (handler == NULL)
513 handler = xmlFindCharEncodingHandler("ascii");
516 * save the content to a temp buffer.
518 buf = xmlOutputBufferCreateFile(out, handler);
519 if (buf == NULL) return(0);
521 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
523 ret = xmlOutputBufferClose(buf);
524 return(ret);
528 * htmlNodeDumpFile:
529 * @out: the FILE pointer
530 * @doc: the document
531 * @cur: the current node
533 * Dump an HTML node, recursive behaviour,children are printed too,
534 * and formatting returns are added.
536 void
537 htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) {
538 htmlNodeDumpFileFormat(out, doc, cur, NULL, 1);
542 * htmlDocDumpMemoryFormat:
543 * @cur: the document
544 * @mem: OUT: the memory pointer
545 * @size: OUT: the memory length
546 * @format: should formatting spaces been added
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.
551 void
552 htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
553 xmlOutputBufferPtr buf;
554 xmlCharEncodingHandlerPtr handler = NULL;
555 const char *encoding;
557 xmlInitParser();
559 if ((mem == NULL) || (size == NULL))
560 return;
561 if (cur == NULL) {
562 *mem = NULL;
563 *size = 0;
564 return;
567 encoding = (const char *) htmlGetMetaEncoding(cur);
569 if (encoding != NULL) {
570 xmlCharEncoding enc;
572 enc = xmlParseCharEncoding(encoding);
573 if (enc != cur->charset) {
574 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
576 * Not supported yet
578 *mem = NULL;
579 *size = 0;
580 return;
583 handler = xmlFindCharEncodingHandler(encoding);
584 if (handler == NULL)
585 htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
587 } else {
588 handler = xmlFindCharEncodingHandler(encoding);
593 * Fallback to HTML or ASCII when the encoding is unspecified
595 if (handler == NULL)
596 handler = xmlFindCharEncodingHandler("HTML");
597 if (handler == NULL)
598 handler = xmlFindCharEncodingHandler("ascii");
600 buf = xmlAllocOutputBufferInternal(handler);
601 if (buf == NULL) {
602 *mem = NULL;
603 *size = 0;
604 return;
607 htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
609 xmlOutputBufferFlush(buf);
610 if (buf->conv != NULL) {
611 *size = xmlBufUse(buf->conv);
612 *mem = xmlStrndup(xmlBufContent(buf->conv), *size);
613 } else {
614 *size = xmlBufUse(buf->buffer);
615 *mem = xmlStrndup(xmlBufContent(buf->buffer), *size);
617 (void)xmlOutputBufferClose(buf);
621 * htmlDocDumpMemory:
622 * @cur: the document
623 * @mem: OUT: the memory pointer
624 * @size: OUT: the memory length
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.
629 void
630 htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
631 htmlDocDumpMemoryFormat(cur, mem, size, 1);
635 /************************************************************************
637 * Dumping HTML tree content to an I/O output buffer *
639 ************************************************************************/
641 void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
644 * htmlDtdDumpOutput:
645 * @buf: the HTML buffer output
646 * @doc: the document
647 * @encoding: the encoding string
649 * TODO: check whether encoding is needed
651 * Dump the HTML document DTD, if any.
653 static void
654 htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
655 const char *encoding ATTRIBUTE_UNUSED) {
656 xmlDtdPtr cur = doc->intSubset;
658 if (cur == NULL) {
659 htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL);
660 return;
662 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
663 xmlOutputBufferWriteString(buf, (const char *)cur->name);
664 if (cur->ExternalID != NULL) {
665 xmlOutputBufferWriteString(buf, " PUBLIC ");
666 xmlBufWriteQuotedString(buf->buffer, cur->ExternalID);
667 if (cur->SystemID != NULL) {
668 xmlOutputBufferWriteString(buf, " ");
669 xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
671 } else if (cur->SystemID != NULL) {
672 xmlOutputBufferWriteString(buf, " SYSTEM ");
673 xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
675 xmlOutputBufferWriteString(buf, ">\n");
679 * htmlAttrDumpOutput:
680 * @buf: the HTML buffer output
681 * @doc: the document
682 * @cur: the attribute pointer
683 * @encoding: the encoding string
685 * Dump an HTML attribute
687 static void
688 htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
689 const char *encoding ATTRIBUTE_UNUSED) {
690 xmlChar *value;
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
699 if (cur == NULL) {
700 return;
702 xmlOutputBufferWriteString(buf, " ");
703 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
704 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
705 xmlOutputBufferWriteString(buf, ":");
707 xmlOutputBufferWriteString(buf, (const char *)cur->name);
708 if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
709 value = xmlNodeListGetString(doc, cur->children, 0);
710 if (value) {
711 xmlOutputBufferWriteString(buf, "=");
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")) ||
716 (!xmlStrcasecmp(cur->name, BAD_CAST "src")) ||
717 ((!xmlStrcasecmp(cur->name, BAD_CAST "name")) &&
718 (!xmlStrcasecmp(cur->parent->name, BAD_CAST "a"))))) {
719 xmlChar *tmp = value;
720 /* xmlURIEscapeStr() escapes '"' so it can be safely used. */
721 xmlBufCCat(buf->buffer, "\"");
723 while (IS_BLANK_CH(*tmp)) tmp++;
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';
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);
747 if (end == NULL) { /* Everything has been written. */
748 break;
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;
761 xmlBufCCat(buf->buffer, "\"");
762 } else {
763 xmlBufWriteQuotedString(buf->buffer, value);
765 xmlFree(value);
766 } else {
767 xmlOutputBufferWriteString(buf, "=\"\"");
773 * htmlAttrListDumpOutput:
774 * @buf: the HTML buffer output
775 * @doc: the document
776 * @cur: the first attribute pointer
777 * @encoding: the encoding string
779 * Dump a list of HTML attributes
781 static void
782 htmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) {
783 if (cur == NULL) {
784 return;
786 while (cur != NULL) {
787 htmlAttrDumpOutput(buf, doc, cur, encoding);
788 cur = cur->next;
795 * htmlNodeListDumpOutput:
796 * @buf: the HTML buffer output
797 * @doc: the document
798 * @cur: the first node
799 * @encoding: the encoding string
800 * @format: should formatting spaces been added
802 * Dump an HTML node list, recursive behaviour,children are printed too.
804 static void
805 htmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
806 xmlNodePtr cur, const char *encoding, int format) {
807 if (cur == NULL) {
808 return;
810 while (cur != NULL) {
811 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
812 cur = cur->next;
817 * htmlNodeDumpFormatOutput:
818 * @buf: the HTML buffer output
819 * @doc: the document
820 * @cur: the current node
821 * @encoding: the encoding string
822 * @format: should formatting spaces been added
824 * Dump an HTML node, recursive behaviour,children are printed too.
826 void
827 htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
828 xmlNodePtr cur, const char *encoding, int format) {
829 const htmlElemDesc * info;
831 xmlInitParser();
833 if ((cur == NULL) || (buf == NULL)) {
834 return;
837 * Special cases.
839 if (cur->type == XML_DTD_NODE)
840 return;
841 if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
842 (cur->type == XML_DOCUMENT_NODE)){
843 htmlDocContentDumpOutput(buf, (xmlDocPtr) cur, encoding);
844 return;
846 if (cur->type == XML_ATTRIBUTE_NODE) {
847 htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur, encoding);
848 return;
850 if (cur->type == HTML_TEXT_NODE) {
851 if (cur->content != NULL) {
852 if (((cur->name == (const xmlChar *)xmlStringText) ||
853 (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
854 ((cur->parent == NULL) ||
855 ((xmlStrcasecmp(cur->parent->name, BAD_CAST "script")) &&
856 (xmlStrcasecmp(cur->parent->name, BAD_CAST "style"))))) {
857 xmlChar *buffer;
859 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
860 if (buffer != NULL) {
861 xmlOutputBufferWriteString(buf, (const char *)buffer);
862 xmlFree(buffer);
864 } else {
865 xmlOutputBufferWriteString(buf, (const char *)cur->content);
868 return;
870 if (cur->type == HTML_COMMENT_NODE) {
871 if (cur->content != NULL) {
872 xmlOutputBufferWriteString(buf, "<!--");
873 xmlOutputBufferWriteString(buf, (const char *)cur->content);
874 xmlOutputBufferWriteString(buf, "-->");
876 return;
878 if (cur->type == HTML_PI_NODE) {
879 if (cur->name == NULL)
880 return;
881 xmlOutputBufferWriteString(buf, "<?");
882 xmlOutputBufferWriteString(buf, (const char *)cur->name);
883 if (cur->content != NULL) {
884 xmlOutputBufferWriteString(buf, " ");
885 xmlOutputBufferWriteString(buf, (const char *)cur->content);
887 xmlOutputBufferWriteString(buf, ">");
888 return;
890 if (cur->type == HTML_ENTITY_REF_NODE) {
891 xmlOutputBufferWriteString(buf, "&");
892 xmlOutputBufferWriteString(buf, (const char *)cur->name);
893 xmlOutputBufferWriteString(buf, ";");
894 return;
896 if (cur->type == HTML_PRESERVE_NODE) {
897 if (cur->content != NULL) {
898 xmlOutputBufferWriteString(buf, (const char *)cur->content);
900 return;
904 * Get specific HTML info for that node.
906 if (cur->ns == NULL)
907 info = htmlTagLookup(cur->name);
908 else
909 info = NULL;
911 xmlOutputBufferWriteString(buf, "<");
912 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
913 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
914 xmlOutputBufferWriteString(buf, ":");
916 xmlOutputBufferWriteString(buf, (const char *)cur->name);
917 if (cur->nsDef)
918 xmlNsListDumpOutput(buf, cur->nsDef);
919 if (cur->properties != NULL)
920 htmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
922 if ((info != NULL) && (info->empty)) {
923 xmlOutputBufferWriteString(buf, ">");
924 if ((format) && (!info->isinline) && (cur->next != NULL)) {
925 if ((cur->next->type != HTML_TEXT_NODE) &&
926 (cur->next->type != HTML_ENTITY_REF_NODE) &&
927 (cur->parent != NULL) &&
928 (cur->parent->name != NULL) &&
929 (cur->parent->name[0] != 'p')) /* p, pre, param */
930 xmlOutputBufferWriteString(buf, "\n");
932 return;
934 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
935 (cur->children == NULL)) {
936 if ((info != NULL) && (info->saveEndTag != 0) &&
937 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
938 (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
939 xmlOutputBufferWriteString(buf, ">");
940 } else {
941 xmlOutputBufferWriteString(buf, "></");
942 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
943 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
944 xmlOutputBufferWriteString(buf, ":");
946 xmlOutputBufferWriteString(buf, (const char *)cur->name);
947 xmlOutputBufferWriteString(buf, ">");
949 if ((format) && (cur->next != NULL) &&
950 (info != NULL) && (!info->isinline)) {
951 if ((cur->next->type != HTML_TEXT_NODE) &&
952 (cur->next->type != HTML_ENTITY_REF_NODE) &&
953 (cur->parent != NULL) &&
954 (cur->parent->name != NULL) &&
955 (cur->parent->name[0] != 'p')) /* p, pre, param */
956 xmlOutputBufferWriteString(buf, "\n");
958 return;
960 xmlOutputBufferWriteString(buf, ">");
961 if ((cur->type != XML_ELEMENT_NODE) &&
962 (cur->content != NULL)) {
964 * Uses the OutputBuffer property to automatically convert
965 * invalids to charrefs
968 xmlOutputBufferWriteString(buf, (const char *) cur->content);
970 if (cur->children != NULL) {
971 if ((format) && (info != NULL) && (!info->isinline) &&
972 (cur->children->type != HTML_TEXT_NODE) &&
973 (cur->children->type != HTML_ENTITY_REF_NODE) &&
974 (cur->children != cur->last) &&
975 (cur->name != NULL) &&
976 (cur->name[0] != 'p')) /* p, pre, param */
977 xmlOutputBufferWriteString(buf, "\n");
978 htmlNodeListDumpOutput(buf, doc, cur->children, encoding, format);
979 if ((format) && (info != NULL) && (!info->isinline) &&
980 (cur->last->type != HTML_TEXT_NODE) &&
981 (cur->last->type != HTML_ENTITY_REF_NODE) &&
982 (cur->children != cur->last) &&
983 (cur->name != NULL) &&
984 (cur->name[0] != 'p')) /* p, pre, param */
985 xmlOutputBufferWriteString(buf, "\n");
987 xmlOutputBufferWriteString(buf, "</");
988 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
989 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
990 xmlOutputBufferWriteString(buf, ":");
992 xmlOutputBufferWriteString(buf, (const char *)cur->name);
993 xmlOutputBufferWriteString(buf, ">");
994 if ((format) && (info != NULL) && (!info->isinline) &&
995 (cur->next != NULL)) {
996 if ((cur->next->type != HTML_TEXT_NODE) &&
997 (cur->next->type != HTML_ENTITY_REF_NODE) &&
998 (cur->parent != NULL) &&
999 (cur->parent->name != NULL) &&
1000 (cur->parent->name[0] != 'p')) /* p, pre, param */
1001 xmlOutputBufferWriteString(buf, "\n");
1006 * htmlNodeDumpOutput:
1007 * @buf: the HTML buffer output
1008 * @doc: the document
1009 * @cur: the current node
1010 * @encoding: the encoding string
1012 * Dump an HTML node, recursive behaviour,children are printed too,
1013 * and formatting returns/spaces are added.
1015 void
1016 htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
1017 xmlNodePtr cur, const char *encoding) {
1018 htmlNodeDumpFormatOutput(buf, doc, cur, encoding, 1);
1022 * htmlDocContentDumpFormatOutput:
1023 * @buf: the HTML buffer output
1024 * @cur: the document
1025 * @encoding: the encoding string
1026 * @format: should formatting spaces been added
1028 * Dump an HTML document.
1030 void
1031 htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
1032 const char *encoding, int format) {
1033 int type;
1035 xmlInitParser();
1037 if ((buf == NULL) || (cur == NULL))
1038 return;
1041 * force to output the stuff as HTML, especially for entities
1043 type = cur->type;
1044 cur->type = XML_HTML_DOCUMENT_NODE;
1045 if (cur->intSubset != NULL) {
1046 htmlDtdDumpOutput(buf, cur, NULL);
1048 if (cur->children != NULL) {
1049 htmlNodeListDumpOutput(buf, cur, cur->children, encoding, format);
1051 xmlOutputBufferWriteString(buf, "\n");
1052 cur->type = (xmlElementType) type;
1056 * htmlDocContentDumpOutput:
1057 * @buf: the HTML buffer output
1058 * @cur: the document
1059 * @encoding: the encoding string
1061 * Dump an HTML document. Formating return/spaces are added.
1063 void
1064 htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
1065 const char *encoding) {
1066 htmlDocContentDumpFormatOutput(buf, cur, encoding, 1);
1069 /************************************************************************
1071 * Saving functions front-ends *
1073 ************************************************************************/
1076 * htmlDocDump:
1077 * @f: the FILE*
1078 * @cur: the document
1080 * Dump an HTML document to an open FILE.
1082 * returns: the number of byte written or -1 in case of failure.
1085 htmlDocDump(FILE *f, xmlDocPtr cur) {
1086 xmlOutputBufferPtr buf;
1087 xmlCharEncodingHandlerPtr handler = NULL;
1088 const char *encoding;
1089 int ret;
1091 xmlInitParser();
1093 if ((cur == NULL) || (f == NULL)) {
1094 return(-1);
1097 encoding = (const char *) htmlGetMetaEncoding(cur);
1099 if (encoding != NULL) {
1100 xmlCharEncoding enc;
1102 enc = xmlParseCharEncoding(encoding);
1103 if (enc != cur->charset) {
1104 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1106 * Not supported yet
1108 return(-1);
1111 handler = xmlFindCharEncodingHandler(encoding);
1112 if (handler == NULL)
1113 htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1114 } else {
1115 handler = xmlFindCharEncodingHandler(encoding);
1120 * Fallback to HTML or ASCII when the encoding is unspecified
1122 if (handler == NULL)
1123 handler = xmlFindCharEncodingHandler("HTML");
1124 if (handler == NULL)
1125 handler = xmlFindCharEncodingHandler("ascii");
1127 buf = xmlOutputBufferCreateFile(f, handler);
1128 if (buf == NULL) return(-1);
1129 htmlDocContentDumpOutput(buf, cur, NULL);
1131 ret = xmlOutputBufferClose(buf);
1132 return(ret);
1136 * htmlSaveFile:
1137 * @filename: the filename (or URL)
1138 * @cur: the document
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.
1145 htmlSaveFile(const char *filename, xmlDocPtr cur) {
1146 xmlOutputBufferPtr buf;
1147 xmlCharEncodingHandlerPtr handler = NULL;
1148 const char *encoding;
1149 int ret;
1151 if ((cur == NULL) || (filename == NULL))
1152 return(-1);
1154 xmlInitParser();
1156 encoding = (const char *) htmlGetMetaEncoding(cur);
1158 if (encoding != NULL) {
1159 xmlCharEncoding enc;
1161 enc = xmlParseCharEncoding(encoding);
1162 if (enc != cur->charset) {
1163 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1165 * Not supported yet
1167 return(-1);
1170 handler = xmlFindCharEncodingHandler(encoding);
1171 if (handler == NULL)
1172 htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1177 * Fallback to HTML or ASCII when the encoding is unspecified
1179 if (handler == NULL)
1180 handler = xmlFindCharEncodingHandler("HTML");
1181 if (handler == NULL)
1182 handler = xmlFindCharEncodingHandler("ascii");
1185 * save the content to a temp buffer.
1187 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
1188 if (buf == NULL) return(0);
1190 htmlDocContentDumpOutput(buf, cur, NULL);
1192 ret = xmlOutputBufferClose(buf);
1193 return(ret);
1197 * htmlSaveFileFormat:
1198 * @filename: the filename
1199 * @cur: the document
1200 * @format: should formatting spaces been added
1201 * @encoding: the document encoding
1203 * Dump an HTML document to a file using a given encoding.
1205 * returns: the number of byte written or -1 in case of failure.
1208 htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
1209 const char *encoding, int format) {
1210 xmlOutputBufferPtr buf;
1211 xmlCharEncodingHandlerPtr handler = NULL;
1212 int ret;
1214 if ((cur == NULL) || (filename == NULL))
1215 return(-1);
1217 xmlInitParser();
1219 if (encoding != NULL) {
1220 xmlCharEncoding enc;
1222 enc = xmlParseCharEncoding(encoding);
1223 if (enc != cur->charset) {
1224 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
1226 * Not supported yet
1228 return(-1);
1231 handler = xmlFindCharEncodingHandler(encoding);
1232 if (handler == NULL)
1233 htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
1235 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
1236 } else {
1237 htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
1241 * Fallback to HTML or ASCII when the encoding is unspecified
1243 if (handler == NULL)
1244 handler = xmlFindCharEncodingHandler("HTML");
1245 if (handler == NULL)
1246 handler = xmlFindCharEncodingHandler("ascii");
1249 * save the content to a temp buffer.
1251 buf = xmlOutputBufferCreateFilename(filename, handler, 0);
1252 if (buf == NULL) return(0);
1254 htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
1256 ret = xmlOutputBufferClose(buf);
1257 return(ret);
1261 * htmlSaveFileEnc:
1262 * @filename: the filename
1263 * @cur: the document
1264 * @encoding: the document encoding
1266 * Dump an HTML document to a file using a given encoding
1267 * and formatting returns/spaces are added.
1269 * returns: the number of byte written or -1 in case of failure.
1272 htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
1273 return(htmlSaveFileFormat(filename, cur, encoding, 1));
1276 #endif /* LIBXML_OUTPUT_ENABLED */
1278 #define bottom_HTMLtree
1279 #include "elfgcchack.h"
1280 #endif /* LIBXML_HTML_ENABLED */