mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
Do not discard non breaking space (fix #151)
This commit is contained in:
parent
fb8d4028dd
commit
c801cb6acf
@ -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) : '';
|
||||
}
|
||||
}
|
||||
|
||||
|
1
test/data/non-breaking-space.rt
Normal file
1
test/data/non-breaking-space.rt
Normal file
@ -0,0 +1 @@
|
||||
<div> </div>
|
9
test/data/non-breaking-space.rt.js
Normal file
9
test/data/non-breaking-space.rt.js
Normal file
@ -0,0 +1,9 @@
|
||||
define([
|
||||
'react',
|
||||
'lodash'
|
||||
], function (React, _) {
|
||||
'use strict';
|
||||
return function () {
|
||||
return React.createElement('div', {}, '\xA0');
|
||||
};
|
||||
});
|
@ -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'});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user