aboutsummaryrefslogtreecommitdiff
path: root/src/doctree.hh
blob: c46a5a10cf2aa740cebb4e0e36dd53da34cfc2b7 (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
#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 () {
         topNode = NULL;
         rootNode = NULL;
         num = 0;
      };

      ~Doctree () { 
         if (rootNode)
            delete rootNode;
      };

      DoctreeNode *push () {
         DoctreeNode *dn = new DoctreeNode ();
         dn->parent = topNode;
         if (dn->parent) {
            dn->sibling = dn->parent->lastChild;
            dn->parent->lastChild = dn;
         }
         dn->num = num++;
         if (!rootNode)
            rootNode = dn;
         topNode = dn;
         return dn;
      };

      void pop () {
         if (topNode)
            topNode = topNode->parent;
      };

      inline DoctreeNode *top () {
         return topNode;
      };
};

#endif