Do not normalize whitespaces within { } expressions

This commit is contained in:
Antonino Porcino 2016-07-09 10:19:19 +02:00
parent a6be6ffc9c
commit 6af5715ea1
4 changed files with 29 additions and 21 deletions

View File

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

View File

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

View File

@ -1,23 +1,24 @@
<div>
<div>
This is a
{this.props.name}
This is a
{this.props.name}
example
</div>
<div> &#160; </div>
<div>{" aaa "}</div>
<pre>
This is a
{this.props.name}
This is a
{this.props.name}
example
</pre>
<textarea>
This is a
{this.props.name}
This is a
{this.props.name}
example
<textarea>
<div rt-pre>
This is a
{this.props.name}
This is a
{this.props.name}
example
</pre>
</div>

View File

@ -4,6 +4,6 @@ define([
], function (React, _) {
'use strict';
return function () {
return React.createElement('div', {}, React.createElement('div', {}, ' This is a ', this.props.name, ' example '), React.createElement('div', {}, ' \xA0 '), React.createElement('pre', {}, '\n This is a \n ', this.props.name, ' \n example\n '), React.createElement('textarea', {}, '\n This is a \n ', this.props.name, ' \n example\n ', React.createElement('textarea', {}, React.createElement('div', {}, '\n This is a \n ', this.props.name, ' \n example\n \n'))));
return React.createElement('div', {}, React.createElement('div', {}, ' This is a ', this.props.name, ' example '), React.createElement('div', {}, ' \xA0 '), React.createElement('div', {}, ' aaa '), React.createElement('pre', {}, '\n This is a\n ', this.props.name, '\n example\n '), React.createElement('textarea', {}, '\n This is a\n ', this.props.name, '\n example\n ', React.createElement('textarea', {}, React.createElement('div', {}, '\n This is a\n ', this.props.name, '\n example\n \n'))));
};
});