text with padding and margin

This commit is contained in:
Christopher Chedeau
2014-04-26 17:11:22 -07:00
parent 8ca7bd0ccb
commit 61919a1b2f
3 changed files with 41 additions and 28 deletions

View File

@@ -26,12 +26,11 @@ var layoutTestUtils = (function() {
return iframe; return iframe;
})(); })();
var body = iframe.contentDocument.body;
var realComputeLayout = computeLayout; var realComputeLayout = computeLayout;
function computeCSSLayout(rootNode) { function computeCSSLayout(rootNode) {
var body = iframe.contentDocument.body;
function fillNodes(node) { function fillNodes(node) {
node.layout = { node.layout = {
width: undefined, width: undefined,
@@ -43,25 +42,7 @@ var layoutTestUtils = (function() {
node.style = {}; node.style = {};
} }
if ('text' in node.style) { if (!node.children || node.style.measure) {
node.style.measure = function(width) {
var span = document.createElement('span');
span.style.position = 'absolute';
if (width !== undefined) {
span.style.width = width + 'px';
}
span.innerText = node.style.text;
body.appendChild(span);
var rect = span.getBoundingClientRect();
body.removeChild(span);
return {
width: rect.width,
height: rect.height
};
}
}
if (!node.children) {
node.children = []; node.children = [];
} }
node.children.forEach(fillNodes); node.children.forEach(fillNodes);
@@ -109,8 +90,8 @@ var layoutTestUtils = (function() {
(node.children || []).forEach(function(child) { (node.children || []).forEach(function(child) {
renderNode(div, child); renderNode(div, child);
}); });
if (node.style.text) { if (node.style.measure) {
div.innerText = node.style.text; div.innerText = node.style.measure.toString();
} }
return div; return div;
} }
@@ -239,6 +220,25 @@ var layoutTestUtils = (function() {
}, },
computeLayout: computeCSSLayout, computeLayout: computeCSSLayout,
computeDOMLayout: computeDOMLayout, computeDOMLayout: computeDOMLayout,
reduceTest: reduceTest reduceTest: reduceTest,
text: function(text) {
var fn = function(width) {
var span = document.createElement('span');
span.style.position = 'absolute';
if (width !== undefined) {
span.style.width = width + 'px';
}
span.innerText = text;
body.appendChild(span);
var rect = span.getBoundingClientRect();
body.removeChild(span);
return {
width: rect.width,
height: rect.height
};
};
fn.toString = function() { return text; }
return fn;
}
} }
})(); })();

View File

@@ -217,8 +217,12 @@ var computeLayout = (function() {
if ('measure' in node.style) { if ('measure' in node.style) {
var dimensions = node.style.measure(node.style.width); var dimensions = node.style.measure(node.style.width);
node.layout.width = dimensions.width; if (!isDimDefined(node, 'row')) {
node.layout.height = dimensions.height; node.layout.width = dimensions.width + getPaddingAndBorderAxis(node, 'row');
}
if (!isDimDefined(node, 'column')) {
node.layout.height = dimensions.height + getPaddingAndBorderAxis(node, 'column');
}
return; return;
} }

View File

@@ -4,6 +4,7 @@ var testRandomLayout = layoutTestUtils.testRandomLayout;
var computeLayout = layoutTestUtils.computeLayout; var computeLayout = layoutTestUtils.computeLayout;
var computeDOMLayout = layoutTestUtils.computeDOMLayout; var computeDOMLayout = layoutTestUtils.computeDOMLayout;
var reduceTest = layoutTestUtils.reduceTest; var reduceTest = layoutTestUtils.reduceTest;
var text = layoutTestUtils.text;
describe('Layout', function() { describe('Layout', function() {
it('should layout a single node with width and height', function() { it('should layout a single node with width and height', function() {
@@ -746,18 +747,25 @@ describe('Layout', function() {
it('should layout node with text', function() { it('should layout node with text', function() {
testLayout( testLayout(
{style: {text: 'kikoo'}}, {style: {measure: text('kikoo')}},
{width: 36, height: 18, top: 0, left: 0} {width: 36, height: 18, top: 0, left: 0}
) )
}); });
it('should layout node with text and width', function() { it('should layout node with text and width', function() {
testLayout( testLayout(
{style: {text: 'kikoo loool', width: 10}}, {style: {measure: text('kikoo loool'), width: 10}},
{width: 10, height: 36, top: 0, left: 0} {width: 10, height: 36, top: 0, left: 0}
) )
}); });
it('should layout node with padding and margin', function() {
testLayout(
{style: {measure: text('kikoo loool'), padding: 5, margin: 5}},
{width: 82, height: 28, top: 5, left: 5}
)
});
it('should layout randomly', function() { it('should layout randomly', function() {
@@ -816,6 +824,7 @@ describe('Layout', function() {
randEnum(node, 0.5, 'alignSelf', ['flex-start', 'center', 'flex-end', 'stretch']); randEnum(node, 0.5, 'alignSelf', ['flex-start', 'center', 'flex-end', 'stretch']);
randEnum(node, 0.5, 'flex', ['none', 1]); randEnum(node, 0.5, 'flex', ['none', 1]);
randEnum(node, 0.5, 'position', ['relative', 'absolute']); randEnum(node, 0.5, 'position', ['relative', 'absolute']);
randEnum(node, 0.5, 'measure', [text('kikoo'), text('kikooooooooooooo looool')]);
randChildren(node, 0.2); randChildren(node, 0.2);
return node; return node;
} }