HTMLtree.c 32 KB

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