Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! fix(ivy): ensure that the correct
document
is available
- Loading branch information
1 parent
3c80157
commit 8f8bc26
Showing
6 changed files
with
62 additions
and
23 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
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,56 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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.io/license | ||
*/ | ||
|
||
/** | ||
* Most of the use of `document` in Angular is from within the DI system so it is possible to simply | ||
* inject the `DOCUMENT` token and are done. | ||
* | ||
* Ivy is special because it does not rely upon the DI and must get hold of the document some other | ||
* way. | ||
* | ||
* The solution is to define `getDocument()` and `setDocument()` top-level functions for ivy. | ||
* Wherever ivy needs the global document, it calls `getDocument()` instead. | ||
* | ||
* When running ivy outside of a browser environment, it is necessary to call `setDocument()` to | ||
* tell ivy what the global `document` is. | ||
* | ||
* Angular does this for us in each of the standard platforms (`Browser`, `Server`, and `WebWorker`) | ||
* by calling `setDocument()` when providing the `DOCUMENT` token. | ||
*/ | ||
let DOCUMENT: Document|undefined = undefined; | ||
|
||
/** | ||
* Tell ivy what the `document` is for this platform. | ||
* | ||
* It is only necessary to call this if the current platform is not a browser. | ||
* | ||
* @param document The object representing the global `document` in this environment. | ||
*/ | ||
export function setDocument(document: Document | undefined): void { | ||
DOCUMENT = document; | ||
} | ||
|
||
/** | ||
* Access the object that represents the `document` for this platform. | ||
* | ||
* Ivy calls this whenever it needs to access the `document` object. | ||
* For example to create the renderer or to do sanitization. | ||
*/ | ||
export function getDocument(): Document { | ||
if (DOCUMENT !== undefined) { | ||
return DOCUMENT; | ||
} else if (typeof document !== 'undefined') { | ||
return document; | ||
} | ||
// No "document" can be found. This should only happen if we are running ivy outside Angular and | ||
// the current platform is not a browser. Since this is not a supported scenario at the moment | ||
// this should not happen in Angular apps. | ||
// Once we support running ivy outside of Angular we will need to publish `setDocument()` as a | ||
// public API. Meanwhile we just return `undefined` and let the application fail. | ||
return undefined !; | ||
} |
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
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