diff --git a/HTMLKit/HTMLElement.h b/HTMLKit/HTMLElement.h index 499c855..f7bf5b7 100644 --- a/HTMLKit/HTMLElement.h +++ b/HTMLKit/HTMLElement.h @@ -26,4 +26,9 @@ - (instancetype)initWithTagName:(NSString *)tagName attributes:(NSDictionary *)attributes; - (instancetype)initWithTagName:(NSString *)tagName attributes:(NSDictionary *)attributes namespace:(HTMLNamespace)namespace; +- (BOOL)hasAttribute:(NSString *)name; +- (NSString *)objectForKeyedSubscript:(NSString *)name; +- (void)setObject:(NSString *)value forKeyedSubscript:(NSString *)attribute; +- (void)removeAttribute:(NSString *)name; + @end diff --git a/HTMLKit/HTMLElement.m b/HTMLKit/HTMLElement.m index 1c9ff5a..746287d 100644 --- a/HTMLKit/HTMLElement.m +++ b/HTMLKit/HTMLElement.m @@ -17,6 +17,8 @@ @implementation HTMLElement +#pragma mark - Init + - (instancetype)init { return [self initWithTagName:nil]; @@ -46,10 +48,52 @@ return self; } +#pragma mark - Attributes + +- (NSString *)id +{ + return _attributes[@"id"] ?: @""; +} + +- (NSString *)className +{ + return _attributes[@"class"]; +} + +- (BOOL)hasAttribute:(NSString *)name +{ + return _attributes[name] != nil; +} + +- (NSString *)objectForKeyedSubscript:(NSString *)name; +{ + return _attributes[name]; +} + +- (void)setObject:(NSString *)value forKeyedSubscript:(NSString *)attribute +{ + _attributes[attribute] = value; +} + +- (void)removeAttribute:(NSString *)name +{ + [_attributes removeObjectForKey:name]; +} + - (NSString *)textContent { #warning Implement Traversing return nil; } +#pragma mark - NSCopying + +- (id)copyWithZone:(NSZone *)zone +{ + HTMLElement *copy = [super copyWithZone:zone]; + copy->_tagName = [_tagName copy]; + copy->_attributes = [_attributes copy]; + return copy; +} + @end