blob: 92cf6f0d16487572426de5899b7263a271ca06bb (
plain)
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
|
#ifndef __DOCTREE_HH__
#define __DOCTREE_HH__
#include "lout/misc.hh"
class DoctreeNode {
public:
DoctreeNode *parent;
DoctreeNode *sibling;
DoctreeNode *lastChild;
int num; // unique ascending id
int element;
lout::misc::SimpleVector<char*> *klass;
const char *pseudo;
const char *id;
DoctreeNode () {
parent = NULL;
sibling = NULL;
lastChild = NULL;
klass = NULL;
pseudo = NULL;
id = NULL;
element = 0;
};
~DoctreeNode () {
dFree ((void*) id);
while (lastChild) {
DoctreeNode *n = lastChild;
lastChild = lastChild->sibling;
delete n;
}
if (klass) {
for (int i = 0; i < klass->size (); i++)
dFree (klass->get(i));
delete klass;
}
}
};
/**
* \brief HTML document tree interface.
*
* The Doctree class defines the interface to the parsed HTML document tree
* as it is used for CSS selector matching.
*/
class Doctree {
private:
DoctreeNode *topNode;
DoctreeNode *rootNode;
int num;
public:
Doctree () {
rootNode = new DoctreeNode;
topNode = rootNode;
num = 0;
};
~Doctree () {
delete rootNode;
};
DoctreeNode *push () {
DoctreeNode *dn = new DoctreeNode ();
dn->parent = topNode;
dn->sibling = dn->parent->lastChild;
dn->parent->lastChild = dn;
dn->num = num++;
topNode = dn;
return dn;
};
void pop () {
assert (topNode != rootNode); // never pop the root node
topNode = topNode->parent;
};
inline DoctreeNode *top () {
if (topNode != rootNode)
return topNode;
else
return NULL;
};
inline DoctreeNode *parent (const DoctreeNode *node) {
if (node->parent != rootNode)
return node->parent;
else
return NULL;
};
inline DoctreeNode *sibling (const DoctreeNode *node) {
return node->sibling;
};
};
#endif
|