directionality-CBXD4hga.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import * as i0 from '@angular/core';
  2. import { InjectionToken, inject, EventEmitter, Injectable } 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. /**
  24. * @docs-private
  25. * @deprecated No longer used, will be removed.
  26. * @breaking-change 21.0.0
  27. */
  28. function DIR_DOCUMENT_FACTORY() {
  29. return inject(DOCUMENT);
  30. }
  31. /** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */
  32. 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;
  33. /** Resolves a string value to a specific direction. */
  34. function _resolveDirectionality(rawValue) {
  35. const value = rawValue?.toLowerCase() || '';
  36. if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) {
  37. return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr';
  38. }
  39. return value === 'rtl' ? 'rtl' : 'ltr';
  40. }
  41. /**
  42. * The directionality (LTR / RTL) context for the application (or a subtree of it).
  43. * Exposes the current direction and a stream of direction changes.
  44. */
  45. class Directionality {
  46. /** The current 'ltr' or 'rtl' value. */
  47. value = 'ltr';
  48. /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */
  49. change = new EventEmitter();
  50. constructor() {
  51. const _document = inject(DIR_DOCUMENT, { optional: true });
  52. if (_document) {
  53. const bodyDir = _document.body ? _document.body.dir : null;
  54. const htmlDir = _document.documentElement ? _document.documentElement.dir : null;
  55. this.value = _resolveDirectionality(bodyDir || htmlDir || 'ltr');
  56. }
  57. }
  58. ngOnDestroy() {
  59. this.change.complete();
  60. }
  61. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: Directionality, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  62. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: Directionality, providedIn: 'root' });
  63. }
  64. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: Directionality, decorators: [{
  65. type: Injectable,
  66. args: [{ providedIn: 'root' }]
  67. }], ctorParameters: () => [] });
  68. export { Directionality as D, _resolveDirectionality as _, DIR_DOCUMENT as a };
  69. //# sourceMappingURL=directionality-CBXD4hga.mjs.map