forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLSerializerTests.m
More file actions
80 lines (65 loc) · 3.05 KB
/
Copy pathHTMLSerializerTests.m
File metadata and controls
80 lines (65 loc) · 3.05 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
// HTMLSerializerTests.m
//
// Public domain. https://github.com/nolanw/HTMLReader
// TODO Use html5lib's serializer tests directly.
#import <XCTest/XCTest.h>
#import "HTMLReader.h"
#import "HTMLTextNode.h"
@interface HTMLSerializerTests : XCTestCase
@end
@implementation HTMLSerializerTests
- (void)testBareElement
{
HTMLElement *node = [[HTMLElement alloc] initWithTagName:@"br" attributes:nil];
XCTAssertEqualObjects(node.serializedFragment, @"<br>");
}
// From html5lib's serializers/core.test
- (void)testAttributes
{
#define TestAttribute(input, expected) do { \
HTMLElement *node = [[HTMLElement alloc] initWithTagName:@"span" attributes:@{ @"title": (input) }]; \
XCTAssertEqualObjects(node.serializedFragment, (expected)); \
} while (0)
TestAttribute(@"test \"with\" "", @"<span title=\"test "with" &quot;\"></span>");
TestAttribute(@"foo", @"<span title=\"foo\"></span>");
TestAttribute(@"foo<bar", @"<span title=\"foo<bar\"></span>");
TestAttribute(@"foo=bar", @"<span title=\"foo=bar\"></span>");
TestAttribute(@"foo>bar", @"<span title=\"foo>bar\"></span>");
TestAttribute(@"foo\"bar", @"<span title=\"foo"bar\"></span>");
TestAttribute(@"foo'bar", @"<span title=\"foo'bar\"></span>");
TestAttribute(@"foo'bar\"baz", @"<span title=\"foo'bar"baz\"></span>");
TestAttribute(@"foo bar", @"<span title=\"foo bar\"></span>");
TestAttribute(@"foo\tbar", @"<span title=\"foo\tbar\"></span>");
TestAttribute(@"foo\nbar", @"<span title=\"foo\nbar\"></span>");
TestAttribute(@"foo\rbar", @"<span title=\"foo\rbar\"></span>");
TestAttribute(@"foo\fbar", @"<span title=\"foo\fbar\"></span>");
}
- (void)testDoctype
{
#define TestDoctype(name, public, system, expected) do { \
HTMLDocumentType *node = [[HTMLDocumentType alloc] initWithName:(name) publicIdentifier:(public) systemIdentifier:(system)]; \
XCTAssertEqualObjects(node.serializedFragment, (expected)); \
} while (0)
TestDoctype(@"HTML", nil, nil, @"<!DOCTYPE HTML>");
TestDoctype(@"HTML", @"-//W3C//DTD HTML 4.01//EN", @"http://www.w3.org/TR/html4/strict.dtd", @"<!DOCTYPE HTML>");
TestDoctype(@"HTML", @"-//W3C//DTD HTML 4.01//EN", nil, @"<!DOCTYPE HTML>");
TestDoctype(@"html", nil, @"http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd", @"<!DOCTYPE html>");
}
- (void)testText
{
HTMLTextNode *node = [[HTMLTextNode alloc] initWithData:@"a<b>c&d"];
XCTAssertEqualObjects(node.serializedFragment, @"a<b>c&d");
}
- (void)testRCDATA
{
HTMLElement *node = [[HTMLElement alloc] initWithTagName:@"script" attributes:nil];
HTMLTextNode *textNode = [[HTMLTextNode alloc] initWithData:@"a<b>c&d"];
[[node mutableChildren] addObject:textNode];
XCTAssertEqualObjects(node.serializedFragment, @"<script>a<b>c&d</script>");
}
- (void)testDescriptionForNonStringAttributes
{
HTMLElement *node = [[HTMLElement alloc] initWithTagName:@"p" attributes:@{@"num": @1}];
XCTAssertEqualObjects(node.serializedFragment, @"<p num=\"1\"></p>");
}
@end