Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace child node #67

Open
lexiaoyao20 opened this issue Apr 17, 2016 · 2 comments
Open

Replace child node #67

lexiaoyao20 opened this issue Apr 17, 2016 · 2 comments

Comments

@lexiaoyao20
Copy link

How to replace child node in parent node?

@lexiaoyao20
Copy link
Author

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

@nolanw
Copy link
Owner

nolanw commented May 7, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants