Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from posthtml/milestone-0.2.6
Milestone 0.2.6
- Loading branch information
Showing
8 changed files
with
2,445 additions
and
265 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
node_modules/ | ||
test/ | ||
coverage/ | ||
node_modules | ||
test | ||
coverage | ||
npm-debug.log | ||
.editorconfig | ||
.jscsrc | ||
.jshintignore | ||
.jshintrc | ||
.travis.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
} |
Oops, something went wrong.