HTMLtree.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  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;
  673. xmlAttrPtr attr;
  674. const htmlElemDesc * info;
  675. xmlInitParser();
  676. if ((cur == NULL) || (buf == NULL)) {
  677. return;
  678. }
  679. root = cur;
  680. while (1) {
  681. switch (cur->type) {
  682. case XML_HTML_DOCUMENT_NODE:
  683. case XML_DOCUMENT_NODE:
  684. if (((xmlDocPtr) cur)->intSubset != NULL) {
  685. htmlDtdDumpOutput(buf, (xmlDocPtr) cur, NULL);
  686. }
  687. if (cur->children != NULL) {
  688. cur = cur->children;
  689. continue;
  690. }
  691. break;
  692. case XML_ELEMENT_NODE:
  693. /*
  694. * Get specific HTML info for that node.
  695. */
  696. if (cur->ns == NULL)
  697. info = htmlTagLookup(cur->name);
  698. else
  699. info = NULL;
  700. xmlOutputBufferWriteString(buf, "<");
  701. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  702. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  703. xmlOutputBufferWriteString(buf, ":");
  704. }
  705. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  706. if (cur->nsDef)
  707. xmlNsListDumpOutput(buf, cur->nsDef);
  708. attr = cur->properties;
  709. while (attr != NULL) {
  710. htmlAttrDumpOutput(buf, doc, attr);
  711. attr = attr->next;
  712. }
  713. if ((info != NULL) && (info->empty)) {
  714. xmlOutputBufferWriteString(buf, ">");
  715. } else if (cur->children == NULL) {
  716. if ((info != NULL) && (info->saveEndTag != 0) &&
  717. (xmlStrcmp(BAD_CAST info->name, BAD_CAST "html")) &&
  718. (xmlStrcmp(BAD_CAST info->name, BAD_CAST "body"))) {
  719. xmlOutputBufferWriteString(buf, ">");
  720. } else {
  721. xmlOutputBufferWriteString(buf, "></");
  722. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  723. xmlOutputBufferWriteString(buf,
  724. (const char *)cur->ns->prefix);
  725. xmlOutputBufferWriteString(buf, ":");
  726. }
  727. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  728. xmlOutputBufferWriteString(buf, ">");
  729. }
  730. } else {
  731. xmlOutputBufferWriteString(buf, ">");
  732. if ((format) && (info != NULL) && (!info->isinline) &&
  733. (cur->children->type != HTML_TEXT_NODE) &&
  734. (cur->children->type != HTML_ENTITY_REF_NODE) &&
  735. (cur->children != cur->last) &&
  736. (cur->name != NULL) &&
  737. (cur->name[0] != 'p')) /* p, pre, param */
  738. xmlOutputBufferWriteString(buf, "\n");
  739. cur = cur->children;
  740. continue;
  741. }
  742. if ((format) && (cur->next != NULL) &&
  743. (info != NULL) && (!info->isinline)) {
  744. if ((cur->next->type != HTML_TEXT_NODE) &&
  745. (cur->next->type != HTML_ENTITY_REF_NODE) &&
  746. (cur->parent != NULL) &&
  747. (cur->parent->name != NULL) &&
  748. (cur->parent->name[0] != 'p')) /* p, pre, param */
  749. xmlOutputBufferWriteString(buf, "\n");
  750. }
  751. break;
  752. case XML_ATTRIBUTE_NODE:
  753. htmlAttrDumpOutput(buf, doc, (xmlAttrPtr) cur);
  754. break;
  755. case HTML_TEXT_NODE:
  756. if (cur->content == NULL)
  757. break;
  758. if (((cur->name == (const xmlChar *)xmlStringText) ||
  759. (cur->name != (const xmlChar *)xmlStringTextNoenc)) &&
  760. ((cur->parent == NULL) ||
  761. ((xmlStrcasecmp(cur->parent->name, BAD_CAST "script")) &&
  762. (xmlStrcasecmp(cur->parent->name, BAD_CAST "style"))))) {
  763. xmlChar *buffer;
  764. buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
  765. if (buffer != NULL) {
  766. xmlOutputBufferWriteString(buf, (const char *)buffer);
  767. xmlFree(buffer);
  768. }
  769. } else {
  770. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  771. }
  772. break;
  773. case HTML_COMMENT_NODE:
  774. if (cur->content != NULL) {
  775. xmlOutputBufferWriteString(buf, "<!--");
  776. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  777. xmlOutputBufferWriteString(buf, "-->");
  778. }
  779. break;
  780. case HTML_PI_NODE:
  781. if (cur->name != NULL) {
  782. xmlOutputBufferWriteString(buf, "<?");
  783. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  784. if (cur->content != NULL) {
  785. xmlOutputBufferWriteString(buf, " ");
  786. xmlOutputBufferWriteString(buf,
  787. (const char *)cur->content);
  788. }
  789. xmlOutputBufferWriteString(buf, ">");
  790. }
  791. break;
  792. case HTML_ENTITY_REF_NODE:
  793. xmlOutputBufferWriteString(buf, "&");
  794. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  795. xmlOutputBufferWriteString(buf, ";");
  796. break;
  797. case HTML_PRESERVE_NODE:
  798. if (cur->content != NULL) {
  799. xmlOutputBufferWriteString(buf, (const char *)cur->content);
  800. }
  801. break;
  802. default:
  803. break;
  804. }
  805. while (1) {
  806. if (cur == root)
  807. return;
  808. if (cur->next != NULL) {
  809. cur = cur->next;
  810. break;
  811. }
  812. /*
  813. * The parent should never be NULL here but we want to handle
  814. * corrupted documents gracefully.
  815. */
  816. if (cur->parent == NULL)
  817. return;
  818. cur = cur->parent;
  819. if ((cur->type == XML_HTML_DOCUMENT_NODE) ||
  820. (cur->type == XML_DOCUMENT_NODE)) {
  821. xmlOutputBufferWriteString(buf, "\n");
  822. } else {
  823. if ((format) && (cur->ns == NULL))
  824. info = htmlTagLookup(cur->name);
  825. else
  826. info = NULL;
  827. if ((format) && (info != NULL) && (!info->isinline) &&
  828. (cur->last->type != HTML_TEXT_NODE) &&
  829. (cur->last->type != HTML_ENTITY_REF_NODE) &&
  830. (cur->children != cur->last) &&
  831. (cur->name != NULL) &&
  832. (cur->name[0] != 'p')) /* p, pre, param */
  833. xmlOutputBufferWriteString(buf, "\n");
  834. xmlOutputBufferWriteString(buf, "</");
  835. if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
  836. xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
  837. xmlOutputBufferWriteString(buf, ":");
  838. }
  839. xmlOutputBufferWriteString(buf, (const char *)cur->name);
  840. xmlOutputBufferWriteString(buf, ">");
  841. if ((format) && (info != NULL) && (!info->isinline) &&
  842. (cur->next != NULL)) {
  843. if ((cur->next->type != HTML_TEXT_NODE) &&
  844. (cur->next->type != HTML_ENTITY_REF_NODE) &&
  845. (cur->parent != NULL) &&
  846. (cur->parent->name != NULL) &&
  847. (cur->parent->name[0] != 'p')) /* p, pre, param */
  848. xmlOutputBufferWriteString(buf, "\n");
  849. }
  850. }
  851. }
  852. }
  853. }
  854. /**
  855. * htmlNodeDumpOutput:
  856. * @buf: the HTML buffer output
  857. * @doc: the document
  858. * @cur: the current node
  859. * @encoding: the encoding string (unused)
  860. *
  861. * Dump an HTML node, recursive behaviour,children are printed too,
  862. * and formatting returns/spaces are added.
  863. */
  864. void
  865. htmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
  866. xmlNodePtr cur, const char *encoding ATTRIBUTE_UNUSED) {
  867. htmlNodeDumpFormatOutput(buf, doc, cur, NULL, 1);
  868. }
  869. /**
  870. * htmlDocContentDumpFormatOutput:
  871. * @buf: the HTML buffer output
  872. * @cur: the document
  873. * @encoding: the encoding string (unused)
  874. * @format: should formatting spaces been added
  875. *
  876. * Dump an HTML document.
  877. */
  878. void
  879. htmlDocContentDumpFormatOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
  880. const char *encoding ATTRIBUTE_UNUSED,
  881. int format) {
  882. htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, format);
  883. }
  884. /**
  885. * htmlDocContentDumpOutput:
  886. * @buf: the HTML buffer output
  887. * @cur: the document
  888. * @encoding: the encoding string (unused)
  889. *
  890. * Dump an HTML document. Formatting return/spaces are added.
  891. */
  892. void
  893. htmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
  894. const char *encoding ATTRIBUTE_UNUSED) {
  895. htmlNodeDumpFormatOutput(buf, cur, (xmlNodePtr) cur, NULL, 1);
  896. }
  897. /************************************************************************
  898. * *
  899. * Saving functions front-ends *
  900. * *
  901. ************************************************************************/
  902. /**
  903. * htmlDocDump:
  904. * @f: the FILE*
  905. * @cur: the document
  906. *
  907. * Dump an HTML document to an open FILE.
  908. *
  909. * returns: the number of byte written or -1 in case of failure.
  910. */
  911. int
  912. htmlDocDump(FILE *f, xmlDocPtr cur) {
  913. xmlOutputBufferPtr buf;
  914. xmlCharEncodingHandlerPtr handler = NULL;
  915. const char *encoding;
  916. int ret;
  917. xmlInitParser();
  918. if ((cur == NULL) || (f == NULL)) {
  919. return(-1);
  920. }
  921. encoding = (const char *) htmlGetMetaEncoding(cur);
  922. if (encoding != NULL) {
  923. xmlCharEncoding enc;
  924. enc = xmlParseCharEncoding(encoding);
  925. if (enc != XML_CHAR_ENCODING_UTF8) {
  926. handler = xmlFindCharEncodingHandler(encoding);
  927. if (handler == NULL)
  928. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  929. }
  930. } else {
  931. /*
  932. * Fallback to HTML or ASCII when the encoding is unspecified
  933. */
  934. if (handler == NULL)
  935. handler = xmlFindCharEncodingHandler("HTML");
  936. if (handler == NULL)
  937. handler = xmlFindCharEncodingHandler("ascii");
  938. }
  939. buf = xmlOutputBufferCreateFile(f, handler);
  940. if (buf == NULL) return(-1);
  941. htmlDocContentDumpOutput(buf, cur, NULL);
  942. ret = xmlOutputBufferClose(buf);
  943. return(ret);
  944. }
  945. /**
  946. * htmlSaveFile:
  947. * @filename: the filename (or URL)
  948. * @cur: the document
  949. *
  950. * Dump an HTML document to a file. If @filename is "-" the stdout file is
  951. * used.
  952. * returns: the number of byte written or -1 in case of failure.
  953. */
  954. int
  955. htmlSaveFile(const char *filename, xmlDocPtr cur) {
  956. xmlOutputBufferPtr buf;
  957. xmlCharEncodingHandlerPtr handler = NULL;
  958. const char *encoding;
  959. int ret;
  960. if ((cur == NULL) || (filename == NULL))
  961. return(-1);
  962. xmlInitParser();
  963. encoding = (const char *) htmlGetMetaEncoding(cur);
  964. if (encoding != NULL) {
  965. xmlCharEncoding enc;
  966. enc = xmlParseCharEncoding(encoding);
  967. if (enc != XML_CHAR_ENCODING_UTF8) {
  968. handler = xmlFindCharEncodingHandler(encoding);
  969. if (handler == NULL)
  970. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  971. }
  972. } else {
  973. /*
  974. * Fallback to HTML or ASCII when the encoding is unspecified
  975. */
  976. if (handler == NULL)
  977. handler = xmlFindCharEncodingHandler("HTML");
  978. if (handler == NULL)
  979. handler = xmlFindCharEncodingHandler("ascii");
  980. }
  981. /*
  982. * save the content to a temp buffer.
  983. */
  984. buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
  985. if (buf == NULL) return(0);
  986. htmlDocContentDumpOutput(buf, cur, NULL);
  987. ret = xmlOutputBufferClose(buf);
  988. return(ret);
  989. }
  990. /**
  991. * htmlSaveFileFormat:
  992. * @filename: the filename
  993. * @cur: the document
  994. * @format: should formatting spaces been added
  995. * @encoding: the document encoding
  996. *
  997. * Dump an HTML document to a file using a given encoding.
  998. *
  999. * returns: the number of byte written or -1 in case of failure.
  1000. */
  1001. int
  1002. htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
  1003. const char *encoding, int format) {
  1004. xmlOutputBufferPtr buf;
  1005. xmlCharEncodingHandlerPtr handler = NULL;
  1006. int ret;
  1007. if ((cur == NULL) || (filename == NULL))
  1008. return(-1);
  1009. xmlInitParser();
  1010. if (encoding != NULL) {
  1011. xmlCharEncoding enc;
  1012. enc = xmlParseCharEncoding(encoding);
  1013. if (enc != XML_CHAR_ENCODING_UTF8) {
  1014. handler = xmlFindCharEncodingHandler(encoding);
  1015. if (handler == NULL)
  1016. htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
  1017. }
  1018. htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
  1019. } else {
  1020. htmlSetMetaEncoding(cur, (const xmlChar *) "UTF-8");
  1021. /*
  1022. * Fallback to HTML or ASCII when the encoding is unspecified
  1023. */
  1024. if (handler == NULL)
  1025. handler = xmlFindCharEncodingHandler("HTML");
  1026. if (handler == NULL)
  1027. handler = xmlFindCharEncodingHandler("ascii");
  1028. }
  1029. /*
  1030. * save the content to a temp buffer.
  1031. */
  1032. buf = xmlOutputBufferCreateFilename(filename, handler, 0);
  1033. if (buf == NULL) return(0);
  1034. htmlDocContentDumpFormatOutput(buf, cur, encoding, format);
  1035. ret = xmlOutputBufferClose(buf);
  1036. return(ret);
  1037. }
  1038. /**
  1039. * htmlSaveFileEnc:
  1040. * @filename: the filename
  1041. * @cur: the document
  1042. * @encoding: the document encoding
  1043. *
  1044. * Dump an HTML document to a file using a given encoding
  1045. * and formatting returns/spaces are added.
  1046. *
  1047. * returns: the number of byte written or -1 in case of failure.
  1048. */
  1049. int
  1050. htmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
  1051. return(htmlSaveFileFormat(filename, cur, encoding, 1));
  1052. }
  1053. #endif /* LIBXML_OUTPUT_ENABLED */
  1054. #define bottom_HTMLtree
  1055. #include "elfgcchack.h"
  1056. #endif /* LIBXML_HTML_ENABLED */