mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
replace htmlMode with hard wired list of self closing tags... caused weird problems with rt-require...
This commit is contained in:
parent
f515c17a67
commit
3ba52d1653
@ -24,6 +24,7 @@ var templatePJSTemplate = _.template('var <%= name %> = function () {\n' +
|
|||||||
'<%= injectedFunctions %>\n' +
|
'<%= injectedFunctions %>\n' +
|
||||||
'return <%= body %>\n' +
|
'return <%= body %>\n' +
|
||||||
'};\n');
|
'};\n');
|
||||||
|
var htmlSelfClosingTags = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"];
|
||||||
|
|
||||||
var templateProp = 'rt-repeat';
|
var templateProp = 'rt-repeat';
|
||||||
var ifProp = 'rt-if';
|
var ifProp = 'rt-if';
|
||||||
@ -341,7 +342,27 @@ function convertHtmlToReact(node, context) {
|
|||||||
// });
|
// });
|
||||||
// return html;
|
// return html;
|
||||||
//}
|
//}
|
||||||
|
function isTag(node) {
|
||||||
|
return node.type === 'tag';
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSelfClosingHtmlTags(nodes) {
|
||||||
|
return _(nodes)
|
||||||
|
.map(function (node) {
|
||||||
|
var externalNodes = [];
|
||||||
|
node.children = handleSelfClosingHtmlTags(node.children);
|
||||||
|
if (node.type === 'tag' && _.contains(htmlSelfClosingTags, node.name)) {
|
||||||
|
externalNodes = _.filter(node.children, isTag);
|
||||||
|
_.forEach(externalNodes, function (child) {
|
||||||
|
child.parent = node;
|
||||||
|
});
|
||||||
|
node.children = _.reject(node.children, isTag);
|
||||||
|
}
|
||||||
|
return [node].concat(externalNodes);
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
.value();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} html
|
* @param {string} html
|
||||||
@ -349,11 +370,12 @@ function convertHtmlToReact(node, context) {
|
|||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
function convertTemplateToReact(html, options) {
|
function convertTemplateToReact(html, options) {
|
||||||
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: false, withStartIndices: true});
|
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
|
||||||
options = _.defaults({}, options, defaultOptions);
|
options = _.defaults({}, options, defaultOptions);
|
||||||
var defines = {'react/addons': 'React', lodash: '_'};
|
var defines = {'react/addons': 'React', lodash: '_'};
|
||||||
var context = defaultContext(html, options);
|
var context = defaultContext(html, options);
|
||||||
var rootTags = _.filter(rootNode.root()[0].children, function (i) { return i.type === 'tag'; });
|
var rootTags = _.filter(rootNode.root()[0].children, function (i) { return i.type === 'tag'; });
|
||||||
|
rootTags = handleSelfClosingHtmlTags(rootTags);
|
||||||
if (!rootTags || rootTags.length === 0) {
|
if (!rootTags || rootTags.length === 0) {
|
||||||
throw new RTCodeError('Document should have a root element');
|
throw new RTCodeError('Document should have a root element');
|
||||||
}
|
}
|
||||||
|
5
test/data/require.rt
Normal file
5
test/data/require.rt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<rt-require dependency="comps/myComp" as="myComp"/>
|
||||||
|
<rt-require dependency="utils/utils" as="utils"/>
|
||||||
|
<myComp>
|
||||||
|
{utils.translate('Hello','es')}
|
||||||
|
</myComp>
|
11
test/data/require.rt.js
Normal file
11
test/data/require.rt.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
define([
|
||||||
|
'react/addons',
|
||||||
|
'lodash',
|
||||||
|
'comps/myComp',
|
||||||
|
'utils/utils'
|
||||||
|
], function (React, _, myComp, utils) {
|
||||||
|
'use strict';
|
||||||
|
return function () {
|
||||||
|
return React.createElement(myComp, {}, '\n' + utils.translate('Hello', 'es') + '\n');
|
||||||
|
};
|
||||||
|
});
|
@ -82,7 +82,7 @@ function errorEqual(err) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test('conversion test', function (t) {
|
test('conversion test', function (t) {
|
||||||
var files = ['div.rt', 'test.rt', 'repeat.rt', 'inputs.rt'];
|
var files = ['div.rt', 'test.rt', 'repeat.rt', 'inputs.rt','require.rt'];
|
||||||
t.plan(files.length);
|
t.plan(files.length);
|
||||||
|
|
||||||
files.forEach(check);
|
files.forEach(check);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user