/
HTMLPurifier_AttrDef_HTML_Pixels.m
103 lines (83 loc) · 2 KB
/
HTMLPurifier_AttrDef_HTML_Pixels.m
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
//
// HTMLPurifier_AttrDef_HTML_Pixels.m
// HTMLPurifier
//
// Created by Lukas Neumann on 11.01.14.
/**
* Validates an integer representation of pixels according to the HTML spec.
*/
#import "HTMLPurifier_AttrDef_HTML_Pixels.h"
#import "BasicPHP.h"
@implementation HTMLPurifier_AttrDef_HTML_Pixels
@synthesize max;
/**
* @param int $max
*/
-(id) initWithMax:(NSNumber*)newMax
{
self = [super init];
max = newMax;
return self;
}
- (id)init
{
return [self initWithMax:nil];
}
/**
* @param string $string
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string
*/
- (NSString*)validateWithString:(NSString*)string config:(HTMLPurifier_Config *)config context:(HTMLPurifier_Context *)context
{
string = trim(string);
if ([string isEqual:@"0"])
{
return string;
}
if ([string isEqual:@""])
{
return nil;
}
NSUInteger length = string.length;
if ((length >= 2) && [substr(string,length - 2) isEqual:@"px"])
{
string = [string substringToIndex:(length - 2)];
}
if (!stringIsNumeric(string))
{
return nil;
}
int num = [string intValue];
if (num < 0)
{
return @"0";
}
// upper-bound value, extremely high values can
// crash operating systems, see <http://ha.ckers.org/imagecrash.html>
// WARNING, above link WILL crash you if you're using Windows
NSNumber* numToNum = @(num);
if (max && (num > max.integerValue))
{
return [NSString stringWithFormat:@"%@",max];
}
return [NSString stringWithFormat:@"%@",numToNum];
}
/**
* @param string $string
* @return HTMLPurifier_AttrDef
*/
- (HTMLPurifier_AttrDef*)make:(NSString*)string
{
if ([string isEqual:@""])
{
max = nil;
} else {
max = @([string intValue]);
}
Class class = [self class];
HTMLPurifier_AttrDef_HTML_Pixels* newAttrDef = [class alloc];
return [newAttrDef initWithMax:max];
}
@end