Merge pull request #108 from ColinEberhardt/api-update

Updated the public API to no-longer extract nodes
This commit is contained in:
Colin Eberhardt
2015-08-17 11:50:27 +01:00
9 changed files with 121 additions and 88 deletions

View File

@@ -114,10 +114,25 @@ var layoutTestUtils = (function() {
if (typeof computeLayout === 'object') {
var fillNodes = computeLayout.fillNodes;
var extractNodes = computeLayout.extractNodes;
var realComputeLayout = computeLayout.computeLayout;
}
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;
}
delete layout.right;
delete layout.bottom;
delete layout.direction;
return layout;
}
function roundLayout(layout) {
// Chrome rounds all the numbers with a precision of 1/64
// Reproduce the same behavior

View File

@@ -80,42 +80,33 @@ var computeLayout = (function() {
return 0;
}
// When transpiled to Java / C the node type has layout, children and style
// properties. For the JavaScript version this function adds these properties
// if they don't already exist.
function fillNodes(node) {
node.layout = {
width: undefined,
height: undefined,
top: 0,
left: 0,
right: 0,
bottom: 0
};
if (!node.layout) {
node.layout = {
width: undefined,
height: undefined,
top: 0,
left: 0,
right: 0,
bottom: 0
};
}
if (!node.style) {
node.style = {};
}
if (!node.children || node.style.measure) {
if (!node.children) {
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;
}
delete layout.right;
delete layout.bottom;
delete layout.direction;
return layout;
}
function getPositiveSpacing(node, type, suffix, locations) {
for (var i = 0; i < locations.length; ++i) {
var location = locations[i];
@@ -981,11 +972,13 @@ var computeLayout = (function() {
return {
computeLayout: layoutNode,
fillNodes: fillNodes,
extractNodes: extractNodes
fillNodes: fillNodes
};
})();
// This module export is only used for the purposes of unit testing this file. When
// the library is packaged this file is included within css-layout.js which forms
// the public API.
if (typeof exports === 'object') {
module.exports = computeLayout;
}

View File

@@ -31,20 +31,10 @@ describe('Javascript Only', function() {
]}
);
});
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}
]}
it('should not replace user-provided layout information', function() {
testFillNodes(
{layout: {width: 200}},
{layout: {width: 200}, style: {}, children: []}
);
});
});

View File

@@ -20,9 +20,7 @@
// @@include('./Layout.js')
return function(node) {
node = computeLayout.fillNodes(node);
computeLayout.fillNodes(node);
computeLayout.computeLayout(node);
node = computeLayout.extractNodes(node);
return node;
};
}));