forked from gwmccull/react-native-svg-uri-reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
440 lines (394 loc) · 11.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import React, { Component } from 'react';
import { View } from 'react-native';
import PropTypes from 'prop-types';
import xmldom from 'xmldom';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import Svg, {
Circle,
Ellipse,
G,
LinearGradient,
RadialGradient,
Line,
Path,
Polygon,
Polyline,
Rect,
Text,
TSpan,
Defs,
Use,
Stop
} from 'react-native-svg';
import * as utils from './utils';
const ACCEPTED_SVG_ELEMENTS = [
'svg',
'g',
'circle',
'path',
'rect',
'defs',
'use',
'line',
'linearGradient',
'radialGradient',
'stop',
'ellipse',
'polygon',
'polyline',
'text',
'tspan'
];
// Attributes from SVG elements that are mapped directly.
const SVG_ATTS = ['viewBox', 'width', 'height'];
const G_ATTS = ['id'];
const CIRCLE_ATTS = ['cx', 'cy', 'r'];
const PATH_ATTS = ['d'];
const RECT_ATTS = ['width', 'height', 'rx', 'ry'];
const LINE_ATTS = ['x1', 'y1', 'x2', 'y2'];
const LINEARG_ATTS = LINE_ATTS.concat(['id', 'gradientUnits']);
const RADIALG_ATTS = CIRCLE_ATTS.concat(['id', 'gradientUnits']);
const STOP_ATTS = ['offset', 'stopColor'];
const ELLIPSE_ATTS = ['cx', 'cy', 'rx', 'ry'];
const TEXT_ATTS = ['fontFamily', 'fontSize', 'fontWeight', 'textAnchor'];
const IMAGE_ATTS = ['width', 'height', 'preserveAspectRatio', 'href', 'clipPath'];
const POLYGON_ATTS = ['points'];
const POLYLINE_ATTS = ['points'];
const USE_ATTS = ['href'];
const COMMON_ATTS = [
'id',
'fill',
'fillOpacity',
'stroke',
'strokeWidth',
'strokeOpacity',
'opacity',
'strokeLinecap',
'strokeLinejoin',
'strokeDasharray',
'strokeDashoffset',
'x',
'y',
'rotate',
'scale',
'origin',
'originX',
'originY',
'transform',
'clipPath'
];
let ind = 0;
// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use#Attributes
function fixXlinkHref(node) {
if (node.attributes) {
const hrefAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'href');
const legacyHrefAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'xlink:href');
return node.attributes[hrefAttr || legacyHrefAttr].value;
}
return null;
}
function fixYPosition(y, node) {
if (node.attributes) {
const fontSizeAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'font-size');
if (fontSizeAttr) {
return '' + (parseFloat(y) - parseFloat(node.attributes[fontSizeAttr].value));
}
}
if (!node.parentNode) {
return y;
}
return fixYPosition(y, node.parentNode);
}
class SvgUri extends Component {
constructor(props) {
super(props);
this.state = { fill: props.fill, svgXmlData: props.svgXmlData, stroke: props.stroke };
// this.state = { fill: props.fill, svgXmlData: props.svgXmlData };
this.createSVGElement = this.createSVGElement.bind(this);
this.obtainComponentAtts = this.obtainComponentAtts.bind(this);
this.inspectNode = this.inspectNode.bind(this);
this.fetchSVGData = this.fetchSVGData.bind(this);
this.isComponentMounted = false;
// Gets the image data from an URL or a static file
if (props.source) {
const source = resolveAssetSource(props.source) || {};
this.fetchSVGData(source.uri);
}
}
componentWillMount() {
this.isComponentMounted = true;
}
componentWillReceiveProps(nextProps) {
if (nextProps.source) {
const source = resolveAssetSource(nextProps.source) || {};
const oldSource = resolveAssetSource(this.props.source) || {};
if (source.uri !== oldSource.uri) {
this.fetchSVGData(source.uri);
}
}
if (nextProps.svgXmlData !== this.props.svgXmlData) {
this.setState({ svgXmlData: nextProps.svgXmlData });
}
if (nextProps.fill !== this.props.fill) {
this.setState({ fill: nextProps.fill });
}
if (nextProps.stroke !== this.props.stroke) {
this.setState({ stroke: nextProps.stroke });
}
}
componentWillUnmount() {
this.isComponentMounted = false;
}
async fetchSVGData(uri) {
let responseXML = null,
error = null;
try {
const response = await fetch(uri);
responseXML = await response.text();
} catch (e) {
error = e;
console.error('ERROR SVG', e);
} finally {
if (this.isComponentMounted) {
this.setState({ svgXmlData: responseXML }, () => {
const { onLoad } = this.props;
if (onLoad && !error) {
onLoad();
}
});
}
}
return responseXML;
}
// Remove empty strings from children array
trimElementChildren(children) {
for (let child of children) {
if (typeof child === 'string') {
if (child.trim().length === 0) {
children.splice(children.indexOf(child), 1);
}
}
}
}
createSVGElement(node, childs) {
this.trimElementChildren(childs);
let componentAtts = {};
const i = ind++;
switch (node.nodeName) {
case 'svg':
componentAtts = this.obtainComponentAtts(node, SVG_ATTS);
if (this.props.width) {
componentAtts.width = this.props.width;
}
if (this.props.height) {
componentAtts.height = this.props.height;
}
return (
<Svg key={i} {...componentAtts}>
{childs}
</Svg>
);
case 'g':
componentAtts = this.obtainComponentAtts(node, G_ATTS);
return (
<G key={i} {...componentAtts}>
{childs}
</G>
);
case 'path':
componentAtts = this.obtainComponentAtts(node, PATH_ATTS);
return (
<Path key={i} {...componentAtts} fill={this.props.fill}>
{childs}
</Path>
);
case 'circle':
componentAtts = this.obtainComponentAtts(node, CIRCLE_ATTS);
return (
<Circle key={i} {...componentAtts}>
{childs}
</Circle>
);
case 'rect':
componentAtts = this.obtainComponentAtts(node, RECT_ATTS);
return (
<Rect key={i} {...componentAtts}>
{childs}
</Rect>
);
case 'line':
componentAtts = this.obtainComponentAtts(node, LINE_ATTS);
return (
<Line key={i} {...componentAtts}>
{childs}
</Line>
);
case 'defs':
return <Defs key={i}>{childs}</Defs>;
case 'use':
componentAtts = this.obtainComponentAtts(node, USE_ATTS);
componentAtts.href = fixXlinkHref(node);
return <Use key={i} {...componentAtts}/>;
case 'linearGradient':
componentAtts = this.obtainComponentAtts(node, LINEARG_ATTS);
return (
<LinearGradient key={i} {...componentAtts}>
{childs}
</LinearGradient>
);
case 'radialGradient':
componentAtts = this.obtainComponentAtts(node, RADIALG_ATTS);
return (
<RadialGradient key={i} {...componentAtts}>
{childs}
</RadialGradient>
);
case 'stop':
componentAtts = this.obtainComponentAtts(node, STOP_ATTS);
return (
<Stop key={i} {...componentAtts}>
{childs}
</Stop>
);
case 'ellipse':
componentAtts = this.obtainComponentAtts(node, ELLIPSE_ATTS);
return (
<Ellipse key={i} {...componentAtts}>
{childs}
</Ellipse>
);
case 'polygon':
componentAtts = this.obtainComponentAtts(node, POLYGON_ATTS);
return (
<Polygon key={i} {...componentAtts}>
{childs}
</Polygon>
);
case 'polyline':
componentAtts = this.obtainComponentAtts(node, POLYLINE_ATTS);
return (
<Polyline key={i} {...componentAtts}>
{childs}
</Polyline>
);
case 'text':
componentAtts = this.obtainComponentAtts(node, TEXT_ATTS);
if (componentAtts.y) {
componentAtts.y = fixYPosition(componentAtts.y, node);
}
return (
<Text key={i} {...componentAtts}>
{childs}
</Text>
);
case 'tspan':
componentAtts = this.obtainComponentAtts(node, TEXT_ATTS);
if (componentAtts.y) {
componentAtts.y = fixYPosition(componentAtts.y, node);
}
return (
<TSpan key={i} {...componentAtts}>
{childs}
</TSpan>
);
case 'image':
componentAtts = this.obtainComponentAtts(node, IMAGE_ATTS);
if (componentAtts.y) {
componentAtts.y = fixYPosition(componentAtts.y, node);
}
return (
<Image key={i} {...componentAtts}>
{childs}
</Image>
);
default:
return null;
}
}
obtainComponentAtts({ attributes }, enabledAttributes) {
const styleAtts = {};
if (this.state.fill && this.props.fillAll) {
styleAtts.fill = this.state.fill;
}
Array.from(attributes).forEach(({ nodeName, nodeValue }) => {
Object.assign(
styleAtts,
utils.transformStyle({
nodeName,
nodeValue,
fillProp: this.state.fill,
strokeProp: this.state.stroke
})
);
});
const componentAtts = Array.from(attributes)
.map(utils.camelCaseNodeName)
.map(utils.removePixelsFromNodeValue)
.filter(utils.getEnabledAttributes(enabledAttributes.concat(COMMON_ATTS)))
.reduce((acc, { nodeName, nodeValue }) => {
acc[nodeName] =
this.state.fill && nodeName === 'fill' && nodeValue !== 'none'
? this.state.fill
: this.state.stroke && nodeName === 'stroke' && nodeValue !== 'none'
? this.state.stroke
: nodeValue;
// acc[nodeName] =
// this.state.fill && nodeName === 'fill' && nodeValue !== 'none' ? this.state.fill : nodeValue;
return acc;
}, {});
Object.assign(componentAtts, styleAtts);
return componentAtts;
}
inspectNode(node) {
// Only process accepted elements
if (!ACCEPTED_SVG_ELEMENTS.includes(node.nodeName)) {
return <View key={ind++} />;
}
// Process the xml node
const arrayElements = [];
// if have children process them.
// Recursive function.
if (node.childNodes && node.childNodes.length > 0) {
for (let i = 0; i < node.childNodes.length; i++) {
const isTextValue = node.childNodes[i].nodeValue;
if (isTextValue) {
arrayElements.push(node.childNodes[i].nodeValue);
} else {
const nodo = this.inspectNode(node.childNodes[i]);
if (nodo != null) {
arrayElements.push(nodo);
}
}
}
}
return this.createSVGElement(node, arrayElements);
}
render() {
try {
if (this.state.svgXmlData == null) {
return null;
}
const inputSVG = this.state.svgXmlData
.substring(this.state.svgXmlData.indexOf('<svg '), this.state.svgXmlData.indexOf('</svg>') + 6)
.replace(/<!-(.*?)->/g, '');
const doc = new xmldom.DOMParser().parseFromString(inputSVG);
const rootSVG = this.inspectNode(doc.childNodes[0]);
return <View style={this.props.style}>{rootSVG}</View>;
} catch (e) {
console.error('ERROR SVG', e);
return null;
}
}
}
SvgUri.propTypes = {
style: PropTypes.object,
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
svgXmlData: PropTypes.string,
source: PropTypes.any,
strokeProp: PropTypes.any,
fill: PropTypes.string,
onLoad: PropTypes.func,
fillAll: PropTypes.bool
}
module.exports = SvgUri;