Skip to content

Commit

Permalink
Fix TransformAtts function
Browse files Browse the repository at this point in the history
  • Loading branch information
matc4 committed Jan 18, 2017
1 parent 2ed98c9 commit 642af5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const SVG_ATTS = ['viewBox'];
const G_ATTS = ['id'];
const CIRCLE_ATTS = ['cx', 'cy', 'r', 'fill', 'stroke'];
const PATH_ATTS = ['d', 'fill', 'stroke'];
const RECT_ATTS = ['width', 'height', 'fill', 'stroke'];
const RECT_ATTS = ['width', 'height', 'fill', 'stroke', 'x', 'y'];
const LINEARG_ATTS = ['id', 'x1', 'y1', 'x2', 'y2'];
const RADIALG_ATTS = ['id', 'cx', 'cy', 'r'];
const STOP_ATTS = ['offset'];
Expand Down Expand Up @@ -150,15 +150,22 @@ class SvgUri extends Component{
}

obtainComponentAtts({attributes}, enabledAttributes) {
return Array.from(attributes)
let styleAtts = {};
Array.from(attributes).forEach(({nodeName, nodeValue}) => {
Object.assign(styleAtts, utils.transformStyle(nodeName, nodeValue, this.props.fill));
});

let componentAtts = Array.from(attributes)
.map(utils.camelCaseNodeName)
.map(utils.removePixelsFromNodeValue)
.map(utils.transformStyle)
.filter(utils.getEnabledAttributes(enabledAttributes))
.reduce((acc, {nodeName, nodeValue}) => ({
...acc,
[nodeName]: this.props.fill && nodeName === 'fill' ? this.props.fill : nodeValue,
}), {});
Object.assign(componentAtts, styleAtts);

return componentAtts;
}

inspectNode(node){
Expand Down
12 changes: 9 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
export const camelCase = value => value.replace(/-([a-z])/g, g => g[1].toUpperCase());

export const camelCaseNodeName = ({nodeName, nodeValue}) => ({nodeName: camelCase(nodeName), nodeValue});

export const removePixelsFromNodeValue = ({nodeName, nodeValue}) => ({nodeName, nodeValue: nodeValue.replace('px', '')});
export const transformStyle = ({nodeName, nodeValue}) => {

export const transformStyle = (nodeName, nodeValue, fillProp) => {
if (nodeName === 'style') {
return nodeValue.split(';')
.reduce((acc, attribute) => {
const [property, value] = attribute.split(':');
return {...acc, [camelCase(property)]: value};
if (property == "")
return acc;
else
return {...acc, [camelCase(property)]: fillProp && property === 'fill' ? fillProp : value};
}, {});
}
return {nodeName, nodeValue};
return null;
};

export const getEnabledAttributes = enabledAttributes => ({nodeName}) => enabledAttributes.includes(nodeName);

0 comments on commit 642af5b

Please sign in to comment.