diff --git a/README.md b/README.md index 665cae1..90c48a2 100644 --- a/README.md +++ b/README.md @@ -8,39 +8,24 @@ Inline local assets referenced in an HTML document. [![David](https://img.shields.io/david/panosoft/inline-html.svg)](https://david-dm.org/panosoft/inline-html) [![npm downloads](https://img.shields.io/npm/dm/inline-html.svg)](https://www.npmjs.com/package/inline-html) -## Installation +This library parses HTML, embeds the contents of local assets that are referenced within that HTML, and returns a new inlined HTML string. -```sh -npm install inline-html -``` +The following HTML elements and CSS data types are inlined: + +- Images - The source path is replaced with a datauri. + +- Linked LESS stylesheets - The LESS is compiled and the result is inlined within a ` -
- - ``` +Assuming ... - `main.less` ```css - @import (inline) 'main.css'; + @import (less) 'main.css'; div { background-image: url('path/to/file'); } ``` @@ -50,57 +35,110 @@ Assuming we have the following files: @font-face { src: url('path/to/file'); } ``` -We can use `inline-html`: +Then ... ```js -var inlineHtml = require('inline-html'); +var co = require('co'); +var inline = require('inline-html'); -inlineHtml('index.html').then(function (html) { - // ... +co(function * () { + var html = ` + + +
+ + `; + html = yield inline.html(html); + console.log(html); + /** + + +
+ + */ }); ``` -To create the following `html` string: +## Installation -```html - - -
- +```sh +npm install inline-html ``` ## API -- [`inlineHtml`](#inlineHtml) +- [`inline.html`](#html) +- [`inline.file`](#file) +- [`Results`](#results) --- - -### inlineHtml ( html [, options] ) + +### inline.html ( html [, options] ) -Reads an HTML file and embeds referenced local assets into the HTML. +Parses an HTML string and embeds referenced local assets into the HTML. -Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results`](#Results) depending on the value of `options.verbose`. +Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results`](#results) depending on the value of `options.verbose`. __Arguments__ -- `html` - An HTML string or a filename of an HTML file to inline. Relative file paths are resolved relative to `options.filename` if a string or the filename's directory if a filename. +- `html` - An HTML string to inline. + + - `options` - - `filename` - The filename used to resolve relative paths when `html` is a string. If `html` is a string and this option is not provided, relative paths will be resolved relative to the process's current working directory. + - `filename` - The filename used to resolve relative paths. If this option is not provided, relative paths will be resolved relative to the process's current working directory. - `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}`. - `verbose` - A boolean that determines the promises fulfillment value. Supported values are: - - `true`: An instance of [`Results`](#Results). - - `false`: An `html` string. (_default_) + - `true`: An instance of [`Results`](#results). + - `false`: An `html` string. (_Default_) - -__Results__ +__Example__ -The `Promise` returned by this function is optionally fulfilled with a `results` object that has the following properties: +```js +co(function * () { + var html = yield inline.html(``); + console.log(html); // +}); +``` + +--- + + +### inline.file ( filename [, options] ) + +Reads an HTML file and embeds referenced local assets into the HTML. + +Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results`](#results) depending on the value of `options.verbose`. + +__Arguments__ + +- `html` - A filename of an HTML file to inline. Relative file paths are resolved relative to the filename's directory. + + +- `options` + - `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}`. + - `verbose` - A boolean that determines the promises fulfillment value. Supported values are: + - `true`: An instance of [`Results`](#results). + - `false`: An `html` string. (_Default_) + +__Example__ + +```js +co(function * () { + html = yield inline.file(`index.html`); + console.log(html); // +}); +``` + +--- + + +### Results + +The `Promise` returned by these functions is optionally fulfilled with a `results` object that has the following properties: - `html` - The inlined html - `files` - An array of filenames for the local assets that were inlined. diff --git a/lib/index.js b/lib/index.js index 0b9e0ba..5e61b48 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,30 +6,22 @@ var inlineLess = require('./inline-less'); var R = require('ramda'); var Ru = require('@panosoft/ramda-utils'); +var inlineHtml = {}; /** * Embed referenced local assets within and HTML file. * * @param {String} html - * Filename or html string. * @param {Object} options * * @return {Promise} */ -var inlineHtml = co.wrap(function * (html, options) { +inlineHtml.html = co.wrap(function * (html, options) { options = Ru.defaults({ filename: null, less: {}, verbose: false }, options || {}); - var filename; - try { - filename = html; - html = yield fs.readFile(filename, 'utf8'); - } - catch (error) { - if (error.code === 'ENOENT') filename = options.filename; - else throw error; - } + var filename = options.filename; // Embed assets var files = [filename]; @@ -53,11 +45,14 @@ var inlineHtml = co.wrap(function * (html, options) { } files = R.uniq(files); - var result = { - html, - files - }; + var result = { html, files }; return (options.verbose ? result : result.html); }); +inlineHtml.file = co.wrap(function * (filename, options) { + var html = yield fs.readFile(filename, 'utf8'); + options = R.merge(options || {}, {filename}); + return yield inlineHtml.html(html, options); +}); + module.exports = inlineHtml; diff --git a/test/fixtures/errors/lessImport/index.html b/test/fixtures/errors/lessImport/index.html deleted file mode 100644 index 21752b8..0000000 --- a/test/fixtures/errors/lessImport/index.html +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/test/fixtures/errors/lessSyntax/index.html b/test/fixtures/errors/lessSyntax/index.html deleted file mode 100644 index 9769b9c..0000000 --- a/test/fixtures/errors/lessSyntax/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/test/fixtures/errors/lessUrl/index.html b/test/fixtures/errors/lessUrl/index.html deleted file mode 100644 index 9769b9c..0000000 --- a/test/fixtures/errors/lessUrl/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/test/fixtures/errors/lessImport/main.less b/test/fixtures/invalidImport.less similarity index 100% rename from test/fixtures/errors/lessImport/main.less rename to test/fixtures/invalidImport.less diff --git a/test/fixtures/errors/lessSyntax/main.less b/test/fixtures/invalidSyntax.less similarity index 100% rename from test/fixtures/errors/lessSyntax/main.less rename to test/fixtures/invalidSyntax.less diff --git a/test/fixtures/errors/lessUrl/main.less b/test/fixtures/invalidUrl.less similarity index 100% rename from test/fixtures/errors/lessUrl/main.less rename to test/fixtures/invalidUrl.less diff --git a/test/index.js b/test/index.js index 05fe8b1..eb1c6a4 100644 --- a/test/index.js +++ b/test/index.js @@ -7,313 +7,331 @@ var fs = require('fs'); var inline = require('../lib'); var path = require('path'); -describe('inlineHtml', function () { +describe('inline-html', () => { - it('html: filename', function () { - var filename = path.resolve(__dirname, './fixtures/file.txt'); - var content = fs.readFileSync(filename, 'utf8'); - return expect(inline(filename)).to.eventually.equal(content); - }); - it('html: string', function () { - var html = 'HTML'; - return expect(inline(html)).to.eventually.equal(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: ignored 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: ignored 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('inline link less', function () { - var filename = path.resolve(__dirname, 'fixtures/basic.less'); - var html = ``; - return expect(inline(html)).to.eventually.match(/`; - return expect(inline(html)).to.eventually.match(/data:.*,.*/); - }); - it('inline css url path in element style attribute', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = `
`; - return expect(inline(html)).to.eventually.match(/data:.*,.*/); - }); - it('inline img src', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - return expect(inline(html)).to.eventually.match(/data:.*,.*/); - }); + it('accept html string', () => { + var html = 'HTML'; + return expect(inline.html(html)).to.eventually.equal(html); + }); + + it('inline link less', () => { + var filename = path.resolve(__dirname, 'fixtures/basic.less'); + var html = ``; + return expect(inline.html(html)).to.eventually.match(/`; + return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); + }); + it('inline css url path in element style attribute', () => { + var filename = path.resolve(__dirname, 'fixtures/file.txt'); + var html = `
`; + return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); + }); + it('inline img src', () => { + var filename = path.resolve(__dirname, 'fixtures/file.txt'); + var html = ``; + return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); + }); - it('options.verbose: return results object if true', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - var options = { verbose: true }; - return expect(inline(html, options)).to.eventually.be.an('object') - .that.contains.keys(['html', 'files']); - }); - it('options.verbose: return html if false', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - var options = { verbose: false }; - return expect(inline(html, options)).to.eventually.be.a('string'); - }); - it('options.verbose: default false', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - return expect(inline(html)).to.eventually.be.a('string'); - }); + 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'); + }); - it('options.filename: ignored for css url path resolution if html filename', function () { - var html = 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(html, options)).to.eventually.match(/"data:.*,.*"/); - }); - it('options.filename: ignored for img src path resolution if html filename', function () { - var html = 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(html, options)).to.eventually.match(/"data:.*,.*"/); - }); - it('options.filename: set basepath for css url path resolution if html string', function () { - var filename = path.resolve(__dirname, 'fixtures/fake.html'); - var dirname = path.dirname(filename); + 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 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(url), options)).to.eventually.equal(html(uri)); - }); - it('options.filename: set basepath for img src path resolution if html string', function () { - var filename = path.resolve(__dirname, 'fixtures/fake.html'); - var dirname = path.dirname(filename); + var html = (path) => ``; + var options = { filename: filename }; + return expect(inline.html(html(url), options)).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 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(url), options)).to.eventually.equal(html(uri)); - }); - it('options.filename: default to cwd for css url path resolution if html string', function () { - var url = 'test/fixtures/file.txt'; // Note: this is relative to cwd - var uri = datauri(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 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(url))).to.eventually.equal(html(uri)); - }); - it('options.filename: default to cwd for img src path resolution if html string', function () { - 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: 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(url))).to.eventually.equal(html(uri)); - }); - it('options.filename: included in results.files for img src if html string and options.verbose true', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - var options = { verbose: true }; - return expect(inline(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 html string and options.verbose true', function () { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - var options = { verbose: true }; - return expect(inline(html, options)).to.eventually.have.property('files') - .that.is.an('array') - .that.contains(filename); - }); + var html = (path) => ``; + return expect(inline.html(html(url))).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('preserve self closing tags', function () { - var html = '
'; - return expect(inline(html)).to.eventually.equal(html); - }); - it('preserve partials', function () { - var html = '{{> partial}}'; - return expect(inline(html)).to.eventually.equal(html); - }); - it('preserve helpers', function () { - var html = '{{helper}}'; - return expect(inline(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('ignore css url remote paths', function () { - var html = ``; - return expect(inline(html)).to.eventually.equal(html); - }); - it('ignore img src remote paths', function () { - var html = ``; - return expect(inline(html)).to.eventually.equal(html); - }); - it('ignore css url template expression paths', function () { - var html = ``; - return expect(inline(html)).to.eventually.equal(html); - }); - it('ignore img src template expression paths', function () { - var html = ``; - return expect(inline(html)).to.eventually.equal(html); - }); - it('ignore query strings and hashes on local paths', function () { - 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(url))).to.eventually.equal(html(uri)); - }); - it('handle assets with a space in their filename', function () { - var filename = path.resolve(__dirname, 'fixtures/file space.txt'); - var uri = datauri(filename); - var html = (source) => ``; - return expect(inline(html(filename))).to.eventually.equal(html(uri)); - }); + it('ignore css url remote paths', () => { + var html = ``; + return expect(inline.html(html)).to.eventually.equal(html); + }); + it('ignore img src remote paths', () => { + var html = ``; + return expect(inline.html(html)).to.eventually.equal(html); + }); + it('ignore css url template expression 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('ignore query strings and hashes on local paths', () => { + 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 assets with a space in their filename', () => { + 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)); + }); - // Error handling - // inline-img - it('throw error when html image source invalid', () => { - return 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, {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); - } - }); - }); - // inline-style - it('throw error when html style attribute syntax invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'index.html'); - var html = `
`; - try { - yield inline(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 error when html style attribute url invalid', () => { - return 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, {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 error when html style syntax invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'index.html'); - var html = ``; - try { - yield inline(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 error when html style url invalid', () => { - return 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, {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); - } - }); - }); - // inline-link-less - it('throw error when link href invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'index.html'); - var href = 'missing.less'; - var resolvedHref = path.resolve(path.dirname(filename), href); - var html = ``; - try { - yield inline(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(resolvedHref); - } - }); - }); - it('throw error when less import invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'fixtures/errors/lessImport/index.html'); - var lessFilename = path.resolve(path.dirname(filename), 'main.less'); - try { - yield inline(filename); - throw new Error('No error thrown'); - } - catch (error) { - expect(error).to.have.property('filename').that.equals(lessFilename); - expect(error).to.have.property('files').that.contains(lessFilename); - } - }); - }); - it('throw error when less syntax invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'fixtures/errors/lessSyntax/index.html'); - var lessFilename = path.resolve(path.dirname(filename), 'main.less'); - try { - yield inline(filename); - throw new Error('No error thrown'); - } - catch (error) { - expect(error).to.have.property('filename').that.equals(lessFilename); - expect(error).to.have.property('files').that.contains(lessFilename); - } - }); - }); - it('throw error when less url invalid', () => { - return co(function * () { - var filename = path.resolve(__dirname, 'fixtures/errors/lessUrl/index.html'); - var lessFilename = path.resolve(path.dirname(filename), 'main.less'); - var badUrl = path.resolve(path.dirname(filename), 'missing.png'); - try { - yield inline(filename); - throw new Error('No error thrown'); - } - catch (error) { - // expect error.filename to be html file, not less file, since images - // aren't inlined until after the compiled less has been inlined into the html. - expect(error).to.have.property('filename').that.equals(filename); - expect(error).to.have.property('files').that.contains(badUrl); - } + // Error handling + // inline-img + it('throw error when html image source invalid', () => { + return 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); + } + }); + }); + // inline-style + it('throw error when html style attribute syntax invalid', () => { + return 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 error when html style attribute url invalid', () => { + return 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 error when html style syntax invalid', () => { + return 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 error when html style url invalid', () => { + return 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); + } + }); + }); + // inline-link-less + it('throw error when link href invalid', () => { + return co(function * () { + var filename = path.resolve(__dirname, 'index.html'); + var href = 'missing.less'; + var resolvedHref = path.resolve(path.dirname(filename), href); + 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(resolvedHref); + } + }); + }); + it('throw error when less import invalid', () => { + return co(function * () { + var filename = path.resolve(__dirname, 'fixtures/index.html'); + var lessBasename = 'invalidImport.less'; + var lessFilename = path.resolve(path.dirname(filename), lessBasename); + var html = ``; + try { + yield inline.html(html, {filename}); + throw new Error('No error thrown'); + } + catch (error) { + expect(error).to.have.property('filename').that.equals(lessFilename); + expect(error).to.have.property('files').that.contains(lessFilename); + } + }); + }); + it('throw error when less syntax invalid', () => { + return co(function * () { + var filename = path.resolve(__dirname, 'fixtures/index.html'); + var lessBasename = 'invalidSyntax.less'; + var lessFilename = path.resolve(path.dirname(filename), lessBasename); + var html = ``; + try { + yield inline.html(html, {filename}); + throw new Error('No error thrown'); + } + catch (error) { + expect(error).to.have.property('filename').that.equals(lessFilename); + expect(error).to.have.property('files').that.contains(lessFilename); + } + }); + }); + it('throw error when less url invalid', () => { + return co(function * () { + var filename = path.resolve(__dirname, 'fixtures/index.html'); + var lessBasename = 'invalidUrl.less'; + var lessFilename = path.resolve(path.dirname(filename), lessBasename); + var badUrl = path.resolve(path.dirname(filename), 'missing.png'); + var html = ``; + try { + yield inline.html(html, {filename}); + throw new Error('No error thrown'); + } + catch (error) { + // expect error.filename to be html file, not less file, since images + // aren't inlined until after the compiled less has been inlined into the html. + expect(error).to.have.property('filename').that.equals(filename); + expect(error).to.have.property('files').that.contains(badUrl); + } + }); }); }); + });