1
0
mirror of https://github.com/bobwen-dev/react-templates synced 2025-04-12 00:56:39 +02:00

moved require to rt-require tag instead of inside of doctype and removed doctype completely

This commit is contained in:
Avi Marcus 2014-12-11 09:30:27 +02:00
parent d60af285c3
commit 8fb5fb3674
20 changed files with 15134 additions and 15131 deletions

View File

@ -1,4 +1,4 @@
<!doctype rt playground="./playground">
<rt-require dependency="./playground" as="playground"/>
<div id="examples">
<div class="example">
<h3>Hello world in react templates</h3>
@ -42,4 +42,4 @@
</p>
<playground id="weatherExample" rt-props="this.state.samples.weather" direction="horizontal"></playground>
</div>
</div>
</div>

View File

@ -1,4 +1,4 @@
<!DOCTYPE rt playground="./playground">
<rt-require dependency="./playground" as="playground"/>
<div class="fiddle">
<div id="header">
<div id="header-title">
@ -41,4 +41,4 @@
<playground ref="playground" direction="vertical" fiddle="{true}" />
</div>
<!--</div>-->
</div>
</div>

View File

@ -1,4 +1,4 @@
<!DOCTYPE rt playGround="./PlayGround.js">
<rt-require dependency="./PlayGround.js" as="playGround"/>
<div>
<h1>React Templates</h1>
<playGround ref="playground" direction="horizontal" rt-props="this.state.samples[0]"/>

View File

@ -1,4 +1,4 @@
<!DOCTYPE rt CodeEditor="./CodeMirrorEditor">
<rt-require dependency="./CodeMirrorEditor" as="CodeEditor"/>
<!--suppress ALL -->
<div class="playground">
<div class="fiddle-row">

View File

@ -1,4 +1,4 @@
<!DOCTYPE rt CodeEditor="./aceEditor">
<rt-require dependency="./aceEditor" as="CodeEditor"/>
<div class="playground">
<div id="{this.props.id}-myTab" role="tabpanel" class="code-area {(this.props.direction === 'horizontal' && 'horizontal') ||'vertical'}">
<!-- Nav tabs -->

File diff suppressed because one or more lines are too long

View File

@ -25,9 +25,6 @@ function convertFile(source, target, options, context) {
}
var html = fs.readFileSync(source).toString();
if (!html.match(/\<\!doctype rt/i)) {
throw new Error('invalid file, missing header');
}
var js = convertTemplateToReact(html, options);
if (!options.dryRun) {
fs.writeFileSync(target, js);
@ -39,4 +36,4 @@ module.exports = {
convertFile: convertFile,
context: require('./context'),
_test: {}
};
};

View File

