showdown.helpers.js 6.8 KB

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