Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjlockwood committed Apr 2, 2018
1 parent c23b791 commit ef111af
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 502 deletions.
21 changes: 8 additions & 13 deletions src/app/scripts/svgo/plugins/cleanupNumericValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ export const cleanupNumericValues = {
floatPrecision: 10,
leadingZero: true,
defaultPx: true,
convertToPx: true
convertToPx: true,
},
};

const regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/;
const removeLeadingZero = tools.removeLeadingZero;
const absoluteLengths = { // relative to px
const absoluteLengths = {
// relative to px
cm: 96 / 2.54,
mm: 96 / 25.4,
in: 96,
pt: 4 / 3,
pc: 16
pc: 16,
};

/**
Expand All @@ -33,12 +34,10 @@ const absoluteLengths = { // relative to px
* @return {Boolean} if false, item will be filtered out
*/
function cleanupNumericValuesFn(item, params) {

if (item.isElem()) {

var match;

item.eachAttr(function (attr) {
item.eachAttr(function(attr) {
match = attr.value.match(regNumericValues);

// if attribute value matches regNumericValues
Expand All @@ -48,12 +47,10 @@ function cleanupNumericValuesFn(item, params) {
units = match[3] || '';

// convert absolute values to pixels
if (params.convertToPx && units && (units in absoluteLengths)) {
if (params.convertToPx && units && units in absoluteLengths) {
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(params.floatPrecision);

if (String(pxNum).length < match[0].length)
num = pxNum,
units = 'px';
if (String(pxNum).length < match[0].length) (num = pxNum), (units = 'px');
}

// and remove leading zero
Expand All @@ -69,7 +66,5 @@ function cleanupNumericValuesFn(item, params) {
attr.value = num + units;
}
});

}

};
}
37 changes: 19 additions & 18 deletions src/app/scripts/svgo/plugins/collapseGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ const animationElems = collections.elemsGroups.animation;

function hasAnimatedAttr(item) {
/* jshint validthis:true */
return item.isElem(animationElems) && item.hasAttr('attributeName', this) ||
!item.isEmpty() && item.content.some(hasAnimatedAttr, this);
return (
(item.isElem(animationElems) && item.hasAttr('attributeName', this)) ||
(!item.isEmpty() && item.content.some(hasAnimatedAttr, this))
);
}

/*
Expand All @@ -40,26 +42,23 @@ function hasAnimatedAttr(item) {
* @return {Boolean} if false, item will be filtered out
*/
function collapseGroupsFn(item) {

// non-empty elements
if (item.isElem() && !item.isElem('switch') && !item.isEmpty()) {

item.content.forEach(function (g, i) {

item.content.forEach(function(g, i) {
// non-empty groups
if (g.isElem('g') && !g.isEmpty()) {

// move group attibutes to the single content element
if (g.hasAttr() && g.content.length === 1) {
var inner = g.content[0];

if (inner.isElem() && !inner.hasAttr('id') &&
!(g.hasAttr('class') && inner.hasAttr('class')) && (
!g.hasAttr('clip-path') && !g.hasAttr('mask') ||
inner.isElem('g') && !g.hasAttr('transform') && !inner.hasAttr('transform')
)
if (
inner.isElem() &&
!inner.hasAttr('id') &&
!(g.hasAttr('class') && inner.hasAttr('class')) &&
((!g.hasAttr('clip-path') && !g.hasAttr('mask')) ||
(inner.isElem('g') && !g.hasAttr('transform') && !inner.hasAttr('transform')))
) {
g.eachAttr(function (attr) {
g.eachAttr(function(attr) {
if (g.content.some(hasAnimatedAttr, attr.name)) return;

if (!inner.hasAttr(attr.name)) {
Expand All @@ -79,13 +78,15 @@ function collapseGroupsFn(item) {
}

// collapse groups without attributes
if (!g.hasAttr() && !g.content.some(function (item) { return item.isElem(animationElems) })) {
if (
!g.hasAttr() &&
!g.content.some(function(item) {
return item.isElem(animationElems);
})
) {
item.spliceContent(i, 1, g.content);
}
}

});

}

};
}
Loading

0 comments on commit ef111af

Please sign in to comment.