Update lua versions
[ryzomcore.git] / nel / tools / htmlcss_test / htmlcss_test.cpp
blob563f5add9f14369aad2b7b8aee5198cd8aaa7bca
1 /*
2 * File: main.cpp
3 * Author: Karu <nimetu@gmail.com>
5 * Created on 2015-04-11
6 */
8 typedef struct _xmlNode xmlNode;
10 #include <string>
11 #include <fstream>
13 #include "nel/misc/types_nl.h"
14 #include "nel/misc/file.h"
15 #include "nel/misc/path.h"
17 #include "nel/gui/html_parser.h"
18 #include "nel/gui/css_parser.h"
19 #include "nel/gui/html_element.h"
20 #include "nel/gui/css_style.h"
22 #include "nel/gui/libwww.h"
24 using namespace std;
25 using namespace NLMISC;
26 using namespace NLGUI;
28 sint indent { 0 };
30 // ***************************************************************************
31 void checkRuleset(CHtmlElement &elm, CCssStyle &style, TStyleVec testset, bool exists)
33 bool verbose = false;
34 std::string existsMessage = exists ? "exist" : "unset";
35 for (auto it : testset)
37 bool failed = (exists != style.hasStyle(it.first));
38 if (failed)
40 bool failed2 = true;
41 if (it.first == "font-size")
43 printf("[%s]: font-size: %d; expected '%s'\n", existsMessage.c_str(), style.Current.FontSize, it.second.c_str());
44 printf(" (%s)\n", elm.toString().c_str());
45 failed2 = false;
47 else if (it.first == "background-color")
49 printf("[%s]: background-color: '%s'; expected '%s'\n", existsMessage.c_str(), style.Current.Background.color.toString().c_str(), it.second.c_str());
50 printf(" (%s)\n", elm.toString().c_str());
51 failed2 = false;
54 if (failed2)
56 printf("[%s] FAIL: '%s': '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str());
57 printf(" (%s)\n", elm.toString().c_str());
58 for (auto it2 : style.Current.StyleRules)
59 printf("'%s': '%s'\n", it2.first.c_str(), it2.second.c_str());
62 else if (exists && !style.checkStyle(it.first, it.second))
64 printf("[%s] FAIL: expecting '%s': '%s', got '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str(), style.getStyle(it.first).c_str());
65 printf(" (%s)\n", elm.toString().c_str());
67 else if (!failed)
69 if (verbose)
70 printf("[%s] PASS: '%s': '%s'\n", existsMessage.c_str(), it.first.c_str(), it.second.c_str());
75 // ***************************************************************************
76 void recursiveHtmlRender(CHtmlElement &elm, CCssStyle &style)
78 bool verbose = false;
79 if (elm.Type == CHtmlElement::TEXT_NODE)
81 std::string val = trim(elm.Value);
82 if (verbose)
83 if (!val.empty())
84 printf("[%d] '%s'\n", indent, val.c_str());
86 else if (elm.Type == CHtmlElement::ELEMENT_NODE)
88 style.pushStyle();
90 if (verbose)
91 printf("========= '%s'\n", elm.toString().c_str());
93 style.getStyleFor(elm);
94 style.applyStyle(elm.Style);
95 if (elm.hasAttribute("data-ruleset"))
97 TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleset"));
98 checkRuleset(elm, style, testset, true);
101 if (elm.hasAttribute("data-ruleunset"))
103 TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleunset"));
104 checkRuleset(elm, style, testset, false);
107 if (elm.hasAttribute("data-ruleset-before"))
109 TStyleVec testset = CCssParser::parseDecls(elm.getAttribute("data-ruleset-before"));
113 for (auto it = elm.Children.begin(); it != elm.Children.end(); ++it)
115 recursiveHtmlRender(*it, style);
118 style.popStyle();
122 // ***************************************************************************
123 void runTestOnFile(const std::string &filename)
125 CHtmlElement dom;
127 CHtmlParser htmlParser;
129 std::vector<CHtmlParser::StyleLink> links;
130 std::vector<std::string> styles;
131 //, elm, styles, links
133 ifstream f(filename);
134 if (!f.is_open())
136 printf("!! failed to open file '%s'\n", filename.c_str());
137 return;
140 printf(": %s\n", filename.c_str());
141 std::string htmlString;
142 std::string line;
143 while (getline(f, line))
144 htmlString += line;
146 htmlParser.getDOM(htmlString, dom, styles, links);
148 CCssStyle style;
149 for (std::string s : styles)
151 if (!s.empty())
152 style.parseStylesheet(s);
155 for (auto it = dom.Children.begin(); it != dom.Children.end(); ++it)
156 recursiveHtmlRender(*it, style);
159 // ***************************************************************************
160 int main(int argc, const char *argv[])
162 CApplicationContext *appContext = new CApplicationContext;
164 // htmlcss_test file.html
165 if (argc == 2)
167 runTestOnFile(argv[1]);
169 else
171 std::vector<std::string> result;
172 CPath::getPathContent("tests/", true /*recursive*/, false /*wantDir*/, true /*wantFile*/, result, NULL /*callback*/, true /*showEverything*/);
173 printf(":: got %ld files\n", result.size());
174 for (const auto &fname : result)
176 if (endsWith(fname, ".html") && fname != "tests/XX-template.html")
177 runTestOnFile(fname);
181 printf(">>> all done\n");
182 return EXIT_SUCCESS;