HTMLtree.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * HTMLtree.c : implementation of access function for an HTML tree.
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #define IN_LIBXML
  9. #include "libxml.h"
  10. #ifdef LIBXML_HTML_ENABLED
  11. #include <string.h> /* for memset() only ! */
  12. #ifdef HAVE_CTYPE_H
  13. #include <ctype.h>
  14. #endif
  15. #ifdef HAVE_STDLIB_H
  16. #include <stdlib.h>
  17. #endif
  18. #include <libxml/xmlmemory.h>
  19. #include <libxml/HTMLparser.h>
  20. #include <libxml/HTMLtree.h>
  21. #include <libxml/entities.h>
  22. #include <libxml/valid.h>
  23. #include <libxml/xmlerror.h>
  24. #include <libxml/parserInternals.h>
  25. #include <libxml/globals.h>
  26. #include <libxml/uri.h>
  27. #include "buf.h"
  28. /************************************************************************
  29. * *
  30. * Getting/Setting encoding meta tags *
  31. * *
  32. ************************************************************************/
  33. /**
  34. * htmlGetMetaEncoding:
  35. * @doc: the document
  36. *
  37. * Encoding definition lookup in the Meta tags
  38. *
  39. * Returns the current encoding as flagged in the HTML source
  40. */
  41. const xmlChar *
  42. htmlGetMetaEncoding(htmlDocPtr doc) {
  43. htmlNodePtr cur;
  44. const xmlChar *content;
  45. const xmlChar *encoding;
  46. if (doc == NULL)
  47. return(NULL);
  48. cur = doc->children;
  49. /*
  50. * Search the html
  51. */
  52. while (cur != NULL) {
  53. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  54. if (xmlStrEqual(cur->name, BAD_CAST"html"))
  55. break;
  56. if (xmlStrEqual(cur->name, BAD_CAST"head"))
  57. goto found_head;
  58. if (xmlStrEqual(cur->name, BAD_CAST"meta"))
  59. goto found_meta;
  60. }
  61. cur = cur->next;
  62. }
  63. if (cur == NULL)
  64. return(NULL);
  65. cur = cur->children;
  66. /*
  67. * Search the head
  68. */
  69. while (cur != NULL) {
  70. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  71. if (xmlStrEqual(cur->name, BAD_CAST"head"))
  72. break;
  73. if (xmlStrEqual(cur->name, BAD_CAST"meta"))
  74. goto found_meta;
  75. }
  76. cur = cur->next;
  77. }
  78. if (cur == NULL)
  79. return(NULL);
  80. found_head:
  81. cur = cur->children;
  82. /*
  83. * Search the meta elements
  84. */
  85. found_meta:
  86. while (cur != NULL) {
  87. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  88. if (xmlStrEqual(cur->name, BAD_CAST"meta")) {
  89. xmlAttrPtr attr = cur->properties;
  90. int http;
  91. const xmlChar *value;
  92. content = NULL;
  93. http = 0;
  94. while (attr != NULL) {
  95. if ((attr->children != NULL) &&
  96. (attr->children->type == XML_TEXT_NODE) &&
  97. (attr->children->next == NULL)) {
  98. value = attr->children->content;
  99. if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
  100. && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
  101. http = 1;
  102. else if ((value != NULL)
  103. && (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
  104. content = value;
  105. if ((http != 0) && (content != NULL))
  106. goto found_content;
  107. }
  108. attr = attr->next;
  109. }
  110. }
  111. }
  112. cur = cur->next;
  113. }
  114. return(NULL);
  115. found_content:
  116. encoding = xmlStrstr(content, BAD_CAST"charset=");
  117. if (encoding == NULL)
  118. encoding = xmlStrstr(content, BAD_CAST"Charset=");
  119. if (encoding == NULL)
  120. encoding = xmlStrstr(content, BAD_CAST"CHARSET=");
  121. if (encoding != NULL) {
  122. encoding += 8;
  123. } else {
  124. encoding = xmlStrstr(content, BAD_CAST"charset =");
  125. if (encoding == NULL)
  126. encoding = xmlStrstr(content, BAD_CAST"Charset =");
  127. if (encoding == NULL)
  128. encoding = xmlStrstr(content, BAD_CAST"CHARSET =");
  129. if (encoding != NULL)
  130. encoding += 9;
  131. }
  132. if (encoding != NULL) {
  133. while ((*encoding == ' ') || (*encoding == '\t')) encoding++;
  134. }
  135. return(encoding);
  136. }
  137. /**
  138. * htmlSetMetaEncoding:
  139. * @doc: the document
  140. * @encoding: the encoding string
  141. *
  142. * Sets the current encoding in the Meta tags
  143. * NOTE: this will not change the document content encoding, just
  144. * the META flag associated.
  145. *
  146. * Returns 0 in case of success and -1 in case of error
  147. */
  148. int
  149. htmlSetMetaEncoding(htmlDocPtr doc, const xmlChar *encoding) {
  150. htmlNodePtr cur, meta = NULL, head = NULL;
  151. const xmlChar *content = NULL;
  152. char newcontent[100];
  153. newcontent[0] = 0;
  154. if (doc == NULL)
  155. return(-1);
  156. /* html isn't a real encoding it's just libxml2 way to get entities */
  157. if (!xmlStrcasecmp(encoding, BAD_CAST "html"))
  158. return(-1);
  159. if (encoding != NULL) {
  160. snprintf(newcontent, sizeof(newcontent), "text/html; charset=%s",
  161. (char *)encoding);
  162. newcontent[sizeof(newcontent) - 1] = 0;
  163. }
  164. cur = doc->children;
  165. /*
  166. * Search the html
  167. */
  168. while (cur != NULL) {
  169. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  170. if (xmlStrcasecmp(cur->name, BAD_CAST"html") == 0)
  171. break;
  172. if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
  173. goto found_head;
  174. if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0)
  175. goto found_meta;
  176. }
  177. cur = cur->next;
  178. }
  179. if (cur == NULL)
  180. return(-1);
  181. cur = cur->children;
  182. /*
  183. * Search the head
  184. */
  185. while (cur != NULL) {
  186. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  187. if (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0)
  188. break;
  189. if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
  190. head = cur->parent;
  191. goto found_meta;
  192. }
  193. }
  194. cur = cur->next;
  195. }
  196. if (cur == NULL)
  197. return(-1);
  198. found_head:
  199. head = cur;
  200. if (cur->children == NULL)
  201. goto create;
  202. cur = cur->children;
  203. found_meta:
  204. /*
  205. * Search and update all the remaining the meta elements carrying
  206. * encoding information
  207. */
  208. while (cur != NULL) {
  209. if ((cur->type == XML_ELEMENT_NODE) && (cur->name != NULL)) {
  210. if (xmlStrcasecmp(cur->name, BAD_CAST"meta") == 0) {
  211. xmlAttrPtr attr = cur->properties;
  212. int http;
  213. const xmlChar *value;
  214. content = NULL;
  215. http = 0;
  216. while (attr != NULL) {
  217. if ((attr->children != NULL) &&
  218. (attr->children->type == XML_TEXT_NODE) &&
  219. (attr->children->next == NULL)) {
  220. value = attr->children->content;
  221. if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv"))
  222. && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
  223. http = 1;
  224. else
  225. {
  226. if ((value != NULL) &&
  227. (!xmlStrcasecmp(attr->name, BAD_CAST"content")))
  228. content = value;
  229. }
  230. if ((http != 0) && (content != NULL))
  231. break;
  232. }
  233. attr = attr->next;
  234. }
  235. if ((http != 0) && (content != NULL)) {
  236. meta = cur;
  237. break;
  238. }
  239. }
  240. }
  241. cur = cur->next;
  242. }
  243. create:
  244. if (meta == NULL) {
  245. if ((encoding != NULL) && (head != NULL)) {
  246. /*
  247. * Create a new Meta element with the right attributes
  248. */
  249. meta = xmlNewDocNode(doc, NULL, BAD_CAST"meta", NULL);
  250. if (head->children == NULL)
  251. xmlAddChild(head, meta);
  252. else
  253. xmlAddPrevSibling(head->children, meta);
  254. xmlNewProp(meta, BAD_CAST"http-equiv", BAD_CAST"Content-Type");
  255. xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent);
  256. }
  257. } else {
  258. /* remove the meta tag if NULL is passed */
  259. if (encoding == NULL) {
  260. xmlUnlinkNode(meta);
  261. xmlFreeNode(meta);
  262. }
  263. /* change the document only if there is a real encoding change */
  264. else if (xmlStrcasestr(content, encoding) == NULL) {
  265. xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent);
  266. }
  267. }
  268. return(0);
  269. }
  270. /**
  271. * booleanHTMLAttrs:
  272. *
  273. * These are the HTML attributes which will be output
  274. * in minimized form, i.e. <option selected="selected"> will be
  275. * output as <option selected>, as per XSLT 1.0 16.2 "HTML Output Method"
  276. *
  277. */
  278. static const char* htmlBooleanAttrs[] = {
  279. "checked", "compact", "declare", "defer", "disabled", "ismap",
  280. "multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
  281. "selected", NULL
  282. };
  283. /**
  284. * htmlIsBooleanAttr:
  285. * @name: the name of the attribute to check
  286. *
  287. * Determine if a given attribute is a boolean attribute.
  288. *
  289. * returns: false if the attribute is not boolean, true otherwise.
  290. */
  291. int
  292. htmlIsBooleanAttr(const xmlChar *name)
  293. {
  294. int i = 0;
  295. while (htmlBooleanAttrs[i] != NULL) {
  296. if (xmlStrcasecmp((const xmlChar *)htmlBooleanAttrs[i], name) == 0)
  297. return 1;
  298. i++;
  299. }
  300. return 0;
  301. }
  302. #ifdef LIBXML_OUTPUT_ENABLED
  303. /*
  304. * private routine exported from xmlIO.c
  305. */
  306. xmlOutputBufferPtr
  307. xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder);
  308. /************************************************************************
  309. * *
  310. * Output error handlers *
  311. * *
  312. ************************************************************************/
  313. /**
  314. * htmlSaveErrMemory:
  315. * @extra: extra information
  316. *
  317. * Handle an out of memory condition
  318. */
  319. static void
  320. htmlSaveErrMemory(const char *extra)
  321. {
  322. __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
  323. }
  324. /**
  325. * htmlSaveErr:
  326. * @code: the error number
  327. * @node: the location of the error.
  328. * @extra: extra information
  329. *
  330. * Handle an out of memory condition
  331. */
  332. static void
  333. htmlSaveErr(int code, xmlNodePtr node, const char *extra)
  334. {
  335. const char *msg = NULL;
  336. switch(code) {
  337. case XML_SAVE_NOT_UTF8:
  338. msg = "string is not in UTF-8\n";
  339. break;
  340. case XML_SAVE_CHAR_INVALID:
  341. msg = "invalid character value\n";
  342. break;
  343. case XML_SAVE_UNKNOWN_ENCODING:
  344. msg = "unknown encoding %s\n";
  345. break;
  346. case XML_SAVE_NO_DOCTYPE:
  347. msg = "HTML has no DOCTYPE\n";
  348. break;
  349. default:
  350. msg = "unexpected error number\n";
  351. }
  352. __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
  353. }
  354. /************************************************************************
  355. * *
  356. * Dumping HTML tree content to a simple buffer *
  357. * *
  358. ************************************************************************/
  359. /**
  360. * htmlBufNodeDumpFormat:
  361. * @buf: the xmlBufPtr output
  362. * @doc: the document
  363. * @cur: the current node
  364. * @format: should formatting spaces been added
  365. *
  366. * Dump an HTML node, recursive behaviour,children are printed too.
  367. *
  368. * Returns the number of byte written or -1 in case of error
  369. */
  370. static size_t
  371. htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur,
  372. int format) {
  373. size_t use;
  374. int ret;
  375. xmlOutputBufferPtr outbuf;
  376. if (cur == NULL) {
  377. return (-1);
  378. }
  379. if (buf == NULL) {
  380. return (-1);
  381. }
  382. outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
  383. if (outbuf == NULL) {
  384. htmlSaveErrMemory("allocating HTML output buffer");
  385. return (-1);
  386. }
  387. memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
  388. outbuf->buffer = buf;
  389. outbuf->encoder = NULL;
  390. outbuf->writecallback = NULL;
  391. outbuf->closecallback = NULL;
  392. outbuf->context = NULL;
  393. outbuf->written = 0;
  394. use = xmlBufUse(buf);
  395. htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format);
  396. xmlFree(outbuf);
  397. ret = xmlBufUse(buf) - use;
  398. return (ret);
  399. }
  400. /**
  401. * htmlNodeDump:
  402. * @buf: the HTML buffer output
  403. * @doc: the document
  404. * @cur: the current node
  405. *
  406. * Dump an HTML node, recursive behaviour,children are printed too,
  407. * and formatting returns are added.
  408. *
  409. * Returns the number of byte written or -1 in case of error
  410. */
  411. int
  412. htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) {
  413. xmlBufPtr buffer;
  414. size_t ret;
  415. if ((buf == NULL) || (cur == NULL))
  416. return(-1);
  417. xmlInitParser();
  418. buffer = xmlBufFromBuffer(buf);
  419. if (buffer == NULL)
  420. return(-1);
  421. ret = htmlBufNodeDumpFormat(buffer, doc, cur, 1);
  422. xmlBufBackToBuffer(buffer);
  423. if (ret > INT_MAX)
  424. return(-1);
  425. return((int) ret);
  426. }
  427. /**
  428. * htmlNodeDumpFileFormat:
  429. * @out: the FILE pointer
  430. * @doc: the document
  431. * @cur: the current node
  432. * @encoding: the document encoding
  433. * @format: should formatting spaces been added
  434. *
  435. * Dump an HTML node, recursive behaviour,children are printed too.
  436. *
  437. * TODO: if encoding == NULL try to save in the doc encoding
  438. *
  439. * returns: the number of byte written or -1 in case of failure.
  440. */
  441. int
  442. htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc,
  443. xmlNodePtr cur, const char *encoding, int format) {
  444. xmlOutputBufferPtr buf;
  445. xmlCharEncodingHandlerPtr handler = NULL;
  446. int ret;
  447. xmlInitParser();
  448. if (encoding != NULL) {
  449. xmlCharEncoding enc;
  450. enc = xmlParseCharEncoding(encoding);
  451. if (enc != XML_CHAR_ENCODING_UTF8) {
  452. handler = xmlFindCharEncodingHandler(encoding);
  453. if (handler == NULL)
  454. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  455. }
  456. } else {
  457. /*
  458. * Fallback to HTML or ASCII when the encoding is unspecified
  459. */
  460. if (handler == NULL)
  461. handler = xmlFindCharEncodingHandler("HTML");
  462. if (handler == NULL)
  463. handler = xmlFindCharEncodingHandler("ascii");
  464. }
  465. /*
  466. * save the content to a temp buffer.
  467. */
  468. buf = xmlOutputBufferCreateFile(out, handler);
  469. if (buf == NULL) return(0);
  470. htmlNodeDumpFormatOutput(buf, doc, cur, NULL, format);
  471. ret = xmlOutputBufferClose(buf);
  472. return(ret);
  473. }
  474. /**
  475. * htmlNodeDumpFile:
  476. * @out: the FILE pointer
  477. * @doc: the document
  478. * @cur: the current node
  479. *
  480. * Dump an HTML node, recursive behaviour,children are printed too,
  481. * and formatting returns are added.
  482. */
  483. void
  484. htmlNodeDumpFile(FILE *out, xmlDocPtr doc, xmlNodePtr cur) {
  485. htmlNodeDumpFileFormat(out, doc, cur, NULL, 1);
  486. }
  487. /**
  488. * htmlDocDumpMemoryFormat:
  489. * @cur: the document
  490. * @mem: OUT: the memory pointer
  491. * @size: OUT: the memory length
  492. * @format: should formatting spaces been added
  493. *
  494. * Dump an HTML document in memory and return the xmlChar * and it's size.
  495. * It's up to the caller to free the memory.
  496. */
  497. void
  498. htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
  499. xmlOutputBufferPtr buf;
  500. xmlCharEncodingHandlerPtr handler = NULL;
  501. const char *encoding;
  502. xmlInitParser();
  503. if ((mem == NULL) || (size == NULL))
  504. return;
  505. if (cur == NULL) {
  506. *mem = NULL;
  507. *size = 0;
  508. return;
  509. }
  510. encoding = (const char *) htmlGetMetaEncoding(cur);
  511. if (encoding != NULL) {
  512. xmlCharEncoding enc;
  513. enc = xmlParseCharEncoding(encoding);
  514. if (enc != XML_CHAR_ENCODING_UTF8) {
  515. handler = xmlFindCharEncodingHandler(encoding);
  516. if (handler == NULL)
  517. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  518. }
  519. } else {
  520. /*
  521. * Fallback to HTML or ASCII when the encoding is unspecified
  522. */
  523. if (handler == NULL)
  524. handler = xmlFindCharEncodingHandler("HTML");
  525. if (handler == NULL)
  526. handler = xmlFindCharEncodingHandler("ascii");
  527. }
  528. buf = xmlAllocOutputBufferInternal(handler);
  529. if (buf == NULL) {
  530. *mem = NULL;
  531. *size = 0;
  532. return;
  533. }
  534. htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
  535. xmlOutputBufferFlush(buf);
  536. if (buf->conv != NULL) {
  537. *size = xmlBufUse(buf->conv);
  538. *mem = xmlStrndup(xmlBufContent(buf->conv), *size);
  539. } else {
  540. *size = xmlBufUse(buf->buffer);
  541. *mem = xmlStrndup(xmlBufContent(buf->buffer), *size);
  542. }
  543. (void)xmlOutputBufferClose(buf);
  544. }
  545. /**
  546. * htmlDocDumpMemory:
  547. * @cur: the document
  548. * @mem: OUT: the memory pointer
  549. * @size: OUT: the memory length
  550. *
  551. * Dump an HTML document in memory and return the xmlChar * and it's size.
  552. * It's up to the caller to free the memory.
  553. */
  554. void
  555. htmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
  556. htmlDocDumpMemoryFormat(cur, mem, size, 1);
  557. }
  558. /************************************************************************
  559. * *
  560. * Dumping HTML tree content to an I/O output buffer *
  561. * *
  562. ************************************************************************/
  563. void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
  564. /**
  565. * htmlDtdDumpOutput:
  566. * @buf: the HTML buffer output
  567. * @doc: the document
  568. * @encoding: the encoding string
  569. *
  570. * TODO: check whether encoding is needed
  571. *
  572. * Dump the HTML document DTD, if any.
  573. */
  574. static void
  575. htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
  576. const char *encoding ATTRIBUTE_UNUSED) {
  577. xmlDtdPtr cur = doc->intSubset;
  578. if (cur == NULL) {
  579. htmlSaveErr(XML_SAVE_NO_DOCTYPE, (xmlNodePtr) doc, NULL);
  580. return;
  581. }
  582. xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
  583. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  584. if (cur->ExternalID != NULL) {
  585. xmlOutputBufferWriteString(buf, " PUBLIC ");
  586. xmlBufWriteQuotedString(buf->buffer, cur->ExternalID);
  587. if (cur->SystemID != NULL) {
  588. xmlOutputBufferWriteString(buf, " ");
  589. xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
  590. }
  591. } else if (cur->SystemID != NULL &&
  592. xmlStrcmp(cur->SystemID, BAD_CAST "about:legacy-compat")) {
  593. xmlOutputBufferWriteString(buf, " SYSTEM ");
  594. xmlBufWriteQuotedString(buf->buffer, cur->SystemID);
  595. }
  596. xmlOutputBufferWriteString(buf, ">\n");
  597. }
  598. /**
  599. * htmlAttrDumpOutput:
  600. * @buf: the HTML buffer output
  601. * @doc: the document
  602. * @cur: the attribute pointer
  603. *
  604. * Dump an HTML attribute
  605. */
  606. static void
  607. htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) {
  608. xmlChar *value;
  609. /*
  610. * The html output method should not escape a & character
  611. * occurring in an attribute value immediately followed by
  612. * a { character (see Section B.7.1 of the HTML 4.0 Recommendation).
  613. * This is implemented in xmlEncodeEntitiesReentrant
  614. */
  615. if (cur == NULL) {
  616. return;
  617. }
  618. xmlOutputBufferWriteString(buf, " ");
  619. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  620. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  621. xmlOutputBufferWriteString(buf, ":");
  622. }
  623. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  624. if ((cur->children != NULL) && (!htmlIsBooleanAttr(cur->name))) {
  625. value = xmlNodeListGetString(doc, cur->children, 0);
  626. if (value) {
  627. xmlOutputBufferWriteString(buf, "=");
  628. if ((cur->ns == NULL) && (cur->parent != NULL) &&
  629. (cur->parent->ns == NULL) &&
  630. ((!xmlStrcasecmp(cur->name, BAD_CAST "href")) ||
  631. (!xmlStrcasecmp(cur->name, BAD_CAST "action")) ||
  632. (!xmlStrcasecmp(cur->name, BAD_CAST "src")) ||
  633. ((!xmlStrcasecmp(cur->name, BAD_CAST "name")) &&
  634. (!xmlStrcasecmp(cur->parent->name, BAD_CAST "a"))))) {
  635. xmlChar *escaped;
  636. xmlChar *tmp = value;
  637. while (IS_BLANK_CH(*tmp)) tmp++;
  638. /*
  639. * the < and > have already been escaped at the entity level
  640. * And doing so here breaks server side includes
  641. */
  642. escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+<>");
  643. if (escaped != NULL) {
  644. xmlBufWriteQuotedString(buf->buffer, escaped);
  645. xmlFree(escaped);
  646. } else {
  647. xmlBufWriteQuotedString(buf->buffer, value);
  648. }
  649. } else {
  650. xmlBufWriteQuotedString(buf->buffer, value);
  651. }
  652. xmlFree(value);
  653. } else {
  654. xmlOutputBufferWriteString(buf, "=\"\"");
  655. }
  656. }
  657. }
  658. /**
  659. * htmlNodeDumpFormatOutput:
  660. * @buf: the HTML buffer output
  661. * @doc: the document
  662. * @cur: the current node
  663. * @encoding: the encoding string (unused)
  664. * @format: should formatting spaces been added
  665. *
  666. * Dump an HTML node, recursive behaviour,children are printed too.
  667. */
  668. void
  669. htmlNodeDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
  670. xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED,
  671. int format) {
  672. xmlNodePtr root, parent;
  673. xmlAttrPtr attr;
  674. const htmlElemDesc * info;
  675. xmlInitParser();
  676. if ((cur == NULL) || (buf == NULL)) {
  677. return;
  678. }
  679. root = cur;
  680. parent = cur->parent;
  681. while (1) {
  682. switch (cur->type) {
  683. case XML_HTML_DOCUMENT_NODE:
  684. case XML_DOCUMENT_NODE:
  685. if (((xmlDocPtr) cur)->intSubset != NULL) {
  686. htmlDtdDumpOutput(buf, (xmlDocPtr) cur, NULL);
  687. }
  688. if (cur->children != NULL) {
  689. /* Always validate cur->parent when descending. */
  690. if (cur->parent == parent) {
  691. parent = cur;
  692. cur = cur->children;
  693. continue;
  694. }
  695. } else {
  696. xmlOutputBufferWriteString(buf, "\n");
  697. }
  698. break;
  699. case XML_ELEMENT_NODE:
  700. /*
  701. * Some users like lxml are known to pass nodes with a corrupted
  702. * tree structure. Fall back to a recursive call to handle this
  703. * case.
  704. */
  705. if ((cur->parent != parent) && (cur->children != NULL)) {
  706. htmlNodeDumpFormatOutput(buf, doc, cur, encoding, format);
  707. break;
  708. }
  709. /*
  710. * Get specific HTML info for that node.
  711. */
  712. if (cur->ns == NULL)
  713. info = htmlTagLookup(cur->name);
  714. else
  715. info = NULL;
  716. xmlOutputBufferWriteString(buf, "<");
  717. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  718. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  719. xmlOutputBufferWriteString(buf, ":");
  720. }
  721. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  722. if (cur->nsDef)
  723. xmlNsListDumpOutput(buf, cur->nsDef);
  724. attr = cur->properties;
  725. while (attr != NULL) {
  726. htmlAttrDumpOutput(buf, doc, attr);
  727. attr = attr->next;
  728. }
  729. if ((info != NULL) && (info->empty)) {
  730. xmlOutputBufferWriteString(buf, ">");
  731. } else if (cur->children == NULL) {
  732. if ((info != NULL) && (info->saveEndTag != 0) &&
  733. (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
  734. (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
  735. xmlOutputBufferWriteString(buf, ">");
  736. } else {
  737. xmlOutputBufferWriteString(buf, "></");
  738. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  739. xmlOutputBufferWriteString(buf,
  740. (const char *)cur->ns->prefix);
  741. xmlOutputBufferWriteString(buf, ":");
  742. }
  743. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  744. xmlOutputBufferWriteString(buf, ">");
  745. }
  746. } else {
  747. xmlOutputBufferWriteString(buf, ">");
  748. if ((format) && (info != NULL) && (!info->isinline) &&
  749. (cur->children->type != HTML_TEXT_NODE) &&
  750. (cur->children->type != HTML_ENTITY_REF_NODE) &&
  751. (cur->children != cur->last) &&
  752. (cur->name != NULL) &&
  753. (cur->name[0] != 'p')) /* p, pre, param */
  754. xmlOutputBufferWriteString(buf, "\n");
  755. parent = cur;
  756. cur = cur->children;
  757. continue;
  758. }
  759. if ((format) && (cur->next != NULL) &&
  760. (info != NULL) && (!info->isinline)) {
  761. if ((cur->next->type != HTML_TEXT_NODE) &&
  762. (cur->next->type != HTML_ENTITY_REF_NODE) &&
  763. (parent != NULL) &&
  764. (parent->name != NULL) &&
  765. (parent->name[0] != 'p')) /* p, pre, param */
  766. xmlOutputBufferWriteString(buf, "\n");
  767. }
  768. break;
  769. case XML_ATTRIBUTE_NODE:
  770. htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur);
  771. break;
  772. case HTML_TEXT_NODE:
  773. if (cur->content == NULL)
  774. break;
  775. if (((cur->name == (const xmlChar *)xmlStringText) ||
  776. (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
  777. ((parent == NULL) ||
  778. ((xmlStrcasecmp(parent->name, BAD_CAST "script")) &&
  779. (xmlStrcasecmp(parent->name, BAD_CAST "style"))))) {
  780. xmlChar *buffer;
  781. buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
  782. if (buffer != NULL) {
  783. xmlOutputBufferWriteString(buf, (const char *)buffer);
  784. xmlFree(buffer);
  785. }
  786. } else {
  787. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  788. }
  789. break;
  790. case HTML_COMMENT_NODE:
  791. if (cur->content != NULL) {
  792. xmlOutputBufferWriteString(buf, "<!--");
  793. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  794. xmlOutputBufferWriteString(buf, "-->");
  795. }
  796. break;
  797. case HTML_PI_NODE:
  798. if (cur->name != NULL) {
  799. xmlOutputBufferWriteString(buf, "<?");
  800. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  801. if (cur->content != NULL) {
  802. xmlOutputBufferWriteString(buf, " ");
  803. xmlOutputBufferWriteString(buf,
  804. (const char *)cur->content);
  805. }
  806. xmlOutputBufferWriteString(buf, ">");
  807. }
  808. break;
  809. case HTML_ENTITY_REF_NODE:
  810. xmlOutputBufferWriteString(buf, "&");
  811. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  812. xmlOutputBufferWriteString(buf, ";");
  813. break;
  814. case HTML_PRESERVE_NODE:
  815. if (cur->content != NULL) {
  816. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  817. }
  818. break;
  819. default:
  820. break;
  821. }
  822. while (1) {
  823. if (cur == root)
  824. return;
  825. if (cur->next != NULL) {
  826. cur = cur->next;
  827. break;
  828. }
  829. cur = parent;
  830. /* cur->parent was validated when descending. */
  831. parent = cur->parent;
  832. if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
  833. (cur->type == XML_DOCUMENT_NODE)) {
  834. xmlOutputBufferWriteString(buf, "\n");
  835. } else {
  836. if ((format) && (cur->ns == NULL))
  837. info = htmlTagLookup(cur->name);
  838. else
  839. info = NULL;
  840. if ((format) && (info != NULL) && (!info->isinline) &&
  841. (cur->last->type != HTML_TEXT_NODE) &&
  842. (cur->last->type != HTML_ENTITY_REF_NODE) &&
  843. (cur->children != cur->last) &&
  844. (cur->name != NULL) &&
  845. (cur->name[0] != 'p')) /* p, pre, param */
  846. xmlOutputBufferWriteString(buf, "\n");
  847. xmlOutputBufferWriteString(buf, "</");
  848. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  849. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  850. xmlOutputBufferWriteString(buf, ":");
  851. }
  852. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  853. xmlOutputBufferWriteString(buf, ">");
  854. if ((format) && (info != NULL) && (!info->isinline) &&
  855. (cur->next != NULL)) {
  856. if ((cur->next->type != HTML_TEXT_NODE) &&
  857. (cur->next->type != HTML_ENTITY_REF_NODE) &&
  858. (parent != NULL) &&
  859. (parent->name != NULL) &&
  860. (parent->name[0] != 'p')) /* p, pre, param */
  861. xmlOutputBufferWriteString(buf, "\n");
  862. }
  863. }
  864. }
  865. }
  866. }
  867. /**
  868. * htmlNodeDumpOutput:
  869. * @buf: the HTML buffer output
  870. * @doc: the document
  871. * @cur: the current node
  872. * @encoding: the encoding string (unused)
  873. *
  874. * Dump an HTML node, recursive behaviour,children are printed too,
  875. * and formatting returns/spaces are added.
  876. */
  877. void
  878. htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
  879. xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED) {
  880. htmlNodeDumpFormatOutput(buf, doc, cur, NULL, 1);
  881. }
  882. /**
  883. * htmlDocContentDumpFormatOutput:
  884. * @buf: the HTML buffer output
  885. * @cur: the document
  886. * @encoding: the encoding string (unused)
  887. * @format: should formatting spaces been added
  888. *
  889. * Dump an HTML document.
  890. */
  891. void
  892. htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
  893. const char *encoding ATTRIBUTE_UNUSED,
  894. int format) {
  895. htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, format);
  896. }
  897. /**
  898. * htmlDocContentDumpOutput:
  899. * @buf: the HTML buffer output
  900. * @cur: the document
  901. * @encoding: the encoding string (unused)
  902. *
  903. * Dump an HTML document. Formatting return/spaces are added.
  904. */
  905. void
  906. htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
  907. const char *encoding ATTRIBUTE_UNUSED) {
  908. htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, 1);
  909. }
  910. /************************************************************************
  911. * *
  912. * Saving functions front-ends *
  913. * *
  914. ************************************************************************/
  915. /**
  916. * htmlDocDump:
  917. * @f: the FILE*
  918. * @cur: the document
  919. *
  920. * Dump an HTML document to an open FILE.
  921. *
  922. * returns: the number of byte written or -1 in case of failure.
  923. */
  924. int
  925. htmlDocDump(FILE *f, xmlDocPtr cur) {
  926. xmlOutputBufferPtr buf;
  927. xmlCharEncodingHandlerPtr handler = NULL;
  928. const char *encoding;
  929. int ret;
  930. xmlInitParser();
  931. if ((cur == NULL) || (f == NULL)) {
  932. return(-1);
  933. }
  934. encoding = (const char *) htmlGetMetaEncoding(cur);
  935. if (encoding != NULL) {
  936. xmlCharEncoding enc;
  937. enc = xmlParseCharEncoding(encoding);
  938. if (enc != XML_CHAR_ENCODING_UTF8) {
  939. handler = xmlFindCharEncodingHandler(encoding);
  940. if (handler == NULL)
  941. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  942. }
  943. } else {
  944. /*
  945. * Fallback to HTML or ASCII when the encoding is unspecified
  946. */
  947. if (handler == NULL)
  948. handler = xmlFindCharEncodingHandler("HTML");
  949. if (handler == NULL)
  950. handler = xmlFindCharEncodingHandler("ascii");
  951. }
  952. buf = xmlOutputBufferCreateFile(f, handler);
  953. if (buf == NULL) return(-1);
  954. htmlDocContentDumpOutput(buf, cur, NULL);
  955. ret = xmlOutputBufferClose(buf);
  956. return(ret);
  957. }
  958. /**
  959. * htmlSaveFile:
  960. * @filename: the filename (or URL)
  961. * @cur: the document
  962. *
  963. * Dump an HTML document to a file. If @filename is "-" the stdout file is
  964. * used.
  965. * returns: the number of byte written or -1 in case of failure.
  966. */
  967. int
  968. htmlSaveFile(const char *filename, xmlDocPtr cur) {
  969. xmlOutputBufferPtr buf;
  970. xmlCharEncodingHandlerPtr handler = NULL;
  971. const char *encoding;
  972. int ret;
  973. if ((cur == NULL) || (filename == NULL))
  974. return(-1);
  975. xmlInitParser();
  976. encoding = (const char *) htmlGetMetaEncoding(cur);
  977. if (encoding != NULL) {
  978. xmlCharEncoding enc;
  979. enc = xmlParseCharEncoding(encoding);
  980. if (enc != XML_CHAR_ENCODING_UTF8) {
  981. handler = xmlFindCharEncodingHandler(encoding);
  982. if (handler == NULL)
  983. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  984. }
  985. } else {
  986. /*
  987. * Fallback to HTML or ASCII when the encoding is unspecified
  988. */
  989. if (handler == NULL)
  990. handler = xmlFindCharEncodingHandler("HTML");
  991. if (handler == NULL)
  992. handler = xmlFindCharEncodingHandler("ascii");
  993. }
  994. /*
  995. * save the content to a temp buffer.
  996. */
  997. buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
  998. if (buf == NULL) return(0);
  999. htmlDocContentDumpOutput(buf, cur, NULL);
  1000. ret = xmlOutputBufferClose(buf);
  1001. return(ret);
  1002. }
  1003. /**
  1004. * htmlSaveFileFormat:
  1005. * @filename: the filename
  1006. * @cur: the document
  1007. * @format: should formatting spaces been added
  1008. * @encoding: the document encoding
  1009. *
  1010. * Dump an HTML document to a file using a given encoding.
  1011. *
  1012. * returns: the number of byte written or -1 in case of failure.
  1013. */
  1014. int
  1015. htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
  1016. const char *encoding, int format) {
  1017. xmlOutputBufferPtr buf;
  1018. xmlCharEncodingHandlerPtr handler = NULL;
  1019. int ret;
  1020. if ((cur == NULL) || (filename == NULL))
  1021. return(-1);
  1022. xmlInitParser();
  1023. if (encoding != NULL) {
  1024. xmlCharEncoding enc;
  1025. enc = xmlParseCharEncoding(encoding);
  1026. if (enc != XML_CHAR_ENCODING_UTF8) {
  1027. handler = xmlFindCharEncodingHandler(encoding);
  1028. if (handler == NULL)
  1029. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  1030. }
  1031. htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
  1032. } else {
  1033. htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
  1034. /*
  1035. * Fallback to HTML or ASCII when the encoding is unspecified
  1036. */
  1037. if (handler == NULL)
  1038. handler = xmlFindCharEncodingHandler("HTML");
  1039. if (handler == NULL)
  1040. handler = xmlFindCharEncodingHandler("ascii");
  1041. }
  1042. /*
  1043. * save the content to a temp buffer.
  1044. */
  1045. buf = xmlOutputBufferCreateFilename(filename, handler, 0);
  1046. if (buf == NULL) return(0);
  1047. htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
  1048. ret = xmlOutputBufferClose(buf);
  1049. return(ret);
  1050. }
  1051. /**
  1052. * htmlSaveFileEnc:
  1053. * @filename: the filename
  1054. * @cur: the document
  1055. * @encoding: the document encoding
  1056. *
  1057. * Dump an HTML document to a file using a given encoding
  1058. * and formatting returns/spaces are added.
  1059. *
  1060. * returns: the number of byte written or -1 in case of failure.
  1061. */
  1062. int
  1063. htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
  1064. return(htmlSaveFileFormat(filename, cur, encoding, 1));
  1065. }
  1066. #endif /* LIBXML_OUTPUT_ENABLED */
  1067. #define bottom_HTMLtree
  1068. #include "elfgcchack.h"
  1069. #endif /* LIBXML_HTML_ENABLED */