Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Get placeholder background from placeholder srcs #20563

Merged
merged 4 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions extensions/amp-video-docking/0.1/amp-video-docking.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
escapeCssSelectorIdent,
isRTL,
removeElement,
scopedQuerySelector,
} from '../../../src/dom';
import {getInternalVideoElementFor} from '../../../src/utils/video';
import {getServiceForDoc} from '../../../src/service';
Expand Down Expand Up @@ -1213,27 +1214,40 @@ export class VideoDocking {

/**
* @param {!../../../src/video-interface.VideoOrBaseElementDef} video
* @private
*/
setPosterImage_(video) {
const attr = 'poster';

const {element} = video;

const placeholderPoster = this.getPlaceholderRefs_()['poster'];
const posterSrc = this.getPosterImageSrc_(video.element);

if (!element.hasAttribute('poster')) {
if (!posterSrc) {
toggle(placeholderPoster, false);
return;
}

const posterSrc = element.getAttribute(attr);

toggle(placeholderPoster, true);
setStyles(placeholderPoster, {
'background-image': `url(${posterSrc})`,
});
}

/**
* @param {!Element} element
* @return {string|undefined}
* @private
*/
getPosterImageSrc_(element) {
const poster = 'poster';
if (element.hasAttribute(poster)) {
return element.getAttribute(poster);
}
const imgEl = scopedQuerySelector(element,
'amp-img[placeholder],img[placeholder],[placeholder] amp-img');
if (imgEl) {
return imgEl.getAttribute('src');
}
}

/**
* @param {number} width
* @param {number} height
Expand Down
41 changes: 41 additions & 0 deletions extensions/amp-video-docking/0.1/test/test-amp-video-docking.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '../amp-video-docking';
import {PlayingStates} from '../../../../src/video-interface';
import {Services} from '../../../../src/services';
import {htmlFor} from '../../../../src/static-template';
import {layoutRectLtwh} from '../../../../src/layout-rect';


Expand Down Expand Up @@ -174,6 +175,46 @@ describes.repeated('', {
viewport.height = 0;
});

describe('#getPosterImageSrc_', () => {

skipForSlot('uses `poster` attr', () => {
const html = htmlFor(env.win.document);
const el = html`<amp-video poster=foo.png></amp-video>`;

expect(docking.getPosterImageSrc_(el)).to.equal('foo.png');
});

skipForSlot('uses `placeholder` amp-img', () => {
const html = htmlFor(env.win.document);
const el = html`<amp-video>
<amp-img src=foo.png placeholder></amp-img>
</amp-video>`;

expect(docking.getPosterImageSrc_(el)).to.equal('foo.png');
});

skipForSlot('uses amp-img in a `placeholder`', () => {
const html = htmlFor(env.win.document);
const el = html`<amp-video>
<div placeholder>
<amp-img src=foo.png></amp-img>
</div>
</amp-video>`;

expect(docking.getPosterImageSrc_(el)).to.equal('foo.png');
});

skipForSlot('uses `placeholder` img', () => {
const html = htmlFor(env.win.document);
const el = html`<amp-video>
<img src=foo.png placeholder>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto above, I didn't think this is valid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not valid AMPHTML but generated by the runtime.

</amp-video>`;

expect(docking.getPosterImageSrc_(el)).to.equal('foo.png');
});

});

it(`should use a ${targetType} as target`, () => {
maybeCreateSlotElementLtwh(190, topBoundary, 200, 100);

Expand Down