Skip to content

Commit 00a53a8

Browse files
committed
add css attribute selector support
1 parent 96ce837 commit 00a53a8

File tree

8 files changed

+189
-45
lines changed

8 files changed

+189
-45
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Options:
132132
- prefix (string): prefix all triggered selectors. Default is `undefined`
133133
- suffix (string): suffix all triggered selectors. Default is `undefined`
134134
- preventRandomName (boolean): does not rename the selectors (good for prefixing/suffixing). Default is `false`
135+
- ignoreAttributeSelector (boolean): set to true it will not care about CSS attribute selectors e.g.: [class*="selector"]. Default is `false`
135136

136137
Example:
137138

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"glob": "^7.1.1",
3838
"json-extra": "^0.5.0",
3939
"lodash": "^4.17.4",
40-
"rcs-core": "^0.5.1"
40+
"rcs-core": "^0.6.0"
4141
},
4242
"devDependencies": {
4343
"chai": "^3.5.0",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.selector[class*="block"] {
2+
}
3+
4+
.block[class*="block"] {
5+
}
6+
7+
.class[id^="pre-"] {
8+
}
9+
10+
.another-class[class$="ctor"] {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.prefix-a-suffix[class*="block"] {
2+
}
3+
4+
.prefix-b-suffix[class*="block"] {
5+
}
6+
7+
.prefix-c-suffix[id^="pre-"] {
8+
}
9+
10+
.prefix-d-suffix[class$="ctor"] {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.prefix-selector-suffix[class*="block"] {
2+
}
3+
4+
.prefix-block-suffix[class*="block"] {
5+
}
6+
7+
.prefix-a-suffix[id^="prefix-pre-"] {
8+
}
9+
10+
.prefix-b-suffix[class$="ctor-suffix"] {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.selector[class*="block"] {
2+
}
3+
4+
.block[class*="block"] {
5+
}
6+
7+
.a[id^="pre-"] {
8+
}
9+
10+
.b[class$="ctor"] {
11+
}

test/index.spec.js

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ const resultsCwd = 'test/files/results';
1313
describe('app.js', () => {
1414
beforeEach(() => {
1515
// reset counter and selectors for tests
16+
rcs.selectorLibrary.excludes = [];
1617
rcs.selectorLibrary.selectors = {};
18+
rcs.selectorLibrary.attributeSelectors = {};
1719
rcs.selectorLibrary.compressedSelectors = {};
18-
rcs.selectorLibrary.excludes = [];
20+
1921
rcs.nameGenerator.resetCountForTests();
2022
});
2123

@@ -25,7 +27,7 @@ describe('app.js', () => {
2527

2628
describe('processing', () => {
2729
it('should process css files', done => {
28-
app.process('**/*.css', {
30+
app.process('**/style*.css', {
2931
collectSelectors: true,
3032
newPath: testCwd,
3133
cwd: fixturesCwd
@@ -71,7 +73,7 @@ describe('app.js', () => {
7173
// duplicated code from the test before
7274
// but another function - especially for css
7375
it('should process css files and prefix them', done => {
74-
app.processCss('**/*.css', {
76+
app.processCss('**/style*.css', {
7577
newPath: testCwd,
7678
cwd: fixturesCwd,
7779
prefix: 'prefixed-'
@@ -87,7 +89,7 @@ describe('app.js', () => {
8789
});
8890

8991
it('should process css files with processCss', done => {
90-
app.processCss('**/*.css', {
92+
app.processCss('**/style*.css', {
9193
newPath: testCwd,
9294
cwd: fixturesCwd
9395
}, (err, data) => {
@@ -108,7 +110,7 @@ describe('app.js', () => {
108110
});
109111

110112
it('should process css files without options', done => {
111-
app.processCss(fixturesCwd + '/**/*.css', (err, data) => {
113+
app.processCss(fixturesCwd + '/**/style*.css', (err, data) => {
112114
let newFile = fs.readFileSync('./rcs/' + fixturesCwd + '/style.css', 'utf8');
113115
let expectedFile = fs.readFileSync(resultsCwd + '/style.css', 'utf8');
114116

@@ -121,8 +123,46 @@ describe('app.js', () => {
121123
});
122124
});
123125

126+
it('should replace the selector attributes correctly', done => {
127+
app.processCss('css-attributes.css', {
128+
newPath: testCwd,
129+
cwd: fixturesCwd
130+
}, err => {
131+
expect(fs.readFileSync(testCwd + '/css-attributes.css', 'utf8')).to.equal(fs.readFileSync(resultsCwd + '/css-attributes.css', 'utf8'));
132+
133+
done();
134+
});
135+
});
136+
137+
it('should replace the selector attributes with pre and suffixes correctly', done => {
138+
app.processCss('css-attributes.css', {
139+
prefix: 'prefix-',
140+
suffix: '-suffix',
141+
newPath: testCwd,
142+
cwd: fixturesCwd
143+
}, err => {
144+
expect(fs.readFileSync(testCwd + '/css-attributes.css', 'utf8')).to.equal(fs.readFileSync(resultsCwd + '/css-attributes-pre-suffix.css', 'utf8'));
145+
146+
done();
147+
});
148+
});
149+
150+
it('should replace the selector attributes without caring about attribute selectors', done => {
151+
app.processCss('css-attributes.css', {
152+
prefix: 'prefix-',
153+
suffix: '-suffix',
154+
ignoreAttributeSelector: true,
155+
newPath: testCwd,
156+
cwd: fixturesCwd
157+
}, err => {
158+
expect(fs.readFileSync(testCwd + '/css-attributes.css', 'utf8')).to.equal(fs.readFileSync(resultsCwd + '/css-attributes-ignore.css', 'utf8'));
159+
160+
done();
161+
});
162+
});
163+
124164
it('should process css files and flatten the directories', done => {
125-
app.process('**/*.css', {
165+
app.process('**/style*.css', {
126166
collectSelectors: true,
127167
flatten: true,
128168
newPath: testCwd,
@@ -199,7 +239,7 @@ describe('app.js', () => {
199239

200240
describe('generating files', () => {
201241
beforeEach(done => {
202-
app.processCss('**/*.css', {
242+
app.processCss('**/style*.css', {
203243
newPath: testCwd,
204244
cwd: fixturesCwd
205245
}, (err, data) => {
@@ -341,7 +381,7 @@ describe('app.js', () => {
341381

342382
describe('load mapping', () => {
343383
beforeEach(done => {
344-
app.processCss('**/*.css', {
384+
app.processCss('**/style*.css', {
345385
newPath: testCwd,
346386
cwd: fixturesCwd
347387
}, (err, data) => {
@@ -410,7 +450,7 @@ describe('app.js', () => {
410450
});
411451

412452
it('should load from a filestring', done => {
413-
app.processCss('**/*.css', {
453+
app.processCss('**/style*.css', {
414454
newPath: testCwd,
415455
cwd: fixturesCwd
416456
}, (err, data) => {

0 commit comments

Comments
 (0)