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

fix: allow captions in devices that use old chrome to be shown #8826

Merged
merged 15 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
7 changes: 7 additions & 0 deletions src/css/components/_text-track.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ video::-webkit-media-text-track-display {
text-align: center !important;
width: 80% !important;
}

.video-js .vjs-text-track-display-inset {
top: 0;
right: 0;
bottom: 0;
left: 0;
}
CarlosVillasenor marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 40 additions & 0 deletions src/js/tracks/text-track-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as Fn from '../utils/fn.js';
import * as Dom from '../utils/dom.js';
import window from 'global/window';
import * as browser from '../utils/browser';

/** @import Player from '../player' */

Expand Down Expand Up @@ -319,6 +320,45 @@
}
this.updateForTrack(descriptionsTrack);
}

if (!window.CSS.supports('inset', '10px')) {
gkatsev marked this conversation as resolved.
Show resolved Hide resolved
const textTrackDisplay = this.el_;
const vjsTextTrackCues = textTrackDisplay.querySelectorAll('.vjs-text-track-cue');
const controlBarHeight = this.player_.controlBar.el_.getBoundingClientRect().height;
const playerHeight = this.player_.el_.getBoundingClientRect().height;

// Clear inline style before getting actual height of textTrackDisplay
textTrackDisplay.style = '';

// Add custom class to textTrackDisplay div child for not inset support styles
textTrackDisplay.firstChild.classList.add('vjs-text-track-display-inset');
Copy link
Member

Choose a reason for hiding this comment

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

an alternative to adding this class is using css @supports. Then, this line won't be necessary and it could all be done in css.

Suggested change
// Add custom class to textTrackDisplay div child for not inset support styles
textTrackDisplay.firstChild.classList.add('vjs-text-track-display-inset');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated this in the last commit, can this be re-reviewed?


// textrack style updates, this styles are required to be inline
tryUpdateStyle(textTrackDisplay, 'position', 'relative');
tryUpdateStyle(textTrackDisplay, 'height', (playerHeight - controlBarHeight) + 'px');
tryUpdateStyle(textTrackDisplay, 'top', 'unset');

if (browser.IS_SMART_TV) {
tryUpdateStyle(textTrackDisplay, 'bottom', playerHeight + 'px');
} else {
tryUpdateStyle(textTrackDisplay, 'bottom', '0px');

Check warning on line 344 in src/js/tracks/text-track-display.js

View check run for this annotation

Codecov / codecov/patch

src/js/tracks/text-track-display.js#L344

Added line #L344 was not covered by tests
}

// vjsTextTrackCue style updates
if (vjsTextTrackCues.length > 0) {
vjsTextTrackCues.forEach((vjsTextTrackCue) => {
// verify if inset styles are inline
if (vjsTextTrackCue.style.inset) {
const insetStyles = vjsTextTrackCue.style.inset.split(' ');

// expected value is always 3
if (insetStyles.length === 3) {
Object.assign(vjsTextTrackCue.style, { top: insetStyles[0], right: insetStyles[1], bottom: insetStyles[2], left: 'unset' });
}
}
});
}
}
}

/**
Expand Down
78 changes: 78 additions & 0 deletions test/unit/tracks/text-track-display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,82 @@ if (!Html5.supportsNativeTextTracks()) {

player.dispose();
});

QUnit.test('should use relative position for vjs-text-track-display element if browser does not support inset property', function(assert) {
// Set conditions for the use of the style modifications
window.CSS.supports = () => false;
browser.IS_SMART_TV = () => true;

const player = TestHelpers.makePlayer();
const track1 = {
kind: 'captions',
label: 'English',
language: 'en',
src: 'en.vtt',
default: true
};

// Add the text track
player.addRemoteTextTrack(track1, true);

player.src({type: 'video/mp4', src: 'http://google.com'});
player.play();

// as if metadata was loaded
player.textTrackDisplay.updateDisplayOverlay();

// Make sure the ready handler runs
this.clock.tick(1);

const textTrack = window.document.querySelector('.vjs-text-track-display');

assert.ok(textTrack.style.position === 'relative', 'Style of position for vjs-text-track-display element should be relative');
assert.ok(textTrack.style.top === 'unset', 'Style of position for vjs-text-track-display element should be unset');
assert.ok(textTrack.style.bottom === '0px', 'Style of bottom for vjs-text-track-display element should be 0px');
player.dispose();
});

QUnit.test('track cue should use values of top, right, botton, left if browser does not support inset property', function(assert) {
// Set conditions for the use of the style modifications
window.CSS.supports = () => false;
browser.IS_SMART_TV = () => true;

const player = TestHelpers.makePlayer();
const track1 = {
kind: 'captions',
label: 'English',
language: 'en',
src: 'en.vtt',
default: true
};

// Add the text track
player.addRemoteTextTrack(track1, true);

player.src({type: 'video/mp4', src: 'http://google.com'});
player.play();

// mock caption
const textTrackDisplay = window.document.querySelector('.vjs-text-track-display').firstChild;
const node = document.createElement('div');

node.classList.add('vjs-text-track-cue');
node.style.inset = '1px 2px 3px';
const textnode = document.createTextNode('Sample text');

node.appendChild(textnode);
textTrackDisplay.appendChild(node);

// avoid captions clear
player.textTrackDisplay.clearDisplay = () => '';

// as if metadata was loaded
player.textTrackDisplay.updateDisplay();

assert.ok(player.textTrackDisplay.el_.querySelector('.vjs-text-track-cue').style.left === 'unset', 'Style of left for vjs-text-track-cue element should be unset');
assert.ok(player.textTrackDisplay.el_.querySelector('.vjs-text-track-cue').style.top === '1px', 'Style of top for vjs-text-track-cue element should be 1px');
assert.ok(player.textTrackDisplay.el_.querySelector('.vjs-text-track-cue').style.right === '2px', 'Style of right for vjs-text-track-cue element should be 2px');
assert.ok(player.textTrackDisplay.el_.firstChild.classList.contains('vjs-text-track-display-inset'), 'Child of textTrackDisplay should contain class of vjs-text-track-display-inset');
player.dispose();
});
}
Loading