Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix - ReferenceError: document is not defined #432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/src/utils/offscreen-fragment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import {
ViewEncapsulation,
AfterViewInit,
OnDestroy,
ElementRef
ElementRef, Inject
} from '@angular/core';

const dummyContainer = document.createDocumentFragment()
import { DOCUMENT } from '@angular/common';

@Component({
selector: 'offscreen-fragment',
template: '<ng-content></ng-content>',
encapsulation: ViewEncapsulation.None
})
export class OffscreenFragmentComponent implements AfterViewInit, OnDestroy {
constructor(private element: ElementRef) {

private dummyContainer!: DocumentFragment;

constructor(private element: ElementRef, @Inject(DOCUMENT) private document: Document) {
this.dummyContainer = this.document.createDocumentFragment();
}

ngAfterViewInit() {
dummyContainer.appendChild(this.element.nativeElement)
this.dummyContainer.appendChild(this.element.nativeElement)
}

// invoked BEFORE component removed from DOM
ngOnDestroy() {
dummyContainer.removeChild(this.element.nativeElement)
this.dummyContainer.removeChild(this.element.nativeElement)
}
}
23 changes: 14 additions & 9 deletions lib/src/utils/transport-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
OnChanges,
AfterViewInit,
OnDestroy,
SimpleChanges
SimpleChanges, Inject
} from '@angular/core';

const dummyContainer = document.createDocumentFragment();
import { DOCUMENT } from '@angular/common';

@Component({
selector: 'transport-container',
Expand All @@ -30,10 +29,16 @@ export class TransportContainerComponent implements OnChanges, AfterViewInit, On

@ViewChild('rootEl') rootElRef?: ElementRef;

private dummyContainer!: DocumentFragment;

constructor(@Inject(DOCUMENT) private document: Document) {
this.dummyContainer = this.document.createDocumentFragment();
}

ngAfterViewInit() {
const rootEl: Element = this.rootElRef?.nativeElement; // assumed defined

replaceEl(rootEl, this.inPlaceOf);
replaceEl(rootEl, this.inPlaceOf, this.dummyContainer);
applyElAttrs(rootEl, undefined, this.elAttrs);

// insurance for if Preact recreates and reroots inPlaceOf element
Expand All @@ -50,8 +55,8 @@ export class TransportContainerComponent implements OnChanges, AfterViewInit, On
if (rootEl) {
// If the ContentContainer's tagName changed, it will create a new DOM element in its
// original place. Detect this and re-replace.
if (this.inPlaceOf.parentNode !== dummyContainer) {
replaceEl(rootEl, this.inPlaceOf);
if (this.inPlaceOf.parentNode !== this.dummyContainer) {
replaceEl(rootEl, this.inPlaceOf, this.dummyContainer);
applyElAttrs(rootEl, undefined, this.elAttrs);
this.reportEl(rootEl as HTMLElement);
} else {
Expand All @@ -67,15 +72,15 @@ export class TransportContainerComponent implements OnChanges, AfterViewInit, On
// invoked BEFORE component removed from DOM
ngOnDestroy() {
// protect against Preact recreating and rerooting inPlaceOf element
if (this.inPlaceOf.parentNode === dummyContainer) {
dummyContainer.removeChild(this.inPlaceOf);
if (this.inPlaceOf.parentNode === this.dummyContainer) {
this.dummyContainer.removeChild(this.inPlaceOf);
}

this.reportEl(null);
}
}

function replaceEl(subject: Element, inPlaceOf: Element): void {
function replaceEl(subject: Element, inPlaceOf: Element, dummyContainer: DocumentFragment): void {
inPlaceOf.parentNode?.insertBefore(subject, inPlaceOf.nextSibling);
dummyContainer.appendChild(inPlaceOf);
}
Expand Down