Add implementation for text-content in the HTML Element
This commit is contained in:
@@ -46,6 +46,10 @@
|
||||
|
||||
- (HTMLNode *)adoptNode:(HTMLNode *)node
|
||||
{
|
||||
if (node == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if (node.type == HTMLNodeDocument) {
|
||||
[NSException raise:HTMLKitNotSupportedError
|
||||
format:@"%@: Not Fount Error, adopting a document node. The operation is not supported.", NSStringFromSelector(_cmd)];
|
||||
|
||||
+16
-3
@@ -7,9 +7,11 @@
|
||||
//
|
||||
|
||||
#import "HTMLElement.h"
|
||||
#import "HTMLDocument.h"
|
||||
#import "HTMLText.h"
|
||||
|
||||
#import "HTMLOrderedDictionary.h"
|
||||
#import "NSString+HTMLKit.h"
|
||||
#import "HTMLText.h"
|
||||
|
||||
@interface HTMLElement ()
|
||||
{
|
||||
@@ -84,8 +86,19 @@
|
||||
|
||||
- (NSString *)textContent
|
||||
{
|
||||
#warning Implement Traversing
|
||||
return nil;
|
||||
NSMutableString *content = [NSMutableString string];
|
||||
for (HTMLNode *node in self.treeEnumerator) {
|
||||
if (node.type == HTMLNodeText) {
|
||||
[content appendString:[(HTMLText *)node data]];
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
- (void)setTextContent:(NSString *)textContent
|
||||
{
|
||||
HTMLText *node = [[HTMLText alloc] initWithData:textContent];
|
||||
[self replaceAllChildNodesWithNode:node];
|
||||
}
|
||||
|
||||
#pragma mark - NSCopying
|
||||
|
||||
@@ -73,6 +73,8 @@ typedef NS_ENUM(short, HTMLNodeType)
|
||||
|
||||
- (HTMLNode *)replaceChildNode:(HTMLNode *)node withNode:(HTMLNode *)replacement;
|
||||
|
||||
- (void)replaceAllChildNodesWithNode:(HTMLNode *)node;
|
||||
|
||||
- (HTMLNode *)removeChildNode:(HTMLNode *)node;
|
||||
|
||||
- (HTMLNode *)removeChildNodeAtIndex:(NSUInteger)index;
|
||||
|
||||
@@ -165,6 +165,16 @@
|
||||
return child;
|
||||
}
|
||||
|
||||
- (void)replaceAllChildNodesWithNode:(HTMLNode *)node
|
||||
{
|
||||
[self removeAllChildNodes];
|
||||
|
||||
if (node != nil) {
|
||||
[self.ownerDocument adoptNode:node];
|
||||
[self insertNode:node beforeChildNode:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (HTMLNode *)removeChildNode:(HTMLNode *)child
|
||||
{
|
||||
if (child.parentNode != self) {
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
{
|
||||
self = [super initWithName:@"#text" type:HTMLNodeText];
|
||||
if (self) {
|
||||
_data = [[NSMutableString alloc] initWithString:data];
|
||||
_data = [[NSMutableString alloc] initWithString:data ?: @""];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user