Skip to content

Commit

Permalink
style: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Apr 4, 2019
1 parent 3243d30 commit cd6c381
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 31 deletions.
27 changes: 21 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import postcss from 'postcss';

const filter = (filter, attributeSelector) => new RegExp(filter.map(attribut => `^\\[${attribut}`).join('|')).test(attributeSelector);
const filter = (filter, attributeSelector) =>
new RegExp(filter.map(attribut => `^\\[${attribut}`).join('|')).test(
attributeSelector
);

export default postcss.plugin('postcss-attribute-selector-prefix', options => {
return nodes => {
Expand All @@ -10,17 +13,29 @@ export default postcss.plugin('postcss-attribute-selector-prefix', options => {
return match;
}

if (options.filter !== undefined && filter(options.filter, match) === false) {
if (
options.filter !== undefined &&
filter(options.filter, match) === false
) {
return match;
}

if (options.ignore !== undefined && filter(options.ignore, match) === true) {
if (
options.ignore !== undefined &&
filter(options.ignore, match) === true
) {
return match;
}

return match.replace(/(\[.*?="?)(.*?)("?])/, (match, before, requireds, after) => {
return `${before}${requireds.split(' ').map(required => options.prefix + required).join(' ')}${after}`;
});
return match.replace(
/(\[.*?="?)(.*?)("?])/,
(match, before, requireds, after) => {
return `${before}${requireds
.split(' ')
.map(required => options.prefix + required)
.join(' ')}${after}`;
}
);
});
});
};
Expand Down
70 changes: 45 additions & 25 deletions test/test-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import test from 'ava';
import postcss from 'postcss';
import plugin from '../src';

const processing = (inputCSS, options) => postcss([plugin(options)]).process(inputCSS).css;
const processing = (inputCSS, options) =>
postcss([plugin(options)]).process(inputCSS).css;

test('processing not options and not attribute selector', t => {
const expected = '.class, .test { color:red; }';
Expand All @@ -17,8 +18,10 @@ test('processing options prefix and not attribute selector', t => {
});

test('processing options prefix multiple', t => {
const expected = '.class, [type="test-text"], [class*="test-lorem-1 test-lorem-2"], [id^="test-myID"] { color:red; }';
const fixtures = '.class, [type="text"], [class*="lorem-1 lorem-2"], [id^="myID"] { color:red; }';
const expected =
'.class, [type="test-text"], [class*="test-lorem-1 test-lorem-2"], [id^="test-myID"] { color:red; }';
const fixtures =
'.class, [type="text"], [class*="lorem-1 lorem-2"], [id^="myID"] { color:red; }';
t.is(processing(fixtures, {prefix: 'test-'}), expected);
});

Expand All @@ -29,46 +32,63 @@ test('processing options prefix and attribute selector', t => {
});

test('processing options prefix, filter and attribute selector', t => {
const expected = '.class, [type="text"], [class*="test-lorem"] { color:red; }';
const expected =
'.class, [type="text"], [class*="test-lorem"] { color:red; }';
const fixtures = '.class, [type="text"], [class*="lorem"] { color:red; }';
t.is(processing(fixtures, {
prefix: 'test-',
filter: ['class']
}), expected);
t.is(
processing(fixtures, {
prefix: 'test-',
filter: ['class']
}),
expected
);
});

test('processing options prefix, filter and attribute selector witch indentical class filter', t => {
const expected = '.class, [type="text"], [alt*="class"] { color:red; }';
const fixtures = '.class, [type="text"], [alt*="class"] { color:red; }';
t.is(processing(fixtures, {
prefix: 'test-',
filter: ['class']
}), expected);
t.is(
processing(fixtures, {
prefix: 'test-',
filter: ['class']
}),
expected
);
});

test('processing options filter and attribute selector', t => {
const expected = '.class, [type="text"], [class*="lorem"] { color:red; }';
const fixtures = '.class, [type="text"], [class*="lorem"] { color:red; }';
t.is(processing(fixtures, {
filter: ['class']
}), expected);
t.is(
processing(fixtures, {
filter: ['class']
}),
expected
);
});

test('processing options prefix, filter, ignore and attribute selector', t => {
const expected = '.class, [type="text"], [class*="lorem"] { color:red; }';
const fixtures = '.class, [type="text"], [class*="lorem"] { color:red; }';
t.is(processing(fixtures, {
prefix: 'test',
filter: ['class'],
ignore: ['class']
}), expected);
t.is(
processing(fixtures, {
prefix: 'test',
filter: ['class'],
ignore: ['class']
}),
expected
);
});

test('processing options prefix, ignore and attribute selector', t => {
const expected = '.class, [type="text"], [class*="test-lorem"] { color:red; }';
const expected =
'.class, [type="text"], [class*="test-lorem"] { color:red; }';
const fixtures = '.class, [type="text"], [class*="lorem"] { color:red; }';
t.is(processing(fixtures, {
prefix: 'test-',
ignore: ['type']
}), expected);
t.is(
processing(fixtures, {
prefix: 'test-',
ignore: ['type']
}),
expected
);
});

0 comments on commit cd6c381

Please sign in to comment.