From c15c1fa2ecb35a7a4aa434b991bc905f765a1734 Mon Sep 17 00:00:00 2001 From: Ivan Bunin Date: Thu, 24 Jan 2019 15:18:52 +0300 Subject: [PATCH 1/2] add replace flag --- index.js | 20 +++++++++++++++++--- spec/px-to-viewport.spec.js | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7bcc46a..d2bb878 100755 --- a/index.js +++ b/index.js @@ -12,7 +12,8 @@ var defaults = { fontViewportUnit: 'vw', // vmin is more suitable. selectorBlackList: [], minPixelValue: 1, - mediaQuery: false + mediaQuery: false, + replace: true }; module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { @@ -36,9 +37,16 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) { if (blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) return; - var unit = getUnit(decl.prop, opts) + var unit = getUnit(decl.prop, opts); + var value = decl.value.replace(pxRegex, createPxReplace(opts.viewportWidth, opts.minPixelValue, opts.unitPrecision, unit)); - decl.value = decl.value.replace(pxRegex, createPxReplace(opts.viewportWidth, opts.minPixelValue, opts.unitPrecision, unit)); + if (declarationExists(decl.parent, decl.prop, value)) return; + + if (opts.replace) { + decl.value = value; + } else { + decl.parent.insertAfter(i, decl.clone({ value: value })); + } }); if (opts.mediaQuery) { @@ -77,3 +85,9 @@ function blacklistedSelector(blacklist, selector) { return selector.match(regex); }); } + +function declarationExists(decls, prop, value) { + return decls.some(function (decl) { + return (decl.prop === prop && decl.value === value); + }); +} \ No newline at end of file diff --git a/spec/px-to-viewport.spec.js b/spec/px-to-viewport.spec.js index 23632e2..f530ac5 100644 --- a/spec/px-to-viewport.spec.js +++ b/spec/px-to-viewport.spec.js @@ -36,6 +36,13 @@ describe('px-to-viewport', function() { expect(processed).toBe(expected); }); + + it('should not add properties that already exist', function () { + var expected = '.rule { font-size: 16px; font-size: 5vw; }'; + var processed = postcss(pxToViewport()).process(expected).css; + + expect(processed).toBe(expected); + }); }); describe('value parsing', function() { @@ -196,3 +203,15 @@ describe('minPixelValue', function () { expect(processed).toBe(expected); }); }); + +describe('replace', function () { + it('should leave fallback pixel unit with root em value', function () { + var options = { + replace: false + }; + var processed = postcss(pxToViewport(options)).process(basicCSS).css; + var expected = '.rule { font-size: 15px; font-size: 4.6875vw }'; + + expect(processed).toBe(expected); + }); +}); \ No newline at end of file From 4dd92416539920c150cf53d2d66f3dcbd590ca7b Mon Sep 17 00:00:00 2001 From: Ivan Bunin Date: Fri, 25 Jan 2019 14:41:20 +0300 Subject: [PATCH 2/2] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 21a16c5..ba118f4 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ Default: fontViewportUnit: 'vw', // vmin is more suitable. selectorBlackList: [], minPixelValue: 1, + replace: true, mediaQuery: false } ``` @@ -111,6 +112,7 @@ Default: - `[/^body$/]` will match `body` but not `.body` - `minPixelValue` (Number) Set the minimum pixel value to replace. - `mediaQuery` (Boolean) Allow px to be converted in media queries. +- `replace` (Boolean) replaces rules containing rems instead of adding fallbacks. ### Use with gulp-postcss