forked from Mynigma/HTMLPurifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLPurifier_HTMLModule_Tidy.m
More file actions
253 lines (225 loc) · 7.21 KB
/
HTMLPurifier_HTMLModule_Tidy.m
File metadata and controls
253 lines (225 loc) · 7.21 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
247
248
249
250
251
252
253
//
// HTMLPurifier_Tidy.m
// HTMLPurifier
//
// Created by Roman Priebe on 25.01.14.
#import "HTMLPurifier_HTMLModule_Tidy.h"
#import "HTMLPurifier_Config.h"
#import "BasicPHP.h"
#import "HTMLPurifier_ElementDef.h"
@implementation HTMLPurifier_HTMLModule_Tidy
/**
* Lazy load constructs the module by determining the necessary
* fixes to create and then delegating to the populate() function.
* @param HTMLPurifier_Config $config
* @todo Wildcard matching and error reporting when an added or
* subtracted fix has no effect.
*/
- (id)initWithConfig:(HTMLPurifier_Config*)config
{
self = [super initWithConfig:config];
if (self) {
_fixesForLevel = [@{@"light" : @[], @"medium" : @[], @"heavy" : @[]} mutableCopy];
_defaultLevel = nil;
_levels = @[@"none", @"light", @"medium", @"heavy"];
// create fixes, initialize fixesForLevel
NSMutableDictionary* fixes = [self makeFixes];
[self makeFixesForLevel:fixes];
// figure out which fixes to use
NSString* level = (NSString*)[config get:@"HTML.TidyLevel"];
NSMutableDictionary* fixes_lookup = [self getFixesForLevel:level];
// get custom fix declarations: these need namespace processing
NSMutableDictionary* add_fixes = [(NSDictionary*)[config get:@"HTML.TidyAdd"] mutableCopy];
NSMutableDictionary* remove_fixes = [(NSDictionary*)[config get:@"HTML.TidyRemove"] mutableCopy];
for(NSString* name in fixes)
{
// needs to be refactored a little to implement globbing
if (([remove_fixes isKindOfClass:[NSDictionary class]] && remove_fixes[name]) ||
([add_fixes isKindOfClass:[NSDictionary class]] && [fixes_lookup isKindOfClass:[NSDictionary class]] && !add_fixes[name] && !fixes_lookup[name]))
{
[fixes removeObjectForKey:name];
}
}
// populate this module with necessary fixes
[self populate:fixes];
}
return self;
}
/**
* Retrieves all fixes per a level, returning fixes for that specific
* level as well as all levels below it.
* @param string $level level identifier, see $levels for valid values
* @return array Lookup up table of fixes
*/
- (NSMutableDictionary*)getFixesForLevel:(NSString*)level
{
if ([level isEqual:self.levels[0]])
{
return [NSMutableDictionary new];
}
NSMutableDictionary* activated_levels = [NSMutableDictionary new];
NSInteger c = self.levels.count;
NSInteger i;
for (i = 1; i < c; i++)
{
NSString* key = [NSString stringWithFormat:@"%ld", (unsigned long)activated_levels.count];
if (key)
[activated_levels setObject:self.levels[i] forKey:key];
if ([self.levels[i] isEqual:level])
{
break;
}
}
if (i == c)
{
TRIGGER_ERROR(@"Tidy level %@ not recognised", htmlspecialchars(level));
return [NSMutableDictionary new];;
}
NSMutableDictionary* ret = [NSMutableDictionary new];
for(NSString* level in activated_levels)
{
for(NSString* fix in self.fixesForLevel[level])
{
ret[fix] = @YES;
}
}
return ret;
}
/**
* Dynamically populates the $fixesForLevel member variable using
* the fixes array. It may be custom overloaded, used in conjunction
* with $defaultLevel, or not used at all.
* @param array $fixes
*/
- (void)makeFixesForLevel:(NSDictionary*)fixes
{
if (!self.defaultLevel)
{
return;
}
if (!self.fixesForLevel[self.defaultLevel])
{
TRIGGER_ERROR(@"Defaul level %@ does not exist", self.defaultLevel);
return;
}
if(fixes.allKeys)
self.fixesForLevel[self.defaultLevel] = fixes.allKeys;
}
/**
* Populates the module with transforms and other special-case code
* based on a list of fixes passed to it
* @param array $fixes Lookup table of fixes to activate
*/
- (void)populate:(NSDictionary*)fixes
{
for(NSString* name in fixes)
{
NSObject* fix = fixes[name];
// determine what the fix is for
NSArray* pair = [self getFixType:name];
NSString* type = pair.count>0?pair[0]:nil;
NSMutableDictionary* params = pair.count>1?pair[1]:nil;
NSObject* e = nil;
if([type isEqual:@"attr_transform_pre"] || [type isEqual:@"attr_transform_post"])
{
NSDictionary* attr = params[@"attr"];
if(params[@"element"])
{
NSString* element = params[@"element"];
if (!self.info[element])
{
e = [self addBlankElement:element];
} else {
e = self.info[element];
}
} else {
type = [NSString stringWithFormat:@"info_%@", type];
e = self;
}
NSMutableDictionary* f = [e valueForKey:type];
if(attr)
f[attr] = fix;
}
else if([type isEqual:@"tag_transform"])
{
if(params[@"element"])
self.info_tag_transform[params[@"element"]] = fix;
}
else if([type isEqual:@"child"] || [type isEqual:@"content_model_type"])
{
NSString* element = params[@"element"];
if (!self.info[element])
{
e = [self addBlankElement:element];
} else {
e = self.info[element];
}
[(HTMLPurifier_ElementDef*)e setValue:fix forKey:type];
}
else
{
TRIGGER_ERROR(@"Fix type %@ not supported", type);
}
}
}
/**
* Parses a fix name and determines what kind of fix it is, as well
* as other information defined by the fix
* @param $name String name of fix
* @return array(string $fix_type, array $fix_parameters)
* @note $fix_parameters is type dependant, see populate() for usage
* of these parameters
*/
- (NSArray*)getFixType:(NSString*)name
{
// parse it
NSMutableString* attr = nil;
NSMutableString* property = attr;
if(strpos(name, @"#") != NSNotFound)
{
NSArray* pair = explode(@"#", name);
name = pair.count>0?pair[0]:nil;
property = pair.count>1?pair[1]:nil;
}
if(strpos(name, @"@") != NSNotFound)
{
NSArray* pair = explode(@"@", name);
name = pair.count>0?pair[0]:nil;
attr = pair.count>1?pair[1]:nil;
}
// figure out the parameters
NSMutableDictionary* params = [NSMutableDictionary new];
if(name && ![name isEqual:@""])
{
params[@"element"] = name;
}
if(attr)
{
params[@"attr"] = attr;
}
// special case: attribute transform
if(attr) {
if (!property)
{
property = [@"pre" mutableCopy];
}
NSMutableString* type = [NSMutableString stringWithFormat:@"attr_transform_%@", property];
return @[type, params];
}
// special case: tag transform
if (!property)
{
return @[@"tag_transform", params];
}
return @[property, params];
}
/**
* Defines all fixes the module will perform in a compact
* associative array of fix name to fix implementation.
* @return array
*/
- (NSMutableDictionary*)makeFixes
{
return nil;
}
@end