Skip to content

Commit c90a486

Browse files
committed
--enh Adding support for use tag
from bencergazda <[email protected]> at vault-development#168
1 parent 3161a15 commit c90a486

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Svg,{
1818
Text,
1919
TSpan,
2020
Defs,
21+
Use,
2122
Stop
2223
} from 'react-native-svg';
2324

@@ -30,6 +31,7 @@ const ACCEPTED_SVG_ELEMENTS = [
3031
'path',
3132
'rect',
3233
'defs',
34+
'use',
3335
'line',
3436
'linearGradient',
3537
'radialGradient',
@@ -59,12 +61,25 @@ const TEXT_ATTS = ['fontFamily', 'fontSize', 'fontWeight', 'textAnchor']
5961
const POLYGON_ATTS = ['points'];
6062
const POLYLINE_ATTS = ['points'];
6163

62-
const COMMON_ATTS = ['fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeOpacity', 'opacity',
64+
const USE_ATTS = ['href'];
65+
66+
const COMMON_ATTS = ['id', 'fill', 'fillOpacity', 'stroke', 'strokeWidth', 'strokeOpacity', 'opacity',
6367
'strokeLinecap', 'strokeLinejoin',
6468
'strokeDasharray', 'strokeDashoffset', 'x', 'y', 'rotate', 'scale', 'origin', 'originX', 'originY', 'transform', 'clipPath'];
6569

6670
let ind = 0;
6771

72+
// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use#Attributes
73+
function fixXlinkHref (node) {
74+
if (node.attributes) {
75+
const hrefAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'href');
76+
const legacyHrefAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'xlink:href');
77+
78+
return node.attributes[hrefAttr || legacyHrefAttr].value;
79+
}
80+
return null;
81+
}
82+
6883
function fixYPosition (y, node) {
6984
if (node.attributes) {
7085
const fontSizeAttr = Object.keys(node.attributes).find(a => node.attributes[a].name === 'font-size');
@@ -189,6 +204,10 @@ class SvgUri extends Component{
189204
return <Line key={i} {...componentAtts}>{childs}</Line>;
190205
case 'defs':
191206
return <Defs key={i}>{childs}</Defs>;
207+
case 'use':
208+
componentAtts = this.obtainComponentAtts(node, USE_ATTS);
209+
componentAtts.href = fixXlinkHref(node);
210+
return <Use key={i} {...componentAtts}/>;
192211
case 'linearGradient':
193212
componentAtts = this.obtainComponentAtts(node, LINEARG_ATTS);
194213
return <LinearGradient key={i} {...componentAtts}>{childs}</LinearGradient>;
@@ -252,7 +271,7 @@ class SvgUri extends Component{
252271
inspectNode(node){
253272
// Only process accepted elements
254273
if (!ACCEPTED_SVG_ELEMENTS.includes(node.nodeName)) {
255-
return (<View />);
274+
return (<View key={ind++} />);
256275
}
257276

258277
// Process the xml node

0 commit comments

Comments
 (0)