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

Horizontal scrolling when embedded as an IFrame #1649

Merged
merged 4 commits into from
Jan 29, 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
9 changes: 8 additions & 1 deletion css/amp.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@
* limitations under the License.
*/

/**
* Horizontal scrolling interferes with embedded scenarios and predominantly
* the result of the non-responsive design.
*/
html, body {
overflow-x: hidden !important;
}

/**
* Margin:0 is currently needed for iOS viewer embeds.
* See:
* https://github.com/ampproject/amphtml/blob/master/spec/amp-html-layout.md
* and {@link ViewportBindingNaturalIosEmbed_} for more info.
*/
body {
/* Undoes mandatory opacity: 0 in doc. */
margin: 0 !important;
}

Expand Down
13 changes: 4 additions & 9 deletions src/service/viewport-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,6 @@ export class ViewportBindingNaturalIosEmbed_ {
/** @const {!Window} */
this.win = win;

/** @private {number} */
this.scrollWidth_ = 0;

/** @private {?Element} */
this.scrollPosEl_ = null;

Expand Down Expand Up @@ -713,9 +710,6 @@ export class ViewportBindingNaturalIosEmbed_ {
const documentElement = this.win.document.documentElement;
const documentBody = this.win.document.body;

// TODO(dvoytenko): need to also find a way to do this on resize.
this.scrollWidth_ = documentBody./*OK*/scrollWidth || 0;

// Embedded scrolling on iOS is rather complicated. IFrames cannot be sized
// and be scrollable. Sizing iframe by scrolling height has a big negative
// that "fixed" position is essentially impossible. The only option we
Expand All @@ -731,11 +725,11 @@ export class ViewportBindingNaturalIosEmbed_ {
// -webkit-overflow-scrolling: touch;
// }
setStyles(documentElement, {
overflow: 'auto',
overflowY: 'auto',
webkitOverflowScrolling: 'touch'
});
setStyles(documentBody, {
overflow: 'auto',
overflowY: 'auto',
webkitOverflowScrolling: 'touch',
position: 'absolute',
top: 0,
Expand Down Expand Up @@ -837,7 +831,8 @@ export class ViewportBindingNaturalIosEmbed_ {

/** @override */
getScrollWidth() {
return Math.max(this.scrollWidth_, this.win./*OK*/innerWidth);
// There's no good way to calculate scroll width on iOS in this mode.
return this.win./*OK*/innerWidth;
}

/** @override */
Expand Down
22 changes: 10 additions & 12 deletions test/functional/test-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ describe('ViewportBindingNatural', () => {
let windowMock;
let binding;
let windowApi;
let documentElement;
let windowEventHandlers;

beforeEach(() => {
Expand All @@ -455,6 +456,12 @@ describe('ViewportBindingNatural', () => {
windowEventHandlers[eventType] = handler;
};
windowApi = new WindowApi();
documentElement = {
style: {}
};
windowApi.document = {
documentElement: documentElement
};
windowMock = sandbox.mock(windowApi);
binding = new ViewportBindingNatural_(windowApi);
});
Expand Down Expand Up @@ -625,25 +632,16 @@ describe('ViewportBindingNaturalIosEmbed', () => {
expect(bodyEventListeners['scroll']).to.not.equal(undefined);
});

it('should pre-calculate scrollWidth', () => {
expect(binding.scrollWidth_).to.equal(777);
});

it('should defer scrollWidth to max of window.innerHeight ' +
' and body.scrollWidth', () => {
binding.scrollWidth_ = 0;
it('should always have scrollWidth equal window.innerWidth', () => {
expect(binding.getScrollWidth()).to.equal(555);

binding.scrollWidth_ = 1000;
expect(binding.getScrollWidth()).to.equal(1000);
});

it('should setup document for embed scrolling', () => {
const documentElement = windowApi.document.documentElement;
const body = windowApi.document.body;
expect(documentElement.style.overflow).to.equal('auto');
expect(documentElement.style.overflowY).to.equal('auto');
expect(documentElement.style.webkitOverflowScrolling).to.equal('touch');
expect(body.style.overflow).to.equal('auto');
expect(body.style.overflowY).to.equal('auto');
expect(body.style.webkitOverflowScrolling).to.equal('touch');
expect(body.style.position).to.equal('absolute');
expect(body.style.top).to.equal(0);
Expand Down