forked from Menci/html-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLDocument.cc
More file actions
executable file
·146 lines (128 loc) · 4.08 KB
/
Copy pathHTMLDocument.cc
File metadata and controls
executable file
·146 lines (128 loc) · 4.08 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "HTMLDocument.h"
#include <exception>
namespace html_parser {
HTMLDocument::Element HTMLDocument::internelGetElementById(const DOM::NodeWithChildren* node, const std::string& id)
{
if (id.empty())
{
throw std::logic_error("Empty string passed to getElementById()");
}
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
if (childElement->idList.find(id) != childElement->idList.end())
{
return HTMLDocument::Element(childElement);
}
if (HTMLDocument::Element element = HTMLDocument::internelGetElementById(childElement.get(), id))
{
return element;
}
}
}
return HTMLDocument::Element();
}
void HTMLDocument::internelGetElementsByName(const DOM::NodeWithChildren* node,
const std::string& name,
std::vector<HTMLDocument::Element>& result)
{
if (name.empty())
{
throw std::logic_error("Empty string passed to getElementsByName()");
}
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
if (HTMLDocument::internelGetAttribute(childElement.get(), "name") == name)
{
result.push_back(HTMLDocument::Element(childElement));
}
HTMLDocument::internelGetElementsByName(childElement.get(), name, result);
}
}
}
void HTMLDocument::internelGetElementsByTagName(const DOM::NodeWithChildren* node,
const std::string& tagName,
std::vector<HTMLDocument::Element>& result)
{
if (tagName.empty())
{
throw std::logic_error("Empty string passed to getElementsByTagName()");
}
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
if (childElement->tagName == tagName)
{
result.push_back(HTMLDocument::Element(childElement));
}
HTMLDocument::internelGetElementsByTagName(childElement.get(), tagName, result);
}
}
}
void HTMLDocument::internelGetElementsByClassName(const DOM::NodeWithChildren* node,
const std::string& className,
std::vector<HTMLDocument::Element>& result)
{
if (className.empty())
{
throw std::logic_error("Empty string passed to getElementsByName()");
}
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
if (childElement->classList.find(className) != childElement->classList.end())
{
result.push_back(HTMLDocument::Element(childElement));
}
HTMLDocument::internelGetElementsByClassName(childElement.get(), className, result);
}
}
}
void HTMLDocument::internelGetChildren(const DOM::NodeWithChildren* node, std::vector<Element>& result)
{
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
result.push_back(HTMLDocument::Element(childElement));
}
}
}
void HTMLDocument::internelGetTextContent(const DOM::NodeWithChildren* node, std::string& result)
{
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childElement = std::dynamic_pointer_cast<DOM::ElementNode>(child))
{
HTMLDocument::internelGetTextContent(childElement.get(), result);
}
else if (auto childTextNode = std::dynamic_pointer_cast<DOM::TextNode>(child))
{
result += childTextNode->content;
}
}
}
void HTMLDocument::internelGetDirectTextContent(const DOM::NodeWithChildren* node, std::string& result)
{
for (const std::shared_ptr<DOM::Node>& child : node->children)
{
if (auto childTextNode = std::dynamic_pointer_cast<DOM::TextNode>(child))
{
result += childTextNode->content;
}
}
}
std::string HTMLDocument::internelGetAttribute(const DOM::ElementNode* node, const std::string& name)
{
std::string clonedName = to_lower(name);
std::unordered_map<std::string, std::string>::const_iterator it = node->attributes.find(clonedName);
if (it == node->attributes.end())
return "";
return it->second;
}
}