diff --git a/HTMLKit/HTMLNode.h b/HTMLKit/HTMLNode.h index eb07dbc..9d753a5 100644 --- a/HTMLKit/HTMLNode.h +++ b/HTMLKit/HTMLNode.h @@ -38,6 +38,7 @@ typedef NS_ENUM(unsigned short, HTMLDocumentPosition) @class HTMLDocument; @class HTMLElement; +@class CSSSelector; @interface HTMLNode : NSObject @@ -121,6 +122,9 @@ typedef NS_ENUM(unsigned short, HTMLDocumentPosition) - (HTMLNodeIterator *)nodeIteratorWithShowOptions:(HTMLNodeFilterShowOptions)showOptions filterBlock:(HTMLNodeFilterValue (^)(HTMLNode *node))filter; +- (HTMLElement *)firstElementMatchingSelector:(CSSSelector *)selector; +- (NSArray *)elementsMatchingSelector:(CSSSelector *)selector; + - (NSString *)treeDescription; @end diff --git a/HTMLKit/HTMLNode.m b/HTMLKit/HTMLNode.m index fdff072..bcfcfaa 100644 --- a/HTMLKit/HTMLNode.m +++ b/HTMLKit/HTMLNode.m @@ -13,6 +13,7 @@ #import "HTMLText.h" #import "HTMLComment.h" #import "HTMLKitDOMExceptions.h" +#import "CSSSelector.h" @interface HTMLDocument (Private) - (void)runRemovingStepsForNode:(HTMLNode *)oldNode @@ -386,6 +387,29 @@ return [HTMLNodeIterator iteratorWithNode:self showOptions:showOptions filter:filter]; } +#pragma mark - Selectors + +- (HTMLElement *)firstElementMatchingSelector:(CSSSelector *)selector +{ + for (HTMLElement *element in [self nodeIteratorWithShowOptions:HTMLNodeFilterShowElement filter:nil]) { + if ([selector acceptElement:element]) { + return element; + } + } + return nil; +} + +- (NSArray *)elementsMatchingSelector:(CSSSelector *)selector +{ + NSMutableArray *result = [NSMutableArray array]; + for (HTMLElement *element in [self nodeIteratorWithShowOptions:HTMLNodeFilterShowElement filter:nil]) { + if ([selector acceptElement:element]) { + [result addObject:element]; + } + } + return result; +} + #ifndef HTMLKIT_NO_DOM_CHECKS #pragma mark - Validity Checks @@ -610,12 +634,7 @@ NS_INLINE void CheckInvalidCombination(HTMLNode *parent, HTMLNode *node, NSStrin - (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p %@>", self.class, self, self.name]; -} - -- (NSString *)debugDescription -{ - return self.treeDescription; + return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, self.name]; } - (id)debugQuickLookObject