Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjlockwood committed Mar 12, 2017
1 parent 6f883dc commit 17c7277
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/app/scripts/import/SvgLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export function loadVectorLayerFromSvgString(svgString: string): VectorLayer {
];
}

extractDefsMapFn(doc.documentElement);
const rootLayer = nodeToLayerDataFn(documentElement, docElContext);
const id = makeFinalNodeIdFn(documentElement, 'vector');
const childrenLayers = rootLayer ? rootLayer.children : undefined;
Expand Down
88 changes: 88 additions & 0 deletions src/app/scripts/svgo/plugins/removeUselessStrokeAndFill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* tslint:disable */

import * as collections from './_collections';

export const removeUselessStrokeAndFill = {
active: true,
type: 'perItem',
fn: removeUselessStrokeAndFillFn,
params: {
stroke: true,
fill: true,
removeNone: false,
hasStyleOrScript: false
},
};

const shape = collections.elemsGroups.shape;
const regStrokeProps = /^stroke/;
const regFillProps = /^fill-/;
const styleOrScript = ['style', 'script'];

/**
* Remove useless stroke and fill attrs.
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*/
function removeUselessStrokeAndFillFn(item, params) {
if (item.isElem(styleOrScript)) {
params.hasStyleOrScript = true;
}

if (!params.hasStyleOrScript && item.isElem(shape) && !item.computedAttr('id')) {
const stroke = params.stroke && item.computedAttr('stroke');
const fill = params.fill && !item.computedAttr('fill', 'none');

// remove stroke*
if (params.stroke && (!stroke
|| stroke == 'none'
|| item.computedAttr('stroke-opacity', '0')
|| item.computedAttr('stroke-width', '0'))) {
const parentStroke = item.parentNode.computedAttr('stroke');
const declineStroke = parentStroke && parentStroke != 'none';

item.eachAttr(function (attr) {
if (regStrokeProps.test(attr.name)) {
item.removeAttr(attr.name);
}
});

if (declineStroke) {
item.addAttr({
name: 'stroke',
value: 'none',
prefix: '',
local: 'stroke'
});
}
}

// remove fill*
if (params.fill && (!fill || item.computedAttr('fill-opacity', '0'))) {
item.eachAttr(function (attr) {
if (regFillProps.test(attr.name)) {
item.removeAttr(attr.name);
}
});
if (fill) {
if (item.hasAttr('fill')) {
item.attr('fill').value = 'none';
} else {
item.addAttr({
name: 'fill',
value: 'none',
prefix: '',
local: 'fill'
});
}
}
}
if (params.removeNone
&& (!stroke || item.hasAttr('stroke') && item.attr('stroke').value == 'none')
&& (!fill || item.hasAttr('fill') && item.attr('fill').value == 'none')) {
return false;
}
}
};
16 changes: 16 additions & 0 deletions src/app/scripts/svgo/plugins/replaceUseElems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const replaceUseElems = {
active: true,
type: 'perItem',
fn: replaceUseElemsFn,
params: undefined,
};

/**
* Replace <use> elems with their referenced content.
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*/
function replaceUseElemsFn(item) {
return !item.isElem('metadata');
}
5 changes: 4 additions & 1 deletion src/app/scripts/svgo/svgo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { removeUnknownsAndDefaults } from './plugins/removeUnknownsAndDefaults';
import { removeEmptyText } from './plugins/removeEmptyText';
import { removeEmptyAttrs } from './plugins/removeEmptyAttrs';
import { removeNonInheritableGroupAttrs } from './plugins/removeNonInheritableGroupAttrs';
import { replaceUseElems } from './plugins/replaceUseElems';
import { removeUselessStrokeAndFill } from './plugins/removeUselessStrokeAndFill';

// The complete list is available here: https://github.com/svg/svgo/blob/master/.svgo.yml
const svgoPlugins = {
Expand All @@ -34,6 +36,7 @@ const svgoPlugins = {
inlineStyles,
// minifyStyles: require('svgo/plugins/minifyStyles'),
convertStyleToAttrs,
// replaceUseElems,
// cleanupIDs: require('svgo/plugins/cleanupIDs'),
// removeRasterImages: require('svgo/plugins/removeRasterImages'), // disabled
removeUselessDefs,
Expand All @@ -42,7 +45,7 @@ const svgoPlugins = {
// convertColors: require('svgo/plugins/convertColors'),
removeUnknownsAndDefaults,
removeNonInheritableGroupAttrs,
// removeUselessStrokeAndFill: require('svgo/plugins/removeUselessStrokeAndFill'),
removeUselessStrokeAndFill,
// removeViewBox: require('svgo/plugins/removeViewBox'), // disabled
// cleanupEnableBackground: require('svgo/plugins/cleanupEnableBackground'),
removeHiddenElems,
Expand Down

0 comments on commit 17c7277

Please sign in to comment.