showdown.helpers.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Created by Estevao on 27/01/2017.
  3. */
  4. /*jshint expr: true*/
  5. /*jshint -W053 */
  6. /*jshint -W010 */
  7. /*jshint -W009 */
  8. var showdown = require('../../.build/showdown.js');
  9. describe('encodeEmailAddress()', function () {
  10. 'use strict';
  11. var encoder = showdown.helper.encodeEmailAddress,
  12. email = 'foobar@example.com',
  13. encodedEmail = encoder(email);
  14. it('should encode email', function () {
  15. encodedEmail.should.not.equal(email);
  16. });
  17. it('should decode to original email', function () {
  18. var decodedEmail = encodedEmail.replace(/&#(.+?);/g, function (wm, cc) {
  19. if (cc.charAt(0) === 'x') {
  20. //hex
  21. return String.fromCharCode('0' + cc);
  22. } else {
  23. //dec
  24. return String.fromCharCode(cc);
  25. }
  26. });
  27. decodedEmail.should.equal(email);
  28. });
  29. });
  30. describe('isString()', function () {
  31. 'use strict';
  32. var isString = showdown.helper.isString;
  33. it('should return true for new String Object', function () {
  34. isString(new String('some string')).should.be.true;
  35. });
  36. it('should return true for String Object', function () {
  37. isString(String('some string')).should.be.true;
  38. });
  39. it('should return true for string literal', function () {
  40. isString('some string').should.be.true;
  41. });
  42. it('should return false for integers', function () {
  43. isString(5).should.be.false;
  44. });
  45. it('should return false for random objects', function () {
  46. isString({foo: 'bar'}).should.be.false;
  47. });
  48. it('should return false for arrays', function () {
  49. isString(['bar']).should.be.false;
  50. });
  51. });
  52. describe('isFunction()', function () {
  53. 'use strict';
  54. var isFunction = showdown.helper.isFunction;
  55. it('should return true for closures', function () {
  56. isFunction(function () {}).should.be.true;
  57. });
  58. it('should return true for defined functions', function () {
  59. function foo () {}
  60. isFunction(foo).should.be.true;
  61. });
  62. it('should return true for function variables', function () {
  63. var bar = function () {};
  64. isFunction(bar).should.be.true;
  65. });
  66. it('should return false for hash objects', function () {
  67. isFunction({}).should.be.false;
  68. });
  69. it('should return false for objects', function () {
  70. isFunction(new Object ()).should.be.false;
  71. });
  72. it('should return false for string primitives', function () {
  73. isFunction('foo').should.be.false;
  74. });
  75. });
  76. describe('isArray()', function () {
  77. 'use strict';
  78. var isArray = showdown.helper.isArray;
  79. it('should return true for short syntax arrays', function () {
  80. isArray([]).should.be.true;
  81. });
  82. it('should return true for array objects', function () {
  83. var myArr = new Array();
  84. isArray(myArr).should.be.true;
  85. });
  86. it('should return false for functions', function () {
  87. isArray(function () {}).should.be.false;
  88. function baz () {}
  89. isArray(baz).should.be.false;
  90. });
  91. it('should return false for objects', function () {
  92. isArray({}).should.be.false;
  93. isArray(new Object ()).should.be.false;
  94. });
  95. it('should return false for strings', function () {
  96. isArray('foo').should.be.false;
  97. isArray(new String('foo')).should.be.false;
  98. });
  99. });
  100. describe('isUndefined()', function () {
  101. 'use strict';
  102. var isUndefined = showdown.helper.isUndefined;
  103. it('should return true if nothing is passed', function () {
  104. isUndefined().should.be.true;
  105. });
  106. it('should return true if a variable is initialized but not defined', function () {
  107. var myVar;
  108. isUndefined(myVar).should.be.true;
  109. });
  110. it('should return false for null', function () {
  111. isUndefined(null).should.be.false;
  112. });
  113. it('should return false for 0', function () {
  114. isUndefined(0).should.be.false;
  115. });
  116. it('should return false for empty string', function () {
  117. isUndefined('').should.be.false;
  118. });
  119. it('should return false for empty booleans false or true', function () {
  120. isUndefined(false).should.be.false;
  121. isUndefined(true).should.be.false;
  122. });
  123. it('should return false for anything not undefined', function () {
  124. isUndefined('foo').should.be.false;
  125. isUndefined(2).should.be.false;
  126. isUndefined({}).should.be.false;
  127. });
  128. });
  129. describe('stdExtName()', function () {
  130. 'use strict';
  131. var stdExtName = showdown.helper.stdExtName;
  132. it('should remove certain chars', function () {
  133. var str = 'bla_- \nbla';
  134. //[_?*+\/\\.^-]
  135. stdExtName(str).should.not.match(/[_?*+\/\\.^-]/g);
  136. });
  137. it('should make everything lowercase', function () {
  138. var str = 'BLABLA';
  139. //[_?*+\/\\.^-]
  140. stdExtName(str).should.equal('blabla');
  141. });
  142. });
  143. describe('forEach()', function () {
  144. 'use strict';
  145. var forEach = showdown.helper.forEach;
  146. it('should throw an error if first parameter is undefined', function () {
  147. (function () {forEach();}).should.throw('obj param is required');
  148. });
  149. it('should throw an error if second parameter is undefined', function () {
  150. (function () {forEach([]);}).should.throw('callback param is required');
  151. });
  152. it('should throw an error if second parameter is not a function', function () {
  153. (function () {forEach([], 'foo');}).should.throw('callback param must be a function/closure');
  154. });
  155. it('should throw an error if first parameter is not an object or an array', function () {
  156. (function () {forEach('foo', function () {});}).should.throw('obj does not seem to be an array or an iterable object');
  157. });
  158. it('should not throw even if object is empty', function () {
  159. (function () {forEach({}, function () {});}).should.not.throw();
  160. });
  161. it('should iterate array items', function () {
  162. var myArray = ['banana', 'orange', 'grape'];
  163. forEach(myArray, function (val, key, obj) {
  164. key.should.be.a('number');
  165. (key % 1).should.equal(0);
  166. val.should.equal(myArray[key]);
  167. obj.should.equal(myArray);
  168. });
  169. });
  170. it('should iterate over object properties', function () {
  171. var myObj = {foo: 'banana', bar: 'orange', baz: 'grape'};
  172. forEach(myObj, function (val, key, obj) {
  173. myObj.should.have.ownProperty(key);
  174. val.should.equal(myObj[key]);
  175. obj.should.equal(myObj);
  176. });
  177. });
  178. it('should iterate only over object own properties', function () {
  179. var Obj1 = {foo: 'banana'},
  180. myObj = Object.create(Obj1);
  181. myObj.bar = 'orange';
  182. myObj.baz = 'grape';
  183. myObj.should.have.ownProperty('bar');
  184. myObj.should.have.ownProperty('baz');
  185. myObj.should.not.have.ownProperty('foo');
  186. forEach(myObj, function (val, key) {
  187. key.should.not.equal('foo');
  188. });
  189. });
  190. });
  191. describe('matchRecursiveRegExp()', function () {
  192. 'use strict';
  193. var rRegExp = showdown.helper.matchRecursiveRegExp;
  194. it('should match nested elements', function () {
  195. var result = rRegExp('<div><div>a</div></div>', '<div\\b[^>]*>', '</div>', 'gim');
  196. result.should.deep.equal([['<div><div>a</div></div>', '<div>a</div>', '<div>', '</div>']]);
  197. });
  198. });