Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Unified Diff: Source/devtools/front_end/ScriptFormatterWorker.js

Issue 18347003: DevTools: Implement CSS pretty-printing (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebased Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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");

Powered by Google App Engine
This is Rietveld 408576698