Skip to content

Commit 6bebd80

Browse files
yunabeaghassemi
authored andcommitted
🐛 Defer the initialization of HighlightHandler until win.document becomes ready. (ampproject#20883)
1 parent 99a01d2 commit 6bebd80

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

extensions/amp-viewer-integration/0.1/highlight-handler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {moveLayoutRect} from '../../../src/layout-rect';
2222
import {parseJson} from '../../../src/json';
2323
import {parseQueryString} from '../../../src/url';
2424
import {resetStyles, setInitialDisplay, setStyles} from '../../../src/style';
25+
import {whenDocumentReady} from '../../../src/document-ready';
2526

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

146-
this.initHighlight_(highlightInfo);
147+
whenDocumentReady(ampdoc.win.document).then(() => {
148+
this.initHighlight_(highlightInfo);
149+
});
147150
}
148151

149152
/**

extensions/amp-viewer-integration/0.1/test/test-highlight-handler.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as docready from '../../../../src/document-ready';
1718
import {HighlightHandler, getHighlightParam} from '../highlight-handler';
1819
import {Messaging, WindowPortEmulator} from '../messaging/messaging';
1920
import {Services} from '../../../../src/services';
@@ -100,6 +101,7 @@ describes.realWin('HighlightHandler', {
100101
},
101102
}, env => {
102103
let root = null;
104+
let docreadyCb = null;
103105
beforeEach(() => {
104106
const {document} = env.win;
105107
root = document.createElement('div');
@@ -110,6 +112,12 @@ describes.realWin('HighlightHandler', {
110112
const div1 = document.createElement('div');
111113
div1.textContent = 'highlighted text';
112114
root.appendChild(div1);
115+
116+
sandbox.stub(docready, 'whenDocumentReady').returns({
117+
then: cb => {
118+
docreadyCb = cb;
119+
},
120+
});
113121
});
114122

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

136+
// initHighlight_ is not called before document become ready.
137+
expect(handler.highlightedNodes_).to.be.null;
138+
docreadyCb();
139+
// initHighlight_ was called in docreadyCb() and highlightedNodes_ is set.
140+
expect(handler.highlightedNodes_).not.to.be.null;
141+
128142
expect(setScrollTop).to.be.calledOnce;
129143
expect(setScrollTop.firstCall.args.length).to.equal(1);
130144

@@ -173,6 +187,7 @@ describes.realWin('HighlightHandler', {
173187

174188
new HighlightHandler(ampdoc,
175189
{sentences: ['amp', 'highlight'], skipRendering: true});
190+
docreadyCb();
176191

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

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

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

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

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

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

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

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

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

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

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

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

281301
// Set up an environment where calcTopToCenterHighlightedNodes_

0 commit comments

Comments
 (0)