@@ -21,18 +21,23 @@ - (id)initWithDocument:(SMXMLDocument *)document {
21
21
return self;
22
22
}
23
23
24
- - (NSString *)descriptionWithIndent : (NSString *)indent truncatedValues : (BOOL )truncated {
24
+ - (NSString *)descriptionWithIndent : (NSString *)indent truncatedValues : (BOOL )truncated encoded : ( BOOL ) encode {
25
25
26
26
NSMutableString *s = [NSMutableString string ];
27
27
[s appendFormat: @" %@ <%@ " , indent, self .name];
28
28
29
- for (NSString *attribute in self.attributes )
30
- [s appendFormat: @" %@ =\" %@ \" " , attribute, [self .attributes objectForKey: attribute]];
29
+ for (NSString *attribute in self.attributes ) {
30
+ NSString *attributeValue = [self .attributes objectForKey: attribute];
31
+ [s appendFormat: @" %@ =\" %@ \" " , attribute, encode ? [self encodeString: attributeValue] : attributeValue];
32
+ }
31
33
32
- NSString *valueOrTrimmed = [self .value stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
34
+ NSString *valueOrTrimmed = [self .value stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
33
35
34
- if (truncated && valueOrTrimmed.length > 25 )
35
- valueOrTrimmed = [NSString stringWithFormat: @" %@ …" , [valueOrTrimmed substringToIndex: 25 ]];
36
+ if (truncated && valueOrTrimmed.length > 25 )
37
+ valueOrTrimmed = [NSString stringWithFormat: @" %@ …" , [valueOrTrimmed substringToIndex: 25 ]];
38
+
39
+ if (encode)
40
+ valueOrTrimmed = [self encodeString: valueOrTrimmed];
36
41
37
42
if (self.children .count ) {
38
43
[s appendString: @" >\n " ];
@@ -43,7 +48,7 @@ - (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)tru
43
48
[s appendFormat: @" %@%@ \n " , childIndent, valueOrTrimmed];
44
49
45
50
for (SMXMLElement *child in self.children )
46
- [s appendFormat: @" %@ \n " , [child descriptionWithIndent: childIndent truncatedValues: truncated]];
51
+ [s appendFormat: @" %@ \n " , [child descriptionWithIndent: childIndent truncatedValues: truncated encoded: encode ]];
47
52
48
53
[s appendFormat: @" %@ </%@ >" , indent, self .name];
49
54
}
@@ -55,12 +60,32 @@ - (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)tru
55
60
return s;
56
61
}
57
62
63
+ - (NSString *)encodeString : (NSString *)string
64
+ {
65
+ if (!string.length )
66
+ return string;
67
+
68
+ NSMutableString *encoded = [NSMutableString stringWithString: string];
69
+
70
+ [encoded replaceOccurrencesOfString: @" &" withString: @" &" options: NSLiteralSearch range: NSMakeRange (0 , [encoded length ])];
71
+ [encoded replaceOccurrencesOfString: @" \" " withString: @" "" options: NSLiteralSearch range: NSMakeRange (0 , [encoded length ])];
72
+ [encoded replaceOccurrencesOfString: @" '" withString: @" '" options: NSLiteralSearch range: NSMakeRange (0 , [encoded length ])];
73
+ [encoded replaceOccurrencesOfString: @" >" withString: @" >" options: NSLiteralSearch range: NSMakeRange (0 , [encoded length ])];
74
+ [encoded replaceOccurrencesOfString: @" <" withString: @" <" options: NSLiteralSearch range: NSMakeRange (0 , [encoded length ])];
75
+
76
+ return encoded;
77
+ }
78
+
58
79
- (NSString *)description {
59
- return [self descriptionWithIndent: @" " truncatedValues: YES ];
80
+ return [self descriptionWithIndent: @" " truncatedValues: YES encoded: NO ];
60
81
}
61
82
62
83
- (NSString *)fullDescription {
63
- return [self descriptionWithIndent: @" " truncatedValues: NO ];
84
+ return [self descriptionWithIndent: @" " truncatedValues: NO encoded: NO ];
85
+ }
86
+
87
+ - (NSString *)encodedDescription {
88
+ return [self descriptionWithIndent: @" " truncatedValues: NO encoded: YES ];
64
89
}
65
90
66
91
- (void )parser : (NSXMLParser *)parser foundCharacters : (NSString *)string {
@@ -187,4 +212,8 @@ - (NSString *)fullDescription {
187
212
return self.root .fullDescription ;
188
213
}
189
214
215
+ - (NSString *)encodedDescription {
216
+ return self.root .encodedDescription ;
217
+ }
218
+
190
219
@end
0 commit comments