Skip to content

Commit

Permalink
Fix Sidebar navigations being cancelled. (#6590)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhatib authored and erwinmombay committed Dec 9, 2016
1 parent 7762cb5 commit e5ac7ca
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 57 deletions.
18 changes: 17 additions & 1 deletion extensions/amp-sidebar/0.1/amp-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {dev} from '../../../src/log';
import {historyForDoc} from '../../../src/history';
import {platformFor} from '../../../src/platform';
import {setStyles, toggle} from '../../../src/style';
import {removeFragment, parseUrl} from '../../../src/url';
import {vsyncFor} from '../../../src/vsync';
import {timerFor} from '../../../src/timer';

Expand Down Expand Up @@ -132,10 +133,25 @@ export class AmpSidebar extends AMP.BaseElement {
this.registerAction('open', this.open_.bind(this));
this.registerAction('close', this.close_.bind(this));

// TODO(mkhatib, #6589): Consider exposing onLocalNavigation from
// document-click service to simplifiy this.
this.element.addEventListener('click', e => {
const target = closestByTag(dev().assertElement(e.target), 'A');
if (target && target.href) {
this.close_();
const tgtLoc = parseUrl(target.href);
const currentHref = this.getAmpDoc().win.location.href;
// Important: Only close sidebar (and hence pop sidebar history entry)
// when navigating locally, Chrome might cancel navigation request
// due to after-navigation history manipulation inside a timer callback.
// See this issue for more details:
// https://github.com/ampproject/amphtml/issues/6585
if (removeFragment(target.href) != removeFragment(currentHref)) {
return;
}

if (tgtLoc.hash) {
this.close_();
}
}
}, true);
}
Expand Down
209 changes: 153 additions & 56 deletions extensions/amp-sidebar/0.1/test/test-amp-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,91 @@ describe('amp-sidebar', () => {
});
});

it('should reflect state of the sidebar', () => {
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const impl = sidebarElement.implementation_;
impl.schedulePause = sandbox.spy();
impl.scheduleResume = sandbox.spy();
impl.vsync_ = {
mutate: function(callback) {
callback();
},
};
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(0);
expect(impl.scheduleResume.callCount).to.equal(0);
impl.toggle_();
expect(impl.isOpen_()).to.be.true;
expect(impl.schedulePause.callCount).to.equal(0);
expect(impl.scheduleResume.callCount).to.equal(1);
impl.toggle_();
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(1);
expect(impl.scheduleResume.callCount).to.equal(1);
impl.toggle_();
expect(impl.isOpen_()).to.be.true;
expect(impl.schedulePause.callCount).to.equal(1);
expect(impl.scheduleResume.callCount).to.equal(2);
impl.toggle_();
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(2);
expect(impl.scheduleResume.callCount).to.equal(2);
});
});

it.skip('should fix scroll leaks on ios safari', () => {
sandbox.stub(platform, 'isIos').returns(true);
sandbox.stub(platform, 'isSafari').returns(true);
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const impl = sidebarElement.implementation_;
impl.vsync_ = {
mutate: function(callback) {
callback();
},
};
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
const scrollLeakSpy = sandbox.spy(impl, 'fixIosElasticScrollLeak_');
impl.buildCallback();
expect(scrollLeakSpy.callCount).to.equal(1);
});
});

it.skip('should adjust for IOS safari bottom bar', () => {
sandbox.stub(platform, 'isIos').returns(true);
sandbox.stub(platform, 'isSafari').returns(true);
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const impl = sidebarElement.implementation_;
impl.vsync_ = {
mutate: function(callback) {
callback();
},
};
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
const compensateIosBottombarSpy =
sandbox.spy(impl, 'compensateIosBottombar_');
const initalChildrenCount = sidebarElement.children.length;
impl.open_();
expect(compensateIosBottombarSpy.callCount).to.equal(1);
// 10 lis + one top padding element inserted
expect(sidebarElement.children.length).to.equal(initalChildrenCount + 1);
});
});

