📄 htmltree.c.svn-base
字号:
void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);/** * htmlDtdDumpOutput: * @buf: the HTML buffer output * @doc: the document * @encoding: the encoding string * * TODO: check whether encoding is needed * * Dump the HTML document DTD, if any. */static voidhtmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding ATTRIBUTE_UNUSED) { xmlDtdPtr cur = doc->intSubset; if (cur == NULL) { htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL); return; } xmlOutputBufferWriteString(buf, "<!DOCTYPE "); xmlOutputBufferWriteString(buf, (const char *)cur->name); if (cur->ExternalID != NULL) { xmlOutputBufferWriteString(buf, " PUBLIC "); xmlBufferWriteQuotedString(buf->buffer, cur->ExternalID); if (cur->SystemID != NULL) { xmlOutputBufferWriteString(buf, " "); xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); } } else if (cur->SystemID != NULL) { xmlOutputBufferWriteString(buf, " SYSTEM "); xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); } xmlOutputBufferWriteString(buf, ">\n");}/** * htmlAttrDumpOutput: * @buf: the HTML buffer output * @doc: the document * @cur: the attribute pointer * @encoding: the encoding string * * Dump an HTML attribute */static voidhtmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding ATTRIBUTE_UNUSED) { xmlChar *value; /* * TODO: The html output method should not escape a & character * occurring in an attribute value immediately followed by * a { character (see Section B.7.1 of the HTML 4.0 Recommendation). */ if (cur == NULL) { return; } xmlOutputBufferWriteString(buf, " "); if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); xmlOutputBufferWriteString(buf, ":"); } xmlOutputBufferWriteString(buf, (const char *)cur->name); if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) { value = xmlNodeListGetString(doc, cur->children, 0); if (value) { xmlOutputBufferWriteString(buf, "="); if ((cur->ns == NULL) && (cur->parent != NULL) && (cur->parent->ns == NULL) && ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) || (!xmlStrcasecmp(cur->name, BAD_CAST "action")) || (!xmlStrcasecmp(cur->name, BAD_CAST "src")))) { xmlChar *escaped; xmlChar *tmp = value; while (IS_BLANK_CH(*tmp)) tmp++; escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+"); if (escaped != NULL) { xmlBufferWriteQuotedString(buf->buffer, escaped); xmlFree(escaped); } else { xmlBufferWriteQuotedString(buf->buffer, value); } } else { xmlBufferWriteQuotedString(buf->buffer, value); } xmlFree(value); } else { xmlOutputBufferWriteString(buf, "=\"\""); } }}/** * htmlAttrListDumpOutput: * @buf: the HTML buffer output * @doc: the document * @cur: the first attribute pointer * @encoding: the encoding string * * Dump a list of HTML attributes */static voidhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, const char *encoding) { if (cur == NULL) { return; } while (cur != NULL) { htmlAttrDumpOutput(buf, doc, cur, encoding); cur = cur->next; }}/** * htmlNodeListDumpOutput: * @buf: the HTML buffer output * @doc: the document * @cur: the first node * @encoding: the encoding string * @format: should formatting spaces been added * * Dump an HTML node list, recursive behaviour,children are printed too. */static voidhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, const char *encoding, int format) { if (cur == NULL) { return; } while (cur != NULL) { htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format); cur = cur->next; }}/** * htmlNodeDumpFormatOutput: * @buf: the HTML buffer output * @doc: the document * @cur: the current node * @encoding: the encoding string * @format: should formatting spaces been added * * Dump an HTML node, recursive behaviour,children are printed too. */voidhtmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, const char *encoding, int format) { const htmlElemDesc * info; xmlInitParser(); if (cur == NULL) { return; } /* * Special cases. */ if (cur->type == XML_DTD_NODE) return; if (cur->type == XML_HTML_DOCUMENT_NODE) { htmlDocContentDumpOutput(buf, (xmlDocPtr) cur, encoding); return; } if (cur->type == HTML_TEXT_NODE) { if (cur->content != NULL) { if (((cur->name == (const xmlChar *)xmlStringText) || (cur->name != (const xmlChar *)xmlStringTextNoenc)) && ((cur->parent == NULL) || ((xmlStrcasecmp(cur->parent->name, BAD_CAST "script")) && (xmlStrcasecmp(cur->parent->name, BAD_CAST "style"))))) { xmlChar *buffer; buffer = xmlEncodeEntitiesReentrant(doc, cur->content); if (buffer != NULL) { xmlOutputBufferWriteString(buf, (const char *)buffer); xmlFree(buffer); } } else { xmlOutputBufferWriteString(buf, (const char *)cur->content); } } return; } if (cur->type == HTML_COMMENT_NODE) { if (cur->content != NULL) { xmlOutputBufferWriteString(buf, "<!--"); xmlOutputBufferWriteString(buf, (const char *)cur->content); xmlOutputBufferWriteString(buf, "-->"); } return; } if (cur->type == HTML_PI_NODE) { if (cur->name == NULL) return; xmlOutputBufferWriteString(buf, "<?"); xmlOutputBufferWriteString(buf, (const char *)cur->name); if (cur->content != NULL) { xmlOutputBufferWriteString(buf, " "); xmlOutputBufferWriteString(buf, (const char *)cur->content); } xmlOutputBufferWriteString(buf, ">"); return; } if (cur->type == HTML_ENTITY_REF_NODE) { xmlOutputBufferWriteString(buf, "&"); xmlOutputBufferWriteString(buf, (const char *)cur->name); xmlOutputBufferWriteString(buf, ";"); return; } if (cur->type == HTML_PRESERVE_NODE) { if (cur->content != NULL) { xmlOutputBufferWriteString(buf, (const char *)cur->content); } return; } /* * Get specific HTML info for that node. */ if (cur->ns == NULL) info = htmlTagLookup(cur->name); else info = NULL; xmlOutputBufferWriteString(buf, "<"); if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); xmlOutputBufferWriteString(buf, ":"); } xmlOutputBufferWriteString(buf, (const char *)cur->name); if (cur->nsDef) xmlNsListDumpOutput(buf, cur->nsDef); if (cur->properties != NULL) htmlAttrListDumpOutput(buf, doc, cur->properties, encoding); if ((info != NULL) && (info->empty)) { xmlOutputBufferWriteString(buf, ">"); if ((format) && (!info->isinline) && (cur->next != NULL)) { if ((cur->next->type != HTML_TEXT_NODE) && (cur->next->type != HTML_ENTITY_REF_NODE) && (cur->parent != NULL) && (cur->parent->name != NULL) && (cur->parent->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "\n"); } return; } if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) && (cur->children == NULL)) { if ((info != NULL) && (info->saveEndTag != 0) && (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) && (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) { xmlOutputBufferWriteString(buf, ">"); } else { xmlOutputBufferWriteString(buf, "></"); if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); xmlOutputBufferWriteString(buf, ":"); } xmlOutputBufferWriteString(buf, (const char *)cur->name); xmlOutputBufferWriteString(buf, ">"); } if ((format) && (cur->next != NULL) && (info != NULL) && (!info->isinline)) { if ((cur->next->type != HTML_TEXT_NODE) && (cur->next->type != HTML_ENTITY_REF_NODE) && (cur->parent != NULL) && (cur->parent->name != NULL) && (cur->parent->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "\n"); } return; } xmlOutputBufferWriteString(buf, ">"); if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) { /* * Uses the OutputBuffer property to automatically convert * invalids to charrefs */ xmlOutputBufferWriteString(buf, (const char *) cur->content); } if (cur->children != NULL) { if ((format) && (info != NULL) && (!info->isinline) && (cur->children->type != HTML_TEXT_NODE) && (cur->children->type != HTML_ENTITY_REF_NODE) && (cur->children != cur->last) && (cur->name != NULL) && (cur->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "\n"); htmlNodeListDumpOutput(buf, doc, cur->children, encoding, format); if ((format) && (info != NULL) && (!info->isinline) && (cur->last->type != HTML_TEXT_NODE) && (cur->last->type != HTML_ENTITY_REF_NODE) && (cur->children != cur->last) && (cur->name != NULL) && (cur->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "\n"); } xmlOutputBufferWriteString(buf, "</"); if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); xmlOutputBufferWriteString(buf, ":"); } xmlOutputBufferWriteString(buf, (const char *)cur->name); xmlOutputBufferWriteString(buf, ">"); if ((format) && (info != NULL) && (!info->isinline) && (cur->next != NULL)) { if ((cur->next->type != HTML_TEXT_NODE) && (cur->next->type != HTML_ENTITY_REF_NODE) && (cur->parent != NULL) && (cur->parent->name != NULL) && (cur->parent->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "\n"); }}/** * htmlNodeDumpOutput: * @buf: the HTML buffer output * @doc: the document * @cur: the current node * @encoding: the encoding string * * Dump an HTML node, recursive behaviour,children are printed too, * and formatting returns/spaces are added. */voidhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, const char *encoding) { htmlNodeDumpFormatOutput(buf, doc, cur, encoding, 1);}/** * htmlDocContentDumpFormatOutput: * @buf: the HTML buffer output * @cur: the document * @encoding: the encoding string * @format: should formatting spaces been added * * Dump an HTML document. */voidhtmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) { int type; xmlInitParser(); /* * force to output the stuff as HTML, especially for entities */ type = cur->type; cur->type = XML_HTML_DOCUMENT_NODE; if (cur->intSubset != NULL) { htmlDtdDumpOutput(buf, cur, NULL); } if (cur->children != NULL) { htmlNodeListDumpOutput(buf, cur, cur->children, encoding, format); } xmlOutputBufferWriteString(buf, "\n"); cur->type = (xmlElementType) type;}/** * htmlDocContentDumpOutput: * @buf: the HTML buffer output * @cur: the document * @encoding: the encoding string * * Dump an HTML document. Formating return/spaces are added. */voidhtmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) { htmlDocContentDumpFormatOutput(buf, cur, encoding, 1);}/************************************************************************ * * * Saving functions front-ends * * * ************************************************************************//** * htmlDocDump: * @f: the FILE* * @cur: the document * * Dump an HTML document to an open FILE. * * returns: the number of byte written or -1 in case of failure. */inthtmlDocDump(FILE *f, xmlDocPtr cur) { xmlOutputBufferPtr buf; xmlCharEncodingHandlerPtr handler = NULL; const char *encoding; int ret; xmlInitParser(); if (cur == NULL) { return(-1); } encoding = (const char *) htmlGetMetaEncoding(cur); if (encoding != NULL) { xmlCharEncoding enc; enc = xmlParseCharEncoding(encoding); if (enc != cur->charset) { if (cur->charset != XML_CHAR_ENCODING_UTF8) { /* * Not supported yet */ return(-1); } handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) return(-1); } } /* * Fallback to HTML or ASCII when the encoding is unspecified */ if (handler == NULL) handler = xmlFindCharEncodingHandler("HTML"); if (handler == NULL) handler = xmlFindCharEncodingHandler("ascii"); buf = xmlOutputBufferCreateFile(f, handler); if (buf == NULL) return(-1); htmlDocContentDumpOutput(buf, cur, NULL); ret = xmlOutputBufferClose(buf); return(ret);}/** * htmlSaveFile: * @filename: the filename (or URL) * @cur: the document * * Dump an HTML document to a file. If @filename is "-" the stdout file is * used. * returns: the number of byte written or -1 in case of failure. */inthtmlSaveFile(const char *filename, xmlDocPtr cur) { xmlOutputBufferPtr buf; xmlCharEncodingHandlerPtr handler = NULL; const char *encoding; int ret; xmlInitParser(); encoding = (const char *) htmlGetMetaEncoding(cur); if (encoding != NULL) { xmlCharEncoding enc; enc = xmlParseCharEncoding(encoding); if (enc != cur->charset) { if (cur->charset != XML_CHAR_ENCODING_UTF8) { /* * Not supported yet */ return(-1); } handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) return(-1); } } /* * Fallback to HTML or ASCII when the encoding is unspecified */ if (handler == NULL) handler = xmlFindCharEncodingHandler("HTML"); if (handler == NULL) handler = xmlFindCharEncodingHandler("ascii"); /* * save the content to a temp buffer. */ buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); if (buf == NULL) return(0); htmlDocContentDumpOutput(buf, cur, NULL); ret = xmlOutputBufferClose(buf); return(ret);}/** * htmlSaveFileFormat: * @filename: the filename * @cur: the document * @format: should formatting spaces been added * @encoding: the document encoding * * Dump an HTML document to a file using a given encoding. * * returns: the number of byte written or -1 in case of failure. */inthtmlSaveFileFormat(const char *filename, xmlDocPtr cur, const char *encoding, int format) { xmlOutputBufferPtr buf; xmlCharEncodingHandlerPtr handler = NULL; int ret; xmlInitParser(); if (encoding != NULL) { xmlCharEncoding enc; enc = xmlParseCharEncoding(encoding); if (enc != cur->charset) { if (cur->charset != XML_CHAR_ENCODING_UTF8) { /* * Not supported yet */ return(-1); } handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) return(-1); htmlSetMetaEncoding(cur, (const xmlChar *) encoding); } } else { htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8"); } /* * Fallback to HTML or ASCII when the encoding is unspecified */ if (handler == NULL) handler = xmlFindCharEncodingHandler("HTML"); if (handler == NULL) handler = xmlFindCharEncodingHandler("ascii"); /* * save the content to a temp buffer. */ buf = xmlOutputBufferCreateFilename(filename, handler, 0); if (buf == NULL) return(0); htmlDocContentDumpFormatOutput(buf, cur, encoding, format); ret = xmlOutputBufferClose(buf); return(ret);}/** * htmlSaveFileEnc: * @filename: the filename * @cur: the document * @encoding: the document encoding * * Dump an HTML document to a file using a given encoding * and formatting returns/spaces are added. * * returns: the number of byte written or -1 in case of failure. */inthtmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { return(htmlSaveFileFormat(filename, cur, encoding, 1));}#endif /* LIBXML_OUTPUT_ENABLED */#endif /* LIBXML_HTML_ENABLED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -