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 @@ +
 
\ No newline at end of file diff --git a/test/data/non-breaking-space.rt.js b/test/data/non-breaking-space.rt.js new file mode 100644 index 0000000..11375d0 --- /dev/null +++ b/test/data/non-breaking-space.rt.js @@ -0,0 +1,9 @@ +define([ + 'react', + 'lodash' +], function (React, _) { + 'use strict'; + return function () { + return React.createElement('div', {}, '\xA0'); + }; +}); \ No newline at end of file diff --git a/test/src/rt.valid.spec.js b/test/src/rt.valid.spec.js index 3d7a344..80aba77 100644 --- a/test/src/rt.valid.spec.js +++ b/test/src/rt.valid.spec.js @@ -30,7 +30,7 @@ module.exports = { }); test('conversion test', t => { - const files = ['div.rt', 'test.rt', 'repeat.rt', 'repeat-with-index.rt', 'inputs.rt', 'virtual.rt', 'stateless.rt', 'style-vendor-prefix.rt']; + const files = ['div.rt', 'test.rt', 'repeat.rt', 'repeat-with-index.rt', 'inputs.rt', 'virtual.rt', 'stateless.rt', 'style-vendor-prefix.rt', 'non-breaking-space.rt']; testFiles(t, files, {modules: 'amd'}); });