Skip to content

Commit 61007cc

Browse files
authored
Merge pull request #23 from /issues/22-multiple-functions
Issues/22 multiple functions
2 parents ef310bc + e805ad2 commit 61007cc

File tree

5 files changed

+65
-42
lines changed

5 files changed

+65
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
This project adheres to [Semantic Versioning](http://semver.org/).
33

4+
## 0.3.4
5+
6+
**Fixed**
7+
8+
- Fixes an issue where only the first of multiple `tidy-` functions in the same declaration value was being processed (#22)
9+
410
## 0.3.3
511

612
**Changed**

lib/tidy-function.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,60 @@ const cleanClone = require('./cleanClone');
1212
*/
1313
module.exports = function tidyFunction(declaration, tidy) {
1414
const FUNCTION_REGEX = /tidy-(span|offset)(|-full)\(([\d.-]+)\)/;
15+
const globalRegExp = new RegExp(FUNCTION_REGEX, 'g');
16+
const localRegExp = new RegExp(FUNCTION_REGEX);
1517

16-
if (FUNCTION_REGEX.test(declaration.value)) {
18+
if (localRegExp.test(declaration.value)) {
1719
const { grid } = tidy;
18-
/**
19-
* match: The full function expression.
20-
* slug: One of either `span` or `offset`.
21-
* modifier: One of either `undefined` or `-full`.
22-
* value: The function's argument.
23-
*/
24-
const [match, slug, modifier, value] = declaration.value.match(FUNCTION_REGEX);
20+
const fullMatch = declaration.value.match(globalRegExp);
2521

2622
/**
27-
* Get the span or offset `calc()` value(s).
23+
* Find all matches in the declaration value.
2824
*
29-
* fluid: calc() function based on 100vw base.
30-
* full: calc() function based on `siteMax` base.
25+
* @param {String} acc The accumulator, based on declaration.value
26+
* @param {String} tidyMatch The full tidy function match(es)
27+
*
28+
* @return {String} The replacement value for the declaration
3129
*/
32-
let { fluid, full } = ('span' === slug) ?
33-
grid.spanCalc(value) :
34-
grid.offsetCalc(value);
30+
const replaceWithValue = fullMatch.reduce((acc, tidyMatch) => {
31+
/**
32+
* match: The full function expression.
33+
* slug: One of either `span` or `offset`.
34+
* modifier: One of either `undefined` or `-full`.
35+
* value: The function's argument.
36+
*/
37+
const [match, slug, modifier, value] = tidyMatch.match(localRegExp);
3538

36-
/**
37-
* If the tidy- function is nested in a calc() function, remove 'calc'
38-
* from the span/offset values.
39-
*/
40-
if (/^calc\(.*\)$/.test(declaration.value)) {
41-
[fluid, full] = [fluid, full].map(calc => (
42-
(undefined !== calc) ? calc.replace('calc', '') : calc));
43-
}
39+
/**
40+
* Get the span or offset `calc()` value(s).
41+
*
42+
* fluid: calc() function based on 100vw base.
43+
* full: calc() function based on `siteMax` base.
44+
*/
45+
let { fluid, full } = ('span' === slug) ?
46+
grid.spanCalc(value) :
47+
grid.offsetCalc(value);
48+
49+
acc = ('-full' === modifier) ?
50+
// tidy-[span|offset]-full()
51+
acc.replace(match, full) :
52+
// tidy-[span|offset] ()
53+
acc.replace(match, fluid);
54+
55+
/**
56+
* Remove nested calc() function resulting from the tidy-* function replacement.
57+
*/
58+
const NESTED_CALC_REGEX = /(calc[(\s]+)(calc\()/;
59+
return (NESTED_CALC_REGEX.test(acc)) ? acc.replace(NESTED_CALC_REGEX, '$1(') : acc;
60+
}, declaration.value);
4461

4562
// Replace declaration(s) with cloned and updated declarations.
46-
if ('-full' === modifier) {
47-
// tidy-[span|offset]-full()
48-
declaration.replaceWith(cleanClone(
49-
declaration,
50-
{
51-
prop: declaration.prop,
52-
value: declaration.value.replace(match, full),
53-
},
54-
));
55-
} else {
56-
// tidy-[span|offset] ()
57-
declaration.replaceWith(cleanClone(
58-
declaration,
59-
{
60-
prop: declaration.prop,
61-
value: declaration.value.replace(match, fluid),
62-
},
63-
));
64-
}
63+
declaration.replaceWith(cleanClone(
64+
declaration,
65+
{
66+
prop: declaration.prop,
67+
value: replaceWithValue,
68+
},
69+
));
6570
}
6671
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-tidy-columns",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"description": "PostCSS plugin to manage column and margin alignment.",
55
"keywords": [
66
"postcss",

test/fixtures/full-suite.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
max-width: calc(tidy-span-full(2) + 20px);
3232
}
3333

34+
/* Multiple `tidy-*` functions in the same property value */
35+
.span-function--multi {
36+
padding: 0 tidy-offset(2) 0 tidy-offset-full(3);
37+
margin: 0 calc(tidy-offset-full(2) + 50px) 0 tidy-offset(1);
38+
}
39+
3440
/* `tidy-offset` with all expected values */
3541
.offset-shorthand {
3642
tidy-offset: 3 / 4;

test/fixtures/full-suite.expected.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
max-width: calc(((((90rem - 0.625rem * 2) / 12 - 1.1458rem) * 2) + 1.25rem) + 20px);
4141
}
4242

43+
/* Multiple `tidy-*` functions in the same property value */
44+
.span-function--multi {
45+
padding: 0 calc((((100vw - 0.625rem * 2) / 12 - 1.1458rem) * 2) + 1.25rem * 2) 0 calc((((90rem - 0.625rem * 2) / 12 - 1.1458rem) * 3) + 1.25rem * 3);
46+
margin: 0 calc(((((90rem - 0.625rem * 2) / 12 - 1.1458rem) * 2) + 1.25rem * 2) + 50px) 0 calc(((100vw - 0.625rem * 2) / 12 - 1.1458rem) + 1.25rem);
47+
}
48+
4349
/* `tidy-offset` with all expected values */
4450
.offset-shorthand {
4551
margin-left: calc((((100vw - 0.625rem * 2) / 12 - 1.1458rem) * 3) + 1.25rem * 3);

0 commit comments

Comments
 (0)