Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knockout.js support and improved obfuscation. #31

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Caleb Brewer
Copyright (c) 2017 Davis Miculis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# This project is looking for a maintainer
I am no longer maintaining this repository and would be happy to transfer it to someone who still actively uses it. Respond to the [maintainer issue](https://github.com/cazzer/gulp-selectors/issues/26) if you are interested in taking ownership. In case you are interested, I use [css-modules](https://github.com/css-modules/css-modules) now instead.

# gulp-selectors
[![Build Status][travis-image]][travis-url] [![Code Climate][cc-image]][cc-url] [![Test Coverage][coverage-image]][coverage-url] [![NPM Version][npm-image]][npm-url]

> Minify those pesky selector names down to nothing with this fancy gulp plugin. Minified selectors will be applied consistently across all files piped into it.

Expand All @@ -13,7 +9,7 @@ Input | Output
`#an-id { ... }` |`#a { ... }`
`<div class="class-name"> ... </div>` |`<div class="a"> ... </div>`

*You're like: `.some-super-descriptive-selector-name {...}`, and it's like: `.a {...}`*
*Converts `.some-super-descriptive-selector-name {...}` to `.a {...}`*

## Usage

Expand All @@ -33,7 +29,7 @@ You can also pass some options into run:

` gs.run(processors, ignores)`

CSS and HTML files are processed well by default, just pass in your glob of files and all classes and IDs will be reduced to a minified form. Of course you can use it for some more specific functions if you like. See the included [sample gulpfile](https://github.com/calebthebrewer/gulp-selectors/blob/master/test/example/gulpfile.js) for a full example of how to effectively use gulp-selectors in your gulp workflow.
CSS and HTML files are processed well by default, just pass in your glob of files and all classes and IDs will be reduced to a minified form. Of course you can use it for some more specific functions if you like.

### Defaults

Expand Down Expand Up @@ -112,13 +108,3 @@ libraries
classes: ['hidden', 'active']
}
```


[travis-url]: https://travis-ci.org/calebthebrewer/gulp-selectors
[travis-image]: https://travis-ci.org/calebthebrewer/gulp-selectors.svg?branch=master
[cc-image]: https://codeclimate.com/github/calebthebrewer/gulp-selectors/badges/gpa.svg
[cc-url]: https://codeclimate.com/github/calebthebrewer/gulp-selectors
[coverage-image]: https://codeclimate.com/github/calebthebrewer/gulp-selectors/badges/coverage.svg
[coverage-url]: https://codeclimate.com/github/calebthebrewer/gulp-selectors
[npm-image]: https://badge.fury.io/js/gulp-selectors.svg
[npm-url]: http://badge.fury.io/js/gulp-selectors
55 changes: 44 additions & 11 deletions lib/processors/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,50 @@ var expressions = require('../utils/expressions');
* @returns {string} Minified file
*/
module.exports = function (file, classLibrary, idLibrary) {
return file.replace(expressions.elementAttribute, function(attributes) {

file = file.replace(expressions.elementAttribute, function(attributes) {
var attribute = attributes.split('=');
return attribute[0] + '=' + attribute[1]
.replace(expressions.selectorName, function(selectorName) {
switch (attribute[0]) {
case 'id':
case 'for':
return idLibrary.get(selectorName);
default: //class
return classLibrary.get(selectorName);
}
});
var attr = attribute[0];
var val = attribute[1];

val = val.replace(expressions.selectorName, function(selectorName) {
switch (attr) {
case 'id':
case 'for':
case 'aria-labelledby':
case 'href':
return idLibrary.get(selectorName);
default: //classes
return classLibrary.get(selectorName);
}
});

return attr + '=' + val;
});

// Knockout.js
classLibrary.getFullNames().forEach(function(selector) {

file = file.replace(expressions.knockoutAttribute, function() {
var match = arguments[0],
bind = arguments[4];

if (bind.match(expressions.knockoutClassString(selector))) {
return match.replace(bind, function() {
return bind.replace(expressions.knockoutClassString(selector), function (match) {
return match.replace(expressions.quoteSelector, function (match) {
return match.replace(expressions.knockoutClassString(selector, true), function(matched) {
return matched.replace(selector, classLibrary.get(selector));
});
});
});
});
}

return match;
});

});

return file;
};
14 changes: 10 additions & 4 deletions lib/processors/js-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ var expressions = require('../utils/expressions');
module.exports = function(file, classLibrary, idLibrary) {

classLibrary.getFullNames().forEach(function(selector) {
file = file.replace(expressions.jsString(selector), function() {
return "'" + classLibrary.get(selector) + "'";
file = file.replace(expressions.jsString(selector), function(text) {
return text.replace(selector, classLibrary.get(selector));
});
file = file.replace(expressions.classString(selector), function (text) {
return text.replace(selector, classLibrary.get(selector));
});
});

idLibrary.getFullNames().forEach(function(selector) {
file = file.replace(expressions.jsString(selector), function() {
return "'" + idLibrary.get(selector) + "'";
file = file.replace(expressions.jsString(selector), function(text) {
return text.replace(selector, idLibrary.get(selector));
});
file = file.replace(expressions.idString(selector), function(text) {
return text.replace(selector, idLibrary.get(selector));
});
});

Expand Down
28 changes: 24 additions & 4 deletions lib/utils/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ module.exports = {
* - name="selector"
* - href="selector"
*/
elementAttribute: /(class|id|for|aria-labelledby)\s*=\s*["'](-?[_a-zA-Z]+[_\w-\s]*)["']/g,
elementAttribute: /(class|id|for|href|aria-labelledby|data-bind)\s*=\s*["']#?(-?[_a-zA-Z]+[_\w-\s]*)["']/g,
/**
* Matches ID Values
*
*
* All values can be single values or lists and lists can be either comma separated (each val is surrounded by single or double quotes OR a space separated list with not internal quotes (only quotes at beginning and end).
* .getElementById
* .getElementById
* .id
* $ Only matches values with "#"
* jQuery Only matches values with "#"
Expand Down Expand Up @@ -94,5 +94,25 @@ module.exports = {
*/
jsString: function(name) {
return new RegExp('[\'|"]' + name + '[\'|"]', 'g');
}
},
classString: function (name) {
return new RegExp('[\'|"]\.' + name + '[\'|"]', 'g');
},
idString: function (name) {
return new RegExp('[\'|"]#' + name + '[\'|"]', 'g');
},
/**
* Knockout.js
**/
knockoutAttribute: /(<([a-z]+\d*)(?:\s+(?!data-bind\s*=\s*)[a-z0-9\-]+(?:=(?:\"[^\"]*\"|\'[^\']*\'|[^>]*))?)*\s+)data-bind\s*=\s*(["'])([\s\S]*?)\3/gi,
// knockoutClass: /css\:\s\{+\s'(.*?)'+\s\}/gi,
knockoutClassString: function (name, short) {
name = name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
if (typeof short !== 'undefined' && short) {
// return new RegExp('(?:^|[^-])\\b('+name+')(?![\\w\\d-])', 'gi');
return new RegExp('[^-\\d\\w]\\b('+name+')\\b[^-\\d\\w]', 'gi');
}
return new RegExp('css\\:\\s\\{.*\\\'?.*?[^-\\d\\w]\\b('+name+')\\b[^-\\d\\w].*?\\\'?.*\\}', 'gi');
},
quoteSelector: new RegExp('([\"\'])(?:(?=(\\\\?))\\2.)*?\\1', 'gi'),
};
2 changes: 1 addition & 1 deletion lib/utils/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Library(ignores) {
}
} else if (!multimatch(name, _ignores).length) {
do {
shortname = generateShortname(size);
shortname = 'xx'+generateShortname(size);
size++;
} while (~_ignores.indexOf(shortname));

Expand Down
47 changes: 20 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{
"name": "gulp-selectors",
"description": "Minify CSS selectors.",
"version": "0.1.8",
"version": "0.1.10",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to bump the minor or major here instead of the patch, since this PR at least adds significant new functionality/support for JS. At most, the ownership change may be considered a breaking change to some.

"license": "MIT",
"main": "index.js",
"author": {
"name": "Caleb Brewer",
"email": "calebthebrewer@gmail.com"
"name": "Davis Miculis",
"email": "mindzhulis@gmail.com"
},
"dependencies": {
"event-stream": "~3.1.7",
"gulp-util": "~3.0.1",
"lodash": "~2.4.1",
"multimatch": "^1.0.1"
"event-stream": "~3.3.4",
"gulp-util": "~3.0.8",
"lodash": "~4.17.4",
"multimatch": "~2.1.0"
},
"devDependencies": {
"gulp": "~3.9.1"
},
"scripts": {

},
"homepage": "https://github.com/calebthebrewer/gulp-selectors",
"bugs": "https://github.com/calebthebrewer/gulp-selectors/issues",
"homepage": "https://github.com/davismiculis/gulp-selectors",
"bugs": "https://github.com/davismiculis/gulp-selectors/issues",
"keywords": [
"gulp",
"gulpplugin",
Expand All @@ -22,26 +30,11 @@
"css",
"selectors",
"styles",
"compress"
"compress",
"obfuscate"
],
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/calebthebrewer/gulp-selectors.git"
},
"readmeFilename": "README.md",
"devDependencies": {
"vows": "~0.7.0",
"jscoverage": "~0.5.6",
"istanbul": "^0.3.2",
"codeclimate-test-reporter": "0.0.4",
"gulp": "~3.8.11"
},
"scripts": {
"test": "vows --spec",
"coverage": "istanbul cover ./node_modules/vows/bin/vows --spec",
"codeclimate": "cat ./coverage/lcov.info | codeclimate",
"test-report": "npm run coverage && npm run codeclimate"
"url": "https://github.com/davismiculis/gulp-selectors.git"
}
}
11 changes: 0 additions & 11 deletions test/example/gulpfile.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/example/index.html

This file was deleted.

9 changes: 0 additions & 9 deletions test/example/script.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/example/style.css

This file was deleted.

54 changes: 0 additions & 54 deletions test/expressions/expressions.class-selector.test.js

This file was deleted.

Loading