Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# NODEJS
node_modules
npm-debug.log
.nyc_output
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: node_js
node_js:
- "node"
- "lts/*"
- "8"

after_script:
- npm run coverage
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ module.exports = function posthtmlStyleToFile(options) {

tree.match({ tag: 'style' }, function(node) {
buf += node.content[0].trim() || '';
return node;

if (options.removeStyle === 'tag' || options.removeStyle === 'all') {
return '';
}

return node;
});

tree.match({ attrs: { style: true }}, function(node) {
Expand All @@ -22,6 +27,11 @@ module.exports = function posthtmlStyleToFile(options) {
node.tag +
(node.attrs.id? ('#' + node.attrs.id ): '') +
cls + '{' + node.attrs.style + '}';

if (options.removeStyle === 'attrs' || options.removeStyle === 'all') {
delete node.attrs.style;
}

return node;
});

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "posthtml-style-to-file",
"version": "0.1.1",
"version": "0.2.0",
"description": "posthtml style-to-file plugin",
"main": "index.js",
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"chai": "^3.2.0",
"jscs": "^1.13.1",
"chai": "^4.2.0",
"coveralls": "^3.0.4",
"jscs": "^3.0.7",
"jshint": "^2.8.0",
"posthtml": "^0.3.0",
"mocha": "^2.2.5"
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"posthtml": "^0.11.4"
},
"scripts": {
"test": "npm run lint && mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "npm run lint && nyc mocha",
"lint": "jshint . && jscs . -v"
},
"repository": {
Expand Down
16 changes: 14 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,21 @@ npm i -D posthtml-style-to-file

<h2 align="center">Usage</h2>

### Options
## Options

__`path`__
#### `path`

Type: `String`
Default: `./result.css`
Description: *Destination path, where the extracted CSS is saved to.*

#### `removeStyle`

Type: `String`
Default: ``
Description: *Removes the specified value `attrs` | `tag` | `all`*

__`removeStyle`__

Destination path, where the extracted CSS is saved to.

Expand Down
86 changes: 72 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,88 @@ var plugin = require('..');
var expect = require('chai').expect;
var fs = require('fs');

var HTML = fs.readFileSync('./test/test.html', 'utf-8'),
CSS = fs.readFileSync('./test/test.css', 'utf-8');
var HTML = fs.readFileSync('./test/test.html', 'utf-8');
var CSS = fs.readFileSync('./test/test.css', 'utf-8');

function test(html, referenceCss, done) {
function test(html, options, callbackSuccess, callbackError) {
posthtml()
.use(plugin({ path: './test/test2.css' }))
.use(plugin(options))
.process(html)
.then(function(/*result*/) {
setTimeout(function() {
expect(referenceCss).to.eql(fs.readFileSync('./test/test2.css', 'utf-8').toString());
done();
}, 20);
}).catch(function(error) {
done(error);
});
.then(callbackSuccess)
.catch(callbackError);
}

describe('Simple test', function() {
it('style to file', function(done) {
test(
HTML,
CSS,
done
{ path: './test/test2.css' },
function() {
setTimeout(function() {
expect(CSS).to.eql(fs.readFileSync('./test/test2.css', 'utf-8').toString());
done();
}, 20);
},
function(error) {
done(error);
}
);
});

it('remove style attrs', function(done) {
test(
HTML,
{
path: './test/test2.css',
removeStyle: 'attrs'
},
function(result) {
setTimeout(function() {
expect(result.html).to.eql(fs.readFileSync('./test/testRemoveAttrs.html', 'utf-8').toString());
done();
}, 20);
},
function(error) {
done(error);
}
);
});

it('remove style tag', function(done) {
test(
HTML,
{
path: './test/test2.css',
removeStyle: 'tag'
},
function(result) {
setTimeout(function() {
expect(result.html).to.eql(fs.readFileSync('./test/testRemoveTag.html', 'utf-8').toString());
done();
}, 20);
},
function(error) {
done(error);
}
);
});

it('remove style all', function(done) {
test(
HTML,
{
path: './test/test2.css',
removeStyle: 'all'
},
function(result) {
setTimeout(function() {
expect(result.html).to.eql(fs.readFileSync('./test/testRemoveAll.html', 'utf-8').toString());
done();
}, 20);
},
function(error) {
done(error);
}
);
});
});
15 changes: 15 additions & 0 deletions test/testRemoveAll.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>Wow</title>

</head>
<body>
<section id="main">
<div class="button">
<div class="button__text">
Text
</div>
</div>
</section>
</body>
</html>
17 changes: 17 additions & 0 deletions test/testRemoveAttrs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>Wow</title>
<style type="text/css">
html{ margin: 0 }
</style>
</head>
<body>
<section id="main">
<div class="button">
<div class="button__text">
Text
</div>
</div>
</section>
</body>
</html>
15 changes: 15 additions & 0 deletions test/testRemoveTag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>Wow</title>

</head>
<body style="background: #fff;">
<section id="main" style="width: 800px; margin: auto;">
<div class="button" style="border: 1px solid #000;">
<div class="button__text">
Text
</div>
</div>
</section>
</body>
</html>