@ -330,23 +330,14 @@ function convertHtmlToReact(node, context) {
}
}
function extractDefinesFromJSXTag(html, defines) {
html = html.replace(/\<\!doctype rt\s*(.*?)\s*\>/i, function (full, reqStr) {
var match = true;
while (match) {
match = false;
/*eslint no-loop-func:0*/
reqStr = reqStr.replace(/\s*(\w+)\s*\=\s*\"([^\"]*)\"\s*/, function (full, varName, reqPath) {
defines[reqPath] = varName;
match = true;
return '';
});
}
return '';
});
return html;
function removeDocType(html) {
html = html.replace(/^\s*\<\!doctype\s+rt\s*>/mi, function () {
return "";
});
return html;
}
/**
* @param {string} html
* @param {{commonJS:boolean}?} options
@ -356,16 +347,30 @@ function convertTemplateToReact(html, options) {
var rootNode = cheerio.load(html, {lowerCaseTags: false, lowerCaseAttributeNames: false, xmlMode: true, withStartIndices: true});
options = _.defaults({}, options, defaultOptions);
var defines = {'react/addons': 'React', lodash: '_'};
html = extractDefinesFromJSXTag(html, defines);
var context = defaultContext(html, options);
var rootTags = _.filter(rootNode.root()[0].children, function (i) { return i.type === 'tag'; });
if (!rootTags || rootTags.length === 0) {
throw new RTCodeError('Document should have a root element');
}
if (rootTags.length > 1) {
throw buildError('Document should have a single root element', context, rootTags[1]);
var firstTag = null;
_.forEach(rootTags, function(tag) {
if (tag.name == 'rt-require') {
if (!tag.attribs['dependency'] || !tag.attribs['as']) {
throw buildError('rt-require needs \'dependency\' and \'as\' attributes', context, tag);
} else if (tag.children.length) {
throw buildError('rt-require may have no children', context, tag);
} else {
defines[tag.attribs['dependency']] = tag.attribs['as'];
}
} else if (firstTag === null) {
firstTag = tag;
} else {
throw buildError('Document should have no more than a single root element', context, tag);
}
});
if (firstTag === null) {
throw buildError('Document should have a single root element', context, rootNode.root()[0]);
}
var firstTag = rootTags[0];
var body = convertHtmlToReact(firstTag, context);
var requirePaths = _(defines).keys().map(function (reqName) { return '"' + reqName + '"'; }).value().join(',');
var requireVars = _(defines).values().value().join(',');

View File

@ -1,3 +1,2 @@
<!doctype rt>
<div>
</div>
</div>

View File

@ -1,4 +1,3 @@
<!doctype rt>
<div>
Strings and execution mixing is:{2>1?'correct':'incorrect'}
</div>
</div>

View File

@ -1,4 +1,3 @@
<!doctype rt>
<div>
{z
</div>
</div>

View File

@ -1,2 +1 @@
<!doctype rt>
<div
<div

View File

@ -1,3 +1,2 @@
<!doctype rt>
<div onClick="">
</div>
</div>

View File

@ -1,3 +1,2 @@
<!doctype rt>
<div rt-scope="a in a in a">
</div>
</div>

View File

@ -1,6 +1,5 @@
<!DOCTYPE rt>
<div rt-scope="{value:'event did not happen because onClick not called'} as data"
onMouseDown="(evt) => data.value = 'event did happen though it should not'"
onClick="() => data.value = 'event did happen though it should not'">
{data.value}
</div>
</div>

View File

@ -1,2 +1 @@
<!doctype rt>
<input rt-scope="'test' as i" alt="test rt-props" style="height:10px;width:3px;" rt-props="{style:{width:'5px'},type:'text',value:i}"/>
<input rt-scope="'test' as i" alt="test rt-props" style="height:10px;width:3px;" rt-props="{style:{width:'5px'},type:'text',value:i}"/>

View File

@ -1,4 +1,3 @@
<!DOCTYPE rt>
<p>
<div rt-repeat="items in this.props.things">
<span style="width:auto;line-height: 5px;"

View File

@ -1,4 +1,3 @@
<!DOCTYPE rt>
<div>
<div rt-scope="['a','b','c'] as items">
<span rt-repeat="item in items">Item:#{itemIndex} = {item}</span>
@ -6,4 +5,4 @@
<span>{typeof (items) == 'undefined'?'items not in scope':'items in scope'}</span>
<span>{typeof (item) == 'undefined'?'item not in scope':'item in scope'}</span>
</div>
</div>

View File

@ -1,5 +1,3 @@
<!doctype rt>
<div>
<div style= "position: relative; textAlign: center;
top: {this.props.config.previewTop};
@ -13,4 +11,4 @@
<div>editor
<div rt-if="!this.props.editorState.previewMode">left bar</div>
</div>
</div>
</div>

View File

@ -11,10 +11,10 @@ var dataPath = path.resolve(__dirname, '..', 'data');
test('invalid tests', function (t) {
var files = [
{file: 'invalid-scope.rt', issue: new reactTemplates.RTCodeError("invalid scope part 'a in a in a'", 14, 1)},
{file: 'invalid-scope.rt', issue: new reactTemplates.RTCodeError("invalid scope part 'a in a in a'", -1, -1)},
{file: 'invalid-html.rt', issue: new reactTemplates.RTCodeError('Document should have a root element', -1, -1)},
{file: 'invalid-exp.rt', issue: new reactTemplates.RTCodeError("Failed to parse text '\n {z\n'", 19, 3)},
{file: 'invalid-lambda.rt', issue: new reactTemplates.RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [onClick='']", 14, 1)}
{file: 'invalid-exp.rt', issue: new reactTemplates.RTCodeError("Failed to parse text '\n {z\n'", 5, -1)},
{file: 'invalid-lambda.rt', issue: new reactTemplates.RTCodeError("when using 'on' events, use lambda '(p1,p2)=>body' notation or use {} to return a callback function. error: [onClick='']", -1, -1)}
];
t.plan(files.length);