html2react
Version:
Utility for turning raw HTML into React elements
70 lines (53 loc) • 2.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
exports.default = getPropsFromAttributes;
var _reactPropertiesMap = require('./react-properties-map');
var _reactPropertiesMap2 = _interopRequireDefault(_reactPropertiesMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getPropsFromAttributes() {
var attributes = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
return attributes.reduce(function (acc, _ref) {
var name = _ref.name;
var value = _ref.value;
var propName = _reactPropertiesMap2.default[name] || name;
var propValue = getPropValue(propName, value);
acc[propName] = propValue;
return acc;
}, {});
}
function formatStylePropName(name) {
// Vendor prefixes other than "ms" should begin with a capital letter.
// See: https://facebook.github.io/react/tips/inline-styles.html.
name = name.replace(/^(\s+)?-(?=ms)/, '').trim();
// Turn, for instance, "-webkit-property" into "WebkitProperty"
// and "font-size" into "fontSize.
return name.replace(/(\-\w)/g, function (match) {
return match[1].toUpperCase();
});
}
function getStylePropValue(value) {
var properties = value.split(';');
var filteredProperties = properties.filter(function (prop) {
return !!prop;
});
return filteredProperties.reduce(function (acc, prop) {
var _prop$split = prop.split(':');
var _prop$split2 = _slicedToArray(_prop$split, 2);
var propName = _prop$split2[0];
var propValue = _prop$split2[1];
propName = formatStylePropName(propName);
acc[propName] = propValue.trim();
return acc;
}, {});
}
function getPropValue() {
var name = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
var value = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
if (name.toLowerCase() === 'style') {
return getStylePropValue(value);
}
return value;
}