Index: Source/devtools/front_end/ScriptFormatterWorker.js |
diff --git a/Source/devtools/front_end/ScriptFormatterWorker.js b/Source/devtools/front_end/ScriptFormatterWorker.js |
index a8d6586a8f1d9fa8371d3bf701be2dc2dd9856e2..812a053a76e34c277f27b87035029439128c5527 100644 |
--- a/Source/devtools/front_end/ScriptFormatterWorker.js |
+++ b/Source/devtools/front_end/ScriptFormatterWorker.js |
@@ -52,6 +52,9 @@ function format(params) |
if (params.mimeType === "text/html") { |
var formatter = new HTMLScriptFormatter(indentString); |
result = formatter.format(params.content); |
+ } else if (params.mimeType === "text/css") { |
+ result.mapping = { original: [0], formatted: [0] }; |
+ result.content = formatCSS(params.content, result.mapping, 0, 0, indentString); |
} else { |
result.mapping = { original: [0], formatted: [0] }; |
result.content = formatScript(params.content, result.mapping, 0, 0, indentString); |
@@ -147,7 +150,7 @@ function formatScript(content, mapping, offset, formattedOffset, indentString) |
var formattedContent; |
try { |
var tokenizer = new Tokenizer(content); |
- var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString); |
+ var builder = new JavaScriptFormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString); |
var formatter = new JavaScriptFormatter(tokenizer, builder); |
formatter.format(); |
formattedContent = builder.content(); |
@@ -157,6 +160,20 @@ function formatScript(content, mapping, offset, formattedOffset, indentString) |
return formattedContent; |
} |
+function formatCSS(content, mapping, offset, formattedOffset, indentString) |
+{ |
+ var formattedContent; |
+ try { |
+ var builder = new CSSFormattedContentBuilder(content, mapping, offset, formattedOffset, indentString); |
+ var formatter = new CSSFormatter(content, builder); |
+ formatter.format(); |
+ formattedContent = builder.content(); |
+ } catch (e) { |
+ formattedContent = content; |
+ } |
+ return formattedContent; |
+} |
+ |
Array.prototype.keySet = function() |
{ |
var keys = {}; |
@@ -180,15 +197,25 @@ HTMLScriptFormatter.prototype = { |
this._position = 0; |
var scriptOpened = false; |
+ var styleOpened = false; |
var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html"); |
function processToken(tokenValue, tokenType, tokenStart, tokenEnd) { |
- if (tokenValue.toLowerCase() === "<script" && tokenType === "xml-tag") { |
+ if (tokenType !== "xml-tag") |
+ return; |
+ if (tokenValue.toLowerCase() === "<script") { |
scriptOpened = true; |
- } else if (scriptOpened && tokenValue === ">" && tokenType === "xml-tag") { |
+ } else if (scriptOpened && tokenValue === ">") { |
scriptOpened = false; |
this._scriptStarted(tokenEnd); |
- } else if (tokenValue.toLowerCase() === "</script" && tokenType === "xml-tag") { |
+ } else if (tokenValue.toLowerCase() === "</script") { |
this._scriptEnded(tokenStart); |
+ } else if (tokenValue.toLowerCase() === "<style") { |
+ styleOpened = true; |
+ } else if (styleOpened && tokenValue === ">") { |
+ styleOpened = false; |
+ this._styleStarted(tokenEnd); |
+ } else if (tokenValue.toLowerCase() === "</style") { |
+ this._styleEnded(tokenStart); |
} |
} |
tokenizer(content, processToken.bind(this)); |
@@ -199,12 +226,32 @@ HTMLScriptFormatter.prototype = { |
_scriptStarted: function(cursor) |
{ |
+ this._handleSubFormatterStart(cursor); |
+ }, |
+ |
+ _scriptEnded: function(cursor) |
+ { |
+ this._handleSubFormatterEnd(formatScript, cursor); |
+ }, |
+ |
+ _styleStarted: function(cursor) |
+ { |
+ this._handleSubFormatterStart(cursor); |
+ }, |
+ |
+ _styleEnded: function(cursor) |
+ { |
+ this._handleSubFormatterEnd(formatCSS, cursor); |
+ }, |
+ |
+ _handleSubFormatterStart: function(cursor) |
+ { |
this._formattedContent += this._content.substring(this._position, cursor); |
this._formattedContent += "\n"; |
this._position = cursor; |
}, |
- _scriptEnded: function(cursor) |
+ _handleSubFormatterEnd: function(formatFunction, cursor) |
{ |
if (cursor === this._position) |
return; |
@@ -212,11 +259,11 @@ HTMLScriptFormatter.prototype = { |
var scriptContent = this._content.substring(this._position, cursor); |
this._mapping.original.push(this._position); |
this._mapping.formatted.push(this._formattedContent.length); |
- var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString); |
+ var formattedScriptContent = formatFunction(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString); |
this._formattedContent += formattedScriptContent; |
this._position = cursor; |
- }, |
+ } |
} |
function require() |
@@ -229,3 +276,4 @@ importScripts("UglifyJS/parse-js.js"); |
var parse = exports; |
importScripts("JavaScriptFormatter.js"); |
+importScripts("CSSFormatter.js"); |