diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 196093b..31102fe 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -293,6 +293,15 @@ function hasNonSimpleChildren(node) { return _.some(node.children, child => child.type === 'tag' && child.attribs[repeatAttr]); } +/** + * Trims a string the same way as String.prototype.trim(), but preserving all non breaking spaces ('\xA0') + * @param {string} text + * @return {string} + */ +function trimHtmlText(text) { + return text.replace(/^[ \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+|[ \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+$/g, ''); +} + /** * @param node * @param {Context} context @@ -420,7 +429,7 @@ function convertHtmlToReact(node, context) { const sanitizedComment = node.data.split('*/').join('* /'); return commentTemplate({data: sanitizedComment}); } else if (node.type === 'text') { - return node.data.trim() ? utils.convertText(node, context, node.data) : ''; + return trimHtmlText(node.data) ? utils.convertText(node, context, node.data) : ''; } } diff --git a/test/data/non-breaking-space.rt b/test/data/non-breaking-space.rt new file mode 100644 index 0000000..f2e6264 --- /dev/null +++ b/test/data/non-breaking-space.rt @@ -0,0 +1 @@ +