util.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. $B64 = function(str) {
  2. return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  3. function toSolidBytes(match, p1) {
  4. return String.fromCharCode('0x' + p1);
  5. }));
  6. }
  7. $.fn.to_em_raw = function(settings){
  8. settings = jQuery.extend({
  9. scope: 'body'
  10. }, settings);
  11. var that = parseInt(this[0]||"0",10),
  12. scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;">&nbsp;</div>').appendTo(settings.scope),
  13. scopeVal = scopeTest.height();
  14. scopeTest.remove();
  15. return (that / scopeVal).toFixed(8);
  16. };
  17. $.fn.to_em = function(settings){
  18. return $(this[0]).to_em_raw(settings) + 'em';
  19. };
  20. $.fn.get_val = function () {
  21. if ($(this).is('input') || $(this).is('select') || $(this).is('textarea')) {
  22. return $(this).val();
  23. } else {
  24. var ret = $(this).html().replace(/<div>/g,'').replace(/<\/div>/g,'\n').replace(/<br\/?>/g,'\n').replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&apos;/g,'\'').trim();
  25. if (ret == '') $(this).empty();
  26. return ret;
  27. }
  28. };
  29. $.fn.set_val = function (val) {
  30. if ($(this).is('input') || $(this).is('select') || $(this).is('textarea')) {
  31. $(this).val(val);
  32. } else {
  33. $(this).text(val);
  34. }
  35. };
  36. $.fn.serializePrettyXML = function () {
  37. return vkbeautify.xml(this.serializeXML(),' ');
  38. };
  39. $.fn.serializeXML = function () {
  40. var out = '';
  41. if (typeof XMLSerializer == 'function') {
  42. var xs = new XMLSerializer();
  43. this.each(function() {
  44. out += xs.serializeToString(this);
  45. });
  46. } else if (this[0] && this[0].xml != 'undefined') {
  47. this.each(function() {
  48. out += this.xml;
  49. });
  50. }
  51. return out;
  52. };
  53. $.fn.serializePrettyXML = function () {
  54. return vkbeautify.xml(this.serializeXML(),' ');
  55. };
  56. String.prototype.repeat = function(num) {
  57. return new Array(num + 1).join(this);
  58. };
  59. String.prototype.unserialize = function() {
  60. var data = this.split("&");
  61. var ret = new Array();
  62. $.each(data, function(){
  63. var properties = this.split("=");
  64. ret.push([properties[0], properties[1]]);
  65. });
  66. return ret;
  67. };
  68. $XR = function(xmlstr) {
  69. if (typeof xmlstr == "string") {
  70. return $.parseXML(xmlstr);
  71. } else {
  72. return $(xmlstr.ownerDocument || xmlstr);
  73. }
  74. };
  75. $X = function(xmlstr) {
  76. return $($.parseXML(xmlstr).documentElement);
  77. };