diff --git a/index.js b/index.js index 6f7634a..b8b9c3c 100644 --- a/index.js +++ b/index.js @@ -1,52 +1,52 @@ -const postcss = require('postcss') +const postcss = require('postcss'); module.exports = function (plugins, options, filterType) { - plugins = [].concat(plugins).filter(Boolean) - options = options || {} + plugins = [].concat(plugins).filter(Boolean); + options = options || {}; - const css = postcss(plugins) + const css = postcss(plugins); - return function posthtmlPostcss (tree) { - const promises = [] + return function (tree) { + const promises = []; - tree.walk(function (node) { - let promise + tree.walk(node => { + let promise; if (node.tag === 'style' && node.content) { - let meetsFilter = true + let meetsFilter = true; if (filterType) { - const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : '' - const meetsTypeAttr = filterType.test(typeAttr) - const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '') - const meetsOtherType = !meetsStandardType && meetsTypeAttr - meetsFilter = meetsStandardType || meetsOtherType + const typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : ''; + const meetsTypeAttr = filterType.test(typeAttr); + const meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === ''); + const meetsOtherType = !meetsStandardType && meetsTypeAttr; + meetsFilter = meetsStandardType || meetsOtherType; } if (meetsFilter) { - const styles = [].concat(node.content).join('') + const styles = [].concat(node.content).join(''); promise = css.process(styles, options) - .then(function (result) { - node.content = [result.css] - }) + .then(result => { + node.content = [result.css]; + }); - promises.push(promise) + promises.push(promise); } } if (node.attrs && node.attrs.style) { promise = css.process(node.attrs.style, options) - .then(function (result) { - node.attrs.style = result.css - }) + .then(result => { + node.attrs.style = result.css; + }); - promises.push(promise) + promises.push(promise); } - return node - }) + return node; + }); - return Promise.all(promises).then(function () { - return tree - }) - } -} + return Promise.all(promises).then(() => { + return tree; + }); + }; +}; diff --git a/test/test.js b/test/test.js index 1af0a94..56da648 100644 --- a/test/test.js +++ b/test/test.js @@ -1,129 +1,133 @@ -const posthtml = require('posthtml') -const css = require('..') -const expect = require('chai').expect +const posthtml = require('posthtml'); +const css = require('..'); +const {expect} = require('chai'); -function test (html, expected, postcssOptions, typeFilter, plugins, done) { - Object.assign(postcssOptions, { from: undefined }) - plugins = plugins || [require('autoprefixer')({ overrideBrowserslist: ['ie >= 10'] })] +function test(html, expected, postcssOptions, typeFilter, plugins, done) { + Object.assign(postcssOptions, {from: undefined}); + plugins = plugins || [require('autoprefixer')({overrideBrowserslist: ['ie >= 10']})]; expect(posthtml([css(plugins, postcssOptions, typeFilter)]) .process(html) - .then(function (result) { - expect(expected).to.eql(result.html) - done() - }).catch(function (error) { - done(error) - })) + .then(result => { + expect(expected).to.eql(result.html); + done(); + }).catch(error => { + done(error); + })); } -describe('use postcss', function () { - it('object options', function () { - expect(function () { posthtml([css({})]) }).to.throw(Error) - }) - - it('options', function () { - expect(function () { posthtml([css([])]) }).to.not.throw(Error) - }) - - it('style tag', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, null, null, done) - }) - - it('style tag empty', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, null, null, done) - }) - - it('style attrs', function (done) { - const html = '
' - const expected = '' - test(html, expected, {}, null, null, done) - }) - - it('style attrs empty', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, null, null, done) - }) - - it('no style', function (done) { - const html = 'text ' - const expected = 'text ' - test(html, expected, {}, null, null, done) - }) - - it('filtered style tag with standard type', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/css$/, null, done) - }) - - it('filtered style tag with standard type (with spaces)', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/css$/, null, done) - }) - - it('filtered style tag with standard type (empty string)', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/css$/, null, done) - }) - - it('filtered style tag with standard type (one empty space)', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/css$/, null, done) - }) - - it('filtered style tag with standard type (two empty spaces)', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/css$/, null, done) - }) - - it('filtered style tag with non-standard type', function (done) { - const html = '' - const expected = '' - test(html, expected, {}, /^text\/other$/, null, done) - }) - - it('filtered out style tag with non-standard type', function (done) { - const html = '' - const expected = html - test(html, expected, {}, /^text\/another$/, null, done) - }) - - it('style tag with newline and not indent', function (done) { - const html = 'text ' - const expected = 'text ' - test(html, expected, {}, null, null, done) - }) - - it('style tag with newline and multyply indent', function (done) { - const html = 'text ' - const expected = 'text ' - test(html, expected, {}, null, null, done) - }) - - it('style tag with newline and indent', function (done) { - const html = 'text ' - const expected = 'text ' - test(html, expected, {}, null, null, done) - }) - - it('style tag with newline and indent + plugin remove "\\n" character', function (done) { - const html = 'text ' - const expected = 'text ' - - function plugin (root) { - root.walk(function (node) { - node.raws.before = node.raws.before.replace('\n', '') - }) +describe('use postcss', () => { + it('object options', () => { + expect(() => { + posthtml([css({})]); + }).to.throw(Error); + }); + + it('options', () => { + expect(() => { + posthtml([css([])]); + }).to.not.throw(Error); + }); + + it('style tag', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, null, null, done); + }); + + it('style tag empty', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, null, null, done); + }); + + it('style attrs', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, null, null, done); + }); + + it('style attrs empty', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, null, null, done); + }); + + it('no style', done => { + const html = 'text '; + const expected = 'text '; + test(html, expected, {}, null, null, done); + }); + + it('filtered style tag with standard type', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (with spaces)', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (empty string)', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (one empty space)', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (two empty spaces)', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with non-standard type', done => { + const html = ''; + const expected = ''; + test(html, expected, {}, /^text\/other$/, null, done); + }); + + it('filtered out style tag with non-standard type', done => { + const html = ''; + const expected = html; + test(html, expected, {}, /^text\/another$/, null, done); + }); + + it('style tag with newline and not indent', done => { + const html = 'text '; + const expected = 'text '; + test(html, expected, {}, null, null, done); + }); + + it('style tag with newline and multyply indent', done => { + const html = 'text '; + const expected = 'text '; + test(html, expected, {}, null, null, done); + }); + + it('style tag with newline and indent', done => { + const html = 'text '; + const expected = 'text '; + test(html, expected, {}, null, null, done); + }); + + it('style tag with newline and indent + plugin remove "\\n" character', done => { + const html = 'text '; + const expected = 'text '; + + function plugin(root) { + root.walk(node => { + node.raws.before = node.raws.before.replace('\n', ''); + }); } - test(html, expected, {}, null, [plugin], done) - }) -}) + test(html, expected, {}, null, [plugin], done); + }); +}); diff --git a/xo.config.js b/xo.config.js index 25cc552..817a419 100644 --- a/xo.config.js +++ b/xo.config.js @@ -1,5 +1,7 @@ module.exports = { + env: ['mocha'], space: true, rules: { + 'promise/prefer-await-to-then': 'off' } };