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