Skip to content

Commit

Permalink
remove support for post-init amp-state protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
samouri committed Jan 10, 2020
1 parent f9d4168 commit 4ca6138
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions extensions/amp-list/0.1/amp-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_();
}

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions extensions/amp-list/0.1/test/test-amp-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4ca6138

Please sign in to comment.