forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSMutableAttributedStringHTMLTest.m
More file actions
183 lines (123 loc) · 6.67 KB
/
Copy pathNSMutableAttributedStringHTMLTest.m
File metadata and controls
183 lines (123 loc) · 6.67 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
//
// NSMutableAttributedStringHTMLTest.m
// DTCoreText
//
// Created by Oliver Drobnik on 6/25/13.
// Copyright (c) 2013 Drobnik.com. All rights reserved.
//
#import "NSMutableAttributedStringHTMLTest.h"
#import "NSMutableAttributedString+HTML.h"
#import "NSAttributedString+HTML.h"
@implementation NSMutableAttributedStringHTMLTest
- (void)testAddCustomHTMLAttribute
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSRange entireString = NSMakeRange(0, [attributedString length]);
// have a range with an attribute
[attributedString addHTMLAttribute:@"class" value:@"oli" range:NSMakeRange(3, 2) replaceExisting:YES];
NSRange effectiveRange;
NSDictionary *dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:3 effectiveRange:&effectiveRange];
NSRange expectedRange=NSMakeRange(3, 2);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
NSString *value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"oli", @"Attribute should be oli");
// add the same name attribute without replacing for the entire string
[attributedString addHTMLAttribute:@"class" value:@"drops" range:entireString replaceExisting:NO];
// part in the middle should still be oli
dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:3 effectiveRange:&effectiveRange];
expectedRange=NSMakeRange(3, 2);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"oli", @"Attribute should be oli");
// part at the start should be drops
dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:0 effectiveRange:&effectiveRange];
expectedRange=NSMakeRange(0, 3);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"drops", @"Attribute should be drops");
// part at the end should be drops
dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:5 effectiveRange:&effectiveRange];
expectedRange=NSMakeRange(5, 5);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"drops", @"Attribute should be drops");
// replace everything
[attributedString addHTMLAttribute:@"class" value:@"foo" range:entireString replaceExisting:YES];
effectiveRange = [attributedString rangeOfHTMLAttribute:@"class" atIndex:0];
expectedRange=NSMakeRange(0, 10);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
dict = [attributedString HTMLAttributesAtIndex:4];
value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"foo", @"Attribute should be foo");
}
- (void)testRemoveCustomHTMLAttribute
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSRange entireString = NSMakeRange(0, [attributedString length]);
// have a range with an attribute
[attributedString addHTMLAttribute:@"class" value:@"oli" range:entireString replaceExisting:YES];
// remove a part at the end
[attributedString removeHTMLAttribute:@"class" range:NSMakeRange(5, 10)];
// tail should now be nil
NSRange effectiveRange;
NSDictionary *dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:5 effectiveRange:&effectiveRange];
NSRange expectedRange=NSMakeRange(5, 5);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
STAssertNil(dict, @"There should be no dictionary in this range");
// head should still be oli
dict = [attributedString attribute:DTCustomAttributesAttribute atIndex:0 effectiveRange:&effectiveRange];
expectedRange=NSMakeRange(0, 5);
STAssertEquals(expectedRange, effectiveRange, @"Effective Range does not match");
id value = [dict objectForKey:@"class"];
STAssertEqualObjects(value, @"oli", @"Attribute should be oli");
}
- (void)testRemoveMultipleCustomHTMLAttribute
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSRange entireString = NSMakeRange(0, [attributedString length]);
// have a range with an attribute
[attributedString addHTMLAttribute:@"class" value:@"oli" range:entireString replaceExisting:YES];
[attributedString addHTMLAttribute:@"foo" value:@"bar" range:entireString replaceExisting:YES];
// there should be two
NSDictionary *attributes = [attributedString HTMLAttributesAtIndex:2];
NSUInteger count = [attributes count];
NSUInteger expectedCount = 2;
STAssertEquals(count, expectedCount, @"There should be 2 custom attributes");
// now remove one
[attributedString removeHTMLAttribute:@"foo" range:entireString];
// there should be one
attributes = [attributedString HTMLAttributesAtIndex:2];
count = [attributes count];
expectedCount = 1;
STAssertEquals(count, expectedCount, @"There should be only 1 custom attribute");
}
- (void)testRangeOfCustomHTMLAttribute
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"1234567890"];
NSRange entireString = NSMakeRange(0, [attributedString length]);
// have a range with an attribute
[attributedString addHTMLAttribute:@"class" value:@"oli" range:NSMakeRange(3, 2) replaceExisting:YES];
// add longer range
[attributedString addHTMLAttribute:@"class" value:@"bar" range:entireString replaceExisting:NO];
// add a second one on top of it all
[attributedString addHTMLAttribute:@"foo" value:@"bar" range:entireString replaceExisting:YES];
// class element in middle is only valid for that range
NSRange expectedRange = NSMakeRange(3, 2);
NSRange queriedRange = [attributedString rangeOfHTMLAttribute:@"class" atIndex:3];
STAssertEquals(expectedRange, queriedRange, @"Range is incorrect");
// global foo is entire range
queriedRange = [attributedString rangeOfHTMLAttribute:@"foo" atIndex:3];
STAssertEquals(queriedRange, entireString, @"Range is incorrect");
// global foo is entire range no matter where you query it
queriedRange = [attributedString rangeOfHTMLAttribute:@"foo" atIndex:9];
STAssertEquals(queriedRange, entireString, @"Range is incorrect");
// the right part of class (with 'bar')
queriedRange = [attributedString rangeOfHTMLAttribute:@"class" atIndex:5];
expectedRange = NSMakeRange(5, 5);
STAssertEquals(queriedRange, expectedRange, @"Range is incorrect");
// the left part of class (with 'bar')
queriedRange = [attributedString rangeOfHTMLAttribute:@"class" atIndex:1];
expectedRange = NSMakeRange(0, 3);
STAssertEquals(queriedRange, expectedRange, @"Range is incorrect");
}
@end