Add implementation for the negation and "has" selectors

The negation selector is specified here:
http://www.w3.org/TR/css3-selectors/#negation

The "has" selector is an extra convenience selector
This commit is contained in:
iska committed 2015-10-19 21:41:33 +02:00
1 parent c44c77d63d
commit 6020b2bdd9
3 files changed
+113

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
//
// CSSPseudoFunctionSelector.h
// HTMLKit
//
// Created by Iska on 07/10/15.
// Copyright © 2015 BrainCookie. All rights reserved.
//
#import "CSSSelector.h"
@interface CSSPseudoFunctionSelector : CSSSelector
+ (nullable instancetype)notSelector:(nonnull CSSSelector *)selector;
+ (nullable instancetype)hasSelector:(nonnull CSSSelector *)selector;
@end
+89
View File
@@ -0,0 +1,89 @@
//
// CSSPseudoFunctionSelector.m
// HTMLKit
//
// Created by Iska on 07/10/15.
// Copyright © 2015 BrainCookie. All rights reserved.
//
#import "CSSPseudoFunctionSelector.h"
#import "HTMLElement.h"
#pragma mark - Declarations
@interface CSSNotSelector : CSSPseudoFunctionSelector
@end
@interface CSSHasSelector : CSSPseudoFunctionSelector
@end
#pragma mark - Base Function Selector
@interface CSSPseudoFunctionSelector ()
{
CSSSelector *_selector;
}
@property (nonatomic, strong, readonly) CSSSelector *selector;
@end
@implementation CSSPseudoFunctionSelector
@synthesize selector = _selector;
+ (nullable instancetype)notSelector:(nonnull CSSSelector *)selector
{
return [[CSSNotSelector alloc] initWithSelector:selector];
}
+ (nullable instancetype)hasSelector:(nonnull CSSSelector *)selector
{
return [[CSSHasSelector alloc] initWithSelector:selector];
}
- (instancetype)initWithSelector:(CSSSelector *)selector
{
self = [super init];
if (self) {
_selector = selector;
}
return self;
}
@end
#pragma mark - Not Selector
@implementation CSSNotSelector
- (BOOL)acceptElement:(HTMLElement *)element
{
return ![self.selector acceptElement:element];
}
- (NSString *)debugDescription
{
return [NSString stringWithFormat:@":not(%@)", self.selector.debugDescription];
}
@end
#pragma mark - Has Selector
@implementation CSSHasSelector
- (BOOL)acceptElement:(HTMLElement *)element
{
for (HTMLNode *child in element.childNodes) {
if (child.nodeType == HTMLNodeElement && [self.selector acceptElement:child.asElement]) {
return YES;
}
}
return NO;
}
- (NSString *)debugDescription
{
return [NSString stringWithFormat:@":has(%@)", self.selector.debugDescription];
}
@end