Skip to content

Commit

Permalink
Merge pull request #436 from Lucassifoni/master
Browse files Browse the repository at this point in the history
Fixes spaces in fractions causing invalid output (#435)
  • Loading branch information
peterramsing authored Feb 9, 2019
2 parents 78be8b8 + b0978ca commit 2d1e71c
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 8 deletions.
12 changes: 11 additions & 1 deletion lib/_lu-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ const testRgb = new RegExp(/rgb/);
const matchRgb = new RegExp(/rgba?\(([^\)]+)\)/);
const testHex = new RegExp(/#\w+/);

/**
* Glues fraction members, meaning "1 / 6" becomes "1/6"
* @param {string} str
* @returns {string}
*/
const glueFractionMembers = function glueFractionMembers(str) {
return str.replace(/\s*\/\s*/, '/');
};

/**
* Returns a three-member number array from a hex string
* @param hex
Expand Down Expand Up @@ -97,5 +106,6 @@ module.exports = {
extractRgbSubstring,
hToD,
safeRgbToRgb,
safeHexToRgb
safeHexToRgb,
glueFractionMembers
};
4 changes: 3 additions & 1 deletion lib/lost-center.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var newBlock = require('./new-block.js');

var lgLogic = require('./_lg-logic');
var lgUtils = require('./_lu-utilities');

module.exports = function lostCenterDecl(css, settings, result) {
css.walkDecls('lost-center', function lostCenterFunction(decl) {
Expand All @@ -18,7 +19,8 @@ module.exports = function lostCenterDecl(css, settings, result) {
return lostFractionPattern.test(value);
};

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');

if (declArr[1] !== undefined && declArr[1].search(/^\d/) !== -1) {
lostCenterPadding = declArr[1];
Expand Down
5 changes: 4 additions & 1 deletion lib/lost-column.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var newBlock = require('./new-block.js');

var lgLogic = require('./_lg-logic');
var lgUtils = require('./_lu-utilities');

module.exports = function lostColumnDecl(css, settings, result) {
css.walkDecls('lost-column', function lostColumnFunction(decl) {
var declArr = [];
Expand All @@ -20,7 +22,8 @@ module.exports = function lostColumnDecl(css, settings, result) {
lostColumnCycle = settings.cycle;
}

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostColumn = declArr[0];

if (declArr[1] !== undefined && declArr[1].search(/^\d/) !== -1) {
Expand Down
5 changes: 4 additions & 1 deletion lib/lost-masonry-column.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var lgUtils = require('./_lu-utilities');

module.exports = function lostMasonryColumnDecl(css, settings) {
css.walkDecls('lost-masonry-column', function lostMasonryColumnFunction(
decl
Expand All @@ -18,7 +20,8 @@ module.exports = function lostMasonryColumnDecl(css, settings) {
});
}

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostMasonryColumn = declArr[0];

if (declArr[1] !== undefined && declArr[1].search(/^\d/) !== -1) {
Expand Down
5 changes: 4 additions & 1 deletion lib/lost-move.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var lgUtils = require('./_lu-utilities');

module.exports = function lostMoveDecl(css, settings) {
css.walkDecls('lost-move', function lostMoveDeclFunction(decl) {
var declArr = [];
Expand All @@ -6,7 +8,8 @@ module.exports = function lostMoveDecl(css, settings) {
var lostMoveRounder = settings.rounder;
var lostMoveGutter = settings.gutter;

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostMove = declArr[0];

if (
Expand Down
5 changes: 4 additions & 1 deletion lib/lost-offset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var lgUtils = require('./_lu-utilities');

module.exports = function lostOffsetDecl(css, settings) {
css.walkDecls('lost-offset', function lostOffsetDeclFunction(decl) {
var declArr = [];
Expand All @@ -16,7 +18,8 @@ module.exports = function lostOffsetDecl(css, settings) {
});
}

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostOffset = declArr[0];
lostOffsetNumerator = declArr[0].split('/')[0];

Expand Down
4 changes: 3 additions & 1 deletion lib/lost-row.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var newBlock = require('./new-block.js');

var lgLogic = require('./_lg-logic');
var lgUtils = require('./_lu-utilities');

module.exports = function lostRowDecl(css, settings, result) {
css.walkDecls('lost-row', function lostRowDeclFunction(decl) {
Expand All @@ -13,7 +14,8 @@ module.exports = function lostRowDecl(css, settings, result) {
var validUnits = ['%', 'vh'];

if (decl.value !== 'none') {
declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostRow = declArr[0];

if (declArr[1] !== undefined && declArr[1].search(/^\d/) !== -1) {
Expand Down
4 changes: 3 additions & 1 deletion lib/lost-waffle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var newBlock = require('./new-block.js');

var lgLogic = require('./_lg-logic');
var lgUtils = require('./_lu-utilities');

module.exports = function lostWaffleDecl(css, settings) {
css.walkDecls('lost-waffle', function lostWaffleDeclFunction(decl) {
Expand Down Expand Up @@ -30,7 +31,8 @@ module.exports = function lostWaffleDecl(css, settings) {
lostWaffleCycle = settings.cycle;
}

declArr = decl.value.split(' ');
const sanitizedDecl = lgUtils.glueFractionMembers(decl.value);
declArr = sanitizedDecl.split(' ');
lostWaffle = declArr[0];

if (declArr[1] !== undefined && declArr[1].search(/^\d/) !== -1) {
Expand Down
10 changes: 10 additions & 0 deletions test/lost-center.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe('lost-center', function() {
);
});

it('generates valid output given spaces are present in the input', function() {
check(
'a { lost-center: 3 / 9; lost-unit: vw }',

'a { max-width: calc(99.9vw * 3/9); margin-left: auto; margin-right: auto }\n' +
"a:before { content: ''; display: table }\n" +
"a:after { content: ''; display: table; clear: both }"
);
});

it('horizontally centers container', function() {
check(
'a { lost-center: 980px }',
Expand Down
11 changes: 11 additions & 0 deletions test/lost-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ describe('lost-column', function() {
);
});

it('generates valid output even with spaces at various places in the declaration', function() {
check(
'a { lost-column: 1 / 3; }',
'a { width: calc(99.9% * 1/3 - (30px - 30px * 1/3)); }\n' +
'a:nth-child(1n) { float: left; margin-right: 30px; clear: none; }\n' +
'a:last-child { margin-right: 0; }\n' +
'a:nth-child(3n) { margin-right: 0; float: right; }\n' +
'a:nth-child(3n + 1) { clear: both; }'
);
});

it('provides 2/5 column layout', function() {
check(
'a { lost-column: 2/5; }',
Expand Down
8 changes: 8 additions & 0 deletions test/lost-move.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('lost-move', function() {
);
});

it('generates valid output even with spaces at various places in the declaration', function() {
check(
'a { lost-move: -1 / 3; }',
'a { position: relative; left: calc(99.9% * -1/3 -' +
' (30px - 30px * -1/3) + 30px); }'
);
});

it('moves element up', function() {
check(
'a { lost-move: 1/3 column; }',
Expand Down
8 changes: 8 additions & 0 deletions test/lost-offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('lost-offset', function() {
);
});

it('generates valid output even with spaces at various places in the declaration', function() {
check(
'a { lost-offset: 1 / 3; }',
'a { margin-left: calc(99.9% * (-1/3 * -1) - (30px - 30px * (-1/3 * -1)) + 30px' +
') !important; }'
);
});

it("Does not move with a zero numerator (of you're so inclined)", function() {
check(
'a { lost-offset: 0/3; }',
Expand Down
9 changes: 9 additions & 0 deletions test/lost-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ describe('lost-row', function() {
);
});

it('generates valid output even with spaces at various places in the declaration', function() {
check(
'a { lost-row: 1 / 3; lost-row-flexbox: no-flex; }',
'a { width: 100%; height: calc(99.9% * 1/3 - (30px - 30px * 1/3));' +
' margin-bottom: 30px; }' +
'a:last-child { margin-bottom: 0; }'
);
});

it('supports flex in long-form', function() {
check(
'a { lost-row: 1/3; lost-row-flexbox: flex; }',
Expand Down
15 changes: 15 additions & 0 deletions test/lost-waffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ describe('lost-waffle', function() {
);
});

it('generates valid output even with spaces at various places in the declaration', function() {
check(
'a { lost-waffle: 1 / 3; }',
'a { width: calc(99.9% * 1/3 - (30px - 30px * 1/3));' +
' max-width: calc(99.9% * 1/3 - (30px - 30px * 1/3));\n' +
' height: calc(99.9% * 1/3 - (30px - 30px * 1/3)); }\n' +
'a:nth-child(1n) { float: left; margin-right: 30px;' +
' margin-bottom: 30px; clear: none; }\n' +
'a:last-child { margin-right: 0; margin-bottom: 0; }\n' +
'a:nth-child(3n) { margin-right: 0; }\n' +
'a:nth-child(3n + 1) { clear: both; }\n' +
'a:nth-last-child(-n + 3) { margin-bottom: 0; }'
);
});

it('supports a custom cycle', function() {
check(
'a { lost-waffle: 2/4 2; }',
Expand Down
8 changes: 8 additions & 0 deletions test/lu-utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
var expect = require('chai').expect;
var utils = require('../lib/_lu-utilities.js');

describe('glueFractionMembers', () => {
it('glues fraction members together, avoiding a class of parsing errors', () => {
expect(utils.glueFractionMembers('-1 / 8')).to.equal('-1/8');
expect(utils.glueFractionMembers('27 / 32')).to.equal('27/32');
expect(utils.glueFractionMembers('1 /1')).to.equal('1/1');
});
});

describe('hToD', () => {
it('converts one or two hex digits to an int value', () => {
expect(utils.hToD(0, 0)).to.equal(0);
Expand Down

0 comments on commit 2d1e71c

Please sign in to comment.