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 into anyway.
it('should handle double quotes within single quoted attributes properly', function() {
var str = '';
expect(htmlFunc(str)).to.equal('');
});
});
// 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 = '';
expect(xml(str)).to.equal(str);
});
});
});
function testBody(html) {
it('should render tags correctly', function() {
var str = ' ';
expect(html(str)).to.equal(' ');
});
it('should retain encoded HTML content within attributes', function() {
var str = '';
expect(html(str)).to.equal('');
});
it('should shorten the "checked" attribute when it contains the value "checked"', function() {
var str = '';
expect(html(str)).to.equal('');
});
it('should not shorten the "name" attribute when it contains the value "name"', function() {
var str = '';
expect(html(str)).to.equal('');
});
it('should render comments correctly', function() {
var str = '';
expect(html(str)).to.equal('');
});
it('should render whitespace by default', function() {
var str = 'hiblah';
expect(html(str)).to.equal(str);
});
it('should normalize whitespace if specified', function() {
var str = 'hiblah ';
expect(html(str, { normalizeWhitespace: true })).to.equal('hiblah ');
});
it('should preserve multiple hyphens in data attributes', function() {
var str = '';
expect(html(str)).to.equal('');
});
it('should not encode characters in script tag', function() {
var str = '';
expect(html(str)).to.equal(str);
});
it('should not encode json data', function() {
var str = '';
expect(html(str)).to.equal(str);
});
}