Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 137 lines (90 sloc) 5.232 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
//
// NSAttributedStringHTMLTest.m
// CoreTextExtensions
//
// Created by Claus Broch on 11/01/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//

#import "NSAttributedStringHTMLTest.h"
#import "NSAttributedString+HTML.h"

#import "NSAttributedString+DTCoreText.h"

#import "DTHTMLAttributedStringBuilder.h"

@implementation NSAttributedStringHTMLTest

- (NSAttributedString *)attributedStringFromHTML:(NSString *)html
{
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
DTHTMLAttributedStringBuilder*stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:nil documentAttributes:NULL];

[stringBuilder buildString];

return [stringBuilder generatedAttributedString];
}

- (void)testParagraphs
{
NSString *html = @"Prefix<p>One\ntwo\n<br>three</p><p>New Paragraph</p>Suffix";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<50726566 69780a4f 6e652074 776f20e2 80a87468 7265650a 4e657720 50617261 67726170 680a5375 66666978>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on Paragraph Test differs");
}


- (void)testHeaderParagraphs
{
NSString *html = @"Prefix<h1>One</h1><h2>One</h2><h3>One</h3><h4>One</h4><h5>One</h5><p>New Paragraph</p>Suffix";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<50726566 69780a4f 6e650a4f 6e650a4f 6e650a4f 6e650a4f 6e650a4e 65772050 61726167 72617068 0a537566 666978>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on Paragraph Test differs");
}


- (void)testListParagraphs
{
NSString *html = @"<p>Before</p><ul><li>One</li><li>Two</li></ul><p>After</p>";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<4265666f 72650a09 e280a209 4f6e650a 09e280a2 0954776f 0a416674 65720a>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}

- (void)testImageParagraphs
{
NSString *html = @"<p>Before</p><img src=\"Oliver.jpg\"><h1>Header</h2><p>after</p><p>Some inline <img src=\"Oliver.jpg\"> text.</p>";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<4265666f 72650aef bfbc0a48 65616465 720a6166 7465720a 536f6d65 20696e6c 696e6520 efbfbc20 74657874 2e0a>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}

- (void)testSpaceNormalization
{
NSString *html = @"<p>Now there is some <b>bold</b>\ntext and spaces\n should be normalized.</p>";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<4e6f7720 74686572 65206973 20736f6d 6520626f 6c642074 65787420 616e6420 73706163 65732073 686f756c 64206265 206e6f72 6d616c69 7a65642e 0a>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}

- (void)testSpaceAndNewlines
{
NSString *html = @"<a>bla</a>\nfollows\n<font color=\"blue\">NSString</font> <font color=\"purple\">*</font>str <font color=\"#000000\">=</font> @<font color=\"#E40000\">\"The Quick Brown Fox Brown\"</font>;";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<626c6120 666f6c6c 6f777320 4e535374 72696e67 202a7374 72203d20 40225468 65205175 69636b20 42726f77 6e20466f 78204272 6f776e22 3b>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on List Test differs");
}

- (void)testMissingClosingTagAndSpacing
{
NSString *html = @"<span>image \n <a href=\"http://sv.wikipedia.org/wiki/Fil:V%C3%A4dersoltavlan_cropped.JPG\"\n late</a> last</span>";
NSAttributedString *string = [self attributedStringFromHTML:html];

NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [dump description];

NSString *resultOnMac = @"<696d6167 65206c61 7374>";

STAssertEqualObjects(resultOnIOS, resultOnMac, @"Output on Invalid Tag Test differs");

}


- (void)testAttributedStringColorToHTML {
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString: @"test"];

UIColor *color = [ UIColor colorWithRed: 1.0 green: 0.0 blue: 0.0 alpha: 1.0 ];

[ string setAttributes: [ NSDictionary dictionaryWithObject: (id)color.CGColor forKey: (id)kCTForegroundColorAttributeName ] range: NSMakeRange(0, 2) ];

NSString *expected = @"<span><span style=\"color:#ff0000;\">te</span>st</span>\n";

STAssertEqualObjects([ string htmlString ], expected, @"Output on HTML string color test differs");

}

@end
Something went wrong with that request. Please try again.