Skip to content

Commit

Permalink
fix shadow doc navigation interception (#32249)
Browse files Browse the repository at this point in the history
* fix shadow doc navigation

* unit test
  • Loading branch information
samouri committed Jan 28, 2021
1 parent 8cac803 commit 271235d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/service/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,11 @@ export class Navigation {
const viewerHasCapability = this.viewer_.hasCapability(
'interceptNavigation'
);
const docOptedIn = this.ampdoc
.getRootNode()
.documentElement.hasAttribute('allow-navigation-interception');
const docOptedIn =
this.ampdoc.isSingleDoc() &&
this.ampdoc
.getRootNode()
.documentElement.hasAttribute('allow-navigation-interception');

if (
!viewerHasCapability ||
Expand Down
22 changes: 22 additions & 0 deletions test/unit/test-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,28 @@ describes.sandboxed('Navigation', {}, () => {
expect(event.defaultPrevented).to.be.false;
});

it('should not intercept requests a shadow doc', () => {
handler.isTrustedViewer_ = true;
handler.ampdoc.isSingleDoc = () => false;
// isSingleDoc affects where the services get stored. So stub the getters.
env.sandbox
.stub(Services, 'urlForDoc')
.returns(handler.ampdoc.win.__AMP_SERVICES.url.obj);
env.sandbox
.stub(Services, 'viewerForDoc')
.returns(handler.ampdoc.win.__AMP_SERVICES.viewer.obj);

handler.handle_(event);

expect(viewerInterceptsNavigationSpy).to.be.calledOnce;
expect(viewerInterceptsNavigationSpy).to.be.calledWithExactly(
'https://www.google.com/other',
'intercept_click'
);
expect(sendMessageStub).to.not.be.called;
expect(event.defaultPrevented).to.be.false;
});

it('should require opted in ampdoc', () => {
ampdoc
.getRootNode()
Expand Down

0 comments on commit 271235d

Please sign in to comment.