Text.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. topSuite("Ext.draw.sprite.Text", function() {
  2. var proto = Ext.draw.sprite.Text.prototype;
  3. describe('makeFontShorthand', function () {
  4. var attr = {
  5. fontVariant: 'small-caps',
  6. fontStyle: 'italic',
  7. fontWeight: 'bold',
  8. fontSize: '34px/100px',
  9. fontFamily: '"Times New Roman", serif'
  10. };
  11. it('should not have a leading or trailing space', function () {
  12. var font = '';
  13. var fakeSprite = {
  14. setAttributes: function (attr) {
  15. font = attr.font;
  16. }
  17. };
  18. proto.makeFontShorthand.call(fakeSprite, attr);
  19. expect(font.length).toEqual(Ext.String.trim(font).length);
  20. });
  21. it('should list all available values in the preferred order', function () {
  22. var font;
  23. var fakeSprite = {
  24. setAttributes: function (attr) {
  25. font = attr.font;
  26. }
  27. };
  28. proto.makeFontShorthand.call(fakeSprite, attr);
  29. expect(font).toEqual('italic small-caps bold 34px/100px "Times New Roman", serif');
  30. });
  31. it('needs to contain at least font-size and font-family', function () {
  32. var sprite = new Ext.draw.sprite.Text();
  33. sprite.setAttributes({
  34. fontWeight: 'bold',
  35. fontStyle: 'italic'
  36. });
  37. expect(sprite.attr.font).toEqual('italic bold 10px sans-serif');
  38. });
  39. });
  40. describe('parseFontShorthand', function () {
  41. it('needs to handle "normal" values properly', function () {
  42. var sprite = new Ext.draw.sprite.Text();
  43. sprite.setAttributes({
  44. font: 'normal 24px Verdana'
  45. });
  46. expect(sprite.attr.fontStyle).toEqual('');
  47. expect(sprite.attr.fontVariant).toEqual('');
  48. expect(sprite.attr.fontWeight).toEqual('');
  49. expect(sprite.attr.fontSize).toEqual('24px');
  50. expect(sprite.attr.fontFamily).toEqual('Verdana');
  51. });
  52. it('should ignore the "inherit" values', function () {
  53. var sprite = new Ext.draw.sprite.Text();
  54. sprite.setAttributes({
  55. font: 'inherit 24px Verdana'
  56. });
  57. expect(sprite.attr.fontStyle).toEqual('');
  58. expect(sprite.attr.fontVariant).toEqual('');
  59. expect(sprite.attr.fontWeight).toEqual('');
  60. expect(sprite.attr.fontSize).toEqual('24px');
  61. expect(sprite.attr.fontFamily).toEqual('Verdana');
  62. });
  63. it('should support font names with spaces in them', function () {
  64. var sprite = new Ext.draw.sprite.Text();
  65. sprite.setAttributes({
  66. font: 'x-large/110% "New Century Schoolbook", serif'
  67. });
  68. expect(sprite.attr.fontFamily).toEqual('"New Century Schoolbook", serif');
  69. });
  70. it('should support font families with more than one font name', function () {
  71. var sprite = new Ext.draw.sprite.Text();
  72. sprite.setAttributes({
  73. font: 'italic small-caps normal 13px/150% Arial, Helvetica, sans-serif'
  74. });
  75. expect(sprite.attr.fontFamily).toEqual('Arial, Helvetica, sans-serif');
  76. });
  77. it('should be able to handle fontSize/lineHeight values ' +
  78. 'by extracting fontSize and discarding lineHeigh', function () {
  79. var sprite = new Ext.draw.sprite.Text();
  80. sprite.setAttributes({
  81. font: 'x-large/110% "New Century Schoolbook", serif'
  82. });
  83. expect(sprite.attr.fontSize).toEqual('x-large');
  84. });
  85. it('should recognize percentage font sizes', function () {
  86. var sprite = new Ext.draw.sprite.Text();
  87. sprite.setAttributes({
  88. font: '80% sans-serif'
  89. });
  90. expect(sprite.attr.fontSize).toEqual('80%');
  91. });
  92. it('should recognize absolute font sizes', function () {
  93. var sprite = new Ext.draw.sprite.Text();
  94. sprite.setAttributes({
  95. font: 'small serif'
  96. });
  97. expect(sprite.attr.fontSize).toEqual('small');
  98. });
  99. it('should recognize font weight values', function () {
  100. var sprite = new Ext.draw.sprite.Text();
  101. sprite.setAttributes({
  102. font: 'italic 600 large Palatino, serif'
  103. });
  104. expect(sprite.attr.fontWeight).toEqual('600');
  105. });
  106. it('should recognize font variant values', function () {
  107. var sprite = new Ext.draw.sprite.Text();
  108. sprite.setAttributes({
  109. font: 'normal small-caps 120%/120% fantasy'
  110. });
  111. expect(sprite.attr.fontVariant).toEqual('small-caps');
  112. });
  113. it('should recognize font style values', function () {
  114. var sprite = new Ext.draw.sprite.Text();
  115. sprite.setAttributes({
  116. font: 'bold large oblique Palatino, serif'
  117. });
  118. expect(sprite.attr.fontStyle).toEqual('oblique');
  119. });
  120. });
  121. describe('fontWeight processor', function () {
  122. // See: http://www.w3.org/TR/css3-fonts/#propdef-font-weight
  123. var def = Ext.draw.sprite.Text.def,
  124. fontWeight = def.getProcessors().fontWeight;
  125. fontWeight = fontWeight.bind(def);
  126. it('should return an empty string for unrecognized values', function () {
  127. var a = fontWeight(Infinity),
  128. b = fontWeight(-Infinity),
  129. c = fontWeight(101),
  130. d = fontWeight('hello'),
  131. e = fontWeight('505'),
  132. f = fontWeight(NaN),
  133. g = fontWeight(null),
  134. h = fontWeight(undefined),
  135. i = fontWeight(true),
  136. j = fontWeight(false);
  137. expect(a).toEqual('');
  138. expect(b).toEqual('');
  139. expect(c).toEqual('');
  140. expect(d).toEqual('');
  141. expect(e).toEqual('');
  142. expect(f).toEqual('');
  143. expect(g).toEqual('');
  144. expect(h).toEqual('');
  145. expect(i).toEqual('');
  146. expect(j).toEqual('');
  147. });
  148. it('should accept strings that can be parsed to a valid number', function () {
  149. var a = fontWeight('700');
  150. expect(a).toEqual('700');
  151. });
  152. it('should always return a string', function () {
  153. var a = fontWeight(400);
  154. expect(a).toEqual('400');
  155. });
  156. it('should only accept numbers that are multiples of 100 in the [100,900] interval', function () {
  157. var a = fontWeight(300),
  158. b = fontWeight(350),
  159. c = fontWeight(0),
  160. d = fontWeight(1000);
  161. expect(a).toEqual('300');
  162. expect(b).toEqual('');
  163. expect(c).toEqual('');
  164. expect(d).toEqual('');
  165. });
  166. });
  167. });