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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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
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.
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.
36 WebInspector.CSSCompletions = function(values, acceptEmptyPrefix)
38 this._values = values.slice();
40 this._acceptEmptyPrefix = acceptEmptyPrefix;
45 * @type {WebInspector.CSSCompletions}
47 WebInspector.CSSCompletions.cssNameCompletions = null;
49 WebInspector.CSSCompletions.requestCSSNameCompletions = function()
51 function propertyNamesCallback(error, names)
54 WebInspector.CSSCompletions.cssNameCompletions = new WebInspector.CSSCompletions(names, false);
56 CSSAgent.getSupportedCSSProperties(propertyNamesCallback);
59 WebInspector.CSSCompletions.prototype = {
60 startsWith: function(prefix)
62 var firstIndex = this._firstIndexOfPrefix(prefix);
63 if (firstIndex === -1)
67 while (firstIndex < this._values.length && this._values[firstIndex].indexOf(prefix) === 0)
68 results.push(this._values[firstIndex++]);
72 firstStartsWith: function(prefix)
74 var foundIndex = this._firstIndexOfPrefix(prefix);
75 return (foundIndex === -1 ? "" : this._values[foundIndex]);
78 _firstIndexOfPrefix: function(prefix)
80 if (!this._values.length)
83 return this._acceptEmptyPrefix ? 0 : -1;
85 var maxIndex = this._values.length - 1;
90 var middleIndex = (maxIndex + minIndex) >> 1;
91 if (this._values[middleIndex].indexOf(prefix) === 0) {
92 foundIndex = middleIndex;
95 if (this._values[middleIndex] < prefix)
96 minIndex = middleIndex + 1;
98 maxIndex = middleIndex - 1;
99 } while (minIndex <= maxIndex);
101 if (foundIndex === undefined)
104 while (foundIndex && this._values[foundIndex - 1].indexOf(prefix) === 0)
113 this._keySet = this._values.keySet();
117 next: function(str, prefix)
119 return this._closest(str, prefix, 1);
122 previous: function(str, prefix)
124 return this._closest(str, prefix, -1);
127 _closest: function(str, prefix, shift)
132 var index = this._values.indexOf(str);
137 index = (index + this._values.length + shift) % this._values.length;
138 return this._values[index];
141 var propertiesWithPrefix = this.startsWith(prefix);
142 var j = propertiesWithPrefix.indexOf(str);
143 j = (j + propertiesWithPrefix.length + shift) % propertiesWithPrefix.length;
144 return propertiesWithPrefix[j];