react-i18next
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -0,1 +1,4 @@ | ||
### 5.4.0 | ||
- replaces regex used to parse nodes from string to an ast implementation solving [#298](https://github.com/i18next/react-i18next/issues/298) | ||
### 5.3.0 | ||
@@ -2,0 +5,0 @@ - Pass extra parameters to Trans parent component |
@@ -525,2 +525,210 @@ define('reactI18next', ['exports', 'react', 'prop-types'], function (exports, React, PropTypes) { 'use strict'; | ||
/** | ||
* This file automatically generated from `pre-publish.js`. | ||
* Do not manually edit. | ||
*/ | ||
var index$2 = { | ||
"area": true, | ||
"base": true, | ||
"br": true, | ||
"col": true, | ||
"embed": true, | ||
"hr": true, | ||
"img": true, | ||
"input": true, | ||
"keygen": true, | ||
"link": true, | ||
"menuitem": true, | ||
"meta": true, | ||
"param": true, | ||
"source": true, | ||
"track": true, | ||
"wbr": true | ||
}; | ||
var attrRE = /([\w-]+)|=|(['"])([.\s\S]*?)\2/g; | ||
var parseTag = function (tag) { | ||
var i = 0; | ||
var key; | ||
var expectingValueAfterEquals = true; | ||
var res = { | ||
type: 'tag', | ||
name: '', | ||
voidElement: false, | ||
attrs: {}, | ||
children: [] | ||
}; | ||
tag.replace(attrRE, function (match) { | ||
if (match === '=') { | ||
expectingValueAfterEquals = true; | ||
i++; | ||
return; | ||
} | ||
if (!expectingValueAfterEquals) { | ||
if (key) { | ||
res.attrs[key] = key; // boolean attribute | ||
} | ||
key=match; | ||
} else { | ||
if (i === 0) { | ||
if (index$2[match] || tag.charAt(tag.length - 2) === '/') { | ||
res.voidElement = true; | ||
} | ||
res.name = match; | ||
} else { | ||
res.attrs[key] = match.replace(/^['"]|['"]$/g, ''); | ||
key=undefined; | ||
} | ||
} | ||
i++; | ||
expectingValueAfterEquals = false; | ||
}); | ||
return res; | ||
}; | ||
/*jshint -W030 */ | ||
var tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g; | ||
// re-used obj for quick lookups of components | ||
var empty = Object.create ? Object.create(null) : {}; | ||
// common logic for pushing a child node onto a list | ||
function pushTextNode(list, html, level, start, ignoreWhitespace) { | ||
// calculate correct end of the content slice in case there's | ||
// no tag after the text node. | ||
var end = html.indexOf('<', start); | ||
var content = html.slice(start, end === -1 ? undefined : end); | ||
// if a node is nothing but whitespace, collapse it as the spec states: | ||
// https://www.w3.org/TR/html4/struct/text.html#h-9.1 | ||
if (/^\s*$/.test(content)) { | ||
content = ' '; | ||
} | ||
// don't add whitespace-only text nodes if they would be trailing text nodes | ||
// or if they would be leading whitespace-only text nodes: | ||
// * end > -1 indicates this is not a trailing text node | ||
// * leading node is when level is -1 and list has length 0 | ||
if ((!ignoreWhitespace && end > -1 && level + list.length >= 0) || content !== ' ') { | ||
list.push({ | ||
type: 'text', | ||
content: content | ||
}); | ||
} | ||
} | ||
var parse = function parse(html, options) { | ||
options || (options = {}); | ||
options.components || (options.components = empty); | ||
var result = []; | ||
var current; | ||
var level = -1; | ||
var arr = []; | ||
var byTag = {}; | ||
var inComponent = false; | ||
html.replace(tagRE, function (tag, index) { | ||
if (inComponent) { | ||
if (tag !== ('</' + current.name + '>')) { | ||
return; | ||
} else { | ||
inComponent = false; | ||
} | ||
} | ||
var isOpen = tag.charAt(1) !== '/'; | ||
var isComment = tag.indexOf('<!--') === 0; | ||
var start = index + tag.length; | ||
var nextChar = html.charAt(start); | ||
var parent; | ||
if (isOpen && !isComment) { | ||
level++; | ||
current = parseTag(tag); | ||
if (current.type === 'tag' && options.components[current.name]) { | ||
current.type = 'component'; | ||
inComponent = true; | ||
} | ||
if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') { | ||
pushTextNode(current.children, html, level, start, options.ignoreWhitespace); | ||
} | ||
byTag[current.tagName] = current; | ||
// if we're at root, push new base node | ||
if (level === 0) { | ||
result.push(current); | ||
} | ||
parent = arr[level - 1]; | ||
if (parent) { | ||
parent.children.push(current); | ||
} | ||
arr[level] = current; | ||
} | ||
if (isComment || !isOpen || current.voidElement) { | ||
if (!isComment) { | ||
level--; | ||
} | ||
if (!inComponent && nextChar !== '<' && nextChar) { | ||
// trailing text node | ||
// if we're at the root, push a base text node. otherwise add as | ||
// a child to the current node. | ||
parent = level === -1 ? result : arr[level].children; | ||
pushTextNode(parent, html, level, start, options.ignoreWhitespace); | ||
} | ||
} | ||
}); | ||
// If the "html" passed isn't actually html, add it as a text node. | ||
if (!result.length && html.length) { | ||
pushTextNode(result, html, 0, 0, options.ignoreWhitespace); | ||
} | ||
return result; | ||
}; | ||
function attrString(attrs) { | ||
var buff = []; | ||
for (var key in attrs) { | ||
buff.push(key + '="' + attrs[key] + '"'); | ||
} | ||
if (!buff.length) { | ||
return ''; | ||
} | ||
return ' ' + buff.join(' '); | ||
} | ||
function stringify(buff, doc) { | ||
switch (doc.type) { | ||
case 'text': | ||
return buff + doc.content; | ||
case 'tag': | ||
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>'); | ||
if (doc.voidElement) { | ||
return buff; | ||
} | ||
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>'; | ||
} | ||
} | ||
var stringify_1 = function (doc) { | ||
return doc.reduce(function (token, rootEl) { | ||
return token + stringify('', rootEl); | ||
}, ''); | ||
}; | ||
var index$1 = { | ||
parse: parse, | ||
stringify: stringify_1 | ||
}; | ||
function hasChildren(node) { | ||
@@ -565,46 +773,32 @@ return node && (node.children || node.props && node.props.children); | ||
var REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
var ast = index$1.parse('<0>' + targetString + '</0>'); | ||
var toRender = str.split(REGEXP).reduce(function (mem, match, i) { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce(function (mem, part, i) { | ||
// is a tag | ||
var isTag = !isNaN(part); | ||
var previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
var child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (React__default.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
return astNodes.reduce(function (mem, node, i) { | ||
if (node.type === 'tag') { | ||
var child = reactNodes[parseInt(node.name, 10)] || {}; | ||
var isElement = React__default.isValidElement(child); | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (typeof child === 'string') { | ||
mem.push(child); | ||
} else if (hasChildren(child)) { | ||
var inner = mapAST(getChildren(child), node.children); | ||
if (isTag) { | ||
var _child = nodes[parseInt(part, 10)] || {}; | ||
var isElement = React__default.isValidElement(_child); | ||
if (typeof _child === 'string') { | ||
mem.push(_child); | ||
} else if (hasChildren(_child)) { | ||
var inner = parseChildren(getChildren(_child), toRender[i + 1]); | ||
mem.push(React__default.cloneElement(_child, _extends({}, _child.props, { key: i }), inner)); | ||
} else if ((typeof _child === 'undefined' ? 'undefined' : _typeof(_child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], _child, i18n.language); | ||
mem.push(React__default.cloneElement(child, _extends({}, child.props, { key: i }), inner)); | ||
} else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
} else { | ||
mem.push(_child); | ||
mem.push(child); | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -614,3 +808,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
var result = mapAST([{ children: children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -617,0 +815,0 @@ |
@@ -1,1 +0,1 @@ | ||
define("reactI18next",["exports","react","prop-types"],function(t,n,e){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?C.translateFuncName:o;return function(o){var s,p=function(n){function e(n,i){m(this,e);var o=j(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,n,i));o.i18n=i.i18n||n.i18n||r.i18n||_,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=O({},C,a,r),n.initialI18nStore&&(o.i18n.services.resourceStore.data=n.initialI18nStore,o.options.wait=!1),n.initialLanguage&&o.i18n.changeLanguage(n.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return w(e,n),b(e,[{key:"getChildContext",value:function(){var t;return t={},S(t,a,this[a]),S(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var n=this,e=function(){n.options.bindI18n&&n.i18n&&n.i18n.on(n.options.bindI18n,n.onI18nChanged),n.options.bindStore&&n.i18n.store&&n.i18n.store.on(n.options.bindStore,n.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){n.mounted&&!n.state.ready&&n.setState({ready:!0}),n.options.wait&&n.mounted&&e()};if(n.i18n.isInitialized)t();else{var i=function e(){setTimeout(function(){n.i18n.off("initialized",e)},1e3),t()};n.i18n.on("initialized",i)}}),this.options.wait||e()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(n){return t.i18n.off(n,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(n){return t.i18n.store&&t.i18n.store.off(n,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,n=this,e=this.state,i=e.i18nLoadedAt,r=e.ready,s=(t={i18nLoadedAt:i},S(t,a,this[a]),S(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!N&&(N=!0,setTimeout(function(){delete n.i18n.options.isInitialSSR},100)),f.createElement(o,O({},this.props,s)))}}]),e}(n.Component);return p.WrappedComponent=o,p.contextTypes={i18n:e.object},p.childContextTypes=(s={},S(s,a,e.func.isRequired),S(s,"i18n",e.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,v(p,o)}}function o(t){return t&&(t.children||t.props&&t.props.children)}function a(t){return t&&t.children?t.children:t.props&&t.props.children}function s(t,n,e){return"[object Array]"!==Object.prototype.toString.call(n)&&(n=[n]),n.forEach(function(n,e){var i=""+e;if("string"==typeof n)t=""+t+n;else if(o(n))t=t+"<"+i+">"+s("",a(n),e+1)+"</"+i+">";else if(f.isValidElement(n))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===n?"undefined":g(n))){var r=O({},n),p=r.format;delete r.format;var u=Object.keys(r);p&&1===u.length?t=t+"<"+i+">{{"+u[0]+", "+p+"}}</"+i+">":1===u.length&&(t=t+"<"+i+">{{"+u[0]+"}}</"+i+">")}}),t}function p(t,n,e){function i(t,n){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]);var r=n.split(T).reduce(function(t,n,e){return n&&t.push(n),t},[]);return r.reduce(function(n,s,p){var u=!isNaN(s),c=p>0&&!isNaN(r[p-1]);if(c){var l=t[parseInt(r[p-1],10)]||{};f.isValidElement(l)&&!o(l)&&(c=!1)}if(c)return n;if(u){var d=t[parseInt(s,10)]||{},h=f.isValidElement(d);if("string"==typeof d)n.push(d);else if(o(d)){var y=i(a(d),r[p+1]);n.push(f.cloneElement(d,O({},d.props,{key:p}),y))}else if("object"!==(void 0===d?"undefined":g(d))||h)n.push(d);else{var v=e.services.interpolator.interpolate(r[p+1],d,e.language);n.push(v)}}return u||c||n.push(s),n},[])}return i(t,n)}function u(t,n){for(var e=0,i=t.length;e<i;e++)if("object"===g(t[e])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[e])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var u=x(s.value,2),c=u[0],l=u[1];n(l,e,c)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else n(t[e],e)}function c(t){var n=[];return u(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===n.indexOf(t)&&n.push(t)})}),n}function l(t){var n=t.components,e=t.i18n,i=c(n);return new Promise(function(t){e.loadNamespaces(i,t)})}var f="default"in n?n.default:n;e="default"in e?e.default:e;var d={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},h={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},y="function"==typeof Object.getOwnPropertySymbols,v=function(t,n,e){if("string"!=typeof n){var i=Object.getOwnPropertyNames(n);y&&(i=i.concat(Object.getOwnPropertySymbols(n)));for(var r=0;r<i.length;++r)if(!(d[i[r]]||h[i[r]]||e&&e[i[r]]))try{t[i[r]]=n[i[r]]}catch(t){}}return t},g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},m=function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")},b=function(){function t(t,n){for(var e=0;e<n.length;e++){var i=n[e];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(n,e,i){return e&&t(n.prototype,e),i&&t(n,i),n}}(),S=function(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t},O=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i])}return t},w=function(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Super expression must either be null or a function, not "+typeof n);t.prototype=Object.create(n&&n.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),n&&(Object.setPrototypeOf?Object.setPrototypeOf(t,n):t.__proto__=n)},I=function(t,n){var e={};for(var i in t)n.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},j=function(t,n){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!n||"object"!=typeof n&&"function"!=typeof n?t:n},x=function(){function t(t,n){var e=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(e.push(a.value),!n||e.length!==n);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return e}return function(n,e){if(Array.isArray(n))return n;if(Symbol.iterator in Object(n))return t(n,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),C={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},_=void 0,N=!1;r.setDefaults=function(t){C=O({},C,t)},r.setI18n=function(t){_=t};var P=function(t){function n(t,e){m(this,n);var i=j(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=e.i18n,i.t=e.t,i}return w(n,t),b(n,[{key:"render",value:function(){var t=this,n=this.props.parent||"span",e=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=O({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),u=this.t(this.props.i18nKey,p);if(!u||"string"!=typeof u)return f.createElement("noscript",null);var c=[],l=function(n,e){if(n.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===e[n]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+n+" for interpolating "+u),e[n];var i=n.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===e[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+u),t.i18n.options.interpolation.format(e[r],o,t.i18n.language)};u.split(e).reduce(function(n,e,i){var r=void 0;if(i%2==0){if(0===e.length)return n;r=a?f.createElement(s,{dangerouslySetInnerHTML:{__html:e}}):e}else r=l(e,t.props);return n.push(r),n},c);var d={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var h="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){h=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(d["data-i18next-options"]=JSON.stringify({ns:h}))}return r&&(d.className=r),o&&(d.style=o),f.createElement.apply(this,[n,d].concat(c))}}]),n}(n.Component);P.propTypes={className:e.string},P.defaultProps={className:""},P.contextTypes={i18n:e.object.isRequired,t:e.func.isRequired};var T=new RegExp("(?:<([^>]*)>(.*?)<\\/\\1>)","gi"),E=function(t){function n(t,e){m(this,n);var i=j(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=e.i18n,i.t=e.t,i}return w(n,t),b(n,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,n=t.children,e=t.count,i=t.parent,r=t.i18nKey,o=I(t,["children","count","parent","i18nKey"]),a=s("",n,0),u=r||a,c=this.t(u,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:e});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var l="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){l=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:l}))}return f.createElement(i,o,p(n,c,this.i18n))}}]),n}(f.Component);E.propTypes={count:e.number,parent:e.string,i18nKey:e.string},E.defaultProps={parent:"div"},E.contextTypes={i18n:e.object.isRequired,t:e.func.isRequired};var R=function(t){function e(t,n){m(this,e);var i=j(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return w(e,t),b(e,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return n.Children.only(t)}}]),e}(n.Component);R.propTypes={i18n:e.object.isRequired,children:e.element.isRequired},R.childContextTypes={i18n:e.object.isRequired},t.loadNamespaces=l,t.translate=r,t.Interpolate=P,t.I18nextProvider=R,t.Trans=E,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
define("reactI18next",["exports","react","prop-types"],function(t,e,n){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?P.translateFuncName:o;return function(o){var s,p=function(e){function n(e,i){O(this,n);var o=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,i));o.i18n=i.i18n||e.i18n||r.i18n||T,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=x({},P,a,r),e.initialI18nStore&&(o.i18n.services.resourceStore.data=e.initialI18nStore,o.options.wait=!1),e.initialLanguage&&o.i18n.changeLanguage(e.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return I(n,e),j(n,[{key:"getChildContext",value:function(){var t;return t={},w(t,a,this[a]),w(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var e=this,n=function(){e.options.bindI18n&&e.i18n&&e.i18n.on(e.options.bindI18n,e.onI18nChanged),e.options.bindStore&&e.i18n.store&&e.i18n.store.on(e.options.bindStore,e.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){e.mounted&&!e.state.ready&&e.setState({ready:!0}),e.options.wait&&e.mounted&&n()};if(e.i18n.isInitialized)t();else{var i=function n(){setTimeout(function(){e.i18n.off("initialized",n)},1e3),t()};e.i18n.on("initialized",i)}}),this.options.wait||n()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(e){return t.i18n.off(e,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(e){return t.i18n.store&&t.i18n.store.off(e,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,e=this,n=this.state,i=n.i18nLoadedAt,r=n.ready,s=(t={i18nLoadedAt:i},w(t,a,this[a]),w(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!k&&(k=!0,setTimeout(function(){delete e.i18n.options.isInitialSSR},100)),y.createElement(o,x({},this.props,s)))}}]),n}(e.Component);return p.WrappedComponent=o,p.contextTypes={i18n:n.object},p.childContextTypes=(s={},w(s,a,n.func.isRequired),w(s,"i18n",n.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,b(p,o)}}function o(t,e,n,i,r){var o=e.indexOf("<",i),a=e.slice(i,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!r&&o>-1&&n+t.length>=0||" "!==a)&&t.push({type:"text",content:a})}function a(t){var e=[];for(var n in t)e.push(n+'="'+t[n]+'"');return e.length?" "+e.join(" "):""}function s(t,e){switch(e.type){case"text":return t+e.content;case"tag":return t+="<"+e.name+(e.attrs?a(e.attrs):"")+(e.voidElement?"/>":">"),e.voidElement?t:t+e.children.reduce(s,"")+"</"+e.name+">"}}function p(t){return t&&(t.children||t.props&&t.props.children)}function c(t){return t&&t.children?t.children:t.props&&t.props.children}function u(t,e,n){return"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.forEach(function(e,n){var i=""+n;if("string"==typeof e)t=""+t+e;else if(p(e))t=t+"<"+i+">"+u("",c(e),n+1)+"</"+i+">";else if(y.isValidElement(e))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===e?"undefined":S(e))){var r=x({},e),o=r.format;delete r.format;var a=Object.keys(r);o&&1===a.length?t=t+"<"+i+">{{"+a[0]+", "+o+"}}</"+i+">":1===a.length&&(t=t+"<"+i+">{{"+a[0]+"}}</"+i+">")}}),t}function l(t,e,n){function i(t,e){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.reduce(function(e,r,o){if("tag"===r.type){var a=t[parseInt(r.name,10)]||{},s=y.isValidElement(a);if("string"==typeof a)e.push(a);else if(p(a)){var u=i(c(a),r.children);e.push(y.cloneElement(a,x({},a.props,{key:o}),u))}else if("object"!==(void 0===a?"undefined":S(a))||s)e.push(a);else{var l=n.services.interpolator.interpolate(r.children[0].content,a,n.language);e.push(l)}}else"text"===r.type&&e.push(r.content);return e},[])}return i([{children:t}],D.parse("<0>"+e+"</0>"))[0].props.children}function f(t,e){for(var n=0,i=t.length;n<i;n++)if("object"===S(t[n])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[n])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var c=_(s.value,2),u=c[0],l=c[1];e(l,n,u)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else e(t[n],n)}function h(t){var e=[];return f(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===e.indexOf(t)&&e.push(t)})}),e}function d(t){var e=t.components,n=t.i18n,i=h(e);return new Promise(function(t){n.loadNamespaces(i,t)})}var y="default"in e?e.default:e;n="default"in n?n.default:n;var m={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},v={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},g="function"==typeof Object.getOwnPropertySymbols,b=function(t,e,n){if("string"!=typeof e){var i=Object.getOwnPropertyNames(e);g&&(i=i.concat(Object.getOwnPropertySymbols(e)));for(var r=0;r<i.length;++r)if(!(m[i[r]]||v[i[r]]||n&&n[i[r]]))try{t[i[r]]=e[i[r]]}catch(t){}}return t},S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},O=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},j=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),w=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},x=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},I=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},C=function(t,e){var n={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},E=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},_=function(){function t(t,e){var n=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),P={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},T=void 0,k=!1;r.setDefaults=function(t){P=x({},P,t)},r.setI18n=function(t){T=t};var N=function(t){function e(t,n){O(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),j(e,[{key:"render",value:function(){var t=this,e=this.props.parent||"span",n=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=x({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),c=this.t(this.props.i18nKey,p);if(!c||"string"!=typeof c)return y.createElement("noscript",null);var u=[],l=function(e,n){if(e.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===n[e]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+e+" for interpolating "+c),n[e];var i=e.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===n[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+c),t.i18n.options.interpolation.format(n[r],o,t.i18n.language)};c.split(n).reduce(function(e,n,i){var r=void 0;if(i%2==0){if(0===n.length)return e;r=a?y.createElement(s,{dangerouslySetInnerHTML:{__html:n}}):n}else r=l(n,t.props);return e.push(r),e},u);var f={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var h="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){h=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(f["data-i18next-options"]=JSON.stringify({ns:h}))}return r&&(f.className=r),o&&(f.style=o),y.createElement.apply(this,[e,f].concat(u))}}]),e}(e.Component);N.propTypes={className:n.string},N.defaultProps={className:""},N.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var R={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},L=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,A=function(t){var e,n=0,i=!0,r={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return t.replace(L,function(o){if("="===o)return i=!0,void n++;i?0===n?((R[o]||"/"===t.charAt(t.length-2))&&(r.voidElement=!0),r.name=o):(r.attrs[e]=o.replace(/^['"]|['"]$/g,""),e=void 0):(e&&(r.attrs[e]=e),e=o),n++,i=!1}),r},W=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,M=Object.create?Object.create(null):{},q=function(t,e){e||(e={}),e.components||(e.components=M);var n,i=[],r=-1,a=[],s={},p=!1;return t.replace(W,function(c,u){if(p){if(c!=="</"+n.name+">")return;p=!1}var l,f="/"!==c.charAt(1),h=0===c.indexOf("\x3c!--"),d=u+c.length,y=t.charAt(d);f&&!h&&(r++,n=A(c),"tag"===n.type&&e.components[n.name]&&(n.type="component",p=!0),n.voidElement||p||!y||"<"===y||o(n.children,t,r,d,e.ignoreWhitespace),s[n.tagName]=n,0===r&&i.push(n),l=a[r-1],l&&l.children.push(n),a[r]=n),(h||!f||n.voidElement)&&(h||r--,!p&&"<"!==y&&y&&(l=-1===r?i:a[r].children,o(l,t,r,d,e.ignoreWhitespace)))}),!i.length&&t.length&&o(i,t,0,0,e.ignoreWhitespace),i},K=function(t){return t.reduce(function(t,e){return t+s("",e)},"")},D={parse:q,stringify:K},$=function(t){function e(t,n){O(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),j(e,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,e=t.children,n=t.count,i=t.parent,r=t.i18nKey,o=C(t,["children","count","parent","i18nKey"]),a=u("",e,0),s=r||a,p=this.t(s,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:n});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var c="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){c=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:c}))}return y.createElement(i,o,l(e,p,this.i18n))}}]),e}(y.Component);$.propTypes={count:n.number,parent:n.string,i18nKey:n.string},$.defaultProps={parent:"div"},$.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var F=function(t){function n(t,e){O(this,n);var i=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return I(n,t),j(n,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return e.Children.only(t)}}]),n}(e.Component);F.propTypes={i18n:n.object.isRequired,children:n.element.isRequired},F.childContextTypes={i18n:n.object.isRequired},t.loadNamespaces=d,t.translate=r,t.Interpolate=N,t.I18nextProvider=F,t.Trans=$,Object.defineProperty(t,"__esModule",{value:!0})}); |
@@ -21,2 +21,6 @@ 'use strict'; | ||
var _htmlParseStringify = require('html-parse-stringify2'); | ||
var _htmlParseStringify2 = _interopRequireDefault(_htmlParseStringify); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -71,46 +75,32 @@ | ||
var REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
var ast = _htmlParseStringify2.default.parse('<0>' + targetString + '</0>'); | ||
var toRender = str.split(REGEXP).reduce(function (mem, match, i) { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce(function (mem, part, i) { | ||
// is a tag | ||
var isTag = !isNaN(part); | ||
var previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
var child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (_react2.default.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
return astNodes.reduce(function (mem, node, i) { | ||
if (node.type === 'tag') { | ||
var child = reactNodes[parseInt(node.name, 10)] || {}; | ||
var isElement = _react2.default.isValidElement(child); | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (typeof child === 'string') { | ||
mem.push(child); | ||
} else if (hasChildren(child)) { | ||
var inner = mapAST(getChildren(child), node.children); | ||
if (isTag) { | ||
var _child = nodes[parseInt(part, 10)] || {}; | ||
var isElement = _react2.default.isValidElement(_child); | ||
if (typeof _child === 'string') { | ||
mem.push(_child); | ||
} else if (hasChildren(_child)) { | ||
var inner = parseChildren(getChildren(_child), toRender[i + 1]); | ||
mem.push(_react2.default.cloneElement(_child, _extends({}, _child.props, { key: i }), inner)); | ||
} else if ((typeof _child === 'undefined' ? 'undefined' : _typeof(_child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], _child, i18n.language); | ||
mem.push(_react2.default.cloneElement(child, _extends({}, child.props, { key: i }), inner)); | ||
} else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
} else { | ||
mem.push(_child); | ||
mem.push(child); | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -120,3 +110,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
var result = mapAST([{ children: children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -123,0 +117,0 @@ |
@@ -17,2 +17,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
import PropTypes from 'prop-types'; | ||
import HTML from 'html-parse-stringify2'; | ||
@@ -58,46 +59,32 @@ function hasChildren(node) { | ||
var REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
var ast = HTML.parse('<0>' + targetString + '</0>'); | ||
var toRender = str.split(REGEXP).reduce(function (mem, match, i) { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce(function (mem, part, i) { | ||
// is a tag | ||
var isTag = !isNaN(part); | ||
var previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
var child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (React.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
return astNodes.reduce(function (mem, node, i) { | ||
if (node.type === 'tag') { | ||
var child = reactNodes[parseInt(node.name, 10)] || {}; | ||
var isElement = React.isValidElement(child); | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (typeof child === 'string') { | ||
mem.push(child); | ||
} else if (hasChildren(child)) { | ||
var inner = mapAST(getChildren(child), node.children); | ||
if (isTag) { | ||
var _child = nodes[parseInt(part, 10)] || {}; | ||
var isElement = React.isValidElement(_child); | ||
if (typeof _child === 'string') { | ||
mem.push(_child); | ||
} else if (hasChildren(_child)) { | ||
var inner = parseChildren(getChildren(_child), toRender[i + 1]); | ||
mem.push(React.cloneElement(_child, _extends({}, _child.props, { key: i }), inner)); | ||
} else if ((typeof _child === 'undefined' ? 'undefined' : _typeof(_child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], _child, i18n.language); | ||
mem.push(React.cloneElement(child, _extends({}, child.props, { key: i }), inner)); | ||
} else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
} else { | ||
mem.push(_child); | ||
mem.push(child); | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -107,3 +94,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
var result = mapAST([{ children: children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -110,0 +101,0 @@ |
@@ -529,2 +529,210 @@ (function (global, factory) { | ||
/** | ||
* This file automatically generated from `pre-publish.js`. | ||
* Do not manually edit. | ||
*/ | ||
var index$2 = { | ||
"area": true, | ||
"base": true, | ||
"br": true, | ||
"col": true, | ||
"embed": true, | ||
"hr": true, | ||
"img": true, | ||
"input": true, | ||
"keygen": true, | ||
"link": true, | ||
"menuitem": true, | ||
"meta": true, | ||
"param": true, | ||
"source": true, | ||
"track": true, | ||
"wbr": true | ||
}; | ||
var attrRE = /([\w-]+)|=|(['"])([.\s\S]*?)\2/g; | ||
var parseTag = function (tag) { | ||
var i = 0; | ||
var key; | ||
var expectingValueAfterEquals = true; | ||
var res = { | ||
type: 'tag', | ||
name: '', | ||
voidElement: false, | ||
attrs: {}, | ||
children: [] | ||
}; | ||
tag.replace(attrRE, function (match) { | ||
if (match === '=') { | ||
expectingValueAfterEquals = true; | ||
i++; | ||
return; | ||
} | ||
if (!expectingValueAfterEquals) { | ||
if (key) { | ||
res.attrs[key] = key; // boolean attribute | ||
} | ||
key=match; | ||
} else { | ||
if (i === 0) { | ||
if (index$2[match] || tag.charAt(tag.length - 2) === '/') { | ||
res.voidElement = true; | ||
} | ||
res.name = match; | ||
} else { | ||
res.attrs[key] = match.replace(/^['"]|['"]$/g, ''); | ||
key=undefined; | ||
} | ||
} | ||
i++; | ||
expectingValueAfterEquals = false; | ||
}); | ||
return res; | ||
}; | ||
/*jshint -W030 */ | ||
var tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g; | ||
// re-used obj for quick lookups of components | ||
var empty = Object.create ? Object.create(null) : {}; | ||
// common logic for pushing a child node onto a list | ||
function pushTextNode(list, html, level, start, ignoreWhitespace) { | ||
// calculate correct end of the content slice in case there's | ||
// no tag after the text node. | ||
var end = html.indexOf('<', start); | ||
var content = html.slice(start, end === -1 ? undefined : end); | ||
// if a node is nothing but whitespace, collapse it as the spec states: | ||
// https://www.w3.org/TR/html4/struct/text.html#h-9.1 | ||
if (/^\s*$/.test(content)) { | ||
content = ' '; | ||
} | ||
// don't add whitespace-only text nodes if they would be trailing text nodes | ||
// or if they would be leading whitespace-only text nodes: | ||
// * end > -1 indicates this is not a trailing text node | ||
// * leading node is when level is -1 and list has length 0 | ||
if ((!ignoreWhitespace && end > -1 && level + list.length >= 0) || content !== ' ') { | ||
list.push({ | ||
type: 'text', | ||
content: content | ||
}); | ||
} | ||
} | ||
var parse = function parse(html, options) { | ||
options || (options = {}); | ||
options.components || (options.components = empty); | ||
var result = []; | ||
var current; | ||
var level = -1; | ||
var arr = []; | ||
var byTag = {}; | ||
var inComponent = false; | ||
html.replace(tagRE, function (tag, index) { | ||
if (inComponent) { | ||
if (tag !== ('</' + current.name + '>')) { | ||
return; | ||
} else { | ||
inComponent = false; | ||
} | ||
} | ||
var isOpen = tag.charAt(1) !== '/'; | ||
var isComment = tag.indexOf('<!--') === 0; | ||
var start = index + tag.length; | ||
var nextChar = html.charAt(start); | ||
var parent; | ||
if (isOpen && !isComment) { | ||
level++; | ||
current = parseTag(tag); | ||
if (current.type === 'tag' && options.components[current.name]) { | ||
current.type = 'component'; | ||
inComponent = true; | ||
} | ||
if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') { | ||
pushTextNode(current.children, html, level, start, options.ignoreWhitespace); | ||
} | ||
byTag[current.tagName] = current; | ||
// if we're at root, push new base node | ||
if (level === 0) { | ||
result.push(current); | ||
} | ||
parent = arr[level - 1]; | ||
if (parent) { | ||
parent.children.push(current); | ||
} | ||
arr[level] = current; | ||
} | ||
if (isComment || !isOpen || current.voidElement) { | ||
if (!isComment) { | ||
level--; | ||
} | ||
if (!inComponent && nextChar !== '<' && nextChar) { | ||
// trailing text node | ||
// if we're at the root, push a base text node. otherwise add as | ||
// a child to the current node. | ||
parent = level === -1 ? result : arr[level].children; | ||
pushTextNode(parent, html, level, start, options.ignoreWhitespace); | ||
} | ||
} | ||
}); | ||
// If the "html" passed isn't actually html, add it as a text node. | ||
if (!result.length && html.length) { | ||
pushTextNode(result, html, 0, 0, options.ignoreWhitespace); | ||
} | ||
return result; | ||
}; | ||
function attrString(attrs) { | ||
var buff = []; | ||
for (var key in attrs) { | ||
buff.push(key + '="' + attrs[key] + '"'); | ||
} | ||
if (!buff.length) { | ||
return ''; | ||
} | ||
return ' ' + buff.join(' '); | ||
} | ||
function stringify(buff, doc) { | ||
switch (doc.type) { | ||
case 'text': | ||
return buff + doc.content; | ||
case 'tag': | ||
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>'); | ||
if (doc.voidElement) { | ||
return buff; | ||
} | ||
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>'; | ||
} | ||
} | ||
var stringify_1 = function (doc) { | ||
return doc.reduce(function (token, rootEl) { | ||
return token + stringify('', rootEl); | ||
}, ''); | ||
}; | ||
var index$1 = { | ||
parse: parse, | ||
stringify: stringify_1 | ||
}; | ||
function hasChildren(node) { | ||
@@ -569,46 +777,32 @@ return node && (node.children || node.props && node.props.children); | ||
var REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
var ast = index$1.parse('<0>' + targetString + '</0>'); | ||
var toRender = str.split(REGEXP).reduce(function (mem, match, i) { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce(function (mem, part, i) { | ||
// is a tag | ||
var isTag = !isNaN(part); | ||
var previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
var child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (React__default.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
return astNodes.reduce(function (mem, node, i) { | ||
if (node.type === 'tag') { | ||
var child = reactNodes[parseInt(node.name, 10)] || {}; | ||
var isElement = React__default.isValidElement(child); | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (typeof child === 'string') { | ||
mem.push(child); | ||
} else if (hasChildren(child)) { | ||
var inner = mapAST(getChildren(child), node.children); | ||
if (isTag) { | ||
var _child = nodes[parseInt(part, 10)] || {}; | ||
var isElement = React__default.isValidElement(_child); | ||
if (typeof _child === 'string') { | ||
mem.push(_child); | ||
} else if (hasChildren(_child)) { | ||
var inner = parseChildren(getChildren(_child), toRender[i + 1]); | ||
mem.push(React__default.cloneElement(_child, _extends({}, _child.props, { key: i }), inner)); | ||
} else if ((typeof _child === 'undefined' ? 'undefined' : _typeof(_child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], _child, i18n.language); | ||
mem.push(React__default.cloneElement(child, _extends({}, child.props, { key: i }), inner)); | ||
} else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
} else { | ||
mem.push(_child); | ||
mem.push(child); | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -618,3 +812,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
var result = mapAST([{ children: children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -621,0 +819,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define("reactI18next",["exports","react","prop-types"],e):e(t.reactI18next=t.reactI18next||{},t.React,t.PropTypes)}(this,function(t,e,n){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?C.translateFuncName:o;return function(o){var s,p=function(e){function n(e,i){g(this,n);var o=w(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,i));o.i18n=i.i18n||e.i18n||r.i18n||P,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=I({},C,a,r),e.initialI18nStore&&(o.i18n.services.resourceStore.data=e.initialI18nStore,o.options.wait=!1),e.initialLanguage&&o.i18n.changeLanguage(e.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return x(n,e),b(n,[{key:"getChildContext",value:function(){var t;return t={},S(t,a,this[a]),S(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var e=this,n=function(){e.options.bindI18n&&e.i18n&&e.i18n.on(e.options.bindI18n,e.onI18nChanged),e.options.bindStore&&e.i18n.store&&e.i18n.store.on(e.options.bindStore,e.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){e.mounted&&!e.state.ready&&e.setState({ready:!0}),e.options.wait&&e.mounted&&n()};if(e.i18n.isInitialized)t();else{var i=function n(){setTimeout(function(){e.i18n.off("initialized",n)},1e3),t()};e.i18n.on("initialized",i)}}),this.options.wait||n()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(e){return t.i18n.off(e,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(e){return t.i18n.store&&t.i18n.store.off(e,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,e=this,n=this.state,i=n.i18nLoadedAt,r=n.ready,s=(t={i18nLoadedAt:i},S(t,a,this[a]),S(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!T&&(T=!0,setTimeout(function(){delete e.i18n.options.isInitialSSR},100)),f.createElement(o,I({},this.props,s)))}}]),n}(e.Component);return p.WrappedComponent=o,p.contextTypes={i18n:n.object},p.childContextTypes=(s={},S(s,a,n.func.isRequired),S(s,"i18n",n.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,v(p,o)}}function o(t){return t&&(t.children||t.props&&t.props.children)}function a(t){return t&&t.children?t.children:t.props&&t.props.children}function s(t,e,n){return"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.forEach(function(e,n){var i=""+n;if("string"==typeof e)t=""+t+e;else if(o(e))t=t+"<"+i+">"+s("",a(e),n+1)+"</"+i+">";else if(f.isValidElement(e))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===e?"undefined":m(e))){var r=I({},e),p=r.format;delete r.format;var u=Object.keys(r);p&&1===u.length?t=t+"<"+i+">{{"+u[0]+", "+p+"}}</"+i+">":1===u.length&&(t=t+"<"+i+">{{"+u[0]+"}}</"+i+">")}}),t}function p(t,e,n){function i(t,e){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]);var r=e.split(N).reduce(function(t,e,n){return e&&t.push(e),t},[]);return r.reduce(function(e,s,p){var u=!isNaN(s),c=p>0&&!isNaN(r[p-1]);if(c){var l=t[parseInt(r[p-1],10)]||{};f.isValidElement(l)&&!o(l)&&(c=!1)}if(c)return e;if(u){var d=t[parseInt(s,10)]||{},h=f.isValidElement(d);if("string"==typeof d)e.push(d);else if(o(d)){var y=i(a(d),r[p+1]);e.push(f.cloneElement(d,I({},d.props,{key:p}),y))}else if("object"!==(void 0===d?"undefined":m(d))||h)e.push(d);else{var v=n.services.interpolator.interpolate(r[p+1],d,n.language);e.push(v)}}return u||c||e.push(s),e},[])}return i(t,e)}function u(t,e){for(var n=0,i=t.length;n<i;n++)if("object"===m(t[n])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[n])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var u=j(s.value,2),c=u[0],l=u[1];e(l,n,c)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else e(t[n],n)}function c(t){var e=[];return u(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===e.indexOf(t)&&e.push(t)})}),e}function l(t){var e=t.components,n=t.i18n,i=c(e);return new Promise(function(t){n.loadNamespaces(i,t)})}var f="default"in e?e.default:e;n="default"in n?n.default:n;var d={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},h={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},y="function"==typeof Object.getOwnPropertySymbols,v=function(t,e,n){if("string"!=typeof e){var i=Object.getOwnPropertyNames(e);y&&(i=i.concat(Object.getOwnPropertySymbols(e)));for(var r=0;r<i.length;++r)if(!(d[i[r]]||h[i[r]]||n&&n[i[r]]))try{t[i[r]]=e[i[r]]}catch(t){}}return t},m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},b=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),S=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},I=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},x=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},O=function(t,e){var n={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},w=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},j=function(){function t(t,e){var n=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),C={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},P=void 0,T=!1;r.setDefaults=function(t){C=I({},C,t)},r.setI18n=function(t){P=t};var _=function(t){function e(t,n){g(this,e);var i=w(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return x(e,t),b(e,[{key:"render",value:function(){var t=this,e=this.props.parent||"span",n=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=I({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),u=this.t(this.props.i18nKey,p);if(!u||"string"!=typeof u)return f.createElement("noscript",null);var c=[],l=function(e,n){if(e.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===n[e]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+e+" for interpolating "+u),n[e];var i=e.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===n[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+u),t.i18n.options.interpolation.format(n[r],o,t.i18n.language)};u.split(n).reduce(function(e,n,i){var r=void 0;if(i%2==0){if(0===n.length)return e;r=a?f.createElement(s,{dangerouslySetInnerHTML:{__html:n}}):n}else r=l(n,t.props);return e.push(r),e},c);var d={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var h="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){h=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(d["data-i18next-options"]=JSON.stringify({ns:h}))}return r&&(d.className=r),o&&(d.style=o),f.createElement.apply(this,[e,d].concat(c))}}]),e}(e.Component);_.propTypes={className:n.string},_.defaultProps={className:""},_.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var N=new RegExp("(?:<([^>]*)>(.*?)<\\/\\1>)","gi"),E=function(t){function e(t,n){g(this,e);var i=w(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return x(e,t),b(e,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,e=t.children,n=t.count,i=t.parent,r=t.i18nKey,o=O(t,["children","count","parent","i18nKey"]),a=s("",e,0),u=r||a,c=this.t(u,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:n});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var l="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){l=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:l}))}return f.createElement(i,o,p(e,c,this.i18n))}}]),e}(f.Component);E.propTypes={count:n.number,parent:n.string,i18nKey:n.string},E.defaultProps={parent:"div"},E.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var R=function(t){function n(t,e){g(this,n);var i=w(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return x(n,t),b(n,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return e.Children.only(t)}}]),n}(e.Component);R.propTypes={i18n:n.object.isRequired,children:n.element.isRequired},R.childContextTypes={i18n:n.object.isRequired},t.loadNamespaces=l,t.translate=r,t.Interpolate=_,t.I18nextProvider=R,t.Trans=E,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define("reactI18next",["exports","react","prop-types"],e):e(t.reactI18next=t.reactI18next||{},t.React,t.PropTypes)}(this,function(t,e,n){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?T.translateFuncName:o;return function(o){var s,p=function(e){function n(e,i){x(this,n);var o=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,i));o.i18n=i.i18n||e.i18n||r.i18n||_,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=w({},T,a,r),e.initialI18nStore&&(o.i18n.services.resourceStore.data=e.initialI18nStore,o.options.wait=!1),e.initialLanguage&&o.i18n.changeLanguage(e.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return I(n,e),O(n,[{key:"getChildContext",value:function(){var t;return t={},j(t,a,this[a]),j(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var e=this,n=function(){e.options.bindI18n&&e.i18n&&e.i18n.on(e.options.bindI18n,e.onI18nChanged),e.options.bindStore&&e.i18n.store&&e.i18n.store.on(e.options.bindStore,e.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){e.mounted&&!e.state.ready&&e.setState({ready:!0}),e.options.wait&&e.mounted&&n()};if(e.i18n.isInitialized)t();else{var i=function n(){setTimeout(function(){e.i18n.off("initialized",n)},1e3),t()};e.i18n.on("initialized",i)}}),this.options.wait||n()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(e){return t.i18n.off(e,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(e){return t.i18n.store&&t.i18n.store.off(e,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,e=this,n=this.state,i=n.i18nLoadedAt,r=n.ready,s=(t={i18nLoadedAt:i},j(t,a,this[a]),j(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!k&&(k=!0,setTimeout(function(){delete e.i18n.options.isInitialSSR},100)),y.createElement(o,w({},this.props,s)))}}]),n}(e.Component);return p.WrappedComponent=o,p.contextTypes={i18n:n.object},p.childContextTypes=(s={},j(s,a,n.func.isRequired),j(s,"i18n",n.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,b(p,o)}}function o(t,e,n,i,r){var o=e.indexOf("<",i),a=e.slice(i,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!r&&o>-1&&n+t.length>=0||" "!==a)&&t.push({type:"text",content:a})}function a(t){var e=[];for(var n in t)e.push(n+'="'+t[n]+'"');return e.length?" "+e.join(" "):""}function s(t,e){switch(e.type){case"text":return t+e.content;case"tag":return t+="<"+e.name+(e.attrs?a(e.attrs):"")+(e.voidElement?"/>":">"),e.voidElement?t:t+e.children.reduce(s,"")+"</"+e.name+">"}}function p(t){return t&&(t.children||t.props&&t.props.children)}function c(t){return t&&t.children?t.children:t.props&&t.props.children}function u(t,e,n){return"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.forEach(function(e,n){var i=""+n;if("string"==typeof e)t=""+t+e;else if(p(e))t=t+"<"+i+">"+u("",c(e),n+1)+"</"+i+">";else if(y.isValidElement(e))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===e?"undefined":S(e))){var r=w({},e),o=r.format;delete r.format;var a=Object.keys(r);o&&1===a.length?t=t+"<"+i+">{{"+a[0]+", "+o+"}}</"+i+">":1===a.length&&(t=t+"<"+i+">{{"+a[0]+"}}</"+i+">")}}),t}function l(t,e,n){function i(t,e){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.reduce(function(e,r,o){if("tag"===r.type){var a=t[parseInt(r.name,10)]||{},s=y.isValidElement(a);if("string"==typeof a)e.push(a);else if(p(a)){var u=i(c(a),r.children);e.push(y.cloneElement(a,w({},a.props,{key:o}),u))}else if("object"!==(void 0===a?"undefined":S(a))||s)e.push(a);else{var l=n.services.interpolator.interpolate(r.children[0].content,a,n.language);e.push(l)}}else"text"===r.type&&e.push(r.content);return e},[])}return i([{children:t}],D.parse("<0>"+e+"</0>"))[0].props.children}function f(t,e){for(var n=0,i=t.length;n<i;n++)if("object"===S(t[n])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[n])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var c=P(s.value,2),u=c[0],l=c[1];e(l,n,u)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else e(t[n],n)}function d(t){var e=[];return f(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===e.indexOf(t)&&e.push(t)})}),e}function h(t){var e=t.components,n=t.i18n,i=d(e);return new Promise(function(t){n.loadNamespaces(i,t)})}var y="default"in e?e.default:e;n="default"in n?n.default:n;var m={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},v={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},g="function"==typeof Object.getOwnPropertySymbols,b=function(t,e,n){if("string"!=typeof e){var i=Object.getOwnPropertyNames(e);g&&(i=i.concat(Object.getOwnPropertySymbols(e)));for(var r=0;r<i.length;++r)if(!(m[i[r]]||v[i[r]]||n&&n[i[r]]))try{t[i[r]]=e[i[r]]}catch(t){}}return t},S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},x=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},O=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),j=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},w=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},I=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},C=function(t,e){var n={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},E=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},P=function(){function t(t,e){var n=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),T={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},_=void 0,k=!1;r.setDefaults=function(t){T=w({},T,t)},r.setI18n=function(t){_=t};var N=function(t){function e(t,n){x(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),O(e,[{key:"render",value:function(){var t=this,e=this.props.parent||"span",n=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=w({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),c=this.t(this.props.i18nKey,p);if(!c||"string"!=typeof c)return y.createElement("noscript",null);var u=[],l=function(e,n){if(e.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===n[e]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+e+" for interpolating "+c),n[e];var i=e.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===n[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+c),t.i18n.options.interpolation.format(n[r],o,t.i18n.language)};c.split(n).reduce(function(e,n,i){var r=void 0;if(i%2==0){if(0===n.length)return e;r=a?y.createElement(s,{dangerouslySetInnerHTML:{__html:n}}):n}else r=l(n,t.props);return e.push(r),e},u);var f={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var d="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){d=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(f["data-i18next-options"]=JSON.stringify({ns:d}))}return r&&(f.className=r),o&&(f.style=o),y.createElement.apply(this,[e,f].concat(u))}}]),e}(e.Component);N.propTypes={className:n.string},N.defaultProps={className:""},N.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var R={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},L=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,A=function(t){var e,n=0,i=!0,r={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return t.replace(L,function(o){if("="===o)return i=!0,void n++;i?0===n?((R[o]||"/"===t.charAt(t.length-2))&&(r.voidElement=!0),r.name=o):(r.attrs[e]=o.replace(/^['"]|['"]$/g,""),e=void 0):(e&&(r.attrs[e]=e),e=o),n++,i=!1}),r},q=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,W=Object.create?Object.create(null):{},M=function(t,e){e||(e={}),e.components||(e.components=W);var n,i=[],r=-1,a=[],s={},p=!1;return t.replace(q,function(c,u){if(p){if(c!=="</"+n.name+">")return;p=!1}var l,f="/"!==c.charAt(1),d=0===c.indexOf("\x3c!--"),h=u+c.length,y=t.charAt(h);f&&!d&&(r++,n=A(c),"tag"===n.type&&e.components[n.name]&&(n.type="component",p=!0),n.voidElement||p||!y||"<"===y||o(n.children,t,r,h,e.ignoreWhitespace),s[n.tagName]=n,0===r&&i.push(n),l=a[r-1],l&&l.children.push(n),a[r]=n),(d||!f||n.voidElement)&&(d||r--,!p&&"<"!==y&&y&&(l=-1===r?i:a[r].children,o(l,t,r,h,e.ignoreWhitespace)))}),!i.length&&t.length&&o(i,t,0,0,e.ignoreWhitespace),i},K=function(t){return t.reduce(function(t,e){return t+s("",e)},"")},D={parse:M,stringify:K},$=function(t){function e(t,n){x(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),O(e,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,e=t.children,n=t.count,i=t.parent,r=t.i18nKey,o=C(t,["children","count","parent","i18nKey"]),a=u("",e,0),s=r||a,p=this.t(s,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:n});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var c="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){c=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:c}))}return y.createElement(i,o,l(e,p,this.i18n))}}]),e}(y.Component);$.propTypes={count:n.number,parent:n.string,i18nKey:n.string},$.defaultProps={parent:"div"},$.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var F=function(t){function n(t,e){x(this,n);var i=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return I(n,t),O(n,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return e.Children.only(t)}}]),n}(e.Component);F.propTypes={i18n:n.object.isRequired,children:n.element.isRequired},F.childContextTypes={i18n:n.object.isRequired},t.loadNamespaces=h,t.translate=r,t.Interpolate=N,t.I18nextProvider=F,t.Trans=$,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "react-i18next", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"description": "Internationalization for react done right. Using the i18next i18n ecosystem.", | ||
@@ -26,3 +26,7 @@ "main": "dist/commonjs/index.js", | ||
"dependencies": { | ||
"hoist-non-react-statics": "1.2.0" | ||
"hoist-non-react-statics": "1.2.0", | ||
"html-parse-stringify2": "2.0.1", | ||
"prop-types": "^15.5.10", | ||
"react": "^15.6.1", | ||
"react-dom": "^15.6.1" | ||
}, | ||
@@ -98,3 +102,4 @@ "devDependencies": { | ||
] | ||
} | ||
}, | ||
"lock": false | ||
} |
@@ -529,2 +529,210 @@ (function (global, factory) { | ||
/** | ||
* This file automatically generated from `pre-publish.js`. | ||
* Do not manually edit. | ||
*/ | ||
var index$2 = { | ||
"area": true, | ||
"base": true, | ||
"br": true, | ||
"col": true, | ||
"embed": true, | ||
"hr": true, | ||
"img": true, | ||
"input": true, | ||
"keygen": true, | ||
"link": true, | ||
"menuitem": true, | ||
"meta": true, | ||
"param": true, | ||
"source": true, | ||
"track": true, | ||
"wbr": true | ||
}; | ||
var attrRE = /([\w-]+)|=|(['"])([.\s\S]*?)\2/g; | ||
var parseTag = function (tag) { | ||
var i = 0; | ||
var key; | ||
var expectingValueAfterEquals = true; | ||
var res = { | ||
type: 'tag', | ||
name: '', | ||
voidElement: false, | ||
attrs: {}, | ||
children: [] | ||
}; | ||
tag.replace(attrRE, function (match) { | ||
if (match === '=') { | ||
expectingValueAfterEquals = true; | ||
i++; | ||
return; | ||
} | ||
if (!expectingValueAfterEquals) { | ||
if (key) { | ||
res.attrs[key] = key; // boolean attribute | ||
} | ||
key=match; | ||
} else { | ||
if (i === 0) { | ||
if (index$2[match] || tag.charAt(tag.length - 2) === '/') { | ||
res.voidElement = true; | ||
} | ||
res.name = match; | ||
} else { | ||
res.attrs[key] = match.replace(/^['"]|['"]$/g, ''); | ||
key=undefined; | ||
} | ||
} | ||
i++; | ||
expectingValueAfterEquals = false; | ||
}); | ||
return res; | ||
}; | ||
/*jshint -W030 */ | ||
var tagRE = /(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g; | ||
// re-used obj for quick lookups of components | ||
var empty = Object.create ? Object.create(null) : {}; | ||
// common logic for pushing a child node onto a list | ||
function pushTextNode(list, html, level, start, ignoreWhitespace) { | ||
// calculate correct end of the content slice in case there's | ||
// no tag after the text node. | ||
var end = html.indexOf('<', start); | ||
var content = html.slice(start, end === -1 ? undefined : end); | ||
// if a node is nothing but whitespace, collapse it as the spec states: | ||
// https://www.w3.org/TR/html4/struct/text.html#h-9.1 | ||
if (/^\s*$/.test(content)) { | ||
content = ' '; | ||
} | ||
// don't add whitespace-only text nodes if they would be trailing text nodes | ||
// or if they would be leading whitespace-only text nodes: | ||
// * end > -1 indicates this is not a trailing text node | ||
// * leading node is when level is -1 and list has length 0 | ||
if ((!ignoreWhitespace && end > -1 && level + list.length >= 0) || content !== ' ') { | ||
list.push({ | ||
type: 'text', | ||
content: content | ||
}); | ||
} | ||
} | ||
var parse = function parse(html, options) { | ||
options || (options = {}); | ||
options.components || (options.components = empty); | ||
var result = []; | ||
var current; | ||
var level = -1; | ||
var arr = []; | ||
var byTag = {}; | ||
var inComponent = false; | ||
html.replace(tagRE, function (tag, index) { | ||
if (inComponent) { | ||
if (tag !== ('</' + current.name + '>')) { | ||
return; | ||
} else { | ||
inComponent = false; | ||
} | ||
} | ||
var isOpen = tag.charAt(1) !== '/'; | ||
var isComment = tag.indexOf('<!--') === 0; | ||
var start = index + tag.length; | ||
var nextChar = html.charAt(start); | ||
var parent; | ||
if (isOpen && !isComment) { | ||
level++; | ||
current = parseTag(tag); | ||
if (current.type === 'tag' && options.components[current.name]) { | ||
current.type = 'component'; | ||
inComponent = true; | ||
} | ||
if (!current.voidElement && !inComponent && nextChar && nextChar !== '<') { | ||
pushTextNode(current.children, html, level, start, options.ignoreWhitespace); | ||
} | ||
byTag[current.tagName] = current; | ||
// if we're at root, push new base node | ||
if (level === 0) { | ||
result.push(current); | ||
} | ||
parent = arr[level - 1]; | ||
if (parent) { | ||
parent.children.push(current); | ||
} | ||
arr[level] = current; | ||
} | ||
if (isComment || !isOpen || current.voidElement) { | ||
if (!isComment) { | ||
level--; | ||
} | ||
if (!inComponent && nextChar !== '<' && nextChar) { | ||
// trailing text node | ||
// if we're at the root, push a base text node. otherwise add as | ||
// a child to the current node. | ||
parent = level === -1 ? result : arr[level].children; | ||
pushTextNode(parent, html, level, start, options.ignoreWhitespace); | ||
} | ||
} | ||
}); | ||
// If the "html" passed isn't actually html, add it as a text node. | ||
if (!result.length && html.length) { | ||
pushTextNode(result, html, 0, 0, options.ignoreWhitespace); | ||
} | ||
return result; | ||
}; | ||
function attrString(attrs) { | ||
var buff = []; | ||
for (var key in attrs) { | ||
buff.push(key + '="' + attrs[key] + '"'); | ||
} | ||
if (!buff.length) { | ||
return ''; | ||
} | ||
return ' ' + buff.join(' '); | ||
} | ||
function stringify(buff, doc) { | ||
switch (doc.type) { | ||
case 'text': | ||
return buff + doc.content; | ||
case 'tag': | ||
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>'); | ||
if (doc.voidElement) { | ||
return buff; | ||
} | ||
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>'; | ||
} | ||
} | ||
var stringify_1 = function (doc) { | ||
return doc.reduce(function (token, rootEl) { | ||
return token + stringify('', rootEl); | ||
}, ''); | ||
}; | ||
var index$1 = { | ||
parse: parse, | ||
stringify: stringify_1 | ||
}; | ||
function hasChildren(node) { | ||
@@ -569,46 +777,32 @@ return node && (node.children || node.props && node.props.children); | ||
var REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
var ast = index$1.parse('<0>' + targetString + '</0>'); | ||
var toRender = str.split(REGEXP).reduce(function (mem, match, i) { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce(function (mem, part, i) { | ||
// is a tag | ||
var isTag = !isNaN(part); | ||
var previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
var child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (React__default.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
return astNodes.reduce(function (mem, node, i) { | ||
if (node.type === 'tag') { | ||
var child = reactNodes[parseInt(node.name, 10)] || {}; | ||
var isElement = React__default.isValidElement(child); | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (typeof child === 'string') { | ||
mem.push(child); | ||
} else if (hasChildren(child)) { | ||
var inner = mapAST(getChildren(child), node.children); | ||
if (isTag) { | ||
var _child = nodes[parseInt(part, 10)] || {}; | ||
var isElement = React__default.isValidElement(_child); | ||
if (typeof _child === 'string') { | ||
mem.push(_child); | ||
} else if (hasChildren(_child)) { | ||
var inner = parseChildren(getChildren(_child), toRender[i + 1]); | ||
mem.push(React__default.cloneElement(_child, _extends({}, _child.props, { key: i }), inner)); | ||
} else if ((typeof _child === 'undefined' ? 'undefined' : _typeof(_child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], _child, i18n.language); | ||
mem.push(React__default.cloneElement(child, _extends({}, child.props, { key: i }), inner)); | ||
} else if ((typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' && !isElement) { | ||
var interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
} else { | ||
mem.push(_child); | ||
mem.push(child); | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -618,3 +812,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
var result = mapAST([{ children: children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -621,0 +819,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define("reactI18next",["exports","react","prop-types"],e):e(t.reactI18next=t.reactI18next||{},t.React,t.PropTypes)}(this,function(t,e,n){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?C.translateFuncName:o;return function(o){var s,p=function(e){function n(e,i){g(this,n);var o=w(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,i));o.i18n=i.i18n||e.i18n||r.i18n||P,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=I({},C,a,r),e.initialI18nStore&&(o.i18n.services.resourceStore.data=e.initialI18nStore,o.options.wait=!1),e.initialLanguage&&o.i18n.changeLanguage(e.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return x(n,e),b(n,[{key:"getChildContext",value:function(){var t;return t={},S(t,a,this[a]),S(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var e=this,n=function(){e.options.bindI18n&&e.i18n&&e.i18n.on(e.options.bindI18n,e.onI18nChanged),e.options.bindStore&&e.i18n.store&&e.i18n.store.on(e.options.bindStore,e.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){e.mounted&&!e.state.ready&&e.setState({ready:!0}),e.options.wait&&e.mounted&&n()};if(e.i18n.isInitialized)t();else{var i=function n(){setTimeout(function(){e.i18n.off("initialized",n)},1e3),t()};e.i18n.on("initialized",i)}}),this.options.wait||n()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(e){return t.i18n.off(e,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(e){return t.i18n.store&&t.i18n.store.off(e,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,e=this,n=this.state,i=n.i18nLoadedAt,r=n.ready,s=(t={i18nLoadedAt:i},S(t,a,this[a]),S(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!T&&(T=!0,setTimeout(function(){delete e.i18n.options.isInitialSSR},100)),f.createElement(o,I({},this.props,s)))}}]),n}(e.Component);return p.WrappedComponent=o,p.contextTypes={i18n:n.object},p.childContextTypes=(s={},S(s,a,n.func.isRequired),S(s,"i18n",n.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,v(p,o)}}function o(t){return t&&(t.children||t.props&&t.props.children)}function a(t){return t&&t.children?t.children:t.props&&t.props.children}function s(t,e,n){return"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.forEach(function(e,n){var i=""+n;if("string"==typeof e)t=""+t+e;else if(o(e))t=t+"<"+i+">"+s("",a(e),n+1)+"</"+i+">";else if(f.isValidElement(e))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===e?"undefined":m(e))){var r=I({},e),p=r.format;delete r.format;var u=Object.keys(r);p&&1===u.length?t=t+"<"+i+">{{"+u[0]+", "+p+"}}</"+i+">":1===u.length&&(t=t+"<"+i+">{{"+u[0]+"}}</"+i+">")}}),t}function p(t,e,n){function i(t,e){"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]);var r=e.split(N).reduce(function(t,e,n){return e&&t.push(e),t},[]);return r.reduce(function(e,s,p){var u=!isNaN(s),c=p>0&&!isNaN(r[p-1]);if(c){var l=t[parseInt(r[p-1],10)]||{};f.isValidElement(l)&&!o(l)&&(c=!1)}if(c)return e;if(u){var d=t[parseInt(s,10)]||{},h=f.isValidElement(d);if("string"==typeof d)e.push(d);else if(o(d)){var y=i(a(d),r[p+1]);e.push(f.cloneElement(d,I({},d.props,{key:p}),y))}else if("object"!==(void 0===d?"undefined":m(d))||h)e.push(d);else{var v=n.services.interpolator.interpolate(r[p+1],d,n.language);e.push(v)}}return u||c||e.push(s),e},[])}return i(t,e)}function u(t,e){for(var n=0,i=t.length;n<i;n++)if("object"===m(t[n])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[n])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var u=j(s.value,2),c=u[0],l=u[1];e(l,n,c)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else e(t[n],n)}function c(t){var e=[];return u(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===e.indexOf(t)&&e.push(t)})}),e}function l(t){var e=t.components,n=t.i18n,i=c(e);return new Promise(function(t){n.loadNamespaces(i,t)})}var f="default"in e?e.default:e;n="default"in n?n.default:n;var d={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},h={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},y="function"==typeof Object.getOwnPropertySymbols,v=function(t,e,n){if("string"!=typeof e){var i=Object.getOwnPropertyNames(e);y&&(i=i.concat(Object.getOwnPropertySymbols(e)));for(var r=0;r<i.length;++r)if(!(d[i[r]]||h[i[r]]||n&&n[i[r]]))try{t[i[r]]=e[i[r]]}catch(t){}}return t},m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},b=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),S=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},I=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},x=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},O=function(t,e){var n={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},w=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},j=function(){function t(t,e){var n=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),C={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},P=void 0,T=!1;r.setDefaults=function(t){C=I({},C,t)},r.setI18n=function(t){P=t};var _=function(t){function e(t,n){g(this,e);var i=w(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return x(e,t),b(e,[{key:"render",value:function(){var t=this,e=this.props.parent||"span",n=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=I({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),u=this.t(this.props.i18nKey,p);if(!u||"string"!=typeof u)return f.createElement("noscript",null);var c=[],l=function(e,n){if(e.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===n[e]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+e+" for interpolating "+u),n[e];var i=e.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===n[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+u),t.i18n.options.interpolation.format(n[r],o,t.i18n.language)};u.split(n).reduce(function(e,n,i){var r=void 0;if(i%2==0){if(0===n.length)return e;r=a?f.createElement(s,{dangerouslySetInnerHTML:{__html:n}}):n}else r=l(n,t.props);return e.push(r),e},c);var d={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var h="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){h=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(d["data-i18next-options"]=JSON.stringify({ns:h}))}return r&&(d.className=r),o&&(d.style=o),f.createElement.apply(this,[e,d].concat(c))}}]),e}(e.Component);_.propTypes={className:n.string},_.defaultProps={className:""},_.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var N=new RegExp("(?:<([^>]*)>(.*?)<\\/\\1>)","gi"),E=function(t){function e(t,n){g(this,e);var i=w(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return x(e,t),b(e,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,e=t.children,n=t.count,i=t.parent,r=t.i18nKey,o=O(t,["children","count","parent","i18nKey"]),a=s("",e,0),u=r||a,c=this.t(u,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:n});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var l="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){l=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:l}))}return f.createElement(i,o,p(e,c,this.i18n))}}]),e}(f.Component);E.propTypes={count:n.number,parent:n.string,i18nKey:n.string},E.defaultProps={parent:"div"},E.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var R=function(t){function n(t,e){g(this,n);var i=w(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return x(n,t),b(n,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return e.Children.only(t)}}]),n}(e.Component);R.propTypes={i18n:n.object.isRequired,children:n.element.isRequired},R.childContextTypes={i18n:n.object.isRequired},t.loadNamespaces=l,t.translate=r,t.Interpolate=_,t.I18nextProvider=R,t.Trans=E,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("prop-types")):"function"==typeof define&&define.amd?define("reactI18next",["exports","react","prop-types"],e):e(t.reactI18next=t.reactI18next||{},t.React,t.PropTypes)}(this,function(t,e,n){"use strict";function i(t){return t.displayName||t.name||"Component"}function r(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.translateFuncName,a=void 0===o?T.translateFuncName:o;return function(o){var s,p=function(e){function n(e,i){x(this,n);var o=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e,i));o.i18n=i.i18n||e.i18n||r.i18n||_,"string"==typeof(t=t||o.i18n.options.defaultNS)&&(t=[t]);var a=o.i18n&&o.i18n.options.react||{};return o.options=w({},T,a,r),e.initialI18nStore&&(o.i18n.services.resourceStore.data=e.initialI18nStore,o.options.wait=!1),e.initialLanguage&&o.i18n.changeLanguage(e.initialLanguage),o.i18n.options.isInitialSSR&&(o.options.wait=!1),o.state={i18nLoadedAt:null,ready:!1},o.onI18nChanged=o.onI18nChanged.bind(o),o.getWrappedInstance=o.getWrappedInstance.bind(o),o}return I(n,e),O(n,[{key:"getChildContext",value:function(){var t;return t={},j(t,a,this[a]),j(t,"i18n",this.i18n),t}},{key:"componentWillMount",value:function(){this[a]=this.i18n.getFixedT(null,"fallback"===this.options.nsMode?t:t[0])}},{key:"componentDidMount",value:function(){var e=this,n=function(){e.options.bindI18n&&e.i18n&&e.i18n.on(e.options.bindI18n,e.onI18nChanged),e.options.bindStore&&e.i18n.store&&e.i18n.store.on(e.options.bindStore,e.onI18nChanged)};this.mounted=!0,this.i18n.loadNamespaces(t,function(){var t=function(){e.mounted&&!e.state.ready&&e.setState({ready:!0}),e.options.wait&&e.mounted&&n()};if(e.i18n.isInitialized)t();else{var i=function n(){setTimeout(function(){e.i18n.off("initialized",n)},1e3),t()};e.i18n.on("initialized",i)}}),this.options.wait||n()}},{key:"componentWillUnmount",value:function(){var t=this;if(this.mounted=!1,this.onI18nChanged){if(this.options.bindI18n){this.options.bindI18n.split(" ").forEach(function(e){return t.i18n.off(e,t.onI18nChanged)})}if(this.options.bindStore){this.options.bindStore.split(" ").forEach(function(e){return t.i18n.store&&t.i18n.store.off(e,t.onI18nChanged)})}}}},{key:"onI18nChanged",value:function(){this.mounted&&this.setState({i18nLoadedAt:new Date})}},{key:"getWrappedInstance",value:function(){return this.options.withRef||console.error("To access the wrapped instance, you need to specify { withRef: true } as the second argument of the translate() call."),this.refs.wrappedInstance}},{key:"render",value:function(){var t,e=this,n=this.state,i=n.i18nLoadedAt,r=n.ready,s=(t={i18nLoadedAt:i},j(t,a,this[a]),j(t,"i18n",this.i18n),t);return this.options.withRef&&(s.ref="wrappedInstance"),!r&&this.options.wait?null:(this.i18n.options.isInitialSSR&&!k&&(k=!0,setTimeout(function(){delete e.i18n.options.isInitialSSR},100)),y.createElement(o,w({},this.props,s)))}}]),n}(e.Component);return p.WrappedComponent=o,p.contextTypes={i18n:n.object},p.childContextTypes=(s={},j(s,a,n.func.isRequired),j(s,"i18n",n.object),s),p.displayName="Translate("+i(o)+")",p.namespaces=t,b(p,o)}}function o(t,e,n,i,r){var o=e.indexOf("<",i),a=e.slice(i,-1===o?void 0:o);/^\s*$/.test(a)&&(a=" "),(!r&&o>-1&&n+t.length>=0||" "!==a)&&t.push({type:"text",content:a})}function a(t){var e=[];for(var n in t)e.push(n+'="'+t[n]+'"');return e.length?" "+e.join(" "):""}function s(t,e){switch(e.type){case"text":return t+e.content;case"tag":return t+="<"+e.name+(e.attrs?a(e.attrs):"")+(e.voidElement?"/>":">"),e.voidElement?t:t+e.children.reduce(s,"")+"</"+e.name+">"}}function p(t){return t&&(t.children||t.props&&t.props.children)}function c(t){return t&&t.children?t.children:t.props&&t.props.children}function u(t,e,n){return"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.forEach(function(e,n){var i=""+n;if("string"==typeof e)t=""+t+e;else if(p(e))t=t+"<"+i+">"+u("",c(e),n+1)+"</"+i+">";else if(y.isValidElement(e))t=t+"<"+i+"></"+i+">";else if("object"===(void 0===e?"undefined":S(e))){var r=w({},e),o=r.format;delete r.format;var a=Object.keys(r);o&&1===a.length?t=t+"<"+i+">{{"+a[0]+", "+o+"}}</"+i+">":1===a.length&&(t=t+"<"+i+">{{"+a[0]+"}}</"+i+">")}}),t}function l(t,e,n){function i(t,e){return"[object Array]"!==Object.prototype.toString.call(t)&&(t=[t]),"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),e.reduce(function(e,r,o){if("tag"===r.type){var a=t[parseInt(r.name,10)]||{},s=y.isValidElement(a);if("string"==typeof a)e.push(a);else if(p(a)){var u=i(c(a),r.children);e.push(y.cloneElement(a,w({},a.props,{key:o}),u))}else if("object"!==(void 0===a?"undefined":S(a))||s)e.push(a);else{var l=n.services.interpolator.interpolate(r.children[0].content,a,n.language);e.push(l)}}else"text"===r.type&&e.push(r.content);return e},[])}return i([{children:t}],D.parse("<0>"+e+"</0>"))[0].props.children}function f(t,e){for(var n=0,i=t.length;n<i;n++)if("object"===S(t[n])){var r=!0,o=!1,a=void 0;try{for(var s,p=Object.entries(t[n])[Symbol.iterator]();!(r=(s=p.next()).done);r=!0){var c=P(s.value,2),u=c[0],l=c[1];e(l,n,u)}}catch(t){o=!0,a=t}finally{try{!r&&p.return&&p.return()}finally{if(o)throw a}}}else e(t[n],n)}function d(t){var e=[];return f(t,function(t){t&&t.namespaces&&t.namespaces.forEach(function(t){-1===e.indexOf(t)&&e.push(t)})}),e}function h(t){var e=t.components,n=t.i18n,i=d(e);return new Promise(function(t){n.loadNamespaces(i,t)})}var y="default"in e?e.default:e;n="default"in n?n.default:n;var m={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},v={name:!0,length:!0,prototype:!0,caller:!0,arguments:!0,arity:!0},g="function"==typeof Object.getOwnPropertySymbols,b=function(t,e,n){if("string"!=typeof e){var i=Object.getOwnPropertyNames(e);g&&(i=i.concat(Object.getOwnPropertySymbols(e)));for(var r=0;r<i.length;++r)if(!(m[i[r]]||v[i[r]]||n&&n[i[r]]))try{t[i[r]]=e[i[r]]}catch(t){}}return t},S="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},x=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},O=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),j=function(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t},w=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},I=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},C=function(t,e){var n={};for(var i in t)e.indexOf(i)>=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},E=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},P=function(){function t(t,e){var n=[],i=!0,r=!1,o=void 0;try{for(var a,s=t[Symbol.iterator]();!(i=(a=s.next()).done)&&(n.push(a.value),!e||n.length!==e);i=!0);}catch(t){r=!0,o=t}finally{try{!i&&s.return&&s.return()}finally{if(r)throw o}}return n}return function(e,n){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,n);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),T={wait:!1,withRef:!1,bindI18n:"languageChanged loaded",bindStore:"added removed",translateFuncName:"t",nsMode:"default"},_=void 0,k=!1;r.setDefaults=function(t){T=w({},T,t)},r.setI18n=function(t){_=t};var N=function(t){function e(t,n){x(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),O(e,[{key:"render",value:function(){var t=this,e=this.props.parent||"span",n=this.props.regexp||this.i18n.services.interpolator.regexp,i=this.props,r=i.className,o=i.style,a=this.props.useDangerouslySetInnerHTML||!1,s=this.props.dangerouslySetInnerHTMLPartElement||"span",p=w({},this.props.options,{interpolation:{prefix:"#$?",suffix:"?$#"}}),c=this.t(this.props.i18nKey,p);if(!c||"string"!=typeof c)return y.createElement("noscript",null);var u=[],l=function(e,n){if(e.indexOf(t.i18n.options.interpolation.formatSeparator)<0)return void 0===n[e]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+e+" for interpolating "+c),n[e];var i=e.split(t.i18n.options.interpolation.formatSeparator),r=i.shift().trim(),o=i.join(t.i18n.options.interpolation.formatSeparator).trim();return void 0===n[r]&&t.i18n.services.logger.warn("interpolator: missed to pass in variable "+r+" for interpolating "+c),t.i18n.options.interpolation.format(n[r],o,t.i18n.language)};c.split(n).reduce(function(e,n,i){var r=void 0;if(i%2==0){if(0===n.length)return e;r=a?y.createElement(s,{dangerouslySetInnerHTML:{__html:n}}):n}else r=l(n,t.props);return e.push(r),e},u);var f={};if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var d="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(this.props.i18nKey&&this.i18n.options.nsSeparator&&this.props.i18nKey.indexOf(this.i18n.options.nsSeparator)>-1){d=this.props.i18nKey.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(f["data-i18next-options"]=JSON.stringify({ns:d}))}return r&&(f.className=r),o&&(f.style=o),y.createElement.apply(this,[e,f].concat(u))}}]),e}(e.Component);N.propTypes={className:n.string},N.defaultProps={className:""},N.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var R={area:!0,base:!0,br:!0,col:!0,embed:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,menuitem:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},L=/([\w-]+)|=|(['"])([.\s\S]*?)\2/g,A=function(t){var e,n=0,i=!0,r={type:"tag",name:"",voidElement:!1,attrs:{},children:[]};return t.replace(L,function(o){if("="===o)return i=!0,void n++;i?0===n?((R[o]||"/"===t.charAt(t.length-2))&&(r.voidElement=!0),r.name=o):(r.attrs[e]=o.replace(/^['"]|['"]$/g,""),e=void 0):(e&&(r.attrs[e]=e),e=o),n++,i=!1}),r},q=/(?:<!--[\S\s]*?-->|<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>)/g,W=Object.create?Object.create(null):{},M=function(t,e){e||(e={}),e.components||(e.components=W);var n,i=[],r=-1,a=[],s={},p=!1;return t.replace(q,function(c,u){if(p){if(c!=="</"+n.name+">")return;p=!1}var l,f="/"!==c.charAt(1),d=0===c.indexOf("\x3c!--"),h=u+c.length,y=t.charAt(h);f&&!d&&(r++,n=A(c),"tag"===n.type&&e.components[n.name]&&(n.type="component",p=!0),n.voidElement||p||!y||"<"===y||o(n.children,t,r,h,e.ignoreWhitespace),s[n.tagName]=n,0===r&&i.push(n),l=a[r-1],l&&l.children.push(n),a[r]=n),(d||!f||n.voidElement)&&(d||r--,!p&&"<"!==y&&y&&(l=-1===r?i:a[r].children,o(l,t,r,h,e.ignoreWhitespace)))}),!i.length&&t.length&&o(i,t,0,0,e.ignoreWhitespace),i},K=function(t){return t.reduce(function(t,e){return t+s("",e)},"")},D={parse:M,stringify:K},$=function(t){function e(t,n){x(this,e);var i=E(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return i.i18n=n.i18n,i.t=n.t,i}return I(e,t),O(e,[{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var t=this.props,e=t.children,n=t.count,i=t.parent,r=t.i18nKey,o=C(t,["children","count","parent","i18nKey"]),a=u("",e,0),s=r||a,p=this.t(s,{interpolation:{prefix:"#$?",suffix:"?$#"},defaultValue:a,count:n});if(this.i18n.options.react&&this.i18n.options.react.exposeNamespace){var c="string"==typeof this.t.ns?this.t.ns:this.t.ns[0];if(r&&this.i18n.options.nsSeparator&&r.indexOf(this.i18n.options.nsSeparator)>-1){c=r.split(this.i18n.options.nsSeparator)[0]}this.t.ns&&(o["data-i18next-options"]=JSON.stringify({ns:c}))}return y.createElement(i,o,l(e,p,this.i18n))}}]),e}(y.Component);$.propTypes={count:n.number,parent:n.string,i18nKey:n.string},$.defaultProps={parent:"div"},$.contextTypes={i18n:n.object.isRequired,t:n.func.isRequired};var F=function(t){function n(t,e){x(this,n);var i=E(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,t,e));return i.i18n=t.i18n,t.initialI18nStore&&(i.i18n.services.resourceStore.data=t.initialI18nStore,i.i18n.options.isInitialSSR=!0),t.initialLanguage&&i.i18n.changeLanguage(t.initialLanguage),i}return I(n,t),O(n,[{key:"getChildContext",value:function(){return{i18n:this.i18n}}},{key:"componentWillReceiveProps",value:function(t){if(this.props.i18n!==t.i18n)throw new Error("[react-i18next][I18nextProvider]does not support changing the i18n object.")}},{key:"render",value:function(){var t=this.props.children;return e.Children.only(t)}}]),n}(e.Component);F.propTypes={i18n:n.object.isRequired,children:n.element.isRequired},F.childContextTypes={i18n:n.object.isRequired},t.loadNamespaces=h,t.translate=r,t.Interpolate=N,t.I18nextProvider=F,t.Trans=$,Object.defineProperty(t,"__esModule",{value:!0})}); |
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import HTML from 'html-parse-stringify2'; | ||
@@ -43,27 +44,15 @@ function hasChildren(node) { | ||
const REGEXP = new RegExp('(?:<([^>]*)>(.*?)<\\/\\1>)', 'gi'); | ||
function renderNodes(children, targetString, i18n) { | ||
function parseChildren(nodes, str) { | ||
if (Object.prototype.toString.call(nodes) !== '[object Array]') nodes = [nodes]; | ||
// parse ast from string with additional wrapper tag | ||
// -> avoids issues in parser removing prepending text nodes | ||
const ast = HTML.parse(`<0>${targetString}</0>`); | ||
const toRender = str.split(REGEXP).reduce((mem, match, i) => { | ||
if (match) mem.push(match); | ||
return mem; | ||
}, []); | ||
function mapAST(reactNodes, astNodes) { | ||
if (Object.prototype.toString.call(reactNodes) !== '[object Array]') reactNodes = [reactNodes]; | ||
if (Object.prototype.toString.call(astNodes) !== '[object Array]') astNodes = [astNodes]; | ||
return toRender.reduce((mem, part, i) => { | ||
// is a tag | ||
const isTag = !isNaN(part); | ||
let previousIsTag = i > 0 ? !isNaN(toRender[i - 1]) : false; | ||
if (previousIsTag) { | ||
const child = nodes[parseInt(toRender[i - 1], 10)] || {}; | ||
if (React.isValidElement(child) && !hasChildren(child)) previousIsTag = false; | ||
} | ||
// will be rendered inside child | ||
if (previousIsTag) return mem; | ||
if (isTag) { | ||
const child = nodes[parseInt(part, 10)] || {}; | ||
return astNodes.reduce((mem, node, i) => { | ||
if (node.type === 'tag') { | ||
const child = reactNodes[parseInt(node.name, 10)] || {}; | ||
const isElement = React.isValidElement(child); | ||
@@ -74,3 +63,3 @@ | ||
} else if (hasChildren(child)) { | ||
const inner = parseChildren(getChildren(child), toRender[i + 1]); | ||
const inner = mapAST(getChildren(child), node.children); | ||
@@ -83,3 +72,3 @@ mem.push(React.cloneElement( | ||
} else if (typeof child === 'object' && !isElement) { | ||
const interpolated = i18n.services.interpolator.interpolate(toRender[i + 1], child, i18n.language); | ||
const interpolated = i18n.services.interpolator.interpolate(node.children[0].content, child, i18n.language); | ||
mem.push(interpolated); | ||
@@ -89,7 +78,5 @@ } else { | ||
} | ||
} else if (node.type === 'text') { | ||
mem.push(node.content); | ||
} | ||
// no element just a string | ||
if (!isTag && !previousIsTag) mem.push(part); | ||
return mem; | ||
@@ -99,3 +86,7 @@ }, []); | ||
return parseChildren(children, targetString); | ||
// call mapAST with having react nodes nested into additional node like | ||
// we did for the string ast from translation | ||
// return the children of that extra node to get expected result | ||
const result = mapAST([{ children }], ast); | ||
return result[0].props.children; | ||
} | ||
@@ -147,3 +138,3 @@ | ||
Trans.defaultProps = { | ||
parent: 'div', | ||
parent: 'div' | ||
}; | ||
@@ -150,0 +141,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
222390
4072
9
31
+ Addedhtml-parse-stringify2@2.0.1
+ Addedprop-types@^15.5.10
+ Addedreact@^15.6.1
+ Addedreact-dom@^15.6.1
+ Addedasap@2.0.6(transitive)
+ Addedcore-js@1.2.7(transitive)
+ Addedcreate-react-class@15.7.0(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedfbjs@0.8.18(transitive)
+ Addedhtml-parse-stringify2@2.0.1(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedpromise@7.3.1(transitive)
+ Addedreact@15.7.0(transitive)
+ Addedreact-dom@15.7.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsetimmediate@1.0.5(transitive)
+ Addedua-parser-js@0.7.39(transitive)
+ Addedvoid-elements@2.0.1(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)
- Removedreact@18.3.1(transitive)
- Removedreact-dom@18.3.1(transitive)
- Removedscheduler@0.23.2(transitive)