From e48ccf42cac2b1ba261b3e564c4d4abc8aa060d1 Mon Sep 17 00:00:00 2001 From: iska Date: Sun, 8 Jan 2017 09:31:37 +0100 Subject: [PATCH] Add HTML Node deep clone implementation --- Sources/HTMLNode.m | 15 +++++++++++++++ Sources/include/HTMLNode.h | 8 ++++++++ 2 files changed, 23 insertions(+) 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.