Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used cached doc position in getLayoutRect. #2547

Merged
merged 1 commit into from
Mar 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/service/viewport-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ export class Viewport {
* @return {!LayoutRect}
*/
getLayoutRect(el) {
return this.binding_.getLayoutRect(el);
return this.binding_.getLayoutRect(el,
this.getScrollLeft(), this.getScrollTop());
}

/**
Expand Down Expand Up @@ -577,9 +578,13 @@ class ViewportBindingDef {
/**
* Returns the rect of the element within the document.
* @param {!Element} unusedEl
* @param {number=} unusedScrollLeft Optional arguments that the caller may
* pass in, if they cached these values and would like to avoid
* remeasure. Requires appropriate updating the values on scroll.
* @param {number=} unusedScrollTop Same comment as above.
* @return {!LayoutRect}
*/
getLayoutRect(unusedEl) {}
getLayoutRect(unusedEl, unusedScrollLeft, unusedScrollTop) {}

/** For testing. */
cleanup_() {}
Expand Down Expand Up @@ -686,9 +691,13 @@ export class ViewportBindingNatural_ {
}

/** @override */
getLayoutRect(el) {
const scrollTop = this.getScrollTop();
const scrollLeft = this.getScrollLeft();
getLayoutRect(el, opt_scrollLeft, opt_scrollTop) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment to onScroll to explain that cached value must be updated or invalidated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const scrollTop = opt_scrollTop != undefined
? opt_scrollTop
: this.getScrollTop();
const scrollLeft = opt_scrollLeft != undefined
? opt_scrollLeft
: this.getScrollLeft();
const b = el./*OK*/getBoundingClientRect();
return layoutRectLtwh(Math.round(b.left + scrollLeft),
Math.round(b.top + scrollTop),
Expand Down
26 changes: 26 additions & 0 deletions test/functional/test-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ describe('Viewport', () => {
viewport.scrollIntoView(element);
});

it('should send cached scroll pos to getLayoutRect', () => {
const element = document.createElement('div');
const bindingMock = sandbox.mock(binding);
viewport.scrollTop_ = 111;
viewport.scrollLeft_ = 222;
bindingMock.expects('getLayoutRect').withArgs(element, 222, 111)
.returns('sentinel').once();
expect(viewport.getLayoutRect(element)).to.equal('sentinel');
});

it('should deletegate scrollWidth', () => {
const bindingMock = sandbox.mock(binding);
bindingMock.expects('getScrollWidth').withArgs().returns(111).once();
Expand Down Expand Up @@ -601,6 +611,22 @@ describe('ViewportBindingNatural', () => {
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});

it('should offset client rect for layout and position passed in', () => {
windowApi.pageXOffset = 1000;
windowApi.pageYOffset = 2000;
windowApi.document = {scrollingElement: {}};
const el = {
getBoundingClientRect: () => {
return {left: 11.5, top: 12.5, width: 13.5, height: 14.5};
},
};
const rect = binding.getLayoutRect(el, 100, 200);
expect(rect.left).to.equal(112); // round(100 + 11.5)
expect(rect.top).to.equal(213); // round(200 + 12.5)
expect(rect.width).to.equal(14); // round(13.5)
expect(rect.height).to.equal(15); // round(14.5)
});
});


Expand Down