Changeset 84620 in webkit


Ignore:
Timestamp:
Apr 22, 2011 5:34:33 AM (13 years ago)
Author:
podivilov@chromium.org
Message:

2011-04-22 Pavel Podivilov <podivilov@chromium.org>

Reviewed by Pavel Feldman.

Web Inspector: js formatter should never insert new tokens or remove existing tokens and comments.
https://bugs.webkit.org/show_bug.cgi?id=59191

  • WebCore.gypi:
  • WebCore.vcproj/WebCore.vcproj:
  • inspector/front-end/ScriptFormatter.js: (WebInspector.ScriptFormatter.prototype.formatContent.didFormatChunks): (WebInspector.ScriptFormatter.prototype.formatContent):
  • inspector/front-end/ScriptFormatterWorker.js: (onmessage):
  • inspector/front-end/SourceFile.js: (WebInspector.SourceFile.prototype._loadResourceContent): (WebInspector.FormattedSourceFile.prototype._didRequestContent): (WebInspector.FormattedSourceMapping): (WebInspector.FormattedSourceMapping.prototype.scriptLocationToSourceLine): (WebInspector.FormattedSourceMapping.prototype.sourceLineToScriptLocation): (WebInspector.FormattedSourceMapping.prototype._convertPosition):
  • inspector/front-end/UglifyJS/JavaScriptFormatter.js: Added. (FormattedContentBuilder): Helper class for building formatted script content. (FormattedContentBuilder.prototype.addToken): (FormattedContentBuilder.prototype.addSpace): (FormattedContentBuilder.prototype.addNewLine): (FormattedContentBuilder.prototype.increaseNestingLevel): (FormattedContentBuilder.prototype.decreaseNestingLevel): (FormattedContentBuilder.prototype.content): (FormattedContentBuilder.prototype.mapping): (Tokenizer): Wrapper class for UglifyJS tokenizer. (Tokenizer.prototype.content): (Tokenizer.prototype.next): (Tokenizer.prototype._convertUglifyToken): (JavaScriptFormatter): New formatter based on UglifyJS tokenizer. (JavaScriptFormatter.prototype.format):
  • inspector/front-end/UglifyJS/process.js: Removed.
  • inspector/front-end/WebKit.qrc:
