├── .editorconfig ├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.{json,yml}] 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "space-before-function-paren": [2, { "named": "never" }], 4 | "no-shadow-restricted-names": [2], 5 | "computed-property-spacing": [2], 6 | "no-empty-character-class": [2], 7 | "no-irregular-whitespace": [2], 8 | "no-unexpected-multiline": [2], 9 | "no-multiple-empty-lines": [2], 10 | "space-return-throw-case": [2], 11 | "no-constant-condition": [2], 12 | "no-extra-boolean-cast": [2], 13 | "no-inner-declarations": [2], 14 | "no-this-before-super": [2], 15 | "no-use-before-define": [2], 16 | "no-array-constructor": [2], 17 | "object-curly-spacing": [2, "always"], 18 | "no-floating-decimal": [2], 19 | "no-warning-comments": [2], 20 | "handle-callback-err": [2], 21 | "no-unneeded-ternary": [2], 22 | "operator-assignment": [2], 23 | "space-before-blocks": [2], 24 | "no-native-reassign": [2], 25 | "no-trailing-spaces": [2], 26 | "operator-linebreak": [2, "after"], 27 | "consistent-return": [2], 28 | "no-duplicate-case": [2], 29 | "no-invalid-regexp": [2], 30 | "no-negated-in-lhs": [2], 31 | "constructor-super": [2], 32 | "no-nested-ternary": [2], 33 | "no-extend-native": [2], 34 | "block-scoped-var": [2], 35 | "no-control-regex": [2], 36 | "no-sparse-arrays": [2], 37 | "no-throw-literal": [2], 38 | "no-return-assign": [2], 39 | "no-const-assign": [2], 40 | "no-class-assign": [2], 41 | "no-extra-parens": [2], 42 | "no-regex-spaces": [2], 43 | "no-implied-eval": [2], 44 | "no-useless-call": [2], 45 | "no-self-compare": [2], 46 | "no-octal-escape": [2], 47 | "no-new-wrappers": [2], 48 | "no-process-exit": [2], 49 | "no-catch-shadow": [2], 50 | "linebreak-style": [2], 51 | "space-infix-ops": [2], 52 | "space-unary-ops": [2], 53 | "no-cond-assign": [2], 54 | "no-func-assign": [2], 55 | "no-unreachable": [2], 56 | "accessor-pairs": [2], 57 | "no-empty-label": [2], 58 | "no-fallthrough": [2], 59 | "no-path-concat": [2], 60 | "no-new-require": [2], 61 | "no-spaced-func": [2], 62 | "no-unused-vars": [2], 63 | "spaced-comment": [2], 64 | "no-delete-var": [2], 65 | "comma-spacing": [2], 66 | "no-extra-semi": [2], 67 | "no-extra-bind": [2], 68 | "arrow-spacing": [2], 69 | "prefer-spread": [2], 70 | "no-new-object": [2], 71 | "no-multi-str": [2], 72 | "semi-spacing": [2], 73 | "no-lonely-if": [2], 74 | "dot-notation": [2], 75 | "dot-location": [2, "property"], 76 | "comma-dangle": [2, "never"], 77 | "no-dupe-args": [2], 78 | "no-dupe-keys": [2], 79 | "no-ex-assign": [2], 80 | "no-obj-calls": [2], 81 | "valid-typeof": [2], 82 | "default-case": [2], 83 | "no-redeclare": [2], 84 | "no-div-regex": [2], 85 | "no-sequences": [2], 86 | "no-label-var": [2], 87 | "comma-style": [2], 88 | "brace-style": [2], 89 | "no-debugger": [2], 90 | "quote-props": [2, "consistent-as-needed"], 91 | "no-iterator": [2], 92 | "no-new-func": [2], 93 | "key-spacing": [2, { "align": "value" }], 94 | "complexity": [2], 95 | "new-parens": [2], 96 | "no-eq-null": [2], 97 | "no-bitwise": [2], 98 | "wrap-iife": [2], 99 | "no-caller": [2], 100 | "use-isnan": [2], 101 | "no-labels": [2], 102 | "no-shadow": [2], 103 | "camelcase": [2], 104 | "eol-last": [2], 105 | "no-octal": [2], 106 | "no-empty": [2], 107 | "no-alert": [2], 108 | "no-proto": [2], 109 | "no-undef": [2], 110 | "no-eval": [2], 111 | "no-with": [2], 112 | "no-void": [2], 113 | "max-len": [2, 80], 114 | "new-cap": [2], 115 | "eqeqeq": [2], 116 | "no-new": [2], 117 | "quotes": [2, "single"], 118 | "indent": [2, 4], 119 | "semi": [2, "always"], 120 | "yoda": [2, "never"] 121 | }, 122 | "ecmaFeatures": { 123 | "modules": true 124 | }, 125 | "env": { 126 | "node": true, 127 | "es6": true 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | 3 | node_modules/ 4 | npm-debug.log 5 | 6 | test/ 7 | .travis.yml 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - stable 5 | - "4" 6 | - "0.12" 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.1 2 | * Basic version 3 | 4 | ## 0.0.0 5 | * Initial upload -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright 2015 Valeriy Komlev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PostCSS Current Selector [![Build Status][ci-img]][ci] 2 | 3 | [PostCSS] plugin which helps you get your current selector. 4 | [postcss-simple-vars]: https://github.com/postcss/postcss-simple-vars 5 | [postcss-nested-ancestors]: https://github.com/toomuchdesign/postcss-nested-ancestors 6 | 7 | Works great with [postcss-simple-vars] for example to get your current selector as a variable 8 | 9 | Alternativly you can use [postcss-nested-ancestors] plugin to reference parent selectors 10 | 11 | [PostCSS]: https://github.com/postcss/postcss 12 | [ci-img]: https://travis-ci.org/komlev/postcss-current-selector.svg 13 | [ci]: https://travis-ci.org/komlev/postcss-current-selector 14 | 15 | ```css 16 | .foo { 17 | $c: %@; 18 | } 19 | ``` 20 | 21 | ```css 22 | .foo { 23 | $c: .foo; 24 | } 25 | ``` 26 | 27 | ## Usage 28 | 29 | ```js 30 | postcss([ require('postcss-current-selector') ]) 31 | ``` 32 | 33 | ## Options 34 | 35 | ### `symbol` 36 | 37 | By default, plugin will replace `%@` match in the declaration, but it could be changed with this option: 38 | 39 | ```js 40 | postcss([ require('postcss-nested')({ symbol: '*@' }) ] 41 | ``` 42 | will look for `*@` text in declaration 43 | 44 | ```css 45 | .foo { 46 | content: "*@"; 47 | } 48 | ``` 49 | 50 | ```css 51 | .foo { 52 | content: ".foo"; 53 | } 54 | ``` 55 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var postcss = require('postcss'); 2 | module.exports = postcss.plugin('postcss-current-selector', function (opts) { 3 | if (!opts) opts = {}; 4 | opts.symbol = opts.symbol || '%@'; 5 | return function (css) { 6 | function processNode(parent) { 7 | parent.walk(function (node) { 8 | if (node.type === 'atrule') { 9 | processNode(node); 10 | } else if (node.type === 'rule') { 11 | processNode(node); 12 | } else if (node.type === 'decl') { 13 | if (node.value.indexOf(opts.symbol) !== -1) { 14 | node.value = 15 | node.value.replace(opts.symbol, parent.selector); 16 | } 17 | } 18 | }); 19 | } 20 | processNode(css); 21 | }; 22 | }); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-current-selector", 3 | "version": "0.0.3", 4 | "description": "PostCSS plugin which helps you get your current selector", 5 | "keywords": [ 6 | "postcss", 7 | "css", 8 | "postcss-plugin", 9 | "selector" 10 | ], 11 | "author": "Valeriy Komlev ", 12 | "license": "MIT", 13 | "repository": "komlev/postcss-current-selector", 14 | "bugs": { 15 | "url": "https://github.com/komlev/postcss-current-selector/issues" 16 | }, 17 | "homepage": "https://github.com/komlev/postcss-current-selector", 18 | "dependencies": { 19 | "postcss": "^5.0.10" 20 | }, 21 | "devDependencies": { 22 | "ava": "^0.17.0", 23 | "eslint": "^1.10.2" 24 | }, 25 | "scripts": { 26 | "test": "ava && eslint *.js" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import postcss from 'postcss'; 2 | import test from 'ava'; 3 | import plugin from './'; 4 | function run(t, input, output, opts = { }) { 5 | return postcss([ plugin(opts) ]).process(input) 6 | .then( result => { 7 | t.deepEqual(result.css, output); 8 | t.deepEqual(result.warnings().length, 0); 9 | }); 10 | } 11 | 12 | test('gets selector in class rule', t => { 13 | return run(t, '.class{content:"%@"}', '.class{content:".class"}'); 14 | }); 15 | 16 | test('set symbol through options', t => { 17 | return run(t, 'a{content:"*@"}', 'a{content:"a"}', { symbol: '*@' }); 18 | }); 19 | 20 | --------------------------------------------------------------------------------