Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class + element matches #8

Merged
merged 14 commits into from
Sep 6, 2016
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"stage": 0
"presets": [
"es2015",
"stage-2"
]
}
10 changes: 5 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
# babel support more syntax stuff than eslint for now
parser: babel-eslint

root: true
extends: eslint:recommended

ecmaFeatures:
modules: true

parserOptions:
sourceType: module

env:
es6: true
browser: true
Expand All @@ -16,7 +16,7 @@ env:
rules:
indent: [2, 2] # 2 spaces indentation
max-len: [2, 80, 4]
quotes: [2, "double"]
quotes: [2, "double", {"allowTemplateLiterals": true}]
semi: [2, "never"]
no-multiple-empty-lines: [2, {"max": 1}]

Expand All @@ -35,7 +35,7 @@ rules:
computed-property-spacing: [2, "never"]

space-unary-ops: [2, {"words": true, "nonwords": false}]
space-after-keywords: [2, "always"]
keyword-spacing: [2, {"before": true, "after": true}]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MoOx I need your confirmation that this is the correct configuration. When I upgraded the package the configuration changed to this style. Please confirm that this is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

space-before-blocks: [2, "always"]
space-before-function-paren: [2, "never"]
space-in-parens: [2, "never"]
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist
node_modules
npm-debug.log
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
sudo: false
language: node_js
node_js:
- "6"
- "4"
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
"dist"
],
"dependencies": {
"balanced-match": "^0.2.0",
"balanced-match": "^0.4.2",
"postcss": "^5.0.0"
},
"devDependencies": {
"babel": "^5.5.8",
"babel-eslint": "^3.1.15",
"babel-tape-runner": "^1.1.0",
"eslint": "^1.0.0",
"babel-cli": "^6.14.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-stage-2": "^6.13.0",
"babel-tape-runner": "^2.0.1",
"eslint": "^3.4.0",
"tape": "^4.0.0"
},
"scripts": {
Expand Down
19 changes: 18 additions & 1 deletion src/replaceRuleSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import balancedMatch from "balanced-match"

const pseudoClass = ":matches"

function isElementSelector(selector) {
return !selector.match(/(\:|\.)/g)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any feedback about this Regex? If you have any dot or colon then you are not an element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess according to the tests, it's ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MoOx everything pass 😄

}

function normalizeSelector(selector, preWhitespace, pre) {
const selectorIsElement = isElementSelector(selector)
const preIsElement = isElementSelector(pre)

if (selectorIsElement && !preIsElement) {
return `${preWhitespace}${selector}${pre}`
}

return `${preWhitespace}${pre}${selector}`
}

function explodeSelector(selector, options) {
if (selector && selector.indexOf(pseudoClass) > -1) {
let newSelectors = []
Expand Down Expand Up @@ -33,7 +48,9 @@ function explodeSelector(selector, options) {

let newParts
if (postSelectors.length === 0) {
newParts = bodySelectors.map((s) => preWhitespace + pre + s)
newParts = bodySelectors.map((s) => {
return normalizeSelector(s, preWhitespace, pre)
})
}
else {
newParts = []
Expand Down
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,11 @@ article h3 + p {}`,
"should works correctly with adjacent selectors and line break"
)

t.equal(
transform(`.foo:matches(p) {color: red;}`),
`p.foo {color: red;}`,
"should works correctly with a class and an element"
)

t.end()
})