Comparing version 0.0.1 to 0.0.2
@@ -42,2 +42,2 @@ 'use strict'; | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "css-layout", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Reimplementation of CSS layout using pure JavaScript", | ||
"main": "src/Layout.js", | ||
"main": "src/main.js", | ||
"scripts": { | ||
"pretest": "./node_modules/eslint/bin/eslint.js src", | ||
"test": "./node_modules/karma/bin/karma start ./karma.conf.js --single-run" | ||
@@ -20,2 +21,3 @@ }, | ||
"devDependencies": { | ||
"eslint": "^0.14.1", | ||
"jasmine-core": "^2.2.0", | ||
@@ -22,0 +24,0 @@ "karma": "^0.12.31", |
@@ -34,3 +34,3 @@ css-layout | ||
- For the JS tests: Open `RunLayoutTests.html` and `RunLayoutRandomTests.html` in Chrome | ||
- For the JS tests: Open `RunLayoutTests.html` and `RunLayoutRandomTests.html` in Chrome or run `$ npm test` | ||
- For the C and Java tests: run `make` in your terminal. It will also transpile the JS code | ||
@@ -52,2 +52,3 @@ | ||
flex | positive number | ||
flexWrap | 'wrap', 'nowrap' | ||
position | 'relative', 'absolute' | ||
@@ -67,5 +68,2 @@ | ||
position: relative; | ||
border: 0 solid black; | ||
margin: 0; | ||
padding: 0; | ||
@@ -76,2 +74,6 @@ display: flex; | ||
flex-shrink: 0; | ||
border: 0 solid black; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
@@ -78,0 +80,0 @@ ``` |
@@ -14,4 +14,2 @@ /** | ||
var reduceTest = layoutTestUtils.reduceTest; | ||
var text = layoutTestUtils.text; | ||
var texts = layoutTestUtils.texts; | ||
@@ -43,10 +41,3 @@ describe('Random layout', function() { | ||
} | ||
function randChildren(node, chance) { | ||
while (rng.nextFloat() < chance) { | ||
if (!node.children) { | ||
node.children = []; | ||
} | ||
node.children.push(generateRandomNode()); | ||
} | ||
} | ||
function randSpacing(node, chance, type, suffix, min, max) { | ||
@@ -93,2 +84,11 @@ randMinMax(node, chance, type + suffix, min, max); | ||
function randChildren(node, chance) { | ||
while (rng.nextFloat() < chance) { | ||
if (!node.children) { | ||
node.children = []; | ||
} | ||
node.children.push(generateRandomNode()); | ||
} | ||
} | ||
randChildren(node, 0.4); | ||
@@ -98,2 +98,12 @@ return node; | ||
function checkRandomLayout(i, node) { | ||
it('should layout randomly #' + i + '.', function(node) { | ||
if (JSON.stringify(computeLayout(node)) !== JSON.stringify(computeDOMLayout(node))) { | ||
node = reduceTest(node); | ||
} | ||
testRandomLayout(node, i); | ||
}.bind(this, node)); | ||
} | ||
for (var i = 0; i < 100; ++i) { | ||
@@ -108,11 +118,5 @@ var node = generateRandomNode(); | ||
it('should layout randomly #' + i +'.', function(node) { | ||
if (JSON.stringify(computeLayout(node)) !== JSON.stringify(computeDOMLayout(node))) { | ||
node = reduceTest(node); | ||
} | ||
testRandomLayout(node, i); | ||
}.bind(this, node)); | ||
checkRandomLayout.call(this, i, node); | ||
} | ||
}); |
@@ -12,2 +12,4 @@ /** | ||
var testLayout = layoutTestUtils.testLayout; | ||
var testFillNodes = layoutTestUtils.testFillNodes; | ||
var testExtractNodes = layoutTestUtils.testExtractNodes; | ||
var text = layoutTestUtils.text; | ||
@@ -17,2 +19,36 @@ var texts = layoutTestUtils.texts; | ||
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() { | ||
@@ -55,3 +91,3 @@ it('should layout a single node with width and height', function() { | ||
{width: 250, height: 250, top: 0, left: 0}, | ||
{width: 250, height: 250, top: 250, left: 0}, | ||
{width: 250, height: 250, top: 250, left: 0} | ||
]} | ||
@@ -223,3 +259,3 @@ ]} | ||
{style: {width: 1000, height: 1000}, children: [ | ||
{style: {width: 100, height: 100, flex: 1}}, | ||
{style: {width: 100, height: 100, flex: 1}} | ||
]}, | ||
@@ -240,3 +276,3 @@ {width: 1000, height: 1000, top: 0, left: 0, children: [ | ||
{width: 200, height: 100, top: 0, left: 0}, | ||
{width: 100, height: 100, top: 100, left: 0}, | ||
{width: 100, height: 100, top: 100, left: 0} | ||
]} | ||
@@ -254,3 +290,3 @@ ); | ||
{width: 200, height: 100, top: 0, left: 400}, | ||
{width: 100, height: 100, top: 100, left: 450}, | ||
{width: 100, height: 100, top: 100, left: 450} | ||
]} | ||
@@ -268,3 +304,3 @@ ); | ||
{width: 200, height: 100, top: 0, left: 800}, | ||
{width: 100, height: 100, top: 100, left: 900}, | ||
{width: 100, height: 100, top: 100, left: 900} | ||
]} | ||
@@ -282,3 +318,3 @@ ); | ||
{width: 200, height: 100, top: 0, left: 800}, | ||
{width: 100, height: 100, top: 100, left: 450}, | ||
{width: 100, height: 100, top: 100, left: 450} | ||
]} | ||
@@ -325,3 +361,3 @@ ); | ||
{style: {height: 100}}, | ||
{style: {height: 200}}, | ||
{style: {height: 200}} | ||
]}, | ||
@@ -373,3 +409,3 @@ {width: 0, height: 100, top: 0, left: 0, children: [ | ||
{style: {}, children: [ | ||
{style: {flex: 1}}, | ||
{style: {flex: 1}} | ||
]}, | ||
@@ -496,3 +532,3 @@ {width: 0, height: 0, top: 0, left: 0, children: [ | ||
{style: {position: 'absolute', width: 50}}, | ||
{style: {flex: 1}}, | ||
{style: {flex: 1}} | ||
]}, | ||
@@ -664,3 +700,3 @@ {width: 500, height: 0, top: 0, left: 0, children: [ | ||
]} | ||
) | ||
); | ||
}); | ||
@@ -700,5 +736,5 @@ | ||
{width: 0, height: 500, top: 0, left: 0}, | ||
{width: 0, height: 0, top: 500, left: 0}, | ||
{width: 0, height: 0, top: 500, left: 0} | ||
]} | ||
) | ||
); | ||
}); | ||
@@ -710,3 +746,3 @@ | ||
{width: 10, height: 10, top: 0, left: 0} | ||
) | ||
); | ||
}); | ||
@@ -722,3 +758,3 @@ | ||
]} | ||
) | ||
); | ||
}); | ||
@@ -734,3 +770,3 @@ | ||
]} | ||
) | ||
); | ||
}); | ||
@@ -951,3 +987,3 @@ | ||
{width: 0, height: 25, top: 0, left: 0}, | ||
{width: 0, height: 75, top: 25, left: 0}, | ||
{width: 0, height: 75, top: 25, left: 0} | ||
]} | ||
@@ -965,3 +1001,3 @@ ); | ||
{width: 0, height: 0, top: 0, left: 0}, | ||
{width: 0, height: 0, top: 0, left: 0}, | ||
{width: 0, height: 0, top: 0, left: 0} | ||
]} | ||
@@ -979,3 +1015,3 @@ ); | ||
{width: 50, height: 100, top: 0, left: 0}, | ||
{width: 50, height: 0, top: 100, left: 0}, | ||
{width: 50, height: 0, top: 100, left: 0} | ||
]} | ||
@@ -1172,3 +1208,3 @@ ); | ||
{style: {width: 40, height: 10}}, | ||
{style: {width: 40, height: 10}}, | ||
{style: {width: 40, height: 10}} | ||
]}, | ||
@@ -1187,3 +1223,3 @@ {width: 100, height: 20, top: 0, left: 0, children: [ | ||
{style: {height: 100}}, | ||
{style: {height: 200}}, | ||
{style: {height: 200}} | ||
]}, | ||
@@ -1233,2 +1269,1 @@ {width: 0, height: 100, top: 0, left: 0, children: [ | ||
}); | ||
@@ -55,3 +55,3 @@ /** | ||
function (str, match1) { | ||
return 'layout.' + (match1 == 'TOP' ? 'y' : 'x'); | ||
return 'layout.' + (match1 === 'TOP' ? 'y' : 'x'); | ||
}) | ||
@@ -101,3 +101,3 @@ .replace( // style.position[CSS_TOP] => style.positionTop | ||
.replace(/ !== /g, ' != ') | ||
.replace(/\n /g, '\n') | ||
.replace(/\n {2}/g, '\n') | ||
.replace(/\/[*]!([^*]+)[*]\//g, '$1') | ||
@@ -123,4 +123,4 @@ .replace(/css_node_t\*/g, 'CSSNode')); | ||
return allTestsInJava.join('\n\n'); | ||
}, | ||
} | ||
} | ||
}; | ||
@@ -127,0 +127,0 @@ if (typeof module !== 'undefined') { |
@@ -47,2 +47,4 @@ /** | ||
var _cachedIframe; | ||
function renderIframe() { | ||
@@ -54,6 +56,5 @@ var iframe = document.createElement('iframe'); | ||
var cachedIframe; | ||
function getIframe() { | ||
if (cachedIframe) { | ||
return cachedIframe; | ||
function getIframe(iframe) { | ||
if (_cachedIframe) { | ||
return _cachedIframe; | ||
} | ||
@@ -63,3 +64,3 @@ | ||
if(doc.readyState === 'complete') { | ||
if (doc.readyState === 'complete') { | ||
var style = document.createElement('style'); | ||
@@ -93,3 +94,3 @@ style.textContent = (function() {/* | ||
doc.head.appendChild(style); | ||
cachedIframe = iframe; | ||
_cachedIframe = iframe; | ||
return iframe; | ||
@@ -103,7 +104,9 @@ } else { | ||
var iframe = renderIframe(); | ||
getIframe(); | ||
getIframe(iframe); | ||
} | ||
if (typeof computeLayout === 'function') { | ||
var realComputeLayout = computeLayout; | ||
if (typeof computeLayout === 'object') { | ||
var fillNodes = computeLayout.fillNodes; | ||
var extractNodes = computeLayout.extractNodes; | ||
var realComputeLayout = computeLayout.computeLayout; | ||
} | ||
@@ -150,30 +153,2 @@ | ||
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); | ||
@@ -265,2 +240,10 @@ realComputeLayout(rootNode); | ||
function testFillNodes(node, filledNode) { | ||
expect(fillNodes(node)).toEqual(filledNode); | ||
} | ||
function testExtractNodes(node, extractedNode) { | ||
expect(extractNodes(node)).toEqual(extractedNode); | ||
} | ||
function testNamedLayout(name, layoutA, layoutB) { | ||
@@ -290,5 +273,8 @@ expect(nameLayout(name, layoutA)) | ||
function rec(node) { | ||
var key; | ||
var value; | ||
// Style | ||
for (var key in node.style) { | ||
var value = node.style[key]; | ||
for (key in node.style) { | ||
value = node.style[key]; | ||
delete node.style[key]; | ||
@@ -302,4 +288,4 @@ if (isWorking()) { | ||
// Round values | ||
for (var key in node.style) { | ||
var value = node.style[key]; | ||
for (key in node.style) { | ||
value = node.style[key]; | ||
if (value > 100) { | ||
@@ -322,3 +308,3 @@ node.style[key] = Math.round(value / 100) * 100; | ||
for (var i = 0; node.children && i < node.children.length; ++i) { | ||
var value = node.children[i]; | ||
value = node.children[i]; | ||
node.children.splice(i, 1); | ||
@@ -379,3 +365,3 @@ if (isWorking()) { | ||
small: 'small', | ||
big: 'loooooooooong with space', | ||
big: 'loooooooooong with space' | ||
}; | ||
@@ -400,3 +386,3 @@ | ||
bigHeight: measureTextSizes(texts.big, 0).height, | ||
bigMinWidth: measureTextSizes(texts.big, 0).width, | ||
bigMinWidth: measureTextSizes(texts.big, 0).width | ||
}; | ||
@@ -415,3 +401,5 @@ } | ||
}, | ||
testRandomLayout: function(node, i) { | ||
testFillNodes: testFillNodes, | ||
testExtractNodes: testExtractNodes, | ||
testRandomLayout: function(node) { | ||
expect({node: node, layout: computeCSSLayout(node)}) | ||
@@ -418,0 +406,0 @@ .toEqual({node: node, layout: computeDOMLayout(node)}); |
@@ -12,2 +12,38 @@ /** | ||
var CSS_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'; | ||
var leading = { | ||
row: 'left', | ||
column: 'top' | ||
}; | ||
var trailing = { | ||
row: 'right', | ||
column: 'bottom' | ||
}; | ||
var pos = { | ||
row: 'left', | ||
column: 'top' | ||
}; | ||
var dim = { | ||
row: 'width', | ||
column: 'height' | ||
}; | ||
function capitalizeFirst(str) { | ||
@@ -30,3 +66,31 @@ return str.charAt(0).toUpperCase() + str.slice(1); | ||
} | ||
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) { | ||
@@ -143,2 +207,9 @@ var key = type + capitalizeFirst(location) + suffix; | ||
function fmaxf(a, b) { | ||
if (a > b) { | ||
return a; | ||
} | ||
return b; | ||
} | ||
// When the user specifically sets a value for width or height | ||
@@ -171,46 +242,4 @@ function setDimensionFromStyle(node, axis) { | ||
var leading = { | ||
row: 'left', | ||
column: 'top' | ||
}; | ||
var trailing = { | ||
row: 'right', | ||
column: 'bottom' | ||
}; | ||
var pos = { | ||
row: 'left', | ||
column: 'top' | ||
}; | ||
var dim = { | ||
row: 'width', | ||
column: 'height' | ||
}; | ||
function layoutNode(node, parentMaxWidth) { | ||
function fmaxf(a, b) { | ||
if (a > b) { | ||
return a; | ||
} | ||
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) { | ||
var/*css_flex_direction_t*/ mainAxis = getFlexDirection(node); | ||
@@ -254,3 +283,3 @@ var/*css_flex_direction_t*/ crossAxis = mainAxis === CSS_FLEX_DIRECTION_ROW ? | ||
if (isRowUndefined || isColumnUndefined) { | ||
var/*css_dim_t*/ measure_dim = node.style.measure( | ||
var/*css_dim_t*/ measureDim = node.style.measure( | ||
/*(c)!node->context,*/ | ||
@@ -260,7 +289,7 @@ width | ||
if (isRowUndefined) { | ||
node.layout.width = measure_dim.width + | ||
node.layout.width = measureDim.width + | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); | ||
} | ||
if (isColumnUndefined) { | ||
node.layout.height = measure_dim.height + | ||
node.layout.height = measureDim.height + | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_COLUMN); | ||
@@ -272,5 +301,10 @@ } | ||
var/*int*/ i; | ||
var/*int*/ ii; | ||
var/*css_node_t**/ child; | ||
var/*css_flex_direction_t*/ axis; | ||
// Pre-fill some dimensions straight from the parent | ||
for (var/*int*/ i = 0; i < node.children.length; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
for (i = 0; i < node.children.length; ++i) { | ||
child = node.children[i]; | ||
// Pre-fill cross axis dimensions when the child is using stretch before | ||
@@ -289,7 +323,7 @@ // we call the recursive layout pass | ||
); | ||
} else if (getPositionType(child) == CSS_POSITION_ABSOLUTE) { | ||
} else if (getPositionType(child) === CSS_POSITION_ABSOLUTE) { | ||
// Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both | ||
// left and right or top and bottom). | ||
for (var/*int*/ ii = 0; ii < 2; ii++) { | ||
var/*css_flex_direction_t*/ axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
for (ii = 0; ii < 2; ii++) { | ||
axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
if (!isUndefined(node.layout[dim[axis]]) && | ||
@@ -322,3 +356,3 @@ !isDimDefined(child, axis) && | ||
var/*int*/ endLine = 0; | ||
var/*int*/ nextOffset = 0; | ||
// var/*int*/ nextOffset = 0; | ||
var/*int*/ alreadyComputedNextLayout = 0; | ||
@@ -342,4 +376,6 @@ // We aggregate the total dimensions of the container in those two variables | ||
var/*int*/ nonFlexibleChildrenCount = 0; | ||
for (var/*int*/ i = startLine; i < node.children.length; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
var/*float*/ maxWidth; | ||
for (i = startLine; i < node.children.length; ++i) { | ||
child = node.children[i]; | ||
var/*float*/ nextContentDim = 0; | ||
@@ -360,12 +396,12 @@ | ||
} else { | ||
var/*float*/ maxWidth = CSS_UNDEFINED; | ||
if (mainAxis === CSS_FLEX_DIRECTION_ROW) { | ||
// do nothing | ||
} else if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { | ||
maxWidth = node.layout[dim[CSS_FLEX_DIRECTION_ROW]] - | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); | ||
} else { | ||
maxWidth = CSS_UNDEFINED; | ||
if (mainAxis !== CSS_FLEX_DIRECTION_ROW) { | ||
maxWidth = parentMaxWidth - | ||
getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) - | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); | ||
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { | ||
maxWidth = node.layout[dim[CSS_FLEX_DIRECTION_ROW]] - | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); | ||
} | ||
} | ||
@@ -431,4 +467,4 @@ | ||
// contains only flexible children. | ||
for (var/*int*/ i = startLine; i < endLine; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
for (i = startLine; i < endLine; ++i) { | ||
child = node.children[i]; | ||
if (isFlex(child)) { | ||
@@ -440,9 +476,7 @@ // At this point we know the final size of the element in the main | ||
var/*float*/ maxWidth = CSS_UNDEFINED; | ||
if (mainAxis === CSS_FLEX_DIRECTION_ROW) { | ||
// do nothing | ||
} else if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { | ||
maxWidth = CSS_UNDEFINED; | ||
if (isDimDefined(node, CSS_FLEX_DIRECTION_ROW)) { | ||
maxWidth = node.layout[dim[CSS_FLEX_DIRECTION_ROW]] - | ||
getPaddingAndBorderAxis(node, CSS_FLEX_DIRECTION_ROW); | ||
} else { | ||
} else if (mainAxis !== CSS_FLEX_DIRECTION_ROW) { | ||
maxWidth = parentMaxWidth - | ||
@@ -462,5 +496,3 @@ getMarginAxis(node, CSS_FLEX_DIRECTION_ROW) - | ||
var/*css_justify_t*/ justifyContent = getJustifyContent(node); | ||
if (justifyContent === CSS_JUSTIFY_FLEX_START) { | ||
// Do nothing | ||
} else if (justifyContent === CSS_JUSTIFY_CENTER) { | ||
if (justifyContent === CSS_JUSTIFY_CENTER) { | ||
leadingMainDim = remainingMainDim / 2; | ||
@@ -495,4 +527,4 @@ } else if (justifyContent === CSS_JUSTIFY_FLEX_END) { | ||
for (var/*int*/ i = startLine; i < endLine; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
for (i = startLine; i < endLine; ++i) { | ||
child = node.children[i]; | ||
@@ -529,3 +561,3 @@ if (getPositionType(child) === CSS_POSITION_ABSOLUTE && | ||
// by the container, then we set it via the children. | ||
if (isUndefined(node.layout[dim[mainAxis]])) { | ||
if (isUndefined(containerMainAxis)) { | ||
containerMainAxis = fmaxf( | ||
@@ -553,4 +585,4 @@ // We're missing the last padding at this point to get the final | ||
for (var/*int*/ i = startLine; i < endLine; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
for (i = startLine; i < endLine; ++i) { | ||
child = node.children[i]; | ||
@@ -573,5 +605,3 @@ if (getPositionType(child) === CSS_POSITION_ABSOLUTE && | ||
var/*css_align_t*/ alignItem = getAlignItem(node, child); | ||
if (alignItem === CSS_ALIGN_FLEX_START) { | ||
// Do nothing | ||
} else if (alignItem === CSS_ALIGN_STRETCH) { | ||
if (alignItem === CSS_ALIGN_STRETCH) { | ||
// You can only stretch if the dimension has not already been set | ||
@@ -588,3 +618,3 @@ // previously. | ||
} | ||
} else { | ||
} else if (alignItem !== CSS_ALIGN_FLEX_START) { | ||
// The remaining space between the parent dimensions+padding and child | ||
@@ -638,9 +668,9 @@ // dimensions+margin. | ||
for (var/*int*/ i = 0; i < node.children.length; ++i) { | ||
var/*css_node_t**/ child = node.children[i]; | ||
if (getPositionType(child) == CSS_POSITION_ABSOLUTE) { | ||
for (i = 0; i < node.children.length; ++i) { | ||
child = node.children[i]; | ||
if (getPositionType(child) === CSS_POSITION_ABSOLUTE) { | ||
// Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both | ||
// left and right or top and bottom). | ||
for (var/*int*/ ii = 0; ii < 2; ii++) { | ||
var/*css_flex_direction_t*/ axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
for (ii = 0; ii < 2; ii++) { | ||
axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
if (!isUndefined(node.layout[dim[axis]]) && | ||
@@ -661,4 +691,4 @@ !isDimDefined(child, axis) && | ||
} | ||
for (var/*int*/ ii = 0; ii < 2; ii++) { | ||
var/*css_flex_direction_t*/ axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
for (ii = 0; ii < 2; ii++) { | ||
axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; | ||
if (isPosDefined(child, trailing[axis]) && | ||
@@ -674,8 +704,28 @@ !isPosDefined(child, leading[axis])) { | ||
} | ||
} | ||
return { | ||
computeLayout: layoutNode, | ||
fillNodes: fillNodes, | ||
extractNodes: extractNodes | ||
}; | ||
})(); | ||
if (typeof module === 'object') { | ||
module.exports = computeLayout; | ||
} | ||
// 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 () { | ||
return computeLayout; | ||
})); |
@@ -11,3 +11,3 @@ /** | ||
var layoutTestUtils = require('./Layout-test-utils.js'); | ||
var computeLayout = require('./Layout.js'); | ||
var computeLayout = require('./Layout.js').computeLayout; | ||
var fs = require('fs'); | ||
@@ -34,3 +34,7 @@ var JavaTranspiler = require('./JavaTranspiler.js'); | ||
global.describe = function(name, cb) { cb(); }; | ||
global.describe = function(name, cb) { | ||
if (name === 'Layout') { | ||
cb(); | ||
} | ||
}; | ||
global.it = function(name, cb) { currentTest = name; cb(); }; | ||
@@ -46,2 +50,10 @@ global.xit = function() { /* ignore skipped tests */ }; | ||
function indent(level) { | ||
var result = ''; | ||
for (var i = 0; i < level; ++i) { | ||
result += ' '; | ||
} | ||
return result; | ||
} | ||
function add(str) { | ||
@@ -55,16 +67,5 @@ if (str.length > 0) { | ||
function isEmpty(obj) { | ||
for (var key in obj) { | ||
return false; | ||
} | ||
return true; | ||
return !Object.keys(obj).length; | ||
} | ||
function indent(level) { | ||
var result = ''; | ||
for (var i = 0; i < level; ++i) { | ||
result += ' '; | ||
} | ||
return result; | ||
} | ||
add('{'); | ||
@@ -80,3 +81,3 @@ level++; | ||
} | ||
function rec_style(node) { | ||
function recStyle(node) { | ||
@@ -87,14 +88,12 @@ function addStyle(str) { | ||
function addEnum(node, js_key, c_key, dict) { | ||
if (js_key in node.style) { | ||
addStyle(c_key + ' = ' + dict[node.style[js_key]] + ';'); | ||
function addEnum(node, jsKey, cKey, dict) { | ||
if (jsKey in node.style) { | ||
addStyle(cKey + ' = ' + dict[node.style[jsKey]] + ';'); | ||
} | ||
} | ||
function addFloat(positive, node, js_key, c_key) { | ||
if (js_key in node.style) { | ||
if (positive === 'positive' && node.style[js_key] < 0) { | ||
// do nothing | ||
} else { | ||
addStyle(c_key + ' = ' + node.style[js_key] + ';'); | ||
function addFloat(positive, node, jsKey, cKey) { | ||
if (jsKey in node.style) { | ||
if (positive !== 'positive' || node.style[jsKey] >= 0) { | ||
addStyle(cKey + ' = ' + node.style[jsKey] + ';'); | ||
} | ||
@@ -167,3 +166,3 @@ } | ||
if (node.children) { | ||
add('init_css_node_children(node_' + (level - 3) +', ' + node.children.length + ');'); | ||
add('init_css_node_children(node_' + (level - 3) + ', ' + node.children.length + ');'); | ||
add('{'); | ||
@@ -175,3 +174,3 @@ level++; | ||
add('node_' + (level - 3) + ' = node_' + (level - 4) + '->get_child(node_' + (level - 4) + '->context, ' + i + ');'); | ||
rec_style(node.children[i]); | ||
recStyle(node.children[i]); | ||
} | ||
@@ -183,3 +182,3 @@ | ||
} | ||
rec_style(test.node); | ||
recStyle(test.node); | ||
level--; | ||
@@ -195,3 +194,3 @@ add('}'); | ||
function rec_layout(node) { | ||
function recLayout(node) { | ||
function addLayout(str) { | ||
@@ -207,3 +206,3 @@ add('node_' + (level - 3) + '->layout.' + str); | ||
if (node.children) { | ||
add('init_css_node_children(node_' + (level - 3) +', ' + node.children.length + ');'); | ||
add('init_css_node_children(node_' + (level - 3) + ', ' + node.children.length + ');'); | ||
add('{'); | ||
@@ -215,3 +214,3 @@ level++; | ||
add('node_' + (level - 3) + ' = node_' + (level - 4) + '->get_child(node_' + (level - 4) + '->context, ' + i + ');'); | ||
rec_layout(node.children[i]); | ||
recLayout(node.children[i]); | ||
} | ||
@@ -223,3 +222,3 @@ | ||
} | ||
rec_layout(test.expectedLayout); | ||
recLayout(test.expectedLayout); | ||
level--; | ||
@@ -253,3 +252,3 @@ add('}'); | ||
.replace(/ !== /g, ' != ') | ||
.replace(/\n /g, '\n') | ||
.replace(/\n {2}/g, '\n') | ||
.replace(/\/\*\(c\)!([^*]+)\*\//g, '$1') | ||
@@ -261,2 +260,3 @@ .replace(/\/[*]!([^*]+)[*]\//g, '$1') | ||
function makeConstDefs() { | ||
/* eslint no-multi-spaces: 3 */ | ||
var lines = [ | ||
@@ -263,0 +263,0 @@ '#define SMALL_WIDTH ' + layoutTestUtils.textSizes.smallWidth, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
456703
53
2683
82
5