Refactor selectors code
- Remove simple sequence - Remove simple selector protocol - Let selectors subclass CSSSelector directly
This commit is contained in:
1 parent
9905f45e27
commit
c44c77d63d
11 files changed
+65
-145
No files matched your search
@@ -7,7 +7,7 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "CSSSimpleSelector.h"
|
||||
#import "CSSSelector.h"
|
||||
|
||||
typedef NS_ENUM(NSUInteger, CSSAttributeSelectorType)
|
||||
{
|
||||
@@ -21,17 +21,18 @@ typedef NS_ENUM(NSUInteger, CSSAttributeSelectorType)
|
||||
CSSAttributeSelectorNot
|
||||
};
|
||||
|
||||
@interface CSSAttributeSelector : NSObject <CSSSimpleSelector>
|
||||
@interface CSSAttributeSelector : CSSSelector
|
||||
|
||||
@property (nonatomic, assign) CSSAttributeSelectorType type;
|
||||
@property (nonatomic, copy) NSString * _Nonnull name;
|
||||
@property (nonatomic, copy) NSString * _Nonnull value;
|
||||
@property (nonatomic, strong, readonly) NSString * _Nonnull name;
|
||||
@property (nonatomic, strong, readonly) NSString * _Nonnull value;
|
||||
|
||||
+ (nullable instancetype)selectorForClass:(nonnull NSString *)className;
|
||||
+ (nullable instancetype)selectorForId:(nonnull NSString *)elementId;
|
||||
+ (nullable instancetype)classSelector:(nonnull NSString *)className;
|
||||
+ (nullable instancetype)idSelector:(nonnull NSString *)elementId;
|
||||
+ (nullable instancetype)attributeSelector:(nonnull NSString *)attributeName;
|
||||
|
||||
- (nullable instancetype)initWithType:(CSSAttributeSelectorType)type
|
||||
attributeName:(nonnull NSString *)name
|
||||
attrbiuteValue:(nullable NSString *)value;
|
||||
attrbiuteValue:(nonnull NSString *)value;
|
||||
|
||||
@end
|
||||
@@ -20,16 +20,21 @@
|
||||
|
||||
@implementation CSSAttributeSelector
|
||||
|
||||
+ (instancetype)selectorForClass:(NSString *)className
|
||||
+ (instancetype)classSelector:(NSString *)className
|
||||
{
|
||||
return [[self alloc] initWithType:CSSAttributeSelectorIncludes attributeName:@"class" attrbiuteValue:className];
|
||||
}
|
||||
|
||||
+ (instancetype)selectorForId:(NSString *)elementId
|
||||
+ (instancetype)idSelector:(NSString *)elementId
|
||||
{
|
||||
return [[self alloc] initWithType:CSSAttributeSelectorExactMatch attributeName:@"id" attrbiuteValue:elementId];
|
||||
}
|
||||
|
||||
+ (nullable instancetype)attributeSelector:(nonnull NSString *)attributeName
|
||||
{
|
||||
return [[self alloc] initWithType:CSSAttributeSelectorExists attributeName:attributeName attrbiuteValue:@""];
|
||||
}
|
||||
|
||||
- (instancetype)initWithType:(CSSAttributeSelectorType)type
|
||||
attributeName:(NSString *)name
|
||||
attrbiuteValue:(NSString *)value
|
||||
@@ -37,8 +42,8 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.type = type;
|
||||
self.name = [name copy];
|
||||
self.value = [value copy];
|
||||
_name = [name copy];
|
||||
_value = [value copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -84,6 +89,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Description
|
||||
|
||||
- (NSString *)debugDescription
|
||||
{
|
||||
if (self.type == CSSAttributeSelectorExists) {
|
||||
@@ -101,9 +108,4 @@
|
||||
return [NSString stringWithFormat:@"[%@%@'%@']", self.name, matcher, self.value];
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, self.debugDescription];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -10,9 +10,8 @@
|
||||
|
||||
@class HTMLElement;
|
||||
|
||||
typedef BOOL (^ CSSSelectorAcceptElementBlock)(HTMLElement * _Nonnull node);
|
||||
@interface CSSSelector : NSObject
|
||||
|
||||
@protocol CSSSelector <NSObject>
|
||||
@required
|
||||
- (BOOL)acceptElement:(nonnull HTMLElement *)element;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// CSSSelector.m
|
||||
// HTMLKit
|
||||
//
|
||||
// Created by Iska on 15/10/15.
|
||||
// Copyright (c) 2015 BrainCookie. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "CSSSelector.h"
|
||||
|
||||
@implementation CSSSelector
|
||||
|
||||
- (BOOL)acceptElement:(HTMLElement *)element
|
||||
{
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - Description
|
||||
|
||||
- (NSString *)debugDescription
|
||||
{
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
return @"";
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, self.debugDescription];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -7,8 +7,6 @@
|
||||
//
|
||||
|
||||
#import "CSSSelector.h"
|
||||
#import "CSSSimpleSelector.h"
|
||||
#import "CSSSimpleSequence.h"
|
||||
#import "CSSTypeSelector.h"
|
||||
#import "CSSAttributeSelector.h"
|
||||
#import "CSSPseudoClassSelector.h"
|
||||
@@ -1,14 +0,0 @@
|
||||
//
|
||||
// CSSSimpleSelector.h
|
||||
// HTMLKit
|
||||
//
|
||||
// Created by Iska on 14/05/15.
|
||||
// Copyright (c) 2015 BrainCookie. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "CSSSelector.h"
|
||||
|
||||
@protocol CSSSimpleSelector <CSSSelector>
|
||||
|
||||
@end
|
||||
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// CSSSimpleSequence.h
|
||||
// HTMLKit
|
||||
//
|
||||
// Created by Iska on 14/05/15.
|
||||
// Copyright (c) 2015 BrainCookie. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "CSSSelector.h"
|
||||
#import "CSSSimpleSelector.h"
|
||||
|
||||
@class CSSTypeSelector;
|
||||
|
||||
@interface CSSSimpleSequence : NSObject <CSSSelector>
|
||||
|
||||
- (nullable instancetype)initWithType:(nonnull CSSTypeSelector *)selector;
|
||||
- (nullable instancetype)initWithSelectors:(nonnull NSArray<id<CSSSimpleSelector>> *)selectors;
|
||||
|
||||
- (void)addSelector:(nonnull id<CSSSimpleSelector>)selector;
|
||||
|
||||
@end
|
||||
@@ -1,64 +0,0 @@
|
||||
//
|
||||
// CSSSimpleSequence.m
|
||||
// HTMLKit
|
||||
//
|
||||
// Created by Iska on 14/05/15.
|
||||
// Copyright (c) 2015 BrainCookie. All rights reserved.
|
||||
//
|
||||
|
||||
#import "CSSSimpleSequence.h"
|
||||
|
||||
@interface CSSSimpleSequence ()
|
||||
{
|
||||
NSMutableArray *_selectors;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation CSSSimpleSequence
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
return [self initWithSelectors:@[]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithType:(CSSTypeSelector *)selector
|
||||
{
|
||||
return [self initWithSelectors:@[selector]];
|
||||
}
|
||||
|
||||
- (instancetype)initWithSelectors:(NSArray *)selectors
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_selectors = [[NSMutableArray alloc] initWithArray:selectors];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)addSelector:(id<CSSSimpleSelector>)selector
|
||||
{
|
||||
[_selectors addObject:selector];
|
||||
}
|
||||
|
||||
- (BOOL)acceptElement:(HTMLElement *)element
|
||||
{
|
||||
for (id<CSSSelector> selector in _selectors) {
|
||||
if (![selector acceptElement:element]) {
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)debugDescription
|
||||
{
|
||||
NSArray *descriptions = [_selectors valueForKey:@"debugDescription"];
|
||||
return [descriptions componentsJoinedByString:@""];
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, self.debugDescription];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -8,11 +8,10 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "CSSSelector.h"
|
||||
#import "CSSSimpleSelector.h"
|
||||
|
||||
@interface CSSTypeSelector : NSObject <CSSSimpleSelector>
|
||||
@interface CSSTypeSelector : CSSSelector
|
||||
|
||||
@property (nonatomic, copy) NSString * _Nonnull type;
|
||||
@property (nonatomic, strong, readonly) NSString * _Nonnull type;
|
||||
|
||||
+ (nullable instancetype)universalSelector;
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.type = [type copy];
|
||||
_type = [type copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -37,18 +37,14 @@
|
||||
if ([_type isEqualToString:@"*"] || [_type isEqualToStringIgnoringCase:element.tagName]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - Description
|
||||
|
||||
- (NSString *)debugDescription
|
||||
{
|
||||
return self.type;
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, self.debugDescription];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user