diff --git a/HTMLKit/HTMLDocument.h b/HTMLKit/HTMLDocument.h index 7c0655f..222ea81 100644 --- a/HTMLKit/HTMLDocument.h +++ b/HTMLKit/HTMLDocument.h @@ -27,6 +27,12 @@ typedef NS_ENUM(short, HTMLDocumentReadyState) @property (nonatomic, assign, readonly) HTMLDocumentReadyState readyState; +@property (nonatomic, strong) HTMLElement *rootElement; + +@property (nonatomic, strong) HTMLElement *head; + +@property (nonatomic, strong) HTMLElement *body; + + (instancetype)documentWithString:(NSString *)string; - (HTMLNode *)adoptNode:(HTMLNode *)node; diff --git a/HTMLKit/HTMLDocument.m b/HTMLKit/HTMLDocument.m index 1837124..912f269 100644 --- a/HTMLKit/HTMLDocument.m +++ b/HTMLKit/HTMLDocument.m @@ -8,6 +8,7 @@ #import "HTMLDocument.h" #import "HTMLParser.h" +#import "HTMLNodeIterator.h" #import "HTMLKitDOMExceptions.h" @interface HTMLNode (Private) @@ -66,6 +67,53 @@ } } +#pragma mark - + +- (HTMLElement *)rootElement +{ + for (HTMLNode *node = self.firstChiledNode; node; node = node.nextSibling) { + if (node.nodeType == HTMLNodeElement) { + return node.asElement; + } + } + return nil; +} + +- (void)setRootElement:(HTMLElement *)rootElement +{ + [self replaceChildNode:self.rootElement withNode:rootElement]; +} + +- (HTMLElement *)head +{ + for (HTMLNode *node in [self nodeIteratorWithFilter:nil showOptions:HTMLNodeFilterShowElement]) { + if ([node.asElement.tagName isEqualToString:@"head"]) { + return node.asElement; + } + } + return nil; +} + +- (void)setHead:(HTMLElement *)head +{ + [self replaceChildNode:self.head withNode:head]; +} + +- (HTMLElement *)body +{ + for (HTMLNode *node in [self nodeIteratorWithFilter:nil showOptions:HTMLNodeFilterShowElement]) { + if ([node.asElement.tagName isEqualToString:@"body"]) { + return node.asElement; + } + } + return nil; +} + +- (void)setBody:(HTMLElement *)body +{ + [self replaceChildNode:self.body withNode:body]; +} + #pragma mark - Node Iterators - (void)attachNodeIterator:(HTMLNodeIterator *)iterator