├── .editorconfig ├── .eslintrc.js ├── .gitattributes ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── .npmignore ├── .prettierrc ├── LICENSE ├── README.md ├── example ├── .eslintrc.js ├── index.js ├── main-rem.css └── main.css ├── index.js ├── lib ├── filter-prop-list.js ├── pixel-unit-regex.js └── type.js ├── package-lock.json ├── package.json └── spec └── pxtorem-spec.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.md] 11 | trim_trailing_whitespace = false 12 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | commonjs: true, 4 | es6: true, 5 | node: true 6 | }, 7 | extends: "eslint:recommended", 8 | globals: { 9 | Atomics: "readonly", 10 | SharedArrayBuffer: "readonly" 11 | }, 12 | parserOptions: { 13 | ecmaVersion: 2018 14 | }, 15 | rules: { 16 | "no-console": 1 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | node-version: [8.x, 10.x, 12.x] 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm run lint 21 | - run: npm test 22 | env: 23 | CI: true 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | example 3 | .github 4 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "useTabs": false 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (MIT License) 2 | 3 | Copyright (C) 2014 Jonathan Cuthbert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postcss-pxtorem [![NPM version](https://badge.fury.io/js/postcss-pxtorem.svg)](http://badge.fury.io/js/postcss-pxtorem) 2 | 3 | A plugin for [PostCSS](https://github.com/ai/postcss) that generates rem units from pixel units. 4 | 5 | ## Install 6 | 7 | ```shell 8 | $ npm install postcss postcss-pxtorem --save-dev 9 | ``` 10 | 11 | ## Usage 12 | 13 | Pixels are the easiest unit to use (*opinion*). The only issue with them is that they don't let browsers change the default font size of 16. This script converts every px value to a rem from the properties you choose to allow the browser to set the font size. 14 | 15 | 16 | ### Input/Output 17 | 18 | *With the default settings, only font related properties are targeted.* 19 | 20 | ```css 21 | // input 22 | h1 { 23 | margin: 0 0 20px; 24 | font-size: 32px; 25 | line-height: 1.2; 26 | letter-spacing: 1px; 27 | } 28 | 29 | // output 30 | h1 { 31 | margin: 0 0 20px; 32 | font-size: 2rem; 33 | line-height: 1.2; 34 | letter-spacing: 0.0625rem; 35 | } 36 | ``` 37 | 38 | ### Example 39 | 40 | ```js 41 | var fs = require('fs'); 42 | var postcss = require('postcss'); 43 | var pxtorem = require('postcss-pxtorem'); 44 | var css = fs.readFileSync('main.css', 'utf8'); 45 | var options = { 46 | replace: false 47 | }; 48 | var processedCss = postcss(pxtorem(options)).process(css).css; 49 | 50 | fs.writeFile('main-rem.css', processedCss, function (err) { 51 | if (err) { 52 | throw err; 53 | } 54 | console.log('Rem file written.'); 55 | }); 56 | ``` 57 | 58 | ### options 59 | 60 | Type: `Object | Null` 61 | Default: 62 | ```js 63 | { 64 | rootValue: 16, 65 | unitPrecision: 5, 66 | propList: ['font', 'font-size', 'line-height', 'letter-spacing', 'word-spacing'], 67 | selectorBlackList: [], 68 | replace: true, 69 | mediaQuery: false, 70 | minPixelValue: 0, 71 | exclude: /node_modules/i 72 | } 73 | ``` 74 | 75 | - `rootValue` (Number | Function) Represents the root element font size or returns the root element font size based on the [`input`](https://api.postcss.org/Input.html) parameter 76 | - `unitPrecision` (Number) The decimal numbers to allow the REM units to grow to. 77 | - `propList` (Array) The properties that can change from px to rem. 78 | - Values need to be exact matches. 79 | - Use wildcard `*` to enable all properties. Example: `['*']` 80 | - Use `*` at the start or end of a word. (`['*position*']` will match `background-position-y`) 81 | - Use `!` to not match a property. Example: `['*', '!letter-spacing']` 82 | - Combine the "not" prefix with the other prefixes. Example: `['*', '!font*']` 83 | - `selectorBlackList` (Array) The selectors to ignore and leave as px. 84 | - If value is string, it checks to see if selector contains the string. 85 | - `['body']` will match `.body-class` 86 | - If value is regexp, it checks to see if the selector matches the regexp. 87 | - `[/^body$/]` will match `body` but not `.body` 88 | - `replace` (Boolean) Replaces rules containing rems instead of adding fallbacks. 89 | - `mediaQuery` (Boolean) Allow px to be converted in media queries. 90 | - `minPixelValue` (Number) Set the minimum pixel value to replace. 91 | - `exclude` (String, Regexp, Function) The file path to ignore and leave as px. 92 | - If value is string, it checks to see if file path contains the string. 93 | - `'exclude'` will match `\project\postcss-pxtorem\exclude\path` 94 | - If value is regexp, it checks to see if file path matches the regexp. 95 | - `/exclude/i` will match `\project\postcss-pxtorem\exclude\path` 96 | - If value is function, you can use exclude function to return a true and the file will be ignored. 97 | - the callback will pass the file path as a parameter, it should returns a Boolean result. 98 | - `function (file) { return file.indexOf('exclude') !== -1; }` 99 | - `unit` (String) Set the default unit to convert, default is `px`. 100 | 101 | ### Use with gulp-postcss and autoprefixer 102 | 103 | ```js 104 | var gulp = require('gulp'); 105 | var postcss = require('gulp-postcss'); 106 | var autoprefixer = require('autoprefixer'); 107 | var pxtorem = require('postcss-pxtorem'); 108 | 109 | gulp.task('css', function () { 110 | 111 | var processors = [ 112 | autoprefixer({ 113 | browsers: 'last 1 version' 114 | }), 115 | pxtorem({ 116 | replace: false 117 | }) 118 | ]; 119 | 120 | return gulp.src(['build/css/**/*.css']) 121 | .pipe(postcss(processors)) 122 | .pipe(gulp.dest('build/css')); 123 | }); 124 | ``` 125 | 126 | ### A message about ignoring properties 127 | Currently, the easiest way to have a single property ignored is to use a capital in the pixel unit declaration. 128 | 129 | ```css 130 | // `px` is converted to `rem` 131 | .convert { 132 | font-size: 16px; // converted to 1rem 133 | } 134 | 135 | // `Px` or `PX` is ignored by `postcss-pxtorem` but still accepted by browsers 136 | .ignore { 137 | border: 1Px solid; // ignored 138 | border-width: 2PX; // ignored 139 | } 140 | ``` 141 | -------------------------------------------------------------------------------- /example/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | rules: { 3 | "no-console": 0 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /example/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const fs = require("fs"); 4 | const postcss = require("postcss"); 5 | const pxtorem = require(".."); 6 | 7 | const css = fs.readFileSync("main.css", "utf8"); 8 | const options = { 9 | replace: false 10 | }; 11 | const processedCss = postcss(pxtorem(options)).process(css).css; 12 | 13 | fs.writeFile("main-rem.css", processedCss, function(err) { 14 | if (err) { 15 | throw err; 16 | } 17 | console.log("Rem file written."); 18 | }); 19 | -------------------------------------------------------------------------------- /example/main-rem.css: -------------------------------------------------------------------------------- 1 | .class { 2 | margin: -10px .5em; 3 | padding: 5rem .5px; 4 | border: 3px solid black; 5 | font-size: 14px; 6 | font-size: 0.875rem; 7 | line-height: 20px; 8 | line-height: 1.25rem; 9 | } 10 | .class2 { 11 | font-size: 32px; 12 | font-size: 2rem; 13 | line-height: 1em; 14 | } 15 | @media (min-width: 750px) { 16 | .class3 { 17 | font-size: 16px; 18 | font-size: 1rem; 19 | line-height: 22px; 20 | line-height: 1.375rem; 21 | } 22 | } 23 | 24 | /* 25 | .class { 26 | font-size: 16px; 27 | } 28 | */ -------------------------------------------------------------------------------- /example/main.css: -------------------------------------------------------------------------------- 1 | .class { 2 | margin: -10px .5em; 3 | padding: 5rem .5px; 4 | border: 3px solid black; 5 | font-size: 14px; 6 | line-height: 20px; 7 | } 8 | .class2 { 9 | font-size: 32px; 10 | font-size: 2rem; 11 | line-height: 1em; 12 | } 13 | @media (min-width: 750px) { 14 | .class3 { 15 | font-size: 16px; 16 | line-height: 22px; 17 | } 18 | } 19 | 20 | /* 21 | .class { 22 | font-size: 16px; 23 | } 24 | */ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const pxRegex = require("./lib/pixel-unit-regex"); 2 | const filterPropList = require("./lib/filter-prop-list"); 3 | const type = require("./lib/type"); 4 | 5 | const defaults = { 6 | rootValue: 16, 7 | unitPrecision: 5, 8 | selectorBlackList: [], 9 | propList: ["font", "font-size", "line-height", "letter-spacing", "word-spacing"], 10 | replace: true, 11 | mediaQuery: false, 12 | minPixelValue: 0, 13 | exclude: null, 14 | unit: "px", 15 | }; 16 | 17 | const legacyOptions = { 18 | root_value: "rootValue", 19 | unit_precision: "unitPrecision", 20 | selector_black_list: "selectorBlackList", 21 | prop_white_list: "propList", 22 | media_query: "mediaQuery", 23 | propWhiteList: "propList", 24 | }; 25 | 26 | function convertLegacyOptions(options) { 27 | if (typeof options !== "object") return; 28 | if ( 29 | ((typeof options["prop_white_list"] !== "undefined" && 30 | options["prop_white_list"].length === 0) || 31 | (typeof options.propWhiteList !== "undefined" && 32 | options.propWhiteList.length === 0)) && 33 | typeof options.propList === "undefined" 34 | ) { 35 | options.propList = ["*"]; 36 | delete options["prop_white_list"]; 37 | delete options.propWhiteList; 38 | } 39 | Object.keys(legacyOptions).forEach((key) => { 40 | if (Reflect.has(options, key)) { 41 | options[legacyOptions[key]] = options[key]; 42 | delete options[key]; 43 | } 44 | }); 45 | } 46 | 47 | function createPxReplace(rootValue, unitPrecision, minPixelValue) { 48 | return (m, $1) => { 49 | if (!$1) return m; 50 | const pixels = parseFloat($1); 51 | if (pixels < minPixelValue) return m; 52 | const fixedVal = toFixed(pixels / rootValue, unitPrecision); 53 | return fixedVal + "rem"; 54 | }; 55 | } 56 | 57 | function toFixed(number, precision) { 58 | const multiplier = Math.pow(10, precision + 1), 59 | wholeNumber = Math.floor(number * multiplier); 60 | return (Math.round(wholeNumber / 10) * 10) / multiplier; 61 | } 62 | 63 | function declarationExists(decls, prop, value) { 64 | return decls.some((decl) => decl.prop === prop && decl.value === value); 65 | } 66 | 67 | function blacklistedSelector(blacklist, selector) { 68 | if (typeof selector !== "string") return; 69 | return blacklist.some((regex) => { 70 | if (typeof regex === "string") { 71 | return selector.indexOf(regex) !== -1; 72 | } 73 | return selector.match(regex); 74 | }); 75 | } 76 | 77 | function createPropListMatcher(propList) { 78 | const hasWild = propList.indexOf("*") > -1; 79 | const matchAll = hasWild && propList.length === 1; 80 | const lists = { 81 | exact: filterPropList.exact(propList), 82 | contain: filterPropList.contain(propList), 83 | startWith: filterPropList.startWith(propList), 84 | endWith: filterPropList.endWith(propList), 85 | notExact: filterPropList.notExact(propList), 86 | notContain: filterPropList.notContain(propList), 87 | notStartWith: filterPropList.notStartWith(propList), 88 | notEndWith: filterPropList.notEndWith(propList), 89 | }; 90 | return (prop) => { 91 | if (matchAll) return true; 92 | return ( 93 | (hasWild || 94 | lists.exact.indexOf(prop) > -1 || 95 | lists.contain.some(function (m) { 96 | return prop.indexOf(m) > -1; 97 | }) || 98 | lists.startWith.some(function (m) { 99 | return prop.indexOf(m) === 0; 100 | }) || 101 | lists.endWith.some(function (m) { 102 | return prop.indexOf(m) === prop.length - m.length; 103 | })) && 104 | !( 105 | lists.notExact.indexOf(prop) > -1 || 106 | lists.notContain.some(function (m) { 107 | return prop.indexOf(m) > -1; 108 | }) || 109 | lists.notStartWith.some(function (m) { 110 | return prop.indexOf(m) === 0; 111 | }) || 112 | lists.notEndWith.some(function (m) { 113 | return prop.indexOf(m) === prop.length - m.length; 114 | }) 115 | ) 116 | ); 117 | }; 118 | } 119 | 120 | module.exports = (options = {}) => { 121 | convertLegacyOptions(options); 122 | const opts = Object.assign({}, defaults, options); 123 | const satisfyPropList = createPropListMatcher(opts.propList); 124 | const exclude = opts.exclude; 125 | let isExcludeFile = false; 126 | let pxReplace; 127 | return { 128 | postcssPlugin: "postcss-pxtorem", 129 | Once(css) { 130 | const filePath = css.source.input.file; 131 | if ( 132 | exclude && 133 | ((type.isFunction(exclude) && exclude(filePath)) || 134 | (type.isString(exclude) && filePath.indexOf(exclude) !== -1) || 135 | filePath.match(exclude) !== null) 136 | ) { 137 | isExcludeFile = true; 138 | } else { 139 | isExcludeFile = false; 140 | } 141 | 142 | const rootValue = 143 | typeof opts.rootValue === "function" 144 | ? opts.rootValue(css.source.input) 145 | : opts.rootValue; 146 | pxReplace = createPxReplace( 147 | rootValue, 148 | opts.unitPrecision, 149 | opts.minPixelValue, 150 | ); 151 | }, 152 | Declaration(decl) { 153 | if (isExcludeFile) return; 154 | 155 | if ( 156 | decl.value.indexOf(opts.unit) === -1 || 157 | !satisfyPropList(decl.prop) || 158 | blacklistedSelector(opts.selectorBlackList, decl.parent.selector) 159 | ) 160 | return; 161 | 162 | const value = decl.value.replace(pxRegex(opts.unit), pxReplace); 163 | 164 | // if rem unit already exists, do not add or replace 165 | if (declarationExists(decl.parent, decl.prop, value)) return; 166 | 167 | if (opts.replace) { 168 | decl.value = value; 169 | } else { 170 | decl.cloneAfter({ value: value }); 171 | } 172 | }, 173 | AtRule(atRule) { 174 | if (isExcludeFile) return; 175 | 176 | if (opts.mediaQuery && atRule.name === "media") { 177 | if (atRule.params.indexOf(opts.unit) === -1) return; 178 | atRule.params = atRule.params.replace(pxRegex(opts.unit), pxReplace); 179 | } 180 | }, 181 | }; 182 | }; 183 | module.exports.postcss = true; 184 | -------------------------------------------------------------------------------- /lib/filter-prop-list.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | exact: list => list.filter(m => m.match(/^[^*!]+$/)), 3 | contain: list => 4 | list.filter(m => m.match(/^\*.+\*$/)).map(m => m.substr(1, m.length - 2)), 5 | endWith: list => list.filter(m => m.match(/^\*[^*]+$/)).map(m => m.substr(1)), 6 | startWith: list => 7 | list.filter(m => m.match(/^[^*!]+\*$/)).map(m => m.substr(0, m.length - 1)), 8 | notExact: list => 9 | list.filter(m => m.match(/^![^*].*$/)).map(m => m.substr(1)), 10 | notContain: list => 11 | list.filter(m => m.match(/^!\*.+\*$/)).map(m => m.substr(2, m.length - 3)), 12 | notEndWith: list => 13 | list.filter(m => m.match(/^!\*[^*]+$/)).map(m => m.substr(2)), 14 | notStartWith: list => 15 | list.filter(m => m.match(/^![^*]+\*$/)).map(m => m.substr(1, m.length - 2)) 16 | }; 17 | -------------------------------------------------------------------------------- /lib/pixel-unit-regex.js: -------------------------------------------------------------------------------- 1 | // excluding regex trick: http://www.rexegg.com/regex-best-trick.html 2 | 3 | // Not anything inside double quotes 4 | // Not anything inside single quotes 5 | // Not anything inside url() 6 | // Any digit followed by px 7 | // !singlequotes|!doublequotes|!url()|pixelunit 8 | 9 | module.exports = (unit) => 10 | new RegExp( 11 | `"[^"]+"|'[^']+'|url\\([^)]+\\)|var\\([^)]+\\)|(\\d*\\.?\\d+)${unit}`, 12 | 'g', 13 | ); 14 | -------------------------------------------------------------------------------- /lib/type.js: -------------------------------------------------------------------------------- 1 | const type = s => 2 | Object.prototype.toString 3 | .call(s) 4 | .slice(8, -1) 5 | .toLowerCase(); 6 | 7 | const types = [ 8 | "String", 9 | "Array", 10 | "Undefined", 11 | "Boolean", 12 | "Number", 13 | "Function", 14 | "Symbol", 15 | "Object" 16 | ]; 17 | 18 | module.exports = types.reduce((acc, str) => { 19 | acc["is" + str] = val => type(val) === str.toLowerCase(); 20 | return acc; 21 | }, {}); 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-pxtorem", 3 | "description": "A CSS post-processor that converts px to rem.", 4 | "version": "6.1.0", 5 | "author": "cuth", 6 | "license": "MIT", 7 | "repository": { 8 | "type": "git", 9 | "url": "git@github.com:cuth/postcss-pxtorem.git" 10 | }, 11 | "bugs": "https://github.com/cuth/postcss-pxtorem/issues", 12 | "main": "index.js", 13 | "scripts": { 14 | "lint": "eslint .", 15 | "test": "jasmine-node spec" 16 | }, 17 | "husky": { 18 | "hooks": { 19 | "pre-commit": "lint-staged" 20 | } 21 | }, 22 | "lint-staged": { 23 | "*.js": [ 24 | "eslint --fix", 25 | "prettier --write" 26 | ] 27 | }, 28 | "devDependencies": { 29 | "eslint": "^6.8.0", 30 | "husky": "^4.2.3", 31 | "jasmine-node": "^1.16.0", 32 | "lint-staged": "^10.0.8", 33 | "postcss": "^8.0.0", 34 | "prettier": "^1.19.1" 35 | }, 36 | "keywords": [ 37 | "css", 38 | "rem", 39 | "pixel", 40 | "px", 41 | "postcss", 42 | "postcss-plugin" 43 | ], 44 | "peerDependencies": { 45 | "postcss": "^8.0.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /spec/pxtorem-spec.js: -------------------------------------------------------------------------------- 1 | // Jasmine unit tests 2 | // To run tests, run these commands from the project root: 3 | // 1. `npm install -g jasmine-node` 4 | // 2. `jasmine-node spec` 5 | 6 | /* global describe, it, expect */ 7 | 8 | "use strict"; 9 | var postcss = require("postcss"); 10 | var pxtorem = require(".."); 11 | var basicCSS = ".rule { font-size: 15px }"; 12 | var filterPropList = require("../lib/filter-prop-list"); 13 | 14 | describe("pxtorem", function() { 15 | it("should work on the readme example", function() { 16 | var input = 17 | "h1 { margin: 0 0 20px; font-size: 32px; line-height: 1.2; letter-spacing: 1px; }"; 18 | var output = 19 | "h1 { margin: 0 0 20px; font-size: 2rem; line-height: 1.2; letter-spacing: 0.0625rem; }"; 20 | var processed = postcss(pxtorem()).process(input).css; 21 | 22 | expect(processed).toBe(output); 23 | }); 24 | 25 | it("should replace the px unit with rem", function() { 26 | var processed = postcss(pxtorem()).process(basicCSS).css; 27 | var expected = ".rule { font-size: 0.9375rem }"; 28 | 29 | expect(processed).toBe(expected); 30 | }); 31 | 32 | it("should ignore non px properties", function() { 33 | var expected = ".rule { font-size: 2em }"; 34 | var processed = postcss(pxtorem()).process(expected).css; 35 | 36 | expect(processed).toBe(expected); 37 | }); 38 | 39 | it("should handle < 1 values and values without a leading 0 - legacy", function() { 40 | var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }"; 41 | var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }"; 42 | var options = { 43 | propWhiteList: ["margin"] 44 | }; 45 | var processed = postcss(pxtorem(options)).process(rules).css; 46 | 47 | expect(processed).toBe(expected); 48 | }); 49 | 50 | it("should ignore px in custom property names", function() { 51 | var rules = 52 | ":root { --rem-14px: 14px; } .rule { font-size: var(--rem-14px); }"; 53 | var expected = 54 | ":root { --rem-14px: 0.875rem; } .rule { font-size: var(--rem-14px); }"; 55 | var options = { 56 | propList: ["--*", "font-size"] 57 | }; 58 | var processed = postcss(pxtorem(options)).process(rules).css; 59 | 60 | expect(processed).toBe(expected); 61 | }); 62 | 63 | it("should handle < 1 values and values without a leading 0", function() { 64 | var rules = ".rule { margin: 0.5rem .5px -0.2px -.2em }"; 65 | var expected = ".rule { margin: 0.5rem 0.03125rem -0.0125rem -.2em }"; 66 | var options = { 67 | propList: ["margin"] 68 | }; 69 | var processed = postcss(pxtorem(options)).process(rules).css; 70 | 71 | expect(processed).toBe(expected); 72 | }); 73 | 74 | it("should not add properties that already exist", function() { 75 | var expected = ".rule { font-size: 16px; font-size: 1rem; }"; 76 | var processed = postcss(pxtorem()).process(expected).css; 77 | 78 | expect(processed).toBe(expected); 79 | }); 80 | 81 | it("should remain unitless if 0", function() { 82 | var expected = ".rule { font-size: 0px; font-size: 0; }"; 83 | var processed = postcss(pxtorem()).process(expected).css; 84 | 85 | expect(processed).toBe(expected); 86 | }); 87 | }); 88 | 89 | describe("value parsing", function() { 90 | it("should not replace values in double quotes or single quotes - legacy", function() { 91 | var options = { 92 | propWhiteList: [] 93 | }; 94 | var rules = 95 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }"; 96 | var expected = 97 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }"; 98 | var processed = postcss(pxtorem(options)).process(rules).css; 99 | 100 | expect(processed).toBe(expected); 101 | }); 102 | 103 | it("should not replace values in double quotes or single quotes", function() { 104 | var options = { 105 | propList: ["*"] 106 | }; 107 | var rules = 108 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 16px; }"; 109 | var expected = 110 | ".rule { content: '16px'; font-family: \"16px\"; font-size: 1rem; }"; 111 | var processed = postcss(pxtorem(options)).process(rules).css; 112 | 113 | expect(processed).toBe(expected); 114 | }); 115 | 116 | it("should not replace values in `url()` - legacy", function() { 117 | var options = { 118 | propWhiteList: [] 119 | }; 120 | var rules = ".rule { background: url(16px.jpg); font-size: 16px; }"; 121 | var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }"; 122 | var processed = postcss(pxtorem(options)).process(rules).css; 123 | 124 | expect(processed).toBe(expected); 125 | }); 126 | 127 | it("should not replace values in `url()`", function() { 128 | var options = { 129 | propList: ["*"] 130 | }; 131 | var rules = ".rule { background: url(16px.jpg); font-size: 16px; }"; 132 | var expected = ".rule { background: url(16px.jpg); font-size: 1rem; }"; 133 | var processed = postcss(pxtorem(options)).process(rules).css; 134 | 135 | expect(processed).toBe(expected); 136 | }); 137 | 138 | it("should not replace values with an uppercase P or X", function() { 139 | var options = { 140 | propList: ["*"] 141 | }; 142 | var rules = 143 | ".rule { margin: 12px calc(100% - 14PX); height: calc(100% - 20px); font-size: 12Px; line-height: 16px; }"; 144 | var expected = 145 | ".rule { margin: 0.75rem calc(100% - 14PX); height: calc(100% - 1.25rem); font-size: 12Px; line-height: 1rem; }"; 146 | var processed = postcss(pxtorem(options)).process(rules).css; 147 | 148 | expect(processed).toBe(expected); 149 | }); 150 | }); 151 | 152 | describe("rootValue", function() { 153 | // Deprecate 154 | it("should replace using a root value of 10 - legacy", function() { 155 | var expected = ".rule { font-size: 1.5rem }"; 156 | var options = { 157 | root_value: 10 158 | }; 159 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 160 | 161 | expect(processed).toBe(expected); 162 | }); 163 | 164 | it("should replace using a root value of 10", function() { 165 | var expected = ".rule { font-size: 1.5rem }"; 166 | var options = { 167 | rootValue: 10 168 | }; 169 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 170 | 171 | expect(processed).toBe(expected); 172 | }); 173 | 174 | it("should replace using different root values with different files", function() { 175 | var css2 = ".rule { font-size: 20px }"; 176 | var expected = ".rule { font-size: 1rem }"; 177 | var options = { 178 | rootValue: function(input) { 179 | if (input.from.indexOf("basic.css") !== -1) { 180 | return 15; 181 | } 182 | return 20; 183 | } 184 | }; 185 | var processed1 = postcss(pxtorem(options)).process(basicCSS, { 186 | from: "/tmp/basic.css" 187 | }).css; 188 | var processed2 = postcss(pxtorem(options)).process(css2, { 189 | from: "/tmp/whatever.css" 190 | }).css; 191 | 192 | expect(processed1).toBe(expected); 193 | expect(processed2).toBe(expected); 194 | }); 195 | }); 196 | 197 | describe("unitPrecision", function() { 198 | // Deprecate 199 | it("should replace using a decimal of 2 places - legacy", function() { 200 | var expected = ".rule { font-size: 0.94rem }"; 201 | var options = { 202 | unit_precision: 2 203 | }; 204 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 205 | 206 | expect(processed).toBe(expected); 207 | }); 208 | 209 | it("should replace using a decimal of 2 places", function() { 210 | var expected = ".rule { font-size: 0.94rem }"; 211 | var options = { 212 | unitPrecision: 2 213 | }; 214 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 215 | 216 | expect(processed).toBe(expected); 217 | }); 218 | }); 219 | 220 | describe("propWhiteList", function() { 221 | // Deprecate 222 | it("should only replace properties in the white list - legacy", function() { 223 | var expected = ".rule { font-size: 15px }"; 224 | var options = { 225 | prop_white_list: ["font"] 226 | }; 227 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 228 | 229 | expect(processed).toBe(expected); 230 | }); 231 | 232 | it("should only replace properties in the white list - legacy", function() { 233 | var expected = ".rule { font-size: 15px }"; 234 | var options = { 235 | propWhiteList: ["font"] 236 | }; 237 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 238 | 239 | expect(processed).toBe(expected); 240 | }); 241 | 242 | it("should only replace properties in the white list - legacy", function() { 243 | var css = ".rule { margin: 16px; margin-left: 10px }"; 244 | var expected = ".rule { margin: 1rem; margin-left: 10px }"; 245 | var options = { 246 | propWhiteList: ["margin"] 247 | }; 248 | var processed = postcss(pxtorem(options)).process(css).css; 249 | 250 | expect(processed).toBe(expected); 251 | }); 252 | 253 | it("should only replace properties in the prop list", function() { 254 | var css = 255 | ".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }"; 256 | var expected = 257 | ".rule { font-size: 1rem; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 1rem }"; 258 | var options = { 259 | propWhiteList: ["*font*", "margin*", "!margin-left", "*-right", "pad"] 260 | }; 261 | var processed = postcss(pxtorem(options)).process(css).css; 262 | 263 | expect(processed).toBe(expected); 264 | }); 265 | 266 | it("should only replace properties in the prop list with wildcard", function() { 267 | var css = 268 | ".rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }"; 269 | var expected = 270 | ".rule { font-size: 16px; margin: 1rem; margin-left: 5px; padding: 5px; padding-right: 16px }"; 271 | var options = { 272 | propWhiteList: ["*", "!margin-left", "!*padding*", "!font*"] 273 | }; 274 | var processed = postcss(pxtorem(options)).process(css).css; 275 | 276 | expect(processed).toBe(expected); 277 | }); 278 | 279 | it("should replace all properties when white list is empty", function() { 280 | var rules = ".rule { margin: 16px; font-size: 15px }"; 281 | var expected = ".rule { margin: 1rem; font-size: 0.9375rem }"; 282 | var options = { 283 | propWhiteList: [] 284 | }; 285 | var processed = postcss(pxtorem(options)).process(rules).css; 286 | 287 | expect(processed).toBe(expected); 288 | }); 289 | }); 290 | 291 | describe("selectorBlackList", function() { 292 | // Deprecate 293 | it("should ignore selectors in the selector black list - legacy", function() { 294 | var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }"; 295 | var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }"; 296 | var options = { 297 | selector_black_list: [".rule2"] 298 | }; 299 | var processed = postcss(pxtorem(options)).process(rules).css; 300 | 301 | expect(processed).toBe(expected); 302 | }); 303 | 304 | it("should ignore selectors in the selector black list", function() { 305 | var rules = ".rule { font-size: 15px } .rule2 { font-size: 15px }"; 306 | var expected = ".rule { font-size: 0.9375rem } .rule2 { font-size: 15px }"; 307 | var options = { 308 | selectorBlackList: [".rule2"] 309 | }; 310 | var processed = postcss(pxtorem(options)).process(rules).css; 311 | 312 | expect(processed).toBe(expected); 313 | }); 314 | 315 | it("should ignore every selector with `body$`", function() { 316 | var rules = 317 | "body { font-size: 16px; } .class-body$ { font-size: 16px; } .simple-class { font-size: 16px; }"; 318 | var expected = 319 | "body { font-size: 1rem; } .class-body$ { font-size: 16px; } .simple-class { font-size: 1rem; }"; 320 | var options = { 321 | selectorBlackList: ["body$"] 322 | }; 323 | var processed = postcss(pxtorem(options)).process(rules).css; 324 | 325 | expect(processed).toBe(expected); 326 | }); 327 | 328 | it("should only ignore exactly `body`", function() { 329 | var rules = 330 | "body { font-size: 16px; } .class-body { font-size: 16px; } .simple-class { font-size: 16px; }"; 331 | var expected = 332 | "body { font-size: 16px; } .class-body { font-size: 1rem; } .simple-class { font-size: 1rem; }"; 333 | var options = { 334 | selectorBlackList: [/^body$/] 335 | }; 336 | var processed = postcss(pxtorem(options)).process(rules).css; 337 | 338 | expect(processed).toBe(expected); 339 | }); 340 | }); 341 | 342 | describe("replace", function() { 343 | it("should leave fallback pixel unit with root em value", function() { 344 | var options = { 345 | replace: false 346 | }; 347 | var processed = postcss(pxtorem(options)).process(basicCSS).css; 348 | var expected = ".rule { font-size: 15px; font-size: 0.9375rem }"; 349 | 350 | expect(processed).toBe(expected); 351 | }); 352 | }); 353 | 354 | describe("mediaQuery", function() { 355 | // Deprecate 356 | it("should replace px in media queries", function() { 357 | var options = { 358 | media_query: true 359 | }; 360 | var processed = postcss(pxtorem(options)).process( 361 | "@media (min-width: 500px) { .rule { font-size: 16px } }" 362 | ).css; 363 | var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }"; 364 | 365 | expect(processed).toBe(expected); 366 | }); 367 | 368 | it("should replace px in media queries", function() { 369 | var options = { 370 | mediaQuery: true 371 | }; 372 | var processed = postcss(pxtorem(options)).process( 373 | "@media (min-width: 500px) { .rule { font-size: 16px } }" 374 | ).css; 375 | var expected = "@media (min-width: 31.25rem) { .rule { font-size: 1rem } }"; 376 | 377 | expect(processed).toBe(expected); 378 | }); 379 | }); 380 | 381 | describe("minPixelValue", function() { 382 | it("should not replace values below minPixelValue", function() { 383 | var options = { 384 | propWhiteList: [], 385 | minPixelValue: 2 386 | }; 387 | var rules = 388 | ".rule { border: 1px solid #000; font-size: 16px; margin: 1px 10px; }"; 389 | var expected = 390 | ".rule { border: 1px solid #000; font-size: 1rem; margin: 1px 0.625rem; }"; 391 | var processed = postcss(pxtorem(options)).process(rules).css; 392 | 393 | expect(processed).toBe(expected); 394 | }); 395 | }); 396 | 397 | describe("filter-prop-list", function() { 398 | it('should find "exact" matches from propList', function() { 399 | var propList = [ 400 | "font-size", 401 | "margin", 402 | "!padding", 403 | "*border*", 404 | "*", 405 | "*y", 406 | "!*font*" 407 | ]; 408 | var expected = "font-size,margin"; 409 | expect(filterPropList.exact(propList).join()).toBe(expected); 410 | }); 411 | 412 | it('should find "contain" matches from propList and reduce to string', function() { 413 | var propList = [ 414 | "font-size", 415 | "*margin*", 416 | "!padding", 417 | "*border*", 418 | "*", 419 | "*y", 420 | "!*font*" 421 | ]; 422 | var expected = "margin,border"; 423 | expect(filterPropList.contain(propList).join()).toBe(expected); 424 | }); 425 | 426 | it('should find "start" matches from propList and reduce to string', function() { 427 | var propList = [ 428 | "font-size", 429 | "*margin*", 430 | "!padding", 431 | "border*", 432 | "*", 433 | "*y", 434 | "!*font*" 435 | ]; 436 | var expected = "border"; 437 | expect(filterPropList.startWith(propList).join()).toBe(expected); 438 | }); 439 | 440 | it('should find "end" matches from propList and reduce to string', function() { 441 | var propList = [ 442 | "font-size", 443 | "*margin*", 444 | "!padding", 445 | "border*", 446 | "*", 447 | "*y", 448 | "!*font*" 449 | ]; 450 | var expected = "y"; 451 | expect(filterPropList.endWith(propList).join()).toBe(expected); 452 | }); 453 | 454 | it('should find "not" matches from propList and reduce to string', function() { 455 | var propList = [ 456 | "font-size", 457 | "*margin*", 458 | "!padding", 459 | "border*", 460 | "*", 461 | "*y", 462 | "!*font*" 463 | ]; 464 | var expected = "padding"; 465 | expect(filterPropList.notExact(propList).join()).toBe(expected); 466 | }); 467 | 468 | it('should find "not contain" matches from propList and reduce to string', function() { 469 | var propList = [ 470 | "font-size", 471 | "*margin*", 472 | "!padding", 473 | "!border*", 474 | "*", 475 | "*y", 476 | "!*font*" 477 | ]; 478 | var expected = "font"; 479 | expect(filterPropList.notContain(propList).join()).toBe(expected); 480 | }); 481 | 482 | it('should find "not start" matches from propList and reduce to string', function() { 483 | var propList = [ 484 | "font-size", 485 | "*margin*", 486 | "!padding", 487 | "!border*", 488 | "*", 489 | "*y", 490 | "!*font*" 491 | ]; 492 | var expected = "border"; 493 | expect(filterPropList.notStartWith(propList).join()).toBe(expected); 494 | }); 495 | 496 | it('should find "not end" matches from propList and reduce to string', function() { 497 | var propList = [ 498 | "font-size", 499 | "*margin*", 500 | "!padding", 501 | "!border*", 502 | "*", 503 | "!*y", 504 | "!*font*" 505 | ]; 506 | var expected = "y"; 507 | expect(filterPropList.notEndWith(propList).join()).toBe(expected); 508 | }); 509 | }); 510 | 511 | describe("exclude", function() { 512 | it("should ignore file path with exclude RegEx", function() { 513 | var options = { 514 | exclude: /exclude/i 515 | }; 516 | var processed = postcss(pxtorem(options)).process(basicCSS, { 517 | from: "exclude/path" 518 | }).css; 519 | expect(processed).toBe(basicCSS); 520 | }); 521 | 522 | it("should not ignore file path with exclude String", function() { 523 | var options = { 524 | exclude: "exclude" 525 | }; 526 | var processed = postcss(pxtorem(options)).process(basicCSS, { 527 | from: "exclude/path" 528 | }).css; 529 | expect(processed).toBe(basicCSS); 530 | }); 531 | 532 | it("should not ignore file path with exclude function", function() { 533 | var options = { 534 | exclude: function(file) { 535 | return file.indexOf("exclude") !== -1; 536 | } 537 | }; 538 | var processed = postcss(pxtorem(options)).process(basicCSS, { 539 | from: "exclude/path" 540 | }).css; 541 | expect(processed).toBe(basicCSS); 542 | }); 543 | 544 | it("should only replace properties in the prop list with wildcard", function() { 545 | var input = 546 | "h1 { margin: 0 0 20rpx; font-size: 32rpx; line-height: 1.2; letter-spacing: 1rpx; width: 30px;}"; 547 | var output = 548 | "h1 { margin: 0 0 1.25rem; font-size: 2rem; line-height: 1.2; letter-spacing: 0.0625rem; width: 30px;}"; 549 | var options = { 550 | unit: "rpx", 551 | propList: ["*"] 552 | }; 553 | var processed = postcss(pxtorem(options)).process(input).css; 554 | 555 | expect(processed).toBe(output); 556 | }); 557 | }); 558 | --------------------------------------------------------------------------------