it('should close sidebar if clicked on anchor', () => {
it('should close sidebar if clicked on a non-local anchor', () => {
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const anchor = sidebarElement.getElementsByTagName('a')[0];
anchor.href = '#newloc';
const impl = sidebarElement.implementation_;
impl.schedulePause = sandbox.spy();
impl.vsync_ = {
Expand All @@ -306,6 +386,15 @@ describe('amp-sidebar', () => {
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
location: {
href: window.location.href,
},
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
Expand All @@ -316,11 +405,11 @@ describe('amp-sidebar', () => {
});
});


it('should not close sidebar if clicked on non-anchor', () => {
it('should not close sidebar if clicked on a new origin navigation', () => {
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const li = sidebarElement.getElementsByTagName('li')[0];
const anchor = sidebarElement.getElementsByTagName('a')[0];
anchor.href = '#newloc';
const impl = sidebarElement.implementation_;
impl.schedulePause = sandbox.spy();
impl.vsync_ = {
Expand All @@ -340,22 +429,33 @@ describe('amp-sidebar', () => {
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
li.dispatchEvent ?
li.dispatchEvent(eventObj) :
li.fireEvent('onkeydown', eventObj);
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
location: {
// Mocking navigating from example.com -> localhost:9876
href: 'http://example.com',
},
},
};
});
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
expect(impl.schedulePause.callCount).to.equal(0);
});
});

it('should reflect state of the sidebar', () => {
it('should not close sidebar if clicked on new page navigation', () => {
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const anchor = sidebarElement.getElementsByTagName('a')[0];
anchor.href = '#newloc';
const impl = sidebarElement.implementation_;
impl.schedulePause = sandbox.spy();
impl.scheduleResume = sandbox.spy();
impl.vsync_ = {
mutate: function(callback) {
callback();
Expand All @@ -364,54 +464,42 @@ describe('amp-sidebar', () => {
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(0);
expect(impl.scheduleResume.callCount).to.equal(0);
impl.toggle_();
expect(impl.isOpen_()).to.be.true;
expect(impl.schedulePause.callCount).to.equal(0);
expect(impl.scheduleResume.callCount).to.equal(1);
impl.toggle_();
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(1);
expect(impl.scheduleResume.callCount).to.equal(1);
impl.toggle_();
expect(impl.isOpen_()).to.be.true;
expect(impl.schedulePause.callCount).to.equal(1);
expect(impl.scheduleResume.callCount).to.equal(2);
impl.toggle_();
expect(impl.isOpen_()).to.be.false;
expect(impl.schedulePause.callCount).to.equal(2);
expect(impl.scheduleResume.callCount).to.equal(2);
});
});

it.skip('should fix scroll leaks on ios safari', () => {
sandbox.stub(platform, 'isIos').returns(true);
sandbox.stub(platform, 'isSafari').returns(true);
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const impl = sidebarElement.implementation_;
impl.vsync_ = {
mutate: function(callback) {
callback();
},
};
sandbox.stub(timer, 'delay', function(callback) {
callback();
expect(sidebarElement.hasAttribute('open')).to.be.false;
impl.open_();
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
sandbox.stub(sidebarElement, 'getAmpDoc', () => {
return {
win: {
location: {
// Mocking navigating from
// /context.html?old=context -> /context.html
href: 'http://localhost:9876/context.html?old=context',
},
},
};
});
const scrollLeakSpy = sandbox.spy(impl, 'fixIosElasticScrollLeak_');
impl.buildCallback();
expect(scrollLeakSpy.callCount).to.equal(1);
anchor.dispatchEvent ?
anchor.dispatchEvent(eventObj) :
anchor.fireEvent('onkeydown', eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
expect(impl.schedulePause.callCount).to.equal(0);
});
});

it.skip('should adjust for IOS safari bottom bar', () => {
sandbox.stub(platform, 'isIos').returns(true);
sandbox.stub(platform, 'isSafari').returns(true);
it('should not close sidebar if clicked on non-anchor', () => {
return getAmpSidebar().then(obj => {
const sidebarElement = obj.ampSidebar;
const li = sidebarElement.getElementsByTagName('li')[0];
const impl = sidebarElement.implementation_;
impl.schedulePause = sandbox.spy();
impl.vsync_ = {
mutate: function(callback) {
callback();
Expand All @@ -420,13 +508,22 @@ describe('amp-sidebar', () => {
sandbox.stub(timer, 'delay', function(callback) {
callback();
});
const compensateIosBottombarSpy =
sandbox.spy(impl, 'compensateIosBottombar_');
const initalChildrenCount = sidebarElement.children.length;
expect(sidebarElement.hasAttribute('open')).to.be.false;
impl.open_();
expect(compensateIosBottombarSpy.callCount).to.equal(1);
// 10 lis + one top padding element inserted
expect(sidebarElement.children.length).to.equal(initalChildrenCount + 1);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
const eventObj = document.createEventObject ?
document.createEventObject() : document.createEvent('Events');
if (eventObj.initEvent) {
eventObj.initEvent('click', true, true);
}
li.dispatchEvent ?
li.dispatchEvent(eventObj) :
li.fireEvent('onkeydown', eventObj);
expect(sidebarElement.hasAttribute('open')).to.be.true;
expect(sidebarElement.getAttribute('aria-hidden')).to.equal('false');
expect(sidebarElement.style.display).to.equal('');
expect(impl.schedulePause.callCount).to.equal(0);
});
});
});

0 comments on commit e5ac7ca

Please sign in to comment.