UnicodeBidiDirection.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @typechecks
  8. *
  9. */
  10. /**
  11. * Constants to represent text directionality
  12. *
  13. * Also defines a *global* direciton, to be used in bidi algorithms as a
  14. * default fallback direciton, when no better direction is found or provided.
  15. *
  16. * NOTE: Use `setGlobalDir()`, or update `initGlobalDir()`, to set the initial
  17. * global direction value based on the application.
  18. *
  19. * Part of the implementation of Unicode Bidirectional Algorithm (UBA)
  20. * Unicode Standard Annex #9 (UAX9)
  21. * http://www.unicode.org/reports/tr9/
  22. */
  23. 'use strict';
  24. var invariant = require("./invariant");
  25. var NEUTRAL = 'NEUTRAL'; // No strong direction
  26. var LTR = 'LTR'; // Left-to-Right direction
  27. var RTL = 'RTL'; // Right-to-Left direction
  28. var globalDir = null; // == Helpers ==
  29. /**
  30. * Check if a directionality value is a Strong one
  31. */
  32. function isStrong(dir) {
  33. return dir === LTR || dir === RTL;
  34. }
  35. /**
  36. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  37. * property.
  38. */
  39. function getHTMLDir(dir) {
  40. !isStrong(dir) ? process.env.NODE_ENV !== "production" ? invariant(false, '`dir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  41. return dir === LTR ? 'ltr' : 'rtl';
  42. }
  43. /**
  44. * Get string value to be used for `dir` HTML attribute or `direction` CSS
  45. * property, but returns null if `dir` has same value as `otherDir`.
  46. * `null`.
  47. */
  48. function getHTMLDirIfDifferent(dir, otherDir) {
  49. !isStrong(dir) ? process.env.NODE_ENV !== "production" ? invariant(false, '`dir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  50. !isStrong(otherDir) ? process.env.NODE_ENV !== "production" ? invariant(false, '`otherDir` must be a strong direction to be converted to HTML Direction') : invariant(false) : void 0;
  51. return dir === otherDir ? null : getHTMLDir(dir);
  52. } // == Global Direction ==
  53. /**
  54. * Set the global direction.
  55. */
  56. function setGlobalDir(dir) {
  57. globalDir = dir;
  58. }
  59. /**
  60. * Initialize the global direction
  61. */
  62. function initGlobalDir() {
  63. setGlobalDir(LTR);
  64. }
  65. /**
  66. * Get the global direction
  67. */
  68. function getGlobalDir() {
  69. if (!globalDir) {
  70. this.initGlobalDir();
  71. }
  72. !globalDir ? process.env.NODE_ENV !== "production" ? invariant(false, 'Global direction not set.') : invariant(false) : void 0;
  73. return globalDir;
  74. }
  75. var UnicodeBidiDirection = {
  76. // Values
  77. NEUTRAL: NEUTRAL,
  78. LTR: LTR,
  79. RTL: RTL,
  80. // Helpers
  81. isStrong: isStrong,
  82. getHTMLDir: getHTMLDir,
  83. getHTMLDirIfDifferent: getHTMLDirIfDifferent,
  84. // Global Direction
  85. setGlobalDir: setGlobalDir,
  86. initGlobalDir: initGlobalDir,
  87. getGlobalDir: getGlobalDir
  88. };
  89. module.exports = UnicodeBidiDirection;