Skip to content

Commit

Permalink
Merge pull request #15 from gucong3000/support_stylelint
Browse files Browse the repository at this point in the history
support for `stylelint`
  • Loading branch information
gucong3000 authored May 11, 2017
2 parents 7fb3783 + c8d35f0 commit 60c6410
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 42 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.stylelintrc*
.travis.yml
.vscode
.nyc_output
appveyor.yml
coverage
gulpfile.js
Expand Down
6 changes: 3 additions & 3 deletions lib/clearAtRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function clearAtRule (atRule) {
return;
}

if (atRule.name === name && !atRuleFixedParams) {
unprefixed = atRule;
} else {
if (atRule.name[0] === '-' || atRuleFixedParams) {
prefixedAtRule.push(atRule);
} else {
unprefixed = atRule;
}
});

Expand Down
17 changes: 4 additions & 13 deletions lib/clearDecl.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
'use strict';
const postcss = require('postcss');
const unprefixDecl = require('./unprefix').decl;
const rePrefix = require('./util').rePrefix;
const util = require('./util');
function walkDecl (decl) {
if (rePrefix.test(decl.prop) || /(^|,|\s)-\w+-.+/i.test(decl.value)) {
if (util.rePrefix.test(decl.prop) || /(^|,|\s)-\w+-.+/i.test(decl.value)) {
clearDecl(decl);
}
}

function clearDecl (decl) {
const rule = decl.parent;
const prop = postcss.vendor.unprefixed(decl.prop);
const prefixedDecls = [];
let unprefixed;
let lastUnprefixed;

rule.walkDecls(new RegExp('^-\\w+-' + prop + '$', 'i'), function (decl) {
if (decl.parent !== rule) {
return;
}
prefixedDecls.push(decl);
});
const prefixedDecls = util.getDecls(rule, new RegExp('^-\\w+-' + prop + '$', 'i'));

rule.walkDecls(unprefixDecl(decl).prop || prop, function (decl) {
if (decl.parent !== rule) {
return;
}
util.getDecls(rule, unprefixDecl(decl).prop || prop).forEach(function (decl) {
lastUnprefixed = unprefixDecl(decl);
if (lastUnprefixed.value) {
prefixedDecls.push(decl);
Expand Down
27 changes: 6 additions & 21 deletions lib/clearRule.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
'use strict';
const PSEUDO_MAP = {
'input-placeholder': '::placeholder',
'placeholder': '::placeholder',
'full-screen': ':fullscreen',
};

function unprefixSelector (selector) {
let fixed;
selector = selector.replace(/(\:+)-\w+-(\w+(-\w+)*)/g, function (s, colon, pseudo) {
fixed = true;
pseudo = pseudo.toLowerCase();
return PSEUDO_MAP[pseudo] || (colon + pseudo);
});
if (fixed) {
return selector;
}
}
const unprefixSelector = require('./unprefixSelector');

function clearRule (rule) {
const parent = rule.parent;
const selector = unprefixSelector(rule.selector);
if (!selector) {
let selector = unprefixSelector(rule.selector);
if (!selector.fixed) {
return;
}
const prefixedDecls = [];
let unprefixed;
selector = selector.toString();
const parent = rule.parent;
parent.walkRules(function (rule) {
if (rule.parent !== parent) {
return;
}
if (rule.selector === selector) {
unprefixed = rule;
} else if (unprefixSelector(rule.selector) === selector) {
} else if (unprefixSelector(rule.selector).toString() === selector) {
prefixedDecls.push(rule);
}
});
Expand Down
4 changes: 2 additions & 2 deletions lib/unprefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ function unprefix (decl) {
function unprefixDecl (decl) {
const result = unprefix (decl);
result.replace = function () {
if (result.prop) {
if (result.prop && decl.prop.toLowerCase() !== result.prop) {
decl.prop = result.prop;
}
if (result.value) {
if (result.value && decl.value.toLowerCase() !== result.value) {
decl.value = result.value;
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/unprefixParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ function unprefixMedia (params) {
if (!param) {
return true;
}
const prop = /^(?:-\w+-)?(\w+-)?device-pixel-ratio$/i.exec(param[1]);
const prop = /^(?:-\w+-)?((?:\w+-)*?)device-pixel-ratio$/i.exec(param[1]);
if (prop) {
const key = (prop[1] ? prop[1].toLowerCase() : '') + 'resolution';
const key = prop[1].toLowerCase() + 'resolution';
if (!rule[key]) {
rule[key] = null;
}
Expand Down
33 changes: 33 additions & 0 deletions lib/unprefixSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const util = require('./util');
const removeExtraSelector = util.removeExtra(/(.+?)(?:\s*,\s*\1)+/gm);
const PSEUDO_MAP = {
'input-placeholder': '::placeholder',
'placeholder': '::placeholder',
'full-screen': ':fullscreen',
};

const parser = require('postcss-selector-parser');
function unprefixSelector (selector) {
let fixed;
selector = parser(function unprefixSelector (selectors) {
selectors.walk(function (selector) {
const value = /^(\:+)-\w+-(\w+(-\w+)*)$/.exec(selector.value);
if (!value) {
return;
}
fixed = true;
const pseudo = value[2].toLowerCase();
selector.value = PSEUDO_MAP[pseudo] || (value[1] + pseudo);
});
}).process(selector);

return {
fixed: fixed,
toString: function () {
return removeExtraSelector(selector.result);
},
};
}

module.exports = unprefixSelector;
4 changes: 4 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ function getDecls (parent, prop) {
if (!parent) {
return result;
}

if (typeof prop === 'string') {
prop = new RegExp('^' + prop + '$', 'i');
}
parent.walkDecls(prop, function (decl) {
if (decl.parent !== parent) {
return;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"nyc": "^10.3.2",
"postcss-less": "^0.16.1",
"postcss-reporter": "^3.0.0",
"postcss-selector-parser": "^2.2.3",
"stylelint": "^7.10.1"
},
"homepage": "https://github.com/gucong3000/postcss-unprefix#readme",
Expand Down Expand Up @@ -62,5 +63,5 @@
"pretest": "eslint .",
"test": "mocha --no-timeouts"
},
"version": "2.0.0"
"version": "2.0.1"
}
8 changes: 8 additions & 0 deletions test/fixtures/selectors.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ p::-moz-selection {
color: white;
background: black;
}

input:-moz-placeholder, input::placeholder {
color: pink;
}

a[data-foo=":-webkit-full-screen"] {

}
8 changes: 8 additions & 0 deletions test/fixtures/selectors.out.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ p::selection {
color: white;
background: black;
}

input::placeholder {
color: pink;
}

a[data-foo=":-webkit-full-screen"] {

}
7 changes: 7 additions & 0 deletions test/fixtures/uppercase.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a {
dIsPlAy: -webkit-flex;
}

a {
DISPLAY: -webkit-flex;
}
7 changes: 7 additions & 0 deletions test/fixtures/uppercase.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a {
dIsPlAy: flex;
}

a {
DISPLAY: flex;
}

0 comments on commit 60c6410

Please sign in to comment.