forked from cheeriojs/dom-serializer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
127 lines (100 loc) · 4.42 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
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', function() {
// only test applicable to the default setup
describe('(html)', function() {
var htmlFunc = _.partial(html, {});
// 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, {}) ));
// run html with turned off decodeEntities
describe('(html, {decodeEntities: false})', _.partial( testBody, _.partial(html, {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=""/>');
});
});
});
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 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 iframe nodes with a closing slash in HTML mode', function() {
var str = '<iframe src="test"></iframe>';
expect(html(str)).to.equal(str);
});
}