| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 function format(params) | 46 function format(params) |
| 47 { | 47 { |
| 48 // Default to a 4-space indent. | 48 // Default to a 4-space indent. |
| 49 var indentString = params.indentString || " "; | 49 var indentString = params.indentString || " "; |
| 50 var result = {}; | 50 var result = {}; |
| 51 | 51 |
| 52 if (params.mimeType === "text/html") { | 52 if (params.mimeType === "text/html") { |
| 53 var formatter = new HTMLScriptFormatter(indentString); | 53 var formatter = new HTMLScriptFormatter(indentString); |
| 54 result = formatter.format(params.content); | 54 result = formatter.format(params.content); |
| 55 } else if (params.mimeType === "text/css") { |
| 56 result.mapping = { original: [0], formatted: [0] }; |
| 57 result.content = formatCSS(params.content, result.mapping, 0, 0, indentS
tring); |
| 55 } else { | 58 } else { |
| 56 result.mapping = { original: [0], formatted: [0] }; | 59 result.mapping = { original: [0], formatted: [0] }; |
| 57 result.content = formatScript(params.content, result.mapping, 0, 0, inde
ntString); | 60 result.content = formatScript(params.content, result.mapping, 0, 0, inde
ntString); |
| 58 } | 61 } |
| 59 postMessage(result); | 62 postMessage(result); |
| 60 } | 63 } |
| 61 | 64 |
| 62 function getChunkCount(totalLength, chunkSize) | 65 function getChunkCount(totalLength, chunkSize) |
| 63 { | 66 { |
| 64 if (totalLength <= chunkSize) | 67 if (totalLength <= chunkSize) |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 tokenizer(line, processToken); | 143 tokenizer(line, processToken); |
| 141 } | 144 } |
| 142 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); | 145 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); |
| 143 } | 146 } |
| 144 | 147 |
| 145 function formatScript(content, mapping, offset, formattedOffset, indentString) | 148 function formatScript(content, mapping, offset, formattedOffset, indentString) |
| 146 { | 149 { |
| 147 var formattedContent; | 150 var formattedContent; |
| 148 try { | 151 try { |
| 149 var tokenizer = new Tokenizer(content); | 152 var tokenizer = new Tokenizer(content); |
| 150 var builder = new FormattedContentBuilder(tokenizer.content(), mapping,
offset, formattedOffset, indentString); | 153 var builder = new JavaScriptFormattedContentBuilder(tokenizer.content(),
mapping, offset, formattedOffset, indentString); |
| 151 var formatter = new JavaScriptFormatter(tokenizer, builder); | 154 var formatter = new JavaScriptFormatter(tokenizer, builder); |
| 152 formatter.format(); | 155 formatter.format(); |
| 153 formattedContent = builder.content(); | 156 formattedContent = builder.content(); |
| 154 } catch (e) { | 157 } catch (e) { |
| 155 formattedContent = content; | 158 formattedContent = content; |
| 156 } | 159 } |
| 157 return formattedContent; | 160 return formattedContent; |
| 158 } | 161 } |
| 159 | 162 |
| 163 function formatCSS(content, mapping, offset, formattedOffset, indentString) |
| 164 { |
| 165 var formattedContent; |
| 166 try { |
| 167 var builder = new CSSFormattedContentBuilder(content, mapping, offset, f
ormattedOffset, indentString); |
| 168 var formatter = new CSSFormatter(content, builder); |
| 169 formatter.format(); |
| 170 formattedContent = builder.content(); |
| 171 } catch (e) { |
| 172 formattedContent = content; |
| 173 } |
| 174 return formattedContent; |
| 175 } |
| 176 |
| 160 Array.prototype.keySet = function() | 177 Array.prototype.keySet = function() |
| 161 { | 178 { |
| 162 var keys = {}; | 179 var keys = {}; |
| 163 for (var i = 0; i < this.length; ++i) | 180 for (var i = 0; i < this.length; ++i) |
| 164 keys[this[i]] = true; | 181 keys[this[i]] = true; |
| 165 return keys; | 182 return keys; |
| 166 }; | 183 }; |
| 167 | 184 |
| 168 HTMLScriptFormatter = function(indentString) | 185 HTMLScriptFormatter = function(indentString) |
| 169 { | 186 { |
| 170 this._indentString = indentString; | 187 this._indentString = indentString; |
| 171 } | 188 } |
| 172 | 189 |
| 173 HTMLScriptFormatter.prototype = { | 190 HTMLScriptFormatter.prototype = { |
| 174 format: function(content) | 191 format: function(content) |
| 175 { | 192 { |
| 176 this.line = content; | 193 this.line = content; |
| 177 this._content = content; | 194 this._content = content; |
| 178 this._formattedContent = ""; | 195 this._formattedContent = ""; |
| 179 this._mapping = { original: [0], formatted: [0] }; | 196 this._mapping = { original: [0], formatted: [0] }; |
| 180 this._position = 0; | 197 this._position = 0; |
| 181 | 198 |
| 182 var scriptOpened = false; | 199 var scriptOpened = false; |
| 200 var styleOpened = false; |
| 183 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html"
); | 201 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html"
); |
| 184 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { | 202 function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { |
| 185 if (tokenValue.toLowerCase() === "<script" && tokenType === "xml-tag
") { | 203 if (tokenType !== "xml-tag") |
| 204 return; |
| 205 if (tokenValue.toLowerCase() === "<script") { |
| 186 scriptOpened = true; | 206 scriptOpened = true; |
| 187 } else if (scriptOpened && tokenValue === ">" && tokenType === "xml-
tag") { | 207 } else if (scriptOpened && tokenValue === ">") { |
| 188 scriptOpened = false; | 208 scriptOpened = false; |
| 189 this._scriptStarted(tokenEnd); | 209 this._scriptStarted(tokenEnd); |
| 190 } else if (tokenValue.toLowerCase() === "</script" && tokenType ===
"xml-tag") { | 210 } else if (tokenValue.toLowerCase() === "</script") { |
| 191 this._scriptEnded(tokenStart); | 211 this._scriptEnded(tokenStart); |
| 212 } else if (tokenValue.toLowerCase() === "<style") { |
| 213 styleOpened = true; |
| 214 } else if (styleOpened && tokenValue === ">") { |
| 215 styleOpened = false; |
| 216 this._styleStarted(tokenEnd); |
| 217 } else if (tokenValue.toLowerCase() === "</style") { |
| 218 this._styleEnded(tokenStart); |
| 192 } | 219 } |
| 193 } | 220 } |
| 194 tokenizer(content, processToken.bind(this)); | 221 tokenizer(content, processToken.bind(this)); |
| 195 | 222 |
| 196 this._formattedContent += this._content.substring(this._position); | 223 this._formattedContent += this._content.substring(this._position); |
| 197 return { content: this._formattedContent, mapping: this._mapping }; | 224 return { content: this._formattedContent, mapping: this._mapping }; |
| 198 }, | 225 }, |
| 199 | 226 |
| 200 _scriptStarted: function(cursor) | 227 _scriptStarted: function(cursor) |
| 201 { | 228 { |
| 229 this._handleSubFormatterStart(cursor); |
| 230 }, |
| 231 |
| 232 _scriptEnded: function(cursor) |
| 233 { |
| 234 this._handleSubFormatterEnd(formatScript, cursor); |
| 235 }, |
| 236 |
| 237 _styleStarted: function(cursor) |
| 238 { |
| 239 this._handleSubFormatterStart(cursor); |
| 240 }, |
| 241 |
| 242 _styleEnded: function(cursor) |
| 243 { |
| 244 this._handleSubFormatterEnd(formatCSS, cursor); |
| 245 }, |
| 246 |
| 247 _handleSubFormatterStart: function(cursor) |
| 248 { |
| 202 this._formattedContent += this._content.substring(this._position, cursor
); | 249 this._formattedContent += this._content.substring(this._position, cursor
); |
| 203 this._formattedContent += "\n"; | 250 this._formattedContent += "\n"; |
| 204 this._position = cursor; | 251 this._position = cursor; |
| 205 }, | 252 }, |
| 206 | 253 |
| 207 _scriptEnded: function(cursor) | 254 _handleSubFormatterEnd: function(formatFunction, cursor) |
| 208 { | 255 { |
| 209 if (cursor === this._position) | 256 if (cursor === this._position) |
| 210 return; | 257 return; |
| 211 | 258 |
| 212 var scriptContent = this._content.substring(this._position, cursor); | 259 var scriptContent = this._content.substring(this._position, cursor); |
| 213 this._mapping.original.push(this._position); | 260 this._mapping.original.push(this._position); |
| 214 this._mapping.formatted.push(this._formattedContent.length); | 261 this._mapping.formatted.push(this._formattedContent.length); |
| 215 var formattedScriptContent = formatScript(scriptContent, this._mapping,
this._position, this._formattedContent.length, this._indentString); | 262 var formattedScriptContent = formatFunction(scriptContent, this._mapping
, this._position, this._formattedContent.length, this._indentString); |
| 216 | 263 |
| 217 this._formattedContent += formattedScriptContent; | 264 this._formattedContent += formattedScriptContent; |
| 218 this._position = cursor; | 265 this._position = cursor; |
| 219 }, | 266 } |
| 220 } | 267 } |
| 221 | 268 |
| 222 function require() | 269 function require() |
| 223 { | 270 { |
| 224 return parse; | 271 return parse; |
| 225 } | 272 } |
| 226 | 273 |
| 227 var exports = {}; | 274 var exports = {}; |
| 228 importScripts("UglifyJS/parse-js.js"); | 275 importScripts("UglifyJS/parse-js.js"); |
| 229 var parse = exports; | 276 var parse = exports; |
| 230 | 277 |
| 231 importScripts("JavaScriptFormatter.js"); | 278 importScripts("JavaScriptFormatter.js"); |
| 279 importScripts("CSSFormatter.js"); |
| OLD | NEW |