From e773117f1f42add100abb6f5a83822db1302edb8 Mon Sep 17 00:00:00 2001 From: iska Date: Sun, 20 Dec 2015 16:42:08 +0100 Subject: [PATCH] Fix the returned object of the fragment parsing algorithm - Return a non-nil empty array instead of nil when context element is nil - Return a copy of the document root's child nodes instead of the "view"-array - Use the root selector instead of the index-based access when rerunning the algorithm with the same context element --- HTMLKit/HTMLParser.m | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/HTMLKit/HTMLParser.m b/HTMLKit/HTMLParser.m index 6f7bb46..edfd6ea 100644 --- a/HTMLKit/HTMLParser.m +++ b/HTMLKit/HTMLParser.m @@ -17,6 +17,7 @@ #import "HTMLElementAdjustment.h" #import "HTMLMarker.h" #import "NSString+HTMLKit.h" +#import "CSSSelectors.h" @interface HTMLTokenizer (Private) @property (nonatomic, weak) HTMLParser *parser; @@ -124,11 +125,12 @@ - (NSArray *)parseFragmentWithContextElement:(HTMLElement *)contextElement { if (contextElement == nil) { - return nil; + return @[]; } if ([_contextElement isEqual:contextElement]) { - return [_document childNodeAtIndex:0].childNodes.array; + HTMLElement *root = [_document firstElementMatchingSelector:rootSelector()]; + return root? root.childNodes.objectEnumerator.allObjects: @[]; } [self initializeDocument]; @@ -173,7 +175,7 @@ [self runParser]; - return root.childNodes.array; + return root.childNodes.objectEnumerator.allObjects; } - (void)runParser