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

Defer the initialization of HighlightHandler until win.document becomes ready #20883

Merged
merged 3 commits into from Feb 20, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion extensions/amp-viewer-integration/0.1/highlight-handler.js
Expand Up @@ -22,6 +22,7 @@ import {moveLayoutRect} from '../../../src/layout-rect';
import {parseJson} from '../../../src/json';
import {parseQueryString} from '../../../src/url';
import {resetStyles, setInitialDisplay, setStyles} from '../../../src/style';
import {whenDocumentReady} from '../../../src/document-ready';

/**
* The message name sent by viewers to dismiss highlights.
Expand Down Expand Up @@ -143,7 +144,9 @@ export class HighlightHandler {
/** @private {?Array<!Element>} */
this.highlightedNodes_ = null;

this.initHighlight_(highlightInfo);
whenDocumentReady(ampdoc.win.document).then(() => {
this.initHighlight_(highlightInfo);
});
}

/**
Expand Down
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import * as docready from '../../../../src/document-ready';
import {HighlightHandler, getHighlightParam} from '../highlight-handler';
import {Messaging, WindowPortEmulator} from '../messaging/messaging';
import {Services} from '../../../../src/services';
Expand Down Expand Up @@ -100,6 +101,7 @@ describes.realWin('HighlightHandler', {
},
}, env => {
let root = null;
let docreadyCb = null;
beforeEach(() => {
const {document} = env.win;
root = document.createElement('div');
Expand All @@ -110,6 +112,12 @@ describes.realWin('HighlightHandler', {
const div1 = document.createElement('div');
div1.textContent = 'highlighted text';
root.appendChild(div1);

sandbox.stub(docready, 'whenDocumentReady').returns({
then: cb => {
docreadyCb = cb;
},
});
});

it('initialize with visibility=visible', () => {
Expand All @@ -125,6 +133,12 @@ describes.realWin('HighlightHandler', {
const handler = new HighlightHandler(
ampdoc,{sentences: ['amp', 'highlight']});

// initHighlight_ is not called before document become ready.
expect(handler.highlightedNodes_).to.be.null;
docreadyCb();
// initHighlight_ was called in docreadyCb() and highlightedNodes_ is set.
expect(handler.highlightedNodes_).not.to.be.null;

expect(setScrollTop).to.be.calledOnce;
expect(setScrollTop.firstCall.args.length).to.equal(1);

Expand Down Expand Up @@ -173,6 +187,7 @@ describes.realWin('HighlightHandler', {

new HighlightHandler(ampdoc,
{sentences: ['amp', 'highlight'], skipRendering: true});
docreadyCb();

expect(scrollStub).not.to.be.called;

Expand Down Expand Up @@ -202,6 +217,7 @@ describes.realWin('HighlightHandler', {
Services.viewerForDoc(ampdoc), 'sendMessage');

new HighlightHandler(ampdoc, {sentences: ['amp', 'highlight']});
docreadyCb();

expect(scrollStub).not.to.be.called;

Expand Down Expand Up @@ -230,6 +246,7 @@ describes.realWin('HighlightHandler', {
.returns(VisibilityState.PRERENDER);

new HighlightHandler(ampdoc,{sentences: ['amp', 'highlight']});
docreadyCb();

expect(setScrollTop).to.be.calledOnce;
expect(setScrollTop.firstCall.args.length).to.equal(1);
Expand All @@ -244,6 +261,7 @@ describes.realWin('HighlightHandler', {

it('calcTopToCenterHighlightedNodes_ center elements', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
docreadyCb();
expect(handler.highlightedNodes_).not.to.be.null;

const viewport = Services.viewportForDoc(env.ampdoc);
Expand All @@ -259,6 +277,7 @@ describes.realWin('HighlightHandler', {

it('calcTopToCenterHighlightedNodes_ too tall element', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
docreadyCb();
expect(handler.highlightedNodes_).not.to.be.null;

const viewport = Services.viewportForDoc(env.ampdoc);
Expand All @@ -276,6 +295,7 @@ describes.realWin('HighlightHandler', {

it('mayAdjustTop_', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
docreadyCb();
expect(handler.highlightedNodes_).not.to.be.null;

// Set up an environment where calcTopToCenterHighlightedNodes_
Expand Down