Skip to content

Commit

Permalink
Merge pull request #26 from posthtml/milestone-0.2.6
Browse files Browse the repository at this point in the history
Milestone 0.2.6
  • Loading branch information
Scrum committed Nov 29, 2017
2 parents 4ae4599 + 7781c65 commit 5fc85b6
Show file tree
Hide file tree
Showing 8 changed files with 2,445 additions and 265 deletions.
56 changes: 0 additions & 56 deletions .jscsrc

This file was deleted.

2 changes: 0 additions & 2 deletions .jshintignore

This file was deleted.

8 changes: 0 additions & 8 deletions .jshintrc

This file was deleted.

7 changes: 4 additions & 3 deletions .npmignore
@@ -1,8 +1,9 @@
node_modules/
test/
coverage/
node_modules
test
coverage
npm-debug.log
.editorconfig
.jscsrc
.jshintignore
.jshintrc
.travis.yml
137 changes: 68 additions & 69 deletions index.js
@@ -1,85 +1,84 @@
/* global Promise */
var postcss = require('postcss');
var postcss = require('postcss')

function indentResolve(str, options) {
if (str.match(/\n(?!\n)\s*/g) === null) {
return str;
}

if (options.length === undefined) {
options.lastLine = str.substr(str.lastIndexOf('\n') + 1);
var newStr = str.substr(0, str.lastIndexOf('\n') + 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;
}));
function indentResolve (str, options) {
if (str.match(/\n(?!\n)\s*/g) === null) {
return str
}

if (options.length === Infinity) {
return str;
}
if (options.length === undefined) {
options.lastLine = str.substr(str.lastIndexOf('\n') + 1)
var newStr = str.substr(0, str.lastIndexOf('\n') + 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
}))

options.match = new Array(options.length).join(' ');
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;
if (options.length === Infinity) {
return str
}
return str;

options.match = new Array(options.length).join(' ')
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
}
return str
}

module.exports = function(plugins, options, filterType) {
plugins = [].concat(plugins);
options = options || {};
module.exports = function (plugins, options, filterType) {
plugins = [].concat(plugins).filter(plugin => typeof plugin === 'function')
options = options || {}

var css = postcss(plugins);
var css = postcss(plugins)

return function posthtmlPostcss(tree) {
var promises = [];
return function posthtmlPostcss (tree) {
var promises = []

tree.walk(function(node) {
var promise,
indent = {
length: undefined,
match: '',
lastLine: ''
};
tree.walk(function (node) {
var promise
var indent = {
length: undefined,
match: '',
lastLine: ''
}

if (node.tag === 'style' && node.content) {
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;
}
if (node.tag === 'style' && node.content) {
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
}

if (meetsFilter) {
var styles = indentResolve([].concat(node.content).join(''), indent);
promise = css.process(styles, options)
.then(function(result) {
node.content = [indentResolve(result.css, indent)];
});
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);
}
}
promises.push(promise)
}
}

if (node.attrs && node.attrs.style) {
promise = css.process(node.attrs.style, options)
.then(function(result) {
node.attrs.style = result.css;
});
if (node.attrs && node.attrs.style) {
promise = css.process(node.attrs.style, options)
.then(function (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(function () {
return tree
})
}
}

0 comments on commit 5fc85b6

Please sign in to comment.