Skip to content

Commit

Permalink
style: after lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Nov 12, 2020
1 parent 9173ab9 commit 969315f
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 152 deletions.
60 changes: 30 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
});
};
};
248 changes: 126 additions & 122 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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 = '<style>a {display: flex;}</style>'
const expected = '<style>a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, null, null, done)
})

it('style tag empty', function (done) {
const html = '<style></style>'
const expected = '<style></style>'
test(html, expected, {}, null, null, done)
})

it('style attrs', function (done) {
const html = '<div style="display: flex;"></div>'
const expected = '<div style="display: -ms-flexbox;display: flex;"></div>'
test(html, expected, {}, null, null, done)
})

it('style attrs empty', function (done) {
const html = '<div style></div>'
const expected = '<div style=""></div>'
test(html, expected, {}, null, null, done)
})

it('no style', function (done) {
const html = 'text <div></div>'
const expected = 'text <div></div>'
test(html, expected, {}, null, null, done)
})

it('filtered style tag with standard type', function (done) {
const html = '<style type="text/css">a {display: flex;}</style>'
const expected = '<style type="text/css">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/css$/, null, done)
})

it('filtered style tag with standard type (with spaces)', function (done) {
const html = '<style type=" text/css ">a {display: flex;}</style>'
const expected = '<style type=" text/css ">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/css$/, null, done)
})

it('filtered style tag with standard type (empty string)', function (done) {
const html = '<style type="">a {display: flex;}</style>'
const expected = '<style type="">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/css$/, null, done)
})

it('filtered style tag with standard type (one empty space)', function (done) {
const html = '<style type=" ">a {display: flex;}</style>'
const expected = '<style type=" ">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/css$/, null, done)
})

it('filtered style tag with standard type (two empty spaces)', function (done) {
const html = '<style type=" ">a {display: flex;}</style>'
const expected = '<style type=" ">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/css$/, null, done)
})

it('filtered style tag with non-standard type', function (done) {
const html = '<style type="text/other">a {display: flex;}</style>'
const expected = '<style type="text/other">a {display: -ms-flexbox;display: flex;}</style>'
test(html, expected, {}, /^text\/other$/, null, done)
})

it('filtered out style tag with non-standard type', function (done) {
const html = '<style type="text/other">a {display: flex;}</style>'
const expected = html
test(html, expected, {}, /^text\/another$/, null, done)
})

it('style tag with newline and not indent', function (done) {
const html = 'text <style>\n.test { color: red; }</style>'
const expected = 'text <style>\n.test { color: red; }</style>'
test(html, expected, {}, null, null, done)
})

it('style tag with newline and multyply indent', function (done) {
const html = 'text <style>\n .test {\n color: red;\n}</style>'
const expected = 'text <style>\n .test {\n color: red;\n}</style>'
test(html, expected, {}, null, null, done)
})

it('style tag with newline and indent', function (done) {
const html = 'text <style>\n .test { color: red; }</style>'
const expected = 'text <style>\n .test { color: red; }</style>'
test(html, expected, {}, null, null, done)
})

it('style tag with newline and indent + plugin remove "\\n" character', function (done) {
const html = 'text <style>\n .test { color: red; }</style>'
const expected = 'text <style> .test { color: red; }</style>'

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 = '<style>a {display: flex;}</style>';
const expected = '<style>a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, null, null, done);
});

it('style tag empty', done => {
const html = '<style></style>';
const expected = '<style></style>';
test(html, expected, {}, null, null, done);
});

it('style attrs', done => {
const html = '<div style="display: flex;"></div>';
const expected = '<div style="display: -ms-flexbox;display: flex;"></div>';
test(html, expected, {}, null, null, done);
});

it('style attrs empty', done => {
const html = '<div style></div>';
const expected = '<div style=""></div>';
test(html, expected, {}, null, null, done);
});

it('no style', done => {
const html = 'text <div></div>';
const expected = 'text <div></div>';
test(html, expected, {}, null, null, done);
});

it('filtered style tag with standard type', done => {
const html = '<style type="text/css">a {display: flex;}</style>';
const expected = '<style type="text/css">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/css$/, null, done);
});

it('filtered style tag with standard type (with spaces)', done => {
const html = '<style type=" text/css ">a {display: flex;}</style>';
const expected = '<style type=" text/css ">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/css$/, null, done);
});

it('filtered style tag with standard type (empty string)', done => {
const html = '<style type="">a {display: flex;}</style>';
const expected = '<style type="">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/css$/, null, done);
});

it('filtered style tag with standard type (one empty space)', done => {
const html = '<style type=" ">a {display: flex;}</style>';
const expected = '<style type=" ">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/css$/, null, done);
});

it('filtered style tag with standard type (two empty spaces)', done => {
const html = '<style type=" ">a {display: flex;}</style>';
const expected = '<style type=" ">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/css$/, null, done);
});

it('filtered style tag with non-standard type', done => {
const html = '<style type="text/other">a {display: flex;}</style>';
const expected = '<style type="text/other">a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, /^text\/other$/, null, done);
});

it('filtered out style tag with non-standard type', done => {
const html = '<style type="text/other">a {display: flex;}</style>';
const expected = html;
test(html, expected, {}, /^text\/another$/, null, done);
});

it('style tag with newline and not indent', done => {
const html = 'text <style>\n.test { color: red; }</style>';
const expected = 'text <style>\n.test { color: red; }</style>';
test(html, expected, {}, null, null, done);
});

it('style tag with newline and multyply indent', done => {
const html = 'text <style>\n .test {\n color: red;\n}</style>';
const expected = 'text <style>\n .test {\n color: red;\n}</style>';
test(html, expected, {}, null, null, done);
});

it('style tag with newline and indent', done => {
const html = 'text <style>\n .test { color: red; }</style>';
const expected = 'text <style>\n .test { color: red; }</style>';
test(html, expected, {}, null, null, done);
});

it('style tag with newline and indent + plugin remove "\\n" character', done => {
const html = 'text <style>\n .test { color: red; }</style>';
const expected = 'text <style> .test { color: red; }</style>';

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);
});
});
2 changes: 2 additions & 0 deletions xo.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = {
env: ['mocha'],
space: true,
rules: {
'promise/prefer-await-to-then': 'off'
}
};

0 comments on commit 969315f

Please sign in to comment.