tizen beta release
[framework/web/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / ScriptFormatter.js
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 /**
32  * @constructor
33  */
34 WebInspector.ScriptFormatter = function()
35 {
36     this._tasks = [];
37 }
38
39 WebInspector.ScriptFormatter.locationToPosition = function(lineEndings, location)
40 {
41     var position = location.lineNumber ? lineEndings[location.lineNumber - 1] + 1 : 0;
42     return position + location.columnNumber;
43 }
44
45 WebInspector.ScriptFormatter.positionToLocation = function(lineEndings, position)
46 {
47     var location = {};
48     location.lineNumber = lineEndings.upperBound(position - 1);
49     if (!location.lineNumber)
50         location.columnNumber = position;
51     else
52         location.columnNumber = position - lineEndings[location.lineNumber - 1] - 1;
53     return location;
54 }
55
56 WebInspector.ScriptFormatter.prototype = {
57     formatContent: function(mimeType, content, callback)
58     {
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);
63     },
64
65     _didFormatContent: function(event)
66     {
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);
73     },
74
75     get _worker()
76     {
77         if (!this._cachedWorker) {
78             this._cachedWorker = new Worker("ScriptFormatterWorker.js");
79             this._cachedWorker.onmessage = this._didFormatContent.bind(this);
80         }
81         return this._cachedWorker;
82     }
83 }
84
85 /**
86  * @constructor
87  */
88 WebInspector.FormatterMappingPayload = function()
89 {
90     this.original = [];
91     this.formatted = [];
92 }
93
94 /**
95  * @constructor
96  * @param {WebInspector.FormatterMappingPayload} mapping
97  */
98 WebInspector.FormattedSourceMapping = function(originalLineEndings, formattedLineEndings, mapping)
99 {
100     this._originalLineEndings = originalLineEndings;
101     this._formattedLineEndings = formattedLineEndings;
102     this._mapping = mapping;
103 }
104
105 WebInspector.FormattedSourceMapping.prototype = {
106     originalToFormatted: function(location)
107     {
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);
111     },
112
113     formattedToOriginal: function(location)
114     {
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);
118     },
119
120     _convertPosition: function(positions1, positions2, position)
121     {
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;
127     }
128 }