var co = require('co'); var datauri = require('datauri'); var expect = require('chai') .use(require('chai-as-promised')) .expect; var fs = require('fs'); var inline = require('../lib'); var path = require('path'); describe('inline-html', () => { describe('file()', () => { it('exists', () => expect(inline).to.have.property('file').that.is.a('function')); it('accept html filename', () => { var filename = path.resolve(__dirname, './fixtures/file.txt'); var content = fs.readFileSync(filename, 'utf8'); return expect(inline.file(filename)).to.eventually.equal(content); }); it('options.filename: always reset to current filename and used for css url path resolution', () => { var filename = path.resolve(__dirname, 'fixtures/css-url.html'); // file contains url path relative to itself var options = { filename: __filename }; // sets filename relative to this test file return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/); }); it('options.filename: always reset to current filename and used for img src path resolution', () => { var filename = path.resolve(__dirname, 'fixtures/img.html'); // file contains src relative to itself var options = { filename: __filename }; // sets filename relative to this test file return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/); }); it('throw error when file not found', () => expect(inline.file('missing.html')).to.eventually.be.rejected); }); describe('html()', () => { it('exists', () => expect(inline).to.have.property('html').that.is.a('function')); it('accept html string', () => { var html = 'HTML'; return expect(inline.html(html)).to.eventually.equal(html); }); it('preserve self closing tags', () => { var html = '
'; return expect(inline.html(html)).to.eventually.equal(html); }); it('preserve partials', () => { var html = '{{> partial}}'; return expect(inline.html(html)).to.eventually.equal(html); }); it('preserve helpers', () => { var html = '{{helper}}'; return expect(inline.html(html)).to.eventually.equal(html); }); it('options.filename: default to cwd for css url path resolution', () => { var url = 'test/fixtures/file.txt'; // Note: this is relative to cwd var uri = datauri(url); var html = (path) => ``; return expect(inline.html(html(url))).to.eventually.equal(html(uri)); }); it('options.filename: set basepath for css url path resolution', () => { var filename = path.resolve(__dirname, 'fixtures/fake.html'); var dirname = path.dirname(filename); var url = 'file.txt'; // Note: path relative to filename's dirname var uri = datauri(path.resolve(dirname, url)); var html = (path) => ``; var options = { filename: filename }; return expect(inline.html(html(url), options)).to.eventually.equal(html(uri)); }); it('options.filename: default to cwd for img src path resolution', () => { var url = 'test/fixtures/file.txt'; // Note: path relative to cwd var uri = datauri(url); var html = (path) => ``; return expect(inline.html(html(url))).to.eventually.equal(html(uri)); }); it('options.filename: set basepath for img src path resolution', () => { var filename = path.resolve(__dirname, 'fixtures/fake.html'); var dirname = path.dirname(filename); var url = 'file.txt'; // Note: path relative to filename's dirname var uri = datauri(path.resolve(dirname, url)); var html = (path) => ``; var options = { filename: filename }; return expect(inline.html(html(url), options)).to.eventually.equal(html(uri)); }); it('options.filename: included in results.files for img src if options.verbose true', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; var options = { verbose: true }; return expect(inline.html(html, options)).to.eventually.have.property('files') .that.is.an('array') .that.contains(filename); }); it('options.filename: included in results.files for css url path if options.verbose true', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; var options = { verbose: true }; return expect(inline.html(html, options)).to.eventually.have.property('files') .that.is.an('array') .that.contains(filename); }); it('options.verbose: return results object if true', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; var options = { verbose: true }; return expect(inline.html(html, options)).to.eventually.be.an('object') .that.contains.keys(['html', 'files']); }); it('options.verbose: return html if false', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; var options = { verbose: false }; return expect(inline.html(html, options)).to.eventually.be.a('string'); }); it('options.verbose: default false', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; return expect(inline.html(html)).to.eventually.be.a('string'); }); describe('css-url', () => { it('inline local url in style element', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); }); it('inline local url in style attribute', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = `
`; return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); }); it('ignore remote url', () => { var html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('ignore template expression url', () => { var html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('ignore url query strings and hashes', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var url = `${filename}?query=string#hash`; var uri = datauri(filename); var html = (source) => ``; return expect(inline.html(html(url))).to.eventually.equal(html(uri)); }); it('handle url with spaces', () => { var filename = path.resolve(__dirname, 'fixtures/file space.txt'); var uri = datauri(filename); var html = (source) => ``; return expect(inline.html(html(filename))).to.eventually.equal(html(uri)); }); it('throw when syntax invalid in style element', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var html = ``; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(filename); } })); it('throw when syntax invalid in style attribute', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(filename); } })); it('throw when url invalid in style element', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var url = 'missing.png'; var resolvedUrl = path.resolve(path.dirname(filename), url); var html = ``; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedUrl); } })); it('throw when url invalid in style attribute ', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var url = 'missing.png'; var resolvedUrl = path.resolve(path.dirname(filename), url); var html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedUrl); } })); it('include all urls in error.files up until and including invalid url in style element', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var validUrl = 'fixtures/file.txt'; var invalidUrl = 'missing.png'; var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); var html = ` `; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedValidUrl); expect(error).to.have.property('files').that.contains(resolvedInvalidUrl); } })); it('include all urls in error.files up until and including invalid url in style attribute', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var validUrl = 'fixtures/file.txt'; var invalidUrl = 'missing.png'; var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); var html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedValidUrl); expect(error).to.have.property('files').that.contains(resolvedInvalidUrl); } })); it('include all urls in error.files up until and including invalid url when style element valid and style attribute invalid', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var validUrl = 'fixtures/file.txt'; var invalidUrl = 'missing.png'; var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); var html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedValidUrl); expect(error).to.have.property('files').that.contains(resolvedInvalidUrl); } })); }); describe('img', () => { it('inline img src', () => { var filename = path.resolve(__dirname, 'fixtures/file.txt'); var html = ``; return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); }); it('ignore img src remote paths', () => { var html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('ignore img src template expression paths', () => { var html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('throw when src invalid', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var source = 'missing.png'; var html = ``; var resolvedSource = path.resolve(path.dirname(filename), source); try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedSource); } })); it('include all sources in error.files up until and including invalid source', () => co(function * () { var filename = path.resolve(__dirname, 'index.html'); var valid = 'fixtures/file.txt'; var invalid = 'missing.png'; var resolvedInvalid = path.resolve(path.dirname(filename), invalid); var resolvedValid = path.resolve(path.dirname(filename), valid); var html = ` `; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); } catch (error) { expect(error).to.have.property('filename').that.equals(filename); expect(error).to.have.property('files').that.contains(resolvedValid); expect(error).to.have.property('files').that.contains(resolvedInvalid); } })); }); describe('link-less', () => { it('inline link less', () => { var filename = path.resolve(__dirname, 'fixtures/basic.less'); var html = ``; return expect(inline.html(html)).to.eventually.match(/