2014-09-26 16:22:44 -07:00
|
|
|
/* globals document, computeLayout */
|
2014-04-18 16:35:37 -07:00
|
|
|
|
|
|
|
var layoutTestUtils = (function() {
|
2014-09-26 16:22:44 -07:00
|
|
|
var cachedIframe;
|
|
|
|
function getIframe() {
|
|
|
|
if (cachedIframe) {
|
|
|
|
return cachedIframe;
|
|
|
|
}
|
2014-04-18 16:35:37 -07:00
|
|
|
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;
|
2014-04-18 16:35:37 -07:00
|
|
|
position: relative;
|
|
|
|
|
|
|
|
display: flex;
|
2014-09-19 18:26:53 -07:00
|
|
|
display: -webkit-flex;
|
2014-04-18 16:35:37 -07:00
|
|
|
flex-direction: column;
|
2014-09-19 18:26:53 -07:00
|
|
|
-webkit-flex-direction: column;
|
2014-10-08 09:35:44 -07:00
|
|
|
align-items: stretch;
|
|
|
|
-webkit-align-items: stretch;
|
2014-04-18 16:35:37 -07:00
|
|
|
justify-content: flex-start;
|
2014-09-19 18:26:53 -07:00
|
|
|
-webkit-justify-content: flex-start;
|
2014-04-18 16:35:37 -07:00
|
|
|
flex-shrink: 0;
|
2014-09-19 18:26:53 -07:00
|
|
|
-webkit-flex-shrink: 0;
|
2014-04-18 16:35:37 -07:00
|
|
|
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
*/} + '').slice(15, -4);
|
|
|
|
doc.head.appendChild(style);
|
2014-09-26 16:22:44 -07:00
|
|
|
cachedIframe = iframe;
|
2014-04-18 16:35:37 -07:00
|
|
|
return iframe;
|
2014-09-26 16:22:44 -07:00
|
|
|
}
|
2014-04-18 16:35:37 -07:00
|
|
|
|
2014-04-26 19:02:16 -07:00
|
|
|
|
2014-09-26 16:22:44 -07:00
|
|
|
if (typeof computeLayout === 'function') {
|
|
|
|
var realComputeLayout = computeLayout;
|
|
|
|
}
|
2014-04-26 12:46:21 -07:00
|
|
|
|
|
|
|
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) {
|
2014-04-26 12:46:21 -07:00
|
|
|
node.children = [];
|
|
|
|
}
|
|
|
|
node.children.forEach(fillNodes);
|
|
|
|
}
|
|
|
|
|
2014-04-27 12:37:26 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-26 12:46:21 -07:00
|
|
|
fillNodes(rootNode);
|
2014-10-08 09:35:44 -07:00
|
|
|
rootNode.style.alignSelf = 'flex-start';
|
2014-04-27 12:37:26 -07:00
|
|
|
realComputeLayout(rootNode);
|
|
|
|
return extractNodes(rootNode);
|
2014-04-26 12:46:21 -07:00
|
|
|
}
|
|
|
|
|
2014-04-18 16:35:37 -07:00
|
|
|
function computeDOMLayout(node) {
|
2014-09-26 16:22:44 -07:00
|
|
|
var body = getIframe().contentDocument.body;
|
2014-04-18 16:35:37 -07:00
|
|
|
|
|
|
|
function transfer(div, node, name, ext) {
|
|
|
|
if (name in node.style) {
|
2014-09-19 18:26:53 -07:00
|
|
|
div.style['-webkit-' + name] = node.style[name] + (ext || '');
|
2014-04-18 16:35:37 -07:00
|
|
|
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');
|
2014-04-18 16:35:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2014-04-18 16:35:37 -07:00
|
|
|
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
|
|
|
}
|
2014-04-18 16:35:37 -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
|
2014-04-18 16:35:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2014-04-18 16:35:37 -07:00
|
|
|
}
|
|
|
|
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) {
|
2014-04-26 12:46:21 -07:00
|
|
|
// 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),
|
2014-04-26 12:46:21 -07:00
|
|
|
computeCSSLayout(node)
|
2014-04-21 16:52:53 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isWorking()) {
|
2014-04-22 09:32:49 -07:00
|
|
|
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;
|
2014-04-21 18:50:34 -07:00
|
|
|
} 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);
|
2014-04-21 17:07:05 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-04-22 09:32:49 -07:00
|
|
|
return node;
|
2014-04-21 16:52:53 -07:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:22:44 -07:00
|
|
|
var iframeText;
|
2014-09-19 18:36:18 -07:00
|
|
|
function measureTextSizes(text, width) {
|
2014-09-26 16:22:44 -07:00
|
|
|
iframeText = iframeText || document.createElement('iframe');
|
|
|
|
document.body.appendChild(iframeText);
|
|
|
|
|
2014-09-19 18:36:18 -07:00
|
|
|
var body = iframeText.contentDocument.body;
|
|
|
|
if (width === undefined || width !== width) {
|
|
|
|
width = Infinity;
|
|
|
|
}
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
var span = document.createElement('span');
|
|
|
|
span.style.display = 'flex';
|
|
|
|
span.style.flexDirection = 'column';
|
|
|
|
span.style.alignItems = 'flex-start';
|
|
|
|
span.innerText = text;
|
|
|
|
|
|
|
|
div.appendChild(span);
|
|
|
|
body.appendChild(div);
|
|
|
|
var rect = span.getBoundingClientRect();
|
|
|
|
body.removeChild(div);
|
|
|
|
return {
|
|
|
|
width: rect.width,
|
|
|
|
height: rect.height
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var texts = {
|
|
|
|
small: 'small',
|
|
|
|
big: 'loooooooooong with space',
|
|
|
|
};
|
|
|
|
|
2014-09-26 20:38:34 -07:00
|
|
|
var preDefinedTextSizes = {
|
|
|
|
smallWidth: 34.671875,
|
|
|
|
smallHeight: 18,
|
|
|
|
bigWidth: 172.421875,
|
|
|
|
bigHeight: 36,
|
|
|
|
bigMinWidth: 100.453125
|
|
|
|
};
|
|
|
|
|
2014-09-26 16:22:44 -07:00
|
|
|
var textSizes;
|
|
|
|
if (typeof require === 'function') {
|
2014-09-26 20:38:34 -07:00
|
|
|
textSizes = preDefinedTextSizes;
|
2014-09-26 16:22:44 -07:00
|
|
|
} else {
|
|
|
|
textSizes = {
|
|
|
|
smallWidth: measureTextSizes(texts.small, 0).width,
|
|
|
|
smallHeight: measureTextSizes(texts.small, 0).height,
|
|
|
|
bigWidth: measureTextSizes(texts.big).width,
|
|
|
|
bigHeight: measureTextSizes(texts.big, 0).height,
|
|
|
|
bigMinWidth: measureTextSizes(texts.big, 0).width,
|
|
|
|
};
|
|
|
|
}
|
2014-09-19 18:22:09 -07:00
|
|
|
|
2014-04-18 16:35:37 -07:00
|
|
|
return {
|
2014-09-19 18:36:18 -07:00
|
|
|
texts: texts,
|
2014-09-19 18:22:09 -07:00
|
|
|
textSizes: textSizes,
|
2014-09-26 20:38:34 -07:00
|
|
|
preDefinedTextSizes: preDefinedTextSizes,
|
2014-04-18 16:35:37 -07:00
|
|
|
testLayout: function(node, expectedLayout) {
|
2014-04-26 12:46:21 -07:00
|
|
|
var layout = computeCSSLayout(node);
|
2014-04-18 16:35:37 -07:00
|
|
|
var domLayout = computeDOMLayout(node);
|
|
|
|
testNamedLayout('expected-dom', expectedLayout, domLayout);
|
|
|
|
testNamedLayout('layout-dom', layout, domLayout);
|
|
|
|
},
|
|
|
|
testRandomLayout: function(node, i) {
|
2014-04-26 12:46:21 -07:00
|
|
|
expect({i: i, node: node, layout: computeCSSLayout(node)})
|
2014-04-18 16:35:37 -07:00
|
|
|
.toEqual({i: i, node: node, layout: computeDOMLayout(node)});
|
2014-04-19 22:08:10 -07:00
|
|
|
},
|
2014-09-25 17:56:02 -07:00
|
|
|
testsFinished: function() {
|
|
|
|
console.log('tests finished!');
|
|
|
|
},
|
2014-04-26 12:46:21 -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-06-04 10:51:23 -07:00
|
|
|
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
|
2014-09-19 18:36:18 -07:00
|
|
|
if (text === texts.small) {
|
2014-09-19 18:22:09 -07:00
|
|
|
return {
|
|
|
|
width: Math.min(textSizes.smallWidth, width),
|
|
|
|
height: textSizes.smallHeight
|
|
|
|
};
|
2014-04-28 12:34:04 -07:00
|
|
|
}
|
2014-09-19 18:36:18 -07:00
|
|
|
if (text === texts.big) {
|
2014-06-04 10:51:23 -07:00
|
|
|
var res = {
|
2014-09-19 18:22:09 -07:00
|
|
|
width: width >= textSizes.bigWidth ? textSizes.bigWidth : Math.max(textSizes.bigMinWidth, width),
|
|
|
|
height: width >= textSizes.bigWidth ? textSizes.smallHeight : textSizes.bigHeight
|
2014-06-04 10:51:23 -07:00
|
|
|
};
|
2014-09-19 18:22:09 -07:00
|
|
|
return res;
|
2014-04-28 12:34:04 -07:00
|
|
|
}
|
2014-04-26 17:11:22 -07:00
|
|
|
};
|
2014-09-19 18:36:18 -07:00
|
|
|
fn.toString = function() { return text; };
|
2014-04-26 17:11:22 -07:00
|
|
|
return fn;
|
|
|
|
}
|
2014-09-26 20:38:34 -07:00
|
|
|
};
|
2014-04-18 16:35:37 -07:00
|
|
|
})();
|
2014-09-26 16:22:44 -07:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = layoutTestUtils;
|
|
|
|
}
|