diff --git a/extensions/amp-list/0.1/amp-list.js b/extensions/amp-list/0.1/amp-list.js index 58c917e1e9f71..4e6da408d3b7b 100644 --- a/extensions/amp-list/0.1/amp-list.js +++ b/extensions/amp-list/0.1/amp-list.js @@ -263,6 +263,10 @@ export class AmpList extends AMP.BaseElement { this.initializeLoadMoreElements_(); } + if (this.isAmpStateSrc_(this.element.getAttribute('src'))) { + return this.renderLocalData_(this.element.getAttribute('src')); + } + return this.fetchList_(); } @@ -371,7 +375,7 @@ export class AmpList extends AMP.BaseElement { escapeCssSelectorIdent(`#${ampStateId}`) ); if (!ampStateEl) { - const errorMsg = `amp-state could not be found for id: ${ampStateId}`; + const errorMsg = `An amp-state element with id: ${ampStateId} could not be found.`; return Promise.reject(new Error(errorMsg)); } if ( @@ -413,8 +417,8 @@ export class AmpList extends AMP.BaseElement { const src = mutations['src']; const state = /** @type {!JsonObject} */ (mutations)['state']; if (src !== undefined) { - if (typeof src === 'object' || this.isAmpStateSrc_(src)) { - promise = this.renderLocalData_(/** @type {!Object} */ (src)); + if (typeof src === 'object') { + promise = this.renderLocalData_(src); } else if (typeof src === 'string') { // Defer to fetch in layoutCallback() before first layout. if (this.layoutCompleted_) { @@ -602,8 +606,6 @@ export class AmpList extends AMP.BaseElement { const elementSrc = this.element.getAttribute('src'); if (!elementSrc) { return Promise.resolve(); - } else if (this.isAmpStateSrc_(elementSrc)) { - return this.renderLocalData_(elementSrc); } let fetch; diff --git a/extensions/amp-list/0.1/test/test-amp-list.js b/extensions/amp-list/0.1/test/test-amp-list.js index 219a3412688ed..b43e6e48bf157 100644 --- a/extensions/amp-list/0.1/test/test-amp-list.js +++ b/extensions/amp-list/0.1/test/test-amp-list.js @@ -1277,6 +1277,23 @@ describes.repeated( .once(); await list.layoutCallback(); }); + + // Only valid for init. + it('should throw if setting [src] with "amp-state:" protocol.', async () => { + toggleExperiment(win, experimentName, true); + bind.getState = () => [1, 2, 3]; + + const ampStateEl = doc.createElement('amp-state'); + ampStateEl.setAttribute('id', 'okapis'); + const ampStateJson = doc.createElement('script'); + ampStateJson.setAttribute('type', 'application/json'); + ampStateEl.appendChild(ampStateJson); + doc.body.appendChild(ampStateEl); + + expect(list.mutatedAttributesCallback({ + 'src': 'amp-state:okapis', + })).to.eventually.be.rejectedWith('Invalid value: amp-state:okapis) + }); }); }); // with amp-bind }