|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {EnvironmentProviders, InjectionToken, makeEnvironmentProviders} from './di/index'; |
| 10 | + |
| 11 | +/** Defines the default value of the `NG_REFLECT_ATTRS_FLAG` flag. */ |
| 12 | +export const NG_REFLECT_ATTRS_FLAG_DEFAULT = false; |
| 13 | + |
| 14 | +/** |
| 15 | + * Defines an internal flag that indicates whether the runtime code should be |
| 16 | + * producing `ng-reflect-*` attributes. |
| 17 | + */ |
| 18 | +export const NG_REFLECT_ATTRS_FLAG = new InjectionToken<boolean>( |
| 19 | + typeof ngDevMode === 'undefined' || ngDevMode ? 'NG_REFLECT_FLAG' : '', |
| 20 | + { |
| 21 | + providedIn: 'root', |
| 22 | + factory: () => NG_REFLECT_ATTRS_FLAG_DEFAULT, |
| 23 | + }, |
| 24 | +); |
| 25 | + |
| 26 | +/** |
| 27 | + * Enables the logic to produce `ng-reflect-*` attributes on elements with bindings. |
| 28 | + * |
| 29 | + * Note: this is a dev-mode only setting and it will have no effect in production mode. |
| 30 | + * In production mode, the `ng-reflect-*` attributes are *never* produced by Angular. |
| 31 | + * |
| 32 | + * Important: using and relying on the `ng-reflect-*` attributes is not recommended, |
| 33 | + * they are deprecated and only present for backwards compatibility. Angular will stop |
| 34 | + * producing them in one of the future versions. |
| 35 | + * |
| 36 | + * @publicApi |
| 37 | + */ |
| 38 | +export function provideNgReflectAttributes(): EnvironmentProviders { |
| 39 | + const providers = |
| 40 | + typeof ngDevMode === 'undefined' || ngDevMode |
| 41 | + ? [ |
| 42 | + { |
| 43 | + provide: NG_REFLECT_ATTRS_FLAG, |
| 44 | + useValue: true, |
| 45 | + }, |
| 46 | + ] |
| 47 | + : []; |
| 48 | + return makeEnvironmentProviders(providers); |
| 49 | +} |
| 50 | + |
| 51 | +export function normalizeDebugBindingName(name: string) { |
| 52 | + // Attribute names with `$` (eg `x-y$`) are valid per spec, but unsupported by some browsers |
| 53 | + name = camelCaseToDashCase(name.replace(/[$@]/g, '_')); |
| 54 | + return `ng-reflect-${name}`; |
| 55 | +} |
| 56 | + |
| 57 | +const CAMEL_CASE_REGEXP = /([A-Z])/g; |
| 58 | + |
| 59 | +function camelCaseToDashCase(input: string): string { |
| 60 | + return input.replace(CAMEL_CASE_REGEXP, (...m: any[]) => '-' + m[1].toLowerCase()); |
| 61 | +} |
| 62 | + |
| 63 | +export function normalizeDebugBindingValue(value: any): string { |
| 64 | + try { |
| 65 | + // Limit the size of the value as otherwise the DOM just gets polluted. |
| 66 | + return value != null ? value.toString().slice(0, 30) : value; |
| 67 | + } catch (e) { |
| 68 | + return '[ERROR] Exception while trying to serialize the value'; |
| 69 | + } |
| 70 | +} |
0 commit comments