]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/TNode.cc
ocaml 3.09 transition
[helm.git] / helm / DEVEL / mathml_editor / src / TNode.cc
1 /* This file is part of EdiTeX, an editor of mathematical
2  * expressions based on TeX syntax.
3  * 
4  * Copyright (C) 2002-2003 Luca Padovani <lpadovan@cs.unibo.it>,
5  *                    2003 Paolo Marinelli <pmarinel@cs.unibo.it>.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * For more information, please visit the project's home page
22  * http://helm.cs.unibo.it/editex/
23  * or send an email to <lpadovan@cs.unibo.it>
24  */
25
26 #include "globals.hh"
27 #include "TNode.hh"
28 #include <cassert>
29
30 TNode
31 TNode::next() const
32 {
33   assert(node);
34   DOM::Node p = node.get_nextSibling();
35   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
36   return p;
37 }
38
39 TNode
40 TNode::nextL() const
41 {
42   assert(node);
43   if (TNode n = next())
44     if (n.isG()) return n.firstL();
45     else return n;
46   else return TNode();
47 }
48
49 TNode
50 TNode::prev() const
51 {
52   assert(node);
53   DOM::Node p = node.get_previousSibling();
54   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_previousSibling();
55   return p;
56 }
57
58 TNode
59 TNode::prevL() const
60 {
61   assert(node);
62   if (TNode n = prev())
63     if (n.isG()) return n.lastL();
64     else return n;
65   else return TNode();
66 }
67
68 TNode
69 TNode::last() const
70 {
71   assert(node);
72   DOM::Node p = node.get_lastChild();
73   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_previousSibling();
74   return p;
75 }
76
77 TNode
78 TNode::lastL() const
79 {
80   assert(node);
81   if (TNode n = last())
82     if (n.isG()) return n.lastL();
83     else return n;
84   else
85     return TNode();
86 }
87
88 TNode
89 TNode::first() const
90 {
91   assert(node);
92   DOM::Node p = node.get_firstChild();
93   while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
94   return p;
95 }
96
97 TNode
98 TNode::firstL() const
99 {
100   assert(node);
101   if (TNode n = first())
102     if (n.isG()) return n.firstL();
103     else return n;
104   else
105     return TNode();
106 }
107
108 TNode
109 TNode::core() const
110 {
111   assert(node);
112   // check also if there is a macro embellishment (\not)
113   if (isSb() || isSp()) return first().core();
114   else return *this;
115 }
116
117 TNode
118 TNode::parent() const
119 {
120   assert(node);
121   DOM::Node p = node.get_parentNode();
122   assert(!p || p.get_nodeType() == DOM::Node::ELEMENT_NODE);
123   return p;
124 }
125
126 unsigned
127 TNode::size() const
128 {
129   assert(node);
130   unsigned size = 0;
131   TNode p = first();
132   while (p)
133     {
134       p = p.next();
135       size++;
136     }
137
138   return size;
139 }
140
141 void
142 TNode::remove() const
143 {
144   assert(node);
145   DOM::Node parent = node.get_parentNode();
146   parent.removeChild(node);
147 }
148
149 void
150 TNode::replace(const TNode& newNode) const
151 {
152   assert(node);
153   DOM::Node parent = node.get_parentNode();
154   parent.replaceChild(newNode.node, node);
155 }
156
157 void
158 TNode::replace(const TNode& first, const TNode& last) const
159 {
160   assert(node);
161   assert(first);
162
163   TNode p = first;
164   while (p != last)
165     {
166       TNode next = p.next();
167       insert(p);
168       p = next;
169     }
170   remove();
171 }
172
173 void
174 TNode::insert(const TNode& newNode) const
175 {
176   assert(node);
177   DOM::Node parent = node.get_parentNode();
178   parent.insertBefore(newNode.node, node);
179 }
180
181 void
182 TNode::append(const TNode& newNode) const
183 {
184   assert(node);
185   node.appendChild(newNode.node);
186 }
187
188 void
189 TNode::append(const TNode& first, const TNode& last) const
190 {
191   assert(node);
192   assert(first);
193   assert(last);
194
195   TNode p = first;
196   while (p != last)
197     {
198       TNode next = p.next();
199       append(p);
200       p = next;
201     }
202 }
203
204 void
205 TNode::prepend(const TNode& newNode) const
206 {
207   assert(node);
208   DOM::Node parent = node.get_parentNode();
209   parent.insertBefore(newNode.node, parent.get_firstChild());
210 }
211
212 #if 0
213 #endif
214
215 TNode
216 TNode::child(unsigned pos) const
217 {
218   assert(node);
219   TNode p = first();
220   while (p && pos-- > 0) p = p.next();
221   return p;
222 }
223
224 std::string
225 TNode::get(const std::string& name) const
226 {
227   assert(node);
228   return node.getAttribute(name);
229 }
230
231 void
232 TNode::set(const std::string& name, const std::string& value) const
233 {
234   assert(node);
235   node.setAttribute(name, value);
236 }