Skip to content

Commit

Permalink
[dispatchEvent] check argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayc0 committed Nov 2, 2022
1 parent 90cc014 commit f997faf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
84 changes: 47 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,43 @@ import { match, parse, Feature, Query, MediaState } from "css-mediaquery";

let state: MediaState = {};

const now = Date.now();

// Event was added in node 15, so until we drop the support for versions before it, we need to use this
class EventLegacy {
type: "change";
timeStamp: number;

bubbles = false;
cancelBubble = false;
cancelable = false;
composed = false;
target = null;
currentTarget = null;
defaultPrevented = false;
eventPhase = 0;
isTrusted = false;
initEvent = () => {};
composedPath = () => [];
preventDefault = () => {};
stopImmediatePropagation = () => {};
stopPropagation = () => {};
returnValue = true;
srcElement = null;
// See https://developer.mozilla.org/en-US/docs/Web/API/Event/eventPhase
NONE = 0;
CAPTURING_PHASE = 1;
AT_TARGET = 2;
BUBBLING_PHASE = 3;
constructor(type: "change") {
this.type = type;
this.timeStamp = Date.now() - now; // See https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp#value
}
}

// @ts-expect-error
const EventCompat: typeof Event = typeof Event === "undefined" ? EventLegacy : Event;

const getFeaturesFromQuery = (query: Query): Set<Feature> => {
const parsedQuery = parse(query);
const features = new Set<Feature>();
Expand Down Expand Up @@ -75,6 +112,16 @@ export const matchMedia: typeof window.matchMedia = (query: string) => {
if (event === "change") removeListener(callback);
},
dispatchEvent: (event: MediaQueryListEvent) => {
if (!event) {
throw new TypeError(
`Failed to execute 'dispatchEvent' on 'EventTarget': 1 argument required, but only 0 present.`,
);
}
if (!(event instanceof EventCompat)) {
throw new TypeError(
`Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.`,
);
}
if (event.type !== "change") {
return true;
}
Expand Down Expand Up @@ -108,43 +155,6 @@ export const matchMedia: typeof window.matchMedia = (query: string) => {
return mql;
};

const now = Date.now();

// Event was added in node 15, so until we drop the support for versions before it, we need to use this
class EventLegacy {
type: "change";
timeStamp: number;

bubbles = false;
cancelBubble = false;
cancelable = false;
composed = false;
target = null;
currentTarget = null;
defaultPrevented = false;
eventPhase = 0;
isTrusted = false;
initEvent = () => {};
composedPath = () => [];
preventDefault = () => {};
stopImmediatePropagation = () => {};
stopPropagation = () => {};
returnValue = true;
srcElement = null;
// See https://developer.mozilla.org/en-US/docs/Web/API/Event/eventPhase
NONE = 0;
CAPTURING_PHASE = 1;
AT_TARGET = 2;
BUBBLING_PHASE = 3;
constructor(type: "change") {
this.type = type;
this.timeStamp = Date.now() - now; // See https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp#value
}
}

// @ts-expect-error
const EventCompat: typeof Event = typeof Event === "undefined" ? EventLegacy : Event;

export class MediaQueryListEvent extends EventCompat {
readonly media: string;
readonly matches: boolean;
Expand Down
14 changes: 14 additions & 0 deletions tests/listeners.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,17 @@ test.serial("`once: true` should be cleared after a regular `removeEventListener
mql.dispatchEvent(new MediaQueryListEvent("change", { matches: false, media: "(custom-non-valid)" }));
t.is(calls.length, 2);
});

test.serial("`.dispatchEvent` can only receive events", (t) => {
const mql = matchMedia("(min-width: 500px)");
if (typeof Event !== "undefined") {
t.is(mql.dispatchEvent(new Event("hello")), true);
}
t.is(mql.dispatchEvent(new MediaQueryListEvent("change", { matches: false, media: "(custom-non-valid)" })), true);

const err1 = t.throws(() => mql.dispatchEvent());
t.is(err1.message, `Failed to execute 'dispatchEvent' on 'EventTarget': 1 argument required, but only 0 present.`);

const err2 = t.throws(() => mql.dispatchEvent("hello"));
t.is(err2.message, `Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.`);
});

0 comments on commit f997faf

Please sign in to comment.