mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
Do not normalize whitespaces within { } expressions
This commit is contained in:
parent
a6be6ffc9c
commit
6af5715ea1
@ -3,7 +3,6 @@ const cheerio = require('cheerio');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const esprima = require('esprima');
|
const esprima = require('esprima');
|
||||||
const escodegen = require('escodegen');
|
const escodegen = require('escodegen');
|
||||||
const normalizeHtmlWhitespace = require('normalize-html-whitespace');
|
|
||||||
const reactDOMSupport = require('./reactDOMSupport');
|
const reactDOMSupport = require('./reactDOMSupport');
|
||||||
const reactNativeSupport = require('./reactNativeSupport');
|
const reactNativeSupport = require('./reactNativeSupport');
|
||||||
const reactPropTemplates = require('./reactPropTemplates');
|
const reactPropTemplates = require('./reactPropTemplates');
|
||||||
@ -440,15 +439,11 @@ function convertHtmlToReact(node, context) {
|
|||||||
const sanitizedComment = node.data.split('*/').join('* /');
|
const sanitizedComment = node.data.split('*/').join('* /');
|
||||||
return commentTemplate({data: sanitizedComment});
|
return commentTemplate({data: sanitizedComment});
|
||||||
} else if (node.type === 'text') {
|
} else if (node.type === 'text') {
|
||||||
let text = node.data;
|
|
||||||
const parentNode = node.parent;
|
const parentNode = node.parent;
|
||||||
if (parentNode !== undefined) {
|
const overrideNormalize = parentNode !== undefined && (parentNode.name === 'pre' || parentNode.name === 'textarea' || _.has(parentNode.attribs, preAttr));
|
||||||
const preserveWhitespaces = parentNode.name === 'pre' || parentNode.name === 'textarea' || _.has(parentNode.attribs, preAttr);
|
const normalizeWhitespaces = context.options.normalizeHtmlWhitespace && !overrideNormalize;
|
||||||
if (context.options.normalizeHtmlWhitespace && !preserveWhitespaces) {
|
const text = node.data;
|
||||||
text = normalizeHtmlWhitespace(text);
|
return trimHtmlText(text) ? utils.convertText(node, context, text, normalizeWhitespaces) : '';
|
||||||
}
|
|
||||||
}
|
|
||||||
return trimHtmlText(text) ? utils.convertText(node, context, text) : '';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
src/utils.js
18
src/utils.js
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const esprima = require('esprima');
|
const esprima = require('esprima');
|
||||||
|
const normalizeHtmlWhitespace = require('normalize-html-whitespace');
|
||||||
const rtError = require('./RTCodeError');
|
const rtError = require('./RTCodeError');
|
||||||
const RTCodeError = rtError.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
|
* @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 node
|
||||||
* @param {Context} context
|
* @param {Context} context
|
||||||
* @param {string} txt
|
* @param {string} txt
|
||||||
|
* @param {boolean} [removeWhitespaces]
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
function convertText(node, context, txt) {
|
function convertText(node, context, txt, normalizeWhitespaces) {
|
||||||
let res = '';
|
let res = '';
|
||||||
let first = true;
|
let first = true;
|
||||||
const concatChar = node.type === 'text' ? ',' : '+';
|
const concatChar = node.type === 'text' ? ',' : '+';
|
||||||
@ -156,7 +168,7 @@ function convertText(node, context, txt) {
|
|||||||
const start = txt.indexOf('{');
|
const start = txt.indexOf('{');
|
||||||
const pre = txt.substr(0, start);
|
const pre = txt.substr(0, start);
|
||||||
if (pre) {
|
if (pre) {
|
||||||
res += (first ? '' : concatChar) + JSON.stringify(pre);
|
res += (first ? '' : concatChar) + jsonText(pre, normalizeWhitespaces);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
let curlyCounter = 1;
|
let curlyCounter = 1;
|
||||||
@ -174,7 +186,7 @@ function convertText(node, context, txt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (txt) {
|
if (txt) {
|
||||||
res += (first ? '' : concatChar) + JSON.stringify(txt);
|
res += (first ? '' : concatChar) + jsonText(txt, normalizeWhitespaces);
|
||||||
}
|
}
|
||||||
if (res === '') {
|
if (res === '') {
|
||||||
res = 'true';
|
res = 'true';
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
example
|
example
|
||||||
</div>
|
</div>
|
||||||
<div>   </div>
|
<div>   </div>
|
||||||
|
<div>{" aaa "}</div>
|
||||||
<pre>
|
<pre>
|
||||||
This is a
|
This is a
|
||||||
{this.props.name}
|
{this.props.name}
|
||||||
|
@ -4,6 +4,6 @@ define([
|
|||||||
], function (React, _) {
|
], function (React, _) {
|
||||||
'use strict';
|
'use strict';
|
||||||
return function () {
|
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'))));
|
||||||
};
|
};
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user