Files
yoga/src/transpile.html

196 lines
5.5 KiB
HTML
Raw Normal View History

<script src="Layout.js"></script>
<style>
textarea {
height: 200px;
width: 800px;
border: 1px solid black;
font-size: 12px;
font-family: monospace;
}
</style>
<h1>layoutCode</h1>
<textarea id="layout_code" onclick="this.select()"></textarea>
<script>
document.getElementById('layout_code').value = computeLayout.layoutNode.toString()
.replace(/\.children\.length/g, '.children_count')
.replace(/layout\[dim/g, 'layout.dimensions[dim')
.replace(/layout\[pos/g, 'layout.position[pos')
.replace(/layout\[leading/g, 'layout.position[leading')
.replace(/style\[dim/g, 'style.dimensions[dim')
2014-04-18 15:48:44 -07:00
.replace(/node.children\[i\]/g, '&node.children[i]')
.replace(/node\./g, 'node->')
.replace(/child\./g, 'child->')
.replace(/var\/\*([^\/]+)\*\//g, '$1')
.replace(/\n /g, '\n');
</script>
2014-04-18 17:15:03 -07:00
<h1>Tests</h1>
<textarea id="test_code" onclick="this.select()"></textarea>
<script>
var currentTest = '';
var allTests = [];
var layoutTestUtils = {
testLayout: function(node, expectedLayout) {
allTests.push({name: currentTest, node: node, expectedLayout: expectedLayout});
},
testRandomLayout: function(node, i) {
}
};
function describe(name, cb) { cb(); }
function it(name, cb) { currentTest = name; cb(); }
</script>
<script src="__tests__/Layout-test.js"></script>
<script>
function printLayout(test) {
var level = 1;
var res = [];
function add(str) {
res.push(indent(level) + str);
}
function indent(level) {
var result = '';
for (var i = 0; i < level; ++i) {
result += ' ';
}
return result;
}
function addEnum(node, js_key, c_key, dict) {
if (js_key in node.style) {
add('node->style' + '.' + c_key + ' = ' + dict[node.style[js_key]] + ';');
}
}
function addFloat(node, js_key, c_key) {
if (js_key in node.style) {
add('node->style' + '.' + c_key + ' = ' + node.style[js_key] + ';');
}
}
function addSpacing(node, spacing) {
addFloat(node, spacing, spacing + '[CSS_LEFT]');
addFloat(node, spacing, spacing + '[CSS_TOP]');
addFloat(node, spacing, spacing + '[CSS_RIGHT]');
addFloat(node, spacing, spacing + '[CSS_BOTTOM]');
addFloat(node, spacing + 'Left', spacing + '[CSS_LEFT]');
addFloat(node, spacing + 'Top', spacing + '[CSS_TOP]');
addFloat(node, spacing + 'Right', spacing + '[CSS_RIGHT]');
addFloat(node, spacing + 'Bottom', spacing + '[CSS_BOTTOM]');
}
2014-04-19 14:26:19 -07:00
add('{');
level++;
2014-04-19 14:35:54 -07:00
// Output the style node
add('css_node_t *root_node = new_css_node();');
2014-04-19 14:26:19 -07:00
add('{');
level++;
add('css_node_t *node = root_node;');
function rec_style(node) {
2014-04-19 12:15:01 -07:00
addEnum(node, 'flexDirection', 'flex_direction', {
'row': 'CSS_FLEX_DIRECTION_ROW',
'column': 'CSS_FLEX_DIRECTION_COLUMN'
});
addEnum(node, 'justifyContent', 'justify_content', {
'flex-start': 'CSS_JUSTIFY_FLEX_START',
'center': 'CSS_JUSTIFY_CENTER',
'flex-end': 'CSS_JUSTIFY_FLEX_END',
'space-between': 'CSS_JUSTIFY_SPACE_BETWEEN',
'space-around': 'CSS_JUSTIFY_SPACE_AROUND'
2014-04-19 12:15:01 -07:00
});
addEnum(node, 'alignItems', 'align_items', {
'flex-start': 'CSS_ALIGN_FLEX_START',
'center': 'CSS_ALIGN_CENTER',
'flex-end': 'CSS_ALIGN_FLEX_END',
'stretch': 'CSS_ALIGN_STRETCH'
});
addEnum(node, 'alignSelf', 'align_self', {
'flex-start': 'CSS_ALIGN_FLEX_START',
'center': 'CSS_ALIGN_CENTER',
'flex-end': 'CSS_ALIGN_FLEX_END',
'stretch': 'CSS_ALIGN_STRETCH'
});
addEnum(node, 'flex', 'flex', {
'none': 'CSS_FLEX_NONE',
'1': 'CSS_FLEX_ONE'
});
addFloat(node, 'width', 'dimensions[CSS_WIDTH]');
addFloat(node, 'height', 'dimensions[CSS_HEIGHT]');
addSpacing(node, 'margin');
addSpacing(node, 'padding');
addFloat(node, 'left', 'position[CSS_LEFT]');
addFloat(node, 'top', 'position[CSS_TOP]');
addFloat(node, 'right', 'position[CSS_RIGHT]');
addFloat(node, 'bottom', 'position[CSS_BOTTOM]');
2014-04-19 12:15:01 -07:00
if (node.children) {
2014-04-18 17:15:03 -07:00
add('init_css_node_children(node, ' + node.children.length + ');');
add('css_node_t *outer_node = node;');
add('{');
level++;
add('css_node_t *node;');
for (var i = 0; i < node.children.length; ++i) {
add('node = &outer_node->children[' + i + '];');
2014-04-19 14:26:19 -07:00
rec_style(node.children[i]);
2014-04-18 17:15:03 -07:00
}
level--;
add('}');
}
}
2014-04-19 14:26:19 -07:00
rec_style(test.node);
level--;
add('}');
add('');
2014-04-18 17:15:03 -07:00
2014-04-19 14:35:54 -07:00
// Output the expected layout node
2014-04-19 14:26:19 -07:00
add('css_node_t *root_layout = new_css_node();');
2014-04-18 17:15:03 -07:00
add('{');
level++;
2014-04-19 14:26:19 -07:00
add('css_node_t *node = root_layout;');
function rec_layout(node) {
add('node->layout.position[CSS_TOP] = ' + node.top + ';');
add('node->layout.position[CSS_LEFT] = ' + node.left + ';');
add('node->layout.dimensions[CSS_WIDTH] = ' + node.width + ';');
add('node->layout.dimensions[CSS_HEIGHT] = ' + node.height + ';');
if (node.children) {
add('init_css_node_children(node, ' + node.children.length + ');');
add('css_node_t *outer_node = node;');
add('{');
level++;
add('css_node_t *node;');
for (var i = 0; i < node.children.length; ++i) {
add('node = &outer_node->children[' + i + '];');
rec_layout(node.children[i]);
}
level--;
add('}');
}
}
rec_layout(test.expectedLayout);
level--;
add('}');
2014-04-19 14:35:54 -07:00
add('');
2014-04-19 14:26:19 -07:00
2014-04-19 14:35:54 -07:00
// Do the test
add('test("' + test.name.replace(/"/g, '\\"') + '", root_node, root_layout);');
2014-04-18 17:15:03 -07:00
level--;
add('}');
return res.join('\n');
}
document.getElementById('test_code').value = allTests.map(printLayout).join('\n\n');
</script>