Skip to content

Commit

Permalink
capricorn86#475@minor: Add attributes to HTMLMediaElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
rudywaltz committed Jun 26, 2022
1 parent 20b2e98 commit 7939b02
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
@@ -1,3 +1,5 @@
import DOMException from '../../exception/DOMException';
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum';
import HTMLElement from '../html-element/HTMLElement';
import IHTMLMediaElement from './IHTMLMediaElement';

Expand All @@ -8,6 +10,7 @@ import IHTMLMediaElement from './IHTMLMediaElement';
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base.
*/
export default class HTMLMediaElement extends HTMLElement implements IHTMLMediaElement {
#volume = 1;
/**
* Returns autoplay.
*
Expand Down Expand Up @@ -98,7 +101,7 @@ export default class HTMLMediaElement extends HTMLElement implements IHTMLMediaE
/**
* Returns muted.
*
* @returns muted.
* @returns Muted.
*/
public get muted(): boolean {
return this.getAttributeNS(null, 'muted') !== null;
Expand All @@ -107,7 +110,7 @@ export default class HTMLMediaElement extends HTMLElement implements IHTMLMediaE
/**
* Sets muted.
*
* @param muted muted.
* @param muted Muted.
*/
public set muted(muted: boolean) {
if (!muted) {
Expand All @@ -117,6 +120,37 @@ export default class HTMLMediaElement extends HTMLElement implements IHTMLMediaE
}
}

/**
* Returns volume.
*
* @returns Volume.
*/
public get volume(): number {
return this.#volume;
}

/**
* Sets volume.
*
* @param volume Volume.
*/
public set volume(volume: number | string) {
const parsedVolume = Number(volume);

if (isNaN(parsedVolume)) {
throw new TypeError(
`Failed to set the 'volume' property on 'HTMLMediaElement': The provided double value is non-finite.`
);
}
if (parsedVolume < 0 || parsedVolume > 1) {
throw new DOMException(
`Failed to set the 'volume' property on 'HTMLMediaElement': The volume provided (${parsedVolume}) is outside the range [0, 1].`,
DOMExceptionNameEnum.indexSizeError
);
}
this.#volume = parsedVolume;
}

/**
*
*/
Expand Down
Expand Up @@ -20,15 +20,16 @@ export default interface IHTMLMediaElement extends IHTMLElement {
// DefaultPlaybackRate; // TODO
// DisableRemotePlayback: boolean; // TODO
// Duration: number; // TODO
// Ended: boolean; // TODO
// Ended: boolean; // TODO readonly
// Error; // TODO object
loop: boolean;
// MediaKeys; // TODO
muted: boolean;
// NetworkState; // TODO
paused: boolean;
// Played: boolean; // TODO
paused: boolean; // TODO readonly?
// Played: // TODO timeranges
// PlaybackRate: number; // TODO
volume: number | string;

/**
* The HTMLMediaElement.pause() method will pause playback of the media, if the media is already in a paused state this method will have no effect.
Expand All @@ -39,7 +40,7 @@ export default interface IHTMLMediaElement extends IHTMLElement {
/**
* The HTMLMediaElement play() method attempts to begin playback of the media. It returns a Promise which is resolved when playback has been successfully started.
*/
// play(): Promise<void>; // TODO function
// Play(): Promise<void>; // TODO function

/**
* Clones a node.
Expand Down
@@ -1,5 +1,7 @@
import Window from '../../../src/window/Window';
import IWindow from '../../../src/window/IWindow';
import DOMException from '../../../src/exception/DOMException';
import DOMExceptionNameEnum from '../../../src/exception/DOMExceptionNameEnum';
import IDocument from '../../../src/nodes/document/IDocument';
import IHTMLMediaElement from '../../../src/nodes/html-media-element/IHTMLMediaElement';

Expand Down Expand Up @@ -41,4 +43,43 @@ describe('HTMLMediaElement', () => {
expect(element.getAttribute('paused')).toBe('');
});
});

describe('volume()', () => {
it('Returns default value', () => {
expect(element.volume).toBe(1);
});

it('Set value', () => {
element.volume = 0.5;
expect(element.volume).toBe(0.5);
});

it('Set parse volmue as a number', () => {
element.volume = '0.5';
expect(element.volume).toBe(0.5);
});

it('Throw type error if volume is not a number', () => {
expect(() => {
element.volume = 'zeropointfive';
}).toThrowError(
new TypeError(
`Failed to set the 'volume' property on 'HTMLMediaElement': The provided double value is non-finite.`
)
);
});

for (const volume of [-0.4, 1.3]) {
it(`Throw error if out of range: ${volume}`, () => {
expect(() => {
element.volume = volume;
}).toThrowError(
new DOMException(
`Failed to set the 'volume' property on 'HTMLMediaElement': The volume provided (${volume}) is outside the range [0, 1].`,
DOMExceptionNameEnum.indexSizeError
)
);
});
}
});
});

0 comments on commit 7939b02

Please sign in to comment.