UNPKG

html-react-parser

Version:
68 lines (54 loc) 1.89 kB
var reactProperty = require('react-property'); var utilities = require('./utilities'); var setStyleProp = utilities.setStyleProp; var htmlProperties = reactProperty.html; var svgProperties = reactProperty.svg; var isCustomAttribute = reactProperty.isCustomAttribute; var hasOwnProperty = Object.prototype.hasOwnProperty; /** * Converts HTML/SVG DOM attributes to React props. * * @param {object} [attributes={}] - HTML/SVG DOM attributes. * @return {object} - React props. */ function attributesToProps(attributes) { attributes = attributes || {}; var attributeName; var attributeNameLowerCased; var attributeValue; var property; var props = {}; for (attributeName in attributes) { attributeValue = attributes[attributeName]; // ARIA (aria-*) or custom data (data-*) attribute if (isCustomAttribute(attributeName)) { props[attributeName] = attributeValue; continue; } // convert HTML attribute to React prop attributeNameLowerCased = attributeName.toLowerCase(); if (hasOwnProperty.call(htmlProperties, attributeNameLowerCased)) { property = htmlProperties[attributeNameLowerCased]; props[property.propertyName] = property.hasBooleanValue || (property.hasOverloadedBooleanValue && !attributeValue) ? true : attributeValue; continue; } // convert SVG attribute to React prop if (hasOwnProperty.call(svgProperties, attributeName)) { property = svgProperties[attributeName]; props[property.propertyName] = attributeValue; continue; } // preserve custom attribute if React >=16 if (utilities.PRESERVE_CUSTOM_ATTRIBUTES) { props[attributeName] = attributeValue; } } // transform inline style to object setStyleProp(attributes.style, props); return props; } module.exports = attributesToProps;