bidi_spec.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { bidi } from "../../src/core/bidi.js";
  16. describe("bidi", function () {
  17. it(
  18. "should mark text as LTR if there's only LTR-characters, " +
  19. "when the string is very short",
  20. function () {
  21. const str = "foo";
  22. const bidiText = bidi(str, -1, false);
  23. expect(bidiText.str).toEqual("foo");
  24. expect(bidiText.dir).toEqual("ltr");
  25. }
  26. );
  27. it("should mark text as LTR if there's only LTR-characters", function () {
  28. const str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit.";
  29. const bidiText = bidi(str, -1, false);
  30. expect(bidiText.str).toEqual(
  31. "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
  32. );
  33. expect(bidiText.dir).toEqual("ltr");
  34. });
  35. it("should mark text as RTL if more than 30% of text is RTL", function () {
  36. // 33% of test text are RTL characters
  37. const test = "\u0645\u0635\u0631 Egypt";
  38. const result = "Egypt \u0631\u0635\u0645";
  39. const bidiText = bidi(test, -1, false);
  40. expect(bidiText.str).toEqual(result);
  41. expect(bidiText.dir).toEqual("rtl");
  42. });
  43. it("should mark text as LTR if less than 30% of text is RTL", function () {
  44. const test = "Egypt is known as \u0645\u0635\u0631 in Arabic.";
  45. const result = "Egypt is known as \u0631\u0635\u0645 in Arabic.";
  46. const bidiText = bidi(test, -1, false);
  47. expect(bidiText.str).toEqual(result);
  48. expect(bidiText.dir).toEqual("ltr");
  49. });
  50. it(
  51. "should mark text as RTL if less than 30% of text is RTL, " +
  52. "when the string is very short (issue 11656)",
  53. function () {
  54. const str = "()\u05d1("; // 25% of the string is RTL characters.
  55. const bidiText = bidi(str, -1, false);
  56. expect(bidiText.str).toEqual("(\u05d1)(");
  57. expect(bidiText.dir).toEqual("rtl");
  58. }
  59. );
  60. });