react-htmlcontent
Advanced tools
Comparing version 1.2.2 to 1.2.3
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
@@ -8,3 +10,9 @@ | ||
}); | ||
exports.enhanceTypo = exports.default = void 0; | ||
Object.defineProperty(exports, "enhanceTypo", { | ||
enumerable: true, | ||
get: function get() { | ||
return _enhanceTypo.default; | ||
} | ||
}); | ||
exports.default = void 0; | ||
@@ -25,33 +33,8 @@ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _react = _interopRequireDefault(require("react")); | ||
var React = _interopRequireWildcard(require("react")); | ||
var _propTypes = require("prop-types"); | ||
var _typography = _interopRequireDefault(require("./typography")); | ||
var _enhanceTypo = _interopRequireDefault(require("./enhanceTypo")); | ||
var containsHtmlPatt = /(<[A-z]+[^>]*>)(.*?)(<\/[A-z]+>)/g; | ||
var replaceHtmlContentPatt = /([^<]*)(\s?<[A-z]+[^>]*>)([\s\S]*?)\s?(<\/[A-z]+>\s?)([^<]*)/gi; | ||
var _enhanceTypo = function _enhanceTypo(data) { | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cs'; | ||
var config = arguments.length > 2 ? arguments[2] : undefined; | ||
if (data && typeof data === 'string') { | ||
if (containsHtmlPatt.test(data)) { | ||
return data.replace(replaceHtmlContentPatt, function (_, beforeTag, startTag, body, endTag, afterTag) { | ||
if (containsHtmlPatt.test(body)) { | ||
return _enhanceTypo(body); | ||
} else { | ||
return (0, _typography.default)(beforeTag, locale, config) + (beforeTag.lastIndexOf(' ') === beforeTag.length - 1 ? ' ' : '') + startTag + (0, _typography.default)(body, locale, config) + endTag + (0, _typography.default)(afterTag, locale, config); | ||
} | ||
}); | ||
} else { | ||
return (0, _typography.default)(data, locale, config); | ||
} | ||
} | ||
return ''; | ||
}; | ||
var enhanceTypo = _enhanceTypo; | ||
/** | ||
@@ -61,9 +44,6 @@ * Component for displaying HTML content | ||
*/ | ||
exports.enhanceTypo = enhanceTypo; | ||
var HTMLContent = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
(0, _inherits2.default)(HTMLContent, _React$Component); | ||
function (_React$PureComponent) { | ||
(0, _inherits2.default)(HTMLContent, _React$PureComponent); | ||
@@ -76,19 +56,21 @@ function HTMLContent() { | ||
(0, _createClass2.default)(HTMLContent, [{ | ||
key: "shouldComponentUpdate", | ||
value: function shouldComponentUpdate(nextProps) { | ||
return this.props.children === nextProps.children; | ||
} | ||
}, { | ||
key: "render", | ||
// shouldComponentUpdate(nextProps: string) { | ||
// return this.props.children === nextProps.children | ||
// } | ||
value: function render() { | ||
var _this$props = this.props, | ||
children = _this$props.children, | ||
_this$props$as = _this$props.as, | ||
as = _this$props$as === void 0 ? 'div' : _this$props$as, | ||
tagName = _this$props.tagName, | ||
locale = _this$props.locale, | ||
rest = (0, _objectWithoutProperties2.default)(_this$props, ["children", "tagName", "locale"]); | ||
_this$props$locale = _this$props.locale, | ||
locale = _this$props$locale === void 0 ? cs : _this$props$locale, | ||
rest = (0, _objectWithoutProperties2.default)(_this$props, ["children", "as", "tagName", "locale"]); | ||
if (tagName) console.warn('Property "tagName" is deprecated and will be removed in next major version. Use property "as" instead.'); | ||
if (typeof children !== 'string') throw new Error('You can pass only string as a children.'); | ||
var Tag = tagName; | ||
return _react.default.createElement(Tag, (0, _extends2.default)({ | ||
var Tag = as || tagName; | ||
return React.createElement(Tag, (0, _extends2.default)({ | ||
dangerouslySetInnerHTML: { | ||
__html: enhanceTypo(children) | ||
__html: (0, _enhanceTypo.default)(children) | ||
} | ||
@@ -99,20 +81,4 @@ }, rest)); | ||
return HTMLContent; | ||
}(_react.default.Component); | ||
}(React.PureComponent); | ||
HTMLContent.propTypes = { | ||
/** | ||
* HTML string to display | ||
*/ | ||
children: _propTypes.string.isRequired, | ||
/** | ||
* Tag name or react component (supporting dangerouslySetInnerHTML) to wrap HTML | ||
*/ | ||
tagName: _propTypes.string, | ||
/** | ||
* Locale of HTML text | ||
*/ | ||
locale: (0, _propTypes.oneOf)(['cs', 'sk', 'en-us', 'rue']) | ||
}; | ||
HTMLContent.defaultProps = { | ||
@@ -119,0 +85,0 @@ tagName: 'div', |
{ | ||
"name": "react-htmlcontent", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "React HTMLContent Component", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1,6 +0,13 @@ | ||
import {enhanceTypo} from '../src/index' | ||
import { enhanceTypo } from '../src/index' | ||
import assert from 'assert' | ||
describe('Existing handling', () => { | ||
let testCase = { 'V prdeli je krásně a nevím co s tím.': 'V prdeli je krásně a nevím co s tím.', 'Já s vámi prostě nesouhlasím v žádném i jiném případě': 'Já s vámi prostě nesouhlasím v žádném i jiném případě', 'I 10 000 obrů a trollů nedokáže udělat tohle..': 'I 10 000 obrů a trollů nedokáže udělat tohle.' } | ||
let testCase = { | ||
'V prdeli je krásně a nevím co s tím.': | ||
'V prdeli je krásně a nevím co s tím.', | ||
'Já s vámi prostě nesouhlasím v žádném i jiném případě': | ||
'Já s vámi prostě nesouhlasím v žádném i jiném případě', | ||
'I 10 000 obrů a trollů nedokáže udělat tohle..': | ||
'I 10 000 obrů a trollů nedokáže udělat tohle.', | ||
} | ||
Object.keys(testCase).forEach(key => { | ||
@@ -7,0 +14,0 @@ it('', () => { |
1127978
70
2861