resolve merge conflicts

This commit is contained in:
daviskoh
2015-02-17 21:30:41 -05:00
9 changed files with 139 additions and 38 deletions

View File

@@ -15,7 +15,8 @@
"__dirname": true, "__dirname": true,
"module": true, "module": true,
"console": true, "console": true,
"setTimeout": true "setTimeout": true,
"define": true
}, },
"rules": { "rules": {
"quotes": "single", "quotes": "single",

View File

@@ -2,7 +2,7 @@
"name": "css-layout", "name": "css-layout",
"version": "0.0.1", "version": "0.0.1",
"description": "Reimplementation of CSS layout using pure JavaScript", "description": "Reimplementation of CSS layout using pure JavaScript",
"main": "src/Layout.js", "main": "src/main.js",
"scripts": { "scripts": {
"pretest": "./node_modules/eslint/bin/eslint.js src", "pretest": "./node_modules/eslint/bin/eslint.js src",
"test": "./node_modules/karma/bin/karma start ./karma.conf.js --single-run" "test": "./node_modules/karma/bin/karma start ./karma.conf.js --single-run"

View File

@@ -100,8 +100,10 @@ var layoutTestUtils = (function() {
getIframe(iframe); getIframe(iframe);
} }
if (typeof computeLayout === 'function') { if (typeof computeLayout === 'object') {
var realComputeLayout = computeLayout; var fillNodes = computeLayout.fillNodes;
var extractNodes = computeLayout.extractNodes;
var realComputeLayout = computeLayout.computeLayout;
} }
function roundLayout(layout) { function roundLayout(layout) {
@@ -143,34 +145,6 @@ var layoutTestUtils = (function() {
} }
function computeCSSLayout(rootNode) { function computeCSSLayout(rootNode) {
function fillNodes(node) {
node.layout = {
width: undefined,
height: undefined,
top: 0,
left: 0
};
if (!node.style) {
node.style = {};
}
if (!node.children || node.style.measure) {
node.children = [];
}
node.children.forEach(fillNodes);
}
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
return layout;
}
fillNodes(rootNode); fillNodes(rootNode);
realComputeLayout(rootNode); realComputeLayout(rootNode);
return roundLayout(extractNodes(rootNode)); return roundLayout(extractNodes(rootNode));
@@ -257,6 +231,14 @@ var layoutTestUtils = (function() {
return namedLayout; return namedLayout;
} }
function testFillNodes(node, filledNode) {
expect(fillNodes(node)).toEqual(filledNode);
}
function testExtractNodes(node, extractedNode) {
expect(extractNodes(node)).toEqual(extractedNode);
}
function testNamedLayout(name, layoutA, layoutB) { function testNamedLayout(name, layoutA, layoutB) {
expect(nameLayout(name, layoutA)) expect(nameLayout(name, layoutA))
.toEqual(nameLayout(name, layoutB)); .toEqual(nameLayout(name, layoutB));
@@ -404,6 +386,8 @@ var layoutTestUtils = (function() {
testNamedLayout('expected-dom', expectedLayout, domLayout); testNamedLayout('expected-dom', expectedLayout, domLayout);
testNamedLayout('layout-dom', layout, domLayout); testNamedLayout('layout-dom', layout, domLayout);
}, },
testFillNodes: testFillNodes,
testExtractNodes: testExtractNodes,
testRandomLayout: function(node) { testRandomLayout: function(node) {
expect({node: node, layout: computeCSSLayout(node)}) expect({node: node, layout: computeCSSLayout(node)})
.toEqual({node: node, layout: computeDOMLayout(node)}); .toEqual({node: node, layout: computeDOMLayout(node)});

View File

@@ -336,6 +336,7 @@ static float getRelativePosition(css_node_t *node, css_flex_direction_t axis) {
static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) { static void layoutNodeImpl(css_node_t *node, float parentMaxWidth) {
/** START_GENERATED **/ /** START_GENERATED **/
css_flex_direction_t mainAxis = getFlexDirection(node); css_flex_direction_t mainAxis = getFlexDirection(node);
css_flex_direction_t crossAxis = mainAxis == CSS_FLEX_DIRECTION_ROW ? css_flex_direction_t crossAxis = mainAxis == CSS_FLEX_DIRECTION_ROW ?
CSS_FLEX_DIRECTION_COLUMN : CSS_FLEX_DIRECTION_COLUMN :

View File

@@ -62,6 +62,34 @@ var computeLayout = (function() {
return 0; return 0;
} }
function fillNodes(node) {
node.layout = {
width: undefined,
height: undefined,
top: 0,
left: 0
};
if (!node.style) {
node.style = {};
}
if (!node.children || node.style.measure) {
node.children = [];
}
node.children.forEach(fillNodes);
return node;
}
function extractNodes(node) {
var layout = node.layout;
delete node.layout;
if (node.children && node.children.length > 0) {
layout.children = node.children.map(extractNodes);
} else {
delete node.children;
}
return layout;
}
function getPositiveSpacing(node, type, suffix, location) { function getPositiveSpacing(node, type, suffix, location) {
var key = type + capitalizeFirst(location) + suffix; var key = type + capitalizeFirst(location) + suffix;
@@ -208,7 +236,8 @@ var computeLayout = (function() {
return -getPosition(node, trailing[axis]); return -getPosition(node, trailing[axis]);
} }
return function layoutNode(node, parentMaxWidth) { function layoutNode(node, parentMaxWidth) {
var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node); var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node);
var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ? var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ?
CSS_FLEX_DIRECTION_COLUMN : CSS_FLEX_DIRECTION_COLUMN :
@@ -651,9 +680,30 @@ var computeLayout = (function() {
} }
} }
} }
}
return {
computeLayout: layoutNode,
fillNodes: fillNodes,
extractNodes: extractNodes
}; };
})(); })();
if (typeof module === 'object') { // UMD (Universal Module Definition)
module.exports = computeLayout; // See https://github.com/umdjs/umd for reference
} (function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
return computeLayout;
}));

View File

@@ -9,10 +9,46 @@
/* globals layoutTestUtils */ /* globals layoutTestUtils */
var testLayout = layoutTestUtils.testLayout; var testLayout = layoutTestUtils.testLayout;
var testFillNodes = layoutTestUtils.testFillNodes;
var testExtractNodes = layoutTestUtils.testExtractNodes;
var text = layoutTestUtils.text; var text = layoutTestUtils.text;
var texts = layoutTestUtils.texts; var texts = layoutTestUtils.texts;
var textSizes = layoutTestUtils.textSizes; var textSizes = layoutTestUtils.textSizes;
describe('Javascript Only', function() {
it('should fill root node with layout, style, and children', function() {
testFillNodes(
{},
{layout: {width: undefined, height: undefined, top: 0, left: 0}, style: {}, children: []}
);
});
it('should fill root and child node with layout, style, and children', function() {
testFillNodes(
{children: [{}]},
{layout: {width: undefined, height: undefined, top: 0, left: 0}, style: {}, children: [
{layout: {width: undefined, height: undefined, top: 0, left: 0}, style: {}, children: []}
]}
);
});
it('should pull out just the layout object from root', function() {
testExtractNodes(
{layout: {width: undefined, height: undefined, top: 0, left: 0}},
{width: undefined, height: undefined, top: 0, left: 0}
);
});
it('should pull out just the layout object from root and children', function() {
testExtractNodes(
{layout: {width: undefined, height: undefined, top: 0, left: 0}, children: [
{layout: {width: undefined, height: undefined, top: 0, left: 0}}
]},
{width: undefined, height: undefined, top: 0, left: 0, children: [
{width: undefined, height: undefined, top: 0, left: 0}
]}
);
});
});
describe('Layout', function() { describe('Layout', function() {
it('should layout a single node with width and height', function() { it('should layout a single node with width and height', function() {
testLayout({ testLayout({

View File

@@ -283,6 +283,7 @@ public class LayoutEngine {
/** START_GENERATED **/ /** START_GENERATED **/
CSSFlexDirection mainAxis = getFlexDirection(node); CSSFlexDirection mainAxis = getFlexDirection(node);
CSSFlexDirection crossAxis = mainAxis == CSSFlexDirection.ROW ? CSSFlexDirection crossAxis = mainAxis == CSSFlexDirection.ROW ?
CSSFlexDirection.COLUMN : CSSFlexDirection.COLUMN :

24
src/main.js Normal file
View File

@@ -0,0 +1,24 @@
// UMD (Universal Module Definition)
// See https://github.com/umdjs/umd for reference
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {
var layout = require('./Layout.js');
return function(node) {
node = layout.fillNodes(node);
layout.computeLayout(node);
node = layout.extractNodes(node);
return node;
};
}));

View File

@@ -8,7 +8,7 @@
*/ */
var layoutTestUtils = require('./Layout-test-utils.js'); var layoutTestUtils = require('./Layout-test-utils.js');
var computeLayout = require('./Layout.js'); var computeLayout = require('./Layout.js').computeLayout;
var fs = require('fs'); var fs = require('fs');
var JavaTranspiler = require('./JavaTranspiler.js'); var JavaTranspiler = require('./JavaTranspiler.js');
@@ -30,7 +30,11 @@ global.layoutTestUtils = {
textSizes: layoutTestUtils.textSizes textSizes: layoutTestUtils.textSizes
}; };
global.describe = function(name, cb) { cb(); }; global.describe = function(name, cb) {
if (name === 'Layout') {
cb();
}
};
global.it = function(name, cb) { currentTest = name; cb(); }; global.it = function(name, cb) { currentTest = name; cb(); };
global.xit = function() { /* ignore skipped tests */ }; global.xit = function() { /* ignore skipped tests */ };