Files
yoga/src/Layout-test-utils.js

301 lines
8.2 KiB
JavaScript
Raw Normal View History

/** @nolint */
var layoutTestUtils = (function() {
var iframe = (function() {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var doc = iframe.contentDocument;
var style = document.createElement('style');
style.innerText = (function() {/*
body, div {
box-sizing: border-box;
2014-04-22 11:31:42 -07:00
border: 0 solid black;
position: relative;
display: flex;
flex-direction: column;
align-items: flex-start;
justify-content: flex-start;
flex-shrink: 0;
margin: 0;
padding: 0;
}
*/} + '').slice(15, -4);
doc.head.appendChild(style);
return iframe;
})();
2014-04-26 17:11:22 -07:00
var body = iframe.contentDocument.body;
2014-04-26 19:02:16 -07:00
var iframeText = document.createElement('iframe');
document.body.appendChild(iframeText);
var realComputeLayout = computeLayout;
function computeCSSLayout(rootNode) {
function fillNodes(node) {
node.layout = {
width: undefined,
height: undefined,
top: 0,
left: 0
};
if (!node.style) {
node.style = {};
}
2014-04-26 17:11:22 -07:00
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 extractNodes(rootNode);
}
function computeDOMLayout(node) {
var body = iframe.contentDocument.body;
function transfer(div, node, name, ext) {
if (name in node.style) {
div.style[name] = node.style[name] + (ext || '');
}
}
2014-04-22 11:31:42 -07:00
function transferSpacing(div, node, type, suffix) {
transfer(div, node, type + suffix, 'px');
transfer(div, node, type + 'Left' + suffix, 'px');
transfer(div, node, type + 'Top' + suffix, 'px');
transfer(div, node, type + 'Bottom' + suffix, 'px');
transfer(div, node, type + 'Right' + suffix, 'px');
}
function renderNode(parent, node) {
var div = document.createElement('div');
transfer(div, node, 'width', 'px');
transfer(div, node, 'height', 'px');
transfer(div, node, 'top', 'px');
transfer(div, node, 'left', 'px');
transfer(div, node, 'right', 'px');
transfer(div, node, 'bottom', 'px');
2014-04-22 11:31:42 -07:00
transferSpacing(div, node, 'margin', '');
transferSpacing(div, node, 'padding', '');
transferSpacing(div, node, 'border', 'Width');
transfer(div, node, 'flexDirection');
transfer(div, node, 'flex');
transfer(div, node, 'justifyContent');
transfer(div, node, 'alignSelf');
transfer(div, node, 'alignItems');
transfer(div, node, 'position');
parent.appendChild(div);
(node.children || []).forEach(function(child) {
renderNode(div, child);
});
2014-04-26 17:11:22 -07:00
if (node.style.measure) {
div.innerText = node.style.measure.toString();
2014-04-26 12:16:27 -07:00
}
return div;
}
var div = renderNode(body, node);
function buildLayout(absoluteRect, div) {
var rect = div.getBoundingClientRect();
var result = {
2014-09-11 09:23:30 -07:00
width: rect.width,
height: rect.height,
top: rect.top - absoluteRect.top,
left: rect.left - absoluteRect.left
};
var children = [];
for (var child = div.firstChild; child; child = child.nextSibling) {
2014-04-26 12:16:27 -07:00
if (child.nodeType !== 3 /* textNode */) {
children.push(buildLayout(rect, child));
}
}
if (children.length) {
result.children = children;
}
return result;
}
var layout = buildLayout({left: 0, top: 0}, div);
body.removeChild(div);
return layout;
}
function nameLayout(name, layout) {
var namedLayout = {name: name};
for (var key in layout) {
namedLayout[key] = layout[key];
}
return namedLayout;
}
function testNamedLayout(name, layoutA, layoutB) {
expect(nameLayout(name, layoutA))
.toEqual(nameLayout(name, layoutB));
}
2014-04-21 16:52:53 -07:00
function isEqual(a, b) {
// computeCSSLayout and computeDOMLayout output a tree with same ordered elements
2014-04-21 16:52:53 -07:00
return JSON.stringify(a) === JSON.stringify(b);
}
function reduceTest(node) {
function isWorking() {
return isEqual(
computeDOMLayout(node),
computeCSSLayout(node)
2014-04-21 16:52:53 -07:00
);
}
if (isWorking()) {
return node;
2014-04-21 16:52:53 -07:00
}
var isModified = true;
function rec(node) {
// Style
for (var key in node.style) {
var value = node.style[key];
delete node.style[key];
if (isWorking()) {
node.style[key] = value;
} else {
isModified = true;
}
}
// Round values
for (var key in node.style) {
var value = node.style[key];
if (value > 100) {
node.style[key] = Math.round(value / 100) * 100;
} else if (value > 10) {
node.style[key] = Math.round(value / 10) * 10;
} else if (value > 1) {
2014-04-21 16:52:53 -07:00
node.style[key] = 5;
}
if (node.style[key] !== value) {
if (isWorking()) {
node.style[key] = value;
} else {
isModified = true;
}
}
}
// Children
for (var i = 0; node.children && i < node.children.length; ++i) {
var value = node.children[i];
node.children.splice(i, 1);
if (isWorking()) {
if (!node.children) {
node.children = [];
}
2014-04-21 16:52:53 -07:00
node.children.splice(i, 0, value);
rec(node.children[i]);
} else {
i--;
isModified = true;
}
}
}
while (isModified) {
isModified = false;
rec(node);
}
return node;
2014-04-21 16:52:53 -07:00
}
var textSizes = {
smallWidth: 34.671875,
smallHeight: 18,
bigWidth: 172.421875,
bigHeight: 36,
bigMinWidth: 100.453125,
};
return {
textSizes: textSizes,
testLayout: function(node, expectedLayout) {
var layout = computeCSSLayout(node);
var domLayout = computeDOMLayout(node);
testNamedLayout('expected-dom', expectedLayout, domLayout);
testNamedLayout('layout-dom', layout, domLayout);
},
testRandomLayout: function(node, i) {
expect({i: i, node: node, layout: computeCSSLayout(node)})
.toEqual({i: i, node: node, layout: computeDOMLayout(node)});
2014-04-19 22:08:10 -07:00
},
computeLayout: computeCSSLayout,
2014-04-21 16:52:53 -07:00
computeDOMLayout: computeDOMLayout,
2014-04-26 17:11:22 -07:00
reduceTest: reduceTest,
text: function(text) {
2014-04-26 19:02:16 -07:00
var body = iframeText.contentDocument.body;
var fn = function(width) {
if (width === undefined || width !== width) {
width = Infinity;
}
2014-04-28 12:34:04 -07:00
// Constants for testing purposes between C/JS and other platforms
// Comment this block of code if you want to use the browser to
// generate proper sizes
if (text === 'small') {
return {
width: Math.min(textSizes.smallWidth, width),
height: textSizes.smallHeight
};
2014-04-28 12:34:04 -07:00
}
if (text === 'loooooooooong with space') {
var res = {
width: width >= textSizes.bigWidth ? textSizes.bigWidth : Math.max(textSizes.bigMinWidth, width),
height: width >= textSizes.bigWidth ? textSizes.smallHeight : textSizes.bigHeight
};
return res;
2014-04-28 12:34:04 -07:00
}
return;
2014-04-26 19:02:16 -07:00
var div = document.createElement('div');
div.style.width = (width === Infinity ? 10000000 : width) + 'px';
div.style.display = 'flex';
div.style.flexDirection = 'column';
div.style.alignItems = 'flex-start';
2014-04-26 17:11:22 -07:00
var span = document.createElement('span');
2014-04-26 19:02:16 -07:00
span.style.display = 'flex';
span.style.flexDirection = 'column';
span.style.alignItems = 'flex-start';
2014-04-26 17:11:22 -07:00
span.innerText = text;
2014-04-26 19:02:16 -07:00
div.appendChild(span);
body.appendChild(div);
2014-04-26 17:11:22 -07:00
var rect = span.getBoundingClientRect();
2014-04-26 19:02:16 -07:00
body.removeChild(div);
2014-04-26 17:11:22 -07:00
return {
width: rect.width,
height: rect.height
};
};
fn.toString = function() { return text; }
return fn;
}
}
})();