Skip to content

Commit

Permalink
Ensure src is the last propagated attribute to avoid multiple request…
Browse files Browse the repository at this point in the history
…s for different content on an amp-img in safari (#32634)
  • Loading branch information
kristoferbaxter committed Feb 12, 2021
1 parent acbb8a5 commit 447e48e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions builtins/amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const ATTRIBUTES_TO_PROPAGATE = [
'aria-labelledby',
'crossorigin',
'referrerpolicy',
'title',
'sizes',
'src',
'srcset',
'title',
'src',
];

export class AmpImg extends BaseElement {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/test-amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,36 @@ describes.sandboxed('amp-img', {}, (env) => {
expect(AmpImg.prerenderAllowed(el)).to.equal(true);
});

it('should propogate src as the final attribute when provided a srcset', () => {
// Providing src before srcset will cause Safari 14.4 to request two src values for the same `img`.
const el = document.createElement('amp-img');
el.setAttribute('src', 'test.jpg');
el.setAttribute('srcset', SRCSET_STRING);
el.setAttribute('width', 300);
el.setAttribute('height', 200);
el.getResources = () => Services.resourcesForDoc(document);
el.getPlaceholder = () => {
const img = document.createElement('img');
img.src = 'data:image/svg+xml;charset=utf-8,%3Csvg%3E%3C/svg%3E';
return img;
};
el.getLayout = () => 'responsive';
el.getLayoutSize = () => ({width: 300, height: 200});

const impl = new AmpImg(el);
const propagateAttributesSpy = sandbox.spy(impl, 'propagateAttributes');
impl.buildCallback();
impl.layoutCallback();

expect(propagateAttributesSpy).to.be.calledOnce;
const spiedAttributesToPropagate = propagateAttributesSpy.getCall(0)
.args[0];

expect(
spiedAttributesToPropagate[spiedAttributesToPropagate.length - 1]
).to.equal('src');
});

it('should propagate ARIA attributes', () => {
const el = document.createElement('amp-img');
el.setAttribute('src', 'test.jpg');
Expand Down

0 comments on commit 447e48e

Please sign in to comment.