[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Web/Javascripts/prototype/ -> string.js (source)

   1  Object.extend(String.prototype, {
   2    gsub: function(pattern, replacement) {
   3      var result = '', source = this, match;
   4      replacement = arguments.callee.prepareReplacement(replacement);
   5      
   6      while (source.length > 0) {
   7        if (match = source.match(pattern)) {
   8          result += source.slice(0, match.index);
   9          result += (replacement(match) || '').toString();
  10          source  = source.slice(match.index + match[0].length);
  11        } else {
  12          result += source, source = '';
  13        }
  14      }
  15      return result;
  16    },
  17    
  18    sub: function(pattern, replacement, count) {
  19      replacement = this.gsub.prepareReplacement(replacement);
  20      count = count === undefined ? 1 : count;
  21      
  22      return this.gsub(pattern, function(match) {
  23        if (--count < 0) return match[0];
  24        return replacement(match);
  25      });
  26    },
  27    
  28    scan: function(pattern, iterator) {
  29      this.gsub(pattern, iterator);
  30      return this;
  31    },
  32    
  33    truncate: function(length, truncation) {
  34      length = length || 30;
  35      truncation = truncation === undefined ? '...' : truncation;
  36      return this.length > length ? 
  37        this.slice(0, length - truncation.length) + truncation : this;
  38    },
  39  
  40    strip: function() {
  41      return this.replace(/^\s+/, '').replace(/\s+$/, '');
  42    },
  43    
  44    stripTags: function() {
  45      return this.replace(/<\/?[^>]+>/gi, '');
  46    },
  47  
  48    stripScripts: function() {
  49      return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
  50    },
  51    
  52    extractScripts: function() {
  53      var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
  54      var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
  55      return (this.match(matchAll) || []).map(function(scriptTag) {
  56        return (scriptTag.match(matchOne) || ['', ''])[1];
  57      });
  58    },
  59    
  60    evalScripts: function() {
  61      return this.extractScripts().map(function(script) { return eval(script) });
  62    },
  63  
  64    escapeHTML: function() {
  65      var div = document.createElement('div');
  66      var text = document.createTextNode(this);
  67      div.appendChild(text);
  68      return div.innerHTML;
  69    },
  70  
  71    unescapeHTML: function() {
  72      var div = document.createElement('div');
  73      div.innerHTML = this.stripTags();
  74      return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
  75    },
  76    
  77    toQueryParams: function() {
  78      var pairs = this.match(/^\??(.*)$/)[1].split('&');
  79      return pairs.inject({}, function(params, pairString) {
  80        var pair = pairString.split('=');
  81        params[pair[0]] = pair[1];
  82        return params;
  83      });
  84    },
  85    
  86    toArray: function() {
  87      return this.split('');
  88    },
  89    
  90    camelize: function() {
  91      var oStringList = this.split('-');
  92      if (oStringList.length == 1) return oStringList[0];
  93        
  94      var camelizedString = this.indexOf('-') == 0
  95        ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) 
  96        : oStringList[0];
  97        
  98      for (var i = 1, len = oStringList.length; i < len; i++) {
  99        var s = oStringList[i];
 100        camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
 101      }
 102      
 103      return camelizedString;
 104    },
 105  
 106    inspect: function() {
 107      return "'" + this.replace(/\\/g, '\\\\').replace(/'/g, '\\\'') + "'";
 108    }
 109  });
 110  
 111  String.prototype.gsub.prepareReplacement = function(replacement) {
 112    if (typeof replacement == 'function') return replacement;
 113    var template = new Template(replacement);
 114    return function(match) { return template.evaluate(match) };
 115  }
 116  
 117  String.prototype.parseQuery = String.prototype.toQueryParams;
 118  
 119  var Template = Class.create();
 120  Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
 121  Template.prototype = {
 122    initialize: function(template, pattern) {
 123      this.template = template.toString();
 124      this.pattern  = pattern || Template.Pattern;
 125    },
 126    
 127    evaluate: function(object) {
 128      return this.template.gsub(this.pattern, function(match) {
 129        var before = match[1];
 130        if (before == '\\') return match[2];
 131        return before + (object[match[3]] || '').toString();
 132      });
 133    }
 134  }


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7