Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug in the background image-set function #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
167 changes: 88 additions & 79 deletions lib/shorthanders/background.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,88 @@
module.exports = function(shorthand, declarations) {
var bgProps = {};

shorthand.properties.forEach(function(property) {
bgProps[property] = (declarations[property] ? declarations[property].value.split(',') : []);

// Unify bg layers that have commas because of things like line-gradient
var unifiedLayers = [];
var inParenthese = false;
bgProps[property].forEach(function(bgLayer) {
if (inParenthese) {
unifiedLayers[unifiedLayers.length - 1] = unifiedLayers[unifiedLayers.length - 1] += ',' + bgLayer;

if (bgLayer.indexOf(')') > -1) {
inParenthese = false;
}
}
else {
unifiedLayers.push(bgLayer);

if (bgLayer.indexOf('(') > -1) {
inParenthese = true;
}
}
});

bgProps[property] = unifiedLayers;
});

var nbLayers = bgProps['background-image'].length;
var bgLayerProperties = ['background-image', 'background-repeat', 'background-position', 'background-attachment', 'background-origin', 'background-clip', 'background-size'];

// Define default bg position if it's undefined and we have defined a bg size
if (!bgProps['background-position'][0] && bgProps['background-size'][0]) {
bgProps['background-position'][0] = '0 0';
}

// Replicate properties that are just defined once accross all layers
bgLayerProperties.forEach(function(layerProperty) {
if (bgProps[layerProperty].length < nbLayers) {
for (var i = bgProps[layerProperty].length ; i < nbLayers ; i++) {
bgProps[layerProperty].push(bgProps[layerProperty][0]);
}
}
});

// Stringify the properties
var shorthandedProperty = "";

for (var i = 0 ; i < nbLayers ; i++) {
var layerShorthand = shorthand.shorthandSyntax;
if (i > 0) layerShorthand = ', ' + layerShorthand;

bgLayerProperties.forEach(function(bgLayerProperty) {
if (bgProps[bgLayerProperty][i]) {
layerShorthand = layerShorthand.replace(bgLayerProperty, bgProps[bgLayerProperty][i]);
}
else {
layerShorthand = layerShorthand.replace(bgLayerProperty, '');
}

layerShorthand = layerShorthand.replace('background-color', '');
});

// Separate background-size from background-position with a slash only if background-size is specified
if (bgProps['background-size'][i]) {
layerShorthand = layerShorthand.replace('{{bg-position-size-slash}}', '/');
}
else {
layerShorthand = layerShorthand.replace('{{bg-position-size-slash}}', '');
}

shorthandedProperty += layerShorthand.trim();
}

shorthandedProperty += ' ' + (bgProps['background-color'][0] || "");

return shorthandedProperty.replace(/\s+/g, ' ').trim();
};
module.exports = function (shorthand, declarations) {
var bgProps = {};

shorthand.properties.forEach(function (property) {
bgProps[property] = (declarations[property] ? declarations[property].value.split(',') : []);

// Unify bg layers that have commas because of things like line-gradient
var unifiedLayers = [];
var inParenthese = false;

bgProps[property].forEach(function (bgLayer) {
if (inParenthese) {
unifiedLayers[unifiedLayers.length - 1] = unifiedLayers[unifiedLayers.length - 1] += ',' + bgLayer;

//! FIX for image-set function and gradient
if (unifiedLayers[unifiedLayers.length - 1].includes('image-set') || unifiedLayers[unifiedLayers.length - 1].includes('gradient')) {
if (bgLayer.indexOf(')') > -1 && bgLayer[bgLayer.lenght] === ')') {
inParenthese = false;
}
} else {
if (bgLayer.indexOf(')') > -1) {
inParenthese = false;
}
}
}
else {
unifiedLayers.push(bgLayer);

if (bgLayer.indexOf('(') > -1) {
inParenthese = true;
}
}

});
bgProps[property] = unifiedLayers;

});

var nbLayers = bgProps['background-image'].length;
var bgLayerProperties = ['background-image', 'background-repeat', 'background-position', 'background-attachment', 'background-origin', 'background-clip', 'background-size'];

// Define default bg position if it's undefined and we have defined a bg size
if (!bgProps['background-position'][0] && bgProps['background-size'][0]) {
bgProps['background-position'][0] = '0 0';
}

// Replicate properties that are just defined once accross all layers
bgLayerProperties.forEach(function (layerProperty) {
if (bgProps[layerProperty].length < nbLayers) {
for (var i = bgProps[layerProperty].length; i < nbLayers; i++) {
bgProps[layerProperty].push(bgProps[layerProperty][0]);
}
}
});

// Stringify the properties
var shorthandedProperty = "";

for (var i = 0; i < nbLayers; i++) {
var layerShorthand = shorthand.shorthandSyntax;
if (i > 0) layerShorthand = ', ' + layerShorthand;

bgLayerProperties.forEach(function (bgLayerProperty) {
if (bgProps[bgLayerProperty][i]) {
layerShorthand = layerShorthand.replace(bgLayerProperty, bgProps[bgLayerProperty][i]);
}
else {
layerShorthand = layerShorthand.replace(bgLayerProperty, '');
}

layerShorthand = layerShorthand.replace('background-color', '');
});

// Separate background-size from background-position with a slash only if background-size is specified
if (bgProps['background-size'][i]) {
layerShorthand = layerShorthand.replace('{{bg-position-size-slash}}', '/');
}
else {
layerShorthand = layerShorthand.replace('{{bg-position-size-slash}}', '');
}

shorthandedProperty += layerShorthand.trim();
}

shorthandedProperty += ' ' + (bgProps['background-color'][0] || "");

return shorthandedProperty.replace(/\s+/g, ' ').trim();
};