2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 WebInspector.ScriptFormatter = function()
39 WebInspector.ScriptFormatter.locationToPosition = function(lineEndings, location)
41 var position = location.lineNumber ? lineEndings[location.lineNumber - 1] + 1 : 0;
42 return position + location.columnNumber;
45 WebInspector.ScriptFormatter.positionToLocation = function(lineEndings, position)
48 location.lineNumber = lineEndings.upperBound(position - 1);
49 if (!location.lineNumber)
50 location.columnNumber = position;
52 location.columnNumber = position - lineEndings[location.lineNumber - 1] - 1;
56 WebInspector.ScriptFormatter.prototype = {
57 formatContent: function(mimeType, content, callback)
59 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '');
60 var data = { mimeType: mimeType, content: content, indentString: " " };
61 this._tasks.push({ data: data, callback: callback });
62 this._worker.postMessage(data);
65 _didFormatContent: function(event)
67 var task = this._tasks.shift();
68 var originalContent = task.data.content;
69 var formattedContent = event.data.content;
70 var mapping = event.data["mapping"];
71 var sourceMapping = new WebInspector.FormattedSourceMapping(originalContent.lineEndings(), formattedContent.lineEndings(), mapping);
72 task.callback(formattedContent, sourceMapping);
77 if (!this._cachedWorker) {
78 this._cachedWorker = new Worker("ScriptFormatterWorker.js");
79 this._cachedWorker.onmessage = this._didFormatContent.bind(this);
81 return this._cachedWorker;
88 WebInspector.FormatterMappingPayload = function()
96 * @param {WebInspector.FormatterMappingPayload} mapping
98 WebInspector.FormattedSourceMapping = function(originalLineEndings, formattedLineEndings, mapping)
100 this._originalLineEndings = originalLineEndings;
101 this._formattedLineEndings = formattedLineEndings;
102 this._mapping = mapping;
105 WebInspector.FormattedSourceMapping.prototype = {
106 originalToFormatted: function(location)
108 var originalPosition = WebInspector.ScriptFormatter.locationToPosition(this._originalLineEndings, location);
109 var formattedPosition = this._convertPosition(this._mapping.original, this._mapping.formatted, originalPosition);
110 return WebInspector.ScriptFormatter.positionToLocation(this._formattedLineEndings, formattedPosition);
113 formattedToOriginal: function(location)
115 var formattedPosition = WebInspector.ScriptFormatter.locationToPosition(this._formattedLineEndings, location);
116 var originalPosition = this._convertPosition(this._mapping.formatted, this._mapping.original, formattedPosition);
117 return WebInspector.ScriptFormatter.positionToLocation(this._originalLineEndings, originalPosition);
120 _convertPosition: function(positions1, positions2, position)
122 var index = positions1.upperBound(position) - 1;
123 var convertedPosition = positions2[index] + position - positions1[index];
124 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
125 convertedPosition = positions2[index + 1];
126 return convertedPosition;