Do not discard non breaking space (fix #151)

This commit is contained in:
Antonino Porcino 2016-07-08 08:58:55 +02:00
parent fb8d4028dd
commit c801cb6acf
4 changed files with 21 additions and 2 deletions

View File

@ -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) : '';
}
}

View File

@ -0,0 +1 @@
<div>&#160;</div>

View File

@ -0,0 +1,9 @@
define([
'react',
'lodash'
], function (React, _) {
'use strict';
return function () {
return React.createElement('div', {}, '\xA0');
};
});

View File

@ -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'});
});