Skip to content

Commit

Permalink
Merge pull request #1397 from canalplus/misc/imediaelement
Browse files Browse the repository at this point in the history
 [Proposal] Forbid usage of the HTMLMediaElement and MSE TS types
  • Loading branch information
peaBerberian authored Jul 31, 2024
2 parents 8ca7472 + d6feb0d commit c7c2c60
Show file tree
Hide file tree
Showing 53 changed files with 480 additions and 183 deletions.
16 changes: 16 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ module.exports = {
Symbol: {
message: "Avoid using the `Symbol` type. Did you mean `symbol`?",
},
HTMLMediaElement: {
message:
"Avoid relying on `HTMLMediaElement` directly unless it is API-facing. Prefer our more restricted `IMediaElement` type",
},
MediaSource: {
message:
"Avoid relying on `MediaSource` directly unless it is API-facing. Prefer our more restricted `IMediaSource` type",
},
SourceBuffer: {
message:
"Avoid relying on `SourceBuffer` directly unless it is API-facing. Prefer our more restricted `ISourceBuffer` type",
},
SourceBufferList: {
message:
"Avoid relying on `SourceBufferList` directly unless it is API-facing. Prefer our more restricted `ISourceBufferList` type",
},
},
},
],
Expand Down
7 changes: 4 additions & 3 deletions src/compat/__tests__/add_text_track.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, beforeEach, it, expect, vi } from "vitest";
import type { IMediaElement } from "../../compat/browser_compatibility_types";

// Needed for calling require (which itself is needed to mock properly) because
// it is not type-checked:
Expand All @@ -23,7 +24,7 @@ describe("compat - addTextTrack", () => {
const fakeMediaElement = {
textTracks: [fakeTextTrack],
addTextTrack: mockAddTextTrack,
} as unknown as HTMLMediaElement;
} as unknown as IMediaElement;

vi.doMock("../browser_detection", () => ({
isIEOrEdge: true,
Expand Down Expand Up @@ -51,7 +52,7 @@ describe("compat - addTextTrack", () => {
const fakeMediaElement = {
textTracks: fakeTextTracks,
addTextTrack: mockAddTextTrack,
} as unknown as HTMLMediaElement;
} as unknown as IMediaElement;

vi.doMock("../browser_detection", () => ({
isIEOrEdge: true,
Expand Down Expand Up @@ -95,7 +96,7 @@ describe("compat - addTextTrack", () => {
textTracks: fakeTextTracks,
appendChild: mockAppendChild,
childNodes: fakeChildNodes,
} as unknown as HTMLMediaElement;
} as unknown as IMediaElement;

const spyOnCreateElement = vi
.spyOn(document, "createElement")
Expand Down
7 changes: 4 additions & 3 deletions src/compat/__tests__/change_source_buffer_type.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { describe, it, expect, vi } from "vitest";
import log from "../../log";
import type { ISourceBuffer } from "../browser_compatibility_types";
import tryToChangeSourceBufferType from "../change_source_buffer_type";

describe("Compat - tryToChangeSourceBufferType", () => {
it("should just return false if the SourceBuffer provided does not have a changeType method", () => {
const spy = vi.spyOn(log, "warn");
const fakeSourceBuffer: SourceBuffer = {} as unknown as SourceBuffer;
const fakeSourceBuffer: ISourceBuffer = {} as unknown as ISourceBuffer;
expect(tryToChangeSourceBufferType(fakeSourceBuffer, "toto")).toBe(false);
expect(spy).not.toHaveBeenCalled();
});
Expand All @@ -15,7 +16,7 @@ describe("Compat - tryToChangeSourceBufferType", () => {
const changeTypeFn = vi.fn();
const fakeSourceBuffer = {
changeType: changeTypeFn,
} as unknown as SourceBuffer;
} as unknown as ISourceBuffer;
expect(tryToChangeSourceBufferType(fakeSourceBuffer, "toto")).toBe(true);
expect(spy).not.toHaveBeenCalled();
});
Expand All @@ -28,7 +29,7 @@ describe("Compat - tryToChangeSourceBufferType", () => {
});
const fakeSourceBuffer = {
changeType: changeTypeFn,
} as unknown as SourceBuffer;
} as unknown as ISourceBuffer;
expect(tryToChangeSourceBufferType(fakeSourceBuffer, "toto")).toBe(false);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(
Expand Down
4 changes: 2 additions & 2 deletions src/compat/add_text_track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { ICompatTextTrack } from "./browser_compatibility_types";
import type { ICompatTextTrack, IMediaElement } from "./browser_compatibility_types";
import { isIEOrEdge } from "./browser_detection";

/**
Expand All @@ -28,7 +28,7 @@ import { isIEOrEdge } from "./browser_detection";
* @param {HTMLMediaElement} mediaElement
* @returns {Object}
*/
export default function addTextTrack(mediaElement: HTMLMediaElement): {
export default function addTextTrack(mediaElement: IMediaElement): {
track: ICompatTextTrack;
trackElement: HTMLTrackElement | undefined;
} {
Expand Down
Loading

0 comments on commit c7c2c60

Please sign in to comment.