diff --git a/Sources/HTMLNode.m b/Sources/HTMLNode.m index d188bb0..e1c74c3 100644 --- a/Sources/HTMLNode.m +++ b/Sources/HTMLNode.m @@ -668,6 +668,21 @@ NS_INLINE void CheckInvalidCombination(HTMLNode *parent, HTMLNode *node, NSStrin #endif +#pragma mark - Clone + +- (instancetype)cloneNodeDeep:(BOOL)deep +{ + HTMLNode *copy = [self copy]; + + if (deep) { + for (HTMLNode *child in self.childNodes) { + [copy appendNode:[child cloneNodeDeep:YES]]; + } + } + + return copy; +} + #pragma mark - NSCopying - (id)copyWithZone:(NSZone *)zone diff --git a/Sources/include/HTMLNode.h b/Sources/include/HTMLNode.h index 5e94f2e..c9ca332 100644 --- a/Sources/include/HTMLNode.h +++ b/Sources/include/HTMLNode.h @@ -186,6 +186,14 @@ typedef NS_OPTIONS(unsigned short, HTMLDocumentPosition) */ - (BOOL)isEmpty; +/** + Clones this node. + + @param deep If `YES` then also clones child nodes. Otherwise a shallow clone is returned, which behaves the same as `copy`. + @return A clone of this node. + */ +- (instancetype)cloneNodeDeep:(BOOL)deep; + /** Returns the child node at a given index.