Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #273 from arv/scroll-top-setter
Browse files Browse the repository at this point in the history
Make sure we handle both getter and setter for scrollTop etc
  • Loading branch information
arv committed Oct 15, 2013
2 parents 8842682 + 80b8a47 commit 12db94a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/wrappers/HTMLElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@
}
});

function getterRequiresRendering(name) {
defineGetter(HTMLElement, name, function() {
function getter(name) {
return function() {
scope.renderAllPending();
return this.impl[name];
});
};
}

function getterRequiresRendering(name) {
defineGetter(HTMLElement, name, getter(name));
}

[
Expand All @@ -147,11 +151,26 @@
'offsetTop',
'offsetWidth',
'scrollHeight',
'scrollLeft',
'scrollTop',
'scrollWidth',
].forEach(getterRequiresRendering);

function getterAndSetterRequiresRendering(name) {
Object.defineProperty(HTMLElement.prototype, name, {
get: getter(name),
set: function(v) {
scope.renderAllPending();
this.impl[name] = v;
},
configurable: true,
enumerable: true
});
}

[
'scrollLeft',
'scrollTop',
].forEach(getterAndSetterRequiresRendering);

function methodRequiresRendering(name) {
Object.defineProperty(HTMLElement.prototype, name, {
value: function() {
Expand Down
83 changes: 83 additions & 0 deletions test/js/HTMLElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2013 The Polymer Authors. All rights reserved.
* Use of this source code is goverened by a BSD-style
* license that can be found in the LICENSE file.
*/

suite('HTMLElement', function() {

var div;

setup(function() {
div = document.body.appendChild(document.createElement('div'));
div.style.cssText =
'width: 100px; height: 100px; overflow: scroll;' +
'position: absolute; top: 100px; left: 100px;' +
'border: 10px solid red';
var sr = div.createShadowRoot();
var div2 = sr.appendChild(document.createElement('div'));
div2.style.cssText = 'width: 1000px; height: 1000px';
});

teardown(function() {
if (div && div.parentNode)
div.parentNode.removeChild(div);
div = undefined;
});

test('scrollTop', function() {
assert.equal(div.scrollTop, 0);
div.scrollTop = 100;
assert.equal(div.scrollTop, 100);
});

test('scrollLeft', function() {
assert.equal(div.scrollLeft, 0);
div.scrollLeft = 100;
assert.equal(div.scrollLeft, 100);
});

test('scrollHeight', function() {
assert.equal(div.scrollHeight, 1000);
});

test('scrollWidth', function() {
assert.equal(div.scrollHeight, 1000);
});

test('clientHeight', function() {
div.style.overflow = 'hidden';
assert.equal(div.clientHeight, 100);
});

test('clientLeft', function() {
div.style.overflow = 'hidden';
assert.equal(div.clientLeft, 10);
});

test('clientTop', function() {
assert.equal(div.clientTop, 10);
});

test('clientWidth', function() {
div.style.overflow = 'hidden';
assert.equal(div.clientWidth, 100);
});

test('offsetHeight', function() {
assert.equal(div.offsetHeight, 120);
});

test('offsetLeft', function() {
assert.equal(div.offsetLeft, 100);
});

test('offsetTop', function() {
assert.equal(div.offsetTop, 100);
});

test('offsetWidth', function() {
assert.equal(div.offsetWidth, 120);
});

});
1 change: 1 addition & 0 deletions test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var modules = [
'HTMLButtonElement.js',
'HTMLCanvasElement.js',
'HTMLContentElement.js',
'HTMLElement.js',
'HTMLFieldSetElement.js',
'HTMLHeadElement.js',
'HTMLHtmlElement.js',
Expand Down

0 comments on commit 12db94a

Please sign in to comment.