Skip to content
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
6 changes: 5 additions & 1 deletion packages/angular/src/lib/nativescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ViewportScroller, XhrFactory, ɵNullViewportScroller as NullViewportScroller } from '@angular/common';
import { ApplicationModule, ErrorHandler, Inject, NgModule, NO_ERRORS_SCHEMA, Optional, Provider, RendererFactory2, SkipSelf, StaticProvider, ɵINJECTOR_SCOPE as INJECTOR_SCOPE } from '@angular/core';
import { ApplicationModule, CSP_NONCE, ErrorHandler, Inject, NgModule, NO_ERRORS_SCHEMA, Optional, Provider, RendererFactory2, SkipSelf, StaticProvider, ɵINJECTOR_SCOPE as INJECTOR_SCOPE } from '@angular/core';
import { Color, Device, View } from '@nativescript/core';
import { AppHostView } from './app-host-view';
import { NativescriptXhrFactory } from './nativescript-xhr-factory';
Expand Down Expand Up @@ -39,6 +39,10 @@ export const NATIVESCRIPT_MODULE_STATIC_PROVIDERS: StaticProvider[] = [
{ provide: NAMESPACE_FILTERS, useClass: PlatformNamespaceFilter, deps: [DEVICE], multi: true },
{ provide: DEVICE, useValue: Device },
{ provide: XhrFactory, useClass: NativescriptXhrFactory, deps: [] },
// No CSP in a NativeScript runtime. Providing it also stops the token's default
// factory reading `DOCUMENT.body.querySelector()`. Must stay root-scoped: the root
// injector resolves `providedIn: 'root'` tokens before consulting the platform.
{ provide: CSP_NONCE, useValue: null },
];
export const NATIVESCRIPT_MODULE_PROVIDERS: Provider[] = [{ provide: ViewportScroller, useClass: NullViewportScroller }];

Expand Down
29 changes: 29 additions & 0 deletions packages/angular/src/lib/platform-nativescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,41 @@ export class NativeScriptSanitizer extends Sanitizer {
// };
// }

/**
* Angular probes the DOM to discover optional configuration it may have been handed
* out of band - a `ngCspNonce` attribute on `<body>`, a transfer state `<script>` in
* the document. None of that can exist here, so the lookups answer "nothing found".
* Leaving the methods undefined instead surfaces as a `TypeError` thrown from deep
* inside framework code, since `document.body` is present and merely hollow.
*/
const NO_DOM_QUERIES = {
querySelector: () => null,
querySelectorAll: () => [],
getElementById: () => null,
getElementsByTagName: () => [],
getElementsByClassName: () => [],
getAttribute: () => null,
hasAttribute: () => false,
};

export class NativeScriptDocument {
// Required by the AnimationDriver
public body: any = {
isOverride: true,
...NO_DOM_QUERIES,
};

public head: any = {
...NO_DOM_QUERIES,
};

public querySelector = NO_DOM_QUERIES.querySelector;
public querySelectorAll = NO_DOM_QUERIES.querySelectorAll;
public getElementById = NO_DOM_QUERIES.getElementById;
public getElementsByTagName = NO_DOM_QUERIES.getElementsByTagName;
public getElementsByClassName = NO_DOM_QUERIES.getElementsByClassName;

// Deliberately still throws: constructing real nodes is a caller bug, not a probe.
createElement(tag: string) {
throw new Error('NativeScriptDocument is not DOM Document. There is no createElement() method.');
}
Expand Down
Loading