react-inlinesvg 1.1.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lib/helpers.js +1 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +22 -7
- package/lib/index.js.map +1 -0
- package/package.json +37 -40
- package/src/helpers.ts +49 -0
- package/src/index.tsx +418 -0
package/README.md
CHANGED
|
@@ -34,6 +34,9 @@ Cache remote SVGs.
|
|
|
34
34
|
**description** {string}
|
|
35
35
|
A description for your SVG. It will override an existing `<desc>` tag.
|
|
36
36
|
|
|
37
|
+
**innerRef** {React.Ref|function}
|
|
38
|
+
Get the SVG HTMLElement.
|
|
39
|
+
|
|
37
40
|
**loader** {node}
|
|
38
41
|
A component to be shown while the SVG is loading.
|
|
39
42
|
|
package/lib/helpers.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+BAAmD;AAEtC,QAAA,SAAS,GAAG,cAAM,OAAA,iBAAa,EAAb,CAAa,CAAC;AAEhC,QAAA,iBAAiB,GAAG;IAC/B,0BAA0B;IAC1B,IAAI,CAAC,QAAQ,EAAE;QACb,OAAO,KAAK,CAAC;KACd;IAED,IAAM,GAAG,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC1C,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC;IAC1B,OAAO,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,UAAU,CAAC,YAAY,KAAK,4BAA4B,CAAC;AACxF,CAAC,CAAC;AAEF,gDAAgD;AAChD;IAAoC,kCAAK;IAKvC,wBAAY,OAAe,EAAE,IAAa;QAA1C,YACE,iBAAO,SAOR;QALC,KAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,KAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,OAAO,KAAI,CAAC;IACd,CAAC;IACH,qBAAC;AAAD,CAAC,AAdD,CAAoC,KAAK,GAcxC;AAdY,wCAAc;AAgBd,QAAA,sBAAsB,GAAG;IACpC,OAAA,yBAAiB,EAAE,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,KAAK,IAAI;AAAvE,CAAuE,CAAC;AAE7D,QAAA,YAAY,GAAG,UAAC,MAAc;IACzC,IAAM,OAAO,GAAG,4BAA4B,CAAC;IAC7C,IAAM,OAAO,GAAG,YAAY,CAAC;IAC7B,IAAM,OAAO,GAAG,KAAG,OAAO,GAAG,OAAO,CAAC,WAAW,EAAE,GAAG,OAAS,CAAC;IAE/D,IAAM,eAAe,GAAG,UAAC,SAAiB;QACxC,OAAA,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IAAvD,CAAuD,CAAC;IAE1D,IAAI,CAAC,GAAG,EAAE,CAAC;IACX,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;QAC/B,CAAC,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;KAC/B;IACD,OAAO,CAAC,CAAC;AACX,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface IProps {
|
|
|
6
6
|
children?: React.ReactNode;
|
|
7
7
|
description?: string;
|
|
8
8
|
loader?: React.ReactNode;
|
|
9
|
+
innerRef?: React.Ref<HTMLElement>;
|
|
9
10
|
onError?: (error: InlineSVGError | IFetchError) => void;
|
|
10
11
|
onLoad?: (src: string, isCached: boolean) => void;
|
|
11
12
|
preProcessor?: (code: string) => string;
|
package/lib/index.js
CHANGED
|
@@ -12,6 +12,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
12
12
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
13
|
};
|
|
14
14
|
})();
|
|
15
|
+
var __assign = (this && this.__assign) || function () {
|
|
16
|
+
__assign = Object.assign || function(t) {
|
|
17
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
+
s = arguments[i];
|
|
19
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
+
t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
15
26
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
16
27
|
var t = {};
|
|
17
28
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -205,6 +216,10 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
205
216
|
var _this = this;
|
|
206
217
|
var _a = this.props, _b = _a.baseURL, baseURL = _b === void 0 ? '' : _b, uniquifyIDs = _a.uniquifyIDs;
|
|
207
218
|
var replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
|
|
219
|
+
var linkAttributes = ['href', 'xlink:href'];
|
|
220
|
+
var isDataValue = function (name, value) {
|
|
221
|
+
return linkAttributes.indexOf(name) >= 0 && (value ? value.indexOf('#') < 0 : false);
|
|
222
|
+
};
|
|
208
223
|
if (!uniquifyIDs) {
|
|
209
224
|
return node;
|
|
210
225
|
}
|
|
@@ -212,14 +227,14 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
212
227
|
if (d.attributes && d.attributes.length) {
|
|
213
228
|
var attributes_1 = Object.values(d.attributes);
|
|
214
229
|
attributes_1.forEach(function (a) {
|
|
215
|
-
var match = a.value.match(
|
|
230
|
+
var match = a.value.match(/url\((.*?)\)/);
|
|
216
231
|
if (match && match[1]) {
|
|
217
|
-
a.value = "url(" + baseURL + match[1] + "__" + _this.hash + ")";
|
|
232
|
+
a.value = a.value.replace(match[0], "url(" + baseURL + match[1] + "__" + _this.hash + ")");
|
|
218
233
|
}
|
|
219
234
|
});
|
|
220
235
|
replaceableAttributes.forEach(function (r) {
|
|
221
236
|
var attribute = attributes_1.find(function (a) { return a.name === r; });
|
|
222
|
-
if (attribute) {
|
|
237
|
+
if (attribute && !isDataValue(r, attribute.value)) {
|
|
223
238
|
attribute.value = attribute.value + "__" + _this.hash;
|
|
224
239
|
}
|
|
225
240
|
});
|
|
@@ -265,7 +280,6 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
265
280
|
}
|
|
266
281
|
};
|
|
267
282
|
InlineSVG.prototype.getElement = function () {
|
|
268
|
-
var _a = this.props, baseURL = _a.baseURL, cacheRequests = _a.cacheRequests, children = _a.children, description = _a.description, onError = _a.onError, onLoad = _a.onLoad, loader = _a.loader, preProcessor = _a.preProcessor, src = _a.src, title = _a.title, uniqueHash = _a.uniqueHash, uniquifyIDs = _a.uniquifyIDs, rest = __rest(_a, ["baseURL", "cacheRequests", "children", "description", "onError", "onLoad", "loader", "preProcessor", "src", "title", "uniqueHash", "uniquifyIDs"]);
|
|
269
283
|
try {
|
|
270
284
|
var node = this.getNode();
|
|
271
285
|
var element = react_from_dom_1.default(node);
|
|
@@ -273,7 +287,7 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
273
287
|
throw new helpers_1.InlineSVGError('Could not convert the src to a React element');
|
|
274
288
|
}
|
|
275
289
|
this.setState({
|
|
276
|
-
element:
|
|
290
|
+
element: element,
|
|
277
291
|
status: exports.STATUS.READY,
|
|
278
292
|
});
|
|
279
293
|
}
|
|
@@ -323,9 +337,9 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
323
337
|
return null;
|
|
324
338
|
}
|
|
325
339
|
var _a = this.state, element = _a.element, status = _a.status;
|
|
326
|
-
var _b = this.props, _c = _b.children, children = _c === void 0 ? null : _c, _d = _b.loader, loader = _d === void 0 ? null : _d;
|
|
340
|
+
var _b = this.props, baseURL = _b.baseURL, cacheRequests = _b.cacheRequests, _c = _b.children, children = _c === void 0 ? null : _c, description = _b.description, innerRef = _b.innerRef, _d = _b.loader, loader = _d === void 0 ? null : _d, onError = _b.onError, onLoad = _b.onLoad, preProcessor = _b.preProcessor, src = _b.src, title = _b.title, uniqueHash = _b.uniqueHash, uniquifyIDs = _b.uniquifyIDs, rest = __rest(_b, ["baseURL", "cacheRequests", "children", "description", "innerRef", "loader", "onError", "onLoad", "preProcessor", "src", "title", "uniqueHash", "uniquifyIDs"]);
|
|
327
341
|
if (element) {
|
|
328
|
-
return element;
|
|
342
|
+
return React.cloneElement(element, __assign({ ref: innerRef }, rest));
|
|
329
343
|
}
|
|
330
344
|
if ([exports.STATUS.UNSUPPORTED, exports.STATUS.FAILED].indexOf(status) > -1) {
|
|
331
345
|
return children;
|
|
@@ -339,3 +353,4 @@ var InlineSVG = /** @class */ (function (_super) {
|
|
|
339
353
|
return InlineSVG;
|
|
340
354
|
}(React.PureComponent));
|
|
341
355
|
exports.default = InlineSVG;
|
|
356
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAA+B;AAE/B,iDAAqC;AAErC,qCAA4F;AAuC/E,QAAA,MAAM,GAAG;IACpB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,KAAK,EAAE,OAAO;IACd,WAAW,EAAE,aAAa;CAC3B,CAAC;AAEF,IAAM,UAAU,GAAoC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AAExE;IAAuC,6BAAmC;IAUxE,mBAAY,KAAa;QAAzB,YACE,kBAAM,KAAK,CAAC,SA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C,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE;YAC5D,OAAO,QAAQ,CAAC;SACjB;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAzWa,sBAAY,GAAG;QAC3B,aAAa,EAAE,IAAI;QACnB,WAAW,EAAE,KAAK;KACnB,CAAC;IAuWJ,gBAAC;CAAA,AA3WD,CAAuC,KAAK,CAAC,aAAa,GA2WzD;kBA3WoB,SAAS"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-inlinesvg",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "An SVG loader for React",
|
|
5
5
|
"author": "Gil Barbara <gilbarbara@gmail.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"homepage": "https://github.com/gilbarbara/react-inlinesvg#readme",
|
|
20
20
|
"main": "lib/index.js",
|
|
21
21
|
"files": [
|
|
22
|
-
"lib"
|
|
22
|
+
"lib",
|
|
23
|
+
"src"
|
|
23
24
|
],
|
|
24
25
|
"types": "lib/index.d.ts",
|
|
25
26
|
"license": "MIT",
|
|
@@ -28,55 +29,51 @@
|
|
|
28
29
|
"svg"
|
|
29
30
|
],
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"react": "^16.
|
|
32
|
-
"react-dom": "^16.0.0"
|
|
32
|
+
"react": "^16.3.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"exenv": "^1.2.2",
|
|
36
|
-
"react-from-dom": "^0.
|
|
36
|
+
"react-from-dom": "^0.3.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
+
"@gilbarbara/tsconfig": "^0.1.0",
|
|
40
|
+
"@size-limit/preset-small-lib": "^2.2.1",
|
|
39
41
|
"@types/enzyme": "^3.10.3",
|
|
40
42
|
"@types/enzyme-adapter-react-16": "^1.0.5",
|
|
41
43
|
"@types/exenv": "^1.2.0",
|
|
42
|
-
"@types/fetch-mock": "^7.3.
|
|
43
|
-
"@types/jest": "^24.0.
|
|
44
|
-
"@types/node": "^12.
|
|
45
|
-
"@types/node-fetch": "^2.5.
|
|
46
|
-
"@types/react": "^16.
|
|
47
|
-
"@types/react-dom": "^16.
|
|
48
|
-
"bundlesize": "^0.18.0",
|
|
49
|
-
"chalk": "^2.4.2",
|
|
50
|
-
"cross-env": "^5.2.0",
|
|
44
|
+
"@types/fetch-mock": "^7.3.2",
|
|
45
|
+
"@types/jest": "^24.0.23",
|
|
46
|
+
"@types/node": "^12.12.9",
|
|
47
|
+
"@types/node-fetch": "^2.5.3",
|
|
48
|
+
"@types/react": "^16.9.11",
|
|
49
|
+
"@types/react-dom": "^16.9.4",
|
|
51
50
|
"enzyme": "^3.10.0",
|
|
52
|
-
"enzyme-adapter-react-16": "^1.
|
|
53
|
-
"fetch-mock": "^
|
|
51
|
+
"enzyme-adapter-react-16": "^1.15.1",
|
|
52
|
+
"fetch-mock": "^8.0.0",
|
|
54
53
|
"http-server": "^0.11.1",
|
|
55
|
-
"husky": "^3.0
|
|
56
|
-
"jest": "^24.
|
|
54
|
+
"husky": "^3.1.0",
|
|
55
|
+
"jest": "^24.9.0",
|
|
57
56
|
"jest-chain": "^1.1.2",
|
|
58
|
-
"jest-enzyme": "^7.
|
|
57
|
+
"jest-enzyme": "^7.1.2",
|
|
59
58
|
"jest-extended": "^0.11.2",
|
|
60
|
-
"jest-watch-typeahead": "^0.
|
|
59
|
+
"jest-watch-typeahead": "^0.4.2",
|
|
61
60
|
"node-fetch": "^2.6.0",
|
|
62
|
-
"prettier": "^1.
|
|
63
|
-
"react": "^16.
|
|
64
|
-
"react-dom": "^16.
|
|
65
|
-
"
|
|
66
|
-
"rimraf": "^
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"ts-jest": "^24.0
|
|
70
|
-
"
|
|
71
|
-
"tslint": "^5.18.0",
|
|
61
|
+
"prettier": "^1.19.1",
|
|
62
|
+
"react": "^16.12.0",
|
|
63
|
+
"react-dom": "^16.12.0",
|
|
64
|
+
"repo-tools": "^0.2.0",
|
|
65
|
+
"rimraf": "^3.0.0",
|
|
66
|
+
"size-limit": "^2.2.1",
|
|
67
|
+
"start-server-and-test": "^1.10.6",
|
|
68
|
+
"ts-jest": "^24.1.0",
|
|
69
|
+
"tslint": "^5.20.1",
|
|
72
70
|
"tslint-config-prettier": "^1.18.0",
|
|
73
|
-
"tslint-react": "^4.
|
|
74
|
-
"typescript": "^3.
|
|
75
|
-
"yargs": "^13.3.0"
|
|
71
|
+
"tslint-react": "^4.1.0",
|
|
72
|
+
"typescript": "^3.7.2"
|
|
76
73
|
},
|
|
77
74
|
"scripts": {
|
|
78
75
|
"build": "npm run clean && npm run build:ts",
|
|
79
|
-
"build:ts": "tsc
|
|
76
|
+
"build:ts": "tsc",
|
|
80
77
|
"watch": "npm run build:ts -- -w",
|
|
81
78
|
"clean": "rimraf lib",
|
|
82
79
|
"start": "http-server -p 1337 --cors test/__fixtures__",
|
|
@@ -85,8 +82,8 @@
|
|
|
85
82
|
"test:watch": "jest --watch --verbose",
|
|
86
83
|
"lint": "tslint 'src/**/*.ts?(x)' 'test/**/*.ts?(x)' 'tools/**/*.ts?(x)'",
|
|
87
84
|
"format": "prettier \"**/*.{js,jsx,json,yml,yaml,css,less,scss,ts,tsx,md,graphql,mdx}\" --write",
|
|
88
|
-
"validate": "npm run lint && npm run test && npm run
|
|
89
|
-
"
|
|
85
|
+
"validate": "npm run lint && npm run test && npm run size",
|
|
86
|
+
"size": "npm run build && size-limit",
|
|
90
87
|
"prepublishOnly": "npm run validate"
|
|
91
88
|
},
|
|
92
89
|
"browserslist": [
|
|
@@ -100,16 +97,16 @@
|
|
|
100
97
|
"singleQuote": true,
|
|
101
98
|
"trailingComma": "all"
|
|
102
99
|
},
|
|
103
|
-
"
|
|
100
|
+
"size-limit": [
|
|
104
101
|
{
|
|
105
102
|
"path": "./lib/index.js",
|
|
106
|
-
"
|
|
103
|
+
"limit": "8 KB"
|
|
107
104
|
}
|
|
108
105
|
],
|
|
109
106
|
"husky": {
|
|
110
107
|
"hooks": {
|
|
111
|
-
"post-merge": "
|
|
112
|
-
"pre-commit": "
|
|
108
|
+
"post-merge": "repo-tools istall-packages",
|
|
109
|
+
"pre-commit": "repo-tools check-remote && npm run validate"
|
|
113
110
|
}
|
|
114
111
|
}
|
|
115
112
|
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { canUseDOM as canUseDOMFlag } from 'exenv';
|
|
2
|
+
|
|
3
|
+
export const canUseDOM = () => canUseDOMFlag;
|
|
4
|
+
|
|
5
|
+
export const supportsInlineSVG = () => {
|
|
6
|
+
/* istanbul ignore next */
|
|
7
|
+
if (!document) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const div = document.createElement('div');
|
|
12
|
+
div.innerHTML = '<svg />';
|
|
13
|
+
return div.firstChild && div.firstChild.namespaceURI === 'http://www.w3.org/2000/svg';
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// tslint:disable-next-line:no-shadowed-variable
|
|
17
|
+
export class InlineSVGError extends Error {
|
|
18
|
+
public name: string;
|
|
19
|
+
public message: string;
|
|
20
|
+
public data?: object;
|
|
21
|
+
|
|
22
|
+
constructor(message: string, data?: object) {
|
|
23
|
+
super();
|
|
24
|
+
|
|
25
|
+
this.name = 'InlineSVGError';
|
|
26
|
+
this.message = message;
|
|
27
|
+
this.data = data;
|
|
28
|
+
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const isSupportedEnvironment = () =>
|
|
34
|
+
supportsInlineSVG() && typeof window !== 'undefined' && window !== null;
|
|
35
|
+
|
|
36
|
+
export const randomString = (length: number) => {
|
|
37
|
+
const letters = 'abcdefghijklmnopqrstuvwxyz';
|
|
38
|
+
const numbers = '1234567890';
|
|
39
|
+
const charset = `${letters}${letters.toUpperCase()}${numbers}`;
|
|
40
|
+
|
|
41
|
+
const randomCharacter = (character: string) =>
|
|
42
|
+
character[Math.floor(Math.random() * character.length)];
|
|
43
|
+
|
|
44
|
+
let R = '';
|
|
45
|
+
for (let i = 0; i < length; i++) {
|
|
46
|
+
R += randomCharacter(charset);
|
|
47
|
+
}
|
|
48
|
+
return R;
|
|
49
|
+
};
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import convert from 'react-from-dom';
|
|
4
|
+
|
|
5
|
+
import { canUseDOM, InlineSVGError, isSupportedEnvironment, randomString } from './helpers';
|
|
6
|
+
|
|
7
|
+
export interface IProps {
|
|
8
|
+
baseURL?: string;
|
|
9
|
+
cacheRequests?: boolean;
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
description?: string;
|
|
12
|
+
loader?: React.ReactNode;
|
|
13
|
+
innerRef?: React.Ref<HTMLElement>;
|
|
14
|
+
onError?: (error: InlineSVGError | IFetchError) => void;
|
|
15
|
+
onLoad?: (src: string, isCached: boolean) => void;
|
|
16
|
+
preProcessor?: (code: string) => string;
|
|
17
|
+
src: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
uniqueHash?: string;
|
|
20
|
+
uniquifyIDs?: boolean;
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface IState {
|
|
25
|
+
content: string;
|
|
26
|
+
element: React.ReactNode;
|
|
27
|
+
hasCache: boolean;
|
|
28
|
+
status: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface IFetchError extends Error {
|
|
32
|
+
code: string;
|
|
33
|
+
errno: string;
|
|
34
|
+
message: string;
|
|
35
|
+
type: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface IStorageItem {
|
|
39
|
+
content: string;
|
|
40
|
+
queue: any[];
|
|
41
|
+
status: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const STATUS = {
|
|
45
|
+
FAILED: 'failed',
|
|
46
|
+
LOADED: 'loaded',
|
|
47
|
+
LOADING: 'loading',
|
|
48
|
+
PENDING: 'pending',
|
|
49
|
+
READY: 'ready',
|
|
50
|
+
UNSUPPORTED: 'unsupported',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const cacheStore: { [key: string]: IStorageItem } = Object.create(null);
|
|
54
|
+
|
|
55
|
+
export default class InlineSVG extends React.PureComponent<IProps, IState> {
|
|
56
|
+
public static defaultProps = {
|
|
57
|
+
cacheRequests: true,
|
|
58
|
+
uniquifyIDs: false,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// tslint:disable-next-line:variable-name
|
|
62
|
+
private _isMounted = false;
|
|
63
|
+
private readonly hash: string;
|
|
64
|
+
|
|
65
|
+
constructor(props: IProps) {
|
|
66
|
+
super(props);
|
|
67
|
+
|
|
68
|
+
this.state = {
|
|
69
|
+
content: '',
|
|
70
|
+
element: null,
|
|
71
|
+
hasCache: !!props.cacheRequests && !!cacheStore[props.src],
|
|
72
|
+
status: STATUS.PENDING,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
this.hash = props.uniqueHash || randomString(8);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public componentDidMount() {
|
|
79
|
+
this._isMounted = true;
|
|
80
|
+
|
|
81
|
+
if (!canUseDOM()) {
|
|
82
|
+
this.handleError(new InlineSVGError('No DOM'));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const { status } = this.state;
|
|
87
|
+
const { src } = this.props;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
/* istanbul ignore else */
|
|
91
|
+
if (status === STATUS.PENDING) {
|
|
92
|
+
/* istanbul ignore else */
|
|
93
|
+
if (!isSupportedEnvironment()) {
|
|
94
|
+
throw new InlineSVGError('Browser does not support SVG');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* istanbul ignore else */
|
|
98
|
+
if (!src) {
|
|
99
|
+
throw new InlineSVGError('Missing src');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.load();
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
this.handleError(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public componentDidUpdate(prevProps: IProps, prevState: IState) {
|
|
110
|
+
if (!canUseDOM()) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const { hasCache, status } = this.state;
|
|
115
|
+
const { onLoad, src } = this.props;
|
|
116
|
+
|
|
117
|
+
if (prevState.status !== STATUS.READY && status === STATUS.READY) {
|
|
118
|
+
/* istanbul ignore else */
|
|
119
|
+
if (onLoad) {
|
|
120
|
+
onLoad(src, hasCache);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (prevProps.src !== src) {
|
|
125
|
+
if (!src) {
|
|
126
|
+
this.handleError(new InlineSVGError('Missing src'));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.load();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public componentWillUnmount() {
|
|
135
|
+
this._isMounted = false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private processSVG() {
|
|
139
|
+
const { content } = this.state;
|
|
140
|
+
const { preProcessor } = this.props;
|
|
141
|
+
|
|
142
|
+
if (preProcessor) {
|
|
143
|
+
return preProcessor(content);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return content;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private updateSVGAttributes(node: SVGSVGElement): SVGSVGElement {
|
|
150
|
+
const { baseURL = '', uniquifyIDs } = this.props;
|
|
151
|
+
const replaceableAttributes = ['id', 'href', 'xlink:href', 'xlink:role', 'xlink:arcrole'];
|
|
152
|
+
const linkAttributes = ['href', 'xlink:href'];
|
|
153
|
+
const isDataValue = (name: string, value: string) =>
|
|
154
|
+
linkAttributes.indexOf(name) >= 0 && (value ? value.indexOf('#') < 0 : false);
|
|
155
|
+
|
|
156
|
+
if (!uniquifyIDs) {
|
|
157
|
+
return node;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
[...node.children].map(d => {
|
|
161
|
+
if (d.attributes && d.attributes.length) {
|
|
162
|
+
const attributes = Object.values(d.attributes);
|
|
163
|
+
|
|
164
|
+
attributes.forEach(a => {
|
|
165
|
+
const match = a.value.match(/url\((.*?)\)/);
|
|
166
|
+
|
|
167
|
+
if (match && match[1]) {
|
|
168
|
+
a.value = a.value.replace(match[0], `url(${baseURL}${match[1]}__${this.hash})`);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
replaceableAttributes.forEach(r => {
|
|
173
|
+
const attribute = attributes.find(a => a.name === r);
|
|
174
|
+
|
|
175
|
+
if (attribute && !isDataValue(r, attribute.value)) {
|
|
176
|
+
attribute.value = `${attribute.value}__${this.hash}`;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (d.children.length) {
|
|
182
|
+
d = this.updateSVGAttributes(d as SVGSVGElement);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return d;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private getNode() {
|
|
192
|
+
const { description, title } = this.props;
|
|
193
|
+
|
|
194
|
+
try {
|
|
195
|
+
const svgText = this.processSVG();
|
|
196
|
+
const node = convert(svgText, { nodeOnly: true });
|
|
197
|
+
|
|
198
|
+
if (!node || !(node instanceof SVGSVGElement)) {
|
|
199
|
+
throw new InlineSVGError('Could not convert the src to a DOM Node');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const svg = this.updateSVGAttributes(node);
|
|
203
|
+
|
|
204
|
+
if (description) {
|
|
205
|
+
const originalDesc = svg.querySelector('desc');
|
|
206
|
+
|
|
207
|
+
if (originalDesc && originalDesc.parentNode) {
|
|
208
|
+
originalDesc.parentNode.removeChild(originalDesc);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const descElement = document.createElement('desc');
|
|
212
|
+
descElement.innerHTML = description;
|
|
213
|
+
svg.prepend(descElement);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (title) {
|
|
217
|
+
const originalTitle = svg.querySelector('title');
|
|
218
|
+
|
|
219
|
+
if (originalTitle && originalTitle.parentNode) {
|
|
220
|
+
originalTitle.parentNode.removeChild(originalTitle);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const titleElement = document.createElement('title');
|
|
224
|
+
titleElement.innerHTML = title;
|
|
225
|
+
svg.prepend(titleElement);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return svg;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
this.handleError(error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private getElement() {
|
|
235
|
+
try {
|
|
236
|
+
const node = this.getNode() as Node;
|
|
237
|
+
const element = convert(node);
|
|
238
|
+
|
|
239
|
+
if (!element || !React.isValidElement(element)) {
|
|
240
|
+
throw new InlineSVGError('Could not convert the src to a React element');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.setState({
|
|
244
|
+
element,
|
|
245
|
+
status: STATUS.READY,
|
|
246
|
+
});
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this.handleError(new InlineSVGError(error.message));
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private load() {
|
|
253
|
+
/* istanbul ignore else */
|
|
254
|
+
if (this._isMounted) {
|
|
255
|
+
this.setState(
|
|
256
|
+
{
|
|
257
|
+
content: '',
|
|
258
|
+
element: null,
|
|
259
|
+
status: STATUS.LOADING,
|
|
260
|
+
},
|
|
261
|
+
() => {
|
|
262
|
+
const { cacheRequests, src } = this.props;
|
|
263
|
+
const cache = cacheRequests && cacheStore[src];
|
|
264
|
+
|
|
265
|
+
if (cache) {
|
|
266
|
+
/* istanbul ignore else */
|
|
267
|
+
if (cache.status === STATUS.LOADING) {
|
|
268
|
+
cache.queue.push(this.handleLoad);
|
|
269
|
+
} else if (cache.status === STATUS.LOADED) {
|
|
270
|
+
this.handleLoad(cache.content);
|
|
271
|
+
}
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const dataURI = src.match(/data:image\/svg[^,]*?(;base64)?,(.*)/);
|
|
276
|
+
let inlineSrc;
|
|
277
|
+
|
|
278
|
+
if (dataURI) {
|
|
279
|
+
inlineSrc = dataURI[1] ? atob(dataURI[2]) : decodeURIComponent(dataURI[2]);
|
|
280
|
+
} else if (src.indexOf('<svg') >= 0) {
|
|
281
|
+
inlineSrc = src;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (inlineSrc) {
|
|
285
|
+
this.handleLoad(inlineSrc);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
this.request();
|
|
290
|
+
},
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private handleLoad = (content: string) => {
|
|
296
|
+
/* istanbul ignore else */
|
|
297
|
+
if (this._isMounted) {
|
|
298
|
+
this.setState(
|
|
299
|
+
{
|
|
300
|
+
content,
|
|
301
|
+
status: STATUS.LOADED,
|
|
302
|
+
},
|
|
303
|
+
this.getElement,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
private handleError = (error: InlineSVGError | IFetchError) => {
|
|
309
|
+
const { onError } = this.props;
|
|
310
|
+
const status =
|
|
311
|
+
error.message === 'Browser does not support SVG' ? STATUS.UNSUPPORTED : STATUS.FAILED;
|
|
312
|
+
|
|
313
|
+
/* istanbul ignore else */
|
|
314
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
315
|
+
// tslint:disable-next-line:no-console
|
|
316
|
+
console.error(error);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/* istanbul ignore else */
|
|
320
|
+
if (this._isMounted) {
|
|
321
|
+
this.setState({ status }, () => {
|
|
322
|
+
/* istanbul ignore else */
|
|
323
|
+
if (typeof onError === 'function') {
|
|
324
|
+
onError(error);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
private request = () => {
|
|
331
|
+
const { cacheRequests, src } = this.props;
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
if (cacheRequests) {
|
|
335
|
+
cacheStore[src] = { content: '', status: STATUS.LOADING, queue: [] };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return fetch(src)
|
|
339
|
+
.then(response => {
|
|
340
|
+
const contentType = response.headers.get('content-type');
|
|
341
|
+
const [fileType] = (contentType || '').split(/ ?; ?/);
|
|
342
|
+
|
|
343
|
+
if (response.status > 299) {
|
|
344
|
+
throw new InlineSVGError('Not Found');
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (!['image/svg+xml', 'text/plain'].some(d => fileType.indexOf(d) >= 0)) {
|
|
348
|
+
throw new InlineSVGError(`Content type isn't valid: ${fileType}`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return response.text();
|
|
352
|
+
})
|
|
353
|
+
.then(content => {
|
|
354
|
+
this.handleLoad(content);
|
|
355
|
+
|
|
356
|
+
/* istanbul ignore else */
|
|
357
|
+
if (cacheRequests) {
|
|
358
|
+
const cache = cacheStore[src];
|
|
359
|
+
|
|
360
|
+
/* istanbul ignore else */
|
|
361
|
+
if (cache) {
|
|
362
|
+
cache.content = content;
|
|
363
|
+
cache.status = STATUS.LOADED;
|
|
364
|
+
|
|
365
|
+
cache.queue = cache.queue.filter((cb: (content: string) => void) => {
|
|
366
|
+
cb(content);
|
|
367
|
+
|
|
368
|
+
return false;
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
})
|
|
373
|
+
.catch(error => {
|
|
374
|
+
/* istanbul ignore else */
|
|
375
|
+
if (cacheRequests) {
|
|
376
|
+
delete cacheStore[src];
|
|
377
|
+
}
|
|
378
|
+
this.handleError(error);
|
|
379
|
+
});
|
|
380
|
+
} catch (error) {
|
|
381
|
+
this.handleError(new InlineSVGError(error.message));
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
public render() {
|
|
386
|
+
if (!canUseDOM()) {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const { element, status } = this.state;
|
|
391
|
+
const {
|
|
392
|
+
baseURL,
|
|
393
|
+
cacheRequests,
|
|
394
|
+
children = null,
|
|
395
|
+
description,
|
|
396
|
+
innerRef,
|
|
397
|
+
loader = null,
|
|
398
|
+
onError,
|
|
399
|
+
onLoad,
|
|
400
|
+
preProcessor,
|
|
401
|
+
src,
|
|
402
|
+
title,
|
|
403
|
+
uniqueHash,
|
|
404
|
+
uniquifyIDs,
|
|
405
|
+
...rest
|
|
406
|
+
} = this.props;
|
|
407
|
+
|
|
408
|
+
if (element) {
|
|
409
|
+
return React.cloneElement(element as React.ReactElement, { ref: innerRef, ...rest });
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if ([STATUS.UNSUPPORTED, STATUS.FAILED].indexOf(status) > -1) {
|
|
413
|
+
return children;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return loader;
|
|
417
|
+
}
|
|
418
|
+
}
|