From 3ba52d1653ee2a1987ed4cd14b15a86686e2410a Mon Sep 17 00:00:00 2001 From: Avi Marcus Date: Mon, 12 Jan 2015 20:08:30 +0200 Subject: [PATCH] replace htmlMode with hard wired list of self closing tags... caused weird problems with rt-require... --- src/reactTemplates.js | 24 +++++++++++++++++++++++- test/data/require.rt | 5 +++++ test/data/require.rt.js | 11 +++++++++++ test/src/test.js | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/data/require.rt create mode 100644 test/data/require.rt.js diff --git a/src/reactTemplates.js b/src/reactTemplates.js index d6632d8..ae36ada 100644 --- a/src/reactTemplates.js +++ b/src/reactTemplates.js @@ -24,6 +24,7 @@ var templatePJSTemplate = _.template('var <%= name %> = function () {\n' + '<%= injectedFunctions %>\n' + 'return <%= body %>\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 ifProp = 'rt-if'; @@ -341,7 +342,27 @@ function convertHtmlToReact(node, context) { // }); // 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 @@ -349,11 +370,12 @@ function convertHtmlToReact(node, context) { * @return {string} */ 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); var defines = {'react/addons': 'React', lodash: '_'}; var context = defaultContext(html, options); var rootTags = _.filter(rootNode.root()[0].children, function (i) { return i.type === 'tag'; }); + rootTags = handleSelfClosingHtmlTags(rootTags); if (!rootTags || rootTags.length === 0) { throw new RTCodeError('Document should have a root element'); } diff --git a/test/data/require.rt b/test/data/require.rt new file mode 100644 index 0000000..b5a39b9 --- /dev/null +++ b/test/data/require.rt @@ -0,0 +1,5 @@ + + + +{utils.translate('Hello','es')} + diff --git a/test/data/require.rt.js b/test/data/require.rt.js new file mode 100644 index 0000000..799d640 --- /dev/null +++ b/test/data/require.rt.js @@ -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'); + }; +}); \ No newline at end of file diff --git a/test/src/test.js b/test/src/test.js index e8bbba1..b2535a6 100644 --- a/test/src/test.js +++ b/test/src/test.js @@ -82,7 +82,7 @@ function errorEqual(err) { } 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); files.forEach(check);