Skip to content

Commit

Permalink
Chore: update rcs-core
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Dec 23, 2018
1 parent db51c35 commit bc4778a
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 50 deletions.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -84,7 +84,7 @@ postcss([ require('postcss-simple-vars')({ replaceKeyframes: true }) ])
```

- [replaceKeyframes](#replacekeyframes)
- [ignoreAttributeSelector](#ignoreattributeselector)
- [ignoreAttributeSelectors](#ignoreattributeselectors)
- [prefix](#prefix)
- [suffix](#suffix)

Expand Down Expand Up @@ -126,7 +126,7 @@ will be processed to:
}
```

### ignoreAttributeSelector
### ignoreAttributeSelectors

If set to `true` it does not care about attribute selectors. If set to `false` the attribute selector `[class$="lector"]` will not rename `.selector` as it the class ends with `lector`.

Expand All @@ -136,7 +136,7 @@ Default: `false`

Example:

**{ ignoreAttributeSelector: false }**
**{ ignoreAttributeSelectors: false }**
```css
.move[class$="lector"] {}
.selector {}
Expand All @@ -148,7 +148,7 @@ will be process to:
.selector {}
```

**{ ignoreAttributeSelector: true }**
**{ ignoreAttributeSelectors: true }**
```css
.move[class$="lector"] {}
.selector {}
Expand All @@ -162,44 +162,44 @@ will be process to:

### prefix

Prefixes every selectors and attribute selectors (if `ignoreAttributeSelector` is `false`).
Prefixes every selectors and attribute selectors (if `ignoreAttributeSelectors` is `false`).

Type: `string`

Default: `''`

Example:
```css
// { ignoreAttributeSelector: false, prefix: 'pre-' }
// { ignoreAttributeSelectors: false, prefix: 'pre-' }
.move[class^="sel"] {}
.selector {}
```

will be process to:
```css
// { ignoreAttributeSelector: false, prefix: 'pre-' }
// { ignoreAttributeSelectors: false, prefix: 'pre-' }
.pre-a[class^="pre-sel"] {}
.pre-selector {}
```

### suffix

Suffixes every selectors and attribute selectors (if `ignoreAttributeSelector` is `false`).
Suffixes every selectors and attribute selectors (if `ignoreAttributeSelectors` is `false`).

Type: `string`

Default: `''`

Example:
```css
// { ignoreAttributeSelector: false, suffix: '-suf' }
// { ignoreAttributeSelectors: false, suffix: '-suf' }
.move[class$="lector"] {}
.selector {}
```

will be process to:
```css
// { ignoreAttributeSelector: false, suffix: '-suf' }
// { ignoreAttributeSelectors: false, suffix: '-suf' }
.a-suf[class$="lector-suf"] {}
.selector-suf {}
```
31 changes: 12 additions & 19 deletions index.js
Expand Up @@ -3,7 +3,7 @@ import rcs from 'rcs-core';

const replaceCssSelector = (match) => {
const result = rcs.selectorLibrary.get(match, {
isSelectors: true,
addSelectorType: true,
});

return result;
Expand All @@ -13,30 +13,22 @@ const postcssRcs = postcss.plugin('rcs', (options = {}) => (
(root) => {
const data = root.toString();

// fill libraries
if (!options.ignoreAttributeSelector) {
rcs.selectorLibrary.setAttributeSelector(data.match(rcs.replace.regex.attributeSelectors));
}

if (options.replaceKeyframes) {
rcs.keyframesLibrary.fillLibrary(data);
}

rcs.selectorLibrary.fillLibrary(data, options);
rcs.fillLibraries(data, options);

// do a regex of all setted attributes
const regexOfAllSelectors = rcs.selectorLibrary.getAll({
origValues: true,
addSelectorType: true,
regexCss: true,
isSelectors: true,
});

root.walkRules((rule) => {
// * --------------------------- *
// * replace selector attributes *
// * --------------------------- *
if (!options.ignoreAttributeSelector) {
rule.selector = rule.selector.replace(rcs.replace.regex.attributeSelectors, (match) => {
const replaceEscapes = /\\/g;

if (!options.ignoreAttributeSelectors) {
rule.selector = rule.selector.replace(replaceEscapes, '').replace(rcs.replace.regex.attributeSelectors, (match) => {
const re = new RegExp(rcs.replace.regex.attributeSelectors);
const exec = re.exec(match);
const stringChar = exec[3].charAt(0);
Expand Down Expand Up @@ -64,7 +56,7 @@ const postcssRcs = postcss.plugin('rcs', (options = {}) => (
// * ----------------- *
// * replace selectors *
// * ----------------- *
rule.selector = rule.selector.replace(regexOfAllSelectors, replaceCssSelector);
rule.selector = rule.selector.replace(replaceEscapes, '').replace(regexOfAllSelectors, replaceCssSelector);
});

// * ----------------- *
Expand All @@ -77,7 +69,7 @@ const postcssRcs = postcss.plugin('rcs', (options = {}) => (
return;
}

atRule.params = atRule.params.replace(/[a-zA-Z-_]+/g, match => rcs.keyframesLibrary.get(match));
atRule.params = atRule.params.replace(/[a-zA-Z-_]+/g, match => rcs.keyframesLibrary.get(match, { addSelectorType: true }));
});

// animation | animation-name
Expand All @@ -86,8 +78,9 @@ const postcssRcs = postcss.plugin('rcs', (options = {}) => (
return;
}

decl.value = decl.value.replace(rcs.replace.regex.matchFirstWord, match => (
rcs.keyframesLibrary.get(match)
// match the first word
decl.value = decl.value.replace(/[a-zA-Z0-9-_]+/, match => (
rcs.keyframesLibrary.get(match, { addSelectorType: true })
));
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -42,7 +42,7 @@
"homepage": "https://github.com/jpeer264/postcss-rcs#readme",
"dependencies": {
"postcss": "^6.0.1",
"rcs-core": "^0.11.0"
"rcs-core": "^2.4.3"
},
"devDependencies": {
"ava": "^0.18.2",
Expand Down
38 changes: 24 additions & 14 deletions test.js
Expand Up @@ -5,27 +5,20 @@ import rcs from 'rcs-core';
import postcssRcs from './index';

const replaceKeyframes = postcssRcs({ replaceKeyframes: true });
const ignoreAttributeSeletor = postcssRcs({ ignoreAttributeSelector: true });
const ignoreAttributeSeletor = postcssRcs({ ignoreAttributeSelectors: true });
const prefix = postcssRcs({ prefix: 'pre-' });
const suffix = postcssRcs({ suffix: '-suf' });
const presuf = postcssRcs({ prefix: 'pre-', suffix: '-suf' });
const keyframesAndAttributeSelectors = postcssRcs({
ignoreAttributeSelector: true,
ignoreAttributeSelectors: true,
replaceKeyframes: true,
});

test.beforeEach(() => {
// reset counter and selectors for tests
rcs.selectorLibrary.selectors = {};
rcs.selectorLibrary.attributeSelectors = {};
rcs.selectorLibrary.compressedSelectors = {};
rcs.selectorLibrary.excludes = [];

rcs.keyframesLibrary.excludes = [];
rcs.keyframesLibrary.keyframes = {};
rcs.keyframesLibrary.compressedKeyframes = {};

rcs.nameGenerator.resetCountForTests();
rcs.nameGenerator.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
rcs.nameGenerator.reset();
rcs.selectorLibrary.reset();
rcs.keyframesLibrary.reset();
});

test('generell rename test', (t) => {
Expand Down Expand Up @@ -191,7 +184,7 @@ test('prefix suffix and selector attributes', (t) => {
}
`;
const expectedResult = `
.pre-a-suf.pre-selector-suf {
.pre-a-suf.pre-tctor-suf {
border: 1px solid black;
}
Expand Down Expand Up @@ -280,3 +273,20 @@ test('ignore attribute selector and set keyframes', (t) => {

t.is(result.css, expectedResult);
});

test('replace escaped selectors', (t) => {
const input = `
.main.selector\\:test:after {
border: 1px solid black;
}
`;
const expectedResult = `
.a.b:after {
border: 1px solid black;
}
`;

const result = postcss([ignoreAttributeSeletor]).process(input);

t.is(result.css, expectedResult);
});

0 comments on commit bc4778a

Please sign in to comment.