forked from rangerlee/htmlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
52 lines (44 loc) · 1.76 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "html_parser.hpp"
int main() {
std::string data("<html><body><div><span id=\"a\" class=\"x\">a</span><span id=\"b\">b</span></div></body></html>");
HtmlParser parser;
shared_ptr<HtmlDocument> doc = parser.Parse(data.c_str(), data.size());
std::vector<shared_ptr<HtmlElement>> x = doc->GetElementByClassName("x");
shared_ptr<HtmlElement> b = doc->GetElementById("b");
if(!x.empty()){
std::cout << x[0]->GetName() << std::endl;
std::cout << x[0]->GetAttribute("id") << std::endl;
}
if(b){
std::cout << b->GetName() << std::endl;
std::cout << b->GetValue() << std::endl;
}
data = "<html><meta id=\"a\" /><img src=\"b\"><br id=\"c\"><br id=\"d\"/></html>";
doc = parser.Parse(data.c_str(), data.size());
b = doc->GetElementById("b");
if(b){
shared_ptr<HtmlElement> c = b->GetElementById("c");
if(c){
std::cout << "wrong" << std::endl;
} else {
std::cout << "ok" << std::endl;
}
}
data = "<html><img class=\"aa bb cc\"><br class=\"xx yy zz\"></html>";
doc = parser.Parse(data.c_str(), data.size());
std::vector<shared_ptr<HtmlElement>> ab = doc->GetElementByClassName("aa bb");
std::vector<shared_ptr<HtmlElement>> abc = doc->GetElementByClassName("aa cc bb");
std::vector<shared_ptr<HtmlElement>> xz = doc->GetElementByClassName("xx zz");
if(ab.empty() || abc.empty() || xz.empty()){
std::cout << "wrong" << std::endl;
} else {
std::cout << "ok" << std::endl;
}
std::vector<shared_ptr<HtmlElement>> cc = doc->SelectElement("//img[@class]");
if(cc.empty()){
std::cout << "wrong" << std::endl;
} else {
std::cout << "ok" << std::endl;
}
return 0;
}