forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSAttributedStringHTMLTest.m
More file actions
246 lines (182 loc) · 8.64 KB
/
Copy pathNSAttributedStringHTMLTest.m
File metadata and controls
246 lines (182 loc) · 8.64 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// NSAttributedStringHTMLTest.m
// DTCoreText
//
// Created by Claus Broch on 11/01/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#import "NSAttributedStringHTMLTest.h"
#import <DTCoreText/DTCoreText.h>
#import <XCTest/XCTest.h>
@implementation NSAttributedStringHTMLTest
- (void)dumpOneResult:(NSString *)oneResult versusOtherResult:(NSString *)otherResult
{
NSMutableString *dumpOutput = [[NSMutableString alloc] init];
NSData *dump1 = [oneResult dataUsingEncoding:NSUTF8StringEncoding];
NSData *dump2 = [otherResult dataUsingEncoding:NSUTF8StringEncoding];
char *bytes1 = (char *)[dump1 bytes];
char *bytes2 = (char *)[dump2 bytes];
NSUInteger longerLength = MAX([dump1 length], [dump2 length]);
for (NSInteger i = 0; i < longerLength; i++)
{
NSString *out1 = @"- -";
NSString *out2 = @"- -";
if (i<[dump1 length])
{
char b = bytes1[i];
out1 = [NSString stringWithFormat:@"%x", b];
}
if (i<[dump2 length])
{
char b = bytes2[i];
out2 = [NSString stringWithFormat:@"%x", b];
}
[dumpOutput appendFormat:@"%li: %@ %@\n", (long)i, out1, out2];
}
NSLog(@"Dump\%@", dumpOutput);
}
- (NSAttributedString *)attributedStringFromHTML:(NSString *)html
{
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
DTHTMLAttributedStringBuilder*stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:nil documentAttributes:NULL];
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 = [self hexStringForData:dump];
NSString *resultOnMac = @"5072656669780a4f6e652074776f20e280a874687265650a4e6577205061726167726170680a537566666978";
//[self dumpOneResult:resultOnIOS versusOtherResult:resultOnMac];
XCTAssertTrue([resultOnIOS isEqualToString: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 = [self hexStringForData:dump];
NSString *resultOnMac = @"5072656669780a4f6e650a4f6e650a4f6e650a4f6e650a4f6e650a4e6577205061726167726170680a537566666978";
XCTAssertTrue([resultOnIOS isEqualToString: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 = [self hexStringForData:dump];
NSString *resultOnMac = @"4265666f72650a09e280a2094f6e650a09e280a20954776f0a41667465720a";
XCTAssertTrue([resultOnIOS isEqualToString:resultOnMac], @"Output on List Test differs");
}
- (void)testImageParagraphs
{
// needs the size
NSString *html = @"<p>Before</p><img src=\"Oliver.jpg\"><h1>Header</h2><p>after</p><p>Some inline <img width=\"20px\" height=\"20px\" src=\"Oliver.jpg\"> text.</p>";
NSAttributedString *string = [self attributedStringFromHTML:html];
NSData *dump = [[string string] dataUsingEncoding:NSUTF8StringEncoding];
NSString *resultOnIOS = [self hexStringForData:dump];
NSString *resultOnMac = @"4265666f72650aefbfbc0a4865616465720a61667465720a536f6d6520696e6c696e6520efbfbc20746578742e0a";
XCTAssertTrue([resultOnIOS isEqualToString: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 = [self hexStringForData:dump];
NSString *resultOnMac = @"4e6f7720746865726520697320736f6d6520626f6c64207465787420616e64207370616365732073686f756c64206265206e6f726d616c697a65642e0a";
XCTAssertTrue([resultOnIOS isEqualToString: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 = [self hexStringForData:dump];
NSString *resultOnMac = @"626c6120666f6c6c6f7773204e53537472696e67202a737472203d20402254686520517569636b2042726f776e20466f782042726f776e223b";
XCTAssertTrue([resultOnIOS isEqualToString: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 = [self hexStringForData:dump];
NSString *resultOnMac = @"696d616765206c617374";
XCTAssertTrue([resultOnIOS isEqualToString:resultOnMac], @"Output on Invalid Tag Test differs");
}
- (void)testCrashAtEmptyNodeBeforeDivWithiOS6Attributes
{
// This string is the simplest case that caused the crash.
NSString *html = @"<div><i></i><div></div></div>;";
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{DTUseiOS6Attributes: @(YES)};
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data
options:options
documentAttributes:NULL];
XCTAssert(string != nil);
}
// issue #1199
- (void)testDefaultFont
{
// This string is the simplest case that caused the crash.
NSString *html = @"<p>Hello World!</p>";
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{};
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data
options:options
documentAttributes:NULL];
UIFont *font = [string attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
UIFontDescriptor *descriptor = [font fontDescriptor];
BOOL isBold = (descriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0;
XCTAssertFalse(isBold);
BOOL isItalic = (descriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0;
XCTAssertFalse(isItalic);
XCTAssertTrue([font.familyName isEqualToString:@"Times New Roman"]);
}
// issue #1208
- (void)testDefaultFontBold
{
// This string is the simplest case that caused the crash.
NSString *html = @"<b>Hello World!</b>";
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{};
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data
options:options
documentAttributes:NULL];
UIFont *font = [string attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
UIFontDescriptor *descriptor = [font fontDescriptor];
BOOL isBold = (descriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0;
XCTAssertTrue(isBold);
BOOL isItalic = (descriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0;
XCTAssertFalse(isItalic);
XCTAssertTrue([font.familyName isEqualToString:@"Times New Roman"]);
}
- (void)testDefaultFontItalic
{
// This string is the simplest case that caused the crash.
NSString *html = @"<em>Hello World!</em>";
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{};
NSAttributedString *string = [[NSAttributedString alloc] initWithHTMLData:data
options:options
documentAttributes:NULL];
UIFont *font = [string attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
UIFontDescriptor *descriptor = [font fontDescriptor];
BOOL isBold = (descriptor.symbolicTraits & UIFontDescriptorTraitBold) != 0;
XCTAssertFalse(isBold);
BOOL isItalic = (descriptor.symbolicTraits & UIFontDescriptorTraitItalic) != 0;
XCTAssertTrue(isItalic);
XCTAssertTrue([font.familyName isEqualToString:@"Times New Roman"]);
}
- (NSString *)hexStringForData:(NSData *)data
{
const unsigned char *bytes = (const unsigned char *)data.bytes;
NSMutableString *hex = [NSMutableString new];
for (NSInteger i = 0; i < data.length; i++) {
[hex appendFormat:@"%02x", bytes[i]];
}
return [hex copy];
}
@end