Socket
Socket
Sign inDemoInstall

filter-css

Package Overview
Dependencies
70
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.2 to 1.0.0

113

cli.js

@@ -0,83 +1,88 @@

#!/usr/bin/env node
'use strict';
var meow = require('meow');
var _ = require('lodash');
var stdin = require('get-stdin');
var updateNotifier = require('update-notifier');
var filterCss = require('./');
var pkg = require('./package.json');
var ok;
var help = [
'Usage: filtercss <input> [<option>]',
'',
'Options:',
' -i, --ignore RegExp, selector @type to ignore',
' -S, --skipSelectors Don\'t match selectors',
' -T, --skipTypes Don\'t match types',
' -P, --skipDeclarationProperties Don\'t match declaration properties',
' -V, --skiphDeclarationValues Don\'t match declaration vakues',
' -M, --skipMedia Don\'t match media'
].join('\n');
const meow = require('meow');
const isString = require('lodash.isstring');
const isRegExp = require('lodash.isregexp');
const stdin = require('get-stdin');
const filterCss = require('.');
const help = `
Usage: filtercss <input> [<option>]
Options:
-i, --ignore RegExp, selector @type to ignore
-S, --skipSelectors Don't match selectors
-T, --skipTypes Don't match types
-P, --skipDeclarationProperties Don't match declaration properties
-V, --skipDeclarationValues Don't match declaration values
-M, --skipMedia Don't match media
`;
var cli = meow({help: help}, {alias: {
i: 'ignore',
S: 'skipSelectors',
T: 'skipTypes',
P: 'skipDeclarationProperties',
V: 'skiphDeclarationValues',
M: 'skipMedia'
}});
const cli = meow(
help, {
flags: {
ignore: {
alias: 'i'
},
skipSelectors: {
type: 'boolean',
alias: 'S'
},
skipTypes: {
type: 'boolean',
alias: 'T'
},
skipDeclarationProperties: {
type: 'boolean',
alias: 'P'
},
skipDeclarationValues: {
type: 'boolean',
alias: 'V'
},
skipMedia: {
type: 'boolean',
alias: 'M'
}
}
}
);
if (cli.flags['update-notifier'] !== false) {
updateNotifier({pkg: pkg}).notify();
}
function go(data) {
if (!data) {
cli.showHelp();
}
function go(data) {
ok = true;
if (_.isString(cli.flags.ignore) || _.isRegExp(cli.flags.ignore)) {
if (isString(cli.flags.ignore) || isRegExp(cli.flags.ignore)) {
cli.flags.ignore = [cli.flags.ignore];
}
var ignores = _.map(cli.flags.ignore || [], function(ignore) {
const ignores = (cli.flags.ignore || []).map(ignore => {
// check regex
var match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(match[1],match[2]);
return new RegExp(match[1], match[2]);
}
return ignore;
});
if (!data) {
cli.showHelp();
return;
}
var diff = filterCss(data,ignores, {
const diff = filterCss(data, ignores, {
matchSelectors: !cli.flags.skipSelectors,
matchTypes: !cli.flags.skipTypes,
matchDeclarationProperties: !cli.flags.skipDeclarationProperties,
matchDeclarationValues: !cli.flags.skiphDeclarationValues,
matchDeclarationValues: !cli.flags.skipDeclarationValues,
matchMedia: !cli.flags.skipMedia
});
console.log(diff);
process.exit();
}
function die() {
if (ok) {
return;
}
cli.showHelp();
}
if (cli.input[0]) {
go(cli.input[0]);
} else {
// get stdin
stdin().then(go);
setTimeout(die, 200);
}
'use strict';
var _ = require('lodash');
var fs = require('fs');
var css = require('css');
var _default = {
const fs = require('fs');
const css = require('css');
const defaults = require('lodash.defaults');
const isFunction = require('lodash.isfunction');
const isRegExp = require('lodash.isregexp');
const reject = require('lodash.reject');
const result = require('lodash.result');
const _default = {
matchSelectors: true,

@@ -18,3 +23,10 @@ matchTypes: true,

function getValue(element, pluck) {
if (pluck) {
return result(element, pluck);
}
return element;
}
/**

@@ -29,12 +41,5 @@ * Identify ignored selectors

function _matcher(ignores, identifier, node, pluck) {
function getValue(element) {
if (pluck) {
return _.result(element, pluck);
}
return element;
}
return function (element) {
for (var i = 0; i < ignores.length; ++i) {
if (_.isFunction(ignores[i]) && ignores[i](identifier, getValue(element), node || element)) {
return element => {
for (let i = 0; i < ignores.length; ++i) {
if (isFunction(ignores[i]) && ignores[i](identifier, getValue(element, pluck), node || element)) {
return true;

@@ -44,9 +49,11 @@ }

/* If ignore is RegExp and matches selector ... */
if (_.isRegExp(ignores[i]) && ignores[i].test(getValue(element))) {
if (isRegExp(ignores[i]) && ignores[i].test(getValue(element, pluck))) {
return true;
}
if (ignores[i] === getValue(element)) {
if (ignores[i] === getValue(element, pluck)) {
return true;
}
}
return false;

@@ -56,3 +63,2 @@ };

/**

@@ -65,9 +71,7 @@ *

function reduceRules(ignore, opts) {
const matcher = (...args) => _matcher(ignore, ...args);
var matcher = _.partial(_matcher, ignore);
return function reducer(rules, rule) {
// check if whole type is ignored
if (opts.matchTypes && matcher('type', rule)('@' + rule.type)) {
if (opts.matchTypes && matcher('type', rule)(`@${rule.type}`)) {
return rules;

@@ -82,6 +86,5 @@ }

rule.rules = (rule.rules || []).reduce(reducer, []);
rule.rules = _.reduce(rule.rules || [], reducer, []);
if (_.size(rule.rules)) {
if (rule.rules.length > 0) {
rules.push(rule);

@@ -92,9 +95,9 @@ }

if (opts.matchSelectors) {
rule.selectors = _.reject(rule.selectors || [], matcher('selector', rule));
rule.selectors = reject(rule.selectors || [], matcher('selector', rule));
}
if (_.size(rule.selectors)) {
if (rule.selectors.length > 0) {
// check declaration property
if (opts.matchDeclarationProperties) {
rule.declarations = _.reject(rule.declarations || [], matcher('declarationProperty', undefined, 'property'));
rule.declarations = reject(rule.declarations || [], matcher('declarationProperty', undefined, 'property'));
}

@@ -104,7 +107,7 @@

if (opts.matchDeclarationValues) {
rule.declarations = _.reject(rule.declarations || [], matcher('declarationValue', undefined, 'value'));
rule.declarations = reject(rule.declarations || [], matcher('declarationValue', undefined, 'value'));
}
// add rule if something's left
if (_.size(rule.declarations)) {
if (rule.declarations.length > 0) {
rules.push(rule);

@@ -121,19 +124,17 @@ }

function api(stylesheet, ignore, opts) {
opts = defaults(opts || {}, _default);
opts = _.defaults(opts || {}, _default);
if (!_.isArray(ignore)) {
if (!Array.isArray(ignore)) {
ignore = [ignore];
}
var sheet;
let sheet;
try {
sheet = css.parse(read(stylesheet));
} catch (err) {
} catch (error) {
sheet = css.parse(stylesheet);
}
sheet.stylesheet.rules = _.reduce(sheet.stylesheet.rules, reduceRules(ignore, opts), []);
sheet.stylesheet.rules = sheet.stylesheet.rules.reduce(reduceRules(ignore, opts), []);
return css.stringify(sheet);

@@ -140,0 +141,0 @@ }

{
"name": "filter-css",
"version": "0.1.2",
"version": "1.0.0",
"description": "Filter CSS rules",

@@ -13,6 +13,9 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=6"
},
"scripts": {
"test": "jshint . && mocha test/*.js"
"lint": "npm run xo",
"xo": "xo",
"mocha": "mocha",
"test": "npm run xo && npm run mocha"
},

@@ -32,13 +35,22 @@ "files": [

"dependencies": {
"css": "^2.2.0",
"get-stdin": "^5.0.1",
"lodash": "^4.13.1",
"meow": "^3.1.0",
"update-notifier": "^0.7.0"
"css": "^2.2.4",
"get-stdin": "^6.0.0",
"lodash.defaults": "^4.2.0",
"lodash.isfunction": "^3.0.9",
"lodash.isregexp": "^4.0.1",
"lodash.isstring": "^4.0.1",
"lodash.reject": "^4.6.0",
"lodash.result": "^4.5.2",
"meow": "^5.0.0"
},
"devDependencies": {
"chai": "^3.5.0",
"jshint": "^2.7.0",
"mocha": "^2.2.4"
"chai": "^4.2.0",
"mocha": "^6.2.2",
"xo": "^0.24.0"
},
"xo": {
"rules": {
"capitalized-comments": "off"
}
}
}

@@ -1,46 +0,43 @@

# filter-css [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Build status][appveyor-image]][appveyor-url] [![Dependency Status][depstat-image]][depstat-url]
# filter-css [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![dependencies Status][depstat-image]][depstat-url] [![devDependencies Status Status][devdepstat-image]][devdepstat-url]
Filter CSS rules
## Install
```shell
npm install --save filter-css
```
$ npm install --save filter-css
```
## Usage
```js
var filterCss = require('filter-css');
var filtered = filterCss(<input>, <pattern>, <options>);
const filterCss = require('filter-css');
const filtered = filterCss(<input>, <pattern>, <options>);
```
#### Input
### Input
*Required*
Type: `String`
* *Required*
* Type: `String`
Can be a path to the CSS file or a raw CSS string
Can be a path to the CSS file or a raw CSS string.
#### Pattern
### Pattern
*Required*
Type `String`,`RegExp`, `Function` or an `Array` containing it.
Patterns used to discard specific parts of the CSS.
The function is invoked with three arguments (context, value, node).
* `context` Current matching context. Could be one of `['type','media','selector','declarationProperty','declarationValue']`.
* `value` Current value.
* `node` The currently processed AST node generated by [`css`](https://github.com/reworkcss/css).
* *Required*
* Type `String`,`RegExp`, `Function` or an `Array` containing it.
Return true if the element should be discarded.
Patterns used to discard specific parts of the CSS.
The function is invoked with three arguments (`context`, `value`, `node`).
#### Options
* `context`: Current matching context. Could be one of `['type', 'media', 'selector', 'declarationProperty', 'declarationValue']`.
* `value`: Current value.
* `node`: The currently processed AST node generated by [`css`](https://github.com/reworkcss/css).
Per default `filter-css` will be applied to all parts of the CSS. This behavior can be customized by disabling specific matchers
Return true if the element should be discarded.
### Options
Per default `filter-css` will be applied to all parts of the CSS. This behavior can be customized by disabling specific matchers.
| Name | Type | Description |

@@ -54,3 +51,2 @@ | -------------------------- | --------- |-------------- |

## Examples

@@ -60,9 +56,9 @@

.bigBackground {
width: 100%;
height: 100%;
background-image: url('some/big/image.png');
width: 100%;
height: 100%;
background-image: url('some/big/image.png');
}
@font-face {
font-family: 'My awesome font';
font-family: 'My awesome font';
}

@@ -76,5 +72,5 @@

```js
var filterCss = require('filter-css');
const filterCss = require('filter-css');
filterCss('test/fixtures/test.css',[/url\(/,'@font-face',/print/]);
filterCss('test/fixtures/test.css', [/url\(/,'@font-face',/print/]);
```

@@ -84,29 +80,28 @@

.bigBackground {
width: 100%;
height: 100%;
width: 100%;
height: 100%;
}
```
#### Remove all media queries
### Remove all media queries
```js
var filterCss = require('filter-css');
const filterCss = require('filter-css');
filterCss('test/fixtures/test.css',/.*/,{
matchSelectors: false,
matchTypes: false,
matchDeclarationProperties: false,
matchDeclarationValues: false,
matchMedia: true
filterCss('test/fixtures/test.css', /.*/, {
matchSelectors: false,
matchTypes: false,
matchDeclarationProperties: false,
matchDeclarationValues: false,
matchMedia: true
});
```
### Using a function matcher
#### Using a function matcher
```js
var filterCss = require('filter-css');
const filterCss = require('filter-css');
filterCss('test/fixtures/test.css',function(context, value, node) {
return context === 'declarationValue' && value === "url('some/big/image.png')"
filterCss('test/fixtures/test.css', (context, value, node) => {
return context === 'declarationValue' && value === "url('some/big/image.png')"
});

@@ -116,3 +111,4 @@

#### Complete Example
### Complete Example
```js

@@ -131,10 +127,12 @@ filterCss('test/fixtures/test.css', {

```shell
$ cat test/fixture/test.css | filtercss --ignore @font-face
cat test/fixture/test.css | filtercss --ignore @font-face
```
You can also pass in the file as an option.
```shell
$ filtercss test/fixture/test.css --ignore @font-face
filtercss test/fixture/test.css --ignore @font-face
```
#### CLI options
### CLI options

@@ -148,11 +146,11 @@ See `filtercss --help` for a full list of options.

[npm-url]: https://npmjs.org/package/filter-css
[npm-image]: https://badge.fury.io/js/filter-css.svg
[npm-image]: https://img.shields.io/npm/v/filter-css.svg
[travis-url]: https://travis-ci.org/bezoerb/filter-css
[travis-image]: https://travis-ci.org/bezoerb/filter-css.svg?branch=master
[ci-url]: https://github.com/bezoerb/filter-css/actions?workflow=Tests
[ci-image]: https://github.com/bezoerb/filter-css/workflows/Tests/badge.svg
[appveyor-url]: https://ci.appveyor.com/project/bezoerb/filter-css/branch/master
[appveyor-image]: https://ci.appveyor.com/api/projects/status/xypps7lx2usx3akp/branch/master?svg=true
[depstat-url]: https://david-dm.org/bezoerb/filter-css
[depstat-image]: https://img.shields.io/david/bezoerb/filter-css.svg
[depstat-url]: https://david-dm.org/bezoerb/filter-css
[depstat-image]: https://david-dm.org/bezoerb/filter-css.svg
[devdepstat-url]: https://david-dm.org/bezoerb/filter-css?type=dev
[devdepstat-image]: https://img.shields.io/david/dev/bezoerb/filter-css.svg
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc