Skip to content

Instantly share code, notes, and snippets.

@leegao
Created August 7, 2010 17:43
Show Gist options
  • Save leegao/513014 to your computer and use it in GitHub Desktop.
Save leegao/513014 to your computer and use it in GitHub Desktop.
// A Simple Javascript syntax highlighted Editor within 1023 bytes (< 1Kb)
// Demo: http://failboat.me/jpad.html
(d=document).write('<style>#p,#d{text-shadow:0 0 20px #39E;top:0;left:0;white-space:pre;color:#756;width:100%;height:100%;padding:10px;border:none;line-height:20px;position:absolute;background:transparent;font:14px "Courier New"}#p{opacity:0.4}</style><div id="d">JS</div><textarea id="p"></textarea>');p=d[g="getElementById"]("p");n="length";function j(b,a){l=[];for(_ in a){i=0;$=a[_];for(m=b.match(a[_]);$.test(b);)l.push([$.lastIndex-m[i][n],m[i++][n],_])}l.sort(function(c,f){return c[0]+1/c[1]-f[0]-1/f[1]});h=0;z=[];for(_ in l){$=l[_];e=$[0]+$[1];if(!(e<=h)){z.push([h,$[0]-h,g],$);h=e}}z.push([h,b[n],g]);return z}p.onkeyup=function(){var b=d[g]("d"),a=p.value;t=j(a,{"#288;border-bottom:dashed 1px #ccc":/\/\/.*|\/\*(.|\n)+\*\//g,"#D24":/"[^"\n]*"|'[^'\n]*'/g,"#08c":/\b(else|switch|break|throw|case|catch|new|finally|null|try|const|for|continue|function|var|if|return|delete|while|do|with|in)(?!\w)/g});s="";for(i in t){y=t[i];s+="<span style='color:"+y[2]+"'>"+a[ss="substr"](y[0],y[1])+"</span>"}b.innerHTML=s}
@leegao
Copy link
Author

leegao commented Jul 4, 2011

Final submission, which was just shy of full compliance with regex due to 13 bytes.

(d=document).write('<style>#p,#d{top:0;left:0;white-space:pre;color:#756;width:100%;height:100%;padding:9px;border:none;line-height:20px;position:absolute;background:transparent;font:14px "Courier New"}#p{opacity:0.4}</style><div id="d"></div><textarea id="p"></textarea>');p=d[g="getElementById"]("p");j="push";n="length";function c(a,b){l=[];z=[];for(_ in b){i=0;$=b[_];for(m=a.match(b[_]);$.test(a);)l[j]([$.lastIndex-m[i][n],m[i++][n],_])}l.sort(function(f,k){return f[0]-k[0]});h=0;for(_ in l)if(!((e=($=l[_])[0]+$[1])<=h)){z[j]([h,$[0]-h,g],$);h=e}z[j]([h,a[n],g]);return z}function o(a){t=c(a,{"#288":/\/\/.*|\/\*(.|\n)+\*\//g,"#D24":/".*?"|'.*?'/g,"#08c":/\b(else|switch|break|case|catch|new|finally|null|try|for|continue|function|var|if|return|while|do|with|in)(?!\w)/g});s="";for(i in t){y=t[i];s+="<span style='color:"+y[2]+"'>"+a.substr(y[0],y[1])+"</span>"}return s}p.onkeyup=function(){d[g]("d").innerHTML=o(p.value[u="replace"](/</g,"&lt;")[u](/\\.|&#/g,function(a){return"\\&#"+a.charCodeAt(1)+";"}))}

http://js1k.com/2010-first/source/817

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment