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

✨ Text fragments support on amp viewer #34074

Merged
merged 26 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
33 changes: 29 additions & 4 deletions extensions/amp-viewer-integration/0.1/amp-viewer-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {dict} from '../../../src/core/types/object';
import {getAmpdoc} from '../../../src/service';
import {getData, listen, listenOnce} from '../../../src/event-helper';
import {getSourceUrl} from '../../../src/url';
import {isExperimentOn} from '../../../src/experiments';
import {isIframed} from '../../../src/dom';

const TAG = 'amp-viewer-integration';
Expand Down Expand Up @@ -111,10 +112,26 @@ export class AmpViewerIntegration {
}
);
}
/** @type {?HighlightInfoDef} */
const highlightInfo = getHighlightParam(ampdoc);
if (highlightInfo) {
this.highlightHandler_ = new HighlightHandler(ampdoc, highlightInfo);

if (
'fragmentDirective' in document &&
isExperimentOn(this.win, 'enable-text-fragments')
) {
this.win.addEventListener('message', (e) => {
if (
e.origin !== 'https://www.google.com' ||
dmanek marked this conversation as resolved.
Show resolved Hide resolved
e.data?.directive?.length === 0
) {
return;
}
this.updateUrlWithTextFragment_(e.data.directive);
dmanek marked this conversation as resolved.
Show resolved Hide resolved
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
/** @type {?HighlightInfoDef} */
const highlightInfo = getHighlightParam(ampdoc);
if (highlightInfo) {
this.highlightHandler_ = new HighlightHandler(ampdoc, highlightInfo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we just taking advantage of the already coded highlight param, and just using the native fragment handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Perhaps we can use this approach & avoid code changes on GWS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use this approach.

}
}

const port = new WindowPortEmulator(
Expand All @@ -130,6 +147,14 @@ export class AmpViewerIntegration {
);
}

/**
* @param {string} fragment
* @private
*/
updateUrlWithTextFragment_(fragment) {
window.location.replace('#:~:' + fragment);
dmanek marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {?Window} source
* @param {string} origin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import {ViewerForTesting} from '../../viewer-for-testing';
import {done} from 'fetch-mock';
import {getSourceUrl} from '../../../../../src/url';
import {toggleExperiment} from '../../../../../src/experiments';

describes.sandboxed('amp-viewer-integration', {}, () => {
const ampDocSrc = '/test/fixtures/served/ampdoc-with-messaging.html';
Expand Down Expand Up @@ -230,6 +231,79 @@ describes.realWin(
expect(initFocusHandlerStub).to.be.called;
});
});

// TODO(dmanek): remove `ifChrome` once other major browsers support
// text fragments (i.e. 'fragmentDirective' in document = true)
it.configure()
.ifChrome()
.run(
'should update url based on text fragment post message',
async () => {
toggleExperiment(win, 'enable-text-fragments', true, true);

const updateUrlWithTextFragmentSpy = env.sandbox.spy();
ampViewerIntegration.updateUrlWithTextFragment_ = updateUrlWithTextFragmentSpy;

const text = 'will this higlight?';
const message = {
directive: text,
};

const mockEvent = new CustomEvent('message');
mockEvent.origin = 'https://www.google.com';
mockEvent.data = message;

ampViewerIntegration.init();

win.dispatchEvent(mockEvent);
expect(updateUrlWithTextFragmentSpy.calledOnce).to.be.true;
expect(updateUrlWithTextFragmentSpy.getCall(0).args[0]).to.equal(
text
);
}
);

it.configure()
.ifChrome()
.run('should not update url if origin it not google.com', async () => {
toggleExperiment(win, 'enable-text-fragments', true, true);

const updateUrlWithTextFragmentSpy = env.sandbox.spy();
ampViewerIntegration.updateUrlWithTextFragment_ = updateUrlWithTextFragmentSpy;

ampViewerIntegration.init();

const message = {
directive: 'will this highglight?',
};
const mockEvent = new CustomEvent('message');
mockEvent.origin = 'https://www.goøgle.com';
dmanek marked this conversation as resolved.
Show resolved Hide resolved
mockEvent.data = message;

win.dispatchEvent(mockEvent);
expect(updateUrlWithTextFragmentSpy.callCount).to.equal(0);
});

it.configure()
.ifChrome()
.run('should not update url if text fragment is empty', async () => {
toggleExperiment(win, 'enable-text-fragments', true, true);

const updateUrlWithTextFragmentSpy = env.sandbox.spy();
ampViewerIntegration.updateUrlWithTextFragment_ = updateUrlWithTextFragmentSpy;

ampViewerIntegration.init();

const message = {
directive: '',
};
const mockEvent = new CustomEvent('message');
mockEvent.origin = 'https://www.google.com';
mockEvent.data = message;

win.dispatchEvent(mockEvent);
expect(updateUrlWithTextFragmentSpy.callCount).to.equal(0);
});
});
}
);
Expand Down