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

added guards for browser globals #471

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 1 addition & 0 deletions packages/domutils/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export namespace ClipboardExt {
* @param text - The text to copy to the clipboard.
*/
export function copyText(text: string): void {
if (typeof document === 'undefined') return;
// Fetch the document body.
const body = document.body;

Expand Down
12 changes: 8 additions & 4 deletions packages/domutils/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ export namespace Platform {
/**
* A flag indicating whether the platform is Mac.
*/
export const IS_MAC = !!navigator.platform.match(/Mac/i);
export const IS_MAC =
typeof navigator !== 'undefined' && !!navigator.platform.match(/Mac/i);

/**
* A flag indicating whether the platform is Windows.
*/
export const IS_WIN = !!navigator.platform.match(/Win/i);
export const IS_WIN =
typeof navigator !== 'undefined' && !!navigator.platform.match(/Win/i);

/**
* A flag indicating whether the browser is IE.
*/
export const IS_IE = /Trident/.test(navigator.userAgent);
export const IS_IE =
typeof navigator !== 'undefined' && /Trident/.test(navigator.userAgent);

/**
* A flag indicating whether the browser is Edge.
*/
export const IS_EDGE = /Edge/.test(navigator.userAgent);
export const IS_EDGE =
typeof navigator !== 'undefined' && /Edge/.test(navigator.userAgent);

/**
* Test whether the `accel` key is pressed.
Expand Down
8 changes: 6 additions & 2 deletions packages/domutils/src/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ namespace Private {
/**
* An empty element for testing selector validity.
*/
export const testElem = document.createElement('div');
export const testElem =
typeof document !== 'undefined'
? document.createElement('div')
: ({} as any);

/**
* A cross-browser CSS selector matching prototype function.
*/
export const protoMatchFunc = (() => {
let proto = Element.prototype as any;
let proto =
typeof Element === 'undefined' ? ({} as any) : Element.prototype;
return (
proto.matches ||
proto.matchesSelector ||
Expand Down