Changeset 86443 in webkit


Ignore:
Timestamp:
May 13, 2011 10:25:57 AM (13 years ago)
Author:
podivilov@chromium.org
Message:

2011-05-05 Pavel Podivilov <podivilov@chromium.org>

Reviewed by Yury Semikhatsky.

Web Inspector: fix source mapping for formatted scripts.
https://bugs.webkit.org/show_bug.cgi?id=60263

  • inspector/debugger/resources/obfuscated.js: (formatted1): (formatted2):
  • inspector/debugger/script-formatter.html:

2011-05-05 Pavel Podivilov <podivilov@chromium.org>

Reviewed by Yury Semikhatsky.

Web Inspector: fix source mapping for formatted scripts.
https://bugs.webkit.org/show_bug.cgi?id=60263

Source mapping is broken for script chunks that are already formatted at the beginning
and at the end of the script (see script-formatter.html).

  • inspector/front-end/ScriptFormatter.js: (WebInspector.ScriptFormatter.prototype._didFormatContent): (WebInspector.FormattedSourceMapping): (WebInspector.FormattedSourceMapping.prototype.originalToFormatted): (WebInspector.FormattedSourceMapping.prototype.formattedToOriginal): (WebInspector.FormattedSourceMapping.prototype._convertPosition):
  • inspector/front-end/ScriptFormatterWorker.js: (onmessage): (HTMLScriptFormatter.prototype.format):
  • inspector/front-end/SourceFile.js: (WebInspector.FormattedSourceFile.prototype._didRequestContent): (WebInspector.SourceMappingForFormattedSourceFile): (WebInspector.SourceMappingForFormattedSourceFile.prototype.scriptLocationToSourceLine): (WebInspector.SourceMappingForFormattedSourceFile.prototype.sourceLineToScriptLocation):
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r86442 r86443  
     12011-05-05  Pavel Podivilov  <podivilov@chromium.org>
     2
     3        Reviewed by Yury Semikhatsky.
     4
     5        Web Inspector: fix source mapping for formatted scripts.
     6        https://bugs.webkit.org/show_bug.cgi?id=60263
     7
     8        * inspector/debugger/resources/obfuscated.js:
     9        (formatted1):
     10        (formatted2):
     11        * inspector/debugger/script-formatter.html:
     12
    1132011-05-13  Adam Roben  <aroben@apple.com>
    214
  • trunk/LayoutTests/inspector/debugger/resources/obfuscated.js

    r84625 r86443  
     1function formatted1()
     2{
     3    var variable1 = 0;
     4}
     5
    16try{onmessage=function(event){var source=event.data;var formattedSource=beautify(source);var mapping=buildMapping(source,formattedSource);postMessage({formattedSource:formattedSource,mapping:mapping})};function beautify(source){var ast=parse.parse(source);var beautifyOptions=
    27{indent_level:4,indent_start:0,quote_keys:false,space_colon:false};return process.gen_code(ast,beautifyOptions)}function buildMapping(source,formattedSource){var mapping={original:[],formatted:[]};var lastPosition=0;var regexp=/(^|[^\\])\b((?=\D)[\$\.\w]+)\b/g;while(true)
    38{var match=regexp.exec(formattedSource);if(!match)break;var position=source.indexOf(match[2],lastPosition);if(position===-1)throw"No match found in original source for "+match[2];mapping.original.push(position);mapping.formatted.push(match.index+match[1].length);
    49lastPosition=position+match[2].length}return mapping}function require(){return parse}var exports={};importScripts("UglifyJS/parse-js.js");var parse=exports;var exports={};importScripts("UglifyJS/process.js");var process=exports;}catch(e){}
     10
     11function formatted2()
     12{
     13    var variable2 = 0;
     14}
  • trunk/LayoutTests/inspector/debugger/script-formatter.html

    r85321 r86443  
    4545        function testSourceMapping(next)
    4646        {
    47             worker.onmessage = InspectorTest.safeWrap(function(event)
     47            var formatter = new WebInspector.ScriptFormatter();
     48
     49            InspectorTest.showScriptSource("obfuscated.js", didShowScriptSource);
     50            function didShowScriptSource(sourceFrame)
     51            {
     52                formatter.formatContent("text/javascript", sourceFrame._content, didFormatContent);
     53            }
     54
     55            function didFormatContent(content, mapping)
    4856            {
    4957                var source = WebInspector.panels.scripts.visibleView._content;
    50                 var formattedSource = event.data.content;
    51                 var mapping = event.data.mapping;
     58                var formattedSource = content;
    5259
    5360                function testMapping(string)
    5461                {
    55                     var position = source.indexOf(string);
    56                     var index = mapping.original.upperBound(position) - 1;
    57                     var delta = position - mapping.original[index];
    58                     var formattedPosition = Math.min(mapping.formatted[index] + delta, mapping.formatted[index + 1]);
    59                     InspectorTest.assertEquals(string, formattedSource.substr(formattedPosition, string.length));
     62                    var originalLocation = WebInspector.ScriptFormatter.positionToLocation(source.lineEndings(), source.indexOf(string));
     63                    var formattedLocation = mapping.originalToFormatted(originalLocation);
     64                    var formattedPosition = WebInspector.ScriptFormatter.locationToPosition(formattedSource.lineEndings(), formattedLocation);
     65                    var expectedFormattedPosition = formattedSource.indexOf(string);
     66                    InspectorTest.assertEquals(expectedFormattedPosition, formattedPosition);
    6067                }
     68
     69                testMapping("function");
     70                testMapping("formatted1");
     71                testMapping("variable1");
    6172
    6273                testMapping("onmessage");
     
    6677                testMapping("importScripts");
    6778
     79                testMapping("formatted2");
     80                testMapping("variable2");
     81
    6882                next();
    69             });
    70 
    71             worker.onerror = function(event)
    72             {
    73                 InspectorTest.addResult("Error in worker: " + event.data);
    74                 next();
    75             };
    76 
    77             InspectorTest.showScriptSource("obfuscated.js", didShowScriptSource);
    78 
    79             function didShowScriptSource(sourceFrame)
    80             {
    81                 worker.postMessage({ mimeType: "text/javascript", content: sourceFrame._content });
    8283            }
    8384        },
  • trunk/Source/WebCore/ChangeLog

    r86442 r86443  
     12011-05-05  Pavel Podivilov  <podivilov@chromium.org>
     2
     3        Reviewed by Yury Semikhatsky.
     4
     5        Web Inspector: fix source mapping for formatted scripts.
     6        https://bugs.webkit.org/show_bug.cgi?id=60263
     7
     8        Source mapping is broken for script chunks that are already formatted at the beginning
     9        and at the end of the script (see script-formatter.html).
     10
     11        * inspector/front-end/ScriptFormatter.js:
     12        (WebInspector.ScriptFormatter.prototype._didFormatContent):
     13        (WebInspector.FormattedSourceMapping):
     14        (WebInspector.FormattedSourceMapping.prototype.originalToFormatted):
     15        (WebInspector.FormattedSourceMapping.prototype.formattedToOriginal):
     16        (WebInspector.FormattedSourceMapping.prototype._convertPosition):
     17        * inspector/front-end/ScriptFormatterWorker.js:
     18        (onmessage):
     19        (HTMLScriptFormatter.prototype.format):
     20        * inspector/front-end/SourceFile.js:
     21        (WebInspector.FormattedSourceFile.prototype._didRequestContent):
     22        (WebInspector.SourceMappingForFormattedSourceFile):
     23        (WebInspector.SourceMappingForFormattedSourceFile.prototype.scriptLocationToSourceLine):
     24        (WebInspector.SourceMappingForFormattedSourceFile.prototype.sourceLineToScriptLocation):
     25
    1262011-05-13  Adam Roben  <aroben@apple.com>
    227
  • trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js

    r84625 r86443  
    4242}
    4343
    44 WebInspector.ScriptFormatter.lineToPosition = function(lineEndings, lineNumber)
    45 {
    46     return this.locationToPosition(lineEndings, { lineNumber: lineNumber, columnNumber: 0 });
    47 }
    48 
    4944WebInspector.ScriptFormatter.positionToLocation = function(lineEndings, position)
    5045{
     
    7065    {
    7166        var task = this._tasks.shift();
    72         event.data.mapping.originalLineEndings = task.data.content.lineEndings();
    73         task.callback(event.data.content, event.data.mapping);
     67        var originalContent = task.data.content;
     68        var formattedContent = event.data.content;
     69        var sourceMapping = new WebInspector.FormattedSourceMapping(originalContent.lineEndings(), formattedContent.lineEndings(), event.data.mapping);
     70        task.callback(formattedContent, sourceMapping);
    7471    }
    7572}
     73
     74WebInspector.FormattedSourceMapping = function(originalLineEndings, formattedLineEndings, mapping)
     75{
     76    this._originalLineEndings = originalLineEndings;
     77    this._formattedLineEndings = formattedLineEndings;
     78    this._mapping = mapping;
     79}
     80
     81WebInspector.FormattedSourceMapping.prototype = {
     82    originalToFormatted: function(location)
     83    {
     84        var originalPosition = WebInspector.ScriptFormatter.locationToPosition(this._originalLineEndings, location);
     85        var formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition);
     86        return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition);
     87    },
     88
     89    formattedToOriginal: function(location)
     90    {
     91        var formattedPosition = WebInspector.ScriptFormatter.locationToPosition(this._formattedLineEndings, location);
     92        var originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
     93        return WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition);
     94    },
     95
     96    _convertPosition: function(positions1, positions2, position)
     97    {
     98        var index = positions1.upperBound(position) - 1;
     99        var convertedPosition = positions2[index] + position - positions1[index];
     100        if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
     101            convertedPosition = positions2[index + 1];
     102        return convertedPosition;
     103    }
     104}
  • trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js

    r84625 r86443  
    3535        result = formatter.format(event.data.content);
    3636    } else {
    37         result.mapping = { original: [], formatted: [] };
     37        result.mapping = { original: [0], formatted: [0] };
    3838        result.content = formatScript(event.data.content, result.mapping, 0, 0);
    3939    }
     
    7171        this._content = content;
    7272        this._formattedContent = "";
    73         this._mapping = { original: [], formatted: [] };
     73        this._mapping = { original: [0], formatted: [0] };
    7474        this._position = 0;
    7575
  • trunk/Source/WebCore/inspector/front-end/SourceFile.js

    r84629 r86443  
    255255        function didFormatContent(formattedText, mapping)
    256256        {
    257             this._mapping = new WebInspector.FormattedSourceMapping(this._scripts, mapping.originalLineEndings, formattedText.lineEndings(), mapping);
     257            this._mapping = new WebInspector.SourceMappingForFormattedSourceFile(this._scripts, mapping);
    258258            WebInspector.SourceFile.prototype._didRequestContent.call(this, mimeType, formattedText);
    259259        }
     
    294294}
    295295
    296 WebInspector.FormattedSourceMapping = function(scripts, originalLineEndings, formattedLineEndings, mapping)
     296WebInspector.SourceMappingForFormattedSourceFile = function(scripts, mapping)
    297297{
    298298    WebInspector.SourceMapping.call(this, scripts);
    299     this._originalLineEndings = originalLineEndings;
    300     this._formattedLineEndings = formattedLineEndings;
    301299    this._mapping = mapping;
    302300}
    303301
    304 WebInspector.FormattedSourceMapping.prototype = {
     302WebInspector.SourceMappingForFormattedSourceFile.prototype = {
    305303    scriptLocationToSourceLine: function(location)
    306304    {
    307         var originalPosition = WebInspector.ScriptFormatter.locationToPosition(this._originalLineEndings, location);
    308         var formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition);
    309         return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition).lineNumber;
     305        return this._mapping.originalToFormatted(location).lineNumber;
    310306    },
    311307
    312308    sourceLineToScriptLocation: function(lineNumber)
    313309    {
    314         var formattedPosition = WebInspector.ScriptFormatter.lineToPosition(this._formattedLineEndings, lineNumber);
    315         var originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
    316         var originalLocation = WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition);
     310        var originalLocation = this._mapping.formattedToOriginal({ lineNumber: lineNumber, columnNumber: 0 });
    317311        return WebInspector.SourceMapping.prototype._sourceLocationToScriptLocation.call(this, originalLocation.lineNumber, originalLocation.columnNumber);
    318     },
    319 
    320     _convertPosition: function(positions1, positions2, position)
    321     {
    322         var index = positions1.upperBound(position) - 1;
    323         var delta = position - positions1[index];
    324         return Math.min(positions2[index] + delta, positions2[index + 1]);
    325312    }
    326313}
    327314
    328 WebInspector.FormattedSourceMapping.prototype.__proto__ = WebInspector.SourceMapping.prototype;
     315WebInspector.SourceMappingForFormattedSourceFile.prototype.__proto__ = WebInspector.SourceMapping.prototype;
Note: See TracChangeset for help on using the changeset viewer.