Skip to content

Commit 8ef604f

Browse files
jonathantnealromainmenke
authored andcommitted
Update version
Update index.js: use postcss-selector-parser Update package.json: use postcss-selector-parser and bump version Update tests.js: postcss-selector-parser preserves selector separators
1 parent b7182b6 commit 8ef604f

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
var postcss = require('postcss');
2+
var postcssSelectorParser = require('postcss-selector-parser');
23

34
module.exports = postcss.plugin('postcss-pseudo-class-any-link', function (opts) {
4-
var match = new RegExp(':' + (opts && opts.prefix ? '-' + opts.prefix + '-' : '') + 'any-link\\b', 'g');
5+
var pseudoValue = ':' + (opts && opts.prefix ? '-' + opts.prefix + '-' : '') + 'any-link';
6+
var pseudoFallbackValues = [':link', ':visited'];
7+
8+
function eachRule(rule) {
9+
rule.selector = postcssSelectorParser(function (selectors) {
10+
selectors.each(function (selector, index) {
11+
var originalIndex = index;
12+
13+
pseudoFallbackValues.forEach(function (pseudoFallbackValue) {
14+
var clone = selector.clone();
15+
16+
clone.eachPseudo(function (pseudo) {
17+
if (pseudo.value === pseudoValue) {
18+
pseudo.value = pseudoFallbackValue;
19+
20+
selectors.nodes.splice(++index, 0, clone);
21+
}
22+
});
23+
});
24+
25+
if (originalIndex !== index) {
26+
selector.removeSelf();
27+
}
28+
});
29+
}).process(rule.selector).result;
30+
}
531

632
return function (css) {
7-
css.eachRule(function (rule) {
8-
if (match.test(rule.selector)) {
9-
rule.selector = ['link', 'visited'].map(function (replacement) {
10-
return rule.selector.replace(match, ':' + replacement);
11-
}).join(', ');
12-
}
13-
});
33+
css.eachRule(eachRule);
1434
};
1535
});

plugins/postcss-pseudo-class-any-link/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-pseudo-class-any-link",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Use the proposed :any-link pseudo-class in CSS",
55
"keywords": ["postcss", "css", "postcss-plugin", "link", "visited", "any-link", "a", "area", "hyperlink", "href"],
66
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
@@ -20,7 +20,8 @@
2020
"chai": "^2.3.0",
2121
"gulp": "^3.8.11",
2222
"gulp-eslint": "^0.12.0",
23-
"gulp-mocha": "^2.0.1"
23+
"gulp-mocha": "^2.0.1",
24+
"postcss-selector-parser": "^1.0.0"
2425
},
2526
"scripts": {
2627
"test": "gulp"

plugins/postcss-pseudo-class-any-link/test/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var test = function (input, output, opts, done) {
1515

1616
describe('postcss-pseudo-class-any-link', function () {
1717
it(':any-link transforms to :link and :visited', function (done) {
18-
test('ul a:any-link > span { background: yellow; }', 'ul a:link > span, ul a:visited > span { background: yellow; }', {}, done);
18+
test('ul a:any-link > span { background: yellow; }', 'ul a:link > span,ul a:visited > span { background: yellow; }', {}, done);
1919
});
2020

2121
it(':any-link remains :any-link { prefix: "foo" }', function (done) {
@@ -25,7 +25,7 @@ describe('postcss-pseudo-class-any-link', function () {
2525
});
2626

2727
it(':-foo-any-link transforms to :link and :visited { prefix: "foo" }', function (done) {
28-
test('ul a:-foo-any-link > span { background: yellow; }', 'ul a:link > span, ul a:visited > span { background: yellow; }', {
28+
test('ul a:-foo-any-link > span { background: yellow; }', 'ul a:link > span,ul a:visited > span { background: yellow; }', {
2929
prefix: 'foo'
3030
}, done);
3131
});

0 commit comments

Comments
 (0)