Add a tree-description method for HTML Node

The tree description method dumps the HTML tree rooted at this node in the same style
as the HTML5Lib tests
This commit is contained in:
iska
2015-03-30 23:30:41 +02:00
parent e157e746a0
commit 22e397b1f4
2 changed files with 32 additions and 0 deletions
+2
View File
@@ -80,4 +80,6 @@ typedef NS_ENUM(short, HTMLNodeType)
- (NSEnumerator *)treeEnumerator;
- (NSEnumerator *)reverseTreeEnumerator;
- (NSString *)treeDescription;
@end
+30
View File
@@ -353,6 +353,36 @@ NS_INLINE void CheckInvalidCombination(HTMLNode *parent, HTMLNode *node, NSStrin
return copy;
}
#pragma mark - Description
- (NSString *)treeDescription
{
NSMutableString *string = [NSMutableString string];
__weak __block void (^ weakAccumulator) (HTMLNode *, NSUInteger);
void (^ accumulator) (HTMLNode *, NSUInteger);
static NSString *prefix = @"| ";
weakAccumulator = accumulator = ^ (HTMLNode *node, NSUInteger level) {
NSString *indent = [prefix stringByPaddingToLength:level * 2 + prefix.length
withString:@" "
startingAtIndex:0];
if (level > 0) {
[string appendString:@"\n"];
}
[string appendString:indent];
[string appendString:node.description];
for (HTMLNode *child in node.childNodes) {
weakAccumulator(child, level + 1);
}
};
accumulator(self, 0);
return string;
}
- (NSString *)description
{