2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 onmessage = function(event) {
33 if (event.data.mimeType === "text/html") {
34 var formatter = new HTMLScriptFormatter();
35 result = formatter.format(event.data.content);
37 result.mapping = { original: [0], formatted: [0] };
38 result.content = formatScript(event.data.content, result.mapping, 0, 0);
43 function formatScript(content, mapping, offset, formattedOffset)
47 var tokenizer = new Tokenizer(content);
48 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset);
49 var formatter = new JavaScriptFormatter(tokenizer, builder);
51 formattedContent = builder.content();
53 formattedContent = content;
55 return formattedContent;
59 importScripts("SourceTokenizer.js");
60 importScripts("SourceHTMLTokenizer.js");
62 HTMLScriptFormatter = function()
64 WebInspector.SourceHTMLTokenizer.call(this);
67 HTMLScriptFormatter.prototype = {
68 format: function(content)
71 this._content = content;
72 this._formattedContent = "";
73 this._mapping = { original: [0], formatted: [0] };
77 while (cursor < this._content.length)
78 cursor = this.nextToken(cursor);
80 this._formattedContent += this._content.substring(this._position);
81 return { content: this._formattedContent, mapping: this._mapping };
84 scriptStarted: function(cursor)
86 this._formattedContent += this._content.substring(this._position, cursor);
87 this._formattedContent += "\n";
88 this._position = cursor;
91 scriptEnded: function(cursor)
93 if (cursor === this._position)
96 var scriptContent = this._content.substring(this._position, cursor);
97 this._mapping.original.push(this._position);
98 this._mapping.formatted.push(this._formattedContent.length);
99 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length);
101 this._formattedContent += formattedScriptContent;
102 this._position = cursor;
105 styleSheetStarted: function(cursor)
109 styleSheetEnded: function(cursor)
114 HTMLScriptFormatter.prototype.__proto__ = WebInspector.SourceHTMLTokenizer.prototype;
122 importScripts("UglifyJS/parse-js.js");
125 importScripts("JavaScriptFormatter.js");