bidi.mjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import * as i0 from '@angular/core';
  2. import { InjectionToken, inject, EventEmitter, Injectable, Optional, Inject, Directive, Output, Input, NgModule } from '@angular/core';
  3. import { DOCUMENT } from '@angular/common';
  4. /**
  5. * Injection token used to inject the document into Directionality.
  6. * This is used so that the value can be faked in tests.
  7. *
  8. * We can't use the real document in tests because changing the real `dir` causes geometry-based
  9. * tests in Safari to fail.
  10. *
  11. * We also can't re-provide the DOCUMENT token from platform-browser because the unit tests
  12. * themselves use things like `querySelector` in test code.
  13. *
  14. * This token is defined in a separate file from Directionality as a workaround for
  15. * https://github.com/angular/angular/issues/22559
  16. *
  17. * @docs-private
  18. */
  19. const DIR_DOCUMENT = new InjectionToken('cdk-dir-doc', {
  20. providedIn: 'root',
  21. factory: DIR_DOCUMENT_FACTORY,
  22. });
  23. /** @docs-private */
  24. function DIR_DOCUMENT_FACTORY() {
  25. return inject(DOCUMENT);
  26. }
  27. /** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */
  28. const RTL_LOCALE_PATTERN = /^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)/i;
  29. /** Resolves a string value to a specific direction. */
  30. function _resolveDirectionality(rawValue) {
  31. const value = rawValue?.toLowerCase() || '';
  32. if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) {
  33. return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr';
  34. }
  35. return value === 'rtl' ? 'rtl' : 'ltr';
  36. }
  37. /**
  38. * The directionality (LTR / RTL) context for the application (or a subtree of it).
  39. * Exposes the current direction and a stream of direction changes.
  40. */
  41. class Directionality {
  42. constructor(_document) {
  43. /** The current 'ltr' or 'rtl' value. */
  44. this.value = 'ltr';
  45. /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */
  46. this.change = new EventEmitter();
  47. if (_document) {
  48. const bodyDir = _document.body ? _document.body.dir : null;
  49. const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
  50. this.value = _resolveDirectionality(bodyDir || htmlDir || 'ltr');
  51. }
  52. }
  53. ngOnDestroy() {
  54. this.change.complete();
  55. }
  56. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, deps: [{ token: DIR_DOCUMENT, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
  57. static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, providedIn: 'root' }); }
  58. }
  59. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Directionality, decorators: [{
  60. type: Injectable,
  61. args: [{ providedIn: 'root' }]
  62. }], ctorParameters: function () { return [{ type: undefined, decorators: [{
  63. type: Optional
  64. }, {
  65. type: Inject,
  66. args: [DIR_DOCUMENT]
  67. }] }]; } });
  68. /**
  69. * Directive to listen for changes of direction of part of the DOM.
  70. *
  71. * Provides itself as Directionality such that descendant directives only need to ever inject
  72. * Directionality to get the closest direction.
  73. */
  74. class Dir {
  75. constructor() {
  76. /** Normalized direction that accounts for invalid/unsupported values. */
  77. this._dir = 'ltr';
  78. /** Whether the `value` has been set to its initial value. */
  79. this._isInitialized = false;
  80. /** Event emitted when the direction changes. */
  81. this.change = new EventEmitter();
  82. }
  83. /** @docs-private */
  84. get dir() {
  85. return this._dir;
  86. }
  87. set dir(value) {
  88. const previousValue = this._dir;
  89. // Note: `_resolveDirectionality` resolves the language based on the browser's language,
  90. // whereas the browser does it based on the content of the element. Since doing so based
  91. // on the content can be expensive, for now we're doing the simpler matching.
  92. this._dir = _resolveDirectionality(value);
  93. this._rawDir = value;
  94. if (previousValue !== this._dir && this._isInitialized) {
  95. this.change.emit(this._dir);
  96. }
  97. }
  98. /** Current layout direction of the element. */
  99. get value() {
  100. return this.dir;
  101. }
  102. /** Initialize once default value has been set. */
  103. ngAfterContentInit() {
  104. this._isInitialized = true;
  105. }
  106. ngOnDestroy() {
  107. this.change.complete();
  108. }
  109. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Dir, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
  110. static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: Dir, selector: "[dir]", inputs: { dir: "dir" }, outputs: { change: "dirChange" }, host: { properties: { "attr.dir": "_rawDir" } }, providers: [{ provide: Directionality, useExisting: Dir }], exportAs: ["dir"], ngImport: i0 }); }
  111. }
  112. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: Dir, decorators: [{
  113. type: Directive,
  114. args: [{
  115. selector: '[dir]',
  116. providers: [{ provide: Directionality, useExisting: Dir }],
  117. host: { '[attr.dir]': '_rawDir' },
  118. exportAs: 'dir',
  119. }]
  120. }], propDecorators: { change: [{
  121. type: Output,
  122. args: ['dirChange']
  123. }], dir: [{
  124. type: Input
  125. }] } });
  126. class BidiModule {
  127. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: BidiModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
  128. static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.0", ngImport: i0, type: BidiModule, declarations: [Dir], exports: [Dir] }); }
  129. static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: BidiModule }); }
  130. }
  131. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: BidiModule, decorators: [{
  132. type: NgModule,
  133. args: [{
  134. exports: [Dir],
  135. declarations: [Dir],
  136. }]
  137. }] });
  138. /**
  139. * Generated bundle index. Do not edit.
  140. */
  141. export { BidiModule, DIR_DOCUMENT, Dir, Directionality };
  142. //# sourceMappingURL=bidi.mjs.map