2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <DOM/DOMNode.h>
26 @implementation DOMNodeWithChildren
29 [self->childNodes makeObjectsPerformSelector:
30 @selector(_domNodeForgetParentNode:)
33 [self->childNodes release];
37 - (void)_ensureChildNodes {
38 if (self->childNodes == nil)
39 self->childNodes = [[NSMutableArray alloc] init];
42 - (BOOL)_isValidChildNode:(id)_node {
49 [self _ensureChildNodes];
50 return self->childNodes;
52 - (BOOL)hasChildNodes {
53 return [self->childNodes count] > 0 ? YES : NO;
56 return [self->childNodes count] > 0
57 ? [self->childNodes objectAtIndex:0]
63 return (count = [self->childNodes count]) > 0
64 ? [self->childNodes objectAtIndex:(count - 1)]
70 - (id)removeChild:(id)_node {
73 if (self->childNodes == nil)
74 /* this node has no childnodes ! */
77 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
78 /* given node is not a child of this node ! */
81 [[_node retain] autorelease];
82 [self->childNodes removeObjectAtIndex:idx];
83 [_node _domNodeForgetParentNode:self];
88 - (id)appendChild:(id)_node {
90 /* adding a 'nil' node ?? */
93 if ([_node nodeType] == DOM_DOCUMENT_FRAGMENT_NODE) {
96 NSMutableArray *cache;
98 fragNodes = [_node childNodes];
100 if ((count = [fragNodes count]) == 0)
101 /* no nodes to add */
105 copy to cache, since 'childNodes' result is 'live' and
106 appendChild modifies the tree
108 cache = [NSMutableArray arrayWithCapacity:count];
109 for (i = 0; i < count; i++)
110 [cache addObject:[fragNodes objectAtIndex:i]];
112 /* append nodes (in reverse order [array implemention is assumed]) .. */
113 for (i = count = [cache count]; i > 0; i--)
114 [self appendChild:[cache objectAtIndex:(i - 1)]];
119 if ((oldParent = [_node parentNode]))
120 [oldParent removeChild:_node];
122 [self _ensureChildNodes];
124 [self->childNodes addObject:_node];
125 [_node _domNodeRegisterParentNode:self];
128 /* return the node 'added' */
132 /* sibling navigation */
134 - (id)_domNodeBeforeNode:(id)_node {
137 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
138 /* given node isn't a child of this node */
141 /* given node is the first child */
144 return [self->childNodes objectAtIndex:(idx - 1)];
146 - (id)_domNodeAfterNode:(id)_node {
149 if ((count = [self->childNodes count]) == 0)
150 /* this node has no children at all .. */
153 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
154 /* given node isn't a child of this node */
156 if (idx == (count - 1))
157 /* given node is the last child */
160 return [self->childNodes objectAtIndex:(idx + 1)];
163 @end /* DOMNodeWithChildren */