-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f883dc
commit 17c7277
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/app/scripts/svgo/plugins/removeUselessStrokeAndFill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters