HTMLReader icon indicating copy to clipboard operation
HTMLReader copied to clipboard

Replace child node

Open lexiaoyao20 opened this issue 9 years ago • 2 comments

How to replace child node in parent node?

lexiaoyao20 avatar Apr 17 '16 07:04 lexiaoyao20

I have create a category.


@interface HTMLNode (Replace)

- (void)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)newNode;

- (void)replaceChildNodeAtIndex:(NSUInteger)index withNode:(HTMLNode *)newNode;

@end

@implementation HTMLNode (Replace)

- (void)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)newNode {
    NSUInteger index = [self.mutableChildren indexOfObject:node];
    if (index == NSNotFound) {
        return;
    }

    [self replaceChildNodeAtIndex:index withNode:newNode];
}

- (void)replaceChildNodeAtIndex:(NSUInteger)index withNode:(HTMLNode *)newNode {
    if (self.mutableChildren.count <= index) {
        return;
    }

    [self.mutableChildren replaceObjectAtIndex:index withObject:newNode];
}

@end

lexiaoyao20 avatar Apr 17 '16 07:04 lexiaoyao20

Hello! I see you've answered your own question, and that's the right way to go about it. I'm guessing you're not the only one wondering, and in either case some convenience methods would be helpful. I'll see about adding these methods to the project.

nolanw avatar May 07 '16 17:05 nolanw