Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

toStandard //JavaScript Function

This function tries to parse bad html and transform into xhtml, by lower-casing the tags, properties and duplicating single properties (readonly becomes readonly="readonly"), my function is very generic.

example:
   1  
   2  s = '<INPUT type=text value="a b c" readonly x="123">blabla<DIV></DIV>';
   3  prompt("", toStandard(s));


code:
   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  function toStandard(s){
   6  	return s.replace(/<(\/?\w+)([^>]*)>/g, function(s, t, c, d){
   7  		return "<" + t.toLowerCase() + c.replace(/\b(\w+)(?:=((?:(?=(["'])).((?!\3)(?:.|\n))*\3)|\S*))?/g, function(s, p, v, a){
   8  			return (p = p.toLowerCase()) + "=" + (a = a ? "" : '"') + (v || p) + a;
   9  		}) + ">";
  10  	});
  11  };

Generic base conversor //Javascript Object


Minimum Common Multiple

[UPDATED CODE AND HELP CAN BE FOUND HERE]


   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com/number/base-conversor [v1.0]
   4  
   5  Conversor = {
   6  	h: '0123456789abcdefghijklmnopqrstuvwxyz',
   7  
   8  	int2base: function( n, base ){
   9  		if( base < 2 || base > this.h.length )
  10  			throw new Error( "base inválida" );
  11  		for( var n = parseInt( n ) || 0, result = ""; n; result = this.h.charAt( n % base ) + result, n = Math.floor( n / base ) );
  12  		return result;
  13  	},
  14  	base2int: function( s, base ){
  15  		for( var i = -1, l = s.length, result = 0; ++i < l; result = result * base + this.h.indexOf( s.charAt( i ) ) );
  16  		return result;
  17  	}
  18  }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS