Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ npm-debug.log*
node_modules
.idea
postcss-px-to-em-master
.vscode
.history
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Default Options:
- If value is regexp, it checks to see if the selector matches the regexp.
- `[/^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.
- `mediaQuery` (Boolean or Regexp or Array) Allow px to be converted in media queries.
Copy link
Member

Choose a reason for hiding this comment

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

...or Array of Regexp) Allow ...

- `replace` (Boolean) replaces rules containing vw instead of adding fallbacks.
- `exclude` (Array or Regexp) Ignore some files like 'node_modules'
- If value is regexp, will ignore the matches files.
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,11 @@ function declarationExists(decls, prop, value) {
}

function validateParams(params, mediaQuery) {
if (Object.prototype.toString.call(mediaQuery) === '[object RegExp]') {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of Object.prototype.toString.call(mediaQuery) === '[object RegExp]' use:

mediaQuery instanceof RegExp

return mediaQuery.test(params)
} else if (Object.prototype.toString.call(mediaQuery) === '[object Array]') {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of Object.prototype.toString.call(mediaQuery) === '[object Array]' use:

Array.isArray(mediaQuery)

return mediaQuery.some(mq => mq.test(params))
}

return !params || (params && mediaQuery);
}
10 changes: 10 additions & 0 deletions spec/px-to-viewport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ describe('mediaQuery', function () {

expect(processed).toBe(expected);
});

it('should only replace px inside media queries if opts.mediaQuery is a RegExp or RegExp array', function() {
var options = {
mediaQuery: /max-width:\s*750px/
};
var processed = postcss(pxToViewport(options)).process('h1 { font-size: 32px } @media (max-width: 750px) { .rule { font-size: 16px } } @media (min-width: 751px) { .rule { font-size: 18px } }').css;
var expected = 'h1 { font-size: 32px } @media (max-width: 750px) { .rule { font-size: 5vw } } @media (min-width: 751px) { .rule { font-size: 18px } }';

expect(processed).toBe(expected);
});

it('should replace px inside media queries if it has params orientation landscape and landscape option', function() {
var options = {
Expand Down