Skip to content
Merged
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
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) {
Copy link
Owner

Choose a reason for hiding this comment

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

Love the JSDoc! Thanks!

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(
Copy link
Owner

Choose a reason for hiding this comment

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

🙌

'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', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

🙌

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