Chromium Code Reviews| 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..ee377fa25ff0a56ecf158be10bf61acec05a0a73 100644 |
| --- a/Source/devtools/front_end/ScriptFormatterWorker.js |
| +++ b/Source/devtools/front_end/ScriptFormatterWorker.js |
| @@ -43,6 +43,9 @@ onmessage = function(event) { |
| self[event.data.method](event.data.params); |
| }; |
| +/** |
| + * @param {Object} params |
| + */ |
| function format(params) |
| { |
| // Default to a 4-space indent. |
| @@ -52,6 +55,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); |
| @@ -59,6 +65,10 @@ function format(params) |
| postMessage(result); |
| } |
| +/** |
| + * @param {number} totalLength |
| + * @param {number} chunkSize |
| + */ |
| function getChunkCount(totalLength, chunkSize) |
| { |
| if (totalLength <= chunkSize) |
| @@ -69,6 +79,9 @@ function getChunkCount(totalLength, chunkSize) |
| return (partialLength / chunkSize) + (remainder ? 1 : 0); |
| } |
| +/** |
| + * @param {Object} params |
| + */ |
| function outline(params) |
| { |
| const chunkSize = 100000; // characters per data chunk |
| @@ -142,12 +155,20 @@ function outline(params) |
| postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); |
| } |
| +/** |
| + * @param {string} content |
| + * @param {{original: Array.<number>, formatted: Array.<number>}} mapping |
| + * @param {number} offset |
| + * @param {number} formattedOffset |
| + * @param {string} indentString |
| + * @return {string} |
| + */ |
| 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 +178,28 @@ function formatScript(content, mapping, offset, formattedOffset, indentString) |
| return formattedContent; |
| } |
| +/** |
| + * @param {string} content |
| + * @param {{original: Array.<number>, formatted: Array.<number>}} mapping |
| + * @param {number} offset |
| + * @param {number} formattedOffset |
| + * @param {string} indentString |
| + * @return {string} |
| + */ |
| +function formatCSS(content, mapping, offset, formattedOffset, indentString) |
|
pfeldman
2013/09/30 15:46:06
Should not be global
|
| +{ |
| + 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 = {}; |
| @@ -165,12 +208,18 @@ Array.prototype.keySet = function() |
| return keys; |
| }; |
| +/** |
| + * @param {string} indentString |
| + */ |
| HTMLScriptFormatter = function(indentString) |
|
vsevik
2013/09/30 16:12:13
HTMLFormatter?
|
| { |
| this._indentString = indentString; |
| } |
| HTMLScriptFormatter.prototype = { |
| + /** |
| + * @param {string} content |
| + */ |
| format: function(content) |
| { |
| this.line = content; |
| @@ -180,15 +229,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)); |
| @@ -197,14 +256,53 @@ HTMLScriptFormatter.prototype = { |
| return { content: this._formattedContent, mapping: this._mapping }; |
| }, |
| + /** |
| + * @param {number} cursor |
| + */ |
| _scriptStarted: function(cursor) |
| { |
| + this._handleSubFormatterStart(cursor); |
| + }, |
| + |
| + /** |
| + * @param {number} cursor |
| + */ |
| + _scriptEnded: function(cursor) |
| + { |
| + this._handleSubFormatterEnd(formatScript, cursor); |
| + }, |
| + |
| + /** |
| + * @param {number} cursor |
| + */ |
| + _styleStarted: function(cursor) |
| + { |
| + this._handleSubFormatterStart(cursor); |
| + }, |
| + |
| + /** |
| + * @param {number} cursor |
| + */ |
| + _styleEnded: function(cursor) |
| + { |
| + this._handleSubFormatterEnd(formatCSS, cursor); |
| + }, |
| + |
| + /** |
| + * @param {number} cursor |
| + */ |
| + _handleSubFormatterStart: function(cursor) |
| + { |
| this._formattedContent += this._content.substring(this._position, cursor); |
| this._formattedContent += "\n"; |
| this._position = cursor; |
| }, |
| - _scriptEnded: function(cursor) |
| + /** |
| + * @param {function(string, Array.<number>, number, number, string)} formatFunction |
| + * @param {number} cursor |
| + */ |
| + _handleSubFormatterEnd: function(formatFunction, cursor) |
| { |
| if (cursor === this._position) |
| return; |
| @@ -212,11 +310,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 +327,4 @@ importScripts("UglifyJS/parse-js.js"); |
| var parse = exports; |
| importScripts("JavaScriptFormatter.js"); |
| +importScripts("CSSFormatter.js"); |