From 7f4f1164f022880057af9174bb3c63dd0830afdd Mon Sep 17 00:00:00 2001 From: Ivan Voischev Date: Mon, 29 Aug 2016 14:39:30 +0300 Subject: [PATCH] fix indent fn if has one '\n' char in styles --- index.js | 18 ++++++++++-------- test/test.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index c972f29..a32dfee 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,16 @@ var postcss = require('postcss'); function indentResolve(str, options) { - if (options.length === undefined) { - if (str.match(/\n(?!\n)\s*/g) === null) { - return str; - } + var strLastIndexOf = str.lastIndexOf('\n'); - options.lastLine = str.substr(str.lastIndexOf('\n') + 1); - str = str.substr(0, str.lastIndexOf('\n') + 1); - options.length = Math.min.apply(Math, str.match(/\n(?!\n)\s*/g).filter(function(space) { + if (str.match(/\n(?!\n)\s*/g) === null) { + return str; + } + + if (options.length === undefined) { + options.lastLine = str.substr(strLastIndexOf + 1); + var newStr = str.substr(0, strLastIndexOf + 1); + options.length = Math.min.apply(Math, newStr.match(/\n(?!\n)\s*/g).filter(function(space) { return space.length > 2; }).map(function(space) { return space.length; @@ -23,7 +25,7 @@ function indentResolve(str, options) { str = str.replace(new RegExp(options.match,'g'), ''); } else { str = str.replace(/\n/g, '\n' + options.match); - str = str.substr(0, str.lastIndexOf('\n') + 1) + options.lastLine; + str = str.substr(0, strLastIndexOf + 1) + options.lastLine; } return str; } diff --git a/test/test.js b/test/test.js index 6186010..027b05a 100644 --- a/test/test.js +++ b/test/test.js @@ -3,8 +3,9 @@ var posthtml = require('posthtml'); var css = require('..'); var expect = require('chai').expect; -function test(html, expected, postcssOptions, done) { - expect(posthtml([css([require('autoprefixer')({ browsers: ['last 2 versions'] })], postcssOptions)]) +function test(html, expected, postcssOptions, plugins, done) { + plugins = plugins || [require('autoprefixer')({ browsers: ['last 2 versions'] })]; + expect(posthtml([css(plugins, postcssOptions)]) .process(html) .then(function(result) { expect(expected).to.eql(result.html); @@ -22,36 +23,55 @@ describe('use postcss', function() { it('style tag', function(done) { var html = ''; var expected = ''; - test(html, expected, {}, done); + test(html, expected, {}, null, done); }); it('style tag empty', function(done) { var html = ''; var expected = ''; - test(html, expected, {}, done); + test(html, expected, {}, null, done); }); it('style attrs', function(done) { var html = '
'; var expected = '
'; - test(html, expected, {}, done); + test(html, expected, {}, null, done); }); it('style attrs empty', function(done) { var html = '
'; var expected = '
'; - test(html, expected, {}, done); + test(html, expected, {}, null, done); }); it('no style', function(done) { var html = 'text
'; var expected = 'text
'; - test(html, expected, {}, done); + test(html, expected, {}, null, done); }); it('style tag with newline and not indent', function(done) { var html = 'text '; var expected = 'text '; - test(html, expected, {}, done); + test(html, expected, {}, null, done); + }); + + it('style tag with newline and indent', function(done) { + var html = 'text '; + var expected = 'text '; + test(html, expected, {}, null, done); + }); + + it('style tag with newline and indent + plugin remove "\\n" character', function(done) { + var html = 'text '; + var expected = 'text '; + + function plugin(root) { + root.walk(function(node) { + node.raws.before = node.raws.before.replace('\n', ''); + }); + } + + test(html, expected, {}, [plugin], done); }); });