From 8893f28e4d78a70ba1ecc51caf42ec35e218e51e Mon Sep 17 00:00:00 2001 From: iska Date: Mon, 30 Nov 2015 20:22:35 +0100 Subject: [PATCH] Add HTML Element class-list property --- HTMLKit.playground/Contents.swift | 4 ++++ HTMLKit/HTMLElement.h | 7 +++++-- HTMLKit/HTMLElement.m | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/HTMLKit.playground/Contents.swift b/HTMLKit.playground/Contents.swift index 70e154f..98fb7f9 100644 --- a/HTMLKit.playground/Contents.swift +++ b/HTMLKit.playground/Contents.swift @@ -47,3 +47,7 @@ let filter = HTMLSelectorNodeFilter(selector: nay(typeSelector("p"))) for node in body.nodeIteratorWithShowOptions([.Element], filter: filter) { print(node.innerHTML) } + +h2.classList.add(["class2", "class3"]) + +print(body.outerHTML) diff --git a/HTMLKit/HTMLElement.h b/HTMLKit/HTMLElement.h index a4c88ec..03c1153 100644 --- a/HTMLKit/HTMLElement.h +++ b/HTMLKit/HTMLElement.h @@ -9,6 +9,7 @@ #import #import "HTMLNamespaces.h" #import "HTMLNode.h" +#import "HTMLDOMTokenList.h" NS_ASSUME_NONNULL_BEGIN @@ -18,12 +19,14 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy, readonly) NSString *tagName; -@property (nonatomic, strong) NSMutableDictionary *attributes; - @property (nonatomic, copy) NSString *elementId; @property (nonatomic, copy) NSString *className; +@property (nonatomic, strong, readonly) HTMLDOMTokenList *classList; + +@property (nonatomic, strong) NSMutableDictionary *attributes; + - (instancetype)initWithTagName:(NSString *)tagName; - (instancetype)initWithTagName:(NSString *)tagName attributes:(NSDictionary *)attributes; - (instancetype)initWithTagName:(NSString *)tagName namespace:(HTMLNamespace)htmlNamespace attributes:(NSDictionary *)attributes; diff --git a/HTMLKit/HTMLElement.m b/HTMLKit/HTMLElement.m index 5a8861c..8cbb9f4 100644 --- a/HTMLKit/HTMLElement.m +++ b/HTMLKit/HTMLElement.m @@ -10,7 +10,7 @@ #import "HTMLParser.h" #import "HTMLDocument.h" #import "HTMLText.h" - +#import "HTMLDOMTokenList.h" #import "HTMLOrderedDictionary.h" #import "NSString+HTMLKit.h" @@ -53,18 +53,35 @@ return self; } -#pragma mark - Attributes +#pragma mark - Special Attributes - (NSString *)elementId { return _attributes[@"id"] ?: @""; } +- (void)setElementId:(NSString *)elementId +{ + _attributes[@"id"] = elementId; +} + - (NSString *)className { - return _attributes[@"class"]; + return _attributes[@"class"] ?: @""; } +- (void)setClassName:(NSString *)className +{ + _attributes[@"class"] = className; +} + +- (HTMLDOMTokenList *)classList +{ + return [[HTMLDOMTokenList alloc] initWithElement:self attribute:@"class" value:self.className]; +} + +#pragma mark - Attributes + - (BOOL)hasAttribute:(NSString *)name { return _attributes[name] != nil;