tizen beta release
[framework/web/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / CSSCompletions.js
1 /*
2  * Copyright (C) 2010 Nikita Vasilyev. All rights reserved.
3  * Copyright (C) 2010 Joseph Pecoraro. All rights reserved.
4  * Copyright (C) 2010 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *     * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /**
34  * @constructor
35  */
36 WebInspector.CSSCompletions = function(values, acceptEmptyPrefix)
37 {
38     this._values = values.slice();
39     this._values.sort();
40     this._acceptEmptyPrefix = acceptEmptyPrefix;
41 }
42
43
44 /**
45  * @type {WebInspector.CSSCompletions}
46  */
47 WebInspector.CSSCompletions.cssNameCompletions = null;
48
49 WebInspector.CSSCompletions.requestCSSNameCompletions = function()
50 {
51     function propertyNamesCallback(error, names)
52     {
53         if (!error)
54             WebInspector.CSSCompletions.cssNameCompletions = new WebInspector.CSSCompletions(names, false);
55     }
56     CSSAgent.getSupportedCSSProperties(propertyNamesCallback);
57 }
58
59 WebInspector.CSSCompletions.prototype = {
60     startsWith: function(prefix)
61     {
62         var firstIndex = this._firstIndexOfPrefix(prefix);
63         if (firstIndex === -1)
64             return [];
65
66         var results = [];
67         while (firstIndex < this._values.length && this._values[firstIndex].indexOf(prefix) === 0)
68             results.push(this._values[firstIndex++]);
69         return results;
70     },
71
72     firstStartsWith: function(prefix)
73     {
74         var foundIndex = this._firstIndexOfPrefix(prefix);
75         return (foundIndex === -1 ? "" : this._values[foundIndex]);
76     },
77
78     _firstIndexOfPrefix: function(prefix)
79     {
80         if (!this._values.length)
81             return -1;
82         if (!prefix)
83             return this._acceptEmptyPrefix ? 0 : -1;
84
85         var maxIndex = this._values.length - 1;
86         var minIndex = 0;
87         var foundIndex;
88
89         do {
90             var middleIndex = (maxIndex + minIndex) >> 1;
91             if (this._values[middleIndex].indexOf(prefix) === 0) {
92                 foundIndex = middleIndex;
93                 break;
94             }
95             if (this._values[middleIndex] < prefix)
96                 minIndex = middleIndex + 1;
97             else
98                 maxIndex = middleIndex - 1;
99         } while (minIndex <= maxIndex);
100
101         if (foundIndex === undefined)
102             return -1;
103
104         while (foundIndex && this._values[foundIndex - 1].indexOf(prefix) === 0)
105             foundIndex--;
106
107         return foundIndex;
108     },
109
110     keySet: function()
111     {
112         if (!this._keySet)
113             this._keySet = this._values.keySet();
114         return this._keySet;
115     },
116
117     next: function(str, prefix)
118     {
119         return this._closest(str, prefix, 1);
120     },
121
122     previous: function(str, prefix)
123     {
124         return this._closest(str, prefix, -1);
125     },
126
127     _closest: function(str, prefix, shift)
128     {
129         if (!str)
130             return "";
131
132         var index = this._values.indexOf(str);
133         if (index === -1)
134             return "";
135
136         if (!prefix) {
137             index = (index + this._values.length + shift) % this._values.length;
138             return this._values[index];
139         }
140
141         var propertiesWithPrefix = this.startsWith(prefix);
142         var j = propertiesWithPrefix.indexOf(str);
143         j = (j + propertiesWithPrefix.length + shift) % propertiesWithPrefix.length;
144         return propertiesWithPrefix[j];
145     }
146 }