html5-printer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // g++ -Wall -O2 contrib/html5-printer.cpp -o html5-printer -ltinyxml2
  2. // This program demonstrates how to use "tinyxml2" to generate conformant HTML5
  3. // by deriving from the "tinyxml2::XMLPrinter" class.
  4. // http://dev.w3.org/html5/markup/syntax.html
  5. // In HTML5, there are 16 so-called "void" elements. "void elements" NEVER have
  6. // inner content (but they MAY have attributes), and are assumed to be self-closing.
  7. // An example of a self-closig HTML5 element is "<br/>" (line break)
  8. // All other elements are called "non-void" and MUST never self-close.
  9. // Examples: "<div class='lolcats'></div>".
  10. // tinyxml2::XMLPrinter will emit _ALL_ XML elements with no inner content as
  11. // self-closing. This behavior produces space-effeceint XML, but incorrect HTML5.
  12. // Author: Dennis Jenkins, dennis (dot) jenkins (dot) 75 (at) gmail (dot) com.
  13. // License: Same as tinyxml2 (zlib, see below)
  14. // This example is a small contribution to the world! Enjoy it!
  15. /*
  16. This software is provided 'as-is', without any express or implied
  17. warranty. In no event will the authors be held liable for any
  18. damages arising from the use of this software.
  19. Permission is granted to anyone to use this software for any
  20. purpose, including commercial applications, and to alter it and
  21. redistribute it freely, subject to the following restrictions:
  22. 1. The origin of this software must not be misrepresented; you must
  23. not claim that you wrote the original software. If you use this
  24. software in a product, an acknowledgment in the product documentation
  25. would be appreciated but is not required.
  26. 2. Altered source versions must be plainly marked as such, and
  27. must not be misrepresented as being the original software.
  28. 3. This notice may not be removed or altered from any source
  29. distribution.
  30. */
  31. #include "../tinyxml2.h"
  32. #include <iostream>
  33. #if defined (_MSC_VER)
  34. #define strcasecmp stricmp
  35. #endif
  36. using namespace tinyxml2;
  37. // Contrived input containing a mix of void and non-void HTML5 elements.
  38. // When printed via XMLPrinter, some non-void elements will self-close (not valid HTML5).
  39. static const char input[] =
  40. "<html><body><p style='a'></p><br/>&copy;<col a='1' b='2'/><div a='1'></div></body></html>";
  41. // XMLPrinterHTML5 is small enough, just put the entire implementation inline.
  42. class XMLPrinterHTML5 : public XMLPrinter
  43. {
  44. public:
  45. XMLPrinterHTML5 (FILE* file=0, bool compact = false, int depth = 0) :
  46. XMLPrinter (file, compact, depth)
  47. {}
  48. protected:
  49. virtual void CloseElement () {
  50. if (_elementJustOpened && !isVoidElement (_stack.PeekTop())) {
  51. SealElementIfJustOpened();
  52. }
  53. XMLPrinter::CloseElement();
  54. }
  55. virtual bool isVoidElement (const char *name) {
  56. // Complete list of all HTML5 "void elements",
  57. // http://dev.w3.org/html5/markup/syntax.html
  58. static const char *list[] = {
  59. "area", "base", "br", "col", "command", "embed", "hr", "img",
  60. "input", "keygen", "link", "meta", "param", "source", "track", "wbr",
  61. NULL
  62. };
  63. // I could use 'bsearch', but I don't have MSVC to test on (it would work with gcc/libc).
  64. for (const char **p = list; *p; ++p) {
  65. if (!strcasecmp (name, *p)) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. };
  72. int main (void) {
  73. XMLDocument doc (false);
  74. doc.Parse (input);
  75. std::cout << "INPUT:\n" << input << "\n\n";
  76. XMLPrinter prn (NULL, true);
  77. doc.Print (&prn);
  78. std::cout << "XMLPrinter (not valid HTML5):\n" << prn.CStr() << "\n\n";
  79. XMLPrinterHTML5 html5 (NULL, true);
  80. doc.Print (&html5);
  81. std::cout << "XMLPrinterHTML5:\n" << html5.CStr() << "\n";
  82. return 0;
  83. }