ScriptFormatterWorker.js [plain text]
onmessage = function(event) {
var result = {};
if (event.data.mimeType === "text/html") {
var formatter = new HTMLScriptFormatter();
result = formatter.format(event.data.content);
} else {
result.mapping = { original: [0], formatted: [0] };
result.content = formatScript(event.data.content, result.mapping, 0, 0);
}
postMessage(result);
};
function formatScript(content, mapping, offset, formattedOffset)
{
var formattedContent;
try {
var tokenizer = new Tokenizer(content);
var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset);
var formatter = new JavaScriptFormatter(tokenizer, builder);
formatter.format();
formattedContent = builder.content();
} catch (e) {
formattedContent = content;
}
return formattedContent;
}
WebInspector = {};
importScripts("SourceTokenizer.js");
importScripts("SourceHTMLTokenizer.js");
HTMLScriptFormatter = function()
{
WebInspector.SourceHTMLTokenizer.call(this);
}
HTMLScriptFormatter.prototype = {
format: function(content)
{
this.line = content;
this._content = content;
this._formattedContent = "";
this._mapping = { original: [0], formatted: [0] };
this._position = 0;
var cursor = 0;
while (cursor < this._content.length)
cursor = this.nextToken(cursor);
this._formattedContent += this._content.substring(this._position);
return { content: this._formattedContent, mapping: this._mapping };
},
scriptStarted: function(cursor)
{
this._formattedContent += this._content.substring(this._position, cursor);
this._formattedContent += "\n";
this._position = cursor;
},
scriptEnded: function(cursor)
{
if (cursor === this._position)
return;
var scriptContent = this._content.substring(this._position, cursor);
var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length);
this._formattedContent += formattedScriptContent;
this._position = cursor;
},
styleSheetStarted: function(cursor)
{
},
styleSheetEnded: function(cursor)
{
}
}
HTMLScriptFormatter.prototype.__proto__ = WebInspector.SourceHTMLTokenizer.prototype;
function require()
{
return parse;
}
var exports = {};
importScripts("UglifyJS/parse-js.js");
var parse = exports;
importScripts("JavaScriptFormatter.js");