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

chore: Remove safe-json-parse #8790

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
"m3u8-parser": "^7.1.0",
"mpd-parser": "^1.2.2",
"mux.js": "^7.0.1",
"safe-json-parse": "4.0.0",
"videojs-contrib-quality-levels": "4.1.0",
"videojs-font": "4.2.0",
"videojs-vtt.js": "0.15.5"
Expand Down
12 changes: 5 additions & 7 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { bufferedPercent } from './utils/buffer.js';
import * as stylesheet from './utils/stylesheet.js';
import FullscreenApi from './fullscreen-api.js';
import MediaError from './media-error.js';
import safeParseTuple from 'safe-json-parse/tuple';
import {merge} from './utils/obj';
import {silencePromise, isPromise} from './utils/promise';
import textTrackConverter from './tracks/text-track-list-converter.js';
Expand Down Expand Up @@ -5231,13 +5230,12 @@ class Player extends Component {
// Check if data-setup attr exists.
if (dataSetup !== null) {
// Parse options JSON
// If empty string, make it a parsable json object.
const [err, data] = safeParseTuple(dataSetup || '{}');

if (err) {
log.error(err);
try {
// If empty string, make it a parsable json object.
Object.assign(tagOptions, JSON.parse(dataSetup || '{}'));
} catch (e) {
log.error('data-setup', e);
}
Object.assign(tagOptions, data);
}

Object.assign(baseOptions, tagOptions);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/setup.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-env qunit */
import TestHelpers from './test-helpers.js';
import sinon from 'sinon';
import window from 'global/window';

QUnit.module('Setup');

Expand All @@ -19,3 +21,20 @@ QUnit.test('should set options from data-setup even if autoSetup is not called b
assert.ok(player.options_.playsinline === true);
player.dispose();
});

QUnit.test('should log an error if data-setup has invalid JSON', function(assert) {
const logError = sinon.spy(window.console, 'error');

const el = TestHelpers.makeTag();

el.setAttribute(
'data-setup',
"{'controls': true}"
);

const player = TestHelpers.makePlayer({}, el);

assert.ok(logError.calledWith('VIDEOJS:', 'ERROR:', 'data-setup'));
player.dispose();
window.console.error.restore();
});
3 changes: 1 addition & 2 deletions test/unit/tracks/text-track-settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import TextTrackSettings from '../../../src/js/tracks/text-track-settings.js';
import TestHelpers from '../test-helpers.js';
import * as Events from '../../../src/js/utils/events.js';
import safeParseTuple from 'safe-json-parse/tuple';
import sinon from 'sinon';
import window from 'global/window';
import Component from '../../../src/js/component.js';
Expand Down Expand Up @@ -120,7 +119,7 @@ QUnit.test('should update settings', function(assert) {
Events.trigger(player.$('.vjs-done-button'), 'click');

assert.deepEqual(
safeParseTuple(window.localStorage.getItem('vjs-text-track-settings'))[1],
JSON.parse(window.localStorage.getItem('vjs-text-track-settings')),
newSettings,
'values are saved'
);
Expand Down