diff --git a/lib/css-url.js b/lib/css-url.js index f0756f3..41a3af9 100644 --- a/lib/css-url.js +++ b/lib/css-url.js @@ -10,8 +10,6 @@ const url = require('url'); const collapseWhitespace = str => string(str).collapseWhitespace().toString(); const forEachIndexed = R.addIndex(R.forEach); -const format = (path) => url.format(path); -const parse = (path) => url.parse(path); const resolve = path.resolve; const augmentError = (error, filename, files) => { @@ -24,13 +22,12 @@ const augmentError = (error, filename, files) => { * Returns url path without query string and hash if present. * * @param path - * * @returns path */ const cleanUrl = R.pipe( - parse, + url.parse, R.pick(['protocol', 'host', 'pathname']), - format, + url.format, decodeURI ); /** @@ -68,16 +65,14 @@ const inlineStyles = ($, filename) => { try { const styles = $('style') .toArray(); - const contents = R.map(style => { const css = $(style).html(); const result = inlineUrl(filename, css); files = R.concat(files, result.files); return result.css; }, styles); - - forEachIndexed((style, index) => $(style).html(contents[index]), styles); - + const replaceStyle = (style, index) => $(style).html(contents[index]); + forEachIndexed(replaceStyle, styles); return { $, files }; } catch (error) { throw augmentError(error, filename, files); } @@ -95,7 +90,6 @@ const inlineStyleAttributes = ($, filename) => { const elements = $('*') .filter('[style]') .toArray(); - const styles = R.map(element => { var style = $(element).attr('style'); const rule = wrap(style); @@ -104,9 +98,8 @@ const inlineStyleAttributes = ($, filename) => { style = R.pipe( collapseWhitespace, unwrap )(result.css); return style; }, elements); - - forEachIndexed((element, index) => $(element).attr('style', styles[index]), elements); - + const replaceElementStyle = (element, index) => $(element).attr('style', styles[index]); + forEachIndexed(replaceElementStyle, elements); return { $, files }; } catch (error) { throw augmentError(error, filename, files); } diff --git a/lib/img.js b/lib/img.js index eca6438..ea2f846 100644 --- a/lib/img.js +++ b/lib/img.js @@ -6,7 +6,6 @@ const path = require('path'); const R = require('ramda'); const forEachIndexed = R.addIndex(R.forEach); -const resolve = R.curry((a,b) => path.resolve(a,b)); /** * Inline sourced image files @@ -17,11 +16,8 @@ const resolve = R.curry((a,b) => path.resolve(a,b)); * Filename used to resolve relative sources being inlined */ const inlineImg = co.wrap(function * ($, filename) { - var files; const basedir = path.dirname(filename); - const getAttr = R.curry((attr, element) => $(element).attr(attr)); - const setAttr = R.curry((attr, element, value) => $(element).attr(attr, value)); - const getFilename = R.pipe(getAttr('src'), resolve(basedir)); + var files; try { const images = $('img') .filter((index, element) => { @@ -29,13 +25,12 @@ const inlineImg = co.wrap(function * ($, filename) { return isLocalPath(source) && !isTemplateExpression(source); }) .toArray(); - + const getFilename = element => path.resolve(basedir, $(element).attr('src')); const filenames = R.map(getFilename, images); files = R.uniq(filenames); const uris = yield R.map(datauri, filenames); - - forEachIndexed((image, index) => setAttr('src', image, uris[index]), images); - + const replaceImageSource = (image, index) => $(image).attr('src', uris[index]); + forEachIndexed(replaceImageSource, images); return { $, files }; } catch (error) { diff --git a/lib/link-less.js b/lib/link-less.js index fc6a899..e258a57 100644 --- a/lib/link-less.js +++ b/lib/link-less.js @@ -7,7 +7,6 @@ const R = require('ramda'); const Ru = require('@panosoft/ramda-utils'); const forEachIndexed = R.addIndex(R.forEach); -const resolve = R.curry((a,b) => path.resolve(a,b)); const render = R.curryN(2, co.wrap(function * (options, filename) { options = R.merge(options || {}, { filename }); @@ -28,24 +27,21 @@ const render = R.curryN(2, co.wrap(function * (options, filename) { const inlineLess = co.wrap(function * ($, filename, options) { options = Ru.defaults({ less: {} }, options || {}); options = Ru.defaults({ relativeUrls: true }, options.less); - var files = []; const basedir = path.dirname(filename); - const getAttr = R.curry((attr, element) => $(element).attr(attr)); - const getStylesheet = R.pipe(getAttr('href'), resolve(basedir)); + var files = []; try { const links = $('link[rel="stylesheet/less"]') .filter((index, link) => isLocalPath($(link).attr('href'))) .toArray(); - - const stylesheets = R.map(getStylesheet, links); - files = R.concat(files, stylesheets); - const outputs = yield R.map(render(options), stylesheets); + const getHref = element => path.resolve(basedir, $(element).attr('href')); + const hrefs = R.map(getHref, links); + files = R.concat(files, hrefs); + const outputs = yield R.map(render(options), hrefs); const imports = R.flatten(R.map(R.prop('imports'), outputs)); files = R.concat(files, imports); const styles = R.map(output => $('`; + const 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); + const filename = path.resolve(__dirname, 'fixtures/fake.html'); + const dirname = path.dirname(filename); - var url = 'file.txt'; // Note: path relative to filename's dirname - var uri = datauri(path.resolve(dirname, url)); + const url = 'file.txt'; // Note: path relative to filename's dirname + const uri = datauri(path.resolve(dirname, url)); - var html = (path) => ``; - var options = { filename: filename }; + const html = (path) => ``; + const 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); + const url = 'test/fixtures/file.txt'; // Note: path relative to cwd + const uri = datauri(url); - var html = (path) => ``; + const 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); + const filename = path.resolve(__dirname, 'fixtures/fake.html'); + const dirname = path.dirname(filename); - var url = 'file.txt'; // Note: path relative to filename's dirname - var uri = datauri(path.resolve(dirname, url)); + const url = 'file.txt'; // Note: path relative to filename's dirname + const uri = datauri(path.resolve(dirname, url)); - var html = (path) => ``; - var options = { filename: filename }; + const html = (path) => ``; + const 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 }; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = ``; + const 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 }; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = ``; + const options = { verbose: true }; return expect(inline.html(html, options)).to.eventually.have.property('files') .that.is.an('array') .that.contains(filename); @@ -109,60 +109,60 @@ describe('inline-html', () => { it('options.verbose: return results object if true', () => { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; - var options = { verbose: true }; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = ``; + const 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 }; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = ``; + const 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 = ``; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const 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 = ``; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const 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 = `
`; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = `
`; return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); }); it('ignore remote url', () => { - var html = ``; + const html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('ignore template expression url', () => { - var html = ``; + const 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) => ``; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const url = `${filename}?query=string#hash`; + const uri = datauri(filename); + const 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) => ``; + const filename = path.resolve(__dirname, 'fixtures/file space.txt'); + const uri = datauri(filename); + const 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 = ``; + const filename = path.resolve(__dirname, 'index.html'); + const html = ``; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); @@ -173,8 +173,8 @@ describe('inline-html', () => { } })); it('throw when syntax invalid in style attribute', () => co(function * () { - var filename = path.resolve(__dirname, 'index.html'); - var html = `
`; + const filename = path.resolve(__dirname, 'index.html'); + const html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); @@ -185,10 +185,10 @@ describe('inline-html', () => { } })); 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 = ``; + const filename = path.resolve(__dirname, 'index.html'); + const url = 'missing.png'; + const resolvedUrl = path.resolve(path.dirname(filename), url); + const html = ``; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); @@ -199,10 +199,10 @@ describe('inline-html', () => { } })); 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 = `
`; + const filename = path.resolve(__dirname, 'index.html'); + const url = 'missing.png'; + const resolvedUrl = path.resolve(path.dirname(filename), url); + const html = `
`; try { yield inline.html(html, {filename}); throw new Error('No error thrown'); @@ -213,12 +213,12 @@ describe('inline-html', () => { } })); 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 = ` + const filename = path.resolve(__dirname, 'index.html'); + const validUrl = 'fixtures/file.txt'; + const invalidUrl = 'missing.png'; + const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); + const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); + const html = ` `; @@ -233,12 +233,12 @@ describe('inline-html', () => { } })); 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 = ` + const filename = path.resolve(__dirname, 'index.html'); + const validUrl = 'fixtures/file.txt'; + const invalidUrl = 'missing.png'; + const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); + const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); + const html = `
`; @@ -253,12 +253,12 @@ describe('inline-html', () => { } })); 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 = ` + const filename = path.resolve(__dirname, 'index.html'); + const validUrl = 'fixtures/file.txt'; + const invalidUrl = 'missing.png'; + const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl); + const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl); + const html = `
`; @@ -276,23 +276,23 @@ describe('inline-html', () => { describe('img', () => { it('inline img src', () => { - var filename = path.resolve(__dirname, 'fixtures/file.txt'); - var html = ``; + const filename = path.resolve(__dirname, 'fixtures/file.txt'); + const html = ``; return expect(inline.html(html)).to.eventually.match(/data:.*,.*/); }); it('ignore img src remote paths', () => { - var html = ``; + const html = ``; return expect(inline.html(html)).to.eventually.equal(html); }); it('ignore img src template expression paths', () => { - var html = ``; + const 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); + const filename = path.resolve(__dirname, 'index.html'); + const source = 'missing.png'; + const html = ``; + const resolvedSource = path.resolve(path.dirname(filename), source); try { yield inline.html(html, {filename}); throw new Error('No error thrown'); @@ -303,12 +303,12 @@ describe('inline-html', () => { } })); 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 = ` + const filename = path.resolve(__dirname, 'index.html'); + const valid = 'fixtures/file.txt'; + const invalid = 'missing.png'; + const resolvedInvalid = path.resolve(path.dirname(filename), invalid); + const resolvedValid = path.resolve(path.dirname(filename), valid); + const html = ` `; @@ -326,21 +326,21 @@ describe('inline-html', () => { describe('link-less', () => { it('inline link less', () => { - var filename = path.resolve(__dirname, 'fixtures/basic.less'); - var html = ``; + const filename = path.resolve(__dirname, 'fixtures/basic.less'); + const html = ``; return expect(inline.html(html)).to.eventually.match(/