Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework javascript api to match README #50

Merged
merged 1 commit into from
Feb 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "css-layout",
"version": "0.0.1",
"description": "Reimplementation of CSS layout using pure JavaScript",
"main": "src/Layout.js",
"main": "src/main.js",
"scripts": {
"test": "./node_modules/karma/bin/karma start ./karma.conf.js --single-run"
},
Expand Down
44 changes: 14 additions & 30 deletions src/Layout-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ var layoutTestUtils = (function() {
getIframe();
}

if (typeof computeLayout === 'function') {
var realComputeLayout = computeLayout;
if (typeof computeLayout === 'object') {
var fillNodes = computeLayout.fillNodes;
var extractNodes = computeLayout.extractNodes;
var realComputeLayout = computeLayout.computeLayout;
}

function roundLayout(layout) {
Expand Down Expand Up @@ -142,34 +144,6 @@ var layoutTestUtils = (function() {
}

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);
realComputeLayout(rootNode);
return roundLayout(extractNodes(rootNode));
Expand Down Expand Up @@ -256,6 +230,14 @@ var layoutTestUtils = (function() {
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) {
expect(nameLayout(name, layoutA))
.toEqual(nameLayout(name, layoutB));
Expand Down Expand Up @@ -400,6 +382,8 @@ var layoutTestUtils = (function() {
testNamedLayout('expected-dom', expectedLayout, domLayout);
testNamedLayout('layout-dom', layout, domLayout);
},
testFillNodes: testFillNodes,
testExtractNodes: testExtractNodes,
testRandomLayout: function(node, i) {
expect({node: node, layout: computeCSSLayout(node)})
.toEqual({node: node, layout: computeDOMLayout(node)});
Expand Down
77 changes: 56 additions & 21 deletions src/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/


var computeLayout = (function() {
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
Expand All @@ -25,6 +26,34 @@ var computeLayout = (function() {

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) {
var key = type + capitalizeFirst(location) + suffix;
Expand Down Expand Up @@ -188,26 +217,7 @@ var computeLayout = (function() {
return b;
}

var CSS_UNDEFINED = undefined;

var CSS_FLEX_DIRECTION_ROW = 'row';
var CSS_FLEX_DIRECTION_COLUMN = 'column';

var CSS_JUSTIFY_FLEX_START = 'flex-start';
var CSS_JUSTIFY_CENTER = 'center';
var CSS_JUSTIFY_FLEX_END = 'flex-end';
var CSS_JUSTIFY_SPACE_BETWEEN = 'space-between';
var CSS_JUSTIFY_SPACE_AROUND = 'space-around';

var CSS_ALIGN_FLEX_START = 'flex-start';
var CSS_ALIGN_CENTER = 'center';
var CSS_ALIGN_FLEX_END = 'flex-end';
var CSS_ALIGN_STRETCH = 'stretch';

var CSS_POSITION_RELATIVE = 'relative';
var CSS_POSITION_ABSOLUTE = 'absolute';

return function layoutNode(node, parentMaxWidth) {
function layoutNode(node, parentMaxWidth) {
var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node);
var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ?
CSS_FLEX_DIRECTION_COLUMN :
Expand Down Expand Up @@ -649,7 +659,32 @@ var computeLayout = (function() {
}
}
}
};
}

var CSS_UNDEFINED = undefined;

var CSS_FLEX_DIRECTION_ROW = 'row';
var CSS_FLEX_DIRECTION_COLUMN = 'column';

var CSS_JUSTIFY_FLEX_START = 'flex-start';
var CSS_JUSTIFY_CENTER = 'center';
var CSS_JUSTIFY_FLEX_END = 'flex-end';
var CSS_JUSTIFY_SPACE_BETWEEN = 'space-between';
var CSS_JUSTIFY_SPACE_AROUND = 'space-around';

var CSS_ALIGN_FLEX_START = 'flex-start';
var CSS_ALIGN_CENTER = 'center';
var CSS_ALIGN_FLEX_END = 'flex-end';
var CSS_ALIGN_STRETCH = 'stretch';

var CSS_POSITION_RELATIVE = 'relative';
var CSS_POSITION_ABSOLUTE = 'absolute';

return {
computeLayout: layoutNode,
fillNodes: fillNodes,
extractNodes: extractNodes
}
})();

// UMD (Universal Module Definition)
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/Layout-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,46 @@
/* globals layoutTestUtils */

var testLayout = layoutTestUtils.testLayout;
var testFillNodes = layoutTestUtils.testFillNodes;
var testExtractNodes = layoutTestUtils.testExtractNodes;
var text = layoutTestUtils.text;
var texts = layoutTestUtils.texts;
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() {
it('should layout a single node with width and height', function() {
testLayout({
Expand Down
24 changes: 24 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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;
}
}));
9 changes: 7 additions & 2 deletions src/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

var layoutTestUtils = require('./Layout-test-utils.js');
var computeLayout = require('./Layout.js');
var computeLayout = require('./Layout.js').computeLayout;
var fillNodes = require('./Layout.js').fillNodes;
var fs = require('fs');
var JavaTranspiler = require('./JavaTranspiler.js');

Expand All @@ -30,7 +31,11 @@ global.layoutTestUtils = {
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.xit = function() { /* ignore skipped tests */ };

Expand Down