unicode.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { isSurrogateHalf, isCombiningMark, isInsideSurrogatePair, isInsideCombinedSymbol } from '/ckeditor5/utils/unicode.js';
  6. describe( 'utils', () => {
  7. describe( 'unicode', () => {
  8. describe( 'isSurrogateHalf', () => {
  9. it( 'should return true if given character is a surrogate half', () => {
  10. // Half of pile of poo - U+D83D
  11. // It is not string because of problem with Babel replacing half of the surrogate with actual (wrong) character.
  12. expect( isSurrogateHalf( String.fromCharCode( 0xD83D ) ) ).to.be.true;
  13. } );
  14. it( 'should return false if given character is not a surrogate half', () => {
  15. expect( isSurrogateHalf( 'a' ) ).to.be.false;
  16. expect( isSurrogateHalf( '𨭎' ) ).to.be.false;
  17. } );
  18. it( 'should return false for falsy values', () => {
  19. expect( isSurrogateHalf( null ) ).to.be.false;
  20. expect( isSurrogateHalf( undefined ) ).to.be.false;
  21. expect( isSurrogateHalf( false ) ).to.be.false;
  22. expect( isSurrogateHalf( 0 ) ).to.be.false;
  23. } );
  24. } );
  25. describe( 'isCombiningMark', () => {
  26. it( 'should return true if given character is a combining mark', () => {
  27. expect( isCombiningMark( '̣' ) ).to.be.true;
  28. } );
  29. it( 'should return false if given character is not a combining mark', () => {
  30. expect( isCombiningMark( 'a' ) ).to.be.false;
  31. expect( isCombiningMark( 'q̣' ) ).to.be.false;
  32. } );
  33. it( 'should return false for falsy values', () => {
  34. expect( isCombiningMark( null ) ).to.be.false;
  35. expect( isCombiningMark( undefined ) ).to.be.false;
  36. expect( isCombiningMark( false ) ).to.be.false;
  37. expect( isCombiningMark( 0 ) ).to.be.false;
  38. } );
  39. } );
  40. describe( 'isInsideSurrogatePair', () => {
  41. const testString = 'a𨭎b';
  42. it( 'should return true if given offset in a string is inside a surrogate pair', () => {
  43. expect( isInsideSurrogatePair( testString, 2 ) ).to.be.true;
  44. } );
  45. it( 'should return false if given offset in a string is not inside a surrogate pair', () => {
  46. expect( isInsideSurrogatePair( testString, 1 ) ).to.be.false;
  47. expect( isInsideSurrogatePair( testString, 3 ) ).to.be.false;
  48. } );
  49. it( 'should return false if given offset in a string is at the string beginning or end', () => {
  50. expect( isInsideSurrogatePair( testString, 0 ) ).to.be.false;
  51. expect( isInsideSurrogatePair( testString, 4 ) ).to.be.false;
  52. } );
  53. } );
  54. describe( 'isInsideCombinedSymbol', () => {
  55. const testString = 'aa̻̐ͩbb';
  56. it( 'should return true if given offset in a string is inside a symbol with combining mark', () => {
  57. expect( isInsideCombinedSymbol( testString, 2 ) ).to.be.true;
  58. expect( isInsideCombinedSymbol( testString, 3 ) ).to.be.true;
  59. expect( isInsideCombinedSymbol( testString, 4 ) ).to.be.true;
  60. } );
  61. it( 'should return false if given offset in a string is not inside a symbol with combining mark', () => {
  62. expect( isInsideCombinedSymbol( testString, 1 ) ).to.be.false;
  63. expect( isInsideCombinedSymbol( testString, 5 ) ).to.be.false;
  64. } );
  65. it( 'should return false if given offset in a string is at the string beginning or end', () => {
  66. expect( isInsideCombinedSymbol( testString, 0 ) ).to.be.false;
  67. expect( isInsideCombinedSymbol( testString, 6 ) ).to.be.false;
  68. } );
  69. } );
  70. } );
  71. } );