From 8d5ce77f5d7efe6959ad4aedd4cd02f4f9df4462 Mon Sep 17 00:00:00 2001 From: Yuxuan Zhou Date: Fri, 7 Oct 2016 11:13:30 -0700 Subject: [PATCH] ALP for A4A (#5430) --- ads/alp/handler.js | 13 ++++++++++--- build-system/dep-check-config.js | 3 +++ extensions/amp-a4a/0.1/amp-a4a.js | 16 +++++++++++++++- test/functional/test-alp-handler.js | 15 +++++++++++++++ tools/experiments/experiments.js | 5 +++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ads/alp/handler.js b/ads/alp/handler.js index a6d5b31fa71b0..fc8753dfa5ca3 100644 --- a/ads/alp/handler.js +++ b/ads/alp/handler.js @@ -52,9 +52,10 @@ export function installAlpClickHandler(win) { * Filter click event and then transform URL for direct AMP navigation * with impression logging. * @param {!Event} e + * @param {function(string)=} opt_viewerNavigate * @visibleForTesting */ -export function handleClick(e) { +export function handleClick(e, opt_viewerNavigate) { if (e.defaultPrevented) { return; } @@ -91,9 +92,15 @@ export function handleClick(e) { destination = destination.replace(`${urls.cdn}/c/`, 'http://localhost:8000/max/'); } - e.preventDefault(); - navigateTo(win, link.a, destination); + if (opt_viewerNavigate) { + // TODO: viewer navigate only support navigating top level window to + // destination. should we try to open a new window here with target=_blank + // here instead of using viewer navigation. + opt_viewerNavigate(destination); + } else { + navigateTo(win, link.a, destination); + } } /** diff --git a/build-system/dep-check-config.js b/build-system/dep-check-config.js index 33752aa1d49a4..56d1ed7546764 100644 --- a/build-system/dep-check-config.js +++ b/build-system/dep-check-config.js @@ -90,6 +90,9 @@ exports.rules = [ 'ads/google/a4a/**->src/timer.js', 'ads/google/a4a/**->src/viewer.js', 'ads/google/a4a/**->src/viewport.js', + // alp handler needs to depend on src files + 'ads/alp/handler.js->src/dom.js', + 'ads/alp/handler.js->src/config.js', ], }, { diff --git a/extensions/amp-a4a/0.1/amp-a4a.js b/extensions/amp-a4a/0.1/amp-a4a.js index 481f92d164099..1a918a91d5148 100644 --- a/extensions/amp-a4a/0.1/amp-a4a.js +++ b/extensions/amp-a4a/0.1/amp-a4a.js @@ -39,6 +39,8 @@ import { verifySignature, PublicKeyInfoDef, } from './crypto-verifier'; +import {isExperimentOn} from '../../../src/experiments'; +import {handleClick} from '../../../ads/alp/handler'; /** * Dev public key set. This will go away once the dev signing service goes live. @@ -665,7 +667,8 @@ export class AmpA4A extends AMP.BaseElement { html: modifiedCreative, extensionIds: creativeMetaData.customElementExtensions || [], fonts: fontsArray, - }).then(() => { + }).then(friendlyIframeEmbed => { + this.registerAlpHandler_(friendlyIframeEmbed.win); this.rendered_ = true; this.onAmpCreativeRender(); return true; @@ -831,4 +834,15 @@ export class AmpA4A extends AMP.BaseElement { metaData.cssUtf16CharOffsets[0], metaData.cssUtf16CharOffsets[1]); } + + registerAlpHandler_(iframeWin) { + if (!isExperimentOn(this.win, 'alp-for-a4a')) { + return; + } + iframeWin.document.documentElement.addEventListener('click', event => { + handleClick(event, url => { + viewerForDoc(this.getAmpDoc()).navigateTo(url, 'a4a'); + }); + }); + } } diff --git a/test/functional/test-alp-handler.js b/test/functional/test-alp-handler.js index 7ed40659d79c2..0c4a4a032bc2e 100644 --- a/test/functional/test-alp-handler.js +++ b/test/functional/test-alp-handler.js @@ -192,6 +192,21 @@ describe('alp-handler', () => { simpleSuccess(); }); + it('should perform special navigation if specially asked for', () => { + const navigateSpy = sandbox.spy(); + const opt_navigate = val => { + navigateSpy(); + expect(val).to.equal( + 'https://cdn.ampproject.org/c/www.example.com/amp.html#click=' + + 'https%3A%2F%2Ftest.com%3Famp%3D1%26adurl%3Dhttps%253A%252F%252F' + + 'cdn.ampproject.org%252Fc%252Fwww.example.com%252Famp.html'); + }; + handleClick(event, opt_navigate); + expect(event.preventDefault).to.be.calledOnce; + expect(open).to.not.be.called; + expect(navigateSpy).to.be.calledOnce; + }); + it('should navigate if trusted is not set.', () => { delete event.trusted; simpleSuccess(); diff --git a/tools/experiments/experiments.js b/tools/experiments/experiments.js index b793f75ebf2bf..91ecc4fa0adfe 100644 --- a/tools/experiments/experiments.js +++ b/tools/experiments/experiments.js @@ -189,6 +189,11 @@ const EXPERIMENTS = [ spec: 'https://github.com/ampproject/amphtml/issues/4820', cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4894', }, + { + id: 'alp-for-a4a', + name: 'Enable redirect to landing page directly for A4A', + spec: 'https://github.com/ampproject/amphtml/issues/5212', + }, ]; if (getMode().localDev) {