forked from cheeriojs/dom-serializer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
171 lines (136 loc) · 6.31 KB
/
test.js
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
var expect = require('expect.js'),
defaultOpts = require('cheerio').prototype.options,
_ = require('lodash'),
parse = require('cheerio/lib/parse'),
render = require('./index.js');
var html = function(preset, str, options) {
options = _.defaults(options || {}, _.defaults(preset, defaultOpts));
var dom = parse(str, options);
return render(dom, options);
};
var xml = function(str, options) {
options = _.defaults(options || {}, defaultOpts);
options.xmlMode = true;
var dom = parse(str, options);
return render(dom, options);
};
describe('render DOM parsed with htmlparser2', function() {
// only test applicable to the default setup
describe('(html)', function() {
var htmlFunc = _.partial(html, {_useHtmlParser2: true});
// it doesn't really make sense for {decodeEntities: false}
// since currently it will convert <hr class='blah'> into <hr class="blah"> anyway.
it('should handle double quotes within single quoted attributes properly', function() {
var str = '<hr class=\'an "edge" case\' />';
expect(htmlFunc(str)).to.equal('<hr class="an "edge" case">');
});
});
// run html with default options
describe('(html, {})', _.partial( testBody, _.partial(html, {_useHtmlParser2: true}) ));
// run html with turned off decodeEntities
describe('(html, {decodeEntities: false})', _.partial( testBody, _.partial(html, {_useHtmlParser2: true, decodeEntities: false}) ));
describe('(xml)', function() {
it('should render CDATA correctly', function() {
var str = '<a> <b> <![CDATA[ asdf&asdf ]]> <c/> <![CDATA[ asdf&asdf ]]> </b> </a>';
expect(xml(str)).to.equal(str);
});
it('should append ="" to attributes with no value', function() {
var str = '<div dropdown-toggle>';
expect(xml(str)).to.equal('<div dropdown-toggle=""/>');
});
it('should append ="" to boolean attributes with no value', function() {
var str = '<input disabled>';
expect(xml(str)).to.equal('<input disabled=""/>');
});
it('should preserve XML prefixes on attributes', function() {
var str = '<div xmlns:ex="http://example.com/ns"><p ex:ample="attribute">text</p></div>';
expect(xml(str)).to.equal(str);
});
it('should preserve mixed-case XML elements and attributes', function() {
var str = '<svg viewBox="0 0 8 8"><radialGradient/></svg>';
expect(xml(str)).to.equal(str);
});
});
});
function testBody(html) {
it('should render <br /> tags correctly', function() {
var str = '<br />';
expect(html(str)).to.equal('<br>');
});
it('should retain encoded HTML content within attributes', function() {
var str = '<hr class="cheerio & node = happy parsing" />';
expect(html(str)).to.equal('<hr class="cheerio & node = happy parsing">');
});
it('should shorten the "checked" attribute when it contains the value "checked"', function() {
var str = '<input checked/>';
expect(html(str)).to.equal('<input checked>');
});
it('should not shorten the "name" attribute when it contains the value "name"', function() {
var str = '<input name="name"/>';
expect(html(str)).to.equal('<input name="name">');
});
it('should not append ="" to attributes with no value', function() {
var str = '<div dropdown-toggle>';
expect(html(str)).to.equal('<div dropdown-toggle></div>');
});
it('should render comments correctly', function() {
var str = '<!-- comment -->';
expect(html(str)).to.equal('<!-- comment -->');
});
it('should render whitespace by default', function() {
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah</a>';
expect(html(str)).to.equal(str);
});
it('should normalize whitespace if specified', function() {
var str = '<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>';
expect(html(str, { normalizeWhitespace: true })).to.equal('<a href="./haha.html">hi</a> <a href="./blah.html">blah </a>');
});
it('should preserve multiple hyphens in data attributes', function() {
var str = '<div data-foo-bar-baz="value"></div>';
expect(html(str)).to.equal('<div data-foo-bar-baz="value"></div>');
});
it('should not encode characters in script tag', function() {
var str = '<script>alert("hello world")</script>';
expect(html(str)).to.equal(str);
});
it('should not encode json data', function() {
var str = '<script>var json = {"simple_value": "value", "value_with_tokens": ""here & \'there\'""};</script>';
expect(html(str)).to.equal(str);
});
it('should render childless SVG nodes with a closing slash in HTML mode', function() {
var str = '<svg><circle x="12" y="12"/><path d="123M"/><polygon points="60,20 100,40 100,80 60,100 20,80 20,40"/></svg>';
expect(html(str)).to.equal(str);
});
it('should render childless MathML nodes with a closing slash in HTML mode', function() {
var str = '<math><infinity/></math>';
expect(html(str)).to.equal(str);
});
it('should allow SVG elements to have children', function() {
var str = '<svg><circle cx="12" r="12"><title>dot</title></circle></svg>';
expect(html(str)).to.equal(str);
});
it('should not include extra whitespace in SVG self-closed elements', function() {
var str = '<svg><image href="x.png"/> </svg>';
expect(html(str)).to.equal(str);
});
it('should fix-up bad nesting in SVG in HTML mode', function() {
var str = '<svg><g><image href="x.png"></svg>';
expect(html(str)).to.equal('<svg><g><image href="x.png"/></g></svg>');
});
it('should preserve XML prefixed attributes on inline SVG nodes in HTML mode', function() {
var str = '<svg><text id="t" xml:lang="fr">Bonjour</text><use xlink:href="#t"/></svg>';
expect(html(str)).to.equal(str);
});
it('should handle mixed-case SVG content in HTML mode', function() {
var str = '<svg viewBox="0 0 8 8"><radialGradient/></svg>';
expect(html(str)).to.equal(str);
});
it('should render HTML content in SVG foreignObject in HTML mode', function() {
var str = '<svg><foreignObject requiredFeatures=""><img src="test.png" viewbox>text<svg viewBox="0 0 8 8"><circle r="3"/></svg></foreignObject></svg>';
expect(html(str)).to.equal(str);
});
it('should render iframe nodes with a closing tag in HTML mode', function() {
var str = '<iframe src="test"></iframe>';
expect(html(str)).to.equal(str);
});
}