Skip to content

Commit

Permalink
fixup! fix(ivy): ensure that the correct document is available
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Nov 10, 2019
1 parent 3c80157 commit 8f8bc26
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/core_render3_private_export.ts
Expand Up @@ -245,7 +245,7 @@ export {

export {
setDocument as ɵsetDocument
} from './render3/interfaces/renderer';
} from './render3/interfaces/document';

// we reexport these symbols just so that they are retained during the dead code elimination
// performed by rollup while it's creating fesm files.
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/render3/i18n.ts
Expand Up @@ -20,9 +20,10 @@ import {setDelayProjection} from './instructions/all';
import {attachI18nOpCodesDebug} from './instructions/lview_debug';
import {TsickleIssue1009, allocExpando, elementAttributeInternal, elementPropertyInternal, getOrCreateTNode, setInputsForProperty, setNgReflectProperties, textBindingInternal} from './instructions/shared';
import {LContainer, NATIVE} from './interfaces/container';
import {getDocument} from './interfaces/document';
import {COMMENT_MARKER, ELEMENT_MARKER, I18nMutateOpCode, I18nMutateOpCodes, I18nUpdateOpCode, I18nUpdateOpCodes, IcuType, TI18n, TIcu} from './interfaces/i18n';
import {TElementNode, TIcuContainerNode, TNode, TNodeFlags, TNodeType, TProjectionNode} from './interfaces/node';
import {RComment, RElement, RText, getDocument} from './interfaces/renderer';
import {RComment, RElement, RText} from './interfaces/renderer';
import {SanitizerFn} from './interfaces/sanitization';
import {isLContainer} from './interfaces/type_checks';
import {HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} from './interfaces/view';
Expand Down
56 changes: 56 additions & 0 deletions packages/core/src/render3/interfaces/document.ts
@@ -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 !;
}
20 changes: 1 addition & 19 deletions packages/core/src/render3/interfaces/renderer.ts
Expand Up @@ -16,25 +16,7 @@
*/

import {RendererStyleFlags2, RendererType2} from '../../render/api';


let DOCUMENT: Document|undefined = undefined;

export function setDocument(document: Document | undefined): void {
DOCUMENT = document;
}

export function getDocument(): Document {
if (DOCUMENT !== undefined) {
return DOCUMENT;
} else if (typeof document !== 'undefined') {
return document;
} else {
throw new Error(
'No "document" can be found. If you are running outside a browser then you must call `setDocument()` before using the renderer.');
}
}

import {getDocument} from './document';

// TODO: cleanup once the code is merged in angular/angular
export enum RendererStyleFlags3 {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/sanitization/sanitization.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {getDocument} from '../render3/interfaces/renderer';
import {getDocument} from '../render3/interfaces/document';
import {SANITIZER} from '../render3/interfaces/view';
import {getLView} from '../render3/state';
import {renderStringify} from '../render3/util/misc_utils';
Expand Down
Expand Up @@ -40,7 +40,7 @@ export interface MessageBusSource {
/** @deprecated */
export declare const platformWorkerApp: (extraProviders?: StaticProvider[] | undefined) => PlatformRef;

export declare const platformWorkerUi: (extraProviders?: StaticProvider[] | undefined) => PlatformRef;
export declare const platformWorkerUi: (extraProviders?: StaticProvider[] | undefined) => import("@angular/core").PlatformRef;

/** @deprecated */
export interface ReceivedMessage {
Expand Down

0 comments on commit 8f8bc26

Please sign in to comment.