2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <DOM/DOMNode.h>
25 @implementation NGDOMNodeWithChildren
28 [self->childNodes makeObjectsPerformSelector:
29 @selector(_domNodeForgetParentNode:)
32 [self->childNodes release];
36 - (void)_ensureChildNodes {
37 if (self->childNodes == nil)
38 self->childNodes = [[NSMutableArray alloc] init];
41 - (BOOL)_isValidChildNode:(id)_node {
47 - (id<NSObject,DOMNodeList>)childNodes {
48 [self _ensureChildNodes];
50 /* casting NSMutableArray to DOMNodeList */
51 return (id<NSObject,DOMNodeList>)self->childNodes;
53 - (BOOL)hasChildNodes {
54 return [self->childNodes count] > 0 ? YES : NO;
56 - (id<NSObject,DOMNode>)firstChild {
57 return [self->childNodes count] > 0
58 ? [self->childNodes objectAtIndex:0]
61 - (id<NSObject,DOMNode>)lastChild {
64 return (count = [self->childNodes count]) > 0
65 ? [self->childNodes objectAtIndex:(count - 1)]
71 - (id<NSObject,DOMNode>)removeChild:(id<NSObject,DOMNode>)_node {
74 if (self->childNodes == nil)
75 /* this node has no childnodes ! */
78 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
79 /* given node is not a child of this node ! */
82 [[_node retain] autorelease];
83 [self->childNodes removeObjectAtIndex:idx];
84 [(id)_node _domNodeForgetParentNode:self];
89 - (id<NSObject,DOMNode>)appendChild:(id<NSObject,DOMNode>)_node {
91 /* adding a 'nil' node ?? */
94 if ([_node nodeType] == DOM_DOCUMENT_FRAGMENT_NODE) {
97 NSMutableArray *cache;
99 fragNodes = [_node childNodes];
101 if ((count = [fragNodes count]) == 0)
102 /* no nodes to add */
106 copy to cache, since 'childNodes' result is 'live' and
107 appendChild modifies the tree
109 cache = [NSMutableArray arrayWithCapacity:count];
110 for (i = 0; i < count; i++)
111 [cache addObject:[fragNodes objectAtIndex:i]];
113 /* append nodes (in reverse order [array implemention is assumed]) .. */
114 for (i = count = [cache count]; i > 0; i--)
115 [self appendChild:[cache objectAtIndex:(i - 1)]];
120 if ((oldParent = [_node parentNode]))
121 [oldParent removeChild:_node];
123 [self _ensureChildNodes];
125 [self->childNodes addObject:_node];
126 [(id)_node _domNodeRegisterParentNode:self];
129 /* return the node 'added' */
133 /* sibling navigation */
135 - (id)_domNodeBeforeNode:(id)_node {
138 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
139 /* given node isn't a child of this node */
142 /* given node is the first child */
145 return [self->childNodes objectAtIndex:(idx - 1)];
147 - (id)_domNodeAfterNode:(id)_node {
150 if ((count = [self->childNodes count]) == 0)
151 /* this node has no children at all .. */
154 if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
155 /* given node isn't a child of this node */
157 if (idx == (count - 1))
158 /* given node is the last child */
161 return [self->childNodes objectAtIndex:(idx + 1)];
164 @end /* NGDOMNodeWithChildren */