Added --normalize-html-whitespace CLI option

This commit is contained in:
Antonino Porcino 2016-07-08 14:06:47 +02:00
parent d80de30f62
commit 6348e53306
3 changed files with 17 additions and 1 deletions

View File

@ -36,6 +36,7 @@
"escodegen": "^1.8.0",
"esprima": "^2.7.1",
"lodash": "^4.11.1",
"normalize-html-whitespace": "^0.2.0",
"optionator": "^0.8.0",
"text-table": "^0.2.0"
},

View File

@ -116,5 +116,10 @@ $ rt <filename> [<filename> ...] [<args>]`,
enum: Object.keys(reactNativeSupport),
default: reactNativeSupport.default,
description: `React native version to generate code for (${Object.keys(reactNativeSupport).join(', ')})`
}, {
option: 'normalize-html-whitespace',
type: 'Boolean',
default: 'false',
description: 'Remove repeating whitespace from HTML text.'
}]
});

View File

@ -3,6 +3,7 @@ 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');
@ -55,6 +56,7 @@ const includeSrcAttr = 'src';
const requireAttr = 'rt-require';
const importAttr = 'rt-import';
const statelessAttr = 'rt-stateless';
const preAttr = 'rt-pre';
const reactTemplatesSelfClosingTags = [includeNode];
@ -438,7 +440,15 @@ function convertHtmlToReact(node, context) {
const sanitizedComment = node.data.split('*/').join('* /');
return commentTemplate({data: sanitizedComment});
} else if (node.type === 'text') {
return trimHtmlText(node.data) ? utils.convertText(node, context, node.data) : '';
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) : '';
}
}