This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
275 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class BlobMock implements Blob { | ||
size = 0; | ||
type = ''; | ||
slice = () => this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
projects/universal/src/constants/universal-speech-synthesis.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import {DOCUMENT} from '@angular/common'; | ||
import {FactoryProvider} from '@angular/core'; | ||
import { | ||
LOCAL_STORAGE, | ||
LOCATION, | ||
NAVIGATOR, | ||
PERFORMANCE, | ||
SESSION_STORAGE, | ||
SPEECH_SYNTHESIS, | ||
WINDOW, | ||
} from '@ng-web-apis/common'; | ||
import {BlobMock} from '../classes/blob-mock'; | ||
import {EVENT_TARGET} from '../utils/event-target'; | ||
import { | ||
alwaysFalse, | ||
alwaysNull, | ||
alwaysRejected, | ||
alwaysZero, | ||
emptyFunction, | ||
identity, | ||
} from '../utils/functions'; | ||
|
||
const COMPUTED_STYLES: Partial<CSSStyleDeclaration> = { | ||
getPropertyPriority: () => '', | ||
getPropertyValue: () => '', | ||
item: () => '', | ||
removeProperty: () => '', | ||
setProperty: emptyFunction, | ||
}; | ||
const COMPUTED_STYLES_HANDLER: ProxyHandler<CSSStyleDeclaration> = { | ||
get: (obj, key: any) => (key in obj ? obj[key] : null), | ||
}; | ||
const COMPUTED_STYLES_PROXY = new Proxy<CSSStyleDeclaration>( | ||
COMPUTED_STYLES as any, | ||
COMPUTED_STYLES_HANDLER, | ||
); | ||
const CSS_RULES = new (class extends Array<CSSRule> implements CSSRuleList { | ||
item = () => null; | ||
})(); | ||
const BAR_PROP: BarProp = { | ||
visible: false, | ||
}; | ||
const DB_REQUEST: IDBOpenDBRequest = { | ||
...EVENT_TARGET, | ||
onblocked: null, | ||
onerror: null, | ||
onsuccess: null, | ||
onupgradeneeded: null, | ||
error: null, | ||
readyState: 'pending', | ||
result: null as any, // Cannot be accessed for 'pending' state anyway | ||
source: null as any, // null for open requests | ||
transaction: null, | ||
}; | ||
const SELF = ['frames', 'parent', 'self', 'top', 'window']; | ||
const WINDOW_HANDLER: ProxyHandler<Window> = { | ||
get: (windowRef, key: keyof Window) => { | ||
if (SELF.includes(key)) { | ||
return windowRef; | ||
} | ||
|
||
return key.startsWith('on') ? null : windowRef[key]; | ||
}, | ||
}; | ||
|
||
export function windowFactory( | ||
document: Document, | ||
navigator: Navigator, | ||
localStorage: Storage, | ||
location: Location, | ||
performance: Performance, | ||
sessionStorage: Storage, | ||
speechSynthesis: SpeechSynthesis, | ||
): Window { | ||
const windowMock: Window = { | ||
...EVENT_TARGET, | ||
document, | ||
localStorage, | ||
location, | ||
navigator, | ||
performance, | ||
sessionStorage, | ||
speechSynthesis, | ||
URL, | ||
URLSearchParams, | ||
setTimeout, | ||
setInterval, | ||
clearTimeout, | ||
clearInterval, | ||
console, | ||
Blob: BlobMock, | ||
alert: emptyFunction, | ||
clientInformation: navigator, | ||
// TODO: Candidate for token | ||
matchMedia: () => ({ | ||
...EVENT_TARGET, | ||
matches: false, | ||
media: '', | ||
onchange: null, | ||
addListener: emptyFunction, | ||
removeListener: emptyFunction, | ||
}), | ||
// TODO: Candidate for token | ||
caches: { | ||
delete: () => Promise.resolve(false), | ||
has: () => Promise.resolve(false), | ||
keys: () => Promise.resolve([]), | ||
match: alwaysRejected, | ||
open: alwaysRejected, | ||
}, | ||
// TODO: Candidate for token | ||
indexedDB: { | ||
cmp: alwaysZero, | ||
open: () => DB_REQUEST, | ||
deleteDatabase: () => DB_REQUEST, | ||
}, | ||
customElements: { | ||
define: emptyFunction, | ||
get: emptyFunction, | ||
upgrade: emptyFunction, | ||
whenDefined: alwaysRejected, | ||
}, | ||
styleMedia: { | ||
type: '', | ||
matchMedium: alwaysFalse, | ||
}, | ||
crypto: { | ||
subtle: undefined as any, // Insecure context | ||
getRandomValues: identity, | ||
}, | ||
history: { | ||
length: 0, | ||
scrollRestoration: 'auto', | ||
state: {}, | ||
back: emptyFunction, | ||
forward: emptyFunction, | ||
go: emptyFunction, | ||
pushState: emptyFunction, | ||
replaceState: emptyFunction, | ||
}, | ||
closed: false, | ||
defaultStatus: '', | ||
devicePixelRatio: 1, | ||
doNotTrack: '', | ||
frameElement: null as any, // TODO: bug in TypeScript | ||
innerHeight: 0, | ||
innerWidth: 0, | ||
isSecureContext: false, | ||
length: 0, | ||
name: '', | ||
offscreenBuffering: false, | ||
opener: {}, | ||
origin: '', | ||
orientation: '', | ||
outerHeight: 0, | ||
outerWidth: 0, | ||
pageXOffset: 0, | ||
pageYOffset: 0, | ||
screenLeft: 0, | ||
screenTop: 0, | ||
screenX: 0, | ||
screenY: 0, | ||
scrollX: 0, | ||
scrollY: 0, | ||
status: '', | ||
blur: emptyFunction, | ||
cancelAnimationFrame: emptyFunction, | ||
captureEvents: emptyFunction, | ||
close: emptyFunction, | ||
confirm: alwaysFalse, | ||
departFocus: emptyFunction, | ||
focus: emptyFunction, | ||
moveBy: emptyFunction, | ||
moveTo: emptyFunction, | ||
open: alwaysNull, | ||
postMessage: emptyFunction, | ||
print: emptyFunction, | ||
prompt: alwaysNull, | ||
releaseEvents: emptyFunction, | ||
requestAnimationFrame: alwaysZero, | ||
resizeBy: emptyFunction, | ||
resizeTo: emptyFunction, | ||
scroll: emptyFunction, | ||
scrollBy: emptyFunction, | ||
scrollTo: emptyFunction, | ||
stop: emptyFunction, | ||
atob: identity, | ||
btoa: identity, | ||
fetch: alwaysRejected, | ||
createImageBitmap: alwaysRejected, | ||
queueMicrotask: emptyFunction, | ||
locationbar: BAR_PROP, | ||
menubar: BAR_PROP, | ||
personalbar: BAR_PROP, | ||
scrollbars: BAR_PROP, | ||
statusbar: BAR_PROP, | ||
toolbar: BAR_PROP, | ||
getComputedStyle: () => COMPUTED_STYLES_PROXY, | ||
getMatchedCSSRules: () => CSS_RULES, | ||
getSelection: () => null as any, // TODO: old TypeScript issue | ||
} as any; | ||
|
||
return new Proxy(windowMock, WINDOW_HANDLER); | ||
} | ||
|
||
export const UNIVERSAL_WINDOW: FactoryProvider = { | ||
provide: WINDOW, | ||
deps: [ | ||
DOCUMENT, | ||
LOCAL_STORAGE, | ||
LOCATION, | ||
NAVIGATOR, | ||
PERFORMANCE, | ||
SESSION_STORAGE, | ||
SPEECH_SYNTHESIS, | ||
], | ||
useFactory: windowFactory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {alwaysFalse, emptyFunction} from './functions'; | ||
|
||
export const EVENT_TARGET: EventTarget = { | ||
addEventListener: emptyFunction, | ||
dispatchEvent: alwaysFalse, | ||
removeEventListener: emptyFunction, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.