HTMLtree.c 33 KB

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