Changeset 84620 in webkit
- Timestamp:
- Apr 22, 2011 5:34:33 AM (13 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 1 added
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r84619 r84620 1 2011-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 1 40 2011-04-22 Mikhail Naganov <mnaganov@chromium.org> 2 41 -
trunk/Source/WebCore/WebCore.gypi
r84619 r84620 6312 6312 ], 6313 6313 'webinspector_uglifyjs_files': [ 6314 'inspector/front-end/UglifyJS/JavaScriptFormatter.js', 6314 6315 'inspector/front-end/UglifyJS/parse-js.js', 6315 'inspector/front-end/UglifyJS/process.js',6316 6316 ], 6317 6317 'webinspector_image_files': [ -
trunk/Source/WebCore/WebCore.vcproj/WebCore.vcproj
r84619 r84620 67478 67478 </File> 67479 67479 <File 67480 RelativePath="..\inspector\front-end\UglifyJS\JavaScriptFormatter.js" 67481 > 67482 </File> 67483 <File 67480 67484 RelativePath="..\inspector\front-end\UglifyJS\parse-js.js" 67481 67485 > … … 67495 67499 <File 67496 67500 RelativePath="..\inspector\front-end\Popover.js" 67497 >67498 </File>67499 <File67500 RelativePath="..\inspector\front-end\UglifyJS\process.js"67501 67501 > 67502 67502 </File> -
trunk/Source/WebCore/inspector/front-end/ScriptFormatter.js
r83290 r84620 77 77 formatContent: function(text, scripts, callback) 78 78 { 79 text = text.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''); 79 80 var scriptRanges = WebInspector.ScriptFormatter.findScriptRanges(text.lineEndings(), scripts); 80 81 var chunks = this._splitContentIntoChunks(text, scriptRanges); … … 83 84 { 84 85 var result = this._buildContentFromChunks(chunks); 86 result.mapping.originalLineEndings = text.lineEndings(); 85 87 callback(result.text, result.mapping); 86 88 } -
trunk/Source/WebCore/inspector/front-end/ScriptFormatterWorker.js
r83748 r84620 31 31 onmessage = function(event) { 32 32 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() }); 36 38 }; 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: false46 };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 }68 39 69 40 function require() … … 76 47 var parse = exports; 77 48 78 var exports = {}; 79 importScripts("UglifyJS/process.js"); 80 var process = exports; 49 importScripts("UglifyJS/JavaScriptFormatter.js"); -
trunk/Source/WebCore/inspector/front-end/SourceFile.js
r83748 r84620 138 138 } 139 139 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); 147 142 } 148 143 resource.requestContent(didRequestContent.bind(this)); … … 255 250 function didFormatContent(formattedText, mapping) 256 251 { 257 this._mapping = new WebInspector.FormattedSourceMapping(this._scripts, text, formattedText, mapping);252 this._mapping = new WebInspector.FormattedSourceMapping(this._scripts, mapping.originalLineEndings, formattedText.lineEndings(), mapping); 258 253 WebInspector.SourceFile.prototype._didRequestContent.call(this, mimeType, formattedText); 259 254 } … … 294 289 } 295 290 296 WebInspector.FormattedSourceMapping = function(scripts, original Text, formattedText, mapping)291 WebInspector.FormattedSourceMapping = function(scripts, originalLineEndings, formattedLineEndings, mapping) 297 292 { 298 293 WebInspector.SourceMapping.call(this, scripts); 299 this._originalLineEndings = original Text.lineEndings();300 this._formattedLineEndings = formatted Text.lineEndings();294 this._originalLineEndings = originalLineEndings; 295 this._formattedLineEndings = formattedLineEndings; 301 296 this._mapping = mapping; 302 297 } … … 306 301 { 307 302 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); 310 304 return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition).lineNumber; 311 305 }, … … 314 308 { 315 309 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); 318 311 var originalLocation = WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition); 319 312 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]); 320 320 } 321 321 } -
trunk/Source/WebCore/inspector/front-end/WebKit.qrc
r84619 r84620 124 124 <file>WelcomeView.js</file> 125 125 <file>WorkersSidebarPane.js</file> 126 <file>UglifyJS/JavaScriptFormatter.js</file> 126 127 <file>UglifyJS/parse-js.js</file> 127 <file>UglifyJS/process.js</file>128 128 <file>audits.css</file> 129 129 <file>goToLineDialog.css</file>
Note: See TracChangeset
for help on using the changeset viewer.