util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // To avoid duplicates conflicting
  2. var jQueryCrayon = jQuery;
  3. var CRAYON_DEBUG = false;
  4. (function ($) {
  5. $(document).ready(function() {
  6. CrayonUtil.init();
  7. });
  8. CrayonUtil = new function() {
  9. var base = this;
  10. var settings = null;
  11. base.init = function() {
  12. settings = CrayonSyntaxSettings;
  13. };
  14. base.addPrefixToID = function (id) {
  15. return id.replace(/^([#.])?(.*)$/, '$1' + settings.prefix + '$2');
  16. };
  17. base.removePrefixFromID = function(id) {
  18. var re = new RegExp('^[#.]?' + settings.prefix, 'i');
  19. return id.replace(re, '');
  20. };
  21. base.cssElem = function (id) {
  22. return $(base.addPrefixToID(id));
  23. };
  24. base.setDefault = function (v, d) {
  25. return (typeof v == 'undefined') ? d : v;
  26. };
  27. base.setMax = function (v, max) {
  28. return v <= max ? v : max;
  29. };
  30. base.setMin = function (v, min) {
  31. return v >= min ? v : min;
  32. };
  33. base.setRange = function (v, min, max) {
  34. return base.setMax(base.setMin(v, min), max);
  35. };
  36. base.initFancybox = function() {
  37. if (fancyboxInit) {
  38. // Initialise a custom version of Fancybox to avoid conflicting
  39. fancyboxInit(window, document, $, 'crayonFancybox');
  40. }
  41. }
  42. };
  43. // http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t
  44. // [name] is the name of the event "click", "mouseover", ..
  45. // same as you'd pass it to bind()
  46. // [fn] is the handler function
  47. $.fn.bindFirst = function(name, fn) {
  48. // bind as you normally would
  49. // don't want to miss out on any jQuery magic
  50. this.bind(name, fn);
  51. // Thanks to a comment by @Martin, adding support for
  52. // namespaced events too.
  53. var handlers = this.data('events')[name.split('.')[0]];
  54. // take out the handler we just inserted from the end
  55. var handler = handlers.pop();
  56. // move it at the beginning
  57. handlers.splice(0, 0, handler);
  58. };
  59. })(jQueryCrayon);
  60. if (typeof CrayonTagEditorSettings == 'undefined') {
  61. // WP may have already added it
  62. CrayonTagEditorSettings = {};
  63. CrayonSettings = {};
  64. }
  65. RegExp.prototype.execAll = function(string) {
  66. var matches = [];
  67. var match = null;
  68. while ((match = this.exec(string)) != null) {
  69. var matchArray = [];
  70. for ( var i in match) {
  71. if (parseInt(i) == i) {
  72. matchArray.push(match[i]);
  73. }
  74. }
  75. matches.push(matchArray);
  76. }
  77. return matches;
  78. };
  79. // Escape regex chars with \
  80. RegExp.prototype.escape = function(text) {
  81. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  82. };
  83. String.prototype.sliceReplace = function(start, end, repl) {
  84. return this.substring(0, start) + repl + this.substring(end);
  85. };
  86. String.prototype.escape = function() {
  87. var tagsToReplace = {
  88. '&': '&amp;',
  89. '<': '&lt;',
  90. '>': '&gt;'
  91. };
  92. return this.replace(/[&<>]/g, function(tag) {
  93. return tagsToReplace[tag] || tag;
  94. });
  95. };
  96. String.prototype.linkify = function(target) {
  97. target = typeof target != 'undefined' ? target : '';
  98. return this.replace(/(http(s)?:\/\/(\S)+)/gmi, '<a href="$1" target="' + target + '">$1</a>');
  99. };
  100. function console_log(string) {
  101. if (typeof console != 'undefined' && CRAYON_DEBUG) {
  102. console.log(string);
  103. }
  104. }
  105. // # is left unencoded
  106. function crayon_escape(string) {
  107. if (typeof encodeURIComponent == 'function') {
  108. return encodeURIComponent(string);
  109. } else if (typeof escape != 'function') {
  110. return escape(string);
  111. } else {
  112. return string;
  113. }
  114. }
  115. function crayon_decode_html(str) {
  116. return String(str).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(
  117. /&gt;/g, '>');
  118. }
  119. function crayon_encode_html(str) {
  120. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(
  121. />/g, '&gt;');
  122. }
  123. var CrayonSyntaxUtil = new function() {
  124. this.getExt = function(str) {
  125. if (str.indexOf('.') == -1) {
  126. return undefined;
  127. }
  128. var ext = str.split('.');
  129. if (ext.length) {
  130. ext = ext[ext.length - 1];
  131. } else {
  132. ext = '';
  133. }
  134. return ext;
  135. };
  136. };