forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSAttributedString+HTML.m
More file actions
123 lines (92 loc) · 3.35 KB
/
Copy pathNSAttributedString+HTML.m
File metadata and controls
123 lines (92 loc) · 3.35 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
//
// NSAttributedString+HTML.m
// DTCoreText
//
// Created by Oliver Drobnik on 1/9/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#if TARGET_OS_IPHONE
#elif TARGET_OS_MAC
#import <ApplicationServices/ApplicationServices.h>
#endif
#import "DTHTMLElement.h"
#import "DTCoreTextConstants.h"
#import "DTHTMLAttributedStringBuilder.h"
@implementation NSAttributedString (HTML)
- (id)initWithHTMLData:(NSData *)data documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
return [self initWithHTMLData:data options:nil documentAttributes:docAttributes];
}
- (id)initWithHTMLData:(NSData *)data baseURL:(NSURL *)base documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
NSDictionary *optionsDict = nil;
if (base)
{
optionsDict = [NSDictionary dictionaryWithObject:base forKey:NSBaseURLDocumentOption];
}
return [self initWithHTMLData:data options:optionsDict documentAttributes:docAttributes];
}
- (id)initWithHTMLData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary * __autoreleasing*)docAttributes
{
// only with valid data
if (![data length])
{
return nil;
}
DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:data options:options documentAttributes:docAttributes];
void (^callBackBlock)(DTHTMLElement *element) = [options objectForKey:DTWillFlushBlockCallBack];
if (callBackBlock)
{
[stringBuilder setWillFlushCallback:callBackBlock];
}
// This needs to be on a seprate line so that ARC can handle releasing the object properly
// return [stringBuilder generatedAttributedString]; shows leak in instruments
id string = [stringBuilder generatedAttributedString];
return string;
}
#pragma mark - Working with Custom HTML Attributes
- (NSDictionary *)HTMLAttributesAtIndex:(NSUInteger)index
{
return [self attribute:DTCustomAttributesAttribute atIndex:index effectiveRange:NULL];
}
- (NSRange)rangeOfHTMLAttribute:(NSString *)name atIndex:(NSUInteger)index
{
NSRange rangeSoFar;
NSDictionary *attributes = [self attribute:DTCustomAttributesAttribute atIndex:index effectiveRange:&rangeSoFar];
NSAssert(attributes, @"No custom attribute '%@' at index %d", name, (int)index);
// check if there is a value for this custom attribute name
id value = [attributes objectForKey:name];
if (!attributes || !value)
{
return NSMakeRange(NSNotFound, 0);
}
// search towards beginning
while (rangeSoFar.location>0)
{
NSRange extendedRange;
attributes = [self attribute:DTCustomAttributesAttribute atIndex:rangeSoFar.location-1 effectiveRange:&extendedRange];
id extendedValue = [attributes objectForKey:name];
// abort search if key not found or value not identical
if (!extendedValue || ![extendedValue isEqual:value])
{
break;
}
rangeSoFar = NSUnionRange(rangeSoFar, extendedRange);
}
NSUInteger length = [self length];
// search towards end
while (NSMaxRange(rangeSoFar)<length)
{
NSRange extendedRange;
attributes = [self attribute:DTCustomAttributesAttribute atIndex:NSMaxRange(rangeSoFar) effectiveRange:&extendedRange];
id extendedValue = [attributes objectForKey:name];
// abort search if key not found or value not identical
if (!extendedValue || ![extendedValue isEqual:value])
{
break;
}
rangeSoFar = NSUnionRange(rangeSoFar, extendedRange);
}
return rangeSoFar;
}
@end