forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomino_adapter.ts
More file actions
104 lines (88 loc) · 2.88 KB
/
domino_adapter.ts
File metadata and controls
104 lines (88 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {ɵsetRootDomAdapter as setRootDomAdapter} from '@angular/common';
import {ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';
import domino from './bundled-domino';
export function setDomTypes() {
// Make all Domino types available in the global env.
// NB: Any changes here should also be done in `packages/platform-server/init/src/shims.ts`.
Object.assign(globalThis, domino.impl);
(globalThis as any)['KeyboardEvent'] = domino.impl.Event;
}
/**
* Parses a document string to a Document object.
*/
export function parseDocument(html: string, url = '/') {
let window = domino.createWindow(html, url);
let doc = window.document;
return doc;
}
/**
* Serializes a document to string.
*/
export function serializeDocument(doc: Document): string {
return (doc as any).serialize();
}
/**
* DOM Adapter for the server platform based on https://github.com/fgnass/domino.
*/
export class DominoAdapter extends BrowserDomAdapter {
static override makeCurrent() {
setDomTypes();
setRootDomAdapter(new DominoAdapter());
}
override readonly supportsDOMEvents = false;
private static defaultDoc: Document;
override createHtmlDocument(): Document {
return parseDocument('<html><head><title>fakeTitle</title></head><body></body></html>');
}
override getDefaultDocument(): Document {
if (!DominoAdapter.defaultDoc) {
DominoAdapter.defaultDoc = domino.createDocument();
}
return DominoAdapter.defaultDoc;
}
override isElementNode(node: any): boolean {
return node ? node.nodeType === DominoAdapter.defaultDoc.ELEMENT_NODE : false;
}
override isShadowRoot(node: any): boolean {
return node.shadowRoot == node;
}
/** @deprecated No longer being used in Ivy code. To be removed in version 14. */
override getGlobalEventTarget(doc: Document, target: string): EventTarget | null {
if (target === 'window') {
return doc.defaultView;
}
if (target === 'document') {
return doc;
}
if (target === 'body') {
return doc.body;
}
return null;
}
override getBaseHref(doc: Document): string {
// TODO(alxhub): Need relative path logic from BrowserDomAdapter here?
return doc.documentElement!.querySelector('base')?.getAttribute('href') || '';
}
override dispatchEvent(el: Node, evt: any) {
el.dispatchEvent(evt);
// Dispatch the event to the window also.
const doc = el.ownerDocument || el;
const win = (doc as any).defaultView;
if (win) {
win.dispatchEvent(evt);
}
}
override getUserAgent(): string {
return 'Fake user agent';
}
override getCookie(name: string): string {
throw new Error('getCookie has not been implemented');
}
}