Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / TextRange.js
1 /*
2  * Copyright (C) 2013 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  * @param {number} startLine
34  * @param {number} startColumn
35  * @param {number} endLine
36  * @param {number} endColumn
37  */
38 WebInspector.TextRange = function(startLine, startColumn, endLine, endColumn)
39 {
40     this.startLine = startLine;
41     this.startColumn = startColumn;
42     this.endLine = endLine;
43     this.endColumn = endColumn;
44 }
45
46 WebInspector.TextRange.createFromLocation = function(line, column)
47 {
48     return new WebInspector.TextRange(line, column, line, column);
49 }
50
51 /**
52  * @param {!Object} serializedTextRange
53  * @return {!WebInspector.TextRange}
54  */
55 WebInspector.TextRange.fromObject = function(serializedTextRange)
56 {
57     return new WebInspector.TextRange(serializedTextRange.startLine, serializedTextRange.startColumn, serializedTextRange.endLine, serializedTextRange.endColumn);
58 }
59
60 WebInspector.TextRange.prototype = {
61     /**
62      * @return {boolean}
63      */
64     isEmpty: function()
65     {
66         return this.startLine === this.endLine && this.startColumn === this.endColumn;
67     },
68
69     /**
70      * @param {!WebInspector.TextRange} range
71      * @return {boolean}
72      */
73     immediatelyPrecedes: function(range)
74     {
75         if (!range)
76             return false;
77         return this.endLine === range.startLine && this.endColumn === range.startColumn;
78     },
79
80     /**
81      * @param {!WebInspector.TextRange} range
82      * @return {boolean}
83      */
84     immediatelyFollows: function(range)
85     {
86         if (!range)
87             return false;
88         return range.immediatelyPrecedes(this);
89     },
90
91     /**
92      * @return {number}
93      */
94     get linesCount()
95     {
96         return this.endLine - this.startLine;
97     },
98
99     /**
100      * @return {!WebInspector.TextRange}
101      */
102     collapseToEnd: function()
103     {
104         return new WebInspector.TextRange(this.endLine, this.endColumn, this.endLine, this.endColumn);
105     },
106
107     /**
108      * @return {!WebInspector.TextRange}
109      */
110     normalize: function()
111     {
112         if (this.startLine > this.endLine || (this.startLine === this.endLine && this.startColumn > this.endColumn))
113             return new WebInspector.TextRange(this.endLine, this.endColumn, this.startLine, this.startColumn);
114         else
115             return this.clone();
116     },
117
118     /**
119      * @return {!WebInspector.TextRange}
120      */
121     clone: function()
122     {
123         return new WebInspector.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn);
124     },
125
126     /**
127      * @return {!Object}
128      */
129     serializeToObject: function()
130     {
131         var serializedTextRange = {};
132         serializedTextRange.startLine = this.startLine;
133         serializedTextRange.startColumn = this.startColumn;
134         serializedTextRange.endLine = this.endLine;
135         serializedTextRange.endColumn = this.endColumn;
136         return serializedTextRange;
137     },
138
139     /**
140      * @param {!WebInspector.TextRange} other
141      * @return {number}
142      */
143     compareTo: function(other)
144     {
145         if (this.startLine > other.startLine)
146             return 1;
147         if (this.startLine < other.startLine)
148             return -1;
149         if (this.startColumn > other.startColumn)
150             return 1;
151         if (this.startColumn < other.startColumn)
152             return -1;
153         return 0;
154     },
155
156     /**
157      * @param {!WebInspector.TextRange} other
158      * @return {boolean}
159      */
160     equal: function(other)
161     {
162         return this.startLine === other.startLine && this.endLine === other.endLine &&
163             this.startColumn === other.startColumn && this.endColumn === other.endColumn;
164     },
165
166     /**
167      * @param {number} lineOffset
168      * @return {!WebInspector.TextRange}
169      */
170     shift: function(lineOffset)
171     {
172         return new WebInspector.TextRange(this.startLine + lineOffset, this.startColumn, this.endLine + lineOffset, this.endColumn);
173     },
174
175     /**
176      * @return {string}
177      */
178     toString: function()
179     {
180         return JSON.stringify(this);
181     }
182 }
183
184 /**
185  * @constructor
186  * @param {number} offset
187  * @param {number} length
188  */
189 WebInspector.SourceRange = function(offset, length)
190 {
191     this.offset = offset;
192     this.length = length;
193 }