Skip to content

Commit

Permalink
fix: Remove img el when there's no poster source (#8130)
Browse files Browse the repository at this point in the history
* fix: No invalid img el with no poster source

* move dom changes into setSrc
  • Loading branch information
mister-ben authored Feb 20, 2023
1 parent 2c7eea8 commit a27ee05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
61 changes: 37 additions & 24 deletions src/js/poster-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,9 @@ class PosterImage extends ClickableComponent {
* The element that gets created.
*/
createEl() {
const el = Dom.createEl(
'picture', {
className: 'vjs-poster',

// Don't want poster to be tabbable.
tabIndex: -1
},
{},
Dom.createEl('img', {
loading: 'lazy',
crossOrigin: this.crossOrigin()
}, {
alt: ''
})
);

return el;
// The el is an empty div to keep position in the DOM
// A picture and img el will be inserted when a source is set
return Dom.createEl('div', { className: 'vjs-poster'});
}

/**
Expand All @@ -79,9 +65,9 @@ class PosterImage extends ClickableComponent {
crossOrigin(value) {
// `null` can be set to unset a value
if (typeof value === 'undefined') {
if (this.el_) {
if (this.$('img')) {
// If the poster's element exists, give its value
return this.el_.querySelector('img').crossOrigin;
return this.$('img').crossOrigin;
} else if (this.player_.tech_ && this.player_.tech_.isReady_) {
// If not but the tech is ready, query the tech
return this.player_.crossOrigin();
Expand All @@ -97,7 +83,9 @@ class PosterImage extends ClickableComponent {
return;
}

this.el_.querySelector('img').crossOrigin = value;
if (this.$('img')) {
this.$('img').crossOrigin = value;
}

return;
}
Expand Down Expand Up @@ -125,13 +113,38 @@ class PosterImage extends ClickableComponent {
}

/**
* Set the source of the `PosterImage` depending on the display method.
* Set the source of the `PosterImage` depending on the display method. (Re)creates
* the inner picture and img elementss when needed.
*
* @param {string} url
* The URL to the source for the `PosterImage`.
* @param {string} [url]
* The URL to the source for the `PosterImage`. If not specified or falsy,
* any source and ant inner picture/img are removed.
*/
setSrc(url) {
this.el_.querySelector('img').src = url;
if (!url) {
this.el_.textContent = '';
return;
}

if (!this.$('img')) {
this.el_.appendChild(Dom.createEl(
'picture', {
className: 'vjs-poster',

// Don't want poster to be tabbable.
tabIndex: -1
},
{},
Dom.createEl('img', {
loading: 'lazy',
crossOrigin: this.crossOrigin()
}, {
alt: ''
})
));
}

this.$('img').src = url;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/unit/poster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ QUnit.test('should remove itself from the document flow when there is no poster'
true,
'Poster image hides with an empty source'
);
assert.equal(posterImage.$('img'), null, 'Poster image with no source has no img el');

// Updated with a valid source
this.mockPlayer.poster_ = this.poster2;
Expand All @@ -70,6 +71,7 @@ QUnit.test('should remove itself from the document flow when there is no poster'
false,
'Poster image shows again when there is a source'
);
assert.ok(posterImage.$('img'), 'Poster image with source restores img el');

posterImage.dispose();
});
Expand Down

0 comments on commit a27ee05

Please sign in to comment.