87147d23e7392d5b2510ab7889dda20e5f83d655.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. exports.smallImage = function smallImage() {
  2. return "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
  3. };
  4. exports.bind = function(callback, context) {
  5. return function() {
  6. return callback.apply(context, arguments);
  7. };
  8. };
  9. /*
  10. * base64-arraybuffer
  11. * https://github.com/niklasvh/base64-arraybuffer
  12. *
  13. * Copyright (c) 2012 Niklas von Hertzen
  14. * Licensed under the MIT license.
  15. */
  16. exports.decode64 = function(base64) {
  17. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  18. var len = base64.length, i, encoded1, encoded2, encoded3, encoded4, byte1, byte2, byte3;
  19. var output = "";
  20. for (i = 0; i < len; i+=4) {
  21. encoded1 = chars.indexOf(base64[i]);
  22. encoded2 = chars.indexOf(base64[i+1]);
  23. encoded3 = chars.indexOf(base64[i+2]);
  24. encoded4 = chars.indexOf(base64[i+3]);
  25. byte1 = (encoded1 << 2) | (encoded2 >> 4);
  26. byte2 = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  27. byte3 = ((encoded3 & 3) << 6) | encoded4;
  28. if (encoded3 === 64) {
  29. output += String.fromCharCode(byte1);
  30. } else if (encoded4 === 64 || encoded4 === -1) {
  31. output += String.fromCharCode(byte1, byte2);
  32. } else{
  33. output += String.fromCharCode(byte1, byte2, byte3);
  34. }
  35. }
  36. return output;
  37. };
  38. exports.getBounds = function(node) {
  39. if (node.getBoundingClientRect) {
  40. var clientRect = node.getBoundingClientRect();
  41. var width = node.offsetWidth == null ? clientRect.width : node.offsetWidth;
  42. return {
  43. top: clientRect.top,
  44. bottom: clientRect.bottom || (clientRect.top + clientRect.height),
  45. right: clientRect.left + width,
  46. left: clientRect.left,
  47. width: width,
  48. height: node.offsetHeight == null ? clientRect.height : node.offsetHeight
  49. };
  50. }
  51. return {};
  52. };
  53. exports.offsetBounds = function(node) {
  54. var parent = node.offsetParent ? exports.offsetBounds(node.offsetParent) : {top: 0, left: 0};
  55. return {
  56. top: node.offsetTop + parent.top,
  57. bottom: node.offsetTop + node.offsetHeight + parent.top,
  58. right: node.offsetLeft + parent.left + node.offsetWidth,
  59. left: node.offsetLeft + parent.left,
  60. width: node.offsetWidth,
  61. height: node.offsetHeight
  62. };
  63. };
  64. exports.parseBackgrounds = function(backgroundImage) {
  65. var whitespace = ' \r\n\t',
  66. method, definition, prefix, prefix_i, block, results = [],
  67. mode = 0, numParen = 0, quote, args;
  68. var appendResult = function() {
  69. if(method) {
  70. if (definition.substr(0, 1) === '"') {
  71. definition = definition.substr(1, definition.length - 2);
  72. }
  73. if (definition) {
  74. args.push(definition);
  75. }
  76. if (method.substr(0, 1) === '-' && (prefix_i = method.indexOf('-', 1 ) + 1) > 0) {
  77. prefix = method.substr(0, prefix_i);
  78. method = method.substr(prefix_i);
  79. }
  80. results.push({
  81. prefix: prefix,
  82. method: method.toLowerCase(),
  83. value: block,
  84. args: args,
  85. image: null
  86. });
  87. }
  88. args = [];
  89. method = prefix = definition = block = '';
  90. };
  91. args = [];
  92. method = prefix = definition = block = '';
  93. backgroundImage.split("").forEach(function(c) {
  94. if (mode === 0 && whitespace.indexOf(c) > -1) {
  95. return;
  96. }
  97. switch(c) {
  98. case '"':
  99. if(!quote) {
  100. quote = c;
  101. } else if(quote === c) {
  102. quote = null;
  103. }
  104. break;
  105. case '(':
  106. if(quote) {
  107. break;
  108. } else if(mode === 0) {
  109. mode = 1;
  110. block += c;
  111. return;
  112. } else {
  113. numParen++;
  114. }
  115. break;
  116. case ')':
  117. if (quote) {
  118. break;
  119. } else if(mode === 1) {
  120. if(numParen === 0) {
  121. mode = 0;
  122. block += c;
  123. appendResult();
  124. return;
  125. } else {
  126. numParen--;
  127. }
  128. }
  129. break;
  130. case ',':
  131. if (quote) {
  132. break;
  133. } else if(mode === 0) {
  134. appendResult();
  135. return;
  136. } else if (mode === 1) {
  137. if (numParen === 0 && !method.match(/^url$/i)) {
  138. args.push(definition);
  139. definition = '';
  140. block += c;
  141. return;
  142. }
  143. }
  144. break;
  145. }
  146. block += c;
  147. if (mode === 0) {
  148. method += c;
  149. } else {
  150. definition += c;
  151. }
  152. });
  153. appendResult();
  154. return results;
  155. };