UNPKG

@rednetio/react-include

Version:

Server Side Include / Edge Side Include components for isomorphic rendering with React.

152 lines (116 loc) 4.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); var PropTypes = _interopDefault(require('prop-types')); var reactLifecyclesCompat = require('react-lifecycles-compat'); var classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }; var objectWithoutProperties = function (obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }; var possibleConstructorReturn = function (self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }; var createClass$1 = function createClass$$1(name, props, hrefProp, renderString) { var _class, _temp2; return reactLifecyclesCompat.polyfill((_temp2 = _class = function (_PureComponent) { inherits(_class, _PureComponent); function _class() { var _temp, _this, _ret; classCallCheck(this, _class); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = possibleConstructorReturn(this, _PureComponent.call.apply(_PureComponent, [this].concat(args))), _this), _this.state = {}, _temp), possibleConstructorReturn(_this, _ret); } _class.prototype.componentDidMount = function componentDidMount() { var _this2 = this; var xhr = new XMLHttpRequest(); xhr.open('GET', this.props[hrefProp]); xhr.onload = function () { return _this2.setState({ text: xhr.responseText }); }; this.setState({ xhr: xhr }); }; _class.prototype.componentWillUnmount = function componentWillUnmount() { var xhr = this.state.xhr; if (XMLHttpRequest.DONE !== xhr.readyState) { xhr.abort(); } }; _class.prototype.render = function render() { var text = this.state.text; var _props = this.props, Component = _props.component, href = _props[hrefProp], props = objectWithoutProperties(_props, ['component', hrefProp]); return React__default.createElement(Component, _extends({}, props, { dangerouslySetInnerHTML: { __html: 'string' === typeof text ? text : renderString(this.props) } })); }; return _class; }(React.PureComponent), _class.displayName = name, _class.propTypes = _extends({}, props, { component: PropTypes.string.isRequired }), _class.defaultProps = { component: 'div' }, _temp2)); }; var chars = /[<>"]/g; var replacements = { '<': '&lt;', '>': '&gt;', '"': '&quot;' }; var replace = function replace(char) { return replacements[char]; }; var sanitize = function sanitize(attr) { return attr.replace(chars, replace); }; var ESI = createClass$1('ESI', { src: PropTypes.string.isRequired }, 'src', function (_ref) { var src = _ref.src; return '<esi:include src="' + sanitize(src) + '" />'; }); var SSI = createClass$1('SSI', { virtual: PropTypes.string.isRequired }, 'virtual', function (_ref2) { var virtual = _ref2.virtual; return '<!--# include virtual="' + sanitize(virtual) + '" -->'; }); exports.ESI = ESI; exports.SSI = SSI;