diff --git a/src/reactTemplates.js b/src/reactTemplates.js index 54c906b..3b43945 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -3,7 +3,6 @@ const cheerio = require('cheerio'); const _ = require('lodash'); const esprima = require('esprima'); const escodegen = require('escodegen'); -const normalizeHtmlWhitespace = require('normalize-html-whitespace'); const reactDOMSupport = require('./reactDOMSupport'); const reactNativeSupport = require('./reactNativeSupport'); const reactPropTemplates = require('./reactPropTemplates'); @@ -440,15 +439,11 @@ function convertHtmlToReact(node, context) { const sanitizedComment = node.data.split('*/').join('* /'); return commentTemplate({data: sanitizedComment}); } else if (node.type === 'text') { - let text = node.data; const parentNode = node.parent; - if (parentNode !== undefined) { - const preserveWhitespaces = parentNode.name === 'pre' || parentNode.name === 'textarea' || _.has(parentNode.attribs, preAttr); - if (context.options.normalizeHtmlWhitespace && !preserveWhitespaces) { - text = normalizeHtmlWhitespace(text); - } - } - return trimHtmlText(text) ? utils.convertText(node, context, text) : ''; + const overrideNormalize = parentNode !== undefined && (parentNode.name === 'pre' || parentNode.name === 'textarea' || _.has(parentNode.attribs, preAttr)); + const normalizeWhitespaces = context.options.normalizeHtmlWhitespace && !overrideNormalize; + const text = node.data; + return trimHtmlText(text) ? utils.convertText(node, context, text, normalizeWhitespaces) : ''; } } diff --git a/src/utils.js b/src/utils.js index 34af0ac..b9917c2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); const esprima = require('esprima'); +const normalizeHtmlWhitespace = require('normalize-html-whitespace'); const rtError = require('./RTCodeError'); const RTCodeError = rtError.RTCodeError; @@ -142,13 +143,24 @@ const curlyMap = {'{': 1, '}': -1}; * @typedef {{fileName:string,force:boolean,modules:string,defines:*,reactImportPath:string=,lodashImportPath:string=,flow:boolean,name:string,native:boolean,propTemplates:*,format:string,_:*,version:boolean,help:boolean,listTargetVersion:boolean,modules:string, dryRun:boolean}} Options */ +/** + * @param {string} txt + * @param {boolean} removeWhitespaces + * @return {string} + */ +function jsonText(txt, normalizeWhitespaces) { + const text = normalizeWhitespaces ? normalizeHtmlWhitespace(txt) : txt; + return JSON.stringify(text); +} + /** * @param node * @param {Context} context * @param {string} txt + * @param {boolean} [removeWhitespaces] * @return {string} */ -function convertText(node, context, txt) { +function convertText(node, context, txt, normalizeWhitespaces) { let res = ''; let first = true; const concatChar = node.type === 'text' ? ',' : '+'; @@ -156,7 +168,7 @@ function convertText(node, context, txt) { const start = txt.indexOf('{'); const pre = txt.substr(0, start); if (pre) { - res += (first ? '' : concatChar) + JSON.stringify(pre); + res += (first ? '' : concatChar) + jsonText(pre, normalizeWhitespaces); first = false; } let curlyCounter = 1; @@ -174,7 +186,7 @@ function convertText(node, context, txt) { } } if (txt) { - res += (first ? '' : concatChar) + JSON.stringify(txt); + res += (first ? '' : concatChar) + jsonText(txt, normalizeWhitespaces); } if (res === '') { res = 'true'; diff --git a/test/data/whitespace.rt b/test/data/whitespace.rt index 57fbe1d..318f22c 100644 --- a/test/data/whitespace.rt +++ b/test/data/whitespace.rt @@ -1,23 +1,24 @@
- This is a - {this.props.name} + This is a + {this.props.name} example
 
+
{" aaa "}
-        This   is  a   
-        {this.props.name}  
+        This   is  a
+        {this.props.name}
         example