mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
fixed #52
This commit is contained in:
parent
731f8d8b1f
commit
0e744736ce
@ -18,7 +18,6 @@ var propsTemplate = _.template('mergeProps( <%= generatedProps %>, <%= rtProps %
|
|||||||
var propsMergeFunction = 'function mergeProps(inline,external) {\n var res = _.assign({},inline,external)\nif (inline.hasOwnProperty(\'style\')) {\n res.style = _.defaults(res.style, inline.style);\n}\n' +
|
var propsMergeFunction = 'function mergeProps(inline,external) {\n var res = _.assign({},inline,external)\nif (inline.hasOwnProperty(\'style\')) {\n res.style = _.defaults(res.style, inline.style);\n}\n' +
|
||||||
' if (inline.hasOwnProperty(\'className\') && external.hasOwnProperty(\'className\')) {\n' +
|
' if (inline.hasOwnProperty(\'className\') && external.hasOwnProperty(\'className\')) {\n' +
|
||||||
' res.className = external.className + \' \' + inline.className;\n} return res;\n}\n';
|
' res.className = external.className + \' \' + inline.className;\n} return res;\n}\n';
|
||||||
//var classSetTemplate = _.template('React.addons.classSet(<%= classSet %>)');
|
|
||||||
var classSetTemplate = _.template('_.keys(_.pick(<%= classSet %>, _.identity)).join(" ")');
|
var classSetTemplate = _.template('_.keys(_.pick(<%= classSet %>, _.identity)).join(" ")');
|
||||||
var simpleTagTemplate = _.template('<%= name %>(<%= props %><%= children %>)');
|
var simpleTagTemplate = _.template('<%= name %>(<%= props %><%= children %>)');
|
||||||
var tagTemplate = _.template('<%= name %>.apply(this, [<%= props %><%= children %>])');
|
var tagTemplate = _.template('<%= name %>.apply(this, [<%= props %><%= children %>])');
|
||||||
@ -55,7 +54,7 @@ var classAttr = 'class';
|
|||||||
var scopeAttr = 'rt-scope';
|
var scopeAttr = 'rt-scope';
|
||||||
var propsAttr = 'rt-props';
|
var propsAttr = 'rt-props';
|
||||||
|
|
||||||
var defaultOptions = {modules: 'amd', version: false, force: false, format: 'stylish', targetVersion: '0.13.1'};
|
var defaultOptions = {modules: 'amd', version: false, force: false, format: 'stylish', targetVersion: '0.13.1', reactImportPath: 'react/addons', lodashImportPath: 'lodash'};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Context} context
|
* @param {Context} context
|
||||||
@ -230,7 +229,7 @@ function generateProps(node, context) {
|
|||||||
var existing = props[propKey] ? props[propKey] + ' + " " + ' : '';
|
var existing = props[propKey] ? props[propKey] + ' + " " + ' : '';
|
||||||
if (key === classSetAttr) {
|
if (key === classSetAttr) {
|
||||||
props[propKey] = existing + classSetTemplate({classSet: val});
|
props[propKey] = existing + classSetTemplate({classSet: val});
|
||||||
} else if (key === classAttr) {
|
} else if (key === classAttr || key === classNameProp) {
|
||||||
props[propKey] = existing + convertText(node, context, val.trim());
|
props[propKey] = existing + convertText(node, context, val.trim());
|
||||||
}
|
}
|
||||||
} else if (key.indexOf('rt-') !== 0) {
|
} else if (key.indexOf('rt-') !== 0) {
|
||||||
@ -402,12 +401,6 @@ function convertHtmlToReact(node, context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//function removeDocType(html) {
|
|
||||||
// html = html.replace(/^\s*\<\!doctype\s+rt\s*>/mi, function () {
|
|
||||||
// return '';
|
|
||||||
// });
|
|
||||||
// return html;
|
|
||||||
//}
|
|
||||||
/**
|
/**
|
||||||
* @param node
|
* @param node
|
||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
@ -416,15 +409,10 @@ function isTag(node) {
|
|||||||
return node.type === 'tag';
|
return node.type === 'tag';
|
||||||
}
|
}
|
||||||
|
|
||||||
//function isEmptyText(node) {
|
|
||||||
// return node.type === 'text' && /^\s*$/g.test(node.data);
|
|
||||||
//}
|
|
||||||
|
|
||||||
function handleSelfClosingHtmlTags(nodes) {
|
function handleSelfClosingHtmlTags(nodes) {
|
||||||
return _(nodes)
|
return _(nodes)
|
||||||
.map(function (node) {
|
.map(function (node) {
|
||||||
var externalNodes = [];
|
var externalNodes = [];
|
||||||
//node.children = _.reject(node.children, isEmptyText);
|
|
||||||
node.children = handleSelfClosingHtmlTags(node.children);
|
node.children = handleSelfClosingHtmlTags(node.children);
|
||||||
if (node.type === 'tag' && _.contains(htmlSelfClosingTags, node.name)) {
|
if (node.type === 'tag' && _.contains(htmlSelfClosingTags, node.name)) {
|
||||||
externalNodes = _.filter(node.children, isTag);
|
externalNodes = _.filter(node.children, isTag);
|
||||||
@ -470,11 +458,9 @@ function convertRT(html, reportContext, options) {
|
|||||||
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
|
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
|
||||||
options = _.defaults({}, options, defaultOptions);
|
options = _.defaults({}, options, defaultOptions);
|
||||||
|
|
||||||
var reactPath = options.reactImportPath || 'react/addons';
|
|
||||||
var lodashPath = options.lodashImportPath || 'lodash';
|
|
||||||
var defaultDefines = {};
|
var defaultDefines = {};
|
||||||
defaultDefines[reactPath] = 'React';
|
defaultDefines[options.reactImportPath] = 'React';
|
||||||
defaultDefines[lodashPath] = '_';
|
defaultDefines[options.lodashImportPath] = '_';
|
||||||
|
|
||||||
var defines = options.defines ? _.clone(options.defines) : defaultDefines;
|
var defines = options.defines ? _.clone(options.defines) : defaultDefines;
|
||||||
|
|
||||||
@ -493,11 +479,7 @@ function convertRT(html, reportContext, options) {
|
|||||||
} else if (tag.children.length) {
|
} else if (tag.children.length) {
|
||||||
throw RTCodeError.build('rt-require may have no children', context, tag);
|
throw RTCodeError.build('rt-require may have no children', context, tag);
|
||||||
}
|
}
|
||||||
//if (options.modules === 'typescript') {
|
|
||||||
// defines['./' + tag.attribs.dependency] = tag.attribs.as;
|
|
||||||
//} else {
|
|
||||||
defines[tag.attribs.dependency] = tag.attribs.as;
|
defines[tag.attribs.dependency] = tag.attribs.as;
|
||||||
//}
|
|
||||||
} else if (firstTag === null) {
|
} else if (firstTag === null) {
|
||||||
firstTag = tag;
|
firstTag = tag;
|
||||||
} else {
|
} else {
|
||||||
@ -527,7 +509,6 @@ function convertRT(html, reportContext, options) {
|
|||||||
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
|
tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
|
||||||
code = escodegen.generate(tree, {comment: true});
|
code = escodegen.generate(tree, {comment: true});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//console.log(code);
|
|
||||||
throw new RTCodeError(e.message, e.index, -1);
|
throw new RTCodeError(e.message, e.index, -1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
test/data/className.rt
Normal file
1
test/data/className.rt
Normal file
@ -0,0 +1 @@
|
|||||||
|
<div className="a"></div>
|
1
test/data/className.rt.html
Normal file
1
test/data/className.rt.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
<div class="a"></div>
|
@ -2,4 +2,5 @@
|
|||||||
<div class="f g-{1+2} h-i" rt-class="{'a': true, b: false, c: 1, 'd-e': true}"></div>
|
<div class="f g-{1+2} h-i" rt-class="{'a': true, b: false, c: 1, 'd-e': true}"></div>
|
||||||
Order should not matter
|
Order should not matter
|
||||||
<div rt-class="{'a': true, b: false, c: 1, 'd-e': true}" class="f g-{1+2} h-i"></div>
|
<div rt-class="{'a': true, b: false, c: 1, 'd-e': true}" class="f g-{1+2} h-i"></div>
|
||||||
|
<div className="a"></div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1 +1 @@
|
|||||||
<div><div class="f g-3 h-i a c d-e"></div>Order should not matter<div class="a c d-e f g-3 h-i"></div></div>
|
<div><div class="f g-3 h-i a c d-e"></div>Order should not matter<div class="a c d-e f g-3 h-i"></div><div class="a"></div></div>
|
||||||
|
@ -178,7 +178,8 @@ function normalizeHtml(html) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test('html tests', function (t) {
|
test('html tests', function (t) {
|
||||||
var files = ['scope.rt', 'scope-trailing-semicolon.rt', 'scope-variable-references.rt', 'lambda.rt', 'eval.rt', 'props.rt', 'custom-element.rt', 'style.rt', 'concat.rt', 'js-in-attr.rt', 'props-class.rt', 'rt-class.rt'];
|
var files = ['scope.rt', 'scope-trailing-semicolon.rt', 'scope-variable-references.rt', 'lambda.rt', 'eval.rt', 'props.rt', 'custom-element.rt', 'style.rt', 'concat.rt',
|
||||||
|
'js-in-attr.rt', 'props-class.rt', 'rt-class.rt', 'className.rt'];
|
||||||
t.plan(files.length);
|
t.plan(files.length);
|
||||||
|
|
||||||
files.forEach(check);
|
files.forEach(check);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user