-
Notifications
You must be signed in to change notification settings - Fork 29.4k
Expand file tree
/
Copy pathplatform_state.ts
More file actions
68 lines (60 loc) 路 1.77 KB
/
Copy pathplatform_state.ts
File metadata and controls
68 lines (60 loc) 路 1.77 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
/**
* @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 {DOCUMENT} from '@angular/common';
import {
inject,
Inject,
Injectable,
Injector,
傻startMeasuring as startMeasuring,
傻stopMeasuring as stopMeasuring,
傻RuntimeError as RuntimeError,
} from '@angular/core';
import {RuntimeErrorCode} from './errors';
import {serializeDocument} from './domino_adapter';
import {ENABLE_DOM_EMULATION} from './tokens';
/**
* Representation of the current platform state.
*
* @publicApi
*/
@Injectable()
export class PlatformState {
/* @internal */
_enableDomEmulation = enableDomEmulation(inject(Injector));
constructor(@Inject(DOCUMENT) private _doc: any) {}
/**
* Renders the current state of the platform to string.
*/
renderToString(): string {
if (ngDevMode && !this._enableDomEmulation && !window?.document) {
throw new RuntimeError(
RuntimeErrorCode.DISABLED_DOM_EMULATION_IN_NON_BROWSER,
(typeof ngDevMode === 'undefined' || ngDevMode) &&
'Disabled DOM emulation should only run in browser environments',
);
}
const measuringLabel = 'renderToString';
startMeasuring(measuringLabel);
const rendered = this._enableDomEmulation
? serializeDocument(this._doc)
: // In the case we run/test the platform-server in a browser environment
this._doc.documentElement.outerHTML;
stopMeasuring(measuringLabel);
return rendered;
}
/**
* Returns the current DOM state.
*/
getDocument(): any {
return this._doc;
}
}
export function enableDomEmulation(injector: Injector): boolean {
return injector.get(ENABLE_DOM_EMULATION, true);
}