Skip to content

Commit

Permalink
Merge pull request #20 from stevenvachon/master
Browse files Browse the repository at this point in the history
Added `filterType` argument
  • Loading branch information
voischev committed Dec 6, 2016
2 parents f3b3ce7 + ce5841e commit 3ad88db
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
4 changes: 0 additions & 4 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
"disallowSpacesInFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInConditionalExpression": {
"afterTest": true,
"afterConsequent": true
},
"requireSpaceBeforeBlockStatements": 1,
"requireSpaceAfterKeywords": [
"do",
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const postcss = require('posthtml-postcss')

const postcssPlugins = []
const postcssOptions = {}
const filterType = /^text\/css$/

const html = readFileSync('./index.html', 'utf8')

posthtml([ postcss(postcssPlugins, postcssOptions) ])
posthtml([ postcss(postcssPlugins, postcssOptions, filterType) ])
.process(html)
.then((result) => console.log(result.html))
```
Expand All @@ -44,6 +45,7 @@ const postcssPlugins = [
require('autoprefixer')({ browsers: ['last 2 versions'] })
]
const postcssOptions = {}
const filterType = /^text\/css$/

const html = `
<style>div { display: flex; }</style>
Expand Down
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function indentResolve(str, options) {
return str;
}

module.exports = function(plugins, options) {
module.exports = function(plugins, options, filterType) {
plugins = [].concat(plugins);
options = options || {};

Expand All @@ -46,13 +46,24 @@ module.exports = function(plugins, options) {
};

if (node.tag === 'style' && node.content) {
var styles = indentResolve([].concat(node.content).join(''), indent);
promise = css.process(styles, options)
.then(function(result) {
node.content = [indentResolve(result.css, indent)];
});
var meetsFilter = true;
if (filterType) {
var typeAttr = (node.attrs && node.attrs.type) ? node.attrs.type.trim() : '';
var meetsTypeAttr = filterType.test(typeAttr);
var meetsStandardType = filterType.test('text/css') && (meetsTypeAttr || typeAttr === '');
var meetsOtherType = !meetsStandardType && meetsTypeAttr;
meetsFilter = meetsStandardType || meetsOtherType;
}

promises.push(promise);
if (meetsFilter) {
var styles = indentResolve([].concat(node.content).join(''), indent);
promise = css.process(styles, options)
.then(function(result) {
node.content = [indentResolve(result.css, indent)];
});

promises.push(promise);
}
}

if (node.attrs && node.attrs.style) {
Expand Down
64 changes: 53 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var posthtml = require('posthtml');
var css = require('..');
var expect = require('chai').expect;

function test(html, expected, postcssOptions, plugins, done) {
function test(html, expected, postcssOptions, typeFilter, plugins, done) {
plugins = plugins || [require('autoprefixer')({ browsers: ['last 2 versions'] })];
expect(posthtml([css(plugins, postcssOptions)])
expect(posthtml([css(plugins, postcssOptions, typeFilter)])
.process(html)
.then(function(result) {
expect(expected).to.eql(result.html);
Expand All @@ -23,49 +23,91 @@ describe('use postcss', function() {
it('style tag', function(done) {
var html = '<style>a {display: flex;}</style>';
var expected = '<style>a {display: -ms-flexbox;display: flex;}</style>';
test(html, expected, {}, null, done);
test(html, expected, {}, null, null, done);
});

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

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

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

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

it('filtered style tag with standard type', function(done) {
var html = '<style type="text/css">a {display: flex;}</style>';
var 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) {
var html = '<style type=" text/css ">a {display: flex;}</style>';
var 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) {
var html = '<style type="">a {display: flex;}</style>';
var 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) {
var html = '<style type=" ">a {display: flex;}</style>';
var 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) {
var html = '<style type=" ">a {display: flex;}</style>';
var 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) {
var html = '<style type="text/other">a {display: flex;}</style>';
var 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) {
var html = '<style type="text/other">a {display: flex;}</style>';
var expected = html;
test(html, expected, {}, /^text\/another$/, null, done);
});

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

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

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

it('style tag with newline and indent + plugin remove "\\n" character', function(done) {
Expand All @@ -78,6 +120,6 @@ describe('use postcss', function() {
});
}

test(html, expected, {}, [plugin], done);
test(html, expected, {}, null, [plugin], done);
});
});

0 comments on commit 3ad88db

Please sign in to comment.