-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlElement.cpp
More file actions
293 lines (242 loc) · 7.12 KB
/
HtmlElement.cpp
File metadata and controls
293 lines (242 loc) · 7.12 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// HtmlElement.cpp
// Musee
//
// Created by Denis Stadniczuk on 24/06/14.
// Copyright (c) 2014 Denis Stadniczuk. All rights reserved.
//
#include "HtmlElement.h"
#include "HtmlDocument.h"
#include "StringUtil.h"
HtmlElement::HtmlElement(std::string tag)
:HtmlNode(ELEMENT)
{
tagName = tag;
scrollY = 0;
scrollX = 0;
}
HtmlElement::~HtmlElement()
{
while (children.size() > 0) {
HtmlNode *child = children[0];
children.erase(children.begin());
delete child;
}
}
void HtmlElement::appendChild(HtmlNode *child)
{
child->setParent(this);
child->setDocument(document());
children.push_back(child);
Event nodeInsertedEvent;
nodeInsertedEvent.initEvent("DOMNodeInserted", true, false);
nodeInsertedEvent.target = this;
nodeInsertedEvent.currentTarget = this;
this->dispatchEvent(nodeInsertedEvent);
// nodeInsertedEvent.initEvent("DOMNodeInsertedIntoDocument", false, false);
// document()->dispatchEvent(nodeInsertedEvent);
if (document() != NULL) {
document()->needsReRendering();
}
}
void HtmlElement::removeChild(HtmlNode *child)
{
for (int i = (int)children.size()-1; i >= 0; i--) {
if (children[i] == child) {
children.erase(children.begin() + i);
break;
}
}
if (document() != NULL) {
document()->needsReRendering();
}
}
std::vector<HtmlNode*> HtmlElement::childNodes()
{
return children;
}
HtmlNode* HtmlElement::lastChild()
{
if (children.size() > 0) {
return children[children.size()-1];
}
return NULL;
}
void HtmlElement::setAttribute(std::string attrName, std::string attrVal)
{
attributes[attrName] = attrVal;
}
bool HtmlElement::hasAttribute(std::string attrName)
{
return attributes.find(attrName) != attributes.end();
}
std::string HtmlElement::getAttribute(std::string attrName)
{
return attributes[attrName];
}
std::string HtmlElement::toString()
{
std::string out = "<" + tagName;
out += ">";
for (int i = 0; i < childNodes().size(); i++) {
out += childNodes()[i]->toString();
}
out += "</" + tagName + ">";
return out;
}
void HtmlElement::traverse(void* userPointer, traverseFunc func)
{
(*func)(userPointer, this);
for (int i = 0; i < childNodes().size(); i++) {
if (childNodes()[i]->getNodeType() == ELEMENT) {
((HtmlElement*)childNodes()[i])->traverse(userPointer, func);
} else if (childNodes()[i]->getNodeType() == TEXT) {
(*func)(userPointer, childNodes()[i]);
}
}
}
bool HtmlElement::simpleSelectorApplies(std::string simpleSelector)
{
if (simpleSelector[0] == '.') {
std::string cssClass = simpleSelector.substr(1);
if (hasAttribute("class")) {
std::string elemClasses = getAttribute("class");
std::vector<std::string> classes = StringSplit(elemClasses);
for (std::vector<std::string>::iterator it = classes.begin(); it != classes.end(); it++) {
if ((*it) == cssClass) {
return true;
}
}
}
} else if (simpleSelector[0] == '#') {
std::string cssId = simpleSelector.substr(1);
if (hasAttribute("id")) {
std::string elemId = getAttribute("id");
if (elemId == cssId) {
return true;
}
}
} else {
std::string tagName = toUpper(simpleSelector);
struct convert {
void operator()(char& c) { c = toupper((unsigned char)c); }
};
for_each(tagName.begin(), tagName.end(), convert());
if (getTagName() == tagName) {
return true;
}
}
return false;
}
std::vector<HtmlElement*> HtmlElement::traverseSelector(std::string simpleSelector)
{
std::vector<HtmlElement*> ret;
for (int i = 0; i < childNodes().size(); i++) {
if (childNodes()[i]->getNodeType() == ELEMENT) {
HtmlElement *elem = (HtmlElement*)childNodes()[i];
if (elem->simpleSelectorApplies(simpleSelector)) {
ret.push_back(elem);
}
std::vector<HtmlElement*> cs = elem->traverseSelector(simpleSelector);
for (int j = 0; j < cs.size(); j++) {
ret.push_back(cs[j]);
}
}
}
return ret;
}
std::vector<HtmlElement*> HtmlElement::querySelectorAll(std::string selector)
{
//sanitize it first
if (selector == "") {
return std::vector<HtmlElement*>();
}
std::string firstPart;
std::string nextPart;
for (int i = 1; i < selector.length(); i++) {
if (selector[i] == ' ' || selector[i] == '>' || selector[i] == '+') {
firstPart = selector.substr(0, i);
nextPart = selector.substr(i);
break;
}
}
if (firstPart == "" && nextPart == "") {
firstPart = selector;
}
std::vector<HtmlElement*> ret = traverseSelector(firstPart);
if (nextPart != "" && nextPart[0] == ' ') {
std::vector<HtmlElement*> next;
for (int i = 0; i < ret.size(); i++) {
std::vector<HtmlElement*> np = ret[i]->querySelectorAll(nextPart.substr(1));
for (int j = 0; j < np.size(); j++) {
next.push_back(np[j]);
}
}
ret = next;
}
return ret;
}
//events
void HtmlElement::addEventListener(std::string event, EventListener* listener)
{
if (eventListeners.find(event) == eventListeners.end()) {
eventListeners[event] = std::vector<EventListener*>();
}
eventListeners[event].push_back(listener);
}
void HtmlElement::dispatchEvent(Event &event)
{
HtmlElement *parent = this;
while (parent != NULL) {
event.target = parent;
if (event.type == "scroll") {
ScrollEventData *data = (ScrollEventData*)event.data;
float formerX = parent->scrollX;
float targetX = parent->scrollX - data->deltaX;
if (targetX < 0) {
targetX = 0;
}
if (targetX > parent->scrollW - parent->offsetW) {
targetX = parent->scrollW - parent->offsetW;
}
float leftX = formerX - targetX;
float formerY = parent->scrollY;
float targetY = parent->scrollY - data->deltaY;
if (targetY < 0) {
targetY = 0;
}
if (targetY > parent->scrollH - parent->offsetH) {
targetY = parent->scrollH - parent->offsetH;
}
float leftY = formerY - targetY;
parent->scrollX = targetX;
parent->scrollY = targetY;
data->deltaX = leftX;
data->deltaY = leftY;
std::cout << parent->getTagName() << " " << parent->scrollX << " " << parent->scrollY<<"/"<< parent->scrollH<<":"<<parent->offsetH << " " << leftX << " " << leftY << std::endl;
if (document()) {
document()->needsReRendering();
}
}
if (parent->eventListeners.find(event.type) != parent->eventListeners.end()) {
for (int i = 0; i < parent->eventListeners[event.type].size(); i++) {
parent->eventListeners[event.type][i]->call(event);
}
}
if (!event.bubbles) {
break;
}
parent = parent->parentNode();
}
if (!event.canceled) {
//default action
}
}
float HtmlElement::scrollLeft()
{
return scrollX;
}
float HtmlElement::scrollTop()
{
return scrollY;
}