Location:
trunk/Source/WebCore
Files:
1 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r84619 r84620  
     12011-04-22  Pavel Podivilov  <podivilov@chromium.org>
     2
     3        Reviewed by Pavel Feldman.
     4
     5        Web Inspector: js formatter should never insert new tokens or remove existing tokens and comments.
     6        https://bugs.webkit.org/show_bug.cgi?id=59191
     7
     8        * WebCore.gypi:
     9        * WebCore.vcproj/WebCore.vcproj:
     10        * inspector/front-end/ScriptFormatter.js:
     11        (WebInspector.ScriptFormatter.prototype.formatContent.didFormatChunks):
     12        (WebInspector.ScriptFormatter.prototype.formatContent):
     13        * inspector/front-end/ScriptFormatterWorker.js:
     14        (onmessage):
     15        * inspector/front-end/SourceFile.js:
     16        (WebInspector.SourceFile.prototype._loadResourceContent):
     17        (WebInspector.FormattedSourceFile.prototype._didRequestContent):
     18        (WebInspector.FormattedSourceMapping):
     19        (WebInspector.FormattedSourceMapping.prototype.scriptLocationToSourceLine):
     20        (WebInspector.FormattedSourceMapping.prototype.sourceLineToScriptLocation):
     21        (WebInspector.FormattedSourceMapping.prototype._convertPosition):
     22        * inspector/front-end/UglifyJS/JavaScriptFormatter.js: Added.
     23        (FormattedContentBuilder): Helper class for building formatted script content.
     24        (FormattedContentBuilder.prototype.addToken):
     25        (FormattedContentBuilder.prototype.addSpace):
     26        (FormattedContentBuilder.prototype.addNewLine):
     27        (FormattedContentBuilder.prototype.increaseNestingLevel):
     28        (FormattedContentBuilder.prototype.decreaseNestingLevel):
     29        (FormattedContentBuilder.prototype.content):
     30        (FormattedContentBuilder.prototype.mapping):
     31        (Tokenizer): Wrapper class for UglifyJS tokenizer.
     32        (Tokenizer.prototype.content):
     33        (Tokenizer.prototype.next):
     34        (Tokenizer.prototype._convertUglifyToken):
     35        (JavaScriptFormatter): New formatter based on UglifyJS tokenizer.
     36        (JavaScriptFormatter.prototype.format):
     37        * inspector/front-end/UglifyJS/process.js: Removed.
     38        * inspector/front-end/WebKit.qrc:
     39
    1402011-04-22  Mikhail Naganov  <mnaganov@chromium.org>
    241
  • trunk/Source/WebCore/WebCore.gypi

    r84619 r84620  
    63126312        ],
    63136313        'webinspector_uglifyjs_files': [
     6314            'inspector/front-end/UglifyJS/JavaScriptFormatter.js',
    63146315            'inspector/front-end/UglifyJS/parse-js.js',
    6315             'inspector/front-end/UglifyJS/process.js',
    63166316        ],
    63176317        'webinspector_image_files': [
  • trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj

    r84619 r84620  
    6747867478                                </File>
    6747967479                                <File
     67480                                        RelativePath="..\inspector\front-end\UglifyJS\JavaScriptFormatter.js"
     67481                                        >
     67482                                </File>
     67483                                <File
    6748067484                                        RelativePath="..\inspector\front-end\UglifyJS\parse-js.js"
    6748167485                                        >
     
    6749567499                                <File
    6749667500                                        RelativePath="..\inspector\front-end\Popover.js"
    67497                                         >
    67498                                 </File>
    67499                                 <File
    67500                                         RelativePath="..\inspector\front-end\UglifyJS\process.js"
    6750167501                                        >
    6750267502                                </File>
  • trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js

    r83290 r84620  
    7777    formatContent: function(text, scripts, callback)
    7878    {
     79        text = text.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
    7980        var scriptRanges = WebInspector.ScriptFormatter.findScriptRanges(text.lineEndings(), scripts);
    8081        var chunks = this._splitContentIntoChunks(text, scriptRanges);
     
    8384        {
    8485            var result = this._buildContentFromChunks(chunks);
     86            result.mapping.originalLineEndings = text.lineEndings();
    8587            callback(result.text, result.mapping);
    8688        }
  • trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js

    r83748 r84620  
    3131onmessage = function(event) {
    3232    var source = event.data;
    33     var formattedSource = beautify(source);
    34     var mapping = buildMapping(source, formattedSource);
    35     postMessage({ formattedSource: formattedSource, mapping: mapping });
     33    var tokenizer = new Tokenizer(source);
     34    var builder = new FormattedContentBuilder(tokenizer.content());
     35    var formatter = new JavaScriptFormatter(tokenizer, builder);
     36    formatter.format();
     37    postMessage({ formattedSource: builder.content(), mapping: builder.mapping() });
    3638};
    37 
    38 function beautify(source)
    39 {
    40     var ast = parse.parse(source);
    41     var beautifyOptions = {
    42         indent_level: 4,
    43         indent_start: 0,
    44         quote_keys: false,
    45         space_colon: false
    46     };
    47     return process.gen_code(ast, beautifyOptions);
    48 }
    49 
    50 function buildMapping(source, formattedSource)
    51 {
    52     var mapping = { original: [], formatted: [] };
    53     var lastPosition = 0;
    54     var regexp = /(^|[^\\])\b((?=\D)[\$\.\w]+)\b/g;
    55     while (true) {
    56         var match = regexp.exec(formattedSource);
    57         if (!match)
    58             break;
    59         var position = source.indexOf(match[2], lastPosition);
    60         if (position === -1)
    61             throw "No match found in original source for " + match[2];
    62         mapping.original.push(position);
    63         mapping.formatted.push(match.index + match[1].length);
    64         lastPosition = position + match[2].length;
    65     }
    66     return mapping;
    67 }
    6839
    6940function require()
     
    7647var parse = exports;
    7748
    78 var exports = {};
    79 importScripts("UglifyJS/process.js");
    80 var process = exports;
     49importScripts("UglifyJS/JavaScriptFormatter.js");
  • trunk/Source/WebCore/inspector/front-end/SourceFile.js

    r83748 r84620  
    138138            }
    139139
    140             if (resource.type === WebInspector.Resource.Type.Script)
    141                 this._didRequestContent("text/javascript", text);
    142             else {
    143                 // WebKit html lexer normalizes line endings and scripts are passed to VM with "\n" line endings.
    144                 // However, resource content has original line endings, so we have to normalize line endings here.
    145                 this._didRequestContent("text/html", text.replace(/\r\n/g, "\n"));
    146             }
     140            var mimeType = resource.type === WebInspector.Resource.Type.Script ? "text/javascript" : "text/html";
     141            this._didRequestContent(mimeType, text);
    147142        }
    148143        resource.requestContent(didRequestContent.bind(this));
     
    255250        function didFormatContent(formattedText, mapping)
    256251        {
    257             this._mapping = new WebInspector.FormattedSourceMapping(this._scripts, text, formattedText, mapping);
     252            this._mapping = new WebInspector.FormattedSourceMapping(this._scripts, mapping.originalLineEndings, formattedText.lineEndings(), mapping);
    258253            WebInspector.SourceFile.prototype._didRequestContent.call(this, mimeType, formattedText);
    259254        }
     
    294289}
    295290
    296 WebInspector.FormattedSourceMapping = function(scripts, originalText, formattedText, mapping)
     291WebInspector.FormattedSourceMapping = function(scripts, originalLineEndings, formattedLineEndings, mapping)
    297292{
    298293    WebInspector.SourceMapping.call(this, scripts);
    299     this._originalLineEndings = originalText.lineEndings();
    300     this._formattedLineEndings = formattedText.lineEndings();
     294    this._originalLineEndings = originalLineEndings;
     295    this._formattedLineEndings = formattedLineEndings;
    301296    this._mapping = mapping;
    302297}
     
    306301    {
    307302        var originalPosition = WebInspector.ScriptFormatter.locationToPosition(this._originalLineEndings, location);
    308         var index = this._mapping.original.upperBound(originalPosition - 1);
    309         var formattedPosition = this._mapping.formatted[index];
     303        var formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition);
    310304        return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition).lineNumber;
    311305    },
     
    314308    {
    315309        var formattedPosition = WebInspector.ScriptFormatter.lineToPosition(this._formattedLineEndings, lineNumber);
    316         var index = this._mapping.formatted.upperBound(formattedPosition - 1);
    317         var originalPosition = this._mapping.original[index];
     310        var originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
    318311        var originalLocation = WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition);
    319312        return WebInspector.SourceMapping.prototype._sourceLocationToScriptLocation.call(this, originalLocation.lineNumber, originalLocation.columnNumber);
     313    },
     314
     315    _convertPosition: function(positions1, positions2, position)
     316    {
     317        var index = positions1.upperBound(position) - 1;
     318        var delta = position - positions1[index];
     319        return Math.min(positions2[index] + delta, positions2[index + 1]);
    320320    }
    321321}
  • trunk/Source/WebCore/inspector/front-end/WebKit.qrc

    r84619 r84620  
    124124    <file>WelcomeView.js</file>
    125125    <file>WorkersSidebarPane.js</file>
     126    <file>UglifyJS/JavaScriptFormatter.js</file>
    126127    <file>UglifyJS/parse-js.js</file>
    127     <file>UglifyJS/process.js</file>
    128128    <file>audits.css</file>
    129129    <file>goToLineDialog.css</file>
Note: See TracChangeset for help on using the changeset viewer.