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.
35
* @param {Array.<CSSAgent.CSSPropertyInfo|string>} properties
37
WebInspector.CSSCompletions = function(properties)
41
this._shorthands = {};
42
for (var i = 0; i < properties.length; ++i) {
43
var property = properties[i];
44
if (typeof property === "string") {
45
this._values.push(property);
49
var propertyName = property.name;
50
this._values.push(propertyName);
52
var longhands = properties[i].longhands;
54
this._longhands[propertyName] = longhands;
55
for (var j = 0; j < longhands.length; ++j) {
56
var longhandName = longhands[j];
57
var shorthands = this._shorthands[longhandName];
60
this._shorthands[longhandName] = shorthands;
62
shorthands.push(propertyName);
71
* @type {WebInspector.CSSCompletions}
73
WebInspector.CSSCompletions.cssPropertiesMetainfo = null;
75
WebInspector.CSSCompletions.requestCSSNameCompletions = function()
77
function propertyNamesCallback(error, properties)
80
WebInspector.CSSCompletions.cssPropertiesMetainfo = new WebInspector.CSSCompletions(properties);
82
CSSAgent.getSupportedCSSProperties(propertyNamesCallback);
85
WebInspector.CSSCompletions.cssPropertiesMetainfoKeySet = function()
87
if (!WebInspector.CSSCompletions._cssPropertiesMetainfoKeySet)
88
WebInspector.CSSCompletions._cssPropertiesMetainfoKeySet = WebInspector.CSSCompletions.cssPropertiesMetainfo.keySet();
89
return WebInspector.CSSCompletions._cssPropertiesMetainfoKeySet;
92
// Weight of CSS properties based their usage on few popular websites https://gist.github.com/3751436
93
WebInspector.CSSCompletions.Weight = {
94
"-webkit-animation": 1,
95
"-webkit-animation-duration": 1,
96
"-webkit-animation-iteration-count": 1,
97
"-webkit-animation-name": 1,
98
"-webkit-animation-timing-function": 1,
99
"-webkit-appearance": 1,
100
"-webkit-background-clip": 2,
101
"-webkit-border-horizontal-spacing": 1,
102
"-webkit-border-vertical-spacing": 1,
103
"-webkit-box-shadow": 24,
104
"-webkit-font-smoothing": 2,
105
"-webkit-transform": 1,
106
"-webkit-transition": 8,
107
"-webkit-transition-delay": 7,
108
"-webkit-transition-duration": 7,
109
"-webkit-transition-property": 7,
110
"-webkit-transition-timing-function": 6,
111
"-webkit-user-select": 1,
113
"background-attachment": 144,
114
"background-clip": 143,
115
"background-color": 222,
116
"background-image": 201,
117
"background-origin": 142,
118
"background-size": 25,
120
"border-bottom": 121,
121
"border-bottom-color": 121,
122
"border-bottom-left-radius": 50,
123
"border-bottom-right-radius": 50,
124
"border-bottom-style": 114,
125
"border-bottom-width": 120,
126
"border-collapse": 3,
128
"border-left-color": 95,
129
"border-left-style": 89,
130
"border-left-width": 94,
133
"border-right-color": 93,
134
"border-right-style": 88,
135
"border-right-width": 93,
137
"border-top-color": 111,
138
"border-top-left-radius": 49,
139
"border-top-right-radius": 49,
140
"border-top-style": 104,
141
"border-top-width": 109,
163
"list-style-image": 8,
164
"list-style-position": 8,
165
"list-style-type": 17,
167
"margin-bottom": 226,
184
"padding-bottom": 208,
186
"padding-right": 206,
195
"text-decoration": 53,
202
"vertical-align": 37,
212
WebInspector.CSSCompletions.prototype = {
213
startsWith: function(prefix)
215
var firstIndex = this._firstIndexOfPrefix(prefix);
216
if (firstIndex === -1)
220
while (firstIndex < this._values.length && this._values[firstIndex].startsWith(prefix))
221
results.push(this._values[firstIndex++]);
226
* @param {Array.<string>} properties
229
mostUsedOf: function(properties)
233
for (var i = 0; i < properties.length; i++) {
234
var weight = WebInspector.CSSCompletions.Weight[properties[i]];
235
if (weight > maxWeight) {
243
_firstIndexOfPrefix: function(prefix)
245
if (!this._values.length)
250
var maxIndex = this._values.length - 1;
255
var middleIndex = (maxIndex + minIndex) >> 1;
256
if (this._values[middleIndex].startsWith(prefix)) {
257
foundIndex = middleIndex;
260
if (this._values[middleIndex] < prefix)
261
minIndex = middleIndex + 1;
263
maxIndex = middleIndex - 1;
264
} while (minIndex <= maxIndex);
266
if (foundIndex === undefined)
269
while (foundIndex && this._values[foundIndex - 1].startsWith(prefix))
278
this._keySet = this._values.keySet();
282
next: function(str, prefix)
284
return this._closest(str, prefix, 1);
287
previous: function(str, prefix)
289
return this._closest(str, prefix, -1);
292
_closest: function(str, prefix, shift)
297
var index = this._values.indexOf(str);
302
index = (index + this._values.length + shift) % this._values.length;
303
return this._values[index];
306
var propertiesWithPrefix = this.startsWith(prefix);
307
var j = propertiesWithPrefix.indexOf(str);
308
j = (j + propertiesWithPrefix.length + shift) % propertiesWithPrefix.length;
309
return propertiesWithPrefix[j];
313
* @param {string} shorthand
314
* @return {?Array.<string>}
316
longhands: function(shorthand)
318
return this._longhands[shorthand];
322
* @param {string} longhand
323
* @return {?Array.<string>}
325
shorthands: function(longhand)
327
return this._shorthands[longhand];