Skip to content

Commit

Permalink
šŸ› Defer the initialization of HighlightHandler until win.document becā€¦
Browse files Browse the repository at this point in the history
ā€¦omes ready. (ampproject#20883)
  • Loading branch information
yunabe authored and Noran Azmy committed Mar 22, 2019
1 parent 8dcf228 commit a09f268
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit a09f268

Please sign in to comment.