forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpolate.js
82 lines (61 loc) · 1.93 KB
/
Interpolate.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
// https://www.npmjs.org/package/react-interpolate-component
'use strict';
import React from './react-es6';
import invariant from './react-es6/lib/invariant';
import utils from './utils';
function isString(object) {
return Object.prototype.toString.call(object) === '[object String]';
}
var REGEXP = /\%\((.+?)\)s/;
var Interpolate = React.createClass({
displayName: 'Interpolate',
getDefaultProps: function() {
return { component: React.DOM.span };
},
render: function() {
var format = this.props.children || this.props.format;
var parent = this.props.component;
var unsafe = this.props.unsafe === true;
var props = utils.extend({}, this.props);
delete props.children;
delete props.format;
delete props.component;
delete props.unsafe;
invariant(isString(format), 'Interpolate expects either a format string as only child or a `format` prop with a string value');
if (unsafe) {
var content = format.split(REGEXP).reduce(function(memo, match, index) {
var html;
if (index % 2 === 0) {
html = match;
} else {
html = props[match];
delete props[match];
}
if (React.isValidComponent(html)) {
throw new Error('cannot interpolate a React component into unsafe text');
}
memo += html;
return memo;
}, '');
props.dangerouslySetInnerHTML = { __html: content };
return parent(props);
} else {
var args = format.split(REGEXP).reduce(function(memo, match, index) {
var child;
if (index % 2 === 0) {
if (match.length === 0) {
return memo;
}
child = match;
} else {
child = props[match];
delete props[match];
}
memo.push(child);
return memo;
}, [props]);
return parent.apply(null, args);
}
}
});
export default = Interpolate;