From 79054c46d92294410dcbc26244c6a7cd29d1c34c Mon Sep 17 00:00:00 2001 From: Steven D Vachon Date: Thu, 1 Dec 2016 18:14:18 -0500 Subject: [PATCH] Added `filterType` argument closes #18 --- README.md | 4 +++- index.js | 25 ++++++++++++++------ test/test.js | 64 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 33a3d2a..4dd48e3 100644 --- a/README.md +++ b/README.md @@ -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)) ``` @@ -44,6 +45,7 @@ const postcssPlugins = [ require('autoprefixer')({ browsers: ['last 2 versions'] }) ] const postcssOptions = {} +const filterType = /^text\/css$/ const html = ` diff --git a/index.js b/index.js index 216f423..8daaa61 100644 --- a/index.js +++ b/index.js @@ -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 || {}; @@ -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) { diff --git a/test/test.js b/test/test.js index 134471f..6e7446e 100644 --- a/test/test.js +++ b/test/test.js @@ -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); @@ -23,49 +23,91 @@ describe('use postcss', function() { it('style tag', function(done) { var html = ''; var expected = ''; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style tag empty', function(done) { var html = ''; var expected = ''; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style attrs', function(done) { var html = '
'; var expected = '
'; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style attrs empty', function(done) { var html = '
'; var expected = '
'; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('no style', function(done) { var html = 'text
'; var expected = 'text
'; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); + }); + + it('filtered style tag with standard type', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (with spaces)', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (empty string)', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (one empty space)', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with standard type (two empty spaces)', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/css$/, null, done); + }); + + it('filtered style tag with non-standard type', function(done) { + var html = ''; + var expected = ''; + test(html, expected, {}, /^text\/other$/, null, done); + }); + + it('filtered out style tag with non-standard type', function(done) { + var html = ''; + var expected = html; + test(html, expected, {}, /^text\/another$/, null, done); }); it('style tag with newline and not indent', function(done) { var html = 'text '; var expected = 'text '; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style tag with newline and multyply indent', function(done) { var html = 'text '; var expected = 'text '; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style tag with newline and indent', function(done) { var html = 'text '; var expected = 'text '; - test(html, expected, {}, null, done); + test(html, expected, {}, null, null, done); }); it('style tag with newline and indent + plugin remove "\\n" character', function(done) { @@ -78,6 +120,6 @@ describe('use postcss', function() { }); } - test(html, expected, {}, [plugin], done); + test(html, expected, {}, null, [plugin], done); }); });