Visitar URL original
fix: answer DOM probes on NativeScriptDocument instead of throwing by edusperoni · Pull Request #176 · NativeScript/angular · GitHub
Skip to content
Merged
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
fix: answer DOM probes on NativeScriptDocument instead of throwing
* Angular discovers some optional configuration by reading the DOM. The
  CSP_NONCE default factory, for example, evaluates
  `DOCUMENT.body.querySelector('[ngCspNonce]')`. `NativeScriptDocument.body`
  was a hollow object, so the optional chaining in those probes did not help
  and they failed with `body?.querySelector is not a function`. Signal forms
  reach this on the first `[formField]` binding, which crashed every
  signal-form dialog at construction.
* `querySelector`, `querySelectorAll`, `getElementById`,
  `getElementsByTagName` and `getElementsByClassName` now answer "nothing
  found" on the document and on `body`/`head`. `createElement` still throws,
  since constructing real nodes is a caller bug rather than a probe.
* CSP_NONCE is also provided as `null`, as there is no CSP in a NativeScript
  runtime. It has to be a root provider: the root injector resolves
  `providedIn: 'root'` tokens itself before consulting the platform injector,
  so providing it in COMMON_PROVIDERS would have had no effect.
  • Loading branch information
edusperoni committed Aug 17, 2026
commit ec95731af050b96db4516e20ad7de298fdf585cb
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 { IMAGE_CONFIG, 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