3 * Author: Karu <nimetu@gmail.com>
5 * Created on 2015-04-11
8 typedef struct _xmlNode xmlNode
;
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"
25 using namespace NLMISC
;
26 using namespace NLGUI
;
30 // ***************************************************************************
31 void checkRuleset(CHtmlElement
&elm
, CCssStyle
&style
, TStyleVec testset
, bool exists
)
34 std::string existsMessage
= exists
? "exist" : "unset";
35 for (auto it
: testset
)
37 bool failed
= (exists
!= style
.hasStyle(it
.first
));
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());
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());
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());
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
)
79 if (elm
.Type
== CHtmlElement::TEXT_NODE
)
81 std::string val
= trim(elm
.Value
);
84 printf("[%d] '%s'\n", indent
, val
.c_str());
86 else if (elm
.Type
== CHtmlElement::ELEMENT_NODE
)
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
);
122 // ***************************************************************************
123 void runTestOnFile(const std::string
&filename
)
127 CHtmlParser htmlParser
;
129 std::vector
<CHtmlParser::StyleLink
> links
;
130 std::vector
<std::string
> styles
;
131 //, elm, styles, links
133 ifstream
f(filename
);
136 printf("!! failed to open file '%s'\n", filename
.c_str());
140 printf(": %s\n", filename
.c_str());
141 std::string htmlString
;
143 while (getline(f
, line
))
146 htmlParser
.getDOM(htmlString
, dom
, styles
, links
);
149 for (std::string s
: styles
)
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
167 runTestOnFile(argv
[1]);
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");