Add HTML Node methods for selecting elements with given CSS Selectors

This commit is contained in:
iska
2015-10-23 22:49:25 +02:00
parent 6af01a1214
commit 95ffccf67b
2 changed files with 29 additions and 6 deletions
+4
View File
@@ -38,6 +38,7 @@ typedef NS_ENUM(unsigned short, HTMLDocumentPosition)
@class HTMLDocument;
@class HTMLElement;
@class CSSSelector;
@interface HTMLNode : NSObject <NSCopying>
@@ -121,6 +122,9 @@ typedef NS_ENUM(unsigned short, HTMLDocumentPosition)
- (HTMLNodeIterator *)nodeIteratorWithShowOptions:(HTMLNodeFilterShowOptions)showOptions
filterBlock:(HTMLNodeFilterValue (^)(HTMLNode *node))filter;
- (HTMLElement *)firstElementMatchingSelector:(CSSSelector *)selector;
- (NSArray<HTMLElement *> *)elementsMatchingSelector:(CSSSelector *)selector;
- (NSString *)treeDescription;
@end
+25 -6
View File
@@ -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<HTMLElement *> *)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