Skip to content

Commit

Permalink
Fix bugs in layout.js. Add tests for Element.Layout#toCSS, `#toObje…
Browse files Browse the repository at this point in the history
…ct`, and `#toHash`.
  • Loading branch information
savetheclocktower committed Apr 23, 2010
1 parent 08a6188 commit eb26357
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
Fix bugs in layout.js. Add tests for `Element.Layout#toCSS`, `#toObject`, and `#toHash`. (RStankov, Andrew Dupont)

Add `Element.Layout#toObject` and `Element.Layout.toHash`. (Andrew Dupont)

Make `Element.Layout#toCSS` return camelized property names, as expected by `Element.setStyle`. [#1021 state:resolved] (njakobsen, Andrew Dupont)
Expand Down
15 changes: 8 additions & 7 deletions src/dom/layout.js
Expand Up @@ -94,7 +94,7 @@
// Converts the layout hash property names back to the CSS equivalents.
// For now, only the border properties differ.
function cssNameFor(key) {
if (key.includes('border')) key = key + '-width';
if (key.include('border')) key = key + '-width';
return key.camelize();
}

Expand Down Expand Up @@ -358,7 +358,7 @@
if (!Element.Layout.PROPERTIES.include(key)) return;
var value = this.get(key);
if (value != null) obj[key] = value;
});
}, this);
return obj;
},

Expand Down Expand Up @@ -398,6 +398,7 @@
var keys = (args.length === 0) ? Element.Layout.PROPERTIES :
args.join(' ').split(' ');
var css = {};

keys.each( function(key) {
// Key needs to be a valid Element.Layout property...
if (!Element.Layout.PROPERTIES.include(key)) return;
Expand All @@ -407,8 +408,8 @@
var value = this.get(key);
// Unless the value is null, add 'px' to the end and add it to the
// returned object.
if (value) css[cssNameFor(key)] = value + 'px';
});
if (value != null) css[cssNameFor(key)] = value + 'px';
}, this);
return css;
},

Expand Down Expand Up @@ -443,12 +444,12 @@

var bTop = this.get('border-top'),
bBottom = this.get('border-bottom');

var pTop = this.get('padding-top'),
pBottom = this.get('padding-bottom');

if (!this._preComputing) this._end();

return bHeight - bTop - bBottom - pTop - pBottom;
},

Expand Down
23 changes: 21 additions & 2 deletions test/unit/layout_test.js
Expand Up @@ -12,8 +12,6 @@ function isDisplayed(element) {
}

new Test.Unit.Runner({
setup: function() {
},
'test preCompute argument of layout': function() {
var preComputedLayout = $('box1').getLayout(true),
normalLayout = $('box1').getLayout();
Expand Down Expand Up @@ -95,5 +93,26 @@ new Test.Unit.Runner({

this.assertEqual(0, layout.get('top'), 'top');
this.assertIdentical($('box6_parent'), $('box6').getOffsetParent());
},

'test #toCSS, #toObject, #toHash': function() {
var layout = $('box6').getLayout();
var top = layout.get('top');

var cssObject = layout.toCSS('top');

this.assert('top' in cssObject,
"layout object should have 'top' property");

cssObject = layout.toCSS('top left bottom');

$w('top left bottom').each( function(prop) {
this.assert(prop in cssObject, "layout object should have '" +
prop + "' property");
}, this);

var obj = layout.toObject('top');
this.assert('top' in obj,
"object should have 'top' property");
}
});

0 comments on commit eb26357

Please sign in to comment.