Skip to content

Commit b60f667

Browse files
committedMay 3, 2017
Add support for various attributes and polyline component
1 parent 662ae5c commit b60f667

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
 

‎index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ const ACEPTED_SVG_ELEMENTS = [
3939
// Attributes from SVG elements that are mapped directly.
4040
const SVG_ATTS = ['viewBox'];
4141
const G_ATTS = ['id'];
42-
const CIRCLE_ATTS = ['cx', 'cy', 'r', 'fill', 'stroke'];
43-
const PATH_ATTS = ['d', 'fill', 'stroke'];
44-
const RECT_ATTS = ['width', 'height', 'fill', 'stroke', 'x', 'y'];
42+
const CIRCLE_ATTS = ['cx', 'cy', 'r'];
43+
const PATH_ATTS = ['d'];
44+
const RECT_ATTS = ['width', 'height'];
4545
const LINEARG_ATTS = ['id', 'x1', 'y1', 'x2', 'y2'];
4646
const RADIALG_ATTS = ['id', 'cx', 'cy', 'r'];
4747
const STOP_ATTS = ['offset'];
48-
const ELLIPSE_ATTS = ['fill', 'cx', 'cy', 'rx', 'ry'];
48+
const ELLIPSE_ATTS = ['cx', 'cy', 'rx', 'ry'];
4949
const POLYGON_ATTS = ['points'];
50+
const POLYLINE_ATTS = ['points'];
51+
52+
const COMMON_ATTS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeOpacity', 'strokeLinecap', 'strokeLinejoin',
53+
'strokeDasharray', 'strokeDashoffset', 'x', 'y', 'rotate', 'scale', 'origin', 'originX', 'originY'];
5054

5155
let ind = 0;
5256

@@ -144,6 +148,9 @@ class SvgUri extends Component{
144148
case 'polygon':
145149
componentAtts = this.obtainComponentAtts(node, POLYGON_ATTS);
146150
return <Polygon key={i} {...componentAtts}>{childs}</Polygon>;
151+
case 'polyline':
152+
componentAtts = this.obtainComponentAtts(node, POLYLINE_ATTS);
153+
return <Polyline key={i} {...componentAtts}>{childs}</Polyline>;
147154
default:
148155
return null;
149156
}
@@ -152,13 +159,13 @@ class SvgUri extends Component{
152159
obtainComponentAtts({attributes}, enabledAttributes) {
153160
let styleAtts = {};
154161
Array.from(attributes).forEach(({nodeName, nodeValue}) => {
155-
Object.assign(styleAtts, utils.transformStyle(nodeName, nodeValue, this.props.fill));
162+
Object.assign(styleAtts, utils.transformStyle({nodeName, nodeValue, fillProp: this.props.fill}));
156163
});
157164

158165
let componentAtts = Array.from(attributes)
159166
.map(utils.camelCaseNodeName)
160167
.map(utils.removePixelsFromNodeValue)
161-
.filter(utils.getEnabledAttributes(enabledAttributes))
168+
.filter(utils.getEnabledAttributes(enabledAttributes.concat(COMMON_ATTS)))
162169
.reduce((acc, {nodeName, nodeValue}) => ({
163170
...acc,
164171
[nodeName]: this.props.fill && nodeName === 'fill' ? this.props.fill : nodeValue,

‎utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const camelCaseNodeName = ({nodeName, nodeValue}) => ({nodeName: camelCas
44

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

7-
export const transformStyle = (nodeName, nodeValue, fillProp) => {
7+
export const transformStyle = ({nodeName, nodeValue, fillProp}) => {
88
if (nodeName === 'style') {
99
return nodeValue.split(';')
1010
.reduce((acc, attribute) => {
@@ -18,4 +18,4 @@ export const transformStyle = (nodeName, nodeValue, fillProp) => {
1818
return null;
1919
};
2020

21-
export const getEnabledAttributes = enabledAttributes => ({nodeName}) => enabledAttributes.includes(nodeName);
21+
export const getEnabledAttributes = enabledAttributes => ({nodeName}) => enabledAttributes.includes(camelCase(nodeName));

0 commit comments

Comments
 (0)
Please sign in to comment.