UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

1 lines 4.78 kB
{"version":3,"file":"directionality-CBXD4hga.mjs","sources":["../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/bidi/dir-document-token.ts","../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/bidi/directionality.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {DOCUMENT} from '@angular/common';\nimport {inject, InjectionToken} from '@angular/core';\n\n/**\n * Injection token used to inject the document into Directionality.\n * This is used so that the value can be faked in tests.\n *\n * We can't use the real document in tests because changing the real `dir` causes geometry-based\n * tests in Safari to fail.\n *\n * We also can't re-provide the DOCUMENT token from platform-browser because the unit tests\n * themselves use things like `querySelector` in test code.\n *\n * This token is defined in a separate file from Directionality as a workaround for\n * https://github.com/angular/angular/issues/22559\n *\n * @docs-private\n */\nexport const DIR_DOCUMENT = new InjectionToken<Document>('cdk-dir-doc', {\n providedIn: 'root',\n factory: DIR_DOCUMENT_FACTORY,\n});\n\n/**\n * @docs-private\n * @deprecated No longer used, will be removed.\n * @breaking-change 21.0.0\n */\nexport function DIR_DOCUMENT_FACTORY(): Document {\n return inject(DOCUMENT);\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {EventEmitter, Injectable, OnDestroy, inject} from '@angular/core';\nimport {DIR_DOCUMENT} from './dir-document-token';\n\nexport type Direction = 'ltr' | 'rtl';\n\n/** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */\nconst RTL_LOCALE_PATTERN =\n /^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)/i;\n\n/** Resolves a string value to a specific direction. */\nexport function _resolveDirectionality(rawValue: string): Direction {\n const value = rawValue?.toLowerCase() || '';\n\n if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) {\n return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr';\n }\n\n return value === 'rtl' ? 'rtl' : 'ltr';\n}\n\n/**\n * The directionality (LTR / RTL) context for the application (or a subtree of it).\n * Exposes the current direction and a stream of direction changes.\n */\n@Injectable({providedIn: 'root'})\nexport class Directionality implements OnDestroy {\n /** The current 'ltr' or 'rtl' value. */\n readonly value: Direction = 'ltr';\n\n /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */\n readonly change = new EventEmitter<Direction>();\n\n constructor(...args: unknown[]);\n\n constructor() {\n const _document = inject(DIR_DOCUMENT, {optional: true});\n\n if (_document) {\n const bodyDir = _document.body ? _document.body.dir : null;\n const htmlDir = _document.documentElement ? _document.documentElement.dir : null;\n this.value = _resolveDirectionality(bodyDir || htmlDir || 'ltr');\n }\n }\n\n ngOnDestroy() {\n this.change.complete();\n }\n}\n"],"names":[],"mappings":";;;;AAWA;;;;;;;;;;;;;;AAcG;MACU,YAAY,GAAG,IAAI,cAAc,CAAW,aAAa,EAAE;AACtE,IAAA,UAAU,EAAE,MAAM;AAClB,IAAA,OAAO,EAAE,oBAAoB;AAC9B,CAAA;AAED;;;;AAIG;SACa,oBAAoB,GAAA;AAClC,IAAA,OAAO,MAAM,CAAC,QAAQ,CAAC;AACzB;;ACzBA;AACA,MAAM,kBAAkB,GACtB,oHAAoH;AAEtH;AACM,SAAU,sBAAsB,CAAC,QAAgB,EAAA;IACrD,MAAM,KAAK,GAAG,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE;AAE3C,IAAA,IAAI,KAAK,KAAK,MAAM,IAAI,OAAO,SAAS,KAAK,WAAW,IAAI,SAAS,EAAE,QAAQ,EAAE;AAC/E,QAAA,OAAO,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,QAA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