Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #150 from Galooshi/dont-assume-top-left
Browse files Browse the repository at this point in the history
Don't assume elements start at top left corner
  • Loading branch information
trotzig authored Sep 13, 2016
2 parents df228bd + 3c9f568 commit 5869ca2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/happo/public/happo-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ window.happo = {
}
},

/**
* Wrapper around Math.min to handle undefined values.
*/
min: function min(a, b) {
if (a === undefined) {
return b;
}
return Math.min(a, b);
},

// This function takes a node and a box object that we will mutate.
getFullRectRecursive: function getFullRectRecursive(node, box) {
// Since we are already traversing through every node, let's piggyback on
Expand All @@ -192,9 +202,9 @@ window.happo = {

/* eslint-disable no-param-reassign */
box.bottom = Math.max(box.bottom, rect.bottom);
box.left = Math.min(box.left, rect.left);
box.left = this.min(box.left, rect.left);
box.right = Math.max(box.right, rect.right);
box.top = Math.min(box.top, rect.top);
box.top = this.min(box.top, rect.top);
/* eslint-enable no-param-reassign */

for (var i = 0; i < node.children.length; i++) {
Expand All @@ -221,9 +231,9 @@ window.happo = {
// Set up the initial object that we will mutate in our recursive function.
var box = {
bottom: 0,
left: 0,
left: undefined,
right: 0,
top: 0,
top: undefined,
};

var rootNodes = this.getRootNodes();
Expand Down
22 changes: 22 additions & 0 deletions spec/happo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ def snapshot_file_exists?(description, size, file_name)
end
end

describe 'with an element rendering at the bottom right' do
let(:examples_js) { <<-EOS }
happo.define('#{description}', function() {
var elem = document.createElement('div');
elem.style.position = 'fixed';
elem.style.height = '40px';
elem.style.width = '40px';
elem.style.bottom = '0px';
elem.style.right = '0px';
document.body.appendChild(elem);
}, #{example_config});
EOS

it 'includes the component in the snapshot' do
run_happo
path = snapshot_file_name(description, '@large', 'current.png')
image = ChunkyPNG::Image.from_file(path)
expect(image.width).to eq(40)
expect(image.height).to eq(40)
end
end

describe 'with an overflowing element' do
let(:examples_js) { <<-EOS }
happo.define('#{description}', function() {
Expand Down

0 comments on commit 5869ca2

Please sign in to comment.