forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLSerialization.m
207 lines (166 loc) · 6.15 KB
/
HTMLSerialization.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
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
// HTMLSerialization.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import "HTMLSerialization.h"
#import "HTMLComment.h"
#import "HTMLDocument.h"
#import "HTMLDocumentType.h"
#import "HTMLElement.h"
#import "HTMLString.h"
#import "HTMLTextNode.h"
NS_ASSUME_NONNULL_BEGIN
@implementation HTMLNode (Serialization)
- (NSString *)recursiveDescription
{
NSMutableString *string = [NSMutableString new];
RecursiveDescriptionHelper(self, string, 0);
return string;
}
static void RecursiveDescriptionHelper(HTMLNode *self, NSMutableString *string, NSInteger indentLevel)
{
if (indentLevel > 0) {
[string appendString:[@"\n|" stringByPaddingToLength:indentLevel * 4 + 2
withString:@" "
startingAtIndex:0]];
}
[string appendString:self.description];
for (HTMLNode *node in self.children) {
RecursiveDescriptionHelper(node, string, indentLevel + 1);
}
}
- (NSString *)innerHTML
{
NSArray *fragments = [self.children.array valueForKey:@"serializedFragment"];
return [fragments componentsJoinedByString:@""];
}
- (NSString *)serializedFragment
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
@end
@implementation HTMLComment (Serialization)
- (NSString *)description
{
NSString *truncatedData = self.data;
if (truncatedData.length > 37) {
truncatedData = [[truncatedData substringToIndex:37] stringByAppendingString:@"…"];
}
return [NSString stringWithFormat:@"<%@: %p <!-- %@ --> >", self.class, self, truncatedData];
}
- (NSString *)serializedFragment
{
return [NSString stringWithFormat:@"<!--%@-->", self.data];
}
@end
@implementation HTMLDocument (Serialization)
- (NSString *)serializedFragment
{
return self.innerHTML;
}
@end
@implementation HTMLDocumentType (Serialization)
- (NSString *)description
{
NSMutableString *description = [NSMutableString new];
[description appendFormat:@"<%@: %p <!DOCTYPE", self.class, self];
NSString *name = self.name;
if (name.length > 0) {
[description appendFormat:@" %@", name];
}
NSString *publicIdentifier = self.publicIdentifier;
NSString *systemIdentifier = self.systemIdentifier;
if (publicIdentifier.length > 0 || systemIdentifier.length > 0) {
[description appendFormat:@" \"%@\" \"%@\"", publicIdentifier, systemIdentifier];
}
[description appendString:@"> >"];
return description;
}
- (NSString *)serializedFragment
{
return [NSString stringWithFormat:@"<!DOCTYPE %@>", self.name];
}
@end
@implementation HTMLElement (Serialization)
- (NSString *)description
{
NSMutableString *description = [NSMutableString new];
[description appendFormat:@"<%@: %p <", self.class, self];
if (self.htmlNamespace == HTMLNamespaceMathML) {
[description appendString:@"math "];
} else if (self.htmlNamespace == HTMLNamespaceSVG) {
[description appendString:@"svg "];
}
[description appendString:self.tagName];
[self.attributes enumerateKeysAndObjectsUsingBlock:^(id name, id value, BOOL *stop) {
[description appendFormat:@" %@=\"%@\"", name, value];
}];
[description appendFormat:@"> %@ child", @(self.numberOfChildren)];
if (self.numberOfChildren != 1) {
[description appendString:@"ren"];
}
[description appendString:@">"];
return description;
}
- (NSString *)serializedFragment
{
NSMutableString *fragment = [NSMutableString new];
[fragment appendFormat:@"<%@", self.tagName];
[self.attributes enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
if ([name isEqualToString:@"xmlns:xmlns"]) {
name = @"xmlns";
}
if (![value isKindOfClass:[NSString class]]) {
value = value.description;
}
NSMutableString *escapedValue = [value mutableCopy];
void (^replace)(id, id) = ^(NSString *search, NSString *replace) {
NSRange range = NSMakeRange(0, escapedValue.length);
[escapedValue replaceOccurrencesOfString:search withString:replace options:0 range:range];
};
replace(@"&", @"&");
replace(@"\u00A0", @" ");
replace(@"\"", @""");
[fragment appendFormat:@" %@=\"%@\"", name, escapedValue];
}];
[fragment appendString:@">"];
if (StringIsEqualToAnyOf(self.tagName, @"area", @"base", @"basefont", @"bgsound", @"br", @"col", @"embed", @"frame", @"hr", @"img", @"input", @"keygen", @"link", @"menuitem", @"meta", @"param", @"source", @"track", @"wbr")) {
return fragment;
}
if (StringIsEqualToAnyOf(self.tagName, @"pre", @"textarea", @"listing")) {
if ([self.children.firstObject isKindOfClass:[HTMLTextNode class]]) {
HTMLTextNode *textNode = (HTMLTextNode *)self.children.firstObject;
if ([textNode.data hasPrefix:@"\n"]) {
[fragment appendString:@"\n"];
}
}
}
[fragment appendString:self.innerHTML];
[fragment appendFormat:@"</%@>", self.tagName];
return fragment;
}
@end
@implementation HTMLTextNode (Serialization)
- (NSString *)description
{
NSString *truncatedData = self.data;
if (truncatedData.length > 37) {
truncatedData = [[truncatedData substringToIndex:37] stringByAppendingString:@"…"];
}
return [NSString stringWithFormat:@"<%@: %p '%@'>", self.class, self, truncatedData];
}
- (NSString *)serializedFragment
{
NSString *parentTagName = self.parentElement.tagName;
if (StringIsEqualToAnyOf(parentTagName, @"style", @"script", @"xmp", @"iframe", @"noembed", @"noframes", @"plaintext", @"noscript")) {
return self.data;
} else {
NSString *escaped = [self.data stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
escaped = [escaped stringByReplacingOccurrencesOfString:@"\u00A0" withString:@" "];
escaped = [escaped stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
escaped = [escaped stringByReplacingOccurrencesOfString:@">" withString:@">"];
return escaped;
}
}
@end
NS_ASSUME_NONNULL_END