Skip to content

Commit

Permalink
fix indent fn if has one '\n' char in styles
Browse files Browse the repository at this point in the history
  • Loading branch information
voischev committed Aug 29, 2016
1 parent 25021d3 commit 7f4f116
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
18 changes: 10 additions & 8 deletions index.js
Expand Up @@ -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');

This comment has been minimized.

Copy link
@devinus

devinus Aug 29, 2016

Contributor

You can't cache this, the position changes.


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;
Expand All @@ -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;

This comment has been minimized.

Copy link
@devinus

devinus Aug 29, 2016

Contributor

Cuts off prematurely now.

}
return str;
}
Expand Down
36 changes: 28 additions & 8 deletions test/test.js
Expand Up @@ -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);
Expand All @@ -22,36 +23,55 @@ 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, {}, done);
test(html, expected, {}, null, done);
});

it('style tag empty', function(done) {
var html = '<style></style>';
var expected = '<style></style>';
test(html, expected, {}, done);
test(html, expected, {}, 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, {}, done);
test(html, expected, {}, null, done);
});

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

it('no style', function(done) {
var html = 'text <div></div>';
var expected = 'text <div></div>';
test(html, expected, {}, done);
test(html, expected, {}, 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, {}, done);
test(html, expected, {}, 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);
});

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

function plugin(root) {
root.walk(function(node) {
node.raws.before = node.raws.before.replace('\n', '');
});
}

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

0 comments on commit 7f4f116

Please sign in to comment.