UglifyJS/lib/index.html

261 lines
7.5 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<title>ColaScript Playground</title>
<script src="./utils.js"></script>
<script src="./ast.js"></script>
<script src="./parse.js"></script>
<script src="./transform.js"></script>
<script src="./scope.js"></script>
<script src="./output.js"></script>
<script src="./compress.js"></script>
<script src="./sourcemap.js"></script>
<script src="./mozilla-ast.js"></script>
<script src="./translate.js"></script>
<script src="./std.js"></script>
<style>
body {
padding: 0;
margin: 0;
line-height: 100%;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
textarea {
2014-04-28 08:20:12 +00:00
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 33.333%;
height: 100%;
font-size: 12px;
float: left;
font-family: "Andale Mono";
2014-04-28 08:20:12 +00:00
padding: 0 5px 26px 5px;
}
#controls {
position: fixed;
bottom: 0;
left: 0;
2014-04-20 15:57:10 +00:00
height: 25px;
width: 100%;
}
2014-04-20 15:57:10 +00:00
#controls .m {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#controls .bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: white;
opacity: 0.5;
}
2014-04-28 08:20:12 +00:00
#lenstat {
float: right;
margin: 5px 8px 0 0;
}
</style>
</head>
<body>
2014-04-28 08:20:12 +00:00
<textarea id="source" onkeyup="compile()">// `main` functions may binding to a onload event
// Functions can defined without `function` keyword, with and without `type`
main(){
// Unary operators
// Two variants of `isset` operator. Which is better?
bool _seted = true, _empty;
console.log("`_seted` is", _seted?? ? "seted" : "empty");
console.log("`_empty` is", isset _empty ? "seted" : "empty");
// `clone` operator
Array _first = [584], _second = clone _first;
_first.push(404);
console.log("`_first`:",_first,"`_second`:",_second);
// Binary operators
// `pow` operator
console.log("5 ** 3 =", 5 ** 3);
// `modulo` operator
console.log("10 %% 4 = ", 10 %% 4);
// `isset` conditional assigment
int a, b = 4;
console.log("`a` is", a, ", now `a` is", a ?= b);
console.log("`a` is", a, ", now `a` still", a ?= 584);
// `isset` binary conditional
int x, y = 5;
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y), x = 1;
console.log("`x` is", x, ", `y` is", y, ", x ? y = ", x ? y);
// `is` operator
console.log("'hello'", "hello" is String ? "is" : "isnt", "`String`");
// `isnt` operator
console.log("'goodby'", "goodby" isnt Number ? "isnt" : "is", "`Number`");
// Boolean alternatives
console.log("`yes` and `no`", yes is Boolean && no is Boolean ? "is" : "isnt", "`Boolean`");
console.log("`on` and `off`", on is Boolean && off is Boolean ? "is" : "isnt", "`Boolean`");
// Strings
// New quotes symbol, multi-lining and templating
2014-04-18 07:14:54 +00:00
console.log(`
Hello!
My name is ColaScript, and i know who you are }:->
2014-04-28 08:20:12 +00:00
User agent: @{navigator.userAgent}
Vendor: {{navigator.vendor}}
2014-04-18 07:14:54 +00:00
`);
2014-04-28 08:20:12 +00:00
// Raw strings
console.log(r"\n \r \t some@email.ru");
2014-04-28 08:20:12 +00:00
// Multiline RegExp's
2014-04-18 18:33:08 +00:00
RegExp isEmail = /
2014-04-18 07:14:54 +00:00
([\w-\.]+)
@
((?:[\w]+\.)+)
([a-zA-Z]{2,4})
2014-04-28 08:20:12 +00:00
/;
String email = r'some@email.ru';
console.log("@email {{ isEmail.test(email) ? "is" : "isnt" }} valid email adress");
// Arrays
// Empty getter and setter ( pushig )
Array arr = [2, 4, 8, 16, 32];
console.log("Last element of `arr` is", arr[]), arr[] = 64;
console.log("Now last element of `arr` is", arr[]);
// Splice assigment
arr[2..4] = [3, 2, 1];
console.log("`arr` is", arr);
// Range
console.log("`[0..10]` is", [0..10]);
// Calling function
console.log(Profile(
"Dan", "Green",
"HTML5", "JavaScript", "Dart", "PHP", "CSS3", "LESS", "Qt",
age : 19,
petName : "Felix"
));
2014-04-18 07:14:54 +00:00
}
2014-04-28 08:20:12 +00:00
// Functions
// Named, positional and splated arguments, with default values.
// Arrow functions.
Object Profile(String firstName, String secondName, String country = "Russia", Array skills..., age:, petName: "Tux"){
skills.forEach((val) => val == "JavaScript" && console.log("JavaScript - It Awesome!"));
return {
firstName : firstName,
secondName : secondName,
age : age,
country : country,
skills : skills,
petName : petName
};
}
</textarea>
<textarea id="translation"></textarea>
<textarea id="result"></textarea>
2014-04-20 15:57:10 +00:00
<div id="controls">
<div class="bg">&nbsp;</div>
<div class="m">
<button id="exec" onclick="exec()">Execute</button>
2014-04-22 13:33:43 +00:00
<button id="benchmark" onclick="benchmark()">Benchmark</button>
2014-04-20 15:57:10 +00:00
<input type="checkbox" id="is_js" onclick="compile()">
<label for="is_js">js parser</label>
2014-04-28 08:20:12 +00:00
<input type="checkbox" id="main_binding" onclick="compile()">
<label for="main_binding">`main` binding</label>
2014-04-20 15:57:10 +00:00
<span id="lenstat"></span>
</div>
</body>
<script>
var sourceArea = document.getElementById("source"),
translationArea = document.getElementById("translation"),
resultArea = document.getElementById("result"),
2014-04-16 18:39:27 +00:00
isjs = document.getElementById("is_js"),
2014-04-28 08:20:12 +00:00
mainbinding = document.getElementById("main_binding"),
source;
2014-04-18 07:14:54 +00:00
if(!localStorage.source) localStorage.source = source = sourceArea.value;
else sourceArea.value = source = localStorage.source;
2014-04-16 18:39:27 +00:00
isjs.checked = localStorage.isjs == "t";
2014-04-28 08:20:12 +00:00
mainbinding.checked = localStorage.mainbinding == "t";
function compile(){
source = sourceArea.value;
localStorage.source = source;
2014-04-16 18:39:27 +00:00
localStorage.isjs = isjs.checked ? "t" : "f";
2014-04-28 08:20:12 +00:00
localStorage.mainbinding = mainbinding.checked ? "t" : "f";
2014-04-22 13:33:43 +00:00
stream = new Cola.OutputStream({ beautify : true });
2014-04-18 06:10:20 +00:00
compressor = new Cola.Compressor({ is_js : isjs.checked });
try {
// 1. compile
2014-04-18 06:10:20 +00:00
ast = Cola.parse(source, { is_js : isjs.checked });
2014-04-28 08:20:12 +00:00
if(!isjs.checked) ast = ast.toJavaScript({ main_binding : mainbinding.checked });
ast.print(stream);
translationArea.value = stream.toString();
// 2. compress
ast.figure_out_scope();
ast = ast.transform(compressor);
// 3. mangle
ast.figure_out_scope();
ast.compute_char_frequency();
2014-04-22 13:33:43 +00:00
ast.mangle_names({ sort : true, toplevel : true });
2014-04-22 13:33:43 +00:00
stream = new Cola.OutputStream();
ast.print(stream);
resultArea.value = stream.toString();
} catch(e){
translationArea.value = '';
resultArea.value = '';
2014-04-20 18:56:47 +00:00
throw e;
}
2014-04-20 15:57:10 +00:00
document.querySelector('#lenstat').innerHTML = sourceArea.value.length+" : "+translationArea.value.length+" : "+resultArea.value.length;
}
2014-04-22 13:33:43 +00:00
function benchmark(){
2014-04-28 08:20:12 +00:00
console.time("ColaScript");
2014-04-22 13:33:43 +00:00
eval(resultArea.result);
2014-04-28 08:20:12 +00:00
console.timeEnd("ColaScript");
2014-04-22 13:33:43 +00:00
}
function exec(){
eval(resultArea.value)
}
function Translate(){
2014-04-17 16:21:45 +00:00
stream = new Cola.OutputStream({ beautify : true });
2014-04-16 18:39:27 +00:00
translate(Cola.parse(source, null, isjs.checked)).print(stream);
return stream.toString();
}
compile();
</script>
</html>