Skip to content

Commit fc98dfd

Browse files
committed
Merge pull request nfarina#16 from michalkos/master_arc
Added encodedDescription for SMXMLElement
2 parents 19a8290 + a2dae89 commit fc98dfd

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

SMXMLDocument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern NSString *const SMXMLDocumentErrorDomain;
5555
- (SMXMLElement *)descendantWithPath:(NSString *)path;
5656
- (NSString *)valueWithPath:(NSString *)path;
5757
- (NSString *)fullDescription; // like -description, this writes the document out to an XML string, but doesn't truncate the node values.
58+
- (NSString *)encodedDescription; // like -fullDescription, but this does HTML encoding of element content
5859

5960
@end
6061

@@ -68,5 +69,6 @@ extern NSString *const SMXMLDocumentErrorDomain;
6869
+ (SMXMLDocument *)documentWithData:(NSData *)data error:(NSError **)outError;
6970

7071
- (NSString *)fullDescription;
72+
- (NSString *)encodedDescription;
7173

7274
@end

SMXMLDocument.m

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ - (id)initWithDocument:(SMXMLDocument *)document {
2121
return self;
2222
}
2323

24-
- (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)truncated {
24+
- (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)truncated encoded:(BOOL)encode {
2525

2626
NSMutableString *s = [NSMutableString string];
2727
[s appendFormat:@"%@<%@", indent, self.name];
2828

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+
}
3133

32-
NSString *valueOrTrimmed = [self.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
34+
NSString *valueOrTrimmed = [self.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
3335

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];
3641

3742
if (self.children.count) {
3843
[s appendString:@">\n"];
@@ -43,7 +48,7 @@ - (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)tru
4348
[s appendFormat:@"%@%@\n", childIndent, valueOrTrimmed];
4449

4550
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]];
4752

4853
[s appendFormat:@"%@</%@>", indent, self.name];
4954
}
@@ -55,12 +60,32 @@ - (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)tru
5560
return s;
5661
}
5762

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:@"&amp;" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
71+
[encoded replaceOccurrencesOfString:@"\"" withString:@"&quot;" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
72+
[encoded replaceOccurrencesOfString:@"'" withString:@"&#x27;" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
73+
[encoded replaceOccurrencesOfString:@">" withString:@"&gt;" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
74+
[encoded replaceOccurrencesOfString:@"<" withString:@"&lt;" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
75+
76+
return encoded;
77+
}
78+
5879
- (NSString *)description {
59-
return [self descriptionWithIndent:@"" truncatedValues:YES];
80+
return [self descriptionWithIndent:@"" truncatedValues:YES encoded:NO];
6081
}
6182

6283
- (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];
6489
}
6590

6691
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
@@ -187,4 +212,8 @@ - (NSString *)fullDescription {
187212
return self.root.fullDescription;
188213
}
189214

215+
- (NSString *)encodedDescription {
216+
return self.root.encodedDescription;
217+
}
218+
190219
@end

0 commit comments

Comments
 (0)