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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include <DOM/DOMNode.h>
#include "common.h"
@implementation NGDOMNodeWithChildren
- (void)dealloc {
[self->childNodes makeObjectsPerformSelector:
@selector(_domNodeForgetParentNode:)
withObject:nil];
[self->childNodes release];
[super dealloc];
}
- (void)_ensureChildNodes {
if (self->childNodes == nil)
self->childNodes = [[NSMutableArray alloc] init];
}
- (BOOL)_isValidChildNode:(id)_node {
return YES;
}
/* navigation */
- (id<NSObject,DOMNodeList>)childNodes {
[self _ensureChildNodes];
/* casting NSMutableArray to DOMNodeList */
return (id<NSObject,DOMNodeList>)self->childNodes;
}
- (BOOL)hasChildNodes {
return [self->childNodes count] > 0 ? YES : NO;
}
- (id<NSObject,DOMNode>)firstChild {
return [self->childNodes count] > 0
? [self->childNodes objectAtIndex:0]
: nil;
}
- (id<NSObject,DOMNode>)lastChild {
NSUInteger count;
return (count = [self->childNodes count]) > 0
? [self->childNodes objectAtIndex:(count - 1)]
: nil;
}
/* modification */
- (id<NSObject,DOMNode>)removeChild:(id<NSObject,DOMNode>)_node {
NSUInteger idx;
if (self->childNodes == nil)
/* this node has no childnodes ! */
return nil;
if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
/* given node is not a child of this node ! */
return nil;
[[_node retain] autorelease];
[self->childNodes removeObjectAtIndex:idx];
[(id)_node _domNodeForgetParentNode:self];
return _node;
}
- (id<NSObject,DOMNode>)appendChild:(id<NSObject,DOMNode>)_node {
if (_node == nil)
/* adding a 'nil' node ?? */
return nil;
if ([_node nodeType] == DOM_DOCUMENT_FRAGMENT_NODE) {
id fragNodes;
NSUInteger i, count;
NSMutableArray *cache;
fragNodes = [_node childNodes];
if ((count = [fragNodes count]) == 0)
/* no nodes to add */
return nil;
/*
copy to cache, since 'childNodes' result is 'live' and
appendChild modifies the tree
*/
cache = [NSMutableArray arrayWithCapacity:count];
for (i = 0; i < count; i++)
[cache addObject:[fragNodes objectAtIndex:i]];
/* append nodes (in reverse order [array implementation is assumed]) .. */
for (i = count = [cache count]; i > 0; i--)
[self appendChild:[cache objectAtIndex:(i - 1)]];
}
else {
id oldParent;
if ((oldParent = [_node parentNode]))
[oldParent removeChild:_node];
[self _ensureChildNodes];
[self->childNodes addObject:_node];
[(id)_node _domNodeRegisterParentNode:self];
}
/* return the node 'added' */
return _node;
}
/* sibling navigation */
- (id)_domNodeBeforeNode:(id)_node {
NSUInteger idx;
if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
/* given node isn't a child of this node */
return nil;
if (idx == 0)
/* given node is the first child */
return nil;
return [self->childNodes objectAtIndex:(idx - 1)];
}
- (id)_domNodeAfterNode:(id)_node {
NSUInteger idx, count;
if ((count = [self->childNodes count]) == 0)
/* this node has no children at all .. */
return nil;
if ((idx = [self->childNodes indexOfObject:_node]) == NSNotFound)
/* given node isn't a child of this node */
return nil;
if (idx == (count - 1))
/* given node is the last child */
return nil;
return [self->childNodes objectAtIndex:(idx + 1)];
}
@end /* NGDOMNodeWithChildren